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-with-signals.js

461 lines
12 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 w = {
isSignal(e) {
return !1;
2023-09-13 13:20:00 +02:00
},
processReactiveAttribute(e, t, n, r) {
return n;
2023-09-13 13:20:00 +02:00
}
};
function N(e, t = !0) {
return t ? Object.assign(w, e) : (Object.setPrototypeOf(e, w), e);
2023-09-13 13:20:00 +02:00
}
function x(e) {
return w.isPrototypeOf(e) && e !== w ? e : w;
2023-09-13 13:20:00 +02:00
}
// src/helpers.js
function v(e) {
2023-09-13 13:20:00 +02:00
return typeof e > "u";
}
function j(e) {
let t = typeof e;
return t !== "object" ? t : e === null ? "null" : Object.prototype.toString.call(e);
}
function A(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 T = { setDeleteAttr: k };
function k(e, t, n) {
if (Reflect.set(e, t, n), !!v(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 y = [{ scope: document.body, namespace: "html", host: (e) => e ? e(document.body) : document.body }], W = (e) => e === "svg" ? "http://www.w3.org/2000/svg" : e, E = {
get current() {
return y[y.length - 1];
},
get state() {
return [...y];
},
get host() {
return this.current.host;
},
get namespace() {
return this.current.namespace;
},
set namespace(e) {
return this.current.namespace = W(e);
},
elNamespace(e) {
let t = this.namespace;
return this.namespace = e, {
append(...n) {
return E.namespace = t, n.length === 1 ? n[0] : document.createDocumentFragment().append(...n);
}
};
},
push(e = {}) {
return e.namespace && (e.namespace = W(e.namespace)), y.push(Object.assign({}, this.current, e));
},
pop() {
return y.pop();
}
};
function ie(e, t, ...n) {
2023-10-13 17:13:03 +02:00
let r = this, o = x(this), { namespace: c } = E, d = !1, s;
switch ((Object(t) !== t || o.isSignal(t)) && (t = { textContent: t }), !0) {
case typeof e == "function": {
2023-10-13 17:13:03 +02:00
d = !0, E.push({ scope: e, host: (f) => f ? (n.unshift(f), void 0) : s }), s = e(t || void 0);
break;
}
case e === "#text":
2023-10-13 17:13:03 +02:00
s = O.call(r, document.createTextNode(""), t);
2023-09-08 20:18:58 +02:00
break;
case e === "<>":
2023-10-13 17:13:03 +02:00
s = O.call(r, document.createDocumentFragment(), t);
break;
case c !== "html":
2023-10-13 17:13:03 +02:00
s = O.call(r, document.createElementNS(c, e), t);
break;
2023-10-13 17:13:03 +02:00
case !s:
s = O.call(r, document.createElement(e), t);
}
2023-10-13 17:13:03 +02:00
return n.forEach((f) => f(s)), d && E.pop(), s;
}
var { setDeleteAttr: M } = T;
function O(e, ...t) {
let n = this, r = x(this);
if (!t.length)
return e;
let c = (e instanceof SVGElement ? J : I).bind(null, e, "Attribute");
2023-10-13 17:13:03 +02:00
return Object.entries(Object.assign({}, ...t)).forEach(function d([s, f]) {
f = r.processReactiveAttribute(e, s, f, d);
let [m] = s;
if (m === "=")
2023-10-13 17:13:03 +02:00
return c(s.slice(1), f);
if (m === ".")
2023-10-13 17:13:03 +02:00
return F(e, s.slice(1), f);
if (/(aria|data)([A-Z])/.test(s))
2023-10-13 17:13:03 +02:00
return s = s.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase(), c(s, f);
switch (s === "className" && (s = "class"), s) {
case "xlink:href":
2023-10-13 17:13:03 +02:00
return c(s, f, "http://www.w3.org/1999/xlink");
case "textContent":
2023-10-13 17:13:03 +02:00
return M(e, s, f);
case "style":
2023-10-13 17:13:03 +02:00
if (typeof f != "object")
2023-09-21 16:27:00 +02:00
break;
case "dataset":
2023-10-13 17:13:03 +02:00
return D(r, f, F.bind(null, e[s]));
case "ariaset":
2023-10-13 17:13:03 +02:00
return D(r, f, (b, i) => c("aria-" + b, i));
case "classList":
2023-10-13 17:13:03 +02:00
return B.call(n, e, f);
}
2023-10-13 17:13:03 +02:00
return G(e, s) ? M(e, s, f) : c(s, f);
}), e;
}
function B(e, t) {
let n = x(this);
return D(
n,
t,
(r, o) => e.classList.toggle(r, o === -1 ? void 0 : !!o)
), e;
}
function fe(e) {
return Array.from(e.children).forEach((t) => t.remove()), e;
}
2023-09-21 16:27:00 +02:00
function G(e, t) {
if (!Reflect.has(e, t))
return !1;
let n = z(e, t);
return !v(n.set);
2023-09-06 14:54:28 +02:00
}
function z(e, t) {
2023-09-06 14:54:28 +02:00
if (e = Object.getPrototypeOf(e), !e)
return {};
2023-09-21 16:27:00 +02:00
let n = Object.getOwnPropertyDescriptor(e, t);
return n || z(e, t);
2023-09-06 14:54:28 +02:00
}
function D(e, t, n) {
2023-09-21 16:27:00 +02:00
if (!(typeof t != "object" || t === null))
return Object.entries(t).forEach(function([o, c]) {
2023-10-13 17:13:03 +02:00
o && (c = e.processReactiveAttribute(t, o, c, (d) => n(...d)), n(o, c));
});
}
function U(e) {
return Array.isArray(e) ? e.filter(Boolean).join(" ") : e;
}
function I(e, t, n, r) {
return e[(v(r) ? "remove" : "set") + t](n, U(r));
}
function J(e, t, n, r, o = null) {
return e[(v(r) ? "remove" : "set") + t + "NS"](o, n, U(r));
}
function F(e, t, n) {
if (Reflect.set(e, t, n), !!v(n))
return Reflect.deleteProperty(e, t);
}
// src/events.js
function le(e, t, ...n) {
let r = n.length ? new CustomEvent(t, { detail: n[0] }) : new Event(t);
return e.dispatchEvent(r);
}
function S(e, t, n) {
return function(o) {
return o.addEventListener(e, t, n), o;
};
}
var R = Z(), V = /* @__PURE__ */ new WeakSet();
S.connected = function(e, t) {
let n = "connected";
return typeof t != "object" && (t = {}), t.once = !0, function(o) {
let c = "dde:" + n;
return o.addEventListener(c, e, t), o.__dde_lifecycleToEvents ? o : o.isConnected ? (o.dispatchEvent(new Event(c)), o) : (A(t.signal, () => R.offConnected(o, e)) && R.onConnected(o, e), o);
};
};
S.disconnected = function(e, t) {
let n = "disconnected";
return typeof t != "object" && (t = {}), t.once = !0, function(o) {
let c = "dde:" + n;
return o.addEventListener(c, e, t), o.__dde_lifecycleToEvents || A(t.signal, () => R.offDisconnected(o, e)) && R.onDisconnected(o, e), o;
};
};
S.attributeChanged = function(e, t) {
let n = "attributeChanged";
return typeof t != "object" && (t = {}), function(o) {
let c = "dde:" + n;
if (o.addEventListener(c, e, t), o.__dde_lifecycleToEvents || V.has(o))
return o;
2023-10-13 17:13:03 +02:00
let d = new MutationObserver(function(f) {
for (let { attributeName: m, target: b } of f)
b.dispatchEvent(
new CustomEvent(c, { detail: [m, b.getAttribute(m)] })
);
});
2023-10-13 17:13:03 +02:00
return A(t.signal, () => d.disconnect()) && d.observe(o, { attributes: !0 }), o;
};
};
function Z() {
let e = /* @__PURE__ */ new Map(), t = !1, n = new MutationObserver(function(i) {
for (let u of i)
if (u.type === "childList") {
if (m(u.addedNodes, !0)) {
2023-10-13 17:13:03 +02:00
d();
continue;
}
2023-10-13 17:13:03 +02:00
b(u.removedNodes, !0) && d();
}
});
return {
onConnected(i, u) {
c();
2023-10-13 17:13:03 +02:00
let a = o(i);
a.connected.has(u) || (a.connected.add(u), a.length_c += 1);
},
offConnected(i, u) {
if (!e.has(i))
return;
2023-10-13 17:13:03 +02:00
let a = e.get(i);
a.connected.has(u) && (a.connected.delete(u), a.length_c -= 1, r(i, a));
},
onDisconnected(i, u) {
c();
2023-10-13 17:13:03 +02:00
let a = o(i);
a.disconnected.has(u) || (a.disconnected.add(u), a.length_d += 1);
},
offDisconnected(i, u) {
if (!e.has(i))
return;
2023-10-13 17:13:03 +02:00
let a = e.get(i);
a.disconnected.has(u) && (a.disconnected.delete(u), a.length_d -= 1, r(i, a));
}
};
function r(i, u) {
2023-10-13 17:13:03 +02:00
u.length_c || u.length_d || (e.delete(i), d());
}
function o(i) {
if (e.has(i))
return e.get(i);
let u = {
connected: /* @__PURE__ */ new WeakSet(),
length_c: 0,
disconnected: /* @__PURE__ */ new WeakSet(),
length_d: 0
};
return e.set(i, u), u;
}
function c() {
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-09-11 18:32:05 +02:00
}
function s() {
return new Promise(function(i) {
(requestIdleCallback || requestAnimationFrame)(i);
});
}
2023-10-13 17:13:03 +02:00
async function f(i) {
e.size > 30 && await s();
let u = [];
if (!(i instanceof Node))
return u;
2023-10-13 17:13:03 +02:00
for (let a of e.keys())
a === i || !(a instanceof Node) || i.contains(a) && u.push(a);
return u;
}
function m(i, u) {
2023-10-13 17:13:03 +02:00
let a = !1;
for (let p of i) {
2023-10-13 17:13:03 +02:00
if (u && f(p).then(m), !e.has(p))
continue;
let _ = e.get(p);
2023-10-13 17:13:03 +02:00
_.length_c && (p.dispatchEvent(new Event("dde:connected")), _.connected = /* @__PURE__ */ new WeakSet(), _.length_c = 0, _.length_d || e.delete(p), a = !0);
}
2023-10-13 17:13:03 +02:00
return a;
}
function b(i, u) {
2023-10-13 17:13:03 +02:00
let a = !1;
for (let p of i)
2023-10-13 17:13:03 +02:00
u && f(p).then(b), !(!e.has(p) || !e.get(p).length_d) && (p.dispatchEvent(new Event("dde:disconnected")), e.delete(p), a = !0);
return a;
}
}
// 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;
};
});
// src/signals-lib.js
var l = Symbol.for("Signal");
function C(e) {
try {
return Reflect.has(e, l);
} catch {
return !1;
}
}
var g = /* @__PURE__ */ new WeakMap();
function h(e, t) {
if (typeof e != "function")
return $(e, t);
if (C(e))
return e;
let n = $(""), r = () => n(e());
return g.set(r, /* @__PURE__ */ new Set([n])), X(r), n;
}
h.action = function(e, t, ...n) {
let r = e[l], { actions: o } = r;
if (!o || !Reflect.has(o, t))
throw new Error(`'${e}' has no action with name '${t}'!`);
if (o[t].apply(r, n), r.skip)
return Reflect.deleteProperty(r, "skip");
r.listeners.forEach((c) => c(r.value));
};
h.on = function e(t, n, r = {}) {
let { signal: o } = r;
2023-09-07 17:35:23 +02:00
if (!(o && o.aborted)) {
if (Array.isArray(t))
return t.forEach((c) => e(c, n, r));
P(t, n), o && o.addEventListener("abort", () => H(t, n));
2023-09-07 17:35:23 +02:00
}
};
h.symbols = {
signal: l,
2023-09-07 17:35:23 +02:00
onclear: Symbol.for("Signal.onclear")
};
h.attribute = function(e, t = void 0) {
let { host: n } = E, r = n() && n().hasAttribute(e) ? n().getAttribute(e) : t, o = new AbortController(), c = h(r, {
[h.symbols.onclear]() {
o.abort();
}
});
2023-10-13 17:13:03 +02:00
return E.host(S.attributeChanged(function({ detail: d }) {
let [s, f] = d;
s === e && c(f);
}, { signal: o.signal })), c;
};
h.clear = function(...e) {
for (let n of e) {
Reflect.deleteProperty(n, "toJSON");
let r = n[l], { onclear: o } = h.symbols;
r.actions && r.actions[o] && r.actions[o].call(r), t(n, r), Reflect.deleteProperty(n, l);
}
function t(n, r) {
r.listeners.forEach((o) => {
if (r.listeners.delete(o), !g.has(o))
return;
let c = g.get(o);
c.delete(n), !(c.size > 1) && (h.clear(...c), g.delete(o));
});
2023-09-07 17:35:23 +02:00
}
};
h.el = function(e, t) {
let n = document.createComment("<#reactive>"), r = document.createComment("</#reactive>"), o = document.createDocumentFragment();
o.append(n, r);
2023-10-13 17:13:03 +02:00
let c = (d) => {
if (!n.parentNode || !r.parentNode)
return H(e, c);
2023-10-13 17:13:03 +02:00
let s = t(d);
Array.isArray(s) || (s = [s]);
2023-10-13 17:13:03 +02:00
let f = n;
for (; (f = n.nextSibling) !== r; )
f.remove();
n.after(...s);
2023-09-08 20:18:58 +02:00
};
return P(e, c), c(e()), o;
2023-09-08 20:18:58 +02:00
};
var q = {
isSignal: C,
processReactiveAttribute(e, t, n, r) {
return C(n) ? (P(n, (o) => r([t, o])), n()) : n;
}
};
function $(e, t) {
let n = (...r) => r.length ? te(n, ...r) : ee(n);
return Q(n, e, t);
}
var K = Object.assign(/* @__PURE__ */ Object.create(null), {
stopPropagation() {
this.skip = !0;
}
});
function Q(e, t, n) {
return j(n) !== "[object Object]" && (n = {}), e[l] = {
value: t,
actions: n,
listeners: /* @__PURE__ */ new Set()
}, e.toJSON = () => e(), Object.setPrototypeOf(e[l], K), e;
}
var L = [];
function X(e) {
let t = function() {
L.push(t), e(), L.pop();
};
g.has(e) && (g.set(t, g.get(e)), g.delete(e)), t();
}
function Y() {
return L[L.length - 1];
}
function ee(e) {
if (!e[l])
return;
let { value: t, listeners: n } = e[l], r = Y();
return r && n.add(r), g.has(r) && g.get(r).add(e), t;
}
function te(e, t, n) {
if (!e[l])
return;
let r = e[l];
if (!(!n && r.value === t))
return r.value = t, r.listeners.forEach((o) => o(t)), t;
}
function P(e, t) {
if (e[l])
return e[l].listeners.add(t);
}
function H(e, t) {
if (e[l])
return e[l].listeners.delete(t);
}
// signals.js
N(q);
globalThis.dde= {
S: h,
assign: O,
classListDeclarative: B,
createElement: ie,
dispatchEvent: le,
el: ie,
empty: fe,
isSignal: C,
on: S,
registerReactivity: N,
scope: E
};
})();