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

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