-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.naxt.js
executable file
·75 lines (75 loc) · 1.96 KB
/
window.naxt.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
// ? naxt object
const naxt = {};
window.naxt = naxt;
// ? naxt values
naxt.fns = {};
naxt.done = false;
// ? naxt methods
naxt.update = async function (element, api) {
const xhres = await fetch(api);
const html = await xhres.text();
if (html.includes("<")) {
element.innerHTML = html;
const tc = document.createElement("div");
tc.innerHTML = html;
const ses = tc.querySelectorAll("body script");
ses.forEach((se) => {
const jsCode = se.textContent?.trim();
const ns = document.createElement("script");
ns.textContent = jsCode || "";
document.body.appendChild(ns);
ns.remove();
});
}
naxt.hydrate();
};
naxt.refresh = (el) => {
if (el) {
const link = el.getAttribute("data-naxt-refresh-load");
return naxt.update(el, link);
}
};
naxt.hydrate = async () => {
// load links
const lds = Array.from(document.querySelectorAll("[data-naxt-load]"));
await Promise.all(
lds.map((el) => {
const link = el.getAttribute("data-naxt-load");
el.setAttribute("data-naxt-refresh-load", link);
el.removeAttribute("data-naxt-load");
return naxt.update(el, link);
})
);
if (lds.length) {
await naxt.hydrate();
}
// handle click licks
const as = Array.from(document.querySelectorAll("[data-naxt-id]"));
await Promise.all(
as.map(async (ae) => {
ae.onclick = (e) => {
e.preventDefault();
return naxt
.update(
document.getElementById(ae.getAttribute("data-naxt-id")),
ae.href
)
.then(() => {
naxt.hydrate();
});
};
})
);
// call dom waiters
if (!naxt.done) {
const d = {}; //${JSON.stringify(HTML[1])}
for (const k in d) {
naxt.fns[k] = new Function("return " + d[k] + "")();
}
document.querySelectorAll("[data-naxt-activate]").forEach((el) => {
naxt.fns[el.getAttribute("data-naxt-activate")](el);
});
naxt.done = true;
}
};
naxt.hydrate();