From 807ff074540575e6ef8f99ad32b098a816091171 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 2 Dec 2018 17:18:59 +0300 Subject: split main objects to dojo modules --- js/Article.js | 335 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 335 insertions(+) create mode 100644 js/Article.js (limited to 'js/Article.js') diff --git a/js/Article.js b/js/Article.js new file mode 100644 index 000000000..89bb3b3af --- /dev/null +++ b/js/Article.js @@ -0,0 +1,335 @@ +define(["dojo/_base/declare"], function (declare) { + return declare("fox.Article", null, { + _active_article_id: 0, + selectionSetScore: function() { + 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 (row) { + const pic = row.getElementsByClassName("score-pic")[0]; + + if (pic) { + pic.src = pic.src.replace(/score_.*?\.png/, + reply["score_pic"]); + pic.setAttribute("score", reply["score"]); + } + } + }); + } + }); + } + + } else { + alert(__("No articles selected.")); + } + }, + setScore: function(id, pic) { + const score = pic.getAttribute("score"); + + const new_score = prompt(__("Please enter new score for this article:"), score); + + if (new_score != undefined) { + const query = {op: "article", method: "setScore", id: id, score: new_score}; + + xhrJson("backend.php", query, (reply) => { + if (reply) { + pic.src = pic.src.replace(/score_.*?\.png/, reply["score_pic"]); + pic.setAttribute("score", new_score); + pic.setAttribute("title", new_score); + } + }); + } + }, + cdmUnsetActive: function(event) { + const row = $("RROW-" + Article.getActive()); + + if (row) { + row.removeClassName("active"); + const cb = dijit.getEnclosingWidget(row.select(".rchk")[0]); + + if (cb && !row.hasClassName("Selected")) + cb.attr("checked", false); + + Article.setActive(0); + + if (event) + event.stopPropagation(); + + return false; + } + }, + close: function () { + if (dijit.byId("content-insert")) + dijit.byId("headlines-wrap-inner").removeChild( + dijit.byId("content-insert")); + }, + displayUrl: function (id) { + const query = {op: "rpc", method: "getlinktitlebyid", id: id}; + + xhrJson("backend.php", query, (reply) => { + if (reply && reply.link) { + prompt(__("Article URL:"), reply.link); + } + }); + }, + openInNewWindow: function (id) { + const w = window.open(""); + w.opener = null; + w.location = "backend.php?op=article&method=redirect&id=" + id; + + Article.setActive(id); + }, + render: function (article) { + Utils.cleanupMemory("content-insert"); + + dijit.byId("headlines-wrap-inner").addChild( + dijit.byId("content-insert")); + + const c = dijit.byId("content-insert"); + + try { + c.domNode.scrollTop = 0; + } catch (e) { + } + + c.attr('content', article); + PluginHost.run(PluginHost.HOOK_ARTICLE_RENDERED, c.domNode); + + Headlines.correctHeadlinesOffset(Article.getActive()); + + try { + c.focus(); + } catch (e) { + } + }, + view: function(id, noexpand) { + this.setActive(id); + + if (!noexpand) { + console.log("loading article", id); + + const cids = []; + + /* only request uncached articles */ + + this.getRelativeIds(id).each((n) => { + if (!ArticleCache.get(n)) + cids.push(n); + }); + + const cached_article = ArticleCache.get(id); + + if (cached_article) { + console.log('rendering cached', id); + this.render(cached_article); + return false; + } + + xhrPost("backend.php", {op: "article", method: "view", id: id, cids: cids.toString()}, (transport) => { + try { + const reply = Utils.handleRpcJson(transport); + + if (reply) { + + reply.each(function (article) { + if (Article.getActive() == article['id']) { + Article.render(article['content']); + } + ArticleCache.set(article['id'], article['content']); + }); + + } else { + console.error("Invalid object received: " + transport.responseText); + + Article.render("
" + + __('Could not display article (invalid object received - see error console for details)') + "
"); + } + + //const unread_in_buffer = $$("#headlines-frame > div[id*=RROW][class*=Unread]").length; + //request_counters(unread_in_buffer == 0); + + notify(""); + + } catch (e) { + exception_error(e); + } + }) + } + + return false; + }, + editTags: function(id) { + const query = "backend.php?op=article&method=editArticleTags¶m=" + param_escape(id); + + if (dijit.byId("editTagsDlg")) + dijit.byId("editTagsDlg").destroyRecursive(); + + const dialog = new dijit.Dialog({ + id: "editTagsDlg", + title: __("Edit article Tags"), + style: "width: 600px", + execute: function () { + if (this.validate()) { + notify_progress("Saving article tags...", true); + + xhrPost("backend.php", this.attr('value'), (transport) => { + try { + notify(''); + dialog.hide(); + + const data = JSON.parse(transport.responseText); + + if (data) { + const id = data.id; + + const tags = $("ATSTR-" + id); + const tooltip = dijit.byId("ATSTRTIP-" + id); + + if (tags) tags.innerHTML = data.content; + if (tooltip) tooltip.attr('label', data.content_full); + } + } catch (e) { + exception_error(e); + } + }); + } + }, + href: query + }); + + const tmph = dojo.connect(dialog, 'onLoad', function () { + dojo.disconnect(tmph); + + new Ajax.Autocompleter('tags_str', 'tags_choices', + "backend.php?op=article&method=completeTags", + {tokens: ',', paramName: "search"}); + }); + + dialog.show(); + }, + cdmScrollToId: function(id, force) { + const ctr = $("headlines-frame"); + const e = $("RROW-" + id); + + if (!e || !ctr) return; + + if (force || e.offsetTop + e.offsetHeight > (ctr.scrollTop + ctr.offsetHeight) || + e.offsetTop < ctr.scrollTop) { + + // expanded cdm has a 4px margin now + ctr.scrollTop = parseInt(e.offsetTop) - 4; + + Element.hide("floatingTitle"); + } + }, + setActive: function(id) { + console.log("setActive", id); + + $$("div[id*=RROW][class*=active]").each((e) => { + e.removeClassName("active"); + + if (!e.hasClassName("Selected")) { + const cb = dijit.getEnclosingWidget(e.select(".rchk")[0]); + if (cb) cb.attr("checked", false); + } + }); + + this._active_article_id = id; + + const row = $("RROW-" + id); + + if (row) { + if (row.hasAttribute("data-content")) { + console.log("unpacking: " + row.id); + + row.select(".content-inner")[0].innerHTML = row.getAttribute("data-content"); + row.removeAttribute("data-content"); + + PluginHost.run(PluginHost.HOOK_ARTICLE_RENDERED_CDM, row); + } + + if (row.hasClassName("Unread")) { + + Headlines.catchupBatched(() => { + Feeds.decrementFeedCounter(Feeds.getActive(), Feeds.activeIsCat()); + Headlines.toggleUnread(id, 0); + Headlines.updateFloatingTitle(true); + }); + + } + + row.addClassName("active"); + + if (!row.hasClassName("Selected")) { + const cb = dijit.getEnclosingWidget(row.select(".rchk")[0]); + if (cb) cb.attr("checked", true); + } + + PluginHost.run(PluginHost.HOOK_ARTICLE_SET_ACTIVE, this._active_article_id); + } + + Headlines.updateSelectedPrompt(); + }, + getActive: function() { + return this._active_article_id; + }, + scroll: function(offset) { + if (!App.isCombinedMode()) { + const ci = $("content-insert"); + if (ci) { + ci.scrollTop += offset; + } + } else { + const hi = $("headlines-frame"); + if (hi) { + hi.scrollTop += offset; + } + + } + }, + getRelativeIds: function(id, limit) { + + const tmp = []; + + if (!limit) limit = 6; //3 + + const ids = Headlines.getLoaded(); + + for (let i = 0; i < ids.length; i++) { + if (ids[i] == id) { + for (let k = 1; k <= limit; k++) { + //if (i > k-1) tmp.push(ids[i-k]); + if (i < ids.length - k) tmp.push(ids[i + k]); + } + break; + } + } + + return tmp; + }, + mouseIn: function(id) { + this.post_under_pointer = id; + }, + mouseOut: function(id) { + this.post_under_pointer = false; + }, + getUnderPointer: function() { + return this.post_under_pointer; + } + }); +}); \ No newline at end of file -- cgit v1.2.3 From f89924f7a19871e26d5805a6c1863903c6e474bf Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 2 Dec 2018 18:38:27 +0300 Subject: set use strict on JS modules; remove some mostly useless stuff like get_minified_js() --- js/Article.js | 2 ++ 1 file changed, 2 insertions(+) (limited to 'js/Article.js') diff --git a/js/Article.js b/js/Article.js index 89bb3b3af..0e1318948 100644 --- a/js/Article.js +++ b/js/Article.js @@ -1,3 +1,5 @@ +'use strict' +/* global __, ngettext */ define(["dojo/_base/declare"], function (declare) { return declare("fox.Article", null, { _active_article_id: 0, -- cgit v1.2.3 From d9c5c93cef313127b9b5010fbe279fdbddedadec Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 2 Dec 2018 20:07:57 +0300 Subject: move some more stuff out of common.js rework client-side cookie functions a bit limit dojo cachebust based on server scripts modification time remove param_escape() --- js/Article.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'js/Article.js') diff --git a/js/Article.js b/js/Article.js index 0e1318948..7e5241fa5 100644 --- a/js/Article.js +++ b/js/Article.js @@ -176,7 +176,7 @@ define(["dojo/_base/declare"], function (declare) { return false; }, editTags: function(id) { - const query = "backend.php?op=article&method=editArticleTags¶m=" + param_escape(id); + const query = "backend.php?op=article&method=editArticleTags¶m=" + encodeURIComponent(id); if (dijit.byId("editTagsDlg")) dijit.byId("editTagsDlg").destroyRecursive(); -- cgit v1.2.3 From 526389b2d380177490605037fdc3a24ebc688fac Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 2 Dec 2018 20:56:30 +0300 Subject: update notify_* calls to use Notify --- js/Article.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'js/Article.js') diff --git a/js/Article.js b/js/Article.js index 7e5241fa5..b48d82489 100644 --- a/js/Article.js +++ b/js/Article.js @@ -165,7 +165,7 @@ define(["dojo/_base/declare"], function (declare) { //const unread_in_buffer = $$("#headlines-frame > div[id*=RROW][class*=Unread]").length; //request_counters(unread_in_buffer == 0); - notify(""); + Notify.close(); } catch (e) { exception_error(e); @@ -187,11 +187,11 @@ define(["dojo/_base/declare"], function (declare) { style: "width: 600px", execute: function () { if (this.validate()) { - notify_progress("Saving article tags...", true); + Notify.progress("Saving article tags...", true); xhrPost("backend.php", this.attr('value'), (transport) => { try { - notify(''); + Notify.close(); dialog.hide(); const data = JSON.parse(transport.responseText); -- cgit v1.2.3 From 5ead558e435dd8d58f6666b445374b0005a60337 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 2 Dec 2018 22:08:18 +0300 Subject: move Utils to AppBase where it belongs --- js/Article.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'js/Article.js') diff --git a/js/Article.js b/js/Article.js index b48d82489..1a60f8740 100644 --- a/js/Article.js +++ b/js/Article.js @@ -97,7 +97,7 @@ define(["dojo/_base/declare"], function (declare) { Article.setActive(id); }, render: function (article) { - Utils.cleanupMemory("content-insert"); + App.cleanupMemory("content-insert"); dijit.byId("headlines-wrap-inner").addChild( dijit.byId("content-insert")); @@ -144,7 +144,7 @@ define(["dojo/_base/declare"], function (declare) { xhrPost("backend.php", {op: "article", method: "view", id: id, cids: cids.toString()}, (transport) => { try { - const reply = Utils.handleRpcJson(transport); + const reply = App.handleRpcJson(transport); if (reply) { -- cgit v1.2.3 From 84affc7b1d71769038dfd25d74e40d6bf744e5fb Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Mon, 3 Dec 2018 09:33:44 +0300 Subject: rework dojo singleton modules to better work with phpstorm completion (ugh) - declare() is not needed there anyway remove event.observe from login form (not needed) load pluginhost via amd --- js/Article.js | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) (limited to 'js/Article.js') diff --git a/js/Article.js b/js/Article.js index 1a60f8740..d3ae8eed7 100644 --- a/js/Article.js +++ b/js/Article.js @@ -1,9 +1,9 @@ 'use strict' /* global __, ngettext */ define(["dojo/_base/declare"], function (declare) { - return declare("fox.Article", null, { + Article = { _active_article_id: 0, - selectionSetScore: function() { + selectionSetScore: function () { const ids = Headlines.getSelected(); if (ids.length > 0) { @@ -40,7 +40,7 @@ define(["dojo/_base/declare"], function (declare) { alert(__("No articles selected.")); } }, - setScore: function(id, pic) { + setScore: function (id, pic) { const score = pic.getAttribute("score"); const new_score = prompt(__("Please enter new score for this article:"), score); @@ -57,7 +57,7 @@ define(["dojo/_base/declare"], function (declare) { }); } }, - cdmUnsetActive: function(event) { + cdmUnsetActive: function (event) { const row = $("RROW-" + Article.getActive()); if (row) { @@ -119,7 +119,7 @@ define(["dojo/_base/declare"], function (declare) { } catch (e) { } }, - view: function(id, noexpand) { + view: function (id, noexpand) { this.setActive(id); if (!noexpand) { @@ -175,7 +175,7 @@ define(["dojo/_base/declare"], function (declare) { return false; }, - editTags: function(id) { + editTags: function (id) { const query = "backend.php?op=article&method=editArticleTags¶m=" + encodeURIComponent(id); if (dijit.byId("editTagsDlg")) @@ -224,7 +224,7 @@ define(["dojo/_base/declare"], function (declare) { dialog.show(); }, - cdmScrollToId: function(id, force) { + cdmScrollToId: function (id, force) { const ctr = $("headlines-frame"); const e = $("RROW-" + id); @@ -239,7 +239,7 @@ define(["dojo/_base/declare"], function (declare) { Element.hide("floatingTitle"); } }, - setActive: function(id) { + setActive: function (id) { console.log("setActive", id); $$("div[id*=RROW][class*=active]").each((e) => { @@ -287,10 +287,10 @@ define(["dojo/_base/declare"], function (declare) { Headlines.updateSelectedPrompt(); }, - getActive: function() { + getActive: function () { return this._active_article_id; }, - scroll: function(offset) { + scroll: function (offset) { if (!App.isCombinedMode()) { const ci = $("content-insert"); if (ci) { @@ -304,7 +304,7 @@ define(["dojo/_base/declare"], function (declare) { } }, - getRelativeIds: function(id, limit) { + getRelativeIds: function (id, limit) { const tmp = []; @@ -324,14 +324,16 @@ define(["dojo/_base/declare"], function (declare) { return tmp; }, - mouseIn: function(id) { + mouseIn: function (id) { this.post_under_pointer = id; }, - mouseOut: function(id) { + mouseOut: function (id) { this.post_under_pointer = false; }, - getUnderPointer: function() { + getUnderPointer: function () { return this.post_under_pointer; } - }); + } + + return Article; }); \ No newline at end of file -- cgit v1.2.3 From 71fc6d45bd761a9d2715faa68f2b8c0271ee7169 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Mon, 3 Dec 2018 13:38:13 +0300 Subject: refactor error reporting to AppBase; keep exception_error() for now as a shim --- js/Article.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'js/Article.js') diff --git a/js/Article.js b/js/Article.js index d3ae8eed7..04cba8ab7 100644 --- a/js/Article.js +++ b/js/Article.js @@ -168,7 +168,7 @@ define(["dojo/_base/declare"], function (declare) { Notify.close(); } catch (e) { - exception_error(e); + App.Error.report(e); } }) } @@ -206,7 +206,7 @@ define(["dojo/_base/declare"], function (declare) { if (tooltip) tooltip.attr('label', data.content_full); } } catch (e) { - exception_error(e); + App.Error.report(e); } }); } -- cgit v1.2.3