Jake Vanderwerf
2025-10-18 b0194e10a87e16797a568d8a30d53ebecd27d8a4
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
class UserSettings {
    constructor() {
        this.cache = new window.jvbCache('settings');
        this.debouncer = window.debouncer;
 
        this.isLoggedIn = jvbSettings.currentUser !== null;
 
        this.initListeners();
 
        this.subscribers = new Set();
    }
 
    initListeners() {
        this.changeHandler = this.handleChange.bind(this);
 
        document.addEventListener('change', this.changeHandler);
    }
 
    handleChange(e) {
 
    }
 
    saveSetting(name, value) {
        this.cache.setItem(name, value);
        if (this.isLoggedIn) {
 
        }
    }
 
    loadSetting(name) {
        let value = this.cache.getItem(name);
 
        if (this.isLoggedIn) {
 
        }
    }
 
    loadUserSetting(name) {
 
    }
 
    /**
     * Event system
     */
    subscribe(callback) {
        this.subscribers.add(callback);
        return () => this.subscribers.delete(callback);
    }
 
    notify(event, data) {
        this.subscribers.forEach(cb => cb(event, data));
    }
 
    /***************************************************
     CLEANUP
    ***************************************************/
    destroy() {
        document.removeEventListener('change', this.changeHandler);
        this.subscribers.clear();
    }
}
 
document.addEventListener('DOMContentLoaded', function() {
    window.jvbUserSettings = new UserSettings();
});
 
// Theme switching functionality
document.addEventListener('DOMContentLoaded', function() {
    console.log('Theme switch initiated');
    const themeSwitch = document.getElementById('theme-switch');
 
    if (!themeSwitch) return;
 
    // Initialize theme from localStorage or system preference
    const prefersDark = window.matchMedia('(prefers-color-scheme: dark)');
    const storedTheme = localStorage.getItem('theme');
 
    if (storedTheme) {
        document.documentElement.classList.toggle('dark', storedTheme === 'dark');
        themeSwitch.checked = storedTheme === 'dark';
    } else {
        document.documentElement.classList.toggle('dark', prefersDark.matches);
        themeSwitch.checked = prefersDark.matches;
    }
 
    // Handle theme switch changes
    themeSwitch.addEventListener('change', async function () {
        const isDark = this.checked;
        document.documentElement.classList.toggle('dark', isDark);
        localStorage.setItem('theme', isDark ? 'dark' : 'light');
 
        // If user is logged in, save preference
        if (jvbSettings.currentUser !== null) {
            try {
                await fetch(`${jvbSettings.api}settings`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'X-WP-Nonce': jvbSettings.nonce,
                        'action_nonce': jvbSettings.dash,
                    },
                    body: JSON.stringify({
                        dark_mode: isDark,
                        user: jvbSettings.currentUser
                    })
                });
            } catch (error) {
                console.error('Failed to save theme preference:', error);
            }
        }
 
        // Update label
        const label = document.getElementById('theme-switch');
        if (label) {
            label.title = isDark ? 'Toggle Light Mode' : 'Toggle Dark Mode';
        }
    });
 
    // Handle system theme changes
    prefersDark.addEventListener('change', (e) => {
        if (!localStorage.getItem('theme')) {
            const isDark = e.matches;
            document.documentElement.classList.toggle('dark', isDark);
            themeSwitch.checked = isDark;
        }
    });
});