-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptions.js
50 lines (42 loc) · 1.22 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
/* global Settings */
const $$ = (sel, ctx = document) => Array.from(ctx.querySelectorAll(sel) || []);
const $ = (sel, ctx) => $$(sel, ctx)[0];
let timerId;
function showMsg(msg) {
clearTimeout(timerId);
$("#msg").textContent = msg;
timerId = setTimeout(() => {
$("#msg").textContent = "";
}, 1000);
}
function saveOptions(e) {
e.preventDefault();
const colours = Object.keys(Settings.defaults.colours)
.map(id => [id, $(`#${id}`).value])
.reduce((o, [key, val]) => {
o[key] = val;
return o;
}, {});
Settings.save({
refreshTime: Math.max(3, parseInt($("#refresh").value, 10)),
colours
});
showMsg("Saved.");
}
function restoreOptions() {
Settings.fetch().then(res => {
$("#refresh").value = res.refreshTime || 60;
const colours = res.colours || Settings.defaults.colours || {};
Object.entries(colours)
.forEach(([key, val]) => $(`#${key}`).value = val);
});
}
function resetOptions(e) {
e.preventDefault();
Settings.save(Settings.defaults);
restoreOptions();
showMsg("Restored.");
}
document.addEventListener("DOMContentLoaded", restoreOptions);
$("form").addEventListener("submit", saveOptions);
$("#btnReset").addEventListener("click", resetOptions);