-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathoptions-page.js
68 lines (63 loc) · 2.14 KB
/
options-page.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
document.addEventListener('DOMContentLoaded', () => {
const MOUSE_BEHAVIOR = 'mouse-behavior';
const CURSOR_MOVEMENT = 'cursor-movement';
const mouseBehaviorOptionEls =
document.querySelectorAll('input[name="mouse-behavior"]');
const cursorMovementOptionEls =
document.querySelectorAll('input[name="cursor-movement"]');
const statusEl = document.getElementById('status');
let clearStatusTimeout = null;
function save() {
const options = {};
for (const radioButton of mouseBehaviorOptionEls) {
if (radioButton.checked) {
options[MOUSE_BEHAVIOR] = radioButton.id;
break;
}
}
for (const radioButton of cursorMovementOptionEls) {
if (radioButton.checked) {
options[CURSOR_MOVEMENT] = radioButton.id;
break;
}
}
todoistShortcutsSaveOptions(options, () => {
statusEl.textContent = 'Changes saved.';
if (clearStatusTimeout) {
clearTimeout(clearStatusTimeout);
}
clearStatusTimeout = setTimeout(() => {
statusEl.textContent = '';
}, 1000);
});
}
function initialize(options) {
const mouseBehavior = options[MOUSE_BEHAVIOR];
let foundMouseBehaviorOption = false;
for (const radioButton of mouseBehaviorOptionEls) {
radioButton.addEventListener('click', save);
if (radioButton.id === mouseBehavior) {
radioButton.checked = true;
foundMouseBehaviorOption = true;
}
}
if (!foundMouseBehaviorOption) {
console.warn('No mouse behavior option matching ' + mouseBehavior);
}
const cursorMovement = options[CURSOR_MOVEMENT];
let foundCursorMovementOption = false;
for (const radioButton of cursorMovementOptionEls) {
radioButton.addEventListener('click', save);
if (radioButton.id === cursorMovement) {
radioButton.checked = true;
foundCursorMovementOption = true;
}
}
if (!foundCursorMovementOption) {
console.warn('No cursor movement option matching ' + cursorMovement);
}
}
todoistShortcutsLoadOptions(initialize, (e) => {
alert('Oops, error while loading saved options... Error is:\n' + e);
});
});