2020-12-28 19:56:11 +00:00
|
|
|
declare var window: Window;
|
|
|
|
|
|
|
|
export function createEl(html: string): HTMLElement {
|
|
|
|
let div = document.createElement('div') as HTMLDivElement;
|
|
|
|
div.innerHTML = html;
|
|
|
|
return div.firstElementChild as HTMLElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function serializeForm(id: string): Object {
|
|
|
|
const form = document.getElementById(id) as HTMLFormElement;
|
|
|
|
let formData = {};
|
|
|
|
for (let i = 0; i < form.elements.length; i++) {
|
|
|
|
const el = form.elements[i];
|
|
|
|
if ((el as HTMLInputElement).type == "submit") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let name = (el as HTMLInputElement).name;
|
|
|
|
if (!name) {
|
|
|
|
name = el.id;
|
|
|
|
}
|
|
|
|
switch ((el as HTMLInputElement).type) {
|
|
|
|
case "checkbox":
|
|
|
|
formData[name] = (el as HTMLInputElement).checked;
|
|
|
|
break;
|
|
|
|
case "text":
|
|
|
|
case "password":
|
|
|
|
case "email":
|
|
|
|
case "number":
|
|
|
|
formData[name] = (el as HTMLInputElement).value;
|
|
|
|
break;
|
|
|
|
case "select-one":
|
|
|
|
case "select":
|
|
|
|
let val: string = (el as HTMLSelectElement).value.toString();
|
|
|
|
if (!isNaN(val as any)) {
|
|
|
|
formData[name] = +val;
|
|
|
|
} else {
|
|
|
|
formData[name] = val;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return formData;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const rmAttr = (el: HTMLElement, attr: string): void => {
|
|
|
|
if (el.classList.contains(attr)) {
|
|
|
|
el.classList.remove(attr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const addAttr = (el: HTMLElement, attr: string): void => el.classList.add(attr);
|
|
|
|
|
2020-12-30 15:32:44 +00:00
|
|
|
export const _get = (url: string, data: Object, onreadystatechange: (req: XMLHttpRequest) => void): void => {
|
2020-12-28 19:56:11 +00:00
|
|
|
let req = new XMLHttpRequest();
|
|
|
|
req.open("GET", window.URLBase + url, true);
|
|
|
|
req.responseType = 'json';
|
|
|
|
req.setRequestHeader("Authorization", "Bearer " + window.token);
|
|
|
|
req.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
|
2020-12-30 18:31:38 +00:00
|
|
|
req.onreadystatechange = () => { if (req.status == 0) { window.notifications.connectionError(); } else { onreadystatechange(req); } };
|
2020-12-28 19:56:11 +00:00
|
|
|
req.send(JSON.stringify(data));
|
|
|
|
};
|
|
|
|
|
2020-12-30 18:31:38 +00:00
|
|
|
export const _post = (url: string, data: Object, onreadystatechange: (req: XMLHttpRequest) => void, response?: boolean): void => {
|
2020-12-28 19:56:11 +00:00
|
|
|
let req = new XMLHttpRequest();
|
|
|
|
req.open("POST", window.URLBase + url, true);
|
|
|
|
if (response) {
|
|
|
|
req.responseType = 'json';
|
|
|
|
}
|
|
|
|
req.setRequestHeader("Authorization", "Bearer " + window.token);
|
|
|
|
req.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
|
2020-12-30 18:31:38 +00:00
|
|
|
req.onreadystatechange = () => { if (req.status == 0) { window.notifications.connectionError(); } else { onreadystatechange(req); } };
|
2020-12-28 19:56:11 +00:00
|
|
|
req.send(JSON.stringify(data));
|
|
|
|
};
|
|
|
|
|
2020-12-30 18:31:38 +00:00
|
|
|
export function _delete(url: string, data: Object, onreadystatechange: (req: XMLHttpRequest) => void): void {
|
2020-12-28 19:56:11 +00:00
|
|
|
let req = new XMLHttpRequest();
|
|
|
|
req.open("DELETE", window.URLBase + url, true);
|
|
|
|
req.setRequestHeader("Authorization", "Bearer " + window.token);
|
|
|
|
req.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
|
2020-12-30 18:31:38 +00:00
|
|
|
req.onreadystatechange = () => { if (req.status == 0) { window.notifications.connectionError(); } else { onreadystatechange(req); } };
|
2020-12-28 19:56:11 +00:00
|
|
|
req.send(JSON.stringify(data));
|
|
|
|
}
|
|
|
|
|
2020-12-30 15:32:44 +00:00
|
|
|
export function toClipboard (str: string) {
|
|
|
|
const el = document.createElement('textarea') as HTMLTextAreaElement;
|
|
|
|
el.value = str;
|
|
|
|
el.readOnly = true;
|
|
|
|
el.style.position = "absolute";
|
|
|
|
el.style.left = "-9999px";
|
|
|
|
document.body.appendChild(el);
|
|
|
|
const selected = document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false;
|
|
|
|
el.select();
|
|
|
|
document.execCommand("copy");
|
|
|
|
document.body.removeChild(el);
|
|
|
|
if (selected) {
|
|
|
|
document.getSelection().removeAllRanges();
|
|
|
|
document.getSelection().addRange(selected);
|
|
|
|
}
|
|
|
|
}
|
2020-12-30 18:31:38 +00:00
|
|
|
|
|
|
|
export class notificationBox implements NotificationBox {
|
|
|
|
private _box: HTMLDivElement;
|
|
|
|
timeout: number
|
|
|
|
constructor(box: HTMLDivElement, timeout?: number) { this._box = box; this.timeout = timeout || 5; }
|
|
|
|
|
|
|
|
private _error = (message: string): HTMLElement => {
|
|
|
|
const noti = document.createElement('aside');
|
|
|
|
noti.classList.add("aside", "~critical", "!normal", "mt-half", "notification-error");
|
|
|
|
noti.innerHTML = `<strong>Error:</strong> ${message}`;
|
|
|
|
const closeButton = document.createElement('span') as HTMLSpanElement;
|
|
|
|
closeButton.classList.add("button", "~critical", "!low", "ml-1");
|
|
|
|
closeButton.innerHTML = `<i class="icon ri-close-line"></i>`;
|
|
|
|
closeButton.onclick = () => { this._box.removeChild(noti); };
|
|
|
|
noti.appendChild(closeButton);
|
|
|
|
return noti;
|
|
|
|
}
|
|
|
|
|
|
|
|
private _connectionError: boolean = false;
|
|
|
|
connectionError = () => {
|
|
|
|
const noti = this._error("Couldn't connect to jfa-go.");
|
|
|
|
if (this._connectionError) {
|
|
|
|
this._box.querySelector("aside.notification-error").remove();
|
|
|
|
}
|
|
|
|
this._box.appendChild(noti);
|
|
|
|
this._connectionError = true;
|
|
|
|
setTimeout(() => { this._box.removeChild(noti); this._connectionError = false; }, this.timeout*1000);
|
|
|
|
}
|
|
|
|
}
|