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

265 lines
6.8 KiB
JavaScript
Raw Normal View History

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