@oslokommune/punkt-elements
Version:
Komponentbiblioteket til Punkt, et designsystem laget av Oslo Origo
40 lines (39 loc) • 1.88 kB
JavaScript
import { i as r } from "./pkt-slot-controller-BPGj-LC5.js";
class o {
constructor(t) {
this.nodes = [], this.options = [], this.host = t, this.host.addController(this), this.nodes = [], this.options = [], this.observer = new MutationObserver((e) => this.handleMutations(e));
}
hostConnected() {
Array.from(this.host.childNodes).forEach((t) => {
(t.nodeName === "OPTION" || t.nodeName === "DATA") && this.addNode(t);
}), this.observer.observe(this.host, { childList: !0, subtree: !1, attributes: !0 });
}
hostDisconnected() {
this.observer.disconnect();
}
addNode(t) {
(t.nodeName === "OPTION" || t.nodeName === "DATA") && (t.hasAttribute("hidden") && !t.hasAttribute("data-skip") && t.setAttribute("data-hidden", "true"), !t.hasAttribute("hidden") && t.hasAttribute("data-skip") && t.hasAttribute("data-hidden") && t.removeAttribute("data-hidden"), t.setAttribute("class", "pkt-hide"), t.setAttribute("data-skip", "true"), t.setAttribute("hidden", "true"), this.nodes.push(t));
}
createOptions() {
this.options = this.nodes.map((t) => ({
value: t.hasAttribute("value") ? t.getAttribute("value") ?? "" : t.textContent ?? "",
label: t.textContent || t.getAttribute("value") || "",
selected: t.hasAttribute("selected"),
disabled: t.hasAttribute("disabled"),
hidden: t.hasAttribute("data-hidden")
})), this.host.setAttribute("options", JSON.stringify(this.options));
}
handleMutations(t) {
let e = !1;
t.forEach((s) => {
s.type === "childList" && (s.addedNodes.forEach((i) => {
r(i) && (this.addNode(i), e = !0);
}), s.removedNodes.forEach((i) => {
r(i) && (this.nodes = this.nodes.filter((h) => h !== i), e = !0);
})), s.type === "attributes" && r(s.target) && (this.addNode(s.target), e = !0);
}), e && this.createOptions();
}
}
export {
o as P
};