aboutsummaryrefslogtreecommitdiff
path: root/frontend/index.ts
blob: 9babe4aca1233af4b6921a73a87daf725538df5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import htmx from "htmx.org";

htmx.config.historyRestoreAsHxRequest = false;

function setupAnchors() {
    document.querySelectorAll("a").forEach(e => {
        if (!e.href.startsWith(window.location.origin) && /https?:\/\//.test(e.href)) {
            e.target = "_blank";
            return
        }
        if (e.hasAttribute("hx-trigger")) return;
        e.setAttribute("hx-get", e.href)
        e.setAttribute("hx-trigger", "click")
        e.setAttribute("hx-target", "#content")
        e.setAttribute("hx-swap", "outerHTML show:body:top")
        htmx.process(e)
    });
}

// updating history and window title
document.addEventListener("htmx:afterSettle", e => {
    if (e.detail.xhr === undefined) return
    const title = e.detail.xhr.getResponseHeader("Updated-Title")
    if (title?.length !== 0) document.title = title
    const quote = e.detail.xhr.getResponseHeader("Updated-Quote")
    if (quote?.length !== 0)
        document.querySelector("#quote")!.innerHTML = "« " + quote + " »"
    setupAnchors()
})

document.body.addEventListener('htmx:beforeSwap', function(e) {
    if(e.detail.xhr.status !== 404) return
    e.detail.shouldSwap = true;
    e.detail.isError = false;
})

setupAnchors()