From f89924f7a19871e26d5805a6c1863903c6e474bf Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 2 Dec 2018 18:38:27 +0300 Subject: set use strict on JS modules; remove some mostly useless stuff like get_minified_js() --- js/common.js | 493 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 493 insertions(+) create mode 100755 js/common.js (limited to 'js/common.js') diff --git a/js/common.js b/js/common.js new file mode 100755 index 000000000..b63257099 --- /dev/null +++ b/js/common.js @@ -0,0 +1,493 @@ +'use strict' +/* global dijit, __ */ + +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) { + + if (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"] = getInitParam("csrf_token"); + } + + return callOriginal(options); + } +); + +/* xhr shorthand helpers */ + +function xhrPost(url, params, complete) { + console.log("xhrPost:", params); + return new Ajax.Request(url, { + parameters: params, + onComplete: complete + }); +} + +function xhrJson(url, params, complete) { + return 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) { + for (let i=0; i < this.length; i++) { + if (s == this[i]) this.splice(i, 1); + } +}; + +const Lists = { + onRowChecked: function(elem) { + const checked = elem.domNode ? elem.attr("checked") : elem.checked; + // account for dojo checkboxes + elem = elem.domNode || elem; + + const row = elem.up("li"); + + if (row) + checked ? row.addClassName("Selected") : row.removeClassName("Selected"); + } +}; + +// noinspection JSUnusedGlobalSymbols +const Tables = { + onRowChecked: function(elem) { + // account for dojo checkboxes + const checked = elem.domNode ? elem.attr("checked") : elem.checked; + elem = elem.domNode || elem; + + const row = elem.up("tr"); + + if (row) + checked ? row.addClassName("Selected") : row.removeClassName("Selected"); + + }, + select: function(elemId, selected) { + $(elemId).select("tr").each((row) => { + const checkNode = row.select(".dijitCheckBox,input[type=checkbox]")[0]; + if (checkNode) { + const widget = dijit.getEnclosingWidget(checkNode); + + if (widget) { + widget.attr("checked", selected); + } else { + checkNode.checked = selected; + } + + this.onRowChecked(widget); + } + }); + }, + getSelected: function(elemId) { + const rv = []; + + $(elemId).select("tr").each((row) => { + if (row.hasClassName("Selected")) { + // either older prefix-XXX notation or separate attribute + const rowId = row.getAttribute("data-row-id") || row.id.replace(/^[A-Z]*?-/, ""); + + if (!isNaN(rowId)) + rv.push(parseInt(rowId)); + } + }); + + return rv; + } +}; + +function report_error(message, filename, lineno, colno, error) { + exception_error(error, null, filename, lineno); +} + +function exception_error(e, e_compat, filename, lineno, colno) { + if (typeof e == "string") e = e_compat; + + if (!e) return; // no exception object, nothing to report. + + try { + console.error(e); + const msg = e.toString(); + + try { + xhrPost("backend.php", + {op: "rpc", method: "log", + file: e.fileName ? e.fileName : filename, + line: e.lineNumber ? e.lineNumber : lineno, + msg: msg, context: e.stack}, + (transport) => { + console.warn(transport.responseText); + }); + + } catch (e) { + console.error("Exception while trying to log the error.", e); + } + + let content = "

" + msg + "

"; + + if (e.stack) { + content += "
Stack trace:
" + + ""; + } + + content += "
"; + + content += "
"; + + content += ""; + content += "
"; + + if (dijit.byId("exceptionDlg")) + dijit.byId("exceptionDlg").destroyRecursive(); + + const dialog = new dijit.Dialog({ + id: "exceptionDlg", + title: "Unhandled exception", + style: "width: 600px", + content: content}); + + dialog.show(); + + } catch (ei) { + console.error("Exception while trying to report an exception:", ei); + console.error("Original exception:", e); + + alert("Exception occured while trying to report an exception.\n" + + ei.stack + "\n\nOriginal exception:\n" + e.stack); + } + +} + +function param_escape(arg) { + return encodeURIComponent(arg); +} + +function notify_real(msg, no_hide, n_type) { + + const n = $("notify"); + + if (!n) return; + + if (notify_hide_timerid) { + window.clearTimeout(notify_hide_timerid); + } + + if (msg == "") { + if (n.hasClassName("visible")) { + notify_hide_timerid = window.setTimeout(function() { + n.removeClassName("visible") }, 0); + } + return; + } + + /* types: + + 1 - generic + 2 - progress + 3 - error + 4 - info + + */ + + msg = " " + __(msg) + ""; + + if (n_type == 2) { + msg = "" + msg; + no_hide = true; + } else if (n_type == 3) { + msg = "" + msg; + } else if (n_type == 4) { + msg = "" + msg; + } + + msg += " "; + + n.innerHTML = msg; + + window.setTimeout(function() { + // goddamnit firefox + if (n_type == 2) { + n.className = "notify notify_progress visible"; + } else if (n_type == 3) { + n.className = "notify notify_error visible"; + msg = "" + msg; + } else if (n_type == 4) { + n.className = "notify notify_info visible"; + } else { + n.className = "notify visible"; + } + + if (!no_hide) { + notify_hide_timerid = window.setTimeout(function() { + n.removeClassName("visible") }, 5*1000); + } + + }, 10); + +} + +function notify(msg, no_hide) { + notify_real(msg, no_hide, 1); +} + +function notify_progress(msg, no_hide) { + notify_real(msg, no_hide, 2); +} + +function notify_error(msg, no_hide) { + notify_real(msg, no_hide, 3); + +} + +function notify_info(msg, no_hide) { + notify_real(msg, no_hide, 4); +} + +function setCookie(name, value, lifetime, path, domain, secure) { + + let d = false; + + if (lifetime) { + d = new Date(); + d.setTime(d.getTime() + (lifetime * 1000)); + } + + console.log("setCookie: " + name + " => " + value + ": " + d); + + int_setCookie(name, value, d, path, domain, secure); + +} + +function int_setCookie(name, value, expires, path, domain, secure) { + document.cookie= name + "=" + escape(value) + + ((expires) ? "; expires=" + expires.toGMTString() : "") + + ((path) ? "; path=" + path : "") + + ((domain) ? "; domain=" + domain : "") + + ((secure) ? "; secure" : ""); +} + +function delCookie(name, path, domain) { + if (getCookie(name)) { + document.cookie = name + "=" + + ((path) ? ";path=" + path : "") + + ((domain) ? ";domain=" + domain : "" ) + + ";expires=Thu, 01-Jan-1970 00:00:01 GMT"; + } +} + + +function getCookie(name) { + + const dc = document.cookie; + const prefix = name + "="; + let begin = dc.indexOf("; " + prefix); + if (begin == -1) { + begin = dc.indexOf(prefix); + if (begin != 0) return null; + } + else { + begin += 2; + } + let end = document.cookie.indexOf(";", begin); + if (end == -1) { + end = dc.length; + } + return unescape(dc.substring(begin + prefix.length, end)); +} + +// noinspection JSUnusedGlobalSymbols +function displayIfChecked(checkbox, elemId) { + if (checkbox.checked) { + Effect.Appear(elemId, {duration : 0.5}); + } else { + Effect.Fade(elemId, {duration : 0.5}); + } +} + +// noinspection JSUnusedGlobalSymbols +function closeInfoBox() { + const dialog = dijit.byId("infoBox"); + + if (dialog) dialog.hide(); + + return false; +} + +function getInitParam(key) { + return init_params[key]; +} + +function setInitParam(key, value) { + init_params[key] = value; +} + +function fatalError(code, msg, ext_info) { + if (code == 6) { + window.location.href = "index.php"; + } else if (code == 5) { + window.location.href = "public.php?op=dbupdate"; + } else { + + if (msg == "") msg = "Unknown error"; + + if (ext_info) { + if (ext_info.responseText) { + ext_info = ext_info.responseText; + } + } + + /* global ERRORS */ + if (ERRORS && ERRORS[code] && !msg) { + msg = ERRORS[code]; + } + + let content = "
Error code: " + code + "
" + + "

" + msg + "

"; + + if (ext_info) { + content = content + "
Additional information:
" + + ""; + } + + const dialog = new dijit.Dialog({ + title: "Fatal error", + style: "width: 600px", + content: content}); + + dialog.show(); + + } + + return false; + +} + +/* function strip_tags(s) { + return s.replace(/<\/?[^>]+(>|$)/g, ""); +} */ + +// noinspection JSUnusedGlobalSymbols +function uploadIconHandler(rc) { + switch (rc) { + case 0: + notify_info("Upload complete."); + if (App.isPrefs()) { + Feeds.reload(); + } else { + setTimeout('Feeds.reload(false, false)', 50); + } + break; + case 1: + notify_error("Upload failed: icon is too big."); + break; + case 2: + notify_error("Upload failed."); + break; + } +} + +// noinspection JSUnusedGlobalSymbols +function removeFeedIcon(id) { + if (confirm(__("Remove stored feed icon?"))) { + + notify_progress("Removing feed icon...", true); + + const query = { op: "pref-feeds", method: "removeicon", feed_id: id }; + + xhrPost("backend.php", query, () => { + notify_info("Feed icon removed."); + if (App.isPrefs()) { + Feeds.reload(); + } else { + setTimeout('Feeds.reload(false, false)', 50); + } + }); + } + + return false; +} + +// noinspection JSUnusedGlobalSymbols +function uploadFeedIcon() { + const file = $("icon_file"); + + if (file.value.length == 0) { + alert(__("Please select an image file to upload.")); + } else if (confirm(__("Upload new icon for this feed?"))) { + notify_progress("Uploading, please wait...", true); + return true; + } + + return false; +} + +// noinspection JSUnusedGlobalSymbols +function label_to_feed_id(label) { + return _label_base_index - 1 - Math.abs(label); +} + +// noinspection JSUnusedGlobalSymbols +function feed_to_label_id(feed) { + return _label_base_index - 1 + Math.abs(feed); +} + +// http://stackoverflow.com/questions/6251937/how-to-get-selecteduser-highlighted-text-in-contenteditable-element-and-replac +function getSelectionText() { + let text = ""; + + if (typeof window.getSelection != "undefined") { + const sel = window.getSelection(); + if (sel.rangeCount) { + const container = document.createElement("div"); + for (let i = 0, len = sel.rangeCount; i < len; ++i) { + container.appendChild(sel.getRangeAt(i).cloneContents()); + } + text = container.innerHTML; + } + } else if (typeof document.selection != "undefined") { + if (document.selection.type == "Text") { + text = document.selection.createRange().textText; + } + } + + return text.stripTags(); +} + +// noinspection JSUnusedGlobalSymbols +function popupOpenUrl(url) { + const w = window.open(""); + + w.opener = null; + w.location = url; +} + +// noinspection JSUnusedGlobalSymbols +function popupOpenArticle(id) { + const w = window.open("", + "ttrss_article_popup", + "height=900,width=900,resizable=yes,status=no,location=no,menubar=no,directories=no,scrollbars=yes,toolbar=no"); + + w.opener = null; + w.location = "backend.php?op=article&method=view&mode=raw&html=1&zoom=1&id=" + id + "&csrf_token=" + getInitParam("csrf_token"); +} -- cgit v1.2.3 From d9c5c93cef313127b9b5010fbe279fdbddedadec Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 2 Dec 2018 20:07:57 +0300 Subject: move some more stuff out of common.js rework client-side cookie functions a bit limit dojo cachebust based on server scripts modification time remove param_escape() --- js/common.js | 126 +++++++++++++---------------------------------------------- 1 file changed, 27 insertions(+), 99 deletions(-) (limited to 'js/common.js') diff --git a/js/common.js b/js/common.js index b63257099..9a3c1715d 100755 --- a/js/common.js +++ b/js/common.js @@ -54,6 +54,8 @@ Array.prototype.remove = function(s) { } }; +/* common helpers not worthy of separate Dojo modules */ + const Lists = { onRowChecked: function(elem) { const checked = elem.domNode ? elem.attr("checked") : elem.checked; @@ -113,6 +115,31 @@ const Tables = { } }; +const Cookie = { + set: function (name, value, lifetime) { + const d = new Date(); + d.setTime(d.getTime() + lifetime * 1000); + const expires = "expires=" + d.toUTCString(); + document.cookie = name + "=" + encodeURIComponent(value) + "; " + expires; + }, + get: function (name) { + name = name + "="; + const ca = document.cookie.split(';'); + for (let i=0; i < ca.length; i++) { + let c = ca[i]; + while (c.charAt(0) == ' ') c = c.substring(1); + if (c.indexOf(name) == 0) return decodeURIComponent(c.substring(name.length, c.length)); + } + return ""; + }, + delete: function(name) { + const expires = "expires=Thu, 01-Jan-1970 00:00:01 GMT"; + document.cookie = name + "=" + "" + "; " + expires; + } +}; + +/* error reporting */ + function report_error(message, filename, lineno, colno, error) { exception_error(error, null, filename, lineno); } @@ -177,10 +204,6 @@ function exception_error(e, e_compat, filename, lineno, colno) { } -function param_escape(arg) { - return encodeURIComponent(arg); -} - function notify_real(msg, no_hide, n_type) { const n = $("notify"); @@ -263,58 +286,6 @@ function notify_info(msg, no_hide) { notify_real(msg, no_hide, 4); } -function setCookie(name, value, lifetime, path, domain, secure) { - - let d = false; - - if (lifetime) { - d = new Date(); - d.setTime(d.getTime() + (lifetime * 1000)); - } - - console.log("setCookie: " + name + " => " + value + ": " + d); - - int_setCookie(name, value, d, path, domain, secure); - -} - -function int_setCookie(name, value, expires, path, domain, secure) { - document.cookie= name + "=" + escape(value) + - ((expires) ? "; expires=" + expires.toGMTString() : "") + - ((path) ? "; path=" + path : "") + - ((domain) ? "; domain=" + domain : "") + - ((secure) ? "; secure" : ""); -} - -function delCookie(name, path, domain) { - if (getCookie(name)) { - document.cookie = name + "=" + - ((path) ? ";path=" + path : "") + - ((domain) ? ";domain=" + domain : "" ) + - ";expires=Thu, 01-Jan-1970 00:00:01 GMT"; - } -} - - -function getCookie(name) { - - const dc = document.cookie; - const prefix = name + "="; - let begin = dc.indexOf("; " + prefix); - if (begin == -1) { - begin = dc.indexOf(prefix); - if (begin != 0) return null; - } - else { - begin += 2; - } - let end = document.cookie.indexOf(";", begin); - if (end == -1) { - end = dc.length; - } - return unescape(dc.substring(begin + prefix.length, end)); -} - // noinspection JSUnusedGlobalSymbols function displayIfChecked(checkbox, elemId) { if (checkbox.checked) { @@ -324,15 +295,6 @@ function displayIfChecked(checkbox, elemId) { } } -// noinspection JSUnusedGlobalSymbols -function closeInfoBox() { - const dialog = dijit.byId("infoBox"); - - if (dialog) dialog.hide(); - - return false; -} - function getInitParam(key) { return init_params[key]; } @@ -407,40 +369,6 @@ function uploadIconHandler(rc) { } } -// noinspection JSUnusedGlobalSymbols -function removeFeedIcon(id) { - if (confirm(__("Remove stored feed icon?"))) { - - notify_progress("Removing feed icon...", true); - - const query = { op: "pref-feeds", method: "removeicon", feed_id: id }; - - xhrPost("backend.php", query, () => { - notify_info("Feed icon removed."); - if (App.isPrefs()) { - Feeds.reload(); - } else { - setTimeout('Feeds.reload(false, false)', 50); - } - }); - } - - return false; -} - -// noinspection JSUnusedGlobalSymbols -function uploadFeedIcon() { - const file = $("icon_file"); - - if (file.value.length == 0) { - alert(__("Please select an image file to upload.")); - } else if (confirm(__("Upload new icon for this feed?"))) { - notify_progress("Uploading, please wait...", true); - return true; - } - - return false; -} // noinspection JSUnusedGlobalSymbols function label_to_feed_id(label) { -- cgit v1.2.3 From 495248dd9783985750591c050d055ac4c2cf1712 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 2 Dec 2018 20:44:53 +0300 Subject: add Notify object --- js/common.js | 112 +++++++++++++++++++++++++++++++---------------------------- 1 file changed, 59 insertions(+), 53 deletions(-) (limited to 'js/common.js') diff --git a/js/common.js b/js/common.js index 9a3c1715d..4b5165979 100755 --- a/js/common.js +++ b/js/common.js @@ -201,73 +201,79 @@ function exception_error(e, e_compat, filename, lineno, colno) { alert("Exception occured while trying to report an exception.\n" + ei.stack + "\n\nOriginal exception:\n" + e.stack); } - } -function notify_real(msg, no_hide, n_type) { - - const n = $("notify"); - - if (!n) return; +/* runtime notifications */ + +const Notify = { + KIND_GENERIC: 0, + KIND_INFO: 1, + KIND_ERROR: 2, + KIND_PROGRESS: 3, + timeout: 0, + default_timeout: 5 * 1000, + close: function() { + this.msg(""); + }, + msg: function(msg, keep, kind) { + kind = kind || this.KIND_GENERIC; + const notify = $("notify"); - if (notify_hide_timerid) { - window.clearTimeout(notify_hide_timerid); - } + window.clearTimeout(this.timeout); - if (msg == "") { - if (n.hasClassName("visible")) { - notify_hide_timerid = window.setTimeout(function() { - n.removeClassName("visible") }, 0); + if (!msg) { + notify.removeClassName("visible"); + return; } - return; - } - - /* types: - 1 - generic - 2 - progress - 3 - error - 4 - info + let msgfmt = "%s".replace("%s", __(msg)); + let icon = false; - */ - msg = " " + __(msg) + ""; + notify.className = "notify"; - if (n_type == 2) { - msg = "" + msg; - no_hide = true; - } else if (n_type == 3) { - msg = "" + msg; - } else if (n_type == 4) { - msg = "" + msg; - } + console.log('notify', msg, kind); - msg += " "; - - n.innerHTML = msg; - - window.setTimeout(function() { - // goddamnit firefox - if (n_type == 2) { - n.className = "notify notify_progress visible"; - } else if (n_type == 3) { - n.className = "notify notify_error visible"; - msg = "" + msg; - } else if (n_type == 4) { - n.className = "notify notify_info visible"; - } else { - n.className = "notify visible"; + switch (kind) { + case this.KIND_INFO: + notify.addClassName("notify_info") + icon = getInitParam("icon_information"); + break; + case this.KIND_ERROR: + notify.addClassName("notify_error"); + icon = getInitParam("icon_alert"); + break; + case this.KIND_PROGRESS: + notify.addClassName("notify_progress"); + icon = getInitParam("icon_indicator_white") + break; } - if (!no_hide) { - notify_hide_timerid = window.setTimeout(function() { - n.removeClassName("visible") }, 5*1000); - } + if (icon) msgfmt = "".replace("%s", icon) + msgfmt; - }, 10); + msgfmt += (" ") + .replace("%s", getInitParam("icon_cross")); -} + notify.innerHTML = msgfmt; + notify.addClassName("visible"); + + if (!keep) + this.timeout = window.setTimeout(() => { + notify.removeClassName("visible"); + }, this.default_timeout); + + }, + info: function(msg, keep) { + this.msg(msg, keep, this.KIND_INFO); + }, + progress: function(msg, keep) { + this.msg(msg, keep, this.KIND_PROGRESS); + }, + error: function(msg) { + this.msg(msg, true, this.KIND_ERROR); + } +}; function notify(msg, no_hide) { notify_real(msg, no_hide, 1); -- cgit v1.2.3 From 526389b2d380177490605037fdc3a24ebc688fac Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 2 Dec 2018 20:56:30 +0300 Subject: update notify_* calls to use Notify --- js/common.js | 36 ++++++++++++------------------------ 1 file changed, 12 insertions(+), 24 deletions(-) (limited to 'js/common.js') diff --git a/js/common.js b/js/common.js index 4b5165979..ec6381e31 100755 --- a/js/common.js +++ b/js/common.js @@ -217,6 +217,8 @@ const Notify = { }, msg: function(msg, keep, kind) { kind = kind || this.KIND_GENERIC; + keep = keep || false; + const notify = $("notify"); window.clearTimeout(this.timeout); @@ -232,7 +234,7 @@ const Notify = { notify.className = "notify"; - console.log('notify', msg, kind); + console.warn('notify', msg, kind); switch (kind) { case this.KIND_INFO: @@ -252,7 +254,7 @@ const Notify = { if (icon) msgfmt = "".replace("%s", icon) + msgfmt; msgfmt += (" ") + __("Click to close") + "\" onclick=\"Notify.close()\">") .replace("%s", getInitParam("icon_cross")); notify.innerHTML = msgfmt; @@ -265,33 +267,19 @@ const Notify = { }, info: function(msg, keep) { + keep = keep || false; this.msg(msg, keep, this.KIND_INFO); }, progress: function(msg, keep) { + keep = keep || true; this.msg(msg, keep, this.KIND_PROGRESS); }, - error: function(msg) { - this.msg(msg, true, this.KIND_ERROR); + error: function(msg, keep) { + keep = keep || true; + this.msg(msg, keep, this.KIND_ERROR); } }; -function notify(msg, no_hide) { - notify_real(msg, no_hide, 1); -} - -function notify_progress(msg, no_hide) { - notify_real(msg, no_hide, 2); -} - -function notify_error(msg, no_hide) { - notify_real(msg, no_hide, 3); - -} - -function notify_info(msg, no_hide) { - notify_real(msg, no_hide, 4); -} - // noinspection JSUnusedGlobalSymbols function displayIfChecked(checkbox, elemId) { if (checkbox.checked) { @@ -359,7 +347,7 @@ function fatalError(code, msg, ext_info) { function uploadIconHandler(rc) { switch (rc) { case 0: - notify_info("Upload complete."); + Notify.info("Upload complete."); if (App.isPrefs()) { Feeds.reload(); } else { @@ -367,10 +355,10 @@ function uploadIconHandler(rc) { } break; case 1: - notify_error("Upload failed: icon is too big."); + Notify.error("Upload failed: icon is too big."); break; case 2: - notify_error("Upload failed."); + Notify.error("Upload failed."); break; } } -- cgit v1.2.3 From eeb49d375ce7e6addc382bab1a1545e897bb1771 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 2 Dec 2018 20:57:51 +0300 Subject: uploadIconHandler -> CommonDialogs --- js/common.js | 21 --------------------- 1 file changed, 21 deletions(-) (limited to 'js/common.js') diff --git a/js/common.js b/js/common.js index ec6381e31..1da3e6d1b 100755 --- a/js/common.js +++ b/js/common.js @@ -343,27 +343,6 @@ function fatalError(code, msg, ext_info) { return s.replace(/<\/?[^>]+(>|$)/g, ""); } */ -// noinspection JSUnusedGlobalSymbols -function uploadIconHandler(rc) { - switch (rc) { - case 0: - Notify.info("Upload complete."); - if (App.isPrefs()) { - Feeds.reload(); - } else { - setTimeout('Feeds.reload(false, false)', 50); - } - break; - case 1: - Notify.error("Upload failed: icon is too big."); - break; - case 2: - Notify.error("Upload failed."); - break; - } -} - - // noinspection JSUnusedGlobalSymbols function label_to_feed_id(label) { return _label_base_index - 1 - Math.abs(label); -- cgit v1.2.3 From ac8361e6f6e81e25d3c17e14b973af53a9b93885 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 2 Dec 2018 21:52:50 +0300 Subject: add AppBase as a shared ancestor for main and prefs app objects remove event.observe stuff from startup, unneeded --- js/common.js | 38 +++++--------------------------------- 1 file changed, 5 insertions(+), 33 deletions(-) (limited to 'js/common.js') diff --git a/js/common.js b/js/common.js index 1da3e6d1b..de6d13a78 100755 --- a/js/common.js +++ b/js/common.js @@ -1,28 +1,8 @@ 'use strict' /* global dijit, __ */ -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) { - - if (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"] = getInitParam("csrf_token"); - } - - return callOriginal(options); - } -); /* xhr shorthand helpers */ @@ -239,15 +219,15 @@ const Notify = { switch (kind) { case this.KIND_INFO: notify.addClassName("notify_info") - icon = getInitParam("icon_information"); + icon = App.getInitParam("icon_information"); break; case this.KIND_ERROR: notify.addClassName("notify_error"); - icon = getInitParam("icon_alert"); + icon = App.getInitParam("icon_alert"); break; case this.KIND_PROGRESS: notify.addClassName("notify_progress"); - icon = getInitParam("icon_indicator_white") + icon = App.getInitParam("icon_indicator_white") break; } @@ -255,7 +235,7 @@ const Notify = { msgfmt += (" ") - .replace("%s", getInitParam("icon_cross")); + .replace("%s", App.getInitParam("icon_cross")); notify.innerHTML = msgfmt; notify.addClassName("visible"); @@ -289,14 +269,6 @@ function displayIfChecked(checkbox, elemId) { } } -function getInitParam(key) { - return init_params[key]; -} - -function setInitParam(key, value) { - init_params[key] = value; -} - function fatalError(code, msg, ext_info) { if (code == 6) { window.location.href = "index.php"; @@ -390,5 +362,5 @@ function popupOpenArticle(id) { "height=900,width=900,resizable=yes,status=no,location=no,menubar=no,directories=no,scrollbars=yes,toolbar=no"); 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=" + App.getInitParam("csrf_token"); } -- cgit v1.2.3 From 71fc6d45bd761a9d2715faa68f2b8c0271ee7169 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Mon, 3 Dec 2018 13:38:13 +0300 Subject: refactor error reporting to AppBase; keep exception_error() for now as a shim --- js/common.js | 75 ++++++++---------------------------------------------------- 1 file changed, 10 insertions(+), 65 deletions(-) (limited to 'js/common.js') diff --git a/js/common.js b/js/common.js index de6d13a78..427e3034c 100755 --- a/js/common.js +++ b/js/common.js @@ -4,6 +4,16 @@ let _label_base_index = -1024; let loading_progress = 0; +/* error reporting shim */ + +// TODO: deprecated; remove +function exception_error(e, e_compat, filename, lineno, colno) { + if (typeof e == "string") + e = e_compat; + + App.Error.report(e, {filename: filename, lineno: lineno, colno: colno}); +} + /* xhr shorthand helpers */ function xhrPost(url, params, complete) { @@ -118,71 +128,6 @@ const Cookie = { } }; -/* error reporting */ - -function report_error(message, filename, lineno, colno, error) { - exception_error(error, null, filename, lineno); -} - -function exception_error(e, e_compat, filename, lineno, colno) { - if (typeof e == "string") e = e_compat; - - if (!e) return; // no exception object, nothing to report. - - try { - console.error(e); - const msg = e.toString(); - - try { - xhrPost("backend.php", - {op: "rpc", method: "log", - file: e.fileName ? e.fileName : filename, - line: e.lineNumber ? e.lineNumber : lineno, - msg: msg, context: e.stack}, - (transport) => { - console.warn(transport.responseText); - }); - - } catch (e) { - console.error("Exception while trying to log the error.", e); - } - - let content = "

" + msg + "

"; - - if (e.stack) { - content += "
Stack trace:
" + - ""; - } - - content += "
"; - - content += "
"; - - content += ""; - content += "
"; - - if (dijit.byId("exceptionDlg")) - dijit.byId("exceptionDlg").destroyRecursive(); - - const dialog = new dijit.Dialog({ - id: "exceptionDlg", - title: "Unhandled exception", - style: "width: 600px", - content: content}); - - dialog.show(); - - } catch (ei) { - console.error("Exception while trying to report an exception:", ei); - console.error("Original exception:", e); - - alert("Exception occured while trying to report an exception.\n" + - ei.stack + "\n\nOriginal exception:\n" + e.stack); - } -} - /* runtime notifications */ const Notify = { -- cgit v1.2.3