2018-12-02 18:52:50 +00:00
|
|
|
'use strict'
|
|
|
|
/* global __, ngettext */
|
|
|
|
define(["dojo/_base/declare"], function (declare) {
|
|
|
|
return declare("fox.AppBase", null, {
|
|
|
|
_initParams: [],
|
2018-12-02 19:08:18 +00:00
|
|
|
_rpc_seq: 0,
|
|
|
|
hotkey_prefix: 0,
|
|
|
|
hotkey_prefix_pressed: false,
|
|
|
|
hotkey_prefix_timeout: 0,
|
2018-12-03 10:38:13 +00:00
|
|
|
constructor: function() {
|
|
|
|
window.onerror = this.Error.onWindowError;
|
|
|
|
},
|
2018-12-02 18:52:50 +00:00
|
|
|
getInitParam: function(k) {
|
|
|
|
return this._initParams[k];
|
|
|
|
},
|
|
|
|
setInitParam: function(k, v) {
|
|
|
|
this._initParams[k] = v;
|
|
|
|
},
|
|
|
|
enableCsrfSupport: function() {
|
|
|
|
Ajax.Base.prototype.initialize = Ajax.Base.prototype.initialize.wrap(
|
|
|
|
function (callOriginal, options) {
|
|
|
|
|
|
|
|
if (App.getInitParam("csrf_token") != undefined) {
|
|
|
|
Object.extend(options, options || { });
|
|
|
|
|
|
|
|
if (Object.isString(options.parameters))
|
|
|
|
options.parameters = options.parameters.toQueryParams();
|
|
|
|
else if (Object.isHash(options.parameters))
|
|
|
|
options.parameters = options.parameters.toObject();
|
|
|
|
|
|
|
|
options.parameters["csrf_token"] = App.getInitParam("csrf_token");
|
|
|
|
}
|
|
|
|
|
|
|
|
return callOriginal(options);
|
|
|
|
}
|
|
|
|
);
|
2018-12-02 19:08:18 +00:00
|
|
|
},
|
|
|
|
urlParam: function(param) {
|
|
|
|
return String(window.location.href).parseQuery()[param];
|
|
|
|
},
|
|
|
|
next_seq: function() {
|
|
|
|
this._rpc_seq += 1;
|
|
|
|
return this._rpc_seq;
|
|
|
|
},
|
|
|
|
get_seq: function() {
|
|
|
|
return this._rpc_seq;
|
|
|
|
},
|
|
|
|
setLoadingProgress: function(p) {
|
|
|
|
loading_progress += p;
|
|
|
|
|
|
|
|
if (dijit.byId("loading_bar"))
|
|
|
|
dijit.byId("loading_bar").update({progress: loading_progress});
|
|
|
|
|
2018-12-09 18:17:50 +00:00
|
|
|
if (loading_progress >= 90) {
|
|
|
|
$("overlay").hide();
|
|
|
|
}
|
2018-12-02 19:08:18 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
keyeventToAction: function(event) {
|
|
|
|
|
|
|
|
const hotkeys_map = App.getInitParam("hotkeys");
|
|
|
|
const keycode = event.which;
|
|
|
|
const keychar = String.fromCharCode(keycode).toLowerCase();
|
|
|
|
|
|
|
|
if (keycode == 27) { // escape and drop prefix
|
|
|
|
this.hotkey_prefix = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keycode == 16 || keycode == 17) return; // ignore lone shift / ctrl
|
|
|
|
|
|
|
|
if (!this.hotkey_prefix && hotkeys_map[0].indexOf(keychar) != -1) {
|
|
|
|
|
|
|
|
this.hotkey_prefix = keychar;
|
|
|
|
$("cmdline").innerHTML = keychar;
|
|
|
|
Element.show("cmdline");
|
|
|
|
|
|
|
|
window.clearTimeout(this.hotkey_prefix_timeout);
|
|
|
|
this.hotkey_prefix_timeout = window.setTimeout(() => {
|
|
|
|
this.hotkey_prefix = false;
|
|
|
|
Element.hide("cmdline");
|
|
|
|
}, 3 * 1000);
|
|
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Element.hide("cmdline");
|
|
|
|
|
|
|
|
let hotkey_name = keychar.search(/[a-zA-Z0-9]/) != -1 ? keychar : "(" + keycode + ")";
|
|
|
|
|
|
|
|
// ensure ^*char notation
|
|
|
|
if (event.shiftKey) hotkey_name = "*" + hotkey_name;
|
|
|
|
if (event.ctrlKey) hotkey_name = "^" + hotkey_name;
|
|
|
|
if (event.altKey) hotkey_name = "+" + hotkey_name;
|
|
|
|
if (event.metaKey) hotkey_name = "%" + hotkey_name;
|
|
|
|
|
|
|
|
const hotkey_full = this.hotkey_prefix ? this.hotkey_prefix + " " + hotkey_name : hotkey_name;
|
|
|
|
this.hotkey_prefix = false;
|
|
|
|
|
|
|
|
let action_name = false;
|
|
|
|
|
|
|
|
for (const sequence in hotkeys_map[1]) {
|
|
|
|
if (hotkeys_map[1].hasOwnProperty(sequence)) {
|
|
|
|
if (sequence == hotkey_full) {
|
|
|
|
action_name = hotkeys_map[1][sequence];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('keyeventToAction', hotkey_full, '=>', action_name);
|
|
|
|
|
|
|
|
return action_name;
|
|
|
|
},
|
|
|
|
cleanupMemory: function(root) {
|
|
|
|
const dijits = dojo.query("[widgetid]", dijit.byId(root).domNode).map(dijit.byNode);
|
|
|
|
|
|
|
|
dijits.each(function (d) {
|
|
|
|
dojo.destroy(d.domNode);
|
|
|
|
});
|
|
|
|
|
|
|
|
$$("#" + root + " *").each(function (i) {
|
|
|
|
i.parentNode ? i.parentNode.removeChild(i) : true;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
helpDialog: function(topic) {
|
|
|
|
const query = "backend.php?op=backend&method=help&topic=" + encodeURIComponent(topic);
|
|
|
|
|
|
|
|
if (dijit.byId("helpDlg"))
|
|
|
|
dijit.byId("helpDlg").destroyRecursive();
|
|
|
|
|
|
|
|
const dialog = new dijit.Dialog({
|
|
|
|
id: "helpDlg",
|
|
|
|
title: __("Help"),
|
|
|
|
style: "width: 600px",
|
|
|
|
href: query,
|
|
|
|
});
|
|
|
|
|
|
|
|
dialog.show();
|
|
|
|
},
|
|
|
|
displayDlg: function(title, id, param, callback) {
|
|
|
|
Notify.progress("Loading, please wait...", true);
|
|
|
|
|
|
|
|
const query = {op: "dlg", method: id, param: param};
|
|
|
|
|
|
|
|
xhrPost("backend.php", query, (transport) => {
|
|
|
|
try {
|
|
|
|
const content = transport.responseText;
|
|
|
|
|
|
|
|
let dialog = dijit.byId("infoBox");
|
|
|
|
|
|
|
|
if (!dialog) {
|
|
|
|
dialog = new dijit.Dialog({
|
|
|
|
title: title,
|
|
|
|
id: 'infoBox',
|
|
|
|
style: "width: 600px",
|
|
|
|
onCancel: function () {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
onExecute: function () {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
onClose: function () {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
content: content
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
dialog.attr('title', title);
|
|
|
|
dialog.attr('content', content);
|
|
|
|
}
|
|
|
|
|
|
|
|
dialog.show();
|
|
|
|
|
|
|
|
Notify.close();
|
|
|
|
|
|
|
|
if (callback) callback(transport);
|
|
|
|
} catch (e) {
|
2018-12-03 10:38:13 +00:00
|
|
|
this.Error.report(e);
|
2018-12-02 19:08:18 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
handleRpcJson: function(transport) {
|
|
|
|
|
2018-12-06 09:30:11 +00:00
|
|
|
const netalert = $$("#toolbar .net-alert")[0];
|
2018-12-02 19:08:18 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const reply = JSON.parse(transport.responseText);
|
|
|
|
|
|
|
|
if (reply) {
|
|
|
|
const error = reply['error'];
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
const code = error['code'];
|
2018-12-11 10:18:38 +00:00
|
|
|
const msg = error['message'];
|
2018-12-02 19:08:18 +00:00
|
|
|
|
2018-12-11 10:18:38 +00:00
|
|
|
console.warn("[handleRpcJson] received fatal error ", code, msg);
|
2018-12-02 19:08:18 +00:00
|
|
|
|
|
|
|
if (code != 0) {
|
2018-12-11 10:18:38 +00:00
|
|
|
/* global ERRORS */
|
|
|
|
this.Error.fatal(ERRORS[code], {info: msg, code: code});
|
2018-12-02 19:08:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const seq = reply['seq'];
|
|
|
|
|
|
|
|
if (seq && this.get_seq() != seq) {
|
2018-12-16 09:41:27 +00:00
|
|
|
console.log("[handleRpcJson] sequence mismatch: ", seq, '!=', this.get_seq());
|
2018-12-02 19:08:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const message = reply['message'];
|
|
|
|
|
|
|
|
if (message == "UPDATE_COUNTERS") {
|
|
|
|
console.log("need to refresh counters...");
|
|
|
|
Feeds.requestCounters(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
const counters = reply['counters'];
|
|
|
|
|
|
|
|
if (counters)
|
|
|
|
Feeds.parseCounters(counters);
|
|
|
|
|
|
|
|
const runtime_info = reply['runtime-info'];
|
|
|
|
|
|
|
|
if (runtime_info)
|
|
|
|
App.parseRuntimeInfo(runtime_info);
|
|
|
|
|
|
|
|
if (netalert) netalert.hide();
|
|
|
|
|
|
|
|
return reply;
|
|
|
|
|
|
|
|
} else {
|
2018-12-06 09:30:11 +00:00
|
|
|
if (netalert) netalert.show();
|
|
|
|
|
|
|
|
Notify.error("Communication problem with server.");
|
2018-12-02 19:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} catch (e) {
|
2018-12-06 09:30:11 +00:00
|
|
|
if (netalert) netalert.show();
|
|
|
|
|
|
|
|
Notify.error("Communication problem with server.");
|
2018-12-02 19:08:18 +00:00
|
|
|
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
parseRuntimeInfo: function(data) {
|
|
|
|
for (const k in data) {
|
|
|
|
if (data.hasOwnProperty(k)) {
|
|
|
|
const v = data[k];
|
|
|
|
|
|
|
|
console.log("RI:", k, "=>", v);
|
|
|
|
|
|
|
|
if (k == "daemon_is_running" && v != 1) {
|
|
|
|
Notify.error("<span onclick=\"App.explainError(1)\">Update daemon is not running.</span>", true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-10 07:53:11 +00:00
|
|
|
if (k == "recent_log_events") {
|
|
|
|
const alert = $$(".log-alert")[0];
|
|
|
|
|
|
|
|
if (alert) {
|
|
|
|
v > 0 ? alert.show() : alert.hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-02 19:08:18 +00:00
|
|
|
if (k == "daemon_stamp_ok" && v != 1) {
|
|
|
|
Notify.error("<span onclick=\"App.explainError(3)\">Update daemon is not updating feeds.</span>", true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (k == "max_feed_id" || k == "num_feeds") {
|
|
|
|
if (App.getInitParam(k) != v) {
|
|
|
|
console.log("feed count changed, need to reload feedlist.");
|
|
|
|
Feeds.reload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setInitParam(k, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PluginHost.run(PluginHost.HOOK_RUNTIME_INFO_LOADED, data);
|
|
|
|
},
|
|
|
|
backendSanityCallback: function (transport) {
|
|
|
|
const reply = JSON.parse(transport.responseText);
|
|
|
|
|
2018-12-11 10:18:38 +00:00
|
|
|
/* global ERRORS */
|
|
|
|
|
2018-12-02 19:08:18 +00:00
|
|
|
if (!reply) {
|
2018-12-11 10:18:38 +00:00
|
|
|
this.Error.fatal(ERRORS[3], {info: transport.responseText});
|
2018-12-02 19:08:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-11 10:18:38 +00:00
|
|
|
if (reply['error']) {
|
|
|
|
const code = reply['error']['code'];
|
2018-12-02 19:08:18 +00:00
|
|
|
|
2018-12-11 10:18:38 +00:00
|
|
|
if (code && code != 0) {
|
|
|
|
return this.Error.fatal(ERRORS[code],
|
|
|
|
{code: code, info: reply['error']['message']});
|
|
|
|
}
|
2018-12-02 19:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
console.log("sanity check ok");
|
|
|
|
|
|
|
|
const params = reply['init-params'];
|
|
|
|
|
|
|
|
if (params) {
|
|
|
|
console.log('reading init-params...');
|
|
|
|
|
|
|
|
for (const k in params) {
|
|
|
|
if (params.hasOwnProperty(k)) {
|
|
|
|
switch (k) {
|
|
|
|
case "label_base_index":
|
|
|
|
_label_base_index = parseInt(params[k]);
|
|
|
|
break;
|
2018-12-09 13:02:20 +00:00
|
|
|
case "cdm_auto_catchup":
|
2018-12-09 13:09:51 +00:00
|
|
|
if (params[k] == 1) {
|
|
|
|
const hl = $("headlines-frame");
|
|
|
|
if (hl) hl.addClassName("auto_catchup");
|
|
|
|
}
|
2018-12-09 13:02:20 +00:00
|
|
|
break;
|
2018-12-02 19:08:18 +00:00
|
|
|
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]) {
|
|
|
|
if (params[k][1].hasOwnProperty(sequence)) {
|
|
|
|
const filtered = sequence.replace(/\|.*$/, "");
|
|
|
|
tmp[filtered] = params[k][1][sequence];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
params[k][1] = tmp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("IP:", k, "=>", params[k]);
|
|
|
|
this.setInitParam(k, params[k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PluginHost might not be available on non-index pages
|
2018-12-02 21:02:35 +00:00
|
|
|
if (typeof PluginHost !== 'undefined')
|
|
|
|
PluginHost.run(PluginHost.HOOK_PARAMS_LOADED, App._initParams);
|
2018-12-02 19:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.initSecondStage();
|
|
|
|
},
|
2018-12-09 18:17:50 +00:00
|
|
|
toggleNightMode: function() {
|
|
|
|
const link = $("theme_css");
|
|
|
|
|
|
|
|
if (link) {
|
|
|
|
|
|
|
|
let user_theme = "";
|
|
|
|
let user_css = "";
|
|
|
|
|
|
|
|
if (link.getAttribute("href").indexOf("themes/night.css") == -1) {
|
|
|
|
user_css = "themes/night.css?" + Date.now();
|
|
|
|
user_theme = "night.css";
|
|
|
|
} else {
|
|
|
|
user_theme = "default.php";
|
|
|
|
user_css = "css/default.css?" + Date.now();
|
|
|
|
}
|
|
|
|
|
2018-12-13 07:20:05 +00:00
|
|
|
$("main").fade({duration: 0.5, afterFinish: () => {
|
2018-12-09 18:17:50 +00:00
|
|
|
link.setAttribute("href", user_css);
|
2018-12-13 07:20:05 +00:00
|
|
|
$("main").appear({duration: 0.5});
|
2018-12-09 18:17:50 +00:00
|
|
|
xhrPost("backend.php", {op: "rpc", method: "setpref", key: "USER_CSS_THEME", value: user_theme});
|
2018-12-13 07:20:05 +00:00
|
|
|
}});
|
2018-12-09 18:17:50 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
},
|
2018-12-02 19:08:18 +00:00
|
|
|
explainError: function(code) {
|
|
|
|
return this.displayDlg(__("Error explained"), "explainError", code);
|
|
|
|
},
|
2018-12-03 10:38:13 +00:00
|
|
|
Error: {
|
2018-12-11 10:18:38 +00:00
|
|
|
fatal: function (error, params) {
|
|
|
|
params = params || {};
|
|
|
|
|
|
|
|
if (params.code) {
|
|
|
|
if (params.code == 6) {
|
|
|
|
window.location.href = "index.php";
|
|
|
|
return;
|
|
|
|
} else if (params.code == 5) {
|
|
|
|
window.location.href = "public.php?op=dbupdate";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.report(error,
|
|
|
|
Object.extend({title: __("Fatal error")}, params));
|
|
|
|
},
|
2018-12-03 10:38:13 +00:00
|
|
|
report: function(error, params) {
|
|
|
|
params = params || {};
|
|
|
|
|
|
|
|
if (!error) return;
|
|
|
|
|
|
|
|
console.error("[Error.report]", error, params);
|
|
|
|
|
|
|
|
const message = params.message ? params.message : error.toString();
|
|
|
|
|
|
|
|
try {
|
|
|
|
xhrPost("backend.php",
|
|
|
|
{op: "rpc", method: "log",
|
|
|
|
file: params.filename ? params.filename : error.fileName,
|
|
|
|
line: params.lineno ? params.lineno : error.lineNumber,
|
|
|
|
msg: message,
|
|
|
|
context: error.stack},
|
|
|
|
(transport) => {
|
|
|
|
console.warn("[Error.report] log response", transport.responseText);
|
|
|
|
});
|
|
|
|
} catch (re) {
|
|
|
|
console.error("[Error.report] exception while saving logging error on server", re);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (dijit.byId("exceptionDlg"))
|
|
|
|
dijit.byId("exceptionDlg").destroyRecursive();
|
|
|
|
|
2018-12-11 10:18:38 +00:00
|
|
|
let stack_msg = "";
|
|
|
|
|
|
|
|
if (error.stack)
|
|
|
|
stack_msg += `<div><b>Stack trace:</b></div>
|
|
|
|
<textarea name="stack" readonly="1">${error.stack}</textarea>`;
|
|
|
|
|
|
|
|
if (params.info)
|
|
|
|
stack_msg += `<div><b>Additional information:</b></div>
|
|
|
|
<textarea name="stack" readonly="1">${params.info}</textarea>`;
|
|
|
|
|
2018-12-11 09:45:05 +00:00
|
|
|
let content = `<div class="error-contents">
|
|
|
|
<p class="message">${message}</p>
|
2018-12-11 10:18:38 +00:00
|
|
|
${stack_msg}
|
2018-12-11 09:45:05 +00:00
|
|
|
<div class="dlgButtons">
|
|
|
|
<button dojoType="dijit.form.Button"
|
|
|
|
onclick=\"dijit.byId('exceptionDlg').hide()">${__('Close this window')}</button>
|
|
|
|
</div>
|
|
|
|
</div>`;
|
2018-12-03 10:38:13 +00:00
|
|
|
|
|
|
|
const dialog = new dijit.Dialog({
|
|
|
|
id: "exceptionDlg",
|
2018-12-11 10:18:38 +00:00
|
|
|
title: params.title || __("Unhandled exception"),
|
2018-12-03 10:38:13 +00:00
|
|
|
style: "width: 600px",
|
|
|
|
content: content
|
|
|
|
});
|
|
|
|
|
|
|
|
dialog.show();
|
|
|
|
} catch (de) {
|
|
|
|
console.error("[Error.report] exception while showing error dialog", de);
|
|
|
|
|
|
|
|
alert(error.stack ? error.stack : message);
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
onWindowError: function (message, filename, lineno, colno, error) {
|
|
|
|
// called without context (this) from window.onerror
|
|
|
|
App.Error.report(error,
|
|
|
|
{message: message, filename: filename, lineno: lineno, colno: colno});
|
|
|
|
},
|
|
|
|
}
|
2018-12-02 18:52:50 +00:00
|
|
|
});
|
|
|
|
});
|