mirror of
https://github.com/Theodor-Springmann-Stiftung/kgpz_web.git
synced 2025-10-28 16:45:32 +00:00
hx-boost works properly; introduced cache for xslt processor
This commit is contained in:
@@ -23,7 +23,7 @@ func EmbedSafe(fs fs.FS) func(string) template.HTML {
|
||||
}
|
||||
}
|
||||
|
||||
func EmbedUnsafe(fs fs.FS) func(string) string {
|
||||
func Embed(fs fs.FS) func(string) string {
|
||||
embed_cache.Clear()
|
||||
return func(path string) string {
|
||||
val, err := getFileData(fs, path)
|
||||
|
||||
@@ -51,7 +51,9 @@ func (e *Engine) Funcs(app *app.KGPZ) error {
|
||||
e.AddFunc("Lower", strings.ToLower)
|
||||
e.AddFunc("Safe", functions.Safe)
|
||||
|
||||
// Embedding of file contents
|
||||
e.AddFunc("EmbedSafe", functions.EmbedSafe(views.StaticFS))
|
||||
e.AddFunc("Embed", functions.Embed(views.StaticFS))
|
||||
|
||||
// App specific
|
||||
e.AddFunc("GetAgent", app.Library.Agents.Item)
|
||||
|
||||
@@ -1,56 +1,54 @@
|
||||
const u = "[xslt-onload]", f = "xslt-template", a = "xslt-transformed", c = "xslt-remote-template";
|
||||
function h() {
|
||||
const u = "[xslt-onload]", a = "xslt-template", c = "xslt-transformed", i = /* @__PURE__ */ new Map();
|
||||
function p() {
|
||||
let t = htmx.findAll(u);
|
||||
for (let n of t)
|
||||
d(n);
|
||||
for (let e of t)
|
||||
f(e);
|
||||
}
|
||||
function d(t) {
|
||||
if (t.getAttribute(a) === "true")
|
||||
function f(t) {
|
||||
if (t.getAttribute(c) === "true" || !t.hasAttribute(a))
|
||||
return;
|
||||
let n = t.getAttribute(f), o = htmx.find("#" + n);
|
||||
if (o) {
|
||||
let s = o.innerHTML ? new DOMParser().parseFromString(o.innerHTML, "application/xml") : o.contentDocument;
|
||||
console.log(s);
|
||||
let l = new XSLTProcessor();
|
||||
l.importStylesheet(s);
|
||||
let e = new DOMParser().parseFromString(t.innerHTML, "application/xml"), i = l.transformToFragment(e, document), r = new XMLSerializer().serializeToString(i);
|
||||
t.innerHTML = r, t.setAttribute(a, !0);
|
||||
} else if (t.hasAttribute(c)) {
|
||||
let s = t.getAttribute(c), l = new Request(s, {
|
||||
headers: { "Content-Type": "application/xslt+xml" },
|
||||
cache: "default"
|
||||
});
|
||||
fetch(l).then((e) => e.text()).then((e) => {
|
||||
let i = new DOMParser().parseFromString(e, "application/xslt+xml"), r = new XSLTProcessor();
|
||||
r.importStylesheet(i);
|
||||
let p = new DOMParser().parseFromString(t.innerHTML, "application/xml"), T = r.transformToFragment(p, document), m = new XMLSerializer().serializeToString(T);
|
||||
t.innerHTML = m, t.setAttribute(a, !0);
|
||||
});
|
||||
let e = "#" + t.getAttribute(a), r = i.get(e);
|
||||
if (!r) {
|
||||
let o = htmx.find(e);
|
||||
if (o) {
|
||||
let n = o.innerHTML ? new DOMParser().parseFromString(o.innerHTML, "application/xml") : o.contentDocument;
|
||||
r = new XSLTProcessor(), r.importStylesheet(n), i.set(e, r);
|
||||
} else
|
||||
throw new Error("Unknown XSLT template: " + e);
|
||||
}
|
||||
if (r) {
|
||||
let o = new DOMParser().parseFromString(t.innerHTML, "application/xml"), n = r.transformToFragment(o, document), s = new XMLSerializer().serializeToString(n);
|
||||
t.innerHTML = s, t.setAttribute(c, !0), htmx.process(t);
|
||||
} else
|
||||
throw new Error("Unknown XSLT template: " + n);
|
||||
throw new Error("No Processor: " + e);
|
||||
}
|
||||
function S() {
|
||||
h(), document.querySelectorAll("template[simple]").forEach((n) => {
|
||||
let o = n.getAttribute("id"), s = n.content;
|
||||
function T() {
|
||||
document.querySelectorAll("template[simple]").forEach((e) => {
|
||||
let r = e.getAttribute("id"), o = e.content;
|
||||
customElements.define(
|
||||
o,
|
||||
r,
|
||||
class extends HTMLElement {
|
||||
constructor() {
|
||||
super(), this.appendChild(s.cloneNode(!0)), this.slots = this.querySelectorAll("slot");
|
||||
super(), this.appendChild(o.cloneNode(!0)), this.slots = this.querySelectorAll("slot");
|
||||
}
|
||||
connectedCallback() {
|
||||
let l = [];
|
||||
this.slots.forEach((e) => {
|
||||
let i = e.getAttribute("name"), r = this.querySelector(`[slot="${i}"]`);
|
||||
r && (e.replaceWith(r.cloneNode(!0)), l.push(r));
|
||||
}), l.forEach((e) => {
|
||||
e.remove();
|
||||
let n = [];
|
||||
this.slots.forEach((s) => {
|
||||
let m = s.getAttribute("name"), l = this.querySelector(`[slot="${m}"]`);
|
||||
l && (s.replaceWith(l.cloneNode(!0)), n.push(l));
|
||||
}), n.forEach((s) => {
|
||||
s.remove();
|
||||
});
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
function h() {
|
||||
p(), htmx.on("htmx:afterSettle", function(t) {
|
||||
i.clear(), p();
|
||||
}), T();
|
||||
}
|
||||
export {
|
||||
S as setup
|
||||
h as setup
|
||||
};
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
</script>
|
||||
</head>
|
||||
|
||||
<body class="w-full h-full p-3" hx-ext="response-targets">
|
||||
<body class="w-full h-full p-3" hx-ext="response-targets" hx-boost="true">
|
||||
{{ block "_menu" . }}
|
||||
<!-- Default app menu... -->
|
||||
{{ end }}
|
||||
|
||||
@@ -3,7 +3,8 @@ import "./site.css";
|
||||
const ATTR_XSLT_ONLOAD = "[xslt-onload]";
|
||||
const ATTR_XSLT_TEMPLATE = "xslt-template";
|
||||
const ATTR_XSLT_STATE = "xslt-transformed";
|
||||
const ATTR_XSLT_REMOTE_TEMPLATE = "xslt-remote-template";
|
||||
|
||||
const xslt_processors = new Map();
|
||||
|
||||
function setup_xslt() {
|
||||
let els = htmx.findAll(ATTR_XSLT_ONLOAD);
|
||||
@@ -13,50 +14,44 @@ function setup_xslt() {
|
||||
}
|
||||
|
||||
function transform_xslt(element) {
|
||||
if (element.getAttribute(ATTR_XSLT_STATE) === "true") {
|
||||
if (
|
||||
element.getAttribute(ATTR_XSLT_STATE) === "true" ||
|
||||
!element.hasAttribute(ATTR_XSLT_TEMPLATE)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
let templateId = element.getAttribute(ATTR_XSLT_TEMPLATE);
|
||||
let template = htmx.find("#" + templateId);
|
||||
if (template) {
|
||||
let content = template.innerHTML
|
||||
? new DOMParser().parseFromString(template.innerHTML, "application/xml")
|
||||
: template.contentDocument;
|
||||
console.log(content);
|
||||
let processor = new XSLTProcessor();
|
||||
processor.importStylesheet(content);
|
||||
|
||||
let templateId = "#" + element.getAttribute(ATTR_XSLT_TEMPLATE);
|
||||
let processor = xslt_processors.get(templateId);
|
||||
if (!processor) {
|
||||
let template = htmx.find(templateId);
|
||||
if (template) {
|
||||
let content = template.innerHTML
|
||||
? new DOMParser().parseFromString(template.innerHTML, "application/xml")
|
||||
: template.contentDocument;
|
||||
processor = new XSLTProcessor();
|
||||
processor.importStylesheet(content);
|
||||
xslt_processors.set(templateId, processor);
|
||||
} else {
|
||||
throw new Error("Unknown XSLT template: " + templateId);
|
||||
}
|
||||
}
|
||||
|
||||
if (processor) {
|
||||
let data = new DOMParser().parseFromString(element.innerHTML, "application/xml");
|
||||
let frag = processor.transformToFragment(data, document);
|
||||
let s = new XMLSerializer().serializeToString(frag);
|
||||
element.innerHTML = s;
|
||||
element.setAttribute(ATTR_XSLT_STATE, true);
|
||||
} else if (element.hasAttribute(ATTR_XSLT_REMOTE_TEMPLATE)) {
|
||||
let url = element.getAttribute(ATTR_XSLT_REMOTE_TEMPLATE);
|
||||
let req = new Request(url, {
|
||||
headers: { "Content-Type": "application/xslt+xml" },
|
||||
cache: "default",
|
||||
});
|
||||
// WARNING: It is important to set the right cache cache policy server-side; else a request is
|
||||
// made every time.
|
||||
fetch(req)
|
||||
.then((response) => response.text())
|
||||
.then((text) => {
|
||||
let content = new DOMParser().parseFromString(text, "application/xslt+xml");
|
||||
let processor = new XSLTProcessor();
|
||||
processor.importStylesheet(content);
|
||||
let data = new DOMParser().parseFromString(element.innerHTML, "application/xml");
|
||||
let frag = processor.transformToFragment(data, document);
|
||||
let s = new XMLSerializer().serializeToString(frag);
|
||||
element.innerHTML = s;
|
||||
element.setAttribute(ATTR_XSLT_STATE, true);
|
||||
});
|
||||
|
||||
// INFO: This allows to insert htmx elements in the transformed content
|
||||
htmx.process(element);
|
||||
} else {
|
||||
throw new Error("Unknown XSLT template: " + templateId);
|
||||
throw new Error("No Processor: " + templateId);
|
||||
}
|
||||
}
|
||||
|
||||
function setup() {
|
||||
setup_xslt();
|
||||
function setup_templates() {
|
||||
let templates = document.querySelectorAll("template[simple]");
|
||||
templates.forEach((template) => {
|
||||
let templateId = template.getAttribute("id");
|
||||
@@ -90,4 +85,16 @@ function setup() {
|
||||
});
|
||||
}
|
||||
|
||||
// INFO: This is intended to be callled once on website load
|
||||
function setup() {
|
||||
setup_xslt();
|
||||
|
||||
htmx.on("htmx:afterSettle", function (_) {
|
||||
xslt_processors.clear();
|
||||
setup_xslt();
|
||||
});
|
||||
|
||||
setup_templates();
|
||||
}
|
||||
|
||||
export { setup };
|
||||
|
||||
Reference in New Issue
Block a user