2020-06-05 04:44:57 +00:00
|
|
|
'use strict';
|
2018-11-29 18:03:55 +00:00
|
|
|
|
2020-06-05 04:44:57 +00:00
|
|
|
/* global dijit, __, App, Ajax */
|
2021-02-12 16:02:09 +00:00
|
|
|
/* eslint-disable no-new */
|
2008-05-15 04:21:00 +00:00
|
|
|
|
2021-02-18 18:51:18 +00:00
|
|
|
Element.prototype.hasClassName = function(className) {
|
|
|
|
dojo.hasClass(this, className);
|
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.addClassName = function(className) {
|
|
|
|
dojo.addClass(this, className);
|
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.removeClassName = function(className) {
|
|
|
|
dojo.removeClass(this, className);
|
|
|
|
};
|
|
|
|
|
2021-02-18 19:05:06 +00:00
|
|
|
Element.prototype.toggleClassName = function(className) {
|
|
|
|
if (this.hasClassName(className))
|
|
|
|
this.removeClassName(className);
|
|
|
|
else
|
|
|
|
this.addClassName(className);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-02-18 18:51:18 +00:00
|
|
|
Element.prototype.setStyle = function(args) {
|
|
|
|
Object.keys(args).forEach((k) => {
|
|
|
|
this.style[k] = args[k];
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.show = function() {
|
|
|
|
this.style.display = "";
|
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.hide = function() {
|
|
|
|
this.style.display = "none";
|
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.toggle = function() {
|
|
|
|
if (this.visible())
|
|
|
|
this.show();
|
|
|
|
else
|
|
|
|
this.hide();
|
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.visible = function() {
|
|
|
|
// TODO: should we actually check for offsetWidth/offsetHeight == 0?
|
|
|
|
return this.style.display != "none";
|
|
|
|
}
|
|
|
|
|
|
|
|
Element.visible = function(elem) {
|
|
|
|
if (typeof elem == "string")
|
|
|
|
elem = document.getElementById(elem);
|
|
|
|
|
|
|
|
return elem.visible();
|
|
|
|
}
|
|
|
|
|
|
|
|
Element.show = function(elem) {
|
|
|
|
if (typeof elem == "string")
|
|
|
|
elem = document.getElementById(elem);
|
|
|
|
|
|
|
|
return elem.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
Element.hide = function(elem) {
|
|
|
|
if (typeof elem == "string")
|
|
|
|
elem = document.getElementById(elem);
|
|
|
|
|
|
|
|
return elem.hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
Element.toggle = function(elem) {
|
|
|
|
if (typeof elem == "string")
|
|
|
|
elem = document.getElementById(elem);
|
|
|
|
|
|
|
|
return elem.toggle();
|
|
|
|
}
|
2018-12-03 10:38:13 +00:00
|
|
|
|
2021-02-18 18:51:18 +00:00
|
|
|
Element.hasClassName = function (id, className) {
|
|
|
|
return document.getElementById(id).hasClassName(className);
|
|
|
|
}
|
2018-12-03 10:38:13 +00:00
|
|
|
|
2018-11-29 19:21:09 +00:00
|
|
|
/* xhr shorthand helpers */
|
|
|
|
|
2020-06-05 04:44:57 +00:00
|
|
|
/* exported xhrPost */
|
2021-02-18 18:51:18 +00:00
|
|
|
function xhrPost(url, params = {}, complete = undefined) {
|
2018-11-29 19:21:09 +00:00
|
|
|
console.log("xhrPost:", params);
|
2018-12-11 05:42:38 +00:00
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2021-02-18 18:51:18 +00:00
|
|
|
if (typeof __csrf_token != "undefined")
|
|
|
|
params = {...params, ...{csrf_token: __csrf_token}};
|
|
|
|
|
|
|
|
dojo.xhrPost({url: url,
|
|
|
|
postData: dojo.objectToQuery(params),
|
|
|
|
handleAs: "text",
|
|
|
|
error: function(error) {
|
|
|
|
reject(error);
|
|
|
|
},
|
|
|
|
load: function(data, ioargs) {
|
|
|
|
if (complete != undefined)
|
|
|
|
complete(ioargs.xhr);
|
|
|
|
|
|
|
|
resolve(ioargs.xhr)
|
|
|
|
}});
|
2018-11-30 12:07:44 +00:00
|
|
|
});
|
2018-11-29 19:21:09 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 18:51:18 +00:00
|
|
|
Array.prototype.remove = function(s) {
|
|
|
|
for (let i=0; i < this.length; i++) {
|
|
|
|
if (s == this[i]) this.splice(i, 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Array.prototype.uniq = function() {
|
|
|
|
return this.filter((v, i, a) => a.indexOf(v) === i);
|
|
|
|
};
|
|
|
|
|
|
|
|
String.prototype.stripTags = function() {
|
|
|
|
return this.replace(/<\w+(\s+("[^"]*"|'[^']*'|[^>])+)?(\/)?>|<\/\w+>/gi, '');
|
|
|
|
}
|
|
|
|
|
2020-06-05 04:44:57 +00:00
|
|
|
/* exported xhrJson */
|
2021-02-18 18:51:18 +00:00
|
|
|
function xhrJson(url, params = {}, complete = undefined) {
|
2021-02-12 16:02:09 +00:00
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
xhrPost(url, params).then((reply) => {
|
2018-12-11 05:42:38 +00:00
|
|
|
let obj = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
obj = JSON.parse(reply.responseText);
|
|
|
|
} catch (e) {
|
|
|
|
console.error("xhrJson", e, reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (complete != undefined) complete(obj);
|
|
|
|
|
|
|
|
resolve(obj);
|
2021-02-12 16:02:09 +00:00
|
|
|
}));
|
2018-11-29 19:21:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-02 17:07:57 +00:00
|
|
|
/* common helpers not worthy of separate Dojo modules */
|
|
|
|
|
2020-06-05 04:44:57 +00:00
|
|
|
/* exported Lists */
|
2018-12-02 07:33:58 +00:00
|
|
|
const Lists = {
|
|
|
|
onRowChecked: function(elem) {
|
2018-12-02 08:25:32 +00:00
|
|
|
const checked = elem.domNode ? elem.attr("checked") : elem.checked;
|
2018-12-02 07:16:25 +00:00
|
|
|
// account for dojo checkboxes
|
|
|
|
elem = elem.domNode || elem;
|
|
|
|
|
2018-12-02 08:25:32 +00:00
|
|
|
const row = elem.up("li");
|
|
|
|
|
|
|
|
if (row)
|
|
|
|
checked ? row.addClassName("Selected") : row.removeClassName("Selected");
|
2018-12-06 06:52:12 +00:00
|
|
|
},
|
|
|
|
select: function(elemId, selected) {
|
2021-02-18 18:51:18 +00:00
|
|
|
$(elemId).querySelectorAll("li").forEach((row) => {
|
|
|
|
const checkNode = row.querySelector(".dijitCheckBox,input[type=checkbox]");
|
2018-12-06 06:52:12 +00:00
|
|
|
if (checkNode) {
|
|
|
|
const widget = dijit.getEnclosingWidget(checkNode);
|
|
|
|
|
|
|
|
if (widget) {
|
|
|
|
widget.attr("checked", selected);
|
|
|
|
} else {
|
|
|
|
checkNode.checked = selected;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.onRowChecked(widget);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2018-12-02 07:16:25 +00:00
|
|
|
};
|
|
|
|
|
2020-06-05 04:44:57 +00:00
|
|
|
/* exported Tables */
|
2018-12-02 07:33:58 +00:00
|
|
|
const Tables = {
|
|
|
|
onRowChecked: function(elem) {
|
|
|
|
// account for dojo checkboxes
|
2018-12-02 08:25:32 +00:00
|
|
|
const checked = elem.domNode ? elem.attr("checked") : elem.checked;
|
2018-12-02 07:33:58 +00:00
|
|
|
elem = elem.domNode || elem;
|
|
|
|
|
2018-12-02 08:25:32 +00:00
|
|
|
const row = elem.up("tr");
|
|
|
|
|
|
|
|
if (row)
|
|
|
|
checked ? row.addClassName("Selected") : row.removeClassName("Selected");
|
|
|
|
|
|
|
|
},
|
|
|
|
select: function(elemId, selected) {
|
2021-02-18 18:51:18 +00:00
|
|
|
$(elemId).querySelector("tr").forEach((row) => {
|
|
|
|
const checkNode = row.querySelector(".dijitCheckBox,input[type=checkbox]");
|
2018-12-02 08:25:32 +00:00
|
|
|
if (checkNode) {
|
|
|
|
const widget = dijit.getEnclosingWidget(checkNode);
|
|
|
|
|
|
|
|
if (widget) {
|
|
|
|
widget.attr("checked", selected);
|
|
|
|
} else {
|
|
|
|
checkNode.checked = selected;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.onRowChecked(widget);
|
|
|
|
}
|
|
|
|
});
|
2018-12-02 07:46:09 +00:00
|
|
|
},
|
|
|
|
getSelected: function(elemId) {
|
|
|
|
const rv = [];
|
|
|
|
|
2021-02-18 18:51:18 +00:00
|
|
|
$(elemId).querySelector("tr").forEach((row) => {
|
2018-12-02 07:46:09 +00:00
|
|
|
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;
|
2018-12-02 07:33:58 +00:00
|
|
|
}
|
2018-12-02 07:46:09 +00:00
|
|
|
};
|
2018-12-02 07:33:58 +00:00
|
|
|
|
2020-06-05 04:44:57 +00:00
|
|
|
/* exported Cookie */
|
2018-12-02 17:07:57 +00:00
|
|
|
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";
|
2020-06-05 04:44:57 +00:00
|
|
|
document.cookie = name + "=; " + expires;
|
2018-12-02 17:07:57 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-02 17:44:53 +00:00
|
|
|
/* runtime notifications */
|
2020-06-05 04:44:57 +00:00
|
|
|
/* exported Notify */
|
2018-12-02 17:44:53 +00:00
|
|
|
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;
|
2018-12-02 17:56:30 +00:00
|
|
|
keep = keep || false;
|
|
|
|
|
2021-02-18 18:51:18 +00:00
|
|
|
const notify = App.byId("notify");
|
2005-09-05 12:02:00 +00:00
|
|
|
|
2018-12-02 17:44:53 +00:00
|
|
|
window.clearTimeout(this.timeout);
|
2006-05-19 05:44:23 +00:00
|
|
|
|
2018-12-02 17:44:53 +00:00
|
|
|
if (!msg) {
|
|
|
|
notify.removeClassName("visible");
|
|
|
|
return;
|
2006-05-19 05:44:23 +00:00
|
|
|
}
|
2007-03-02 19:58:58 +00:00
|
|
|
|
2018-12-02 17:44:53 +00:00
|
|
|
let msgfmt = "<span class=\"msg\">%s</span>".replace("%s", __(msg));
|
2018-12-06 09:52:44 +00:00
|
|
|
let icon = "";
|
2007-03-02 19:58:58 +00:00
|
|
|
|
2018-12-02 17:44:53 +00:00
|
|
|
notify.className = "notify";
|
2007-03-06 06:54:47 +00:00
|
|
|
|
2018-12-02 17:56:30 +00:00
|
|
|
console.warn('notify', msg, kind);
|
2013-03-18 12:44:23 +00:00
|
|
|
|
2018-12-02 17:44:53 +00:00
|
|
|
switch (kind) {
|
|
|
|
case this.KIND_INFO:
|
|
|
|
notify.addClassName("notify_info")
|
2018-12-06 09:52:44 +00:00
|
|
|
icon = "notifications";
|
2018-12-02 17:44:53 +00:00
|
|
|
break;
|
|
|
|
case this.KIND_ERROR:
|
|
|
|
notify.addClassName("notify_error");
|
2018-12-06 09:52:44 +00:00
|
|
|
icon = "error";
|
2018-12-02 17:44:53 +00:00
|
|
|
break;
|
|
|
|
case this.KIND_PROGRESS:
|
|
|
|
notify.addClassName("notify_progress");
|
2018-12-02 18:52:50 +00:00
|
|
|
icon = App.getInitParam("icon_indicator_white")
|
2018-12-02 17:44:53 +00:00
|
|
|
break;
|
2018-12-06 09:52:44 +00:00
|
|
|
default:
|
|
|
|
icon = "notifications";
|
2014-11-09 17:31:29 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 09:52:44 +00:00
|
|
|
if (icon)
|
|
|
|
if (icon.indexOf("data:image") != -1)
|
|
|
|
msgfmt = "<img src=\"%s\">".replace("%s", icon) + msgfmt;
|
|
|
|
else
|
|
|
|
msgfmt = "<i class='material-icons icon-notify'>%s</i>".replace("%s", icon) + msgfmt;
|
2014-11-09 17:31:29 +00:00
|
|
|
|
2018-12-06 12:22:52 +00:00
|
|
|
msgfmt += "<i class='material-icons icon-close' title=\"" +
|
|
|
|
__("Click to close") + "\" onclick=\"Notify.close()\">close</i>";
|
2014-11-09 17:31:29 +00:00
|
|
|
|
2018-12-02 17:44:53 +00:00
|
|
|
notify.innerHTML = msgfmt;
|
|
|
|
notify.addClassName("visible");
|
|
|
|
|
|
|
|
if (!keep)
|
|
|
|
this.timeout = window.setTimeout(() => {
|
|
|
|
notify.removeClassName("visible");
|
|
|
|
}, this.default_timeout);
|
|
|
|
|
|
|
|
},
|
|
|
|
info: function(msg, keep) {
|
2018-12-02 17:56:30 +00:00
|
|
|
keep = keep || false;
|
2018-12-02 17:44:53 +00:00
|
|
|
this.msg(msg, keep, this.KIND_INFO);
|
|
|
|
},
|
|
|
|
progress: function(msg, keep) {
|
2018-12-02 17:56:30 +00:00
|
|
|
keep = keep || true;
|
2018-12-02 17:44:53 +00:00
|
|
|
this.msg(msg, keep, this.KIND_PROGRESS);
|
|
|
|
},
|
2018-12-02 17:56:30 +00:00
|
|
|
error: function(msg, keep) {
|
|
|
|
keep = keep || true;
|
|
|
|
this.msg(msg, keep, this.KIND_ERROR);
|
2018-12-02 17:44:53 +00:00
|
|
|
}
|
|
|
|
};
|
2006-05-18 12:01:09 +00:00
|
|
|
|
2013-11-12 10:21:28 +00:00
|
|
|
// http://stackoverflow.com/questions/6251937/how-to-get-selecteduser-highlighted-text-in-contenteditable-element-and-replac
|
2020-06-05 04:44:57 +00:00
|
|
|
/* exported getSelectionText */
|
2013-11-12 10:21:28 +00:00
|
|
|
function getSelectionText() {
|
2018-11-29 17:07:23 +00:00
|
|
|
let text = "";
|
2013-11-12 10:21:28 +00:00
|
|
|
|
|
|
|
if (typeof window.getSelection != "undefined") {
|
2018-11-29 17:07:23 +00:00
|
|
|
const sel = window.getSelection();
|
2013-11-12 10:21:28 +00:00
|
|
|
if (sel.rangeCount) {
|
2018-11-29 17:07:23 +00:00
|
|
|
const container = document.createElement("div");
|
|
|
|
for (let i = 0, len = sel.rangeCount; i < len; ++i) {
|
2013-11-12 10:21:28 +00:00
|
|
|
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();
|
|
|
|
}
|