-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.js
More file actions
532 lines (453 loc) · 18.5 KB
/
settings.js
File metadata and controls
532 lines (453 loc) · 18.5 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
<script type="text/javascript">
// Global variables
let APP_SETTINGS;
let EMAIL_TEMPLATE_DATA;
// Global flags
let saveFlag = true; // True if all changes saved, false if unsaved changes
let busyFlag = false; // True if operation in progress, false if no operation in progress
// Initialize application
// Conversion to async allows for parallel data retrieval from Apps Script
window.onload = async function() {
console.log("Initializing settings...");
const toolbar = document.getElementById('toolbar');
const page = document.getElementById('page');
const loadingIndicator = document.getElementById('loading-indicator');
try {
// Fetch data in parallel (async not needed but allows for future data streams)
const [appSettings] = await Promise.all([
new Promise((resolve, reject) => {
google.script.run.withSuccessHandler(resolve).withFailureHandler(reject).getAppSettings();
})
]);
// Set global variables
APP_SETTINGS = appSettings;
// Populate elements with data
await Promise.all([
loadSettings()
]);
setEventListeners();
console.log("Initialization complete!");
} catch (error) {
console.error("Error during initialization: ", error);
} finally {
// Hide loading indicator and show page
loadingIndicator.style.display = 'none';
toolbar.style.display = 'block';
page.style.display = 'flex';
}
};
function setEventListeners() {
console.log("Setting event listeners...");
const allTextInputs = document.querySelectorAll('.table-input[type=text]');
const allSelects = document.querySelectorAll('.table-select, #theme');
const allCurrencyInputs = document.querySelectorAll('.table-input[data-type="currency"]');
const saveChangesButton = document.getElementById('saveChangesButton');
const themeSelect = document.getElementById('theme');
const alertSoundSelect = document.getElementById('alertSound');
const emailSoundSelect = document.getElementById('emailSound');
const successSoundSelect = document.getElementById('successSound');
const removeSoundSelect = document.getElementById('removeSound');
window.addEventListener('beforeunload', function (e) {
if (!saveFlag) {
e.preventDefault();
e.returnValue = '';
}
});
allTextInputs.forEach(input => {
input.addEventListener('input', saveAlert);
});
allSelects.forEach(select => {
select.addEventListener('change', saveAlert);
});
allCurrencyInputs.forEach(input => {
input.addEventListener('input', saveAlert);
input.addEventListener('input', function(event) {
formatCurrency(this);
});
});
themeSelect.addEventListener('change', function() {
const theme = document.getElementById('theme').value;
const customTheme = document.getElementById('customTheme');
if (theme === "custom") {
customTheme.style.display = 'block';
} else {
customTheme.style.display = 'none';
}
});
alertSoundSelect.addEventListener('change', function() {
USER_SETTINGS.alertSound = alertSoundSelect.value;
playNotificationSound("alert");
});
emailSoundSelect.addEventListener('change', function() {
USER_SETTINGS.emailSound = emailSoundSelect.value;
playNotificationSound("email");
});
successSoundSelect.addEventListener('change', function() {
USER_SETTINGS.successSound = successSoundSelect.value;
playNotificationSound("success");
});
removeSoundSelect.addEventListener('change', function() {
USER_SETTINGS.removeSound = removeSound.value;
playNotificationSound("remove");
});
document.getElementById('silentModeSwitch').addEventListener('change', function() {
USER_SETTINGS.silentMode = this.checked ? 'true' : 'false';
saveAlert();
});
document.getElementById('templateSubject').addEventListener('input', saveAlert);
document.getElementById('templateBody').addEventListener('input', saveAlert);
saveChangesButton.addEventListener('click', saveSettings);
console.log("Complete!");
}
///////////////////
// LOAD SETTINGS //
///////////////////
function loadSettings() {
console.log("Loading settings...");
// Appearance
setColorPicker();
const themeSelect = document.getElementById('theme');
const customTheme = document.getElementById('customTheme');
themeSelect.value = USER_SETTINGS.theme;
if (USER_SETTINGS.theme === "custom") {
customTheme.style.display = 'block';
} else {
customTheme.style.display = 'none';
}
// Sound Effects
const silentMode = USER_SETTINGS.silentMode === 'true'; // Convert string to boolean
document.getElementById('alertSound').value = USER_SETTINGS.alertSound;
document.getElementById('emailSound').value = USER_SETTINGS.emailSound;
document.getElementById('removeSound').value = USER_SETTINGS.removeSound;
document.getElementById('successSound').value = USER_SETTINGS.successSound;
document.getElementById('silentModeSwitch').checked = silentMode; // Use boolean to set switch state
//School Information
document.getElementById('schoolName').value = APP_SETTINGS.schoolSettings.schoolName || '';
document.getElementById('schoolYear').value = APP_SETTINGS.schoolSettings.schoolYear || '';
//School Fees
document.getElementById('registrationFee').value = APP_SETTINGS.feeSettings.registrationFee || '';
document.getElementById('registrationFeeEEC').value = APP_SETTINGS.feeSettings.registrationFeeEEC || '';
document.getElementById('hugFee').value = APP_SETTINGS.feeSettings.hugFee || '';
document.getElementById('familyCommitmentFee').value = APP_SETTINGS.feeSettings.familyCommitmentFee || '';
document.getElementById('flashFee').value = APP_SETTINGS.feeSettings.flashFee || '';
document.getElementById('withdrawalFee').value = APP_SETTINGS.feeSettings.withdrawalFee || '';
console.log(APP_SETTINGS.emailTemplateSettings);
loadEmailTemplateSettings(APP_SETTINGS.emailTemplateSettings);
console.log("Complete!");
}
function setColorPicker() {
const themeTypeSelect = document.getElementById('themeTypeSelect');
const primaryColorPicker = document.getElementById('primaryColorPicker');
const accentColorPicker = document.getElementById('accentColorPicker');
themeTypeSelect.value = getComputedStyle(document.documentElement).getPropertyValue('color-scheme').trim();
primaryColorPicker.value = getComputedStyle(document.documentElement).getPropertyValue('--primary-color').trim();
accentColorPicker.value = getComputedStyle(document.documentElement).getPropertyValue('--accent-color').trim();
}
function loadEmailTemplateSettings(emailTemplateSettings) {
EMAIL_TEMPLATE_SETTINGS = {
waitlist: {
subject: emailTemplateSettings.waitlist.subject || '',
body: emailTemplateSettings.waitlist.body || '',
unsavedSubject: '',
unsavedBody: ''
},
evaluation: {
subject: emailTemplateSettings.evaluation.subject || '',
body: emailTemplateSettings.evaluation.body || '',
unsavedSubject: '',
unsavedBody: ''
},
screeningEEC: {
subject: emailTemplateSettings.screeningEEC.subject || '',
body: emailTemplateSettings.screeningEEC.body || '',
unsavedSubject: '',
unsavedBody: ''
},
screeningSchool: {
subject: emailTemplateSettings.screeningSchool.subject || '',
body: emailTemplateSettings.screeningSchool.body || '',
unsavedSubject: '',
unsavedBody: ''
},
acceptanceEEC: {
subject: emailTemplateSettings.acceptanceEEC.subject || '',
body: emailTemplateSettings.acceptanceEEC.body || '',
unsavedSubject: '',
unsavedBody: ''
},
acceptanceSchool: {
subject: emailTemplateSettings.acceptanceSchool.subject || '',
body: emailTemplateSettings.acceptanceSchool.body || '',
unsavedSubject: '',
unsavedBody: ''
},
acceptanceConditionalEEC: {
subject: emailTemplateSettings.acceptanceConditionalEEC.subject || '',
body: emailTemplateSettings.acceptanceConditionalEEC.body || '',
unsavedSubject: '',
unsavedBody: ''
},
acceptanceConditionalSchool: {
subject: emailTemplateSettings.acceptanceConditionalSchool.subject || '',
body: emailTemplateSettings.acceptanceConditionalSchool.body || '',
unsavedSubject: '',
unsavedBody: ''
},
rejection: {
subject: emailTemplateSettings.rejection.subject || '',
body: emailTemplateSettings.rejection.body || '',
unsavedSubject: '',
unsavedBody: ''
},
completion: {
subject: emailTemplateSettings.completion.subject || '',
body: emailTemplateSettings.completion.body || '',
unsavedSubject: '',
unsavedBody: ''
}
};
// Load initial email template settings into UI
const templateTypeSelect = document.getElementById('templateType');
const templateSubjectInput = document.getElementById('templateSubject');
const templateBodyInput = document.getElementById('templateBody');
// Set the default template type to "referral"
const defaultTemplate = EMAIL_TEMPLATE_SETTINGS.waitlist;
templateSubjectInput.value = defaultTemplate.subject;
templateBodyInput.innerHTML = defaultTemplate.body;
// Update unsaved subject on input
templateSubjectInput.addEventListener('input', function() {
const selectedTemplate = EMAIL_TEMPLATE_SETTINGS[templateTypeSelect.value];
if (selectedTemplate) {
selectedTemplate.unsavedSubject = templateSubjectInput.value;
}
});
// Update unsaved body on input
templateBodyInput.addEventListener('input', function() {
const selectedTemplate = EMAIL_TEMPLATE_SETTINGS[templateTypeSelect.value];
if (selectedTemplate) {
selectedTemplate.unsavedBody = templateBodyInput.innerHTML;
}
});
// Handle template type change
templateTypeSelect.addEventListener('change', function() {
const selectedTemplate = EMAIL_TEMPLATE_SETTINGS[templateTypeSelect.value];
if (selectedTemplate) {
templateSubjectInput.value = selectedTemplate.unsavedSubject || selectedTemplate.subject;
templateBodyInput.innerHTML = selectedTemplate.unsavedBody || selectedTemplate.body;
templateBodyInput.scrollTop = 0;
}
});
}
///////////////////
// SAVE SETTINGS //
///////////////////
function saveSettings() {
if (busyFlag) {
showError("Error: OPERATION_IN_PROGRESS");
busyFlag = false;
return;
}
showToast("", "Saving changes...", 5000);
busyFlag = true;
appSettings = getAppSettings();
userSettings = getUserSettings();
google.script.run
.withSuccessHandler(() => {
APP_SETTINGS = appSettings; // Save to global settings
USER_SETTINGS = userSettings; // Save to global user settings
// Update the UI
setTheme();
setColorPicker();
document.getElementById('header-text').innerText = "Falcon Enrollment - " + APP_SETTINGS.schoolSettings.schoolYear;
saveChangesButton.classList.remove('tool-bar-button-unsaved');
playNotificationSound("success");
showToast("", "Settings saved!", 5000);
saveFlag = true;
busyFlag = false;
})
.withFailureHandler((error) => {
const errorString = String(error);
if (errorString.includes("401")) {
sessionError();
} else {
showError("Error: SAVE_FAILURE");
}
saveFlag = false;
busyFlag = false;
})
.writeSettings(userSettings, appSettings);
}
function getUserSettings() {
const theme = document.getElementById('theme').value;
let customThemeType;
let customThemePrimaryColor;
let customThemeAccentColor;
if (theme === 'custom') {
customThemeType = document.getElementById('themeTypeSelect').value;
customThemePrimaryColor = document.getElementById('primaryColorPicker').value;
customThemeAccentColor = document.getElementById('accentColorPicker').value;
} else {
customThemeType = '';
customThemePrimaryColor = '';
customThemeAccentColor = '';
}
return {
theme: theme,
customThemeType: customThemeType,
customThemePrimaryColor: customThemePrimaryColor,
customThemeAccentColor: customThemeAccentColor,
alertSound: document.getElementById('alertSound').value,
emailSound: document.getElementById('emailSound').value,
removeSound: document.getElementById('removeSound').value,
successSound: document.getElementById('successSound').value,
silentMode: document.getElementById('silentModeSwitch').checked ? 'true' : 'false'
};
}
function getAppSettings() {
// Get school settings
const schoolSettings = {
schoolName: document.getElementById('schoolName').value,
schoolYear: document.getElementById('schoolYear').value
};
// Fee settings
const feeSettings = {
registrationFee: document.getElementById('registrationFee').value,
registrationFeeEEC: document.getElementById('registrationFeeEEC').value,
hugFee: document.getElementById('hugFee').value,
familyCommitmentFee: document.getElementById('familyCommitmentFee').value,
flashFee: document.getElementById('flashFee').value,
withdrawalFee: document.getElementById('withdrawalFee').value
};
// Get email template settings
Object.keys(EMAIL_TEMPLATE_SETTINGS).forEach((key) => {
const template = EMAIL_TEMPLATE_SETTINGS[key];
if (template.unsavedSubject !== "" || template.unsavedBody !== "") {
template.subject = template.unsavedSubject || template.subject;
template.body = template.unsavedBody || template.body;
}
});
const emailTemplateSettings = {
waitlist: {
subject: EMAIL_TEMPLATE_SETTINGS.waitlist.subject,
body: EMAIL_TEMPLATE_SETTINGS.waitlist.body
},
evaluation: {
subject: EMAIL_TEMPLATE_SETTINGS.evaluation.subject,
body: EMAIL_TEMPLATE_SETTINGS.evaluation.body
},
screeningEEC: {
subject: EMAIL_TEMPLATE_SETTINGS.screeningEEC.subject,
body: EMAIL_TEMPLATE_SETTINGS.screeningEEC.body
},
screeningSchool: {
subject: EMAIL_TEMPLATE_SETTINGS.screeningSchool.subject,
body: EMAIL_TEMPLATE_SETTINGS.screeningSchool.body
},
acceptanceEEC: {
subject: EMAIL_TEMPLATE_SETTINGS.acceptanceEEC.subject,
body: EMAIL_TEMPLATE_SETTINGS.acceptanceEEC.body
},
acceptanceSchool: {
subject: EMAIL_TEMPLATE_SETTINGS.acceptanceSchool.subject,
body: EMAIL_TEMPLATE_SETTINGS.acceptanceSchool.body
},
acceptanceConditionalEEC: {
subject: EMAIL_TEMPLATE_SETTINGS.acceptanceConditionalEEC.subject,
body: EMAIL_TEMPLATE_SETTINGS.acceptanceConditionalEEC.body
},
acceptanceConditionalSchool: {
subject: EMAIL_TEMPLATE_SETTINGS.acceptanceConditionalSchool.subject,
body: EMAIL_TEMPLATE_SETTINGS.acceptanceConditionalSchool.body
},
rejection: {
subject: EMAIL_TEMPLATE_SETTINGS.rejection.subject,
body: EMAIL_TEMPLATE_SETTINGS.rejection.body
},
completion: {
subject: EMAIL_TEMPLATE_SETTINGS.completion.subject,
body: EMAIL_TEMPLATE_SETTINGS.completion.body
}
};
return {
schoolSettings,
feeSettings,
emailTemplateSettings
};
}
///////////////////////
// UTILITY FUNCTIONS //
///////////////////////
function showError(errorType, callback = "") {
const warningIcon = `<i class="bi bi-exclamation-triangle-fill" style="color: var(--warning-color); margin-right: 10px;"></i>`;
const errorIcon = `<i class="bi bi-x-circle-fill" style="color: var(--error-color); margin-right: 10px;"></i>`;
let title;
let message;
let button1;
let button2;
switch (errorType) {
case "Error: OPERATION_IN_PROGRESS":
title = warningIcon + "Operation In Progress";
message = "Please wait until the operation completes and try again.";
button1 = "Close";
break;
case "Error: SAVE_FAILURE":
title = errorIcon + "Save Error";
message = "An unknown error occurred while saving the settings. The operation could not be completed.";
button1 = "Close";
break;
default:
title = errorIcon + "Error";
message = errorType;
button1 = "Close";
break;
}
playNotificationSound("alert");
showAlertModal(title, message, button1, button2);
}
async function sessionError() {
const errorIcon = `<i class="bi bi-x-circle-fill" style="color: var(--error-color); margin-right: 10px;"></i>`;
const title = `${errorIcon}Session Expired`;
const message = "The current session has expired. Please sign in with Google and try again.";
playNotificationSound("alert");
const buttonText = await showAlertModal(title, message, "Cancel", "Sign in");
if (buttonText === "Sign in") {
const signInUrl = "https://accounts.google.com";
const signInTab = window.open(signInUrl, "_blank");
}
}
function saveAlert() {
const saveChangesButton = document.getElementById("saveChangesButton");
saveFlag = false;
saveChangesButton.classList.add('tool-bar-button-unsaved');
}
/////////////////////
// DATA VALIDATION //
/////////////////////
function formatCurrency(input) {
// Remove all non-digit characters except for the first period
let inputValue = input.value.replace(/[^\d.]+/g, "");
// Split the input value into dollars and cents
let [dollars, cents] = inputValue.split(".");
// Add commas to the dollars part
let formattedDollars = '';
let count = 0;
for (let i = dollars.length - 1; i >= 0; i--) {
count++;
formattedDollars = dollars.charAt(i) + formattedDollars;
if (count % 3 === 0 && i !== 0) {
formattedDollars = ',' + formattedDollars;
}
}
// Limit cents to two digits
if (cents !== undefined) {
cents = cents.slice(0, 2); // Take only the first two characters
}
// Combine dollars and cents with a period
let formattedValue = '$' + formattedDollars;
if (cents !== undefined) {
formattedValue += '.' + cents;
}
// Update the input value with the formatted value
input.value = formattedValue;
}
</script>