From f3c04fc5d85d1e411e731af95074e604e60abafb Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Tue, 11 Dec 2018 10:30:32 +0300 Subject: [PATCH] sync modified scores via mutation observer --- classes/feeds.php | 5 ++-- js/Article.js | 67 ++++++++++++++++++++--------------------------- js/Headlines.js | 21 ++++++++++++++- 3 files changed, 51 insertions(+), 42 deletions(-) diff --git a/classes/feeds.php b/classes/feeds.php index 68cf52f12..ed0789788 100755 --- a/classes/feeds.php +++ b/classes/feeds.php @@ -342,8 +342,6 @@ class Feeds extends Handler_Protected { $line['imported'] = T_sprintf("Imported at %s", make_local_datetime($line["date_entered"], false)); - $score = $line["score"]; - if ($line["tag_cache"]) $tags = explode(",", $line["tag_cache"]); else @@ -372,7 +370,8 @@ class Feeds extends Handler_Protected { /* we don't need those */ - foreach (["date_entered", "guid", "last_published", "last_marked", "tag_cache", "favicon_avg_color", "uuid", "label_cache"] as $k) + foreach (["date_entered", "guid", "last_published", "last_marked", "tag_cache", "favicon_avg_color", + "uuid", "label_cache", "yyiw"] as $k) unset($line[$k]); array_push($reply['content'], $line); diff --git a/js/Article.js b/js/Article.js index cfba3804b..e281ea5ae 100644 --- a/js/Article.js +++ b/js/Article.js @@ -30,37 +30,27 @@ define(["dojo/_base/declare"], function (declare) { const ids = Headlines.getSelected(); if (ids.length > 0) { - console.log(ids); - const score = prompt(__("Please enter new score for selected articles:")); - if (score != undefined) { - const query = { - op: "article", method: "setScore", id: ids.toString(), - score: score - }; + if (parseInt(score) != undefined) { + ids.each((id) => { + const row = $("RROW-" + id); - xhrJson("backend.php", query, (reply) => { - if (reply) { - reply.id.each((id) => { - const row = $("RROW-" + id); + if (row) { + row.setAttribute("data-score", score); - ["score-low", "score-high", "score-half-low", "score-half-high", "score-neutral"] - .each(function(scl) { + const pic = row.select(".icon-score")[0]; + + pic.innerHTML = Article.getScorePic(score); + pic.setAttribute("title", score); + + ["score-low", "score-high", "score-half-low", "score-half-high", "score-neutral"] + .each(function(scl) { + if (row.hasClassName(scl)) row.removeClassName(scl); - }); + }); - row.addClassName(Article.getScoreClass(reply['score'])); - - if (row) { - row.setAttribute("data-score", reply["score"]); - - const pic = row.select(".icon-score")[0]; - - pic.innerHTML = reply["score_pic"]; - pic.setAttribute("title", reply["score"]); - } - }); + row.addClassName(Article.getScoreClass(score)); } }); } @@ -71,26 +61,27 @@ define(["dojo/_base/declare"], function (declare) { }, setScore: function (id, pic) { const row = pic.up("div[id*=RROW]"); - const score = row.getAttribute("data-score"); - const new_score = prompt(__("Please enter new score for this article:"), score); + if (row) { + const score_old = row.getAttribute("data-score"); + const score = prompt(__("Please enter new score for this article:"), score_old); - if (row && new_score != undefined) { - const query = {op: "article", method: "setScore", id: id, score: new_score}; + if (parseInt(score) != undefined) { + row.setAttribute("data-score", score); - xhrJson("backend.php", query, (reply) => { - if (reply) { - pic.innerHTML = reply["score_pic"]; - row.setAttribute("title", new_score); + const pic = row.select(".icon-score")[0]; - ["score-low", "score-high", "score-half-low", "score-half-high", "score-neutral"] - .each(function(scl) { + pic.innerHTML = Article.getScorePic(score); + pic.setAttribute("title", score); + + ["score-low", "score-high", "score-half-low", "score-half-high", "score-neutral"] + .each(function(scl) { + if (row.hasClassName(scl)) row.removeClassName(scl); }); - row.addClassName(Article.getScoreClass(reply['score'])); - } - }); + row.addClassName(Article.getScoreClass(score)); + } } }, cdmUnsetActive: function (event) { diff --git a/js/Headlines.js b/js/Headlines.js index 64b3bd69d..2f056aef6 100755 --- a/js/Headlines.js +++ b/js/Headlines.js @@ -10,7 +10,7 @@ define(["dojo/_base/declare"], function (declare) { const modified = []; mutations.each((m) => { - if (m.type == 'attributes' && m.attributeName == 'class') { + if (m.type == 'attributes' && ['class', 'data-score'].indexOf(m.attributeName) != -1) { const row = m.target; const id = row.getAttribute("data-article-id"); @@ -29,6 +29,8 @@ define(["dojo/_base/declare"], function (declare) { hl.selected = row.hasClassName("Selected"); hl.active = row.hasClassName("active"); + hl.score = row.getAttribute("data-score"); + modified.push({id: hl.id, new: hl, old: hl_old, row: row}); } } @@ -52,6 +54,7 @@ define(["dojo/_base/declare"], function (declare) { deselect: [], activate: [], deactivate: [], + rescore: {}, }; modified.each(function(m) { @@ -69,6 +72,13 @@ define(["dojo/_base/declare"], function (declare) { if (m.old.active != m.new.active) m.new.active ? ops.activate.push(m.row) : ops.deactivate.push(m.row); + + if (m.old.score != m.new.score) { + const score = m.new.score; + + ops.rescore[score] = ops.rescore[score] || []; + ops.rescore[score].push(m.id); + } }); ops.select.each((row) => { @@ -117,6 +127,15 @@ define(["dojo/_base/declare"], function (declare) { promises.push(xhrPost("backend.php", { op: "rpc", method: "catchupSelected", ids: ops.unread.toString(), cmode: 1})); + const scores = Object.keys(ops.rescore); + + if (scores.length != 0) { + scores.each((score) => { + promises.push(xhrPost("backend.php", + { op: "article", method: "setScore", id: ops.rescore[score].toString(), score: score })); + }); + } + if (promises.length > 0) Promise.all([promises]).then(() => { Feeds.requestCounters(true);