]> git.wh0rd.org - tt-rss.git/blobdiff - js/prefs.js
remove ok = confirm() thing
[tt-rss.git] / js / prefs.js
index 9d74d4ce0eee2901694996e529698aa18675fddd..965e12fa6ee42fb35a5b2be79e2f0793bba7a823 100755 (executable)
@@ -1,8 +1,5 @@
 /* global dijit, __ */
 
-let hotkey_prefix = false;
-let hotkey_prefix_pressed = false;
-
 let seq = "";
 
 function notify_callback2(transport, sticky) {
@@ -251,10 +248,7 @@ function removeSelectedLabels() {
        const sel_rows = getSelectedLabels();
 
        if (sel_rows.length > 0) {
-
-               const ok = confirm(__("Remove selected labels?"));
-
-               if (ok) {
+               if (confirm(__("Remove selected labels?"))) {
                        notify_progress("Removing selected labels...");
 
                        const query = { op: "pref-labels", method: "remove",
@@ -277,9 +271,7 @@ function removeSelectedUsers() {
 
        if (sel_rows.length > 0) {
 
-               const ok = confirm(__("Remove selected users? Neither default admin nor your account will be removed."));
-
-               if (ok) {
+               if (confirm(__("Remove selected users? Neither default admin nor your account will be removed."))) {
                        notify_progress("Removing selected users...");
 
                        const query = { op: "pref-users", method: "remove",
@@ -302,10 +294,7 @@ function removeSelectedFilters() {
        const sel_rows = getSelectedFilters();
 
        if (sel_rows.length > 0) {
-
-               const ok = confirm(__("Remove selected filters?"));
-
-               if (ok) {
+               if (confirm(__("Remove selected filters?"))) {
                        notify_progress("Removing selected filters...");
 
                        const query = { op: "pref-filters", method: "remove",
@@ -327,10 +316,7 @@ function removeSelectedFeeds() {
        const sel_rows = getSelectedFeeds();
 
        if (sel_rows.length > 0) {
-
-               const ok = confirm(__("Unsubscribe from selected feeds?"));
-
-               if (ok) {
+               if (confirm(__("Unsubscribe from selected feeds?"))) {
 
                        notify_progress("Unsubscribing from selected feeds...", true);
 
@@ -381,9 +367,7 @@ function resetSelectedUserPass() {
                return;
        }
 
-       const ok = confirm(__("Reset password of selected user?"));
-
-       if (ok) {
+       if (confirm(__("Reset password of selected user?"))) {
                notify_progress("Resetting password for selected user...");
 
                const id = rows[0];
@@ -455,9 +439,7 @@ function joinSelectedFilters() {
                return;
        }
 
-       const ok = confirm(__("Combine selected filters?"));
-
-       if (ok) {
+       if (confirm(__("Combine selected filters?"))) {
                notify_progress("Joining filters...");
 
                xhrPost("backend.php", { op: "pref-filters", method: "join", ids: rows.toString() }, () => {
@@ -494,103 +476,62 @@ function editSelectedFeeds() {
 
        notify_progress("Loading, please wait...");
 
-       const query = "backend.php?op=pref-feeds&method=editfeeds&ids=" +
-               param_escape(rows.toString());
-
-       console.log(query);
-
        if (dijit.byId("feedEditDlg"))
                dijit.byId("feedEditDlg").destroyRecursive();
 
-       new Ajax.Request("backend.php", {
-               parameters: query,
-               onComplete: function (transport) {
-
-                       notify("");
-
-                       const dialog = new dijit.Dialog({
-                               id: "feedEditDlg",
-                               title: __("Edit Multiple Feeds"),
-                               style: "width: 600px",
-                               getChildByName: function (name) {
-                                       let rv = null;
-                                       this.getChildren().each(
-                                               function (child) {
-                                                       if (child.name == name) {
-                                                               rv = child;
-                                                               return;
-                                                       }
-                                               });
-                                       return rv;
-                               },
-                               toggleField: function (checkbox, elem, label) {
-                                       this.getChildByName(elem).attr('disabled', !checkbox.checked);
-
-                                       if ($(label))
-                                               if (checkbox.checked)
-                                                       $(label).removeClassName('insensitive');
-                                               else
-                                                       $(label).addClassName('insensitive');
-
-                               },
-                               execute: function () {
-                                       if (this.validate() && confirm(__("Save changes to selected feeds?"))) {
-                                               const query = this.attr('value');
-
-                                               //console.log(query); return;
-
-                                               /* Form.serialize ignores unchecked checkboxes */
-
-                                               /*if (!query.match("&private=") &&
-                                                       this.getChildByName('private').attr('disabled') == false) {
-                                                       query.private = "false";
-                                               }
+       xhrPost("backend.php", { op: "pref-feeds", method: "editfeeds", ids: rows.toString() }, (transport) => {
+               notify("");
 
-                                               try {
-                                                       if (!query.match("&cache_images=") &&
-                                                               this.getChildByName('cache_images').attr('disabled') == false) {
-                                                               query.cache_images = "false";
-                                                       }
-                                               } catch (e) {
+               const dialog = new dijit.Dialog({
+                       id: "feedEditDlg",
+                       title: __("Edit Multiple Feeds"),
+                       style: "width: 600px",
+                       getChildByName: function (name) {
+                               let rv = null;
+                               this.getChildren().each(
+                                       function (child) {
+                                               if (child.name == name) {
+                                                       rv = child;
+                                                       return;
                                                }
+                                       });
+                               return rv;
+                       },
+                       toggleField: function (checkbox, elem, label) {
+                               this.getChildByName(elem).attr('disabled', !checkbox.checked);
 
-                                               try {
-                                                       if (!query.match("&hide_images=") &&
-                                                               this.getChildByName('hide_images').attr('disabled') == false) {
-                                                               query.hide_images = "false";
-                                                       }
-                                               } catch (e) {
-                                               }
+                               if ($(label))
+                                       if (checkbox.checked)
+                                               $(label).removeClassName('insensitive');
+                                       else
+                                               $(label).addClassName('insensitive');
 
-                                               if (!query.match("&include_in_digest=") &&
-                                                       this.getChildByName('include_in_digest').attr('disabled') == false) {
-                                                       query.include_in_digest = "false";
-                                               }
+                       },
+                       execute: function () {
+                               if (this.validate() && confirm(__("Save changes to selected feeds?"))) {
+                                       const query = this.attr('value');
 
-                                               if (!query.match("&always_display_enclosures=") &&
-                                                       this.getChildByName('always_display_enclosures').attr('disabled') == false) {
-                                                       query.always_display_enclosures = "false";
-                                               }
+                                       /* normalize unchecked checkboxes because [] is not serialized */
 
-                                               if (!query.match("&mark_unread_on_update=") &&
-                                                       this.getChildByName('mark_unread_on_update').attr('disabled') == false) {
-                                                       query.mark_unread_on_update = "false";
-                                               }*/
+                                       Object.keys(query).each((key) => {
+                                               let val = query[key];
 
-                                               notify_progress("Saving data...", true);
+                                               if (typeof val == "object" && val.length == 0)
+                                                       query[key] = ["off"];
+                                       });
 
-                                               xhrPost("backend.php", query, () => {
-                                                       dialog.hide();
-                                                       updateFeedList();
-                                               });
-                                       }
-                               },
-                               content: transport.responseText
-                       });
+                                       notify_progress("Saving data...", true);
 
-                       dialog.show();
+                                       xhrPost("backend.php", query, () => {
+                                               dialog.hide();
+                                               updateFeedList();
+                                       });
+                               }
+                       },
+                       content: transport.responseText
+               });
 
-               }
+               dialog.show();
        });
 }
 
@@ -644,57 +585,58 @@ function updateFilterList() {
        let search = "";
        if (user_search) { search = user_search.value; }
 
-       new Ajax.Request("backend.php", {
-               parameters: "?op=pref-filters&search=" + param_escape(search),
-               onComplete: function(transport) {
-                       dijit.byId('filterConfigTab').attr('content', transport.responseText);
-                       notify("");
-               } });
+       xhrPost("backend.php", { op: "pref-filters", search: search }, (transport) => {
+        dijit.byId('filterConfigTab').attr('content', transport.responseText);
+        notify("");
+    });
 }
 
 function updateLabelList() {
-       new Ajax.Request("backend.php", {
-               parameters: "?op=pref-labels",
-               onComplete: function(transport) {
-                       dijit.byId('labelConfigTab').attr('content', transport.responseText);
-                       notify("");
-               } });
+       xhrPost("backend.php", { op: "pref-labels" }, (transport) => {
+        dijit.byId('labelConfigTab').attr('content', transport.responseText);
+        notify("");
+    });
 }
 
 function updatePrefsList() {
-       new Ajax.Request("backend.php", {
-               parameters: "?op=pref-prefs",
-               onComplete: function(transport) {
-                       dijit.byId('genConfigTab').attr('content', transport.responseText);
-                       notify("");
-               } });
+    xhrPost("backend.php", { op: "pref-prefs" }, (transport) => {
+        dijit.byId('genConfigTab').attr('content', transport.responseText);
+        notify("");
+    });
 }
 
 function updateSystemList() {
-       new Ajax.Request("backend.php", {
-               parameters: "?op=pref-system",
-               onComplete: function(transport) {
-                       dijit.byId('systemConfigTab').attr('content', transport.responseText);
-                       notify("");
-               } });
+    xhrPost("backend.php", { op: "pref-system" }, (transport) => {
+        dijit.byId('systemConfigTab').attr('content', transport.responseText);
+        notify("");
+    });
 }
 
 function selectTab(id, noupdate) {
        if (!noupdate) {
                notify_progress("Loading, please wait...");
 
-               if (id == "feedConfig") {
-                       updateFeedList();
-               } else if (id == "filterConfig") {
-                       updateFilterList();
-               } else if (id == "labelConfig") {
-                       updateLabelList();
-               } else if (id == "genConfig") {
-                       updatePrefsList();
-               } else if (id == "userConfig") {
-                       updateUsersList();
-               } else if (id == "systemConfig") {
-                       updateSystemList();
+               switch (id) {
+                       case "feedConfig":
+                updateFeedList();
+                               break;
+                       case "filterConfig":
+                updateFilterList();
+                               break;
+                       case "labelConfig":
+                updateLabelList();
+                               break;
+                       case "genConfig":
+                updatePrefsList();
+                               break;
+                       case "userConfig":
+                updateUsersList();
+                               break;
+                       case "systemConfig":
+                updateSystemList();
+                               break;
+                       default:
+                               console.warn("unknown tab", id);
                }
 
                const tab = dijit.byId(id + "Tab");
@@ -723,7 +665,7 @@ function init_second_stage() {
                window.setTimeout(function() { editFeed(param) }, 100);
        }
 
-       setTimeout(hotkey_prefix_timeout, 5*1000);
+       setInterval(hotkey_prefix_timeout, 5*1000);
 }
 
 function init() {
@@ -777,16 +719,12 @@ function init() {
                                loading_set_progress(50);
 
                                const clientTzOffset = new Date().getTimezoneOffset() * 60;
+                               const params = { op: "rpc", method: "sanityCheck", clientTzOffset: clientTzOffset };
 
-                               new Ajax.Request("backend.php", {
-                                       parameters: {
-                                               op: "rpc", method: "sanityCheck",
-                                               clientTzOffset: clientTzOffset
-                                       },
-                                       onComplete: function (transport) {
-                                               backend_sanity_check_callback(transport);
-                                       }
+                               xhrPost("backend.php", params, (transport) => {
+                                       backend_sanity_check_callback(transport);
                                });
+
                        } catch (e) {
                                exception_error(e);
                        }
@@ -796,148 +734,72 @@ function init() {
 
 
 function validatePrefsReset() {
-       const ok = confirm(__("Reset to defaults?"));
-
-       if (ok) {
+       if (confirm(__("Reset to defaults?"))) {
 
                const query = "?op=pref-prefs&method=resetconfig";
-               console.log(query);
-
-               new Ajax.Request("backend.php", {
-                       parameters: query,
-                       onComplete: function(transport) {
-                               updatePrefsList();
-                               notify_info(transport.responseText);
-                       } });
 
+               xhrPost("backend.php", { op: "pref-prefs", method: "resetconfig" }, (transport) => {
+            updatePrefsList();
+            notify_info(transport.responseText);
+        });
        }
 
        return false;
-
 }
 
 function pref_hotkey_handler(e) {
-
-       if (e.target.nodeName == "INPUT" || e.target.nodeName == "TEXTAREA") return;
-
-       let keycode = false;
-       let shift_key = false;
-
-       const cmdline = $('cmdline');
-
-       try {
-               shift_key = e.shiftKey;
-       } catch (e) {
-
-       }
-
-       if (window.event) {
-               keycode = window.event.keyCode;
-       } else if (e) {
-               keycode = e.which;
-       }
-
-       let keychar = String.fromCharCode(keycode);
-
-       if (keycode == 27) { // escape
-               hotkey_prefix = false;
-       }
-
-       if (keycode == 16) return; // ignore lone shift
-       if (keycode == 17) return; // ignore lone ctrl
-
-       if (!shift_key) keychar = keychar.toLowerCase();
-
-       var hotkeys = getInitParam("hotkeys");
-
-       if (!hotkey_prefix && hotkeys[0].indexOf(keychar) != -1) {
-
-               const date = new Date();
-               const ts = Math.round(date.getTime() / 1000);
-
-               hotkey_prefix = keychar;
-               hotkey_prefix_pressed = ts;
-
-               cmdline.innerHTML = keychar;
-               Element.show(cmdline);
-
-               return true;
-       }
-
-       Element.hide(cmdline);
-
-       let hotkey = keychar.search(/[a-zA-Z0-9]/) != -1 ? keychar : "(" + keycode + ")";
-       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;
-               }
-       }
-
-       switch (hotkey_action) {
-               case "feed_subscribe":
-                       quickAddFeed();
-                       return false;
-               case "create_label":
-                       addLabel();
-                       return false;
-               case "create_filter":
-                       quickAddFilter();
-                       return false;
-               case "help_dialog":
-                       //helpDialog("prefs");
-                       return false;
-               default:
-                       console.log("unhandled action: " + hotkey_action + "; hotkey: " + hotkey);
+    if (e.target.nodeName == "INPUT" || e.target.nodeName == "TEXTAREA") return;
+
+    const action_name = keyevent_to_action(e);
+
+    if (action_name) {
+        switch (action_name) {
+            case "feed_subscribe":
+                quickAddFeed();
+                return false;
+            case "create_label":
+                addLabel();
+                return false;
+            case "create_filter":
+                quickAddFilter();
+                return false;
+            case "help_dialog":
+                helpDialog("main");
+                return false;
+            default:
+                console.log("unhandled action: " + action_name + "; keycode: " + e.which);
+        }
        }
 }
 
 function removeCategory(id, item) {
 
-       const ok = confirm(__("Remove category %s? Any nested feeds would be placed into Uncategorized.").replace("%s", item.name));
-
-       if (ok) {
-               const query = "?op=pref-feeds&method=removeCat&ids=" +
-                       param_escape(id);
-
+       if (confirm(__("Remove category %s? Any nested feeds would be placed into Uncategorized.").replace("%s", item.name))) {
                notify_progress("Removing category...");
 
-               new Ajax.Request("backend.php", {
-                       parameters: query,
-                       onComplete: function (transport) {
-                               notify('');
-                               updateFeedList();
-                       }
-               });
+        const query = { op: "pref-feeds", method: "removeCat",
+            ids: id };
+
+               xhrPost("backend.php", query, () => {
+            notify('');
+            updateFeedList();
+        });
        }
 }
 
 function removeSelectedCategories() {
-
        const sel_rows = getSelectedCategories();
 
        if (sel_rows.length > 0) {
-
-               const ok = confirm(__("Remove selected categories?"));
-
-               if (ok) {
+               if (confirm(__("Remove selected categories?"))) {
                        notify_progress("Removing selected categories...");
 
-                       const query = "?op=pref-feeds&method=removeCat&ids="+
-                               param_escape(sel_rows.toString());
-
-                       new Ajax.Request("backend.php", {
-                               parameters: query,
-                               onComplete: function(transport) {
-                                               updateFeedList();
-                                       } });
+                       const query = { op: "pref-feeds", method: "removeCat",
+                               ids: sel_rows.toString() };
 
+                       xhrPost("backend.php", query, () => {
+                updateFeedList();
+            });
                }
        } else {
                alert(__("No categories are selected."));
@@ -950,19 +812,12 @@ function createCategory() {
        const title = prompt(__("Category title:"));
 
        if (title) {
-
                notify_progress("Creating category...");
 
-               const query = "?op=pref-feeds&method=addCat&cat=" +
-                       param_escape(title);
-
-               new Ajax.Request("backend.php", {
-                       parameters: query,
-                       onComplete: function (transport) {
-                               notify('');
-                               updateFeedList();
-                       }
-               });
+               xhrPost("backend.php", { op: "pref-feeds", method: "addCat", cat: title }, () => {
+            notify('');
+            updateFeedList();
+        });
        }
 }
 
@@ -982,25 +837,18 @@ function showInactiveFeeds() {
                removeSelected: function () {
                        const sel_rows = this.getSelectedFeeds();
 
-                       console.log(sel_rows);
-
                        if (sel_rows.length > 0) {
-                               const ok = confirm(__("Remove selected feeds?"));
-
-                               if (ok) {
+                               if (confirm(__("Remove selected feeds?"))) {
                                        notify_progress("Removing selected feeds...", true);
 
-                                       const query = "?op=pref-feeds&method=remove&ids=" +
-                                               param_escape(sel_rows.toString());
+                                       const query = { op: "pref-feeds", method: "remove",
+                                               ids: sel_rows.toString() };
 
-                                       new Ajax.Request("backend.php", {
-                                               parameters: query,
-                                               onComplete: function (transport) {
-                                                       notify('');
-                                                       dialog.hide();
-                                                       updateFeedList();
-                                               }
-                                       });
+                                       xhrPost("backend.php", query, () => {
+                        notify('');
+                        dialog.hide();
+                        updateFeedList();
+                    });
                                }
 
                        } else {
@@ -1018,36 +866,27 @@ function showInactiveFeeds() {
 }
 
 function opmlRegenKey() {
-       const ok = confirm(__("Replace current OPML publishing address with a new one?"));
-
-       if (ok) {
-
+       if (confirm(__("Replace current OPML publishing address with a new one?"))) {
                notify_progress("Trying to change address...", true);
 
-               const query = "?op=pref-feeds&method=regenOPMLKey";
-
-               new Ajax.Request("backend.php", {
-                       parameters: query,
-                       onComplete: function (transport) {
-                               const reply = JSON.parse(transport.responseText);
-
-                               const new_link = reply.link;
+               xhrJson("backend.php", { op: "pref-feeds", method: "regenOPMLKey" }, (reply) => {
+            if (reply) {
+                const new_link = reply.link;
+                const e = $('pub_opml_url');
 
-                               const e = $('pub_opml_url');
+                if (new_link) {
+                    e.href = new_link;
+                    e.innerHTML = new_link;
 
-                               if (new_link) {
-                                       e.href = new_link;
-                                       e.innerHTML = new_link;
+                    new Effect.Highlight(e);
 
-                                       new Effect.Highlight(e);
+                    notify('');
 
-                                       notify('');
-
-                               } else {
-                                       notify_error("Could not change feed URL.");
-                               }
-                       }
-               });
+                } else {
+                    notify_error("Could not change feed URL.");
+                }
+            }
+        });
        }
        return false;
 }
@@ -1056,18 +895,14 @@ function labelColorReset() {
        const labels = getSelectedLabels();
 
        if (labels.length > 0) {
-               const ok = confirm(__("Reset selected labels to default colors?"));
+               if (confirm(__("Reset selected labels to default colors?"))) {
 
-               if (ok) {
-                       const query = "?op=pref-labels&method=colorreset&ids=" +
-                               param_escape(labels.toString());
+                       const query = { op: "pref-labels", method: "colorreset",
+                               ids: labels.toString() };
 
-                       new Ajax.Request("backend.php", {
-                               parameters: query,
-                               onComplete: function (transport) {
-                                       updateLabelList();
-                               }
-                       });
+                       xhrPost("backend.php", query, () => {
+                updateLabelList();
+            });
                }
 
        } else {
@@ -1097,9 +932,7 @@ function editProfiles() {
                        const sel_rows = this.getSelectedProfiles();
 
                        if (sel_rows.length > 0) {
-                               const ok = confirm(__("Remove selected profiles? Active and default profiles will not be removed."));
-
-                               if (ok) {
+                               if (confirm(__("Remove selected profiles? Active and default profiles will not be removed."))) {
                                        notify_progress("Removing selected profiles...", true);
 
                                        const query = { op: "rpc", method: "remprofiles",
@@ -1119,10 +952,7 @@ function editProfiles() {
                        const sel_rows = this.getSelectedProfiles();
 
                        if (sel_rows.length == 1) {
-
-                               const ok = confirm(__("Activate selected profile?"));
-
-                               if (ok) {
+                               if (confirm(__("Activate selected profile?"))) {
                                        notify_progress("Loading, please wait...");
 
                     xhrPost("backend.php", { op: "rpc", method: "setprofile", id: sel_rows.toString() },  () => {
@@ -1183,9 +1013,7 @@ function activatePrefProfile() {
 
 function clearFeedAccessKeys() {
 
-       const ok = confirm(__("This will invalidate all previously generated feed URLs. Continue?"));
-
-       if (ok) {
+       if (confirm(__("This will invalidate all previously generated feed URLs. Continue?"))) {
                notify_progress("Clearing URLs...");
 
                xhrPost("backend.php", { op: "pref-feeds", method: "clearKeys" }, () => {