summaryrefslogtreecommitdiffstats
path: root/static/theme-manager.js
blob: 14e146930314e49461c3446d18c30aac7700c912 (plain) (blame)
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
/* jshint esversion: 2024, module: true */

import { ThemeMode } from "./enums.js";

/**
 * Static Theme Utilities
 * @class ThemeUtils
 */
class ThemeUtils {
	/**
	 * Get theme from localStorage
	 * @static
	 * @returns {Symbol} Theme mode
	 */
	static getStoredTheme() {
		const stored = localStorage.getItem("network-ui-theme");
		return stored === "light" ? ThemeMode.LIGHT : ThemeMode.DARK;
	}

	/**
	 * Store theme in localStorage
	 * @static
	 * @param {Symbol} theme - Theme mode
	 */
	static storeTheme(theme) {
		const themeString = theme === ThemeMode.LIGHT ? "light" : "dark";
		localStorage.setItem("network-ui-theme", themeString);
	}

	/**
	 * Get theme icon based on theme mode
	 * @static
	 * @param {Symbol} theme - Theme mode
	 * @returns {string} Icon character
	 */
	static getThemeIcon(theme) {
		return theme === ThemeMode.DARK ? "☀️" : "🌙";
	}

	/**
	 * Apply theme to document
	 * @static
	 * @param {Symbol} theme - Theme mode
	 */
	static applyThemeToDocument(theme) {
		const themeString = theme === ThemeMode.LIGHT ? "light" : "dark";
		document.documentElement.setAttribute("data-theme", themeString);
	}

	/**
	 * Toggle theme mode
	 * @static
	 * @param {Symbol} currentTheme - Current theme mode
	 * @returns {Symbol} New theme mode
	 */
	static toggleTheme(currentTheme) {
		return currentTheme === ThemeMode.DARK ? ThemeMode.LIGHT : ThemeMode.DARK;
	}
}

/**
 * Theme Manager for handling light/dark themes
 * @class ThemeManager
 */
class ThemeManager {
	#elements;
	#theme;

	/**
	 * @param {Object} elements - DOM elements
	 */
	constructor(elements) {
		this.#elements = elements;
		this.#theme = ThemeMode.DARK;
	}

	/**
	 * Initialize theme manager
	 * @method init
	 */
	init() {
		this.#theme = ThemeUtils.getStoredTheme();
		this.#applyTheme(this.#theme);
		this.#setupEventListeners();
	}

	/**
	 * Set up theme event listeners
	 * @private
	 */
	#setupEventListeners() {
		this.#elements.themeToggle?.addEventListener("click", () =>
			this.#handleThemeToggle(),
		);
	}

	/**
	 * Handle theme toggle
	 * @private
	 */
	#handleThemeToggle() {
		const newTheme = ThemeUtils.toggleTheme(this.#theme);
		this.#applyTheme(newTheme);
	}

	/**
	 * Apply theme to UI
	 * @private
	 * @param {Symbol} theme - Theme mode
	 */
	#applyTheme(theme) {
		this.#theme = theme;

		// Apply to document
		ThemeUtils.applyThemeToDocument(theme);

		// Store preference
		ThemeUtils.storeTheme(theme);

		// Update theme icon
		this.#updateThemeIcon();
	}

	/**
	 * Update theme icon
	 * @private
	 */
	#updateThemeIcon() {
		if (this.#elements.themeIcon) {
			this.#elements.themeIcon.textContent = ThemeUtils.getThemeIcon(
				this.#theme,
			);
		}
	}

	/**
	 * Get current theme
	 * @method getCurrentTheme
	 * @returns {Symbol} Current theme mode
	 */
	getCurrentTheme() {
		return this.#theme;
	}

	/**
	 * Set theme programmatically
	 * @method setTheme
	 * @param {Symbol} theme - Theme mode
	 */
	setTheme(theme) {
		if (theme === ThemeMode.LIGHT || theme === ThemeMode.DARK) {
			this.#applyTheme(theme);
		} else {
			console.warn("Invalid theme mode:", theme);
		}
	}

	/**
	 * Check if dark theme is active
	 * @method isDarkTheme
	 * @returns {boolean} True if dark theme is active
	 */
	isDarkTheme() {
		return this.#theme === ThemeMode.DARK;
	}

	/**
	 * Check if light theme is active
	 * @method isLightTheme
	 * @returns {boolean} True if light theme is active
	 */
	isLightTheme() {
		return this.#theme === ThemeMode.LIGHT;
	}
}

export { ThemeManager, ThemeUtils };