]>
Commit | Line | Data |
---|---|---|
70c1267e MF |
1 | |
2 | document.addEventListener('DOMContentLoaded', function () { | |
3 | restoreOptions(); | |
4 | document.querySelector('#saveBtn').addEventListener('click', saveOptions); | |
5 | document.querySelector('#restoreDefaultBtn').addEventListener('click', restoreToDefault); | |
6 | ||
68553004 MF |
7 | ['light', 'system', 'dark'].forEach((theme) => { |
8 | document.getElementById(`theme-${theme}`).addEventListener('click', setThemeClick); | |
9 | }); | |
10 | ||
70c1267e MF |
11 | //document.querySelector('body').addEventListener('click', restoreOptions); |
12 | }); | |
13 | ||
14 | function getSelectValue (selectId) { | |
15 | var select = document.getElementById(selectId); | |
16 | var value = select.children[select.selectedIndex].value; | |
17 | ||
18 | return value; | |
19 | } | |
20 | ||
21 | function setSelectValue (selectId, value) { | |
22 | var select = document.getElementById(selectId); | |
23 | for (var i = 0; i < select.children.length; i++) { | |
24 | var child = select.children[i]; | |
25 | if (child.value == value) { | |
26 | child.selected = "true"; | |
27 | break; | |
28 | } | |
29 | } | |
30 | } | |
31 | ||
68553004 MF |
32 | function setThemeClick() { |
33 | const theme = this.textContent.toLowerCase(); | |
34 | setThemeValue(theme); | |
35 | switchTheme(theme); | |
36 | } | |
37 | ||
38 | function setThemeValue(value) { | |
39 | const element = document.getElementById('prefsTheme'); | |
40 | switch (value) { | |
41 | case 'light': | |
42 | case 'dark': | |
43 | break; | |
44 | default: | |
45 | value = 'system'; | |
46 | break; | |
47 | } | |
48 | element.value = value; | |
49 | ||
50 | ['light', 'system', 'dark'].forEach((theme) => { | |
51 | document.getElementById(`theme-${theme}`).className = | |
52 | value == theme ? 'selected' : ''; | |
53 | }); | |
54 | } | |
55 | ||
70c1267e MF |
56 | function saveOptions () { |
57 | var backupPeriodMinutes = getSelectValue("prefsSelectBackupPeriod"); | |
58 | localStorage.prefsBackupTimer = backupPeriodMinutes; | |
59 | ||
60 | var backupMaxItems = getSelectValue("prefsSelectMaxBackups"); | |
61 | localStorage.prefsMaxBackupItems = backupMaxItems; | |
62 | ||
68553004 MF |
63 | var theme = document.getElementById('prefsTheme').value; |
64 | localStorage.prefsTheme = theme; | |
65 | ||
70c1267e | 66 | // Re-initialize the backup alarm |
e0b6a877 | 67 | chrome.runtime.getBackgroundPage((bg) => bg.initAlarm()); |
70c1267e MF |
68 | |
69 | // Update status to let user know options were saved. | |
70 | var status = document.getElementById("statusDiv"); | |
71 | status.innerHTML = "Options Saved"; | |
72 | setTimeout(function() { | |
73 | status.innerHTML = ""; | |
74 | }, 3000); | |
75 | } | |
76 | ||
77 | function restoreToDefault() { | |
78 | // those values are also set in background.js.. | |
79 | setSelectValue ("prefsSelectBackupPeriod", "5"); | |
80 | setSelectValue ("prefsSelectMaxBackups", "30"); | |
68553004 | 81 | setThemeValue("system"); |
70c1267e MF |
82 | |
83 | saveOptions(); | |
84 | } | |
85 | ||
86 | function restoreOptions() { | |
87 | var backupPeriodMinutes = localStorage.prefsBackupTimer; | |
88 | var backupMaxItems = localStorage.prefsMaxBackupItems; | |
68553004 | 89 | var theme = localStorage.prefsTheme; |
70c1267e MF |
90 | |
91 | setSelectValue ("prefsSelectBackupPeriod", backupPeriodMinutes); | |
92 | setSelectValue ("prefsSelectMaxBackups", backupMaxItems); | |
68553004 | 93 | setThemeValue(theme); |
70c1267e | 94 | } |