2020-10-22 16:50:40 +00:00
|
|
|
declare var window: Window;
|
2020-10-20 22:33:32 +00:00
|
|
|
|
2020-10-22 16:50:40 +00:00
|
|
|
export function serializeForm(id: string): Object {
|
2020-10-20 22:33:32 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-22 16:50:40 +00:00
|
|
|
export const rmAttr = (el: HTMLElement, attr: string): void => {
|
2020-10-20 22:33:32 +00:00
|
|
|
if (el.classList.contains(attr)) {
|
|
|
|
el.classList.remove(attr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-22 16:50:40 +00:00
|
|
|
export const addAttr = (el: HTMLElement, attr: string): void => el.classList.add(attr);
|
2020-10-20 22:33:32 +00:00
|
|
|
|
2020-10-22 16:50:40 +00:00
|
|
|
export const _get = (url: string, data: Object, onreadystatechange: () => void): void => {
|
2020-10-20 22:33:32 +00:00
|
|
|
let req = new XMLHttpRequest();
|
|
|
|
req.open("GET", url, true);
|
|
|
|
req.responseType = 'json';
|
|
|
|
req.setRequestHeader("Authorization", "Basic " + btoa(window.token + ":"));
|
|
|
|
req.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
|
|
|
|
req.onreadystatechange = onreadystatechange;
|
|
|
|
req.send(JSON.stringify(data));
|
|
|
|
};
|
|
|
|
|
2020-10-22 16:50:40 +00:00
|
|
|
export const _post = (url: string, data: Object, onreadystatechange: () => void, response?: boolean): void => {
|
2020-10-20 22:33:32 +00:00
|
|
|
let req = new XMLHttpRequest();
|
|
|
|
req.open("POST", url, true);
|
|
|
|
if (response) {
|
|
|
|
req.responseType = 'json';
|
|
|
|
}
|
|
|
|
req.setRequestHeader("Authorization", "Basic " + btoa(window.token + ":"));
|
|
|
|
req.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
|
|
|
|
req.onreadystatechange = onreadystatechange;
|
|
|
|
req.send(JSON.stringify(data));
|
|
|
|
};
|
|
|
|
|
2020-10-22 16:50:40 +00:00
|
|
|
export function _delete(url: string, data: Object, onreadystatechange: () => void): void {
|
2020-10-20 22:33:32 +00:00
|
|
|
let req = new XMLHttpRequest();
|
|
|
|
req.open("DELETE", url, true);
|
|
|
|
req.setRequestHeader("Authorization", "Basic " + btoa(window.token + ":"));
|
|
|
|
req.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
|
|
|
|
req.onreadystatechange = onreadystatechange;
|
|
|
|
req.send(JSON.stringify(data));
|
|
|
|
}
|
|
|
|
|