@oslokommune/punkt-elements
Version:
Komponentbiblioteket til Punkt, et designsystem laget av Oslo Origo
44 lines (43 loc) • 1.91 kB
JavaScript
import { r as e } from "./element-with-slot-4J2o3SeU.js";
//#region src/controllers/pkt-options-controller.ts
var t = class {
constructor(e) {
this.nodes = [], this.options = [], this.generation = 0, this.host = e, this.host.addController(this), this.nodes = [], this.options = [], this.observer = new MutationObserver((e) => this.handleMutations(e));
}
hostConnected() {
Array.from(this.host.childNodes).forEach((e) => {
(e.nodeName === "OPTION" || e.nodeName === "DATA") && this.addNode(e);
}), this.createOptions(), this.observer.observe(this.host, {
childList: !0,
subtree: !1,
attributes: !0
});
}
hostDisconnected() {
this.observer.disconnect();
}
addNode(e) {
(e.nodeName === "OPTION" || e.nodeName === "DATA") && (e.hasAttribute("hidden") && !e.hasAttribute("data-skip") && e.setAttribute("data-hidden", "true"), !e.hasAttribute("hidden") && e.hasAttribute("data-skip") && e.hasAttribute("data-hidden") && e.removeAttribute("data-hidden"), e.setAttribute("class", "pkt-hide"), e.setAttribute("data-skip", "true"), e.setAttribute("hidden", "true"), this.nodes.push(e));
}
createOptions() {
this.options = this.nodes.map((e) => ({
value: e.hasAttribute("value") ? e.getAttribute("value") ?? "" : e.textContent ?? "",
label: e.textContent || e.getAttribute("value") || "",
selected: e.hasAttribute("selected"),
disabled: e.hasAttribute("disabled"),
hidden: e.hasAttribute("data-hidden")
})), this.generation++, this.host.requestUpdate();
}
handleMutations(t) {
let n = !1;
t.forEach((t) => {
t.type === "childList" && (t.addedNodes.forEach((t) => {
e(t) && (this.addNode(t), n = !0);
}), t.removedNodes.forEach((t) => {
e(t) && (this.nodes = this.nodes.filter((e) => e !== t), n = !0);
})), t.type === "attributes" && e(t.target) && (this.addNode(t.target), n = !0);
}), n && this.createOptions();
}
};
//#endregion
export { t };