@oslokommune/punkt-elements
Version:
Komponentbiblioteket til Punkt, et designsystem laget av Oslo Origo
167 lines (166 loc) • 5.85 kB
JavaScript
import { t as e, u as t } from "./element-cZ85T_aa.js";
import { r as n, t as r } from "./directive-3THFsVew.js";
import { t as i } from "./async-directive-CaQiqjyV.js";
//#region src/controllers/pkt-slot-utils.ts
function a(e) {
return e.classList && (e.classList.contains("_dialog_overlay") || e.classList.contains("backdrop")) ? !0 : typeof e.hasAttribute == "function" && e.hasAttribute("data-skip");
}
function o(e) {
return e.nodeName === "OPTION" || e.nodeName === "DATA";
}
function s(e) {
return e.nodeType === Node.TEXT_NODE && e.nodeValue?.trim() !== "";
}
//#endregion
//#region src/directives/slot-content.ts
var c = /* @__PURE__ */ new WeakMap();
function l(e) {
let t = c.get(e);
return t || (t = new m(e), c.set(e, t)), t;
}
var u = Symbol("default");
function d(e) {
return e === void 0 ? u : e;
}
function f(e, t) {
if (e.nodeType === Node.COMMENT_NODE) return !1;
if (e.nodeType === Node.TEXT_NODE) return s(e) ? (p(t, u, e), !0) : !1;
if (e.nodeType === Node.ELEMENT_NODE) {
let n = e;
if (a(n) || o(n)) return !1;
if (n instanceof HTMLTemplateElement) {
let e = Array.from(n.content.childNodes), r = d(n.getAttribute("slot") ?? void 0);
for (let n of e) p(t, r, n);
return !0;
}
let r = n.getAttribute("slot");
return p(t, r === null ? u : d(r), e), !0;
}
return !1;
}
function p(e, t, n) {
let r = e.get(t);
r || (r = [], e.set(t, r)), r.push(n);
}
var m = class e {
static {
this.PLACEHOLDER_PREFIX = "pkt-slot";
}
constructor(e) {
this.observer = null, this.nodesBySlot = /* @__PURE__ */ new Map(), this.directives = /* @__PURE__ */ new Set(), this.observing = !1, this.trackedNodes = /* @__PURE__ */ new Set(), this.generation = 0, this.initialCollectionDone = !1, this.processingMutations = !1, this.placeholderToNode = /* @__PURE__ */ new Map(), this.nodeToPlaceholder = /* @__PURE__ */ new Map(), this.host = e;
}
collectNodes() {
if (this.initialCollectionDone) return;
this.initialCollectionDone = !0, this.generation++;
let e = Array.from(this.host.childNodes);
for (let t of e) f(t, this.nodesBySlot) && this.trackedNodes.add(t);
}
addDirective(e) {
this.directives.add(e), this.startObserving();
}
removeDirective(e) {
this.directives.delete(e), this.directives.size === 0 && this.stopObserving();
}
getNodes(e) {
return this.nodesBySlot.get(d(e)) ?? [];
}
hasContent(e) {
return this.getNodes(e).length > 0;
}
ensurePlaceholder(t) {
if (this.nodeToPlaceholder.has(t) || t.parentNode !== this.host) return;
let n = document.createComment(e.PLACEHOLDER_PREFIX);
this.host.insertBefore(n, t), this.placeholderToNode.set(n, t), this.nodeToPlaceholder.set(t, n);
}
startObserving() {
this.observing || (this.observing = !0, setTimeout(() => {
this.observing && (this.observer = new MutationObserver((e) => {
this.handleMutations(e);
}), this.observer.observe(this.host, { childList: !0 }));
}, 0));
}
stopObserving() {
this.observing && (this.observing = !1, this.observer?.disconnect(), this.observer = null);
}
handleMutations(e) {
if (this.processingMutations) return;
this.processingMutations = !0;
let t = !1;
for (let n of e) if (n.target === this.host) {
for (let e of n.removedNodes) {
let n = this.placeholderToNode.get(e);
if (n) {
n.parentNode?.removeChild(n), this.trackedNodes.delete(n), this.removeFromSlots(n), this.placeholderToNode.delete(e), this.nodeToPlaceholder.delete(n), t = !0;
continue;
}
if (this.trackedNodes.has(e)) {
this.trackedNodes.delete(e), this.removeFromSlots(e);
let n = this.nodeToPlaceholder.get(e);
n && (n.parentNode?.removeChild(n), this.placeholderToNode.delete(n), this.nodeToPlaceholder.delete(e)), t = !0;
}
}
for (let e of n.addedNodes) this.placeholderToNode.has(e) || !this.trackedNodes.has(e) && this.isUserContent(e) && f(e, this.nodesBySlot) && (this.trackedNodes.add(e), t = !0);
}
t && (this.generation++, this.notifyDirectives()), this.observer?.takeRecords(), this.processingMutations = !1;
}
isUserContent(e) {
if (e.nodeType === Node.COMMENT_NODE) return !1;
if (e.nodeType === Node.TEXT_NODE) return s(e);
if (e.nodeType === Node.ELEMENT_NODE) {
let t = e;
return !(a(t) || o(t));
}
return !1;
}
removeFromSlots(e) {
for (let [t, n] of this.nodesBySlot) {
let r = n.indexOf(e);
if (r !== -1) {
n.splice(r, 1), n.length === 0 && this.nodesBySlot.delete(t);
return;
}
}
}
notifyDirectives() {
for (let e of this.directives) e.handleNodesChanged();
}
}, h = r(class extends i {
constructor(e) {
if (super(e), this.manager = null, this.lastGeneration = -1, e.type !== n.CHILD) throw Error("slotContent() can only be used in child expressions");
}
render(e, n) {
return t;
}
update(e, [t, n]) {
return this.slotName = n, this.manager || (this.manager = l(t), this.manager.collectNodes(), this.manager.addDirective(this)), this.ensurePlaceholders(), this.getNodesOrNoChange();
}
handleNodesChanged() {
if (!this.isConnected || !this.manager) return;
this.ensurePlaceholders();
let e = this.getNodesOrNoChange();
e !== t && this.setValue(e);
}
ensurePlaceholders() {
if (!this.manager) return;
let e = this.manager.getNodes(this.slotName);
for (let t of e) this.manager.ensurePlaceholder(t);
}
disconnected() {
this.manager?.removeDirective(this), this.manager = null, this.lastGeneration = -1;
}
reconnected() {}
getNodesOrNoChange() {
if (!this.manager) return t;
let e = this.manager.generation;
return e === this.lastGeneration ? t : (this.lastGeneration = e, this.manager.getNodes(this.slotName));
}
}), g = class extends e {
connectedCallback() {
l(this).collectNodes(), super.connectedCallback();
}
hasSlotContent(e) {
return l(this).hasContent(e);
}
};
//#endregion
export { h as n, o as r, g as t };