Startseite filter

This commit is contained in:
Simon Martens
2025-02-19 21:37:41 +01:00
parent 0c5e19609f
commit aa7c5f4d6c
11 changed files with 529 additions and 218 deletions

View File

@@ -1,64 +1,154 @@
const f = "script[xslt-onload]", i = "xslt-template", m = "xslt-transformed", c = /* @__PURE__ */ new Map();
function u() {
let t = htmx.findAll(f);
for (let r of t)
p(r);
}
function p(t) {
if (t.getAttribute(m) === "true" || !t.hasAttribute(i))
return;
let r = "#" + t.getAttribute(i), o = c.get(r);
if (!o) {
let s = htmx.find(r);
if (s) {
let l = s.innerHTML ? new DOMParser().parseFromString(s.innerHTML, "application/xml") : s.contentDocument;
o = new XSLTProcessor(), o.importStylesheet(l), c.set(r, o);
} else
throw new Error("Unknown XSLT template: " + r);
}
let a = new DOMParser().parseFromString(t.innerHTML, "application/xml"), e = o.transformToFragment(a, document), n = new XMLSerializer().serializeToString(e);
t.outerHTML = n;
}
function d() {
document.querySelectorAll("template[simple]").forEach((r) => {
let o = r.getAttribute("id"), a = r.content;
customElements.define(
o,
class extends HTMLElement {
constructor() {
super(), this.appendChild(a.cloneNode(!0)), this.slots = this.querySelectorAll("slot");
}
connectedCallback() {
let e = [];
this.slots.forEach((n) => {
let s = n.getAttribute("name"), l = this.querySelector(`[slot="${s}"]`);
l && (n.replaceWith(l.cloneNode(!0)), e.push(l));
}), e.forEach((n) => {
n.remove();
});
}
}
);
});
}
function h() {
u(), htmx.on("htmx:load", function(t) {
u();
}), d();
}
function T(t) {
t || (t = window.location.href);
const r = document.querySelectorAll("nav");
if (r && r.length > 0)
for (const o of r)
o.querySelectorAll("a, [data-url]").forEach((e) => {
if (e.dataset.url && e.dataset.url !== "") {
let n = window.location.origin + e.dataset.url;
t.startsWith(n) ? e.setAttribute("aria-current", "page") : e.removeAttribute("aria-current");
} else e.href && (t.startsWith(e.href) ? e.setAttribute("aria-current", "page") : e.removeAttribute("aria-current"));
});
}
export {
T as setMenuActive,
h as setup
var p = (r) => {
throw TypeError(r);
};
var L = (r, i, t) => i.has(r) || p("Cannot " + t);
var u = (r, i, t) => (L(r, i, "read from private field"), t ? t.call(r) : i.get(r)), o = (r, i, t) => i.has(r) ? p("Cannot add the same private member more than once") : i instanceof WeakSet ? i.add(r) : i.set(r, t), h = (r, i, t, e) => (L(r, i, "write to private field"), e ? e.call(r, t) : i.set(r, t), t), f = (r, i, t) => (L(r, i, "access private method"), t);
const v = "script[xslt-onload]", g = "xslt-template", w = "xslt-transformed", A = "filter-list";
var a, c, T;
class S {
constructor() {
o(this, c);
o(this, a);
h(this, a, /* @__PURE__ */ new Map());
}
setup() {
let i = htmx.findAll(v);
for (let t of i)
f(this, c, T).call(this, t);
}
hookupHTMX() {
htmx.on("htmx:load", (i) => {
this.setup();
});
}
}
a = new WeakMap(), c = new WeakSet(), T = function(i) {
if (i.getAttribute(w) === "true" || !i.hasAttribute(g))
return;
let t = "#" + i.getAttribute(g), e = u(this, a).get(t);
if (!e) {
let d = htmx.find(t);
if (d) {
let E = d.innerHTML ? new DOMParser().parseFromString(d.innerHTML, "application/xml") : d.contentDocument;
e = new XSLTProcessor(), e.importStylesheet(E), u(this, a).set(t, e);
} else
throw new Error("Unknown XSLT template: " + t);
}
let s = new DOMParser().parseFromString(i.innerHTML, "application/xml"), b = e.transformToFragment(s, document), x = new XMLSerializer().serializeToString(b);
i.outerHTML = x;
};
var n, l, _, m;
class k extends HTMLElement {
constructor() {
super();
o(this, l);
o(this, n, !1);
this._items = [], this._url = "", this._filterstart = !1, this._placeholder = "Liste filtern...", this.render();
}
static get observedAttributes() {
return ["data-url"];
}
set items(t) {
Array.isArray(t) && (this._items = t, this.render());
}
get items() {
return this._items;
}
connectedCallback() {
this._url = this.getAttribute("data-url") || "./", this._filterstart = this.getAttribute("data-filterstart") === "true", this._placeholder = this.getAttribute("data-placeholder") || "Liste filtern...", this._filterstart && h(this, n, !0), this.addEventListener("input", this.onInput.bind(this)), this.addEventListener("keydown", this.onEnter.bind(this)), this.addEventListener("focusin", this.onGainFocus.bind(this)), this.addEventListener("focusout", this.onLoseFocus.bind(this));
}
attributeChangedCallback(t, e, s) {
t === "data-url" && e !== s && (this._url = s, this.render()), t === "data-filterstart" && e !== s && (this._filterstart = s === "true", this.render()), t === "data-placeholder" && e !== s && (this._placeholder = s, this.render());
}
onInput(t) {
t.target && t.target.tagName.toLowerCase() === "input" && (this._filter = t.target.value, this.renderList());
}
onGainFocus(t) {
t.target && t.target.tagName.toLowerCase() === "input" && (h(this, n, !1), this.renderList());
}
onLoseFocus(t) {
t.target && t.target.tagName.toLowerCase() === "input" && (t.target.value = "", this._filter = "", this._filterstart && h(this, n, !0), this.renderList());
}
onEnter(t) {
if (t.target && t.target.tagName.toLowerCase() === "input" && t.key === "Enter") {
t.preventDefault();
const e = this.querySelector("a");
e && e.click();
}
}
setHREFFunc(t) {
this.getHREF = t, this.render();
}
setLinkTextFunc(t) {
this.getLinkText = t, this.render();
}
getHREF(t) {
if (t) {
if (!t.id)
return "";
} else return "";
return t.id;
}
getLinkText(t) {
if (t) {
if (!t.name)
return "";
} else return "";
return t.name;
}
renderList() {
let t = this.querySelector("#list");
t && (t.outerHTML = this.List());
}
render() {
this.innerHTML = `
<div class="p-4 font-serif text-base">
${this.Input()}
${this.List()}
</div>
`;
}
Input() {
return `
<div class="flex w-full pb-0.5 border-b border-zinc-600">
<i class="ri-arrow-right-s-line mr-1"></i>
<div class="grow">
<input
type="text"
placeholder="${this._placeholder}"
class="w-full placeholder:italic px-2 " />
</div>
</div>
`;
}
List() {
let t = this._items;
return this._filter && (this._filterstart ? t = this._items.filter((e) => this.getLinkText(e).toLowerCase().startsWith(this._filter.toLowerCase())) : t = this._items.filter((e) => this.getLinkText(e).toLowerCase().includes(this._filter.toLowerCase()))), `
<div id="list" class="links min-h-72 max-h-60 overflow-auto border-b border-zinc-300 ${f(this, l, m).call(this)}">
${t.map(
(e, s) => `
<a
href="${this._url}${this.getHREF(e)}"
class="block px-2.5 py-0.5 hover:bg-slate-200 no-underline ${s % 2 === 0 ? "bg-stone-100" : "bg-stone-50"}"
${f(this, l, _).call(this, e)}>
${this.getLinkText(e)}
</a>
`
).join("")}
</div>
`;
}
}
n = new WeakMap(), l = new WeakSet(), _ = function(t) {
if (!t)
return "";
let e = this.getHREF(t);
return e === "" || !window.location.href.endsWith(e) ? "" : "aria-current='page'";
}, m = function() {
return u(this, n) ? "hidden" : "";
};
customElements.define(A, k);
export {
k as FilterList,
S as XSLTParseProcess
};

File diff suppressed because one or more lines are too long