forked from matchu/Strict-Workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
343 lines (288 loc) · 10.9 KB
/
options.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
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
/*
Localization
*/
// Localize all elements with a data-i18n="message_name" attribute
var localizedElements = document.querySelectorAll('[data-i18n]'), el, message;
for (var i = 0; i < localizedElements.length; i++) {
el = localizedElements[i];
message = chrome.i18n.getMessage(el.getAttribute('data-i18n'));
// Capitalize first letter if element has attribute data-i18n-caps
if (el.hasAttribute('data-i18n-caps')) {
message = message.charAt(0).toUpperCase() + message.substr(1);
}
el.innerHTML = message;
}
/*
Form interaction
*/
var form = document.getElementById('options-form'),
siteWhitelistEl = document.getElementById('whitelist'),
siteBlacklistEl = document.getElementById('blacklist'),
whitelistSelectEl = document.getElementById('blacklist-or-whitelist'),
showNotificationsEl = document.getElementById('show-notifications'),
shouldRingEl = document.getElementById('should-ring'),
clickRestartsEl = document.getElementById('click-restarts'),
allowStopEl = document.getElementById('allow_stop_pomodoro'),
clickSkipBreakEl = document.getElementById('click-skip-break'),
autostartWorkEl = document.getElementById('autostart-work'),
autostartBreakEl = document.getElementById('autostart-break'),
saveSuccessfulEl = document.getElementById('save-successful'),
timeFormatErrorEl = document.getElementById('time-format-error'),
workcyclesEl = document.getElementById('workcycles'),
longbreaksEnableEl = document.getElementById('longbreak-enable'),
operationModeEl = document.getElementById('operation_mode'),
butAddTimeEl = document.getElementById("butAddTime"),
timeTableEl = document.getElementById("timeTable"),
volumeSliderEl = document.getElementById("options_ringVolume");
volumeValueEl = document.getElementById("volumeValue");
background = chrome.extension.getBackgroundPage(),
startCallbacks = {},
durationEls = {};
var timeslots = {};
durationEls['work'] = document.getElementById('work-duration');
durationEls['break'] = document.getElementById('break-duration');
durationEls['longbreak'] = document.getElementById('longbreak-duration');
var TIME_REGEX = /^([0-9]+)(:([0-9]{2}))?$/;
form.onsubmit = function () {
console.log("form submitted");
var durations = {},
duration, durationStr, durationMatch;
for (var key in durationEls) {
durationStr = durationEls[key].value;
durationMatch = durationStr.match(TIME_REGEX);
if (durationMatch) {
console.log(durationMatch);
durations[key] = (60 * parseInt(durationMatch[1], 10));
if (durationMatch[3]) {
durations[key] += parseInt(durationMatch[3], 10);
}
} else {
timeFormatErrorEl.className = 'show';
return false;
}
}
console.log(durations);
saveTimetable();
background.setPrefs({
siteWhitelist: siteWhitelistEl.value.split(/\r?\n/),
siteBlacklist: siteBlacklistEl.value.split(/\r?\n/),
durations: durations,
operationMode: operationModeEl.value,
longbreaksEnabled: longbreaksEnableEl.checked,
cyclesBeforeLongBreak: workcycles.value,
showNotifications: showNotificationsEl.checked,
shouldRing: shouldRingEl.checked,
ringVolume: volumeSliderEl.value,
clickRestarts: clickRestartsEl.checked,
clickSkipBreak: clickSkipBreakEl.checked,
whitelist: whitelistSelectEl.selectedIndex == 1,
autostartWork: autostartWorkEl.checked,
autostartBreak: autostartBreakEl.checked,
allowStop: allowStopEl.checked,
timeslots: timeslots,
})
createContextMenu();
saveSuccessfulEl.className = 'show';
return false;
}
siteBlacklistEl.onfocus = formAltered;
siteWhitelistEl.onfocus = formAltered;
showNotificationsEl.onchange = formAltered;
shouldRingEl.onchange = formAltered;
clickRestartsEl.onchange = formAltered;
clickSkipBreakEl.onchange = formAltered;
autostartWorkEl.onchange = formAltered;
autostartBreakEl.onchange = formAltered;
allowStopEl.onchange = formAltered;
volumeSliderEl.onchange = formAltered;
whitelistSelectEl.onchange = function () {
setListVisibility();
formAltered();
};
//Load current values when page loads
siteBlacklistEl.value = background.PREFS.siteBlacklist.join("\n");
siteWhitelistEl.value = background.PREFS.siteWhitelist.join("\n");
showNotificationsEl.checked = background.PREFS.showNotifications;
shouldRingEl.checked = background.PREFS.shouldRing;
clickRestartsEl.checked = background.PREFS.clickRestarts;
clickSkipBreakEl.checked = background.PREFS.clickSkipBreak;
autostartWorkEl.checked = background.PREFS.autostartWork;
autostartBreakEl.checked = background.PREFS.autostartBreak;
allowStopEl.checked = background.PREFS.allowStop;
operationModeEl.value = background.PREFS.operationMode;
longbreaksEnableEl.checked = background.PREFS.longbreaksEnabled;
workcyclesEl.value = background.PREFS.cyclesBeforeLongBreak;
volumeSliderEl.value = background.PREFS.ringVolume;
whitelistSelectEl.selectedIndex = background.PREFS.whitelist ? 1 : 0;
setListVisibility();
timeslots = background.PREFS.timeslots;
loadTimetable();
//Define button event to add time
document.getElementById("butAddTime").addEventListener("click", addTimeLine);
var duration, minutes, seconds;
for (var key in durationEls) {
duration = background.PREFS.durations[key];
seconds = duration % 60;
minutes = (duration - seconds) / 60;
if (seconds >= 10) {
durationEls[key].value = minutes + ":" + seconds;
} else if (seconds > 0) {
durationEls[key].value = minutes + ":0" + seconds;
} else {
durationEls[key].value = minutes;
}
durationEls[key].onfocus = formAltered;
}
function saveTimetable(){
timeslots = {};
for (var i = 1, row; row = timeTableEl.rows[i]; i++) {
timeslots[i-1] = new TimeSlot();
for (var j = 0, cell; cell = row.cells[j]; j++) {
switch(j){
case 0:
timeslots[i-1].startTime = cell.firstChild.value;
break;
case 1:
timeslots[i-1].stopTime = cell.firstChild.value;
break;
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
timeslots[i-1].daysEnabled[j-2] = cell.firstChild.checked;
break;
default:
//Buttons and not required stuff
}
//iterate through columns
//columns would be accessed using the "col" variable assigned in the for loop
}
}
}
function loadTimetable(){
emptyTimetable();
var i = 0;
for (var id in timeslots) {
addTimeLine();
timeTableEl.rows[i+1].cells[0].firstChild.value = timeslots[id].startTime;
timeTableEl.rows[i+1].cells[1].firstChild.value = timeslots[id].stopTime;
for (var j = 0; j <= 6; j++){
timeTableEl.rows[i+1].cells[j+2].firstChild.checked = timeslots[id].daysEnabled[j];
}
i++;
}
}
function setListVisibility() {
if (whitelistSelectEl.selectedIndex) {
siteBlacklistEl.style.display = 'none';
siteWhitelistEl.style.display = 'inline';
} else {
siteBlacklistEl.style.display = 'inline';
siteWhitelistEl.style.display = 'none';
}
}
function formAltered() {
saveSuccessfulEl.removeAttribute('class');
timeFormatErrorEl.removeAttribute('class');
}
function setInputDisabled(state) {
siteBlacklistEl.disabled = state;
siteWhitelistEl.disabled = state;
whitelistSelectEl.disabled = state;
for (var key in durationEls) {
durationEls[key].disabled = state;
}
showNotificationsEl.disabled = state;
shouldRingEl.disabled = state;
clickRestartsEl.disabled = state;
clickSkipBreakEl.disabled = state;
autostartWorkEl.disabled = state;
autostartBreakEl.disabled = state;
longbreaksEnableEl.disabled = state;
workcyclesEl.disabled = state;
operationModeEl.disabled = state;
butAddTimeEl.disabled = state;
}
//this function add a line to the option page to select an exlusion time (lunch, dinner, weekends...)
function addTimeLine(){
var newRow = timeTableEl.insertRow(-1);
var startTimeCell = newRow.insertCell(-1);
var endTimeCell = newRow.insertCell(-1);
startTimeCell.innerHTML = "";
var inputStartTime = document.createElement("input");
inputStartTime.type = "time";
startTimeCell.appendChild(inputStartTime);
endTimeCell.innerHTML = "";
var inputEndTime = document.createElement("input");
inputEndTime.type = "time";
endTimeCell.appendChild(inputEndTime);
for (var i=0; i<7; i++){
var daysCell = newRow.insertCell(-1);
var checkDays = document.createElement("input");
checkDays.type = "checkbox"
daysCell.innerHTML = "";
daysCell.appendChild(checkDays);
}
var upIconCell = newRow.insertCell(-1);
upIconCell.innerHTML = "";
var upIcon = document.createElement("i");
upIcon.className = "fa fa-arrow-up";
upIcon.addEventListener("click", raiseLine, false);
//deleteIcon.aria-hidden="true";
upIconCell.appendChild(upIcon);
var downIconCell = newRow.insertCell(-1);
downIconCell.innerHTML = "";
var downIcon = document.createElement("i");
downIcon.className = "fa fa-arrow-down";
downIcon.addEventListener("click", lowerLine, false);
//deleteIcon.aria-hidden="true";
downIconCell.appendChild(downIcon);
var deleteIconCell = newRow.insertCell(-1);
deleteIconCell.innerHTML = "";
var deleteIcon = document.createElement("i");
deleteIcon.className = "fa fa-times";
deleteIcon.addEventListener("click", delTimeLine, false);
//deleteIcon.aria-hidden="true";
deleteIconCell.appendChild(deleteIcon);
}
function raiseLine(){
}
function lowerLine(){
}
function delTimeLine(e){
var cell = e.target.parentNode || window.event.srcElement;
cell.parentNode.remove();
}
function emptyTimetable() {
var rowCount = timeTableEl.rows.length;
for (var i = rowCount - 1; i > 0; i--) {
timeTableEl.deleteRow(i);
}
}
startCallbacks.work = function () {
document.body.className = 'work';
setInputDisabled(true);
}
startCallbacks.break = function () {
document.body.removeAttribute('class');
setInputDisabled(false);
}
startCallbacks.longbreak = function () {
document.body.removeAttribute('class');
setInputDisabled(false);
}
if (background.mainPomodoro.mostRecentMode == 'work') {
startCallbacks.work();
}
function TimeSlot (){
this.startTime = '00:00';
this.stopTime = '00:00';
this.daysEnabled = {};
}
volumeValue.innerHTML = volumeSliderEl.value * 100;
volumeSliderEl.oninput = function() {
volumeValue.innerHTML = this.value * 100;
}