Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
218 lines (187 loc) · 6.17 KB
/
Copy pathutils.js
File metadata and controls
218 lines (187 loc) · 6.17 KB
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
215
216
217
218
// Shared utilities for X Touch Controller
// This file contains common functions used across multiple scripts
// Theme management utilities
const ThemeManager = {
cache: {
buttons: null,
savedTheme: null,
initialized: false
},
// Properly bound event handlers using arrow functions
handleThemeClick: (e) => {
e.preventDefault();
e.stopPropagation();
const theme = e.currentTarget.getAttribute('data-theme');
ThemeManager.setTheme(theme);
},
handleThemeTouch: (e) => {
// Only prevent default if we're on a button to avoid blocking scrolling
if (e.currentTarget.classList.contains('theme-btn')) {
e.preventDefault();
e.stopPropagation();
}
const theme = e.currentTarget.getAttribute('data-theme');
ThemeManager.setTheme(theme);
// Add visual feedback for mobile with better error handling
try {
e.currentTarget.style.transform = 'scale(0.95)';
setTimeout(() => {
if (e.currentTarget && e.currentTarget.style) {
e.currentTarget.style.transform = '';
}
}, 150);
} catch (error) {
ErrorHandler.logError(error, 'ThemeManager.handleThemeTouch');
}
},
init() {
// Ensure DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => ThemeManager.init());
return;
}
// Prevent multiple initializations
if (this.cache.initialized) {
return;
}
try {
// Get theme buttons
this.cache.buttons = document.querySelectorAll('.theme-btn');
if (!this.cache.buttons || this.cache.buttons.length === 0) {
console.warn('ThemeManager: No .theme-btn elements found. Retrying in 100ms...');
setTimeout(() => ThemeManager.init(), 100);
return;
}
// Get saved theme with fallback
try {
this.cache.savedTheme = localStorage.getItem('obs-theme') || 'ocean';
} catch (error) {
console.warn('ThemeManager: localStorage not available, using default theme');
this.cache.savedTheme = 'ocean';
}
this.applyTheme(this.cache.savedTheme);
// Add event listeners with proper binding
this.cache.buttons.forEach(btn => {
// Remove existing listeners to prevent duplicates
btn.removeEventListener('click', ThemeManager.handleThemeClick);
btn.removeEventListener('touchstart', ThemeManager.handleThemeTouch);
// Add new listeners
btn.addEventListener('click', ThemeManager.handleThemeClick);
btn.addEventListener('touchstart', ThemeManager.handleThemeTouch, { passive: false });
});
this.cache.initialized = true;
console.log('ThemeManager: Initialized successfully');
} catch (error) {
ErrorHandler.logError(error, 'ThemeManager.init');
// Retry initialization after a delay
setTimeout(() => ThemeManager.init(), 500);
}
},
setTheme(theme) {
if (!theme || theme === this.cache.savedTheme) return; // Skip if same theme or invalid
try {
this.applyTheme(theme);
// Save to localStorage with error handling
try {
localStorage.setItem('obs-theme', theme);
this.cache.savedTheme = theme;
} catch (error) {
console.warn('ThemeManager: Could not save theme to localStorage:', error);
// Still update the cache even if localStorage fails
this.cache.savedTheme = theme;
}
} catch (error) {
ErrorHandler.logError(error, 'ThemeManager.setTheme');
}
},
applyTheme(theme) {
try {
document.documentElement.setAttribute('data-theme', theme);
this.updateActiveThemeButton(theme);
} catch (error) {
ErrorHandler.logError(error, 'ThemeManager.applyTheme');
}
},
updateActiveThemeButton(activeTheme) {
if (!this.cache.buttons || !activeTheme) return;
try {
this.cache.buttons.forEach(btn => {
btn.classList.remove('active');
if (btn.getAttribute('data-theme') === activeTheme) {
btn.classList.add('active');
}
});
} catch (error) {
ErrorHandler.logError(error, 'ThemeManager.updateActiveThemeButton');
}
},
// Method to reinitialize if DOM changes
reinit() {
this.cache.initialized = false;
this.cache.buttons = null;
this.init();
}
};
// Performance utilities
const PerformanceUtils = {
debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
},
throttle(func, limit) {
let inThrottle;
return function() {
const args = arguments;
const context = this;
if (!inThrottle) {
func.apply(context, args);
inThrottle = true;
setTimeout(() => inThrottle = false, limit);
}
};
}
};
// Error handling utilities
const ErrorHandler = {
logError(error, context = '') {
console.error(`[${context}] Error:`, error);
// Could be extended to send errors to a logging service
},
showUserError(message, duration = 5000) {
try {
// Create a temporary error notification
const errorDiv = document.createElement('div');
errorDiv.className = 'alert alert-danger position-fixed';
errorDiv.style.cssText = 'top: 20px; right: 20px; z-index: 9999; min-width: 300px;';
errorDiv.textContent = message;
document.body.appendChild(errorDiv);
setTimeout(() => {
if (errorDiv.parentNode) {
errorDiv.parentNode.removeChild(errorDiv);
}
}, duration);
} catch (error) {
console.error('ErrorHandler.showUserError failed:', error);
}
}
};
// Auto-initialize ThemeManager when DOM is ready
if (typeof document !== 'undefined') {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => ThemeManager.init());
} else {
// DOM is already ready
ThemeManager.init();
}
}
// Export for use in other scripts
if (typeof module !== 'undefined' && module.exports) {
module.exports = { ThemeManager, PerformanceUtils, ErrorHandler };
}