summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2018-12-11 10:30:32 +0300
committerAndrew Dolgov <[email protected]>2018-12-11 10:30:32 +0300
commitf3c04fc5d85d1e411e731af95074e604e60abafb (patch)
tree5290f99e07312caba8fc4f3aacaed07168ab453d /js
parent25ca144bb775f29dfc152a87c975f1fcdb367174 (diff)
sync modified scores via mutation observer
Diffstat (limited to 'js')
-rw-r--r--js/Article.js65
-rwxr-xr-xjs/Headlines.js21
2 files changed, 48 insertions, 38 deletions
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
- };
-
- xhrJson("backend.php", query, (reply) => {
- if (reply) {
- reply.id.each((id) => {
- const row = $("RROW-" + id);
+ if (parseInt(score) != undefined) {
+ ids.each((id) => {
+ const row = $("RROW-" + id);
- ["score-low", "score-high", "score-half-low", "score-half-high", "score-neutral"]
- .each(function(scl) {
- row.removeClassName(scl);
- });
+ if (row) {
+ row.setAttribute("data-score", score);
- row.addClassName(Article.getScoreClass(reply['score']));
+ const pic = row.select(".icon-score")[0];
- if (row) {
- row.setAttribute("data-score", reply["score"]);
+ pic.innerHTML = Article.getScorePic(score);
+ pic.setAttribute("title", score);
- const pic = row.select(".icon-score")[0];
+ ["score-low", "score-high", "score-half-low", "score-half-high", "score-neutral"]
+ .each(function(scl) {
+ if (row.hasClassName(scl))
+ row.removeClassName(scl);
+ });
- 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 (parseInt(score) != undefined) {
+ row.setAttribute("data-score", score);
- if (row && new_score != undefined) {
- const query = {op: "article", method: "setScore", id: id, score: new_score};
+ const pic = row.select(".icon-score")[0];
- xhrJson("backend.php", query, (reply) => {
- if (reply) {
- pic.innerHTML = reply["score_pic"];
- row.setAttribute("title", new_score);
+ pic.innerHTML = Article.getScorePic(score);
+ pic.setAttribute("title", score);
- ["score-low", "score-high", "score-half-low", "score-half-high", "score-neutral"]
- .each(function(scl) {
+ ["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);