2023-11-28 18:32:40 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
function $(name) { return document.getElementById(name); }
|
|
|
|
|
2024-02-15 23:35:40 +01:00
|
|
|
function Tag(name, attrs, body, children, eventName, eventHandler) {
|
2023-11-28 18:32:40 +01:00
|
|
|
let elem = document.createElement(name);
|
|
|
|
if (body) {
|
2024-02-15 23:35:40 +01:00
|
|
|
// elem.innerHTML = body; // innerHTML is ugly (slow, overwrite childNodes)
|
|
|
|
let textNode = document.createTextNode(body);
|
|
|
|
elem.appendChild(textNode);
|
2023-11-28 18:32:40 +01:00
|
|
|
}
|
|
|
|
for (let k in attrs) {
|
|
|
|
elem.setAttribute(k, attrs[k]);
|
|
|
|
}
|
|
|
|
for (let i in children) {
|
|
|
|
elem.appendChild(children[i]);
|
|
|
|
}
|
2024-02-15 23:35:40 +01:00
|
|
|
|
|
|
|
if (eventName && eventHandler) {
|
|
|
|
elem.addEventListener(eventName, eventHandler);
|
|
|
|
}
|
|
|
|
|
2023-11-28 18:32:40 +01:00
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
|
|
|
|
function Text(text) {
|
|
|
|
return document.createTextNode(text);
|
|
|
|
}
|
|
|
|
|
2024-02-15 23:35:40 +01:00
|
|
|
function debounce(func, cancelFunc = ()=>{}, timeout = 500) {
|
2023-11-28 18:32:40 +01:00
|
|
|
let timer;
|
|
|
|
return (...args) => {
|
|
|
|
cancelFunc();
|
|
|
|
clearTimeout(timer);
|
|
|
|
timer = setTimeout(() => { func.apply(this, args); }, timeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendRequest(method, path, query, body, funcDone, funcError) {
|
|
|
|
let req = new XMLHttpRequest();
|
|
|
|
if (query) {
|
|
|
|
let params = new URLSearchParams(query);
|
|
|
|
path = path + "?" + params.toString();
|
|
|
|
}
|
|
|
|
req.open(method, path, true);
|
|
|
|
req.onreadystatechange = function () {
|
|
|
|
if (req.readyState == XMLHttpRequest.DONE) {
|
|
|
|
let status = req.status;
|
|
|
|
if (status === 0 || (status >= 200 && status < 400)) {
|
|
|
|
if (funcDone)
|
|
|
|
funcDone(req.responseText, status);
|
|
|
|
} else {
|
|
|
|
if (funcError)
|
|
|
|
funcError(req.responseText, status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (body) {
|
|
|
|
req.setRequestHeader("Content-Type", "application/json");
|
|
|
|
console.log("SENDING", body)
|
|
|
|
req.send(JSON.stringify(body));
|
|
|
|
} else {
|
|
|
|
req.send();
|
|
|
|
}
|
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|