mirror of
https://github.com/Theodor-Springmann-Stiftung/kgpz_web.git
synced 2025-12-15 19:45:30 +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()
|
embed_cache.Clear()
|
||||||
return func(path string) string {
|
return func(path string) string {
|
||||||
val, err := getFileData(fs, path)
|
val, err := getFileData(fs, path)
|
||||||
|
|||||||
@@ -51,7 +51,9 @@ func (e *Engine) Funcs(app *app.KGPZ) error {
|
|||||||
e.AddFunc("Lower", strings.ToLower)
|
e.AddFunc("Lower", strings.ToLower)
|
||||||
e.AddFunc("Safe", functions.Safe)
|
e.AddFunc("Safe", functions.Safe)
|
||||||
|
|
||||||
|
// Embedding of file contents
|
||||||
e.AddFunc("EmbedSafe", functions.EmbedSafe(views.StaticFS))
|
e.AddFunc("EmbedSafe", functions.EmbedSafe(views.StaticFS))
|
||||||
|
e.AddFunc("Embed", functions.Embed(views.StaticFS))
|
||||||
|
|
||||||
// App specific
|
// App specific
|
||||||
e.AddFunc("GetAgent", app.Library.Agents.Item)
|
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";
|
const u = "[xslt-onload]", a = "xslt-template", c = "xslt-transformed", i = /* @__PURE__ */ new Map();
|
||||||
function h() {
|
function p() {
|
||||||
let t = htmx.findAll(u);
|
let t = htmx.findAll(u);
|
||||||
for (let n of t)
|
for (let e of t)
|
||||||
d(n);
|
f(e);
|
||||||
}
|
}
|
||||||
function d(t) {
|
function f(t) {
|
||||||
if (t.getAttribute(a) === "true")
|
if (t.getAttribute(c) === "true" || !t.hasAttribute(a))
|
||||||
return;
|
return;
|
||||||
let n = t.getAttribute(f), o = htmx.find("#" + n);
|
let e = "#" + t.getAttribute(a), r = i.get(e);
|
||||||
if (o) {
|
if (!r) {
|
||||||
let s = o.innerHTML ? new DOMParser().parseFromString(o.innerHTML, "application/xml") : o.contentDocument;
|
let o = htmx.find(e);
|
||||||
console.log(s);
|
if (o) {
|
||||||
let l = new XSLTProcessor();
|
let n = o.innerHTML ? new DOMParser().parseFromString(o.innerHTML, "application/xml") : o.contentDocument;
|
||||||
l.importStylesheet(s);
|
r = new XSLTProcessor(), r.importStylesheet(n), i.set(e, r);
|
||||||
let e = new DOMParser().parseFromString(t.innerHTML, "application/xml"), i = l.transformToFragment(e, document), r = new XMLSerializer().serializeToString(i);
|
} else
|
||||||
t.innerHTML = r, t.setAttribute(a, !0);
|
throw new Error("Unknown XSLT template: " + e);
|
||||||
} else if (t.hasAttribute(c)) {
|
}
|
||||||
let s = t.getAttribute(c), l = new Request(s, {
|
if (r) {
|
||||||
headers: { "Content-Type": "application/xslt+xml" },
|
let o = new DOMParser().parseFromString(t.innerHTML, "application/xml"), n = r.transformToFragment(o, document), s = new XMLSerializer().serializeToString(n);
|
||||||
cache: "default"
|
t.innerHTML = s, t.setAttribute(c, !0), htmx.process(t);
|
||||||
});
|
|
||||||
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);
|
|
||||||
});
|
|
||||||
} else
|
} else
|
||||||
throw new Error("Unknown XSLT template: " + n);
|
throw new Error("No Processor: " + e);
|
||||||
}
|
}
|
||||||
function S() {
|
function T() {
|
||||||
h(), document.querySelectorAll("template[simple]").forEach((n) => {
|
document.querySelectorAll("template[simple]").forEach((e) => {
|
||||||
let o = n.getAttribute("id"), s = n.content;
|
let r = e.getAttribute("id"), o = e.content;
|
||||||
customElements.define(
|
customElements.define(
|
||||||
o,
|
r,
|
||||||
class extends HTMLElement {
|
class extends HTMLElement {
|
||||||
constructor() {
|
constructor() {
|
||||||
super(), this.appendChild(s.cloneNode(!0)), this.slots = this.querySelectorAll("slot");
|
super(), this.appendChild(o.cloneNode(!0)), this.slots = this.querySelectorAll("slot");
|
||||||
}
|
}
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
let l = [];
|
let n = [];
|
||||||
this.slots.forEach((e) => {
|
this.slots.forEach((s) => {
|
||||||
let i = e.getAttribute("name"), r = this.querySelector(`[slot="${i}"]`);
|
let m = s.getAttribute("name"), l = this.querySelector(`[slot="${m}"]`);
|
||||||
r && (e.replaceWith(r.cloneNode(!0)), l.push(r));
|
l && (s.replaceWith(l.cloneNode(!0)), n.push(l));
|
||||||
}), l.forEach((e) => {
|
}), n.forEach((s) => {
|
||||||
e.remove();
|
s.remove();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
function h() {
|
||||||
|
p(), htmx.on("htmx:afterSettle", function(t) {
|
||||||
|
i.clear(), p();
|
||||||
|
}), T();
|
||||||
|
}
|
||||||
export {
|
export {
|
||||||
S as setup
|
h as setup
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -29,7 +29,7 @@
|
|||||||
</script>
|
</script>
|
||||||
</head>
|
</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" . }}
|
{{ block "_menu" . }}
|
||||||
<!-- Default app menu... -->
|
<!-- Default app menu... -->
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|||||||
@@ -3,7 +3,8 @@ import "./site.css";
|
|||||||
const ATTR_XSLT_ONLOAD = "[xslt-onload]";
|
const ATTR_XSLT_ONLOAD = "[xslt-onload]";
|
||||||
const ATTR_XSLT_TEMPLATE = "xslt-template";
|
const ATTR_XSLT_TEMPLATE = "xslt-template";
|
||||||
const ATTR_XSLT_STATE = "xslt-transformed";
|
const ATTR_XSLT_STATE = "xslt-transformed";
|
||||||
const ATTR_XSLT_REMOTE_TEMPLATE = "xslt-remote-template";
|
|
||||||
|
const xslt_processors = new Map();
|
||||||
|
|
||||||
function setup_xslt() {
|
function setup_xslt() {
|
||||||
let els = htmx.findAll(ATTR_XSLT_ONLOAD);
|
let els = htmx.findAll(ATTR_XSLT_ONLOAD);
|
||||||
@@ -13,50 +14,44 @@ function setup_xslt() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function transform_xslt(element) {
|
function transform_xslt(element) {
|
||||||
if (element.getAttribute(ATTR_XSLT_STATE) === "true") {
|
if (
|
||||||
|
element.getAttribute(ATTR_XSLT_STATE) === "true" ||
|
||||||
|
!element.hasAttribute(ATTR_XSLT_TEMPLATE)
|
||||||
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let templateId = element.getAttribute(ATTR_XSLT_TEMPLATE);
|
|
||||||
let template = htmx.find("#" + templateId);
|
let templateId = "#" + element.getAttribute(ATTR_XSLT_TEMPLATE);
|
||||||
if (template) {
|
let processor = xslt_processors.get(templateId);
|
||||||
let content = template.innerHTML
|
if (!processor) {
|
||||||
? new DOMParser().parseFromString(template.innerHTML, "application/xml")
|
let template = htmx.find(templateId);
|
||||||
: template.contentDocument;
|
if (template) {
|
||||||
console.log(content);
|
let content = template.innerHTML
|
||||||
let processor = new XSLTProcessor();
|
? new DOMParser().parseFromString(template.innerHTML, "application/xml")
|
||||||
processor.importStylesheet(content);
|
: 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 data = new DOMParser().parseFromString(element.innerHTML, "application/xml");
|
||||||
let frag = processor.transformToFragment(data, document);
|
let frag = processor.transformToFragment(data, document);
|
||||||
let s = new XMLSerializer().serializeToString(frag);
|
let s = new XMLSerializer().serializeToString(frag);
|
||||||
element.innerHTML = s;
|
element.innerHTML = s;
|
||||||
element.setAttribute(ATTR_XSLT_STATE, true);
|
element.setAttribute(ATTR_XSLT_STATE, true);
|
||||||
} else if (element.hasAttribute(ATTR_XSLT_REMOTE_TEMPLATE)) {
|
|
||||||
let url = element.getAttribute(ATTR_XSLT_REMOTE_TEMPLATE);
|
// INFO: This allows to insert htmx elements in the transformed content
|
||||||
let req = new Request(url, {
|
htmx.process(element);
|
||||||
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);
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
throw new Error("Unknown XSLT template: " + templateId);
|
throw new Error("No Processor: " + templateId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function setup() {
|
function setup_templates() {
|
||||||
setup_xslt();
|
|
||||||
let templates = document.querySelectorAll("template[simple]");
|
let templates = document.querySelectorAll("template[simple]");
|
||||||
templates.forEach((template) => {
|
templates.forEach((template) => {
|
||||||
let templateId = template.getAttribute("id");
|
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 };
|
export { setup };
|
||||||
|
|||||||
Reference in New Issue
Block a user