-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtedConfigLoader.ts
More file actions
279 lines (250 loc) · 9.23 KB
/
tedConfigLoader.ts
File metadata and controls
279 lines (250 loc) · 9.23 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// tedConfigLoader.ts
interface ColorSet {
name: string;
description: string;
textColor: string;
backgroundColor: string;
}
interface ColorScheme {
name: string;
description: string;
screen: {
textColor: string;
backgroundColor: string;
};
colorSets: {
color1: ColorSet;
color2: ColorSet;
color3: ColorSet;
color4: ColorSet;
};
}
interface TEDColorConfig {
version: string;
description: string;
availableColors: string[];
colorSchemes: {
dark: ColorScheme;
bright: ColorScheme;
custom: ColorScheme;
};
buttonTypeMapping: Record<string, string>;
userPreferences: {
activeScheme: string;
customButtonOverrides: Record<string, { textColor: string; backgroundColor: string }>;
};
}
interface TEDConfig {
tedColorConfig: TEDColorConfig;
}
class TEDColorConfigLoader {
private config: TEDConfig | null = null;
private configPath: string = '/ted-color-config.json'; // Adjust path as needed
/**
* Load TED color configuration from JSON file
*/
async loadConfig(): Promise<TEDConfig> {
try {
const response = await fetch(this.configPath);
if (!response.ok) {
throw new Error(`Failed to load config: ${response.statusText}`);
}
this.config = await response.json();
return this.config!;
} catch (error) {
console.warn('Failed to load TED color config, using defaults:', error);
return this.getDefaultConfig();
}
}
/**
* Apply color scheme from JSON configuration
*/
applyColorScheme(schemeName: keyof TEDColorConfig['colorSchemes']): void {
if (!this.config) {
console.error('Config not loaded. Call loadConfig() first.');
return;
}
const scheme = this.config.tedColorConfig.colorSchemes[schemeName];
if (!scheme) {
console.error(`Color scheme '${schemeName}' not found.`);
return;
}
const root = document.documentElement;
// Apply screen colors
root.style.setProperty('--ted-screen-text', `var(--${scheme.screen.textColor})`);
root.style.setProperty('--ted-screen-background', `var(--${scheme.screen.backgroundColor})`);
// Apply color sets
Object.entries(scheme.colorSets).forEach(([colorSetKey, colorSet]) => {
root.style.setProperty(`--ted-${colorSetKey}-text`, `var(--${colorSet.textColor})`);
root.style.setProperty(`--ted-${colorSetKey}-background`, `var(--${colorSet.backgroundColor})`);
});
// Save user preference
this.config.tedColorConfig.userPreferences.activeScheme = schemeName;
this.saveUserPreferences();
}
/**
* Get available color schemes
*/
getAvailableSchemes(): Array<{ key: string; name: string; description: string }> {
if (!this.config) return [];
return Object.entries(this.config.tedColorConfig.colorSchemes).map(([key, scheme]) => ({
key,
name: scheme.name,
description: scheme.description
}));
}
/**
* Get available colors from palette
*/
getAvailableColors(): string[] {
return this.config?.tedColorConfig.availableColors || [];
}
/**
* Update custom color scheme
*/
updateCustomScheme(updates: Partial<ColorScheme>): void {
if (!this.config) return;
const customScheme = this.config.tedColorConfig.colorSchemes.custom;
Object.assign(customScheme, updates);
this.saveConfig();
this.applyColorScheme('custom');
}
/**
* Override individual button colors
*/
setButtonColorOverride(buttonId: string, textColor: string, backgroundColor: string): void {
if (!this.config) return;
this.config.tedColorConfig.userPreferences.customButtonOverrides[buttonId] = {
textColor,
backgroundColor
};
// Apply the override immediately
const buttonElement = document.querySelector(`[data-button-id="${buttonId}"]`) as HTMLElement;
if (buttonElement) {
buttonElement.style.color = `var(--${textColor})`;
buttonElement.style.backgroundColor = `var(--${backgroundColor})`;
}
this.saveUserPreferences();
}
/**
* Load and apply saved user preferences
*/
loadUserPreferences(): void {
const saved = localStorage.getItem('ted-color-preferences');
if (saved && this.config) {
try {
const preferences = JSON.parse(saved);
this.config.tedColorConfig.userPreferences = {
...this.config.tedColorConfig.userPreferences,
...preferences
};
// Apply saved scheme
this.applyColorScheme(preferences.activeScheme || 'dark');
// Apply custom button overrides
Object.entries(preferences.customButtonOverrides || {}).forEach(([buttonId, colors]: [string, any]) => {
const buttonElement = document.querySelector(`[data-button-id="${buttonId}"]`) as HTMLElement;
if (buttonElement) {
buttonElement.style.color = `var(--${colors.textColor})`;
buttonElement.style.backgroundColor = `var(--${colors.backgroundColor})`;
}
});
} catch (error) {
console.warn('Failed to load user preferences:', error);
}
}
}
/**
* Save user preferences to localStorage
*/
private saveUserPreferences(): void {
if (this.config) {
localStorage.setItem('ted-color-preferences',
JSON.stringify(this.config.tedColorConfig.userPreferences)
);
}
}
/**
* Save entire config (for custom scheme updates)
*/
private async saveConfig(): Promise<void> {
// Note: This would typically save to a server endpoint
// For client-side only, we save to localStorage
if (this.config) {
localStorage.setItem('ted-color-config', JSON.stringify(this.config));
}
}
/**
* Get default configuration if JSON fails to load
*/
private getDefaultConfig(): TEDConfig {
return {
tedColorConfig: {
version: "1.0",
description: "TED Color Scheme Configuration",
availableColors: [
"black", "blue", "green", "cyan", "red", "magenta", "brown",
"light-gray", "white", "light-blue", "light-green", "light-cyan",
"light-red", "light-magenta", "yellow", "dark-gray"
],
colorSchemes: {
dark: {
name: "Dark Environment",
description: "Best for dark operating environments",
screen: { textColor: "green", backgroundColor: "black" },
colorSets: {
color1: { name: "Normal Buttons", description: "", textColor: "green", backgroundColor: "black" },
color2: { name: "Significant DA", description: "", textColor: "yellow", backgroundColor: "black" },
color3: { name: "Utility Buttons", description: "", textColor: "cyan", backgroundColor: "black" },
color4: { name: "Significant Radio", description: "", textColor: "red", backgroundColor: "black" }
}
},
bright: {
name: "Bright Environment",
description: "Best for bright operating environments",
screen: { textColor: "black", backgroundColor: "light-gray" },
colorSets: {
color1: { name: "Normal Buttons", description: "", textColor: "black", backgroundColor: "light-gray" },
color2: { name: "Significant DA", description: "", textColor: "brown", backgroundColor: "light-gray" },
color3: { name: "Utility Buttons", description: "", textColor: "blue", backgroundColor: "light-gray" },
color4: { name: "Significant Radio", description: "", textColor: "red", backgroundColor: "light-gray" }
}
},
custom: {
name: "Custom Colors",
description: "User-defined color scheme",
screen: { textColor: "green", backgroundColor: "black" },
colorSets: {
color1: { name: "Normal Buttons", description: "", textColor: "green", backgroundColor: "black" },
color2: { name: "Significant DA", description: "", textColor: "yellow", backgroundColor: "black" },
color3: { name: "Utility Buttons", description: "", textColor: "cyan", backgroundColor: "black" },
color4: { name: "Significant Radio", description: "", textColor: "red", backgroundColor: "black" }
}
}
},
buttonTypeMapping: {
"intercom-ringback": "color1",
"intercom-override": "color1",
"control-monitor": "color1",
"normal-radio": "color1",
"sidetone-radio": "color1",
"preempt-radio": "color1",
"interphone": "color2",
"special-function": "color3",
"dial-digit": "color3",
"emergency-radio": "color4"
},
userPreferences: {
activeScheme: "dark",
customButtonOverrides: {}
}
}
};
}
}
// Export singleton instance
export const tedColorConfig = new TEDColorConfigLoader();
// Initialize and load config when module is imported
export async function initializeTEDColors(): Promise<void> {
await tedColorConfig.loadConfig();
tedColorConfig.loadUserPreferences();
}