2020-10-22 16:50:40 +00:00
|
|
|
declare var window: Window;
|
2020-10-20 22:33:32 +00:00
|
|
|
|
2021-04-06 19:53:30 +00:00
|
|
|
export function toDateString(date: Date): string {
|
2021-04-07 14:09:44 +00:00
|
|
|
const locale = window.language || (window as any).navigator.userLanguage || window.navigator.language;
|
|
|
|
const t12 = document.getElementById("lang-12h") as HTMLInputElement;
|
|
|
|
const t24 = document.getElementById("lang-24h") as HTMLInputElement;
|
|
|
|
let args1 = {};
|
2023-06-11 14:11:00 +00:00
|
|
|
let args2: Intl.DateTimeFormatOptions = {
|
2021-04-06 19:53:30 +00:00
|
|
|
hour: "2-digit",
|
|
|
|
minute: "2-digit"
|
2021-04-07 14:09:44 +00:00
|
|
|
};
|
2021-04-07 14:17:15 +00:00
|
|
|
if (t12 && t24) {
|
|
|
|
if (t12.checked) {
|
|
|
|
args1["hour12"] = true;
|
|
|
|
args2["hour12"] = true;
|
|
|
|
} else if (t24.checked) {
|
|
|
|
args1["hour12"] = false;
|
|
|
|
args2["hour12"] = false;
|
|
|
|
}
|
2021-04-07 14:09:44 +00:00
|
|
|
}
|
|
|
|
return date.toLocaleDateString(locale, args1) + " " + date.toLocaleString(locale, args2);
|
2021-04-06 19:53:30 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 18:16:23 +00:00
|
|
|
export const _get = (url: string, data: Object, onreadystatechange: (req: XMLHttpRequest) => void): void => {
|
2020-10-20 22:33:32 +00:00
|
|
|
let req = new XMLHttpRequest();
|
2021-01-26 22:57:29 +00:00
|
|
|
if (window.URLBase) { url = window.URLBase + url; }
|
|
|
|
req.open("GET", url, true);
|
2020-10-20 22:33:32 +00:00
|
|
|
req.responseType = 'json';
|
2020-11-12 21:25:52 +00:00
|
|
|
req.setRequestHeader("Authorization", "Bearer " + window.token);
|
2020-10-20 22:33:32 +00:00
|
|
|
req.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
|
2021-01-05 18:16:23 +00:00
|
|
|
req.onreadystatechange = () => {
|
|
|
|
if (req.status == 0) {
|
|
|
|
window.notifications.connectionError();
|
|
|
|
return;
|
|
|
|
} else if (req.status == 401) {
|
2021-01-12 23:15:12 +00:00
|
|
|
window.notifications.customError("401Error", window.lang.notif("error401Unauthorized"));
|
2021-01-05 18:16:23 +00:00
|
|
|
}
|
|
|
|
onreadystatechange(req);
|
|
|
|
};
|
2020-10-20 22:33:32 +00:00
|
|
|
req.send(JSON.stringify(data));
|
|
|
|
};
|
|
|
|
|
2021-01-25 18:01:18 +00:00
|
|
|
export const _post = (url: string, data: Object, onreadystatechange: (req: XMLHttpRequest) => void, response?: boolean, statusHandler?: (req: XMLHttpRequest) => void): void => {
|
2020-10-20 22:33:32 +00:00
|
|
|
let req = new XMLHttpRequest();
|
2020-11-22 16:36:43 +00:00
|
|
|
req.open("POST", window.URLBase + url, true);
|
2020-10-20 22:33:32 +00:00
|
|
|
if (response) {
|
|
|
|
req.responseType = 'json';
|
|
|
|
}
|
2020-11-12 21:25:52 +00:00
|
|
|
req.setRequestHeader("Authorization", "Bearer " + window.token);
|
2020-10-20 22:33:32 +00:00
|
|
|
req.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
|
2021-01-05 18:16:23 +00:00
|
|
|
req.onreadystatechange = () => {
|
2021-01-25 18:01:18 +00:00
|
|
|
if (statusHandler) { statusHandler(req); }
|
|
|
|
else if (req.status == 0) {
|
2021-01-05 18:16:23 +00:00
|
|
|
window.notifications.connectionError();
|
|
|
|
return;
|
|
|
|
} else if (req.status == 401) {
|
2021-01-12 23:15:12 +00:00
|
|
|
window.notifications.customError("401Error", window.lang.notif("error401Unauthorized"));
|
2021-01-05 18:16:23 +00:00
|
|
|
}
|
|
|
|
onreadystatechange(req);
|
|
|
|
};
|
2020-10-20 22:33:32 +00:00
|
|
|
req.send(JSON.stringify(data));
|
|
|
|
};
|
|
|
|
|
2021-01-05 18:16:23 +00:00
|
|
|
export function _delete(url: string, data: Object, onreadystatechange: (req: XMLHttpRequest) => void): void {
|
2020-10-20 22:33:32 +00:00
|
|
|
let req = new XMLHttpRequest();
|
2020-11-22 16:36:43 +00:00
|
|
|
req.open("DELETE", window.URLBase + url, true);
|
2020-11-12 21:25:52 +00:00
|
|
|
req.setRequestHeader("Authorization", "Bearer " + window.token);
|
2020-10-20 22:33:32 +00:00
|
|
|
req.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
|
2021-01-05 18:16:23 +00:00
|
|
|
req.onreadystatechange = () => {
|
|
|
|
if (req.status == 0) {
|
|
|
|
window.notifications.connectionError();
|
|
|
|
return;
|
|
|
|
} else if (req.status == 401) {
|
2021-01-12 23:15:12 +00:00
|
|
|
window.notifications.customError("401Error", window.lang.notif("error401Unauthorized"));
|
2021-01-05 18:16:23 +00:00
|
|
|
}
|
|
|
|
onreadystatechange(req);
|
|
|
|
};
|
2020-10-20 22:33:32 +00:00
|
|
|
req.send(JSON.stringify(data));
|
|
|
|
}
|
|
|
|
|
2021-01-05 18:16:23 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class notificationBox implements NotificationBox {
|
|
|
|
private _box: HTMLDivElement;
|
|
|
|
private _errorTypes: { [type: string]: boolean } = {};
|
|
|
|
private _positiveTypes: { [type: string]: boolean } = {};
|
|
|
|
timeout: number;
|
|
|
|
constructor(box: HTMLDivElement, timeout?: number) { this._box = box; this.timeout = timeout || 5; }
|
|
|
|
|
|
|
|
private _error = (message: string): HTMLElement => {
|
|
|
|
const noti = document.createElement('aside');
|
2021-12-31 00:26:11 +00:00
|
|
|
noti.classList.add("aside", "~critical", "@low", "mt-2", "notification-error");
|
2021-05-29 16:43:11 +00:00
|
|
|
let error = "";
|
|
|
|
if (window.lang) {
|
|
|
|
error = window.lang.strings("error") + ":"
|
|
|
|
}
|
|
|
|
noti.innerHTML = `<strong>${error}</strong> ${message}`;
|
2021-01-05 18:16:23 +00:00
|
|
|
const closeButton = document.createElement('span') as HTMLSpanElement;
|
2021-12-31 00:22:28 +00:00
|
|
|
closeButton.classList.add("button", "~critical", "@low", "ml-4");
|
2021-01-05 18:16:23 +00:00
|
|
|
closeButton.innerHTML = `<i class="icon ri-close-line"></i>`;
|
|
|
|
closeButton.onclick = () => { this._box.removeChild(noti); };
|
|
|
|
noti.appendChild(closeButton);
|
|
|
|
return noti;
|
|
|
|
}
|
|
|
|
|
|
|
|
private _positive = (bold: string, message: string): HTMLElement => {
|
|
|
|
const noti = document.createElement('aside');
|
2021-12-31 00:26:11 +00:00
|
|
|
noti.classList.add("aside", "~positive", "@low", "mt-2", "notification-positive");
|
2021-01-05 18:16:23 +00:00
|
|
|
noti.innerHTML = `<strong>${bold}</strong> ${message}`;
|
|
|
|
const closeButton = document.createElement('span') as HTMLSpanElement;
|
2021-12-31 00:22:28 +00:00
|
|
|
closeButton.classList.add("button", "~positive", "@low", "ml-4");
|
2021-01-05 18:16:23 +00:00
|
|
|
closeButton.innerHTML = `<i class="icon ri-close-line"></i>`;
|
|
|
|
closeButton.onclick = () => { this._box.removeChild(noti); };
|
|
|
|
noti.appendChild(closeButton);
|
|
|
|
return noti;
|
|
|
|
}
|
|
|
|
|
2021-01-12 23:15:12 +00:00
|
|
|
connectionError = () => { this.customError("connectionError", window.lang.notif("errorConnection")); }
|
2021-01-05 18:16:23 +00:00
|
|
|
|
|
|
|
customError = (type: string, message: string) => {
|
|
|
|
this._errorTypes[type] = this._errorTypes[type] || false;
|
|
|
|
const noti = this._error(message);
|
|
|
|
noti.classList.add("error-" + type);
|
|
|
|
const previousNoti: HTMLElement | undefined = this._box.querySelector("aside.error-" + type);
|
|
|
|
if (this._errorTypes[type] && previousNoti !== undefined && previousNoti != null) {
|
|
|
|
previousNoti.remove();
|
|
|
|
}
|
|
|
|
this._box.appendChild(noti);
|
|
|
|
this._errorTypes[type] = true;
|
|
|
|
setTimeout(() => { if (this._box.contains(noti)) { this._box.removeChild(noti); this._errorTypes[type] = false; } }, this.timeout*1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
customPositive = (type: string, bold: string, message: string) => {
|
|
|
|
this._positiveTypes[type] = this._positiveTypes[type] || false;
|
|
|
|
const noti = this._positive(bold, message);
|
|
|
|
noti.classList.add("positive-" + type);
|
|
|
|
const previousNoti: HTMLElement | undefined = this._box.querySelector("aside.positive-" + type);
|
|
|
|
if (this._positiveTypes[type] && previousNoti !== undefined && previousNoti != null) {
|
|
|
|
previousNoti.remove();
|
|
|
|
}
|
|
|
|
this._box.appendChild(noti);
|
|
|
|
this._positiveTypes[type] = true;
|
|
|
|
setTimeout(() => { if (this._box.contains(noti)) { this._box.removeChild(noti); this._positiveTypes[type] = false; } }, this.timeout*1000);
|
|
|
|
}
|
2021-01-12 23:15:12 +00:00
|
|
|
|
|
|
|
customSuccess = (type: string, message: string) => this.customPositive(type, window.lang.strings("success") + ":", message)
|
2021-01-05 18:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const whichAnimationEvent = () => {
|
|
|
|
const el = document.createElement("fakeElement");
|
|
|
|
if (el.style["animation"] !== void 0) {
|
|
|
|
return "animationend";
|
|
|
|
}
|
|
|
|
return "webkitAnimationEnd";
|
|
|
|
}
|
|
|
|
|
|
|
|
export function toggleLoader(el: HTMLElement, small: boolean = true) {
|
|
|
|
if (el.classList.contains("loader")) {
|
|
|
|
el.classList.remove("loader");
|
|
|
|
el.classList.remove("loader-sm");
|
|
|
|
const dot = el.querySelector("span.dot");
|
|
|
|
if (dot) { dot.remove(); }
|
|
|
|
} else {
|
|
|
|
el.classList.add("loader");
|
|
|
|
if (small) { el.classList.add("loader-sm"); }
|
|
|
|
const dot = document.createElement("span") as HTMLSpanElement;
|
|
|
|
dot.classList.add("dot")
|
|
|
|
el.appendChild(dot);
|
|
|
|
}
|
|
|
|
}
|
2021-05-07 17:20:35 +00:00
|
|
|
|
|
|
|
export function addLoader(el: HTMLElement, small: boolean = true) {
|
|
|
|
if (!el.classList.contains("loader")) {
|
|
|
|
el.classList.add("loader");
|
|
|
|
if (small) { el.classList.add("loader-sm"); }
|
|
|
|
const dot = document.createElement("span") as HTMLSpanElement;
|
|
|
|
dot.classList.add("dot")
|
|
|
|
el.appendChild(dot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function removeLoader(el: HTMLElement, small: boolean = true) {
|
|
|
|
if (el.classList.contains("loader")) {
|
|
|
|
el.classList.remove("loader");
|
|
|
|
el.classList.remove("loader-sm");
|
|
|
|
const dot = el.querySelector("span.dot");
|
|
|
|
if (dot) { dot.remove(); }
|
|
|
|
}
|
|
|
|
}
|
2021-07-16 18:38:28 +00:00
|
|
|
|
|
|
|
export function insertText(textarea: HTMLTextAreaElement, text: string) {
|
|
|
|
// https://kubyshkin.name/posts/insert-text-into-textarea-at-cursor-position <3
|
|
|
|
const isSuccess = document.execCommand("insertText", false, text);
|
|
|
|
|
|
|
|
// Firefox (non-standard method)
|
|
|
|
if (!isSuccess && typeof textarea.setRangeText === "function") {
|
|
|
|
const start = textarea.selectionStart;
|
|
|
|
textarea.setRangeText(text);
|
|
|
|
// update cursor to be at the end of insertion
|
|
|
|
textarea.selectionStart = textarea.selectionEnd = start + text.length;
|
|
|
|
|
|
|
|
// Notify any possible listeners of the change
|
|
|
|
const e = document.createEvent("UIEvent");
|
|
|
|
e.initEvent("input", true, false);
|
|
|
|
textarea.dispatchEvent(e);
|
|
|
|
textarea.focus();
|
|
|
|
}
|
|
|
|
}
|
2023-06-18 20:38:12 +00:00
|
|
|
|
|
|
|
export interface DiscordInvite {
|
|
|
|
invite: string;
|
|
|
|
icon: string;
|
|
|
|
}
|