From 0d27227359df5597d4dc3b646eaa63082f42e4f0 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Thu, 29 Nov 2018 22:21:09 +0300 Subject: [PATCH] use xhrPost in more places; various minor cleanup --- index.php | 3 - js/feedlist.js | 103 +++++++++++------------------- js/functions.js | 50 +++++++++------ js/tt-rss.js | 166 +++++++++++++++--------------------------------- js/viewfeed.js | 53 +++++----------- 5 files changed, 134 insertions(+), 241 deletions(-) diff --git a/index.php b/index.php index f3e5928d..3b0962f0 100644 --- a/index.php +++ b/index.php @@ -245,15 +245,12 @@
-
-
{ + + xhrPost("backend.php", query, (transport) => { + try { + setFeedExpandoIcon(feed, is_cat, 'images/blank_icon.gif'); + headlines_callback2(transport, offset, background, infscroll_req); + PluginHost.run(PluginHost.HOOK_FEED_LOADED, [feed, is_cat]); + } catch (e) { + exception_error(e); + } + }); + }, timeout_ms); // Wait 250ms } @@ -235,18 +225,14 @@ function request_counters(force) { counters_last_request = timestamp; - let query = "?op=rpc&method=getAllCounters&seq=" + next_seq(); + let query = {op: "rpc", method: "getAllCounters", seq: next_seq()}; if (!force) - query = query + "&last_article_id=" + getInitParam("last_article_id"); + query.last_article_id = getInitParam("last_article_id"); - console.log(query); - - new Ajax.Request("backend.php", { - parameters: query, - onComplete: function(transport) { - handle_rpc_json(transport); - } }); + xhrPost("backend.php", query, (transport) => { + handle_rpc_json(transport); + }); } else { console.log("request_counters: rate limit reached: " + (timestamp - counters_last_request)); @@ -481,21 +467,11 @@ function catchupFeedInGroup(id) { updateFloatingTitle(true); } - const catchup_query = "?op=rpc&method=catchupFeed&feed_id=" + - id + "&is_cat=false"; - - console.log(catchup_query); - notify_progress("Loading, please wait...", true); - new Ajax.Request("backend.php", { - parameters: catchup_query, - onComplete: function (transport) { - handle_rpc_json(transport); - } - } ); - - //return viewCurrentFeed('MarkAllReadGR:' + id); + xhrPost("backend.php", { op: "rpc", method: "catchupFeed", feed_id: id, is_cat: false}, (transport) => { + handle_rpc_json(transport); + }); } } @@ -532,30 +508,25 @@ function catchupFeed(feed, is_cat, mode) { is_cat: is_cat, mode: mode, search_query: last_search_query[0], search_lang: last_search_query[1]}; - console.log(catchup_query); - notify_progress("Loading, please wait...", true); - new Ajax.Request("backend.php", { - parameters: catchup_query, - onComplete: function(transport) { - handle_rpc_json(transport); + xhrPost("backend.php", catchup_query, (transport) => { + handle_rpc_json(transport); - const show_next_feed = getInitParam("on_catchup_show_next_feed") == "1"; + const show_next_feed = getInitParam("on_catchup_show_next_feed") == "1"; - if (show_next_feed) { - const nuf = getNextUnreadFeed(feed, is_cat); + if (show_next_feed) { + const nuf = getNextUnreadFeed(feed, is_cat); - if (nuf) { - viewfeed({feed: nuf, is_cat: is_cat}); - } - } else if (feed == getActiveFeedId() && is_cat == activeFeedIsCat()) { - viewCurrentFeed(); - } - - notify(""); - } }); + if (nuf) { + viewfeed({feed: nuf, is_cat: is_cat}); + } + } else if (feed == getActiveFeedId() && is_cat == activeFeedIsCat()) { + viewCurrentFeed(); + } + notify(""); + }); } function decrementFeedCounter(feed, is_cat) { diff --git a/js/functions.js b/js/functions.js index 25dc587d..7fbb4e75 100755 --- a/js/functions.js +++ b/js/functions.js @@ -1,10 +1,9 @@ /* global dijit, __ */ -var loading_progress = 0; -var sanity_check_done = false; -var init_params = {}; -var _label_base_index = -1024; -var notify_hide_timerid = false; +let init_params = {}; +let _label_base_index = -1024; +let loading_progress = 0; +let notify_hide_timerid = false; Ajax.Base.prototype.initialize = Ajax.Base.prototype.initialize.wrap( function (callOriginal, options) { @@ -24,6 +23,29 @@ Ajax.Base.prototype.initialize = Ajax.Base.prototype.initialize.wrap( } ); +/* xhr shorthand helpers */ + +function xhrPost(url, params, complete) { + console.log("xhrPost:", params); + new Ajax.Request(url, { + parameters: params, + onComplete: complete + }); +} + +function xhrJson(url, params, complete) { + xhrPost(url, params, (reply) => { + try { + const obj = JSON.parse(reply.responseText); + complete(obj); + } catch (e) { + console.error("xhrJson", e, reply); + complete(null); + } + + }) +} + /* add method to remove element from array */ Array.prototype.remove = function(s) { @@ -46,14 +68,14 @@ function exception_error(e, e_compat, filename, lineno, colno) { const msg = e.toString(); try { - new Ajax.Request("backend.php", { - parameters: {op: "rpc", method: "log", + xhrPost("backend.php", + {op: "rpc", method: "log", file: e.fileName ? e.fileName : filename, line: e.lineNumber ? e.lineNumber : lineno, msg: msg, context: e.stack}, - onComplete: function (transport) { + (transport) => { console.warn(transport.responseText); - } }); + }); } catch (e) { console.error("Exception while trying to log the error.", e); @@ -1082,13 +1104,6 @@ function unsubscribeFeed(feed_id, title) { function backend_sanity_check_callback(transport) { - if (sanity_check_done) { - fatalError(11, "Sanity check request received twice. This can indicate "+ - "presence of Firebug or some other disrupting extension. "+ - "Please disable it and try again."); - return; - } - const reply = JSON.parse(transport.responseText); if (!reply) { @@ -1120,10 +1135,7 @@ function backend_sanity_check_callback(transport) { window.PluginHost && PluginHost.run(PluginHost.HOOK_PARAMS_LOADED, init_params); } - sanity_check_done = true; - init_second_stage(); - } function genUrlChangeKey(feed, is_cat) { diff --git a/js/tt-rss.js b/js/tt-rss.js index 9ff87d65..56e89add 100644 --- a/js/tt-rss.js +++ b/js/tt-rss.js @@ -19,7 +19,7 @@ function get_seq() { } function activeFeedIsCat() { - return _active_feed_is_cat; + return !!_active_feed_is_cat; } function getActiveFeedId() { @@ -113,18 +113,12 @@ function catchupAllFeeds() { if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) { - const query_str = "backend.php?op=feeds&method=catchupAll"; - notify_progress("Marking all feeds as read..."); - //console.log("catchupAllFeeds Q=" + query_str); - - new Ajax.Request("backend.php", { - parameters: query_str, - onComplete: function(transport) { - request_counters(true); - viewCurrentFeed(); - } }); + xhrPost("backend.php", {op: "feeds", method: "catchupAll"}, () => { + request_counters(true); + viewCurrentFeed(); + }); global_unread = 0; updateTitle(""); @@ -253,17 +247,21 @@ function init() { init_hotkey_actions(); - new Ajax.Request("backend.php", { - parameters: { - op: "rpc", method: "sanityCheck", hasAudio: hasAudio, - hasMp3: hasMp3, - clientTzOffset: clientTzOffset, - hasSandbox: hasSandbox - }, - onComplete: function (transport) { - backend_sanity_check_callback(transport); - } + const params = { + op: "rpc", method: "sanityCheck", hasAudio: hasAudio, + hasMp3: hasMp3, + clientTzOffset: clientTzOffset, + hasSandbox: hasSandbox + }; + + xhrPost("backend.php", params, (transport) => { + try { + backend_sanity_check_callback(transport); + } catch (e) { + console.error(e); + } }); + } catch (e) { exception_error(e); } @@ -443,14 +441,9 @@ function init_hotkey_actions() { reverseHeadlineOrder(); }; hotkey_actions["feed_toggle_vgroup"] = function() { - const query_str = "?op=rpc&method=togglepref&key=VFEED_GROUP_BY_FEED"; - - new Ajax.Request("backend.php", { - parameters: query_str, - onComplete: function(transport) { - viewCurrentFeed(); - } }); - + xhrPost("backend.php", {op: "rpc", method: "togglepref", key: "VFEED_GROUP_BY_FEED"}, () => { + viewCurrentFeed(); + }) }; hotkey_actions["catchup_all"] = function() { catchupAllFeeds(); @@ -533,31 +526,24 @@ function init_hotkey_actions() { notify_progress("Loading, please wait..."); const value = isCdmMode() ? "false" : "true"; - const query = "?op=rpc&method=setpref&key=COMBINED_DISPLAY_MODE&value=" + value; - new Ajax.Request("backend.php", { - parameters: query, - onComplete: function(transport) { - setInitParam("combined_display_mode", - !getInitParam("combined_display_mode")); - - closeArticlePanel(); - viewCurrentFeed(); + xhrPost("backend.php", {op: "rpc", method: "setpref", key: "COMBINED_DISPLAY_MODE", value: value}, () => { + setInitParam("combined_display_mode", + !getInitParam("combined_display_mode")); - } }); + closeArticlePanel(); + viewCurrentFeed(); + }) }; hotkey_actions["toggle_cdm_expanded"] = function() { notify_progress("Loading, please wait..."); - const value = getInitParam("cdm_expanded") ? "false" : "true"; - const query = "?op=rpc&method=setpref&key=CDM_EXPANDED&value=" + value; + const value = getInitParam("cdm_expanded") ? "false" : "true"; - new Ajax.Request("backend.php", { - parameters: query, - onComplete: function(transport) { - setInitParam("cdm_expanded", !getInitParam("cdm_expanded")); - viewCurrentFeed(); - } }); + xhrPost("backend.php", {op: "rpc", method: "setpref", key: "CDM_EXPANDED", value: value}, () => { + setInitParam("cdm_expanded", !getInitParam("cdm_expanded")); + viewCurrentFeed(); + }); }; } @@ -685,15 +671,6 @@ function quickMenuGo(opid) { case "qmcShowOnlyUnread": toggleDispRead(); break; - case "qmcAddFilter": - quickAddFilter(); - break; - case "qmcAddLabel": - addLabel(); - break; - case "qmcRescoreFeed": - rescoreCurrentFeed(); - break; case "qmcToggleWidescreen": if (!isCdmMode()) { _widescreen_mode = !_widescreen_mode; @@ -719,18 +696,10 @@ function toggleDispRead() { const hide = !(getInitParam("hide_read_feeds") == "1"); - hideOrShowFeeds(hide); - - const query = "?op=rpc&method=setpref&key=HIDE_READ_FEEDS&value=" + - param_escape(hide); - - setInitParam("hide_read_feeds", hide); - - new Ajax.Request("backend.php", { - parameters: query, - onComplete: function(transport) { - } }); - + xhrPost("backend.php", {op: "rpc", method: "setpref", key: "HIDE_READ_FEEDS", value: hide}, () => { + hideOrShowFeeds(hide); + setInitParam("hide_read_feeds", hide); + }); } function parse_runtime_info(data) { @@ -797,36 +766,6 @@ function viewModeChanged() { return viewCurrentFeed(''); } -function rescoreCurrentFeed() { - - const actid = getActiveFeedId(); - - if (activeFeedIsCat() || actid < 0) { - alert(__("You can't rescore this kind of feed.")); - return; - } - - if (!actid) { - alert(__("Please select some feed first.")); - return; - } - - const fn = getFeedName(actid); - const pr = __("Rescore articles in %s?").replace("%s", fn); - - if (confirm(pr)) { - notify_progress("Rescoring articles..."); - - const query = "?op=pref-feeds&method=rescore&quiet=1&ids=" + actid; - - new Ajax.Request("backend.php", { - parameters: query, - onComplete: function() { - viewCurrentFeed(); - } }); - } -} - function hotkey_handler(e) { if (e.target.nodeName == "INPUT" || e.target.nodeName == "TEXTAREA") return; @@ -966,11 +905,14 @@ function handle_rpc_json(transport, scheduled_call) { if (netalert) netalert.hide(); - } else - if (netalert) - netalert.show(); - else - notify_error("Communication problem with server."); + return reply; + + } else { + if (netalert) + netalert.show(); + else + notify_error("Communication problem with server."); + } } catch (e) { if (netalert) @@ -981,7 +923,7 @@ function handle_rpc_json(transport, scheduled_call) { console.error(e); } - return true; + return false; } function switchPanelMode(wide) { @@ -1027,22 +969,16 @@ function switchPanelMode(wide) { if (article_id) view(article_id); - new Ajax.Request("backend.php", { - parameters: "op=rpc&method=setpanelmode&wide=" + (wide ? 1 : 0), - onComplete: function(transport) { - console.log(transport.responseText); - } }); + xhrPost("backend.php", {op: "rpc", method: "setpanelmode", wide: wide ? 1 : 0}); } function update_random_feed() { console.log("in update_random_feed"); - new Ajax.Request("backend.php", { - parameters: "op=rpc&method=updateRandomFeed", - onComplete: function(transport) { - handle_rpc_json(transport, true); - window.setTimeout(update_random_feed, 30*1000); - } }); + xhrPost("backend.php", { op: "rpc", method: "updateRandomFeed" }, (transport) => { + handle_rpc_json(transport, true); + window.setTimeout(update_random_feed, 30*1000); + }); } function hash_get(key) { diff --git a/js/viewfeed.js b/js/viewfeed.js index 0e451166..b9c635f0 100755 --- a/js/viewfeed.js +++ b/js/viewfeed.js @@ -5,7 +5,7 @@ let _active_article_id = 0; let vgroup_last_feed = false; let post_under_pointer = false; -let last_requested_article = false; +let last_requested_article = 0; let catchup_id_batch = []; let catchup_timeout_id = false; @@ -22,20 +22,15 @@ let _catchup_request_sent = false; let has_storage = 'sessionStorage' in window && window['sessionStorage'] !== null; function headlines_callback2(transport, offset, background, infscroll_req) { - handle_rpc_json(transport); + const reply = handle_rpc_json(transport); console.log("headlines_callback2 [offset=" + offset + "] B:" + background + " I:" + infscroll_req); - let is_cat = false; - let feed_id = false; + if (background) + return; - let reply = false; - - try { - reply = JSON.parse(transport.responseText); - } catch (e) { - console.error(e); - } + var is_cat = false; + var feed_id = false; if (reply) { @@ -43,20 +38,11 @@ function headlines_callback2(transport, offset, background, infscroll_req) { feed_id = reply['headlines']['id']; last_search_query = reply['headlines']['search_query']; - if (background) { - let content = reply['headlines']['content']; - - content = content + "
"; - return; - } + console.log(feed_id, getActiveFeedId(), is_cat, activeFeedIsCat()); if (feed_id != -7 && (feed_id != getActiveFeedId() || is_cat != activeFeedIsCat())) return; - /* dijit.getEnclosingWidget( - document.forms["main_toolbar_form"].update).attr('disabled', - is_cat || feed_id <= 0); */ - try { if (infscroll_req == false) { $("headlines-frame").scrollTop = 0; @@ -85,7 +71,6 @@ function headlines_callback2(transport, offset, background, infscroll_req) { current_first_id = reply['headlines']['first_id']; const counters = reply['counters']; const articles = reply['articles']; - //var runtime_info = reply['runtime-info']; if (infscroll_req == false) { loaded_article_ids = []; @@ -94,17 +79,9 @@ function headlines_callback2(transport, offset, background, infscroll_req) { reply['headlines']['toolbar'], {parseContent: true}); - /*dojo.html.set($("headlines-frame"), - reply['headlines']['content'], - {parseContent: true}); - - $$("#headlines-frame div[id*='RROW']").each(function(row) { - loaded_article_ids.push(row.id); - });*/ - $("headlines-frame").innerHTML = ''; - var tmp = new Element("div"); + let tmp = document.createElement("div"); tmp.innerHTML = reply['headlines']['content']; dojo.parser.parse(tmp); @@ -118,7 +95,7 @@ function headlines_callback2(transport, offset, background, infscroll_req) { } } - var hsp = $("headlines-spacer"); + let hsp = $("headlines-spacer"); if (!hsp) hsp = new Element("DIV", {"id": "headlines-spacer"}); dijit.byId('headlines-frame').domNode.appendChild(hsp); @@ -140,17 +117,17 @@ function headlines_callback2(transport, offset, background, infscroll_req) { const c = dijit.byId("headlines-frame"); const ids = getSelectedArticleIds2(); - var hsp = $("headlines-spacer"); + let hsp = $("headlines-spacer"); if (hsp) c.domNode.removeChild(hsp); - var tmp = new Element("div"); + let tmp = document.createElement("div"); tmp.innerHTML = reply['headlines']['content']; dojo.parser.parse(tmp); while (tmp.hasChildNodes()) { - var row = tmp.removeChild(tmp.firstChild); + let row = tmp.removeChild(tmp.firstChild); if (loaded_article_ids.indexOf(row.id) == -1 || row.hasClassName("cdmFeedTitle")) { dijit.byId("headlines-frame").domNode.appendChild(row); @@ -166,7 +143,7 @@ function headlines_callback2(transport, offset, background, infscroll_req) { console.log("restore selected ids: " + ids); - for (var i = 0; i < ids.length; i++) { + for (let i = 0; i < ids.length; i++) { markHeadline(ids[i]); } @@ -183,7 +160,7 @@ function headlines_callback2(transport, offset, background, infscroll_req) { const first_id_changed = reply['headlines']['first_id_changed']; console.log("first id changed:" + first_id_changed); - var hsp = $("headlines-spacer"); + let hsp = $("headlines-spacer"); if (hsp) { if (first_id_changed) { @@ -199,7 +176,7 @@ function headlines_callback2(transport, offset, background, infscroll_req) { } if (articles) { - for (var i = 0; i < articles.length; i++) { + for (let i = 0; i < articles.length; i++) { const a_id = articles[i]['id']; cache_set("article:" + a_id, articles[i]['content']); } -- 2.39.5