-
Notifications
You must be signed in to change notification settings - Fork 1
/
save-and-restore-timers.js
61 lines (50 loc) · 1.78 KB
/
save-and-restore-timers.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
const saveTimers = () => {
const timers = [];
const timerSections = document.getElementById("timer-sections").children;
for (const timerSection of timerSections) {
const timer = {
leftTimer: {},
rightTimer: {},
};
const timerElems = [...timerSection.querySelectorAll(".timer")];
if (timerSection.classList.contains("normal-timer")) {
timer.type = "normal";
const timerNames = timerElems.map((timerElem) => timerElem
.previousElementSibling.value);
[timer.leftTimer.name, timer.rightTimer.name] = timerNames;
} else if (timerSection.classList.contains("chess-timer")) {
timer.type = "chess";
timer.name = timerSection.getElementsByClassName("timer-name")[0].value;
}
// convert duration to string because JSON.stringify can't serialize BigInt
// remember to convert back to BigInt during deserialization
const timerDurations = timerElems.map((timerElem) =>
String(getCurrentTimerDurationSeconds(timerElem)));
[timer.leftTimer.durationStr, timer.rightTimer.durationStr] = timerDurations;
timers.push(timer);
}
localStorage.setItem("timerConfiguration", JSON.stringify(timers));
};
const restoreTimers = () => {
let timers;
try {
timers = JSON.parse(localStorage.getItem("timerConfiguration"));
} catch (err) {
return;
}
if (timers === null || !Array.isArray(timers) || timers.length === 0) {
return;
}
for (const existingTimerSection of [...document.getElementById("timer-sections")
.children]) {
existingTimerSection.remove();
}
// TODO: set length of input to accommodate string
for (const timer of timers) {
if (timer.type === "normal") {
addNormalTimer(timer);
} else if (timer.type === "chess") {
addChessClockTimer(timer);
}
}
};