File size: 3,194 Bytes
a86a01f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e184579
a86a01f
e184579
a86a01f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e184579
 
 
 
a86a01f
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
(function () {
    const storageKey = "tts-theme";

    function normalize(theme) {
        return theme === "dark" || theme === "light" ? theme : "light";
    }

    function currentTheme() {
        return normalize(
            document.documentElement.dataset.theme ||
            (document.documentElement.classList.contains("dark") ? "dark" : "light")
        );
    }

    function setTheme(theme) {
        const normalized = normalize(theme);
        const isDark = normalized === "dark";
        const root = document.documentElement;

        root.classList.toggle("dark", isDark);
        root.dataset.theme = normalized;

        if (document.body) {
            document.body.classList.toggle("dark", isDark);
            document.body.dataset.theme = normalized;
        }

        try {
            localStorage.setItem(storageKey, normalized);
        } catch {
        }

        updateControls();
        return isDark;
    }

    function updateControls() {
        const isDark = currentTheme() === "dark";
        const title = isDark ? "Switch to light mode" : "Switch to dark mode";
        const iconName = isDark ? "sun" : "moon";

        document.querySelectorAll("[data-theme-toggle]").forEach(button => {
            button.setAttribute("aria-pressed", String(isDark));
            button.setAttribute("aria-label", title);
            button.setAttribute("title", title);

            const icon = button.querySelector("[data-lucide]");
            if (icon) {
                icon.setAttribute("data-lucide", iconName);
            }
        });

        if (window.lucide) {
            window.lucide.createIcons();
        }
    }

    function initTheme() {
        let storedTheme = currentTheme();
        try {
            storedTheme = normalize(localStorage.getItem(storageKey) || storedTheme);
        } catch {
        }

        setTheme(storedTheme);
    }

    window.themePreference = {
        apply: setTheme,
        init: initTheme,
        toggle() {
            return setTheme(currentTheme() === "dark" ? "light" : "dark");
        }
    };

    window.updateTaskifyThemeControls = updateControls;
    window.toggleTaskifyThemeButton = function () {
        window.themePreference.toggle();
        return false;
    };

    if (!window.__taskifyThemeToggleBound) {
        window.__taskifyThemeToggleBound = true;
        document.addEventListener("click", event => {
            const target = event.target instanceof Element ? event.target : event.target?.parentElement;
            const button = target?.closest("[data-theme-toggle]");
            if (!button) {
                return;
            }

            event.preventDefault();
            event.stopPropagation();
            window.themePreference.toggle();
        }, true);
    }

    if (document.readyState === "loading") {
        document.addEventListener("DOMContentLoaded", initTheme, { once: true });
    } else {
        initTheme();
    }

    window.addEventListener("pageshow", updateControls);

    if (window.Blazor && typeof window.Blazor.addEventListener === "function") {
        window.Blazor.addEventListener("enhancedload", updateControls);
    }
})();