-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
140 lines (115 loc) · 4.24 KB
/
popup.js
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
const selectedFrequencyButtonClasses = ['selected', 'bg-blue-500', 'hover:bg-blue-700', 'text-white'];
const unselectedFrequencyButtonClasses = ['border', 'border-white', 'hover:border-gray-200', 'text-blue-500', 'hover:bg-gray-200']
const frequencyButtons = ['button_15', 'button_30', 'button_45', 'button_60'];
let aquaBuddyConfig = {};
function getHours() {
const hours = [];
for (let hour = 0; hour <= 23; hour++) {
const ampm = hour > 11 ? 'PM' : 'AM';
let hourlyTime = hour > 12 ? hour - 12 : hour;
hourlyTime = hourlyTime === 0 ? 12 : hourlyTime;
hourlyTime = hourlyTime < 10 ? '0' + hourlyTime : hourlyTime;
hours.push({
text: `${hourlyTime}:00 ${ampm}`,
value: hour
});
}
return hours;
}
function populateHours(select, hours, selectedOption) {
for (i = 0; i < hours.length; i++) {
let option = document.createElement('option');
const hour = hours[i];
option.text = hour.text;
option.value = hour.value;
option.selected = hour.value === selectedOption;
select.append(option);
}
}
function setDoNotDisturb() {
// Set do not disturb toggle switch
input_doNotDisturb = document.getElementById('input_doNotDisturb');
input_doNotDisturb.checked = aquaBuddyConfig.doNotDisturb;
input_doNotDisturb.onchange = doNotDisturbChangeHandler;
}
function doNotDisturbChangeHandler(e) {
e.preventDefault();
updateConfigInStorage({...aquaBuddyConfig, doNotDisturb: e.target.checked });
}
function setFrequencyButtons() {
for (let i = 0; i < frequencyButtons.length; i++) {
const button = document.getElementById(frequencyButtons[i]);
const frequencyValue = +button.attributes['data-frequency'].value;
if (aquaBuddyConfig.frequency === frequencyValue) {
button.classList.add(...selectedFrequencyButtonClasses);
} else {
button.classList.add(...unselectedFrequencyButtonClasses);
}
button.onclick = frequencyButtonClickHandler;
}
}
function frequencyButtonClickHandler(e) {
e.preventDefault();
// Get current selected frequency button
const selectedButton = document.querySelector('button.selected');
// Get clicked frequency button
const clickedButton = e.target;
// Update config
const frequencyValue = +e.target.attributes['data-frequency'].value;
updateConfigInStorage({...aquaBuddyConfig, frequency: frequencyValue });
// Update styles
selectedButton && selectedButton.classList.remove(...selectedFrequencyButtonClasses);
selectedButton && selectedButton.classList.add(...unselectedFrequencyButtonClasses);
clickedButton && clickedButton.classList.add(...selectedFrequencyButtonClasses);
clickedButton && clickedButton.classList.remove(...unselectedFrequencyButtonClasses);
}
function setQuietHours() {
// Get quite hours drop-downs
const quietHoursFrom = document.getElementById('quietHoursFrom');
const quietHoursTo = document.getElementById('quietHoursTo');
// Populate hours for display
const hours = getHours();
// Populate options in the quite hours drop-downs
populateHours(quietHoursFrom, hours, aquaBuddyConfig.quietHours.from);
populateHours(quietHoursTo, hours, aquaBuddyConfig.quietHours.to);
quietHoursFrom.onchange = quietHoursChangeHandler;
quietHoursTo.onchange = quietHoursChangeHandler;
}
function quietHoursChangeHandler(e) {
e.preventDefault();
const changedSelect = e.target;
console.log(changedSelect);
if (changedSelect.id === 'quietHoursFrom') {
updateConfigInStorage({
...aquaBuddyConfig,
quietHours: {
...aquaBuddyConfig.quietHours,
from: +changedSelect.value
}
});
} else {
updateConfigInStorage({
...aquaBuddyConfig,
quietHours: {
...aquaBuddyConfig.quietHours,
to: +changedSelect.value
}
});
}
}
function setupInterfaceWithConfig() {
setDoNotDisturb();
setFrequencyButtons();
setQuietHours();
}
function updateConfigInStorage(config) {
chrome.storage.sync.set({'aquaBuddyConfig': config});
}
// Initialize the popup window.
document.addEventListener('DOMContentLoaded', function () {
// Get aquaBuddyConfig from storage and attach event listeners
chrome.storage.sync.get(['aquaBuddyConfig'], function(result) {
aquaBuddyConfig = result.aquaBuddyConfig;
setupInterfaceWithConfig();
});
});