-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
105 lines (91 loc) · 5.33 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
// @ts-check
document.addEventListener("DOMContentLoaded", function () {
let workSlot = getWorkSlot();
let tagSlot = getTagSlot();
/** @type {NodeListOf<HTMLInputElement>} */(document.querySelectorAll(`[name="work_slot"]`)).forEach($slot => $slot.addEventListener("click", async () => {
await saveSlotOptions(workSlot, tagSlot);
workSlot = getWorkSlot();
await restoreOptions();
}));
/** @type {NodeListOf<HTMLInputElement>} */(document.querySelectorAll(`[name="tag_slot"]`)).forEach($slot => $slot.addEventListener("click", async () => {
await saveSlotOptions(workSlot, tagSlot);
tagSlot = getTagSlot();
await restoreOptions();
}));
const $otherInputs = /** @type {NodeListOf<HTMLInputElement | HTMLTextAreaElement>} */(document.querySelectorAll(`input[type="checkbox"], input[type="color"], textarea`));
$otherInputs.forEach($input => $input.addEventListener("change", () => saveSlotOptions(workSlot, tagSlot)));
function selectTab() {
const tab = /** @type {HTMLInputElement} */(document.querySelector(`[name="tab"]:checked`)).value;
(/** @type {NodeListOf<HTMLDivElement>} */(document.querySelectorAll(".tab"))).forEach(
$tab => $tab.style.display = $tab.id === `tab_${tab}` ? "block" : "none"
);
}
const $tabs = /** @type {NodeListOf<HTMLInputElement>} */(document.querySelectorAll(`[name="tab"]`));
$tabs.forEach($tab => $tab.addEventListener("click", selectTab));
selectTab();
restoreOptions();
});
function getWorkSlot() {
return Number(/** @type {HTMLInputElement} */(document.querySelector(`[name="work_slot"]:checked`)).value);
}
function getTagSlot() {
return Number(/** @type {HTMLInputElement} */(document.querySelector(`[name="tag_slot"]:checked`)).value);
}
/**
*
* @param {number} workSlot
* @param {number} tagSlot
*/
async function saveSlotOptions(workSlot, tagSlot) {
const options = await loadOptions();
if (!options.works[workSlot]) {
options.works[workSlot] = /** @type {Work} */({modes: {}, makerNames: ""});
}
const work = options.works[workSlot];
const $work_modes = /** @type {NodeListOf<HTMLInputElement>} */(document.querySelectorAll(`[name="work_mode"]`));
work.modes = Array.from($work_modes).filter($mode => $mode.checked).reduce((modes, $mode) => ({...modes, [/** @type {Mode} */($mode.value.slice(5))]: true }), {});
const $makerNames = /** @type {HTMLTextAreaElement} */(document.querySelector(`#makerNames`));
work.makerNames = $makerNames.value;
colorProperties.forEach(prop => work[prop] = /** @type {HTMLInputElement} */(document.querySelector(`[name="work_${prop}"]`)).value);
if (!options.tags[tagSlot]) {
options.tags[tagSlot] = /** @type {Tag} */({modes: {}, tags: ""});
}
const tag = options.tags[tagSlot];
const $tag_modes = /** @type {NodeListOf<HTMLInputElement>} */(document.querySelectorAll(`[name="tag_mode"]`));
tag.modes = Array.from($tag_modes).filter($mode => $mode.checked).reduce((modes, $mode) => ({...modes, [/** @type {TagMode} */($mode.value.slice(4))]: true}), {});
const $tags = /** @type {HTMLTextAreaElement} */(document.querySelector(`#tags`));
tag.tags = $tags.value;
tagColorProperties.forEach(prop => tag[prop] = /** @type {HTMLInputElement} */(document.querySelector(`[name="tag_${prop}"]`)).value);
/** @type {RawOptions} */
const rawOptions = {
options: JSON.stringify(options),
mode: undefined,
ignoreMakerNames: undefined,
};
return new Promise((resolve) => {
chrome.storage.local.set(rawOptions, () => {
document.querySelector("#notification").textContent += "保存しました ";
setTimeout(() => {
document.querySelector("#notification").textContent = document.querySelector("#notification").textContent.slice(7);
}, 1500);
resolve();
});
})
}
async function restoreOptions() {
const options = await loadOptions();
const workSlot = Number(/** @type {HTMLInputElement} */(document.querySelector(`[name="work_slot"]:checked`)).value);
const work = options.works[workSlot] || {modes: {}, makerNames: ""};
const $work_modes = /** @type {NodeListOf<HTMLInputElement>} */(document.querySelectorAll(`[name="work_mode"]`));
$work_modes.forEach($mode => $mode.checked = work.modes[/** @type {Mode} */($mode.value.slice(5))]);
const $makerNames = /** @type {HTMLTextAreaElement} */(document.querySelector(`#makerNames`));
$makerNames.value = work.makerNames;
colorProperties.forEach(prop => /** @type {HTMLInputElement} */(document.querySelector(`[name="work_${prop}"]`)).value = work[prop] || "#bbbbbb");
const tagSlot = Number(/** @type {HTMLInputElement} */(document.querySelector(`[name="tag_slot"]:checked`)).value);
const tag = options.tags[tagSlot] || {modes: {}, tags: ""};
const $tag_modes = /** @type {NodeListOf<HTMLInputElement>} */(document.querySelectorAll(`[name="tag_mode"]`));
$tag_modes.forEach($mode => $mode.checked = tag.modes[/** @type {TagMode} */($mode.value.slice(4))]);
const $tags = /** @type {HTMLTextAreaElement} */(document.querySelector(`#tags`));
$tags.value = tag.tags;
tagColorProperties.forEach(prop => /** @type {HTMLInputElement} */(document.querySelector(`[name="tag_${prop}"]`)).value = tag[prop] || "#bbbbbb");
}