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

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