1
0
mirror of https://tt-rss.org/git/tt-rss.git synced 2024-06-23 11:46:37 +02:00

remove duplicated code from hotkey actions handler

more xhrPost() refactoring
This commit is contained in:
Andrew Dolgov 2018-11-30 12:46:52 +03:00
parent 24c618dd6f
commit 7e8be97b66
4 changed files with 197 additions and 250 deletions

View File

@ -175,7 +175,7 @@ function feedlist_init() {
loading_set_progress(50); loading_set_progress(50);
document.onkeydown = hotkey_handler; document.onkeydown = hotkey_handler;
setTimeout(hotkey_prefix_timeout, 5*1000); setInterval(hotkey_prefix_timeout, 5*1000);
if (!getActiveFeedId()) { if (!getActiveFeedId()) {
viewfeed({feed: -3}); viewfeed({feed: -3});

View File

@ -5,6 +5,9 @@ let _label_base_index = -1024;
let loading_progress = 0; let loading_progress = 0;
let notify_hide_timerid = false; let notify_hide_timerid = false;
let hotkey_prefix = 0;
let hotkey_prefix_pressed = false;
Ajax.Base.prototype.initialize = Ajax.Base.prototype.initialize.wrap( Ajax.Base.prototype.initialize = Ajax.Base.prototype.initialize.wrap(
function (callOriginal, options) { function (callOriginal, options) {
@ -27,14 +30,14 @@ Ajax.Base.prototype.initialize = Ajax.Base.prototype.initialize.wrap(
function xhrPost(url, params, complete) { function xhrPost(url, params, complete) {
console.log("xhrPost:", params); console.log("xhrPost:", params);
new Ajax.Request(url, { return new Ajax.Request(url, {
parameters: params, parameters: params,
onComplete: complete onComplete: complete
}); });
} }
function xhrJson(url, params, complete) { function xhrJson(url, params, complete) {
xhrPost(url, params, (reply) => { return xhrPost(url, params, (reply) => {
try { try {
const obj = JSON.parse(reply.responseText); const obj = JSON.parse(reply.responseText);
complete(obj); complete(obj);
@ -498,9 +501,6 @@ function hotkey_prefix_timeout() {
hotkey_prefix = false; hotkey_prefix = false;
Element.hide('cmdline'); Element.hide('cmdline');
} }
setTimeout(hotkey_prefix_timeout, 1000);
} }
function uploadIconHandler(rc) { function uploadIconHandler(rc) {
@ -1087,8 +1087,25 @@ function backend_sanity_check_callback(transport) {
console.log('reading init-params...'); console.log('reading init-params...');
for (const k in params) { for (const k in params) {
console.log("IP:", k, "=>", params[k]); switch (k) {
if (k == "label_base_index") _label_base_index = parseInt(params[k]); case "label_base_index":
_label_base_index = parseInt(params[k])
break;
case "hotkeys":
// filter mnemonic definitions (used for help panel) from hotkeys map
// i.e. *(191)|Ctrl-/ -> *(191)
const tmp = [];
for (const sequence in params[k][1]) {
const filtered = sequence.replace(/\|.*$/, "");
tmp[filtered] = params[k][1][sequence];
}
params[k][1] = tmp;
break;
}
console.log("IP:", k, "=>", params[k]);
} }
init_params = params; init_params = params;
@ -1537,3 +1554,58 @@ function openArticlePopup(id) {
w.opener = null; w.opener = null;
w.location = "backend.php?op=article&method=view&mode=raw&html=1&zoom=1&id=" + id + "&csrf_token=" + getInitParam("csrf_token"); w.location = "backend.php?op=article&method=view&mode=raw&html=1&zoom=1&id=" + id + "&csrf_token=" + getInitParam("csrf_token");
} }
function keyevent_to_action(e) {
const hotkeys_map = getInitParam("hotkeys");
const keycode = e.which;
const keychar = String.fromCharCode(keycode).toLowerCase();
if (keycode == 27) { // escape and drop prefix
hotkey_prefix = false;
}
if (keycode == 16 || keycode == 17) return; // ignore lone shift / ctrl
if (!hotkey_prefix && hotkeys_map[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");
e.stopPropagation();
return false;
}
Element.hide("cmdline");
let hotkey_name = keychar.search(/[a-zA-Z0-9]/) != -1 ? keychar : "(" + keycode + ")";
// ensure ^*char notation
if (e.shiftKey) hotkey_name = "*" + hotkey_name;
if (e.ctrlKey) hotkey_name = "^" + hotkey_name;
if (e.altKey) hotkey_name = "+" + hotkey_name;
if (e.metaKey) hotkey_name = "%" + hotkey_name;
const hotkey_full = hotkey_prefix ? hotkey_prefix + " " + hotkey_name : hotkey_name;
hotkey_prefix = false;
let action_name = false;
for (const sequence in hotkeys_map[1]) {
if (sequence == hotkey_full) {
action_name = hotkeys_map[1][sequence];
break;
}
}
console.log('keyevent_to_action', hotkey_full, '=>', action_name);
return action_name;
}

View File

@ -1,8 +1,5 @@
/* global dijit, __ */ /* global dijit, __ */
let hotkey_prefix = false;
let hotkey_prefix_pressed = false;
let seq = ""; let seq = "";
function notify_callback2(transport, sticky) { function notify_callback2(transport, sticky) {
@ -494,72 +491,62 @@ function editSelectedFeeds() {
notify_progress("Loading, please wait..."); 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")) if (dijit.byId("feedEditDlg"))
dijit.byId("feedEditDlg").destroyRecursive(); dijit.byId("feedEditDlg").destroyRecursive();
new Ajax.Request("backend.php", { xhrPost("backend.php", { op: "pref-feeds", method: "editfeeds", ids: rows.toString() }, (transport) => {
parameters: query, notify("");
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);
const dialog = new dijit.Dialog({ if ($(label))
id: "feedEditDlg", if (checkbox.checked)
title: __("Edit Multiple Feeds"), $(label).removeClassName('insensitive');
style: "width: 600px", else
getChildByName: function (name) { $(label).addClassName('insensitive');
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) execute: function () {
$(label).removeClassName('insensitive'); if (this.validate() && confirm(__("Save changes to selected feeds?"))) {
else const query = this.attr('value');
$(label).addClassName('insensitive');
}, /* normalize unchecked checkboxes because [] is not serialized */
execute: function () {
if (this.validate() && confirm(__("Save changes to selected feeds?"))) {
const query = this.attr('value');
/* normalize unchecked checkboxes because [] is not serialized */ Object.keys(query).each((key) => {
let val = query[key];
Object.keys(query).each((key) => { if (typeof val == "object" && val.length == 0)
let val = query[key]; query[key] = ["off"];
});
if (typeof val == "object" && val.length == 0) notify_progress("Saving data...", true);
query[key] = ["off"];
});
notify_progress("Saving data...", true); xhrPost("backend.php", query, () => {
dialog.hide();
updateFeedList();
});
}
},
content: transport.responseText
});
xhrPost("backend.php", query, () => { dialog.show();
dialog.hide();
updateFeedList();
});
}
},
content: transport.responseText
});
dialog.show();
}
}); });
} }
@ -613,57 +600,58 @@ function updateFilterList() {
let search = ""; let search = "";
if (user_search) { search = user_search.value; } if (user_search) { search = user_search.value; }
new Ajax.Request("backend.php", { xhrPost("backend.php", { op: "pref-filters", search: search }, (transport) => {
parameters: "?op=pref-filters&search=" + param_escape(search), dijit.byId('filterConfigTab').attr('content', transport.responseText);
onComplete: function(transport) { notify("");
dijit.byId('filterConfigTab').attr('content', transport.responseText); });
notify("");
} });
} }
function updateLabelList() { function updateLabelList() {
new Ajax.Request("backend.php", { xhrPost("backend.php", { op: "pref-labels" }, (transport) => {
parameters: "?op=pref-labels", dijit.byId('labelConfigTab').attr('content', transport.responseText);
onComplete: function(transport) { notify("");
dijit.byId('labelConfigTab').attr('content', transport.responseText); });
notify("");
} });
} }
function updatePrefsList() { function updatePrefsList() {
new Ajax.Request("backend.php", { xhrPost("backend.php", { op: "pref-prefs" }, (transport) => {
parameters: "?op=pref-prefs", dijit.byId('genConfigTab').attr('content', transport.responseText);
onComplete: function(transport) { notify("");
dijit.byId('genConfigTab').attr('content', transport.responseText); });
notify("");
} });
} }
function updateSystemList() { function updateSystemList() {
new Ajax.Request("backend.php", { xhrPost("backend.php", { op: "pref-system" }, (transport) => {
parameters: "?op=pref-system", dijit.byId('systemConfigTab').attr('content', transport.responseText);
onComplete: function(transport) { notify("");
dijit.byId('systemConfigTab').attr('content', transport.responseText); });
notify("");
} });
} }
function selectTab(id, noupdate) { function selectTab(id, noupdate) {
if (!noupdate) { if (!noupdate) {
notify_progress("Loading, please wait..."); notify_progress("Loading, please wait...");
if (id == "feedConfig") { switch (id) {
updateFeedList(); case "feedConfig":
} else if (id == "filterConfig") { updateFeedList();
updateFilterList(); break;
} else if (id == "labelConfig") { case "filterConfig":
updateLabelList(); updateFilterList();
} else if (id == "genConfig") { break;
updatePrefsList(); case "labelConfig":
} else if (id == "userConfig") { updateLabelList();
updateUsersList(); break;
} else if (id == "systemConfig") { case "genConfig":
updateSystemList(); updatePrefsList();
break;
case "userConfig":
updateUsersList();
break;
case "systemConfig":
updateSystemList();
break;
default:
console.warn("unknown tab", id);
} }
const tab = dijit.byId(id + "Tab"); const tab = dijit.byId(id + "Tab");
@ -692,7 +680,7 @@ function init_second_stage() {
window.setTimeout(function() { editFeed(param) }, 100); window.setTimeout(function() { editFeed(param) }, 100);
} }
setTimeout(hotkey_prefix_timeout, 5*1000); setInterval(hotkey_prefix_timeout, 5*1000);
} }
function init() { function init() {
@ -765,105 +753,41 @@ function init() {
function validatePrefsReset() { function validatePrefsReset() {
const ok = confirm(__("Reset to defaults?")); if (confirm(__("Reset to defaults?"))) {
if (ok) {
const query = "?op=pref-prefs&method=resetconfig"; 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; return false;
} }
function pref_hotkey_handler(e) { function pref_hotkey_handler(e) {
if (e.target.nodeName == "INPUT" || e.target.nodeName == "TEXTAREA") return;
if (e.target.nodeName == "INPUT" || e.target.nodeName == "TEXTAREA") return; const action_name = keyevent_to_action(e);
let keycode = false; if (action_name) {
let shift_key = false; switch (action_name) {
case "feed_subscribe":
const cmdline = $('cmdline'); quickAddFeed();
return false;
try { case "create_label":
shift_key = e.shiftKey; addLabel();
} catch (e) { return false;
case "create_filter":
} quickAddFilter();
return false;
if (window.event) { case "help_dialog":
keycode = window.event.keyCode; helpDialog("main");
} else if (e) { return false;
keycode = e.which; default:
} console.log("unhandled action: " + action_name + "; 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);
} }
} }

View File

@ -1,13 +1,11 @@
/* global dijit, __ */ /* global dijit, __ */
let global_unread = -1; let global_unread = -1;
let hotkey_prefix = false;
let hotkey_prefix_pressed = false;
let hotkey_actions = {};
let _widescreen_mode = false; let _widescreen_mode = false;
let _rpc_seq = 0; let _rpc_seq = 0;
let _active_feed_id = 0; let _active_feed_id = 0;
let _active_feed_is_cat = false; let _active_feed_is_cat = false;
let hotkey_actions = {};
function next_seq() { function next_seq() {
_rpc_seq += 1; _rpc_seq += 1;
@ -596,7 +594,7 @@ function init_second_stage() {
if ('sessionStorage' in window && window['sessionStorage'] !== null) if ('sessionStorage' in window && window['sessionStorage'] !== null)
sessionStorage.clear(); sessionStorage.clear();
const hotkeys = getInitParam("hotkeys"); /*const hotkeys = getInitParam("hotkeys");
const tmp = []; const tmp = [];
for (const sequence in hotkeys[1]) { for (const sequence in hotkeys[1]) {
@ -605,7 +603,7 @@ function init_second_stage() {
} }
hotkeys[1] = tmp; hotkeys[1] = tmp;
setInitParam("hotkeys", hotkeys); setInitParam("hotkeys", hotkeys);*/
_widescreen_mode = getInitParam("widescreen"); _widescreen_mode = getInitParam("widescreen");
switchPanelMode(_widescreen_mode); switchPanelMode(_widescreen_mode);
@ -767,65 +765,18 @@ function viewModeChanged() {
} }
function hotkey_handler(e) { function hotkey_handler(e) {
if (e.target.nodeName == "INPUT" || e.target.nodeName == "TEXTAREA") return; if (e.target.nodeName == "INPUT" || e.target.nodeName == "TEXTAREA") return;
let keycode = e.which; const action_name = keyevent_to_action(e);
if (keycode == 27) { // escape and drop prefix if (action_name) {
hotkey_prefix = false; const action_func = hotkey_actions[action_name];
}
if (keycode == 16 || keycode == 17) return; // ignore lone shift / ctrl if (action_func != null) {
action_func();
const hotkeys = getInitParam("hotkeys"); e.stopPropagation();
const keychar = String.fromCharCode(keycode).toLowerCase(); return false;
}
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");
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;
for (const sequence in hotkeys[1]) {
if (sequence == hotkey) {
hotkey_action = hotkeys[1][sequence];
break;
}
}
const action = hotkey_actions[hotkey_action];
if (action != null) {
action();
e.stopPropagation();
return false;
} }
} }