-
Notifications
You must be signed in to change notification settings - Fork 0
/
fasthtml.js
52 lines (44 loc) · 1.79 KB
/
fasthtml.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
function _$ (sel, root = document) {return root.querySelector(sel)}
function _$$(sel, root = document) {return root.querySelectorAll(sel)}
function $H(ss, ...values) {
const r = document.createElement('div');
r.innerHTML = ss.reduce((r,s,i) => r+s + (values[i] || ''), '').trim();
return r.firstElementChild;
}
function $E(t, attrs = {}, children = []) {
if (typeof t === 'string') {
const { style, dataset, className, cls, ...rest } = attrs;
const e = document.createElement(t);
if (style && typeof style === 'object') Object.assign(e.style, style);
if (className || cls) e.className = className || cls;
if (dataset) Object.assign(e.dataset, dataset);
Object.entries(rest).forEach(([k, v]) => {
if (k.startsWith('on') && typeof v === 'function') e.addEventListener(k.slice(2), v);
else e.setAttribute(k, v);
});
const processChild = (child) => {
if (Array.isArray(child)) return $E(...child);
else if (typeof child === 'string' || child instanceof Node) return child;
return '';
};
e.append(...(Array.isArray(children) ? children.map(processChild) : [processChild(children)]));
return e;
} else return t;
}
function proc_htmx(sel, func) {
htmx.onLoad(elt => {
const elements = Array.from(htmx.findAll(elt, sel));
if (elt.matches(sel)) elements.unshift(elt)
elements.forEach(func);
});
}
function domReadyExecute(cb) {
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", cb)
else cb();
}
async function hx_multipart(path, context = {}, verb = 'POST') {
const form = $H`<form style="display:none;" hx-encoding="multipart/form-data"></form>`;
document.body.appendChild(form);
try { await htmx.ajax(verb, path, { ...context, source: form }) }
finally { form.remove() }
}