-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
139 lines (133 loc) · 4.06 KB
/
scripts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const Q = {
c: (endpoint, payload, selector, mode, handleErrors) => {
return fetch(
endpoint,
{
method: `POST`,
headers: {
"Content-Type": `application/json;charset=utf-8`
},
body: payload,
}
).then(resp => {
if (resp.status <= 299) {
const cd = resp.headers.get("Content-Disposition");
if (cd && cd.trim().startsWith("attachment")) {
return resp.blob();
} else {
return resp.text();
}
} else if (resp.status >= 500) {
throw Q.em["500"];
} else {
let msg = Q.em[`${resp.code}`];
if (!msg && resp.code >= 400) msg = Q.em.generic;
throw msg;
}
}).then(data => {
switch (true) {
case data instanceof Blob:
const url = URL.createObjectURL(data);
const a = document.createElement('a');
a.href = url;
a.download = "download";
document.body.appendChild(a);
a.click();
a.remove();
break;
default: // html
const element = document.querySelector(selector);
if (!element) {
throw `element not found for ${selector}`;
}
switch (mode) {
case "append_diff":
const tmpElem = document.createElement(element.nodeName);
tmpElem.innerHTML = data;
const newNodes = Array.from(tmpElem.childNodes);
const script = newNodes.pop();
const existingChildren = element.childNodes;
let c = existingChildren.length;
for (const node of newNodes.slice(c)) {
element.appendChild(node);
}
const newScript = document.createElement("script");
newScript.dataset.quid = script.dataset.quid;
newScript.innerHTML = script.innerHTML;
document.head.appendChild(newScript);
break;
case "replace":
element.outerHTML = data;
break;
default:
throw `mode ${mode} is not valid`;
}
}
}).catch(err => {
if (!handleErrors) throw err;
let msg;
if (typeof err === "string") {
msg = err;
} else if (err.message) {
if (err.message.startsWith("NetworkError")) {
msg = Q.em.network;
} else {
msg = err.message;
}
} else msg = Q.em.generic;
Q.e(msg, 2500);
})
},
f: (elem) => {
let form = elem.localName === "form" ? elem : elem.form;
if (!form) {
throw `element ${elem} should belong to a form`;
}
const fd = new FormData(form);
return Object.fromEntries(fd.entries());
},
d: (func, wait_ms) => {
let timer = null;
return (...args) => {
clearTimeout(timer);
return new Promise((resolve) => {
timer = setTimeout(
() => resolve(func(...args)),
wait_ms,
);
});
};
},
ps: (stateObj) => {
const base = location.origin + location.pathname;
const url = new URL(base);
for (const p in stateObj) {
url.searchParams.append(p, stateObj[p]);
};
window.history.pushState({}, document.title, url);
},
e: (msg, durationMs) => {
const containerClassName = "quince-err-container";
document.querySelectorAll(`.${containerClassName}`).forEach(e => e.remove());
const container = document.createElement("div");
const strong = document.createElement("strong");
strong.innerText = msg;
container.className = containerClassName;
strong.className = "quince-err-msg";
container.appendChild(strong);
document.body.insertAdjacentElement("afterbegin", container);
setTimeout(() => container.remove(), durationMs);
},
em: {
400: "Bad request",
401: "Unauthorised",
402: "Payment required",
403: "Forbidden",
404: "Not found",
422: "Unprocessable entity",
429: "Too many requests",
500: "Internal server error",
generic: "An error occurred",
network: "Network error",
}
};