Debian Codegraph @

docs/assets/js/theme-manager.js

raw symbols SCIP
1 /***************************************************************************
2 * fheroes2: https://github.com/ihhub/fheroes2 *
3 * Copyright (C) 2025 - 2026 *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 // Theme toggle functionality
22 class ThemeManager
23 {
24 // Static properties
25 static themeToggle = null;
26 static themeKey = "theme";
27 static darkTheme = "dark";
28 static lightTheme = "light";
29 static accessibility = null;
30
31 // Static initialization method
32 static initialize()
33 {
34 // Initialize accessibility manager if not already initialized
35 if ( !this.accessibility ) {
36 this.accessibility = globalThis.fheroes2.AccessibilityManager ? new globalThis.fheroes2.AccessibilityManager() : null;
37 }
38
39 this.themeToggle = document.getElementById( "theme-toggle" );
40 this.applySavedTheme();
41 this.setupEventListeners();
42 this.setupAccessibility();
43 }
44
45 static setTheme( theme, saveToStorage = true )
46 {
47 document.documentElement.setAttribute( "data-theme", theme );
48 document.body.setAttribute( "data-theme", theme );
49
50 // Only save to localStorage if explicitly requested
51 if ( saveToStorage ) {
52 localStorage.setItem( this.themeKey, theme );
53 }
54
55 // Update button text and ARIA attributes
56 if ( this.themeToggle ) {
57 this.themeToggle.textContent = theme === this.darkTheme ? "☀️ Good Interface" : "🌙 Evil Interface";
58
59 // Update ARIA attributes
60 this.themeToggle.setAttribute( "aria-label", `Switch to ${theme === this.darkTheme ? this.lightTheme : this.darkTheme} mode` );
61 this.themeToggle.setAttribute( "aria-pressed", theme === this.darkTheme ? "true" : "false" );
62
63 // Announce theme change if accessibility manager is available
64 if ( this.accessibility ) {
65 this.accessibility.announce( `${theme === this.darkTheme ? 'Dark' : 'Light'} mode activated` );
66 }
67 }
68 }
69
70 static applySavedTheme()
71 {
72 const savedTheme = localStorage.getItem( this.themeKey );
73
74 if ( savedTheme ) {
75 this.setTheme( savedTheme );
76 }
77 else {
78 // Check system preference but don't save it to localStorage
79 const prefersDark = globalThis.matchMedia( "(prefers-color-scheme: dark)" ).matches;
80 this.setTheme( prefersDark ? this.darkTheme : this.lightTheme, false );
81 }
82 }
83
84 static setupEventListeners()
85 {
86 // Add event listener to the toggle button
87 if ( this.themeToggle ) {
88 this.themeToggle.addEventListener( "click", () => {
89 const currentTheme = document.documentElement.getAttribute( "data-theme" );
90 this.setTheme( currentTheme === this.darkTheme ? this.lightTheme : this.darkTheme );
91 } );
92 }
93
94 // Listen for system theme changes
95 globalThis.matchMedia( "(prefers-color-scheme: dark)" ).addEventListener( "change", ( e ) => {
96 // Only apply system preference if user hasn't manually set a preference
97 if ( !localStorage.getItem( this.themeKey ) ) {
98 this.setTheme( e.matches ? this.darkTheme : this.lightTheme, false );
99 }
100 } );
101 }
102
103 static setupAccessibility()
104 {
105 if ( this.themeToggle ) {
106 // Set initial ARIA attributes
107 const currentTheme = document.documentElement.getAttribute( "data-theme" );
108 this.themeToggle.setAttribute( "aria-label", `Switch to ${currentTheme === this.darkTheme ? this.lightTheme : this.darkTheme} mode` );
109 this.themeToggle.setAttribute( "aria-pressed", currentTheme === this.darkTheme ? "true" : "false" );
110
111 // Add keyboard event handling
112 this.themeToggle.addEventListener( "keydown", ( event ) => {
113 if ( event.key === "Enter" || event.key === " " ) {
114 event.preventDefault();
115 const currentTheme = document.documentElement.getAttribute( "data-theme" );
116 this.setTheme( currentTheme === this.darkTheme ? this.lightTheme : this.darkTheme );
117 }
118 } );
119 }
120 }
121 }
122
123 // Initialize the theme manager when the DOM is loaded
124 document.addEventListener( "DOMContentLoaded", () => { ThemeManager.initialize(); } );