aboutsummaryrefslogtreecommitdiff
path: root/frontend/index.ts
blob: cd3c45584b7b2244d1bbbc19bc29cb2fdb8f8275 (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
import htmx from "htmx.org";

function setupAnchors() {
    document.querySelectorAll("a").forEach(e => {
        if (!e.href.startsWith(window.location.origin)) {
            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:top")
        htmx.process(e)
    });
}

// updating history and window title
document.addEventListener("htmx:afterSettle", e => {
    const title = e.detail.xhr.getResponseHeader("Updated-Title")
    if (title?.length != 0) document.title = title
    window.history.pushState({}, "", e.detail.pathInfo.finalRequestPath)
    setupAnchors()
})

setupAnchors()