1
0
mirror of https://github.com/jaandrle/deka-dom-el synced 2024-11-22 16:55:23 +01:00
deka-dom-el/dist/dde.js

248 lines
6.1 KiB
JavaScript
Raw Normal View History

//deka-dom-el library is available via global namespace `dde`
(()=> {
// src/helpers.js
function N(e) {
let t = typeof e;
return t !== "object" ? t : e === null ? "null" : Object.prototype.toString.call(e);
}
// src/signals-common.js
2023-09-06 14:54:28 +02:00
var p = {
isTextContent(e) {
return N(e) !== "[object Object]";
},
processReactiveAttribute(e, t, r, n) {
2023-09-08 20:18:58 +02:00
return r;
}
};
2023-09-08 20:18:58 +02:00
function D(e, t = !0) {
2023-09-06 14:54:28 +02:00
return t ? Object.assign(p, e) : (Object.setPrototypeOf(e, p), e);
}
2023-09-08 20:18:58 +02:00
function w(e) {
2023-09-06 14:54:28 +02:00
return p.isPrototypeOf(e) && e !== p ? e : p;
}
// src/dom.js
2023-09-08 20:18:58 +02:00
var E = "html";
2023-09-06 14:54:28 +02:00
function z(e) {
2023-09-08 20:18:58 +02:00
return E = e === "svg" ? "http://www.w3.org/2000/svg" : e, {
append(t) {
2023-09-08 20:18:58 +02:00
return E = "html", t;
}
};
}
function F(e, t, ...r) {
2023-09-08 20:18:58 +02:00
let n = w(this), i;
switch (n.isTextContent(t) && (t = { textContent: t }), !0) {
case typeof e == "function":
2023-09-08 20:18:58 +02:00
i = e(t || void 0);
break;
case e === "#text":
2023-09-08 20:18:58 +02:00
i = x(document.createTextNode(""), t);
break;
2023-09-08 20:18:58 +02:00
case e === "<>":
i = x(document.createDocumentFragment(), t);
break;
2023-09-08 20:18:58 +02:00
case E !== "html":
i = x(document.createElementNS(E, e), t);
break;
case !i:
i = x(document.createElement(e), t);
}
2023-09-08 20:18:58 +02:00
return r.forEach((d) => d(i)), i;
}
var h = new Map(JSON.parse('[["#text,textContent",true],["HTMLElement,textContent",true],["HTMLElement,className",true]]'));
2023-09-08 20:18:58 +02:00
function x(e, ...t) {
let r = w(this);
if (!t.length)
return e;
2023-09-08 20:18:58 +02:00
let n = e instanceof SVGElement, i = (n ? M : j).bind(null, e, "Attribute");
return Object.entries(Object.assign({}, ...t)).forEach(function d([u, f]) {
f = r.processReactiveAttribute(e, u, f, d);
let [l] = u;
2023-09-06 14:54:28 +02:00
if (l === "=")
2023-09-08 20:18:58 +02:00
return i(u.slice(1), f);
2023-09-06 14:54:28 +02:00
if (l === ".")
2023-09-08 20:18:58 +02:00
return C(e, u.slice(1), f);
if (typeof f == "object")
switch (u) {
case "style":
2023-09-08 20:18:58 +02:00
return m(f, j.bind(null, e.style, "Property"));
case "dataset":
2023-09-08 20:18:58 +02:00
return m(f, C.bind(null, e.dataset));
case "ariaset":
2023-09-08 20:18:58 +02:00
return m(f, (g, b) => i("aria-" + g, b));
case "classList":
2023-09-08 20:18:58 +02:00
return R(e, f);
default:
2023-09-08 20:18:58 +02:00
return Reflect.set(e, u, f);
}
2023-09-08 20:18:58 +02:00
if (/(aria|data)([A-Z])/.test(u))
return u = u.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase(), i(u, f);
switch (u) {
2023-09-08 15:57:51 +02:00
case "href":
2023-09-08 20:18:58 +02:00
return i(u, f);
case "xlink:href":
2023-09-08 20:18:58 +02:00
return i(u, f, "http://www.w3.org/1999/xlink");
case "textContent":
if (!n)
break;
2023-09-08 20:18:58 +02:00
return e.appendChild(document.createTextNode(f));
}
2023-09-08 20:18:58 +02:00
return A(e, u) ? C(e, u, f) : i(u, f);
}), e;
}
2023-09-08 20:18:58 +02:00
function R(e, t) {
return typeof t != "object" || m(
t,
(r, n) => e.classList.toggle(r, n === -1 ? void 0 : !!n)
), e;
}
function I(e) {
return Array.from(e.children).forEach((t) => t.remove()), e;
}
2023-09-08 20:18:58 +02:00
function A(e, t) {
let r = "HTMLElement," + t;
if (e instanceof HTMLElement && h.has(r))
return h.get(r);
2023-09-06 14:54:28 +02:00
let n = e.nodeName + "," + t;
if (h.has(n))
return h.get(n);
2023-09-08 20:18:58 +02:00
let [i, d, u] = y(e, t), f = !L(i.set);
return (!f || d) && h.set(u === HTMLElement.prototype ? r : n, f), f;
}
2023-09-08 20:18:58 +02:00
function y(e, t, r = 0) {
2023-09-06 14:54:28 +02:00
if (e = Object.getPrototypeOf(e), !e)
return [{}, r, e];
2023-09-06 14:54:28 +02:00
let n = Object.getOwnPropertyDescriptor(e, t);
2023-09-08 20:18:58 +02:00
return n ? [n, r, e] : y(e, t, r + 1);
2023-09-06 14:54:28 +02:00
}
function m(e, t) {
return Object.entries(e).forEach(([r, n]) => t(r, n));
2023-09-06 14:54:28 +02:00
}
function L(e) {
return typeof e > "u";
}
function j(e, t, r, n) {
return e[(L(n) ? "remove" : "set") + t](r, n);
}
2023-09-08 20:18:58 +02:00
function M(e, t, r, n, i = null) {
return e[(L(n) ? "remove" : "set") + t + "NS"](i, r, n);
}
function C(e, t, r) {
return Reflect.set(e, t, r);
}
// src/events.js
2023-09-08 20:18:58 +02:00
function T(e, t, r) {
return (n) => (n.addEventListener(e, t, r), n);
}
2023-09-08 20:18:58 +02:00
var O = _();
T.connected = function(e, t) {
return function(n) {
2023-09-08 20:18:58 +02:00
O.onConnected(n, e), t && t.signal && t.signal.addEventListener("abort", () => O.offConnected(n, e));
};
};
2023-09-08 20:18:58 +02:00
T.disconnected = function(e, t) {
return function(n) {
2023-09-08 20:18:58 +02:00
O.onDisconnected(n, e), t && t.signal && t.signal.addEventListener("abort", () => O.offDisconnected(n, e));
};
};
2023-09-08 15:57:51 +02:00
function _() {
let e = /* @__PURE__ */ new Map(), t = !1, r = new MutationObserver(function(o) {
2023-09-06 14:54:28 +02:00
for (let c of o)
if (c.type === "childList") {
if (g(c.addedNodes, !0)) {
2023-09-08 20:18:58 +02:00
u();
continue;
}
2023-09-08 20:18:58 +02:00
b(c.removedNodes, !0) && u();
}
});
return {
2023-09-06 14:54:28 +02:00
onConnected(o, c) {
2023-09-08 20:18:58 +02:00
d(), i(o).connected.push(c);
},
2023-09-06 14:54:28 +02:00
offConnected(o, c) {
if (!e.has(o))
return;
2023-09-06 14:54:28 +02:00
let s = e.get(o), a = s.connected;
a.splice(a.indexOf(c), 1), n(o, s);
},
2023-09-06 14:54:28 +02:00
onDisconnected(o, c) {
2023-09-08 20:18:58 +02:00
d(), i(o).disconnected.push(c);
},
2023-09-06 14:54:28 +02:00
offDisconnected(o, c) {
if (!e.has(o))
return;
2023-09-06 14:54:28 +02:00
let s = e.get(o), a = s.disconnected;
a.splice(a.indexOf(c), 1), n(o, s);
}
};
function n(o, c) {
2023-09-08 20:18:58 +02:00
c.connected.length || c.disconnect.length || (e.delete(o), u());
}
2023-09-08 20:18:58 +02:00
function i(o) {
2023-09-06 14:54:28 +02:00
if (e.has(o))
return e.get(o);
let c = { connected: [], disconnected: [] };
2023-09-06 14:54:28 +02:00
return e.set(o, c), c;
}
function d() {
t || (t = !0, r.observe(document.body, { childList: !0, subtree: !0 }));
}
2023-09-08 20:18:58 +02:00
function u() {
!t || e.size || (t = !1, r.disconnect());
}
2023-09-08 20:18:58 +02:00
function f() {
2023-09-06 14:54:28 +02:00
return new Promise(function(o) {
(requestIdleCallback || requestAnimationFrame)(o);
});
}
2023-09-06 14:54:28 +02:00
async function l(o) {
2023-09-08 20:18:58 +02:00
e.size > 30 && await f();
let c = [];
2023-09-06 14:54:28 +02:00
if (!(o instanceof Node))
return c;
for (let s of e.keys())
2023-09-06 14:54:28 +02:00
s === o || !(s instanceof Node) || o.contains(s) && c.push(s);
return c;
}
function g(o, c) {
2023-09-06 14:54:28 +02:00
for (let s of o) {
if (c && l(s).then(g), !e.has(s))
return !1;
let a = e.get(s);
return a.connected.forEach((v) => v(s)), a.connected.length = 0, a.disconnected.length || e.delete(s), !0;
}
}
function b(o, c) {
2023-09-06 14:54:28 +02:00
for (let s of o) {
if (c && l(s).then(b), !e.has(s))
return !1;
let a = e.get(s);
return a.disconnected.forEach((v) => v(s)), a.connected.length = 0, a.disconnected.length = 0, e.delete(s), !0;
}
}
}
// index.js
[HTMLElement, DocumentFragment].forEach((e) => {
let { append: t } = e.prototype;
e.prototype.append = function(...r) {
return t.apply(this, r), this;
};
});
globalThis.dde= {
2023-09-08 20:18:58 +02:00
assign: x,
classListDeclarative: R,
createElement: F,
el: F,
empty: I,
2023-09-06 14:54:28 +02:00
namespace: z,
2023-09-08 20:18:58 +02:00
on: T,
registerReactivity: D
};
})();