aboutsummaryrefslogtreecommitdiff
path: root/htdocs/js/common/35-theme-switcher.js
blob: c6121527ae1b044cc639b054f5a14d6a2a1c9ba8 (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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
var ThemeSwitcher = (function() {
    /**
     * @type {string[]}
     */
    var modes = ['auto', 'dark', 'light'];

    /**
     * @type {number}
     */
    var currentModeIndex = -1;

    /**
     * @type {boolean|null}
     */
    var systemState = null;

    /**
     * @type {function[]}
     */
    var changeListeners = [];

    /**
     * @returns {boolean}
     */
    function isSystemModeSupported() {
        try {
            // crashes on:
            // Mozilla/5.0 (Windows NT 6.2; ARM; Trident/7.0; Touch; rv:11.0; WPDesktop; Lumia 630 Dual SIM) like Gecko
            // Mozilla/5.0 (iPhone; CPU iPhone OS 13_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Mobile/15E148 Safari/604.1
            // Mozilla/5.0 (iPad; CPU OS 12_5_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.2 Mobile/15E148 Safari/604.1
            //
            // error examples:
            // - window.matchMedia("(prefers-color-scheme: dark)").addEventListener is not a function. (In 'window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change",this.onSystemSettingChange.bind(this))', 'window.matchMedia("(prefers-color-scheme: dark)").addEventListener' is undefined)
            // - Object [object MediaQueryList] has no method 'addEventListener'
            return !!window['matchMedia']
                && typeof window.matchMedia("(prefers-color-scheme: dark)").addEventListener === 'function';
        } catch (e) {
            return false
        }
    }

    /**
     * @returns {boolean}
     */
    function isDarkModeApplied() {
        var st = StaticManager.loadedStyles;
        for (var i = 0; i < st.length; i++) {
            var name = st[i];
            if (ge('style_'+name+'_dark'))
                return true;
        }
        return false;
    }

    /**
     * @returns {string}
     */
    function getSavedMode() {
        var val = getCookie('theme');
        if (!val)
            return modes[0];
        if (modes.indexOf(val) === -1) {
            console.error('[ThemeSwitcher getSavedMode] invalid cookie value')
            unsetCookie('theme')
            return modes[0]
        }
        return val
    }

    /**
     * @param {boolean} dark
     */
    function changeTheme(dark) {
        addClass(document.body, 'theme-changing');

        var onDone = function() {
            window.requestAnimationFrame(function() {
                removeClass(document.body, 'theme-changing');
                changeListeners.forEach(function(f) {
                    try {
                        f(dark)
                    } catch (e) {
                        console.error('[ThemeSwitcher->changeTheme->onDone] error while calling user callback:', e)
                    }
                })
            })
        };

        window.requestAnimationFrame(function() {
            if (dark)
                enableDark(onDone);
            else
                disableDark(onDone);
        })
    }

    /**
     * @param {function} callback
     */
    function enableDark(callback) {
        var names = [];
        StaticManager.loadedStyles.forEach(function(name) {
            var el = ge('style_'+name+'_dark');
            if (el)
                return;
            names.push(name);
        });

        var left = names.length;
        names.forEach(function(name) {
            StaticManager.loadStyle(name, 'dark', once(function(e) {
                left--;
                if (left === 0)
                    callback();
            }));
        })
    }

    /**
     * @param {function} callback
     */
    function disableDark(callback) {
        StaticManager.loadedStyles.forEach(function(name) {
            var el = ge('style_'+name+'_dark');
            if (el)
                el.remove();
        })
        callback();
    }

    /**
     * @param {string} mode
     */
    function setLabel(mode) {
        var labelEl = ge('theme-switcher-label');
        labelEl.innerHTML = escape(lang('theme_'+mode));
    }

    return {
        init: function() {
            var cur = getSavedMode();
            currentModeIndex = modes.indexOf(cur);

            var systemSupported = isSystemModeSupported();
            if (!systemSupported) {
                if (currentModeIndex === 0) {
                    modes.shift(); // remove 'auto' from the list
                    currentModeIndex = 1; // set to 'light'
                    if (isDarkModeApplied())
                        disableDark();
                }
            } else {
                /**
                 * @param {boolean} dark
                 */
                var onSystemChange = function(dark) {
                    var prevSystemState = systemState;
                    systemState = dark;

                    if (modes[currentModeIndex] !== 'auto')
                        return;

                    if (systemState !== prevSystemState)
                        changeTheme(systemState);
                };

                window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', function(e) {
                    onSystemChange(e.matches === true)
                });

                onSystemChange(window.matchMedia('(prefers-color-scheme: dark)').matches === true);
            }

            setLabel(modes[currentModeIndex]);
        },

        next: function(e) {
            if (hasClass(document.body, 'theme-changing')) {
                console.log('next: theme changing is in progress, ignoring...')
                return;
            }

            currentModeIndex = (currentModeIndex + 1) % modes.length;
            switch (modes[currentModeIndex]) {
                case 'auto':
                    if (systemState !== null && systemState !== isDarkModeApplied())
                        changeTheme(systemState);
                    break;

                case 'light':
                    if (isDarkModeApplied())
                        changeTheme(false);
                    break;

                case 'dark':
                    if (!isDarkModeApplied())
                        changeTheme(true);
                    break;
            }

            setLabel(modes[currentModeIndex]);
            setCookie('theme', modes[currentModeIndex]);

            return cancelEvent(e);
        },

        /**
         * @param {function} f
         */
        addOnChangeListener: function(f) {
            changeListeners.push(f);
        }
    };
})();