137 lines
3.9 KiB
JavaScript
137 lines
3.9 KiB
JavaScript
DPTW.Errors = function(dptw, inputs){
|
|
|
|
const self = this;
|
|
let started = false,
|
|
thread, toast_timer,
|
|
toast_show_default,
|
|
toast_show_ok,
|
|
toast_show_info,
|
|
toast_show_warn,
|
|
toast_show_error;
|
|
|
|
let item_self = this.item_self;
|
|
|
|
const construct = () => {};
|
|
|
|
this.start = callback => {
|
|
|
|
const end = status => typeof callback == "function" && callback(status);
|
|
|
|
if(started){
|
|
end(false);
|
|
return false;
|
|
};
|
|
started = true;
|
|
|
|
end(true);
|
|
|
|
return true;
|
|
};
|
|
|
|
this.build_toast = () => {
|
|
|
|
item_self = self.item_self = dptw.kanvas.item_self.appendChild(document.createElement("ul"));
|
|
|
|
item_self.setAttribute("class", "toast");
|
|
|
|
toast_timer = dptw.settings("toast_timer");
|
|
toast_show_default = dptw.settings("toast_show_default");
|
|
toast_show_ok = dptw.settings("toast_show_ok");
|
|
toast_show_info = dptw.settings("toast_show_info");
|
|
toast_show_warn = dptw.settings("toast_show_warn");
|
|
toast_show_error = dptw.settings("toast_show_error");
|
|
|
|
thread = dptw.kanvas.threads_add(thread_method);
|
|
|
|
};
|
|
|
|
const thread_method = () => {
|
|
|
|
const date = Date.now(),
|
|
for_delete = [];
|
|
|
|
item_self.querySelectorAll(".toast-item").forEach(item => {
|
|
if(date - Number(item.getAttribute("data-date")) > toast_timer)
|
|
for_delete.push(item);
|
|
});
|
|
|
|
for_delete.forEach(item => item.remove());
|
|
|
|
};
|
|
|
|
this.add_toast = (type, message, variables) => {
|
|
|
|
const text = dptw.i18n.get(message, variables),
|
|
log_mode = {
|
|
info : "log",
|
|
warn : "warn",
|
|
erro : "error",
|
|
error : "error",
|
|
ok : "log"
|
|
}[type] || "log";
|
|
|
|
type = type.toLowerCase();
|
|
|
|
if(
|
|
(type == "ok" && toast_show_ok) ||
|
|
(type == "warn" && toast_show_warn) ||
|
|
(["error", "erro"].includes(type) && toast_show_error) ||
|
|
(type == "info" && toast_show_info) ||
|
|
(!["ok", "warn", "error", "erro", "info"].includes(type) && toast_show_default)
|
|
){
|
|
|
|
const item = item_self.appendChild(document.createElement("li"));
|
|
|
|
item.setAttribute("class", "toast-item");
|
|
item.setAttribute("data-date", Date.now());
|
|
item.setAttribute("data-type", type);
|
|
item.setAttribute("data-i18n", message);
|
|
variables && item.setAttribute("data-i18n-variables", btoa(JSON.stringify(variables)));
|
|
item.setAttribute("title", text);
|
|
|
|
item.innerHTML = text;
|
|
|
|
};
|
|
|
|
console[log_mode]("%c" + text.replace(/<([^> ]+)[^>]*>/g, (...arguments) => {
|
|
|
|
const tag = arguments[1].toLowerCase();
|
|
|
|
return (
|
|
tag == "li" ? "\n - " :
|
|
["p", "/p", "br", "br/"].includes(tag) ? "\n" :
|
|
"");
|
|
}), {
|
|
info : "color:#00F;",
|
|
ok : "color:#0F0;"
|
|
}[type] || null);
|
|
|
|
};
|
|
|
|
this.validate = (code, messages, variables, error_message, ok_message) => {
|
|
|
|
const variables_base64 = btoa(JSON.stringify(variables || (variables = {})));
|
|
|
|
variables.list = ``;
|
|
variables.code = code;
|
|
|
|
for(let i = 0; 1 << i <= code; i ++)
|
|
if((1 << i) & code){
|
|
|
|
const i18n = messages[i] || "error_message_" + i,
|
|
text = dptw.i18n.get(i18n, variables);
|
|
|
|
variables.list += `<li class="error" data-i18n="` + i18n + `" data-i18n-variables="` + variables_base64 + `" title="` + i18n + `">` + text + `</li>`;
|
|
};
|
|
|
|
if(code)
|
|
error_message && self.add_toast("error", error_message, variables);
|
|
else
|
|
ok_message && self.add_toast("ok", ok_message, variables);
|
|
|
|
return !code;
|
|
};
|
|
|
|
construct();
|
|
|
|
}; |