-
Notifications
You must be signed in to change notification settings - Fork 1
/
main-app.js
91 lines (79 loc) · 2.03 KB
/
main-app.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
var mainApp = new Vue({
el: '#main-app',
data: {
initialTime: 60,
currentTimer: undefined,
mainTimerInitialTime: (15 * 60),
mainTimer: {},
timers: []
},
mounted: function () {
var localTimers = JSON.parse(localStorage.getItem("timers"));
if (localTimers) {
localTimers = localTimers.filter(x => x.name?.trim())
localTimers.forEach(x => x.time = this.initialTime)
}
if (localTimers?.length > 0) {
this.timers = localTimers;
} else {
this.timers = Array.from({length: 3}, () => this.emptyTimer())
}
this.mainTimer = {
time: this.mainTimerInitialTime,
active: false
}
this.startTimer();
},
watch: {
timers: {
handler: function (val, oldVal) {
this.updateLocalStorage();
},
deep: true
},
},
methods: {
emptyTimer: function () {
return {
name: "",
time: this.initialTime
};
},
addTimer: function () {
this.timers.push(this.emptyTimer());
},
updateLocalStorage: function () {
localStorage.setItem("timers", JSON.stringify(this.timers));
},
toggleTimer: function (timer) {
this.mainTimer.active = true;
if(this.currentTimer && this.currentTimer === timer) {
this.currentTimer = undefined;
} else {
this.currentTimer = timer;
}
},
startTimer: async function () {
while(true) {
if(this.currentTimer) {
this.currentTimer.time--;
}
if(this.mainTimer.active) {
this.mainTimer.time--;
}
await new Promise(resolve => setTimeout(resolve, 1000));
}
}
},
filters: {
formatTime: function (value) {
var signal = value < 0 ? '-' : '';
value = Math.abs(value);
var minutes = Math.floor(value / 60);
var seconds = value % 60;
minutes = ("" + minutes).padStart(2, '0');
seconds = ("" + seconds).padStart(2, '0');
return signal + minutes + ":" + seconds;
}
}
});