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

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