]> git.wh0rd.org - tt-rss.git/blobdiff - js/tt-rss.js
first stage of headline element handling refactoring
[tt-rss.git] / js / tt-rss.js
index 872407e7391e111446f9347cd7c514e8013441bd..fca9dac199d9f06aaccb897c9bcbc57ca9901c8a 100644 (file)
@@ -1,11 +1,11 @@
-var global_unread = -1;
-var hotkey_prefix = false;
-var hotkey_prefix_pressed = false;
-var hotkey_actions = {};
-var _widescreen_mode = false;
-var _rpc_seq = 0;
-var _active_feed_id = 0;
-var _active_feed_is_cat = false;
+/* global dijit, __ */
+
+let global_unread = -1;
+let _widescreen_mode = false;
+let _rpc_seq = 0;
+let _active_feed_id = 0;
+let _active_feed_is_cat = false;
+let hotkey_actions = {};
 
 function next_seq() {
        _rpc_seq += 1;
@@ -17,7 +17,7 @@ function get_seq() {
 }
 
 function activeFeedIsCat() {
-       return _active_feed_is_cat;
+       return !!_active_feed_is_cat;
 }
 
 function getActiveFeedId() {
@@ -79,14 +79,6 @@ function updateFeedList() {
                        id: "feedTree",
                }, "feedTree");
 
-               /*              var menu = new dijit.Menu({id: 'feedMenu'});
-
-                menu.addChild(new dijit.MenuItem({
-                label: "Simple menu item"
-                }));
-
-                //             menu.bindDomNode(tree.domNode); */
-
                var tmph = dojo.connect(dijit.byId('feedMenu'), '_openMyself', function (event) {
                        console.log(dijit.getEnclosingWidget(event.target));
                        dojo.disconnect(tmph);
@@ -119,18 +111,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("");
@@ -166,7 +152,7 @@ function search() {
                style: "width: 600px",
                execute: function() {
                        if (this.validate()) {
-                               _search_query = dojo.objectToQuery(this.attr('value'));
+                               _search_query = this.attr('value');
                                this.hide();
                                viewCurrentFeed();
                        }
@@ -220,7 +206,7 @@ function init() {
                        "dijit/form/Form",
                        "dijit/form/RadioButton",
                        "dijit/form/Select",
-               "dijit/form/MultiSelect",
+                       "dijit/form/MultiSelect",
                        "dijit/form/SimpleTextarea",
                        "dijit/form/TextBox",
                        "dijit/form/ComboBox",
@@ -249,27 +235,29 @@ function init() {
                                                return false;
 
                                        loading_set_progress(30);
+                                       init_hotkey_actions();
 
                                        const a = document.createElement('audio');
-
                                        const hasAudio = !!a.canPlayType;
                                        const hasSandbox = "sandbox" in document.createElement("iframe");
                                        const hasMp3 = !!(a.canPlayType && a.canPlayType('audio/mpeg;').replace(/no/, ''));
                                        const clientTzOffset = new Date().getTimezoneOffset() * 60;
 
-                                       init_hotkey_actions();
-
-                                       new Ajax.Request("backend.php", {
-                                               parameters: {
+                                       const params = {
                                                        op: "rpc", method: "sanityCheck", hasAudio: hasAudio,
                                                        hasMp3: hasMp3,
                                                        clientTzOffset: clientTzOffset,
                                                        hasSandbox: hasSandbox
-                                               },
-                                               onComplete: function (transport) {
+                                               };
+
+                                       xhrPost("backend.php", params, (transport) => {
+                                               try {
                                                        backend_sanity_check_callback(transport);
+                                               } catch (e) {
+                                                       console.error(e);
                                                }
                                        });
+
                                } catch (e) {
                                        exception_error(e);
                                }
@@ -311,32 +299,6 @@ function init_hotkey_actions() {
        hotkey_actions["prev_article_noexpand"] = function() {
                moveToPost('prev', true, true);
        };
-       hotkey_actions["collapse_article"] = function() {
-               const id = getActiveArticleId();
-               const elem = $("CICD-"+id);
-
-               if (elem) {
-                       if (elem.visible()) {
-                               cdmCollapseArticle(null, id);
-                       }
-                       else {
-                               cdmExpandArticle(id);
-                       }
-               }
-       };
-       hotkey_actions["toggle_expand"] = function() {
-               const id = getActiveArticleId();
-               const elem = $("CICD-"+id);
-
-               if (elem) {
-                       if (elem.visible()) {
-                               cdmCollapseArticle(null, id, false);
-                       }
-                       else {
-                               cdmExpandArticle(id);
-                       }
-               }
-       };
        hotkey_actions["search_dialog"] = function() {
                search();
        };
@@ -373,13 +335,7 @@ function init_hotkey_actions() {
                scrollArticle(-40);
        };
        hotkey_actions["close_article"] = function() {
-               if (isCdmMode()) {
-                       if (!getInitParam("cdm_expanded")) {
-                               cdmCollapseArticle(false, getActiveArticleId());
-                       }
-               } else {
-                       closeArticlePanel();
-               }
+               closeArticlePanel();
        };
        hotkey_actions["email_article"] = function() {
                if (typeof emailArticle != "undefined") {
@@ -449,14 +405,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();
@@ -492,10 +443,12 @@ function init_hotkey_actions() {
 
                        if (row) {
                                const cb = dijit.getEnclosingWidget(
-                                       row.getElementsByClassName("rchk")[0]);
+                                       row.select(".rchk")[0]);
 
                                if (cb) {
-                                       cb.attr("checked", !cb.attr("checked"));
+                                       if (!row.hasClassName("active"))
+                                               cb.attr("checked", !cb.attr("checked"));
+
                                        toggleSelectRowById(cb, "RROW-" + id);
                                        return false;
                                }
@@ -539,31 +492,14 @@ 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"));
+               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;
-
-               new Ajax.Request("backend.php", {
-                       parameters: query,
-                       onComplete: function(transport) {
-                               setInitParam("cdm_expanded", !getInitParam("cdm_expanded"));
-                               viewCurrentFeed();
-                       } });
+                       closeArticlePanel();
+                       viewCurrentFeed();
+               })
        };
 }
 
@@ -603,8 +539,6 @@ function init_second_stage() {
        dijit.getEnclosingWidget(toolbar.order_by).attr('value',
                getInitParam("default_view_order_by"));
 
-       const feeds_sort_by_unread = getInitParam("feeds_sort_by_unread") == 1;
-
        const hash_feed_id = hash_get('f');
        const hash_feed_is_cat = hash_get('c') == "1";
 
@@ -618,7 +552,7 @@ function init_second_stage() {
        if ('sessionStorage' in window && window['sessionStorage'] !== null)
                sessionStorage.clear();
 
-       const hotkeys = getInitParam("hotkeys");
+       /*const hotkeys = getInitParam("hotkeys");
        const tmp = [];
 
        for (const sequence in hotkeys[1]) {
@@ -627,7 +561,7 @@ function init_second_stage() {
        }
 
        hotkeys[1] = tmp;
-       setInitParam("hotkeys", hotkeys);
+       setInitParam("hotkeys", hotkeys);*/
 
        _widescreen_mode = getInitParam("widescreen");
        switchPanelMode(_widescreen_mode);
@@ -693,15 +627,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;
@@ -727,18 +652,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) {
@@ -805,107 +722,20 @@ 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(transport) {
-                               viewCurrentFeed();
-                       } });
-       }
-}
-
 function hotkey_handler(e) {
-
        if (e.target.nodeName == "INPUT" || e.target.nodeName == "TEXTAREA") return;
 
-       let keycode = false;
-
-       const cmdline = $('cmdline');
-
-       if (window.event) {
-               keycode = window.event.keyCode;
-       } else if (e) {
-               keycode = e.which;
-       }
-
-       if (keycode == 27) { // escape
-               hotkey_prefix = false;
-       }
-
-       if (keycode == 16) return; // ignore lone shift
-       if (keycode == 17) return; // ignore lone ctrl
-
-       var hotkeys = getInitParam("hotkeys");
-       const keychar = String.fromCharCode(keycode).toLowerCase();
+       const action_name = keyevent_to_action(e);
 
-       if (!hotkey_prefix && hotkeys[0].indexOf(keychar) != -1) {
+       if (action_name) {
+               const action_func = hotkey_actions[action_name];
 
-               const date = new Date();
-               const ts = Math.round(date.getTime() / 1000);
-
-               hotkey_prefix = keychar;
-               hotkey_prefix_pressed = ts;
-
-               cmdline.innerHTML = keychar;
-               Element.show(cmdline);
-
-               e.stopPropagation();
-
-               // returning false here literally disables ctrl-c in browser lol (because C is a valid prefix)
-               return true;
-       }
-
-       Element.hide(cmdline);
-
-       let hotkey = keychar.search(/[a-zA-Z0-9]/) != -1 ? keychar : "(" + keycode + ")";
-
-       // ensure ^*char notation
-       if (e.shiftKey) hotkey = "*" + hotkey;
-       if (e.ctrlKey) hotkey = "^" + hotkey;
-       if (e.altKey) hotkey = "+" + hotkey;
-       if (e.metaKey) hotkey = "%" + hotkey;
-
-       hotkey = hotkey_prefix ? hotkey_prefix + " " + hotkey : hotkey;
-       hotkey_prefix = false;
-
-       let hotkey_action = false;
-       var hotkeys = getInitParam("hotkeys");
-
-       for (const sequence in hotkeys[1]) {
-               if (sequence == hotkey) {
-                       hotkey_action = hotkeys[1][sequence];
-                       break;
+               if (action_func != null) {
+                       action_func();
+                       e.stopPropagation();
+                       return false;
                }
        }
-
-       const action = hotkey_actions[hotkey_action];
-
-       if (action != null) {
-               action();
-               e.stopPropagation();
-               return false;
-       }
 }
 
 function inPreferences() {
@@ -958,22 +788,18 @@ function handle_rpc_json(transport, scheduled_call) {
 
                        const seq = reply['seq'];
 
-                       if (seq) {
-                               if (get_seq() != seq) {
-                                       console.log("[handle_rpc_json] sequence mismatch: " + seq +
-                                               " (want: " + get_seq() + ")");
-                                       return true;
-                               }
+                       if (seq && get_seq() != seq) {
+                               console.log("[handle_rpc_json] sequence mismatch: " + seq +
+                                       " (want: " + get_seq() + ")");
+                               return true;
                        }
 
                        const message = reply['message'];
 
-                       if (message) {
-                               if (message == "UPDATE_COUNTERS") {
-                                       console.log("need to refresh counters...");
-                                       setInitParam("last_article_id", -1);
-                                       request_counters(true);
-                               }
+                       if (message == "UPDATE_COUNTERS") {
+                               console.log("need to refresh counters...");
+                               setInitParam("last_article_id", -1);
+                               request_counters(true);
                        }
 
                        const counters = reply['counters'];
@@ -988,10 +814,14 @@ function handle_rpc_json(transport, scheduled_call) {
 
                        if (netalert) netalert.hide();
 
-               } else if (netalert)
+                       return reply;
+
+               } else {
+                       if (netalert)
                                netalert.show();
                        else
                                notify_error("Communication problem with server.");
+               }
 
        } catch (e) {
                if (netalert)
@@ -1002,7 +832,7 @@ function handle_rpc_json(transport, scheduled_call) {
                console.error(e);
        }
 
-       return true;
+       return false;
 }
 
 function switchPanelMode(wide) {
@@ -1048,22 +878,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) {