aboutsummaryrefslogtreecommitdiff
path: root/static/js/sidebar.js
blob: 8384c6f454542235b26679b807eedebd7746972f (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
var sidebarStorageKey = "dove-sidebar-expanded";

function getSidebarState() {
    try {
        var stored = localStorage.getItem(sidebarStorageKey);
        return stored ? JSON.parse(stored) : {};
    } catch (error) {
        return {};
    }
}

function saveSidebarState(state) {
    localStorage.setItem(sidebarStorageKey, JSON.stringify(state));
}

function toggleSection(section, expanded) {
    var children = section.querySelector("[data-sidebar-children]");
    var chevron = section.querySelector("[data-sidebar-chevron]");

    if (expanded) {
        children.classList.remove("hidden");
        chevron.style.transform = "rotate(90deg)";
    } else {
        children.classList.add("hidden");
        chevron.style.transform = "";
    }
}

function initSidebar() {
    var currentPath = window.location.pathname;
    var state = getSidebarState();

    document.querySelectorAll("#sidebar-nav > a.nav-link").forEach(function (link) {
        link.classList.remove("active");
        if (link.getAttribute("href") === currentPath) {
            link.classList.add("active");
        }
    });

    document.querySelectorAll("[data-sidebar-section]").forEach(function (section) {
        var prefix = section.dataset.sectionPrefix;
        var trigger = section.querySelector("[data-sidebar-trigger]");
        var chevron = section.querySelector("[data-sidebar-chevron]");
        var pathMatchesSection = currentPath.indexOf(prefix) === 0;

        if (pathMatchesSection && state[prefix] === undefined) {
            state[prefix] = true;
        }

        toggleSection(section, !!state[prefix]);

        trigger.classList.remove("active");
        if (currentPath === prefix || currentPath === prefix + "/") {
            trigger.classList.add("active");
        }

        section.querySelectorAll("[data-sidebar-children] .nav-link").forEach(function (link) {
            link.classList.remove("active");
            var href = link.getAttribute("href");
            if (currentPath.indexOf(href) === 0 && href !== prefix + "/") {
                link.classList.add("active");
            }
        });

        if (!trigger.dataset.sidebarBound) {
            trigger.dataset.sidebarBound = "true";

            trigger.addEventListener("click", function () {
                state[prefix] = true;
                saveSidebarState(state);
                toggleSection(section, true);
            });

            chevron.addEventListener("click", function (event) {
                event.preventDefault();
                event.stopPropagation();
                state[prefix] = !state[prefix];
                saveSidebarState(state);
                toggleSection(section, state[prefix]);
            });
        }
    });

    saveSidebarState(state);
}

document.addEventListener("DOMContentLoaded", initSidebar);
document.body.addEventListener("htmx:afterSwap", initSidebar);