-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
184 lines (148 loc) · 6.33 KB
/
script.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
class TimerView extends HTMLElement {
connectedCallback() {
this._shadow = this.attachShadow({mode: 'closed'});
const slot = document.createElement('slot');
slot.setAttribute('name','timer');
this._shadow.prepend(slot);
this.initSeconds = 4221;
this.initToTime;
const timer = document.createElement('div');
timer.className = 'timer-body';
timer.setAttribute('seconds', this.initSeconds? this.initSeconds : '');
timer.setAttribute('to-time', this.initToTime? this.initToTime : '');
timer.setAttribute('slot', 'timer');
this.hours = 0;
this.minutes = 0;
this.seconds = 0;
this.interval;
this.initSeconds? this.getTimeBySeconds(this.initSeconds) : this.getTimeToTime(this.initToTime);
this.observeAttrChanges();
timer.innerHTML = `
<div class="timer-body-hours" style="display: ${+this.hours > 0? 'block' : 'none'}">
${this.hours < 10? '0' + this.hours : this.hours}:
</div>
<div class="timer-body-min">${this.minutes < 10? '0' + this.minutes : this.minutes}:</div>
<div class="timer-body-sec">${this.seconds < 10? '0' + this.seconds : this.seconds}</div>
`
this.append(timer);
const timerControls = document.createElement('div');
timerControls.className = 'timer-buttons';
timerControls.setAttribute('slot', 'timer');
timerControls.innerHTML = `
<button id="play" type="button"><img src="./images/play.svg" alt="play"></button>
<button id="pause" type="button"><img src="./images/pause.svg" alt="pause"></button>
<button id="reset" type="button"><img src="./images/reset.svg" alt="reset"></button>
`
this.append(timerControls);
let controlButtons = document.getElementsByTagName('button');
Array.from(controlButtons).forEach(button => {
button.addEventListener('click', () => {
if(button.id === 'play') {
this.startTimer();
} else if(button.id === 'pause') {
this.pauseTimer();
} else if(button.id === 'reset') {
this.resetTimer();
}
})
});
}
getTimeBySeconds(sec) {
this.seconds = sec % 60;
this.minutes = Math.floor(sec / 60 % 60);
this.hours = Math.floor(sec / 60 / 60 % 60);
}
getTimeToTime(toTime) {
if(!toTime) return;
let initialToTime = toTime.split(':');
let currentTime = new Date().getTime();
let settedTime = new Date().setHours(initialToTime[0], initialToTime[1], initialToTime[2], 0);
let timeDif = new Date(settedTime - currentTime).toISOString().slice(11, -5);
let initTimeDif = timeDif.split(':');
this.seconds = +initTimeDif[2];
this.minutes = +initTimeDif[1];
this.hours = +initTimeDif[0];
}
updateHTML(updatedPart) {
let hours = document.querySelector('.timer-body-hours');
let min = document.querySelector('.timer-body-min');
let sec = document.querySelector('.timer-body-sec');
if(updatedPart === 'seconds') {
sec.innerHTML = this.seconds < 10? '0' + this.seconds : this.seconds;
} else if(updatedPart === 'minutes') {
sec.innerHTML = this.seconds < 10? '0' + this.seconds : this.seconds;
min.innerHTML = this.minutes < 10? '0' + this.minutes + ':' : this.minutes + ':';
} else if(updatedPart === 'fullTime') {
hours.innerHTML = this.hours < 10? '0' + this.hours + ':' : this.hours + ':';
min.innerHTML = this.minutes < 10? '0' + this.minutes + ':' : this.minutes + ':';
sec.innerHTML = this.seconds < 10? '0' + this.seconds : this.seconds;
}
this.hours < 0? hours.style.display = "none" : hours.style.display = "block";
}
observeAttrChanges() {
let target = document.getElementsByTagName('timer-view')[0];
const config = {
attributes: true,
childList: true,
subtree: true,
};
const reset = (mutationsList, observer) => {
for (let mutation of mutationsList) {
if (mutation.type === "attributes") {
Array.from(mutation.target.attributes).forEach(attr => {
if(attr.name === 'seconds') {
this.initSeconds = attr.value;
} else if(attr.name === 'to-time') {
this.initToTime = attr.value;
}
})
this.resetTimer();
}
}
}
const observer = new MutationObserver(reset);
observer.observe(target, config);
}
startTimer() {
this.interval = setInterval(() => {
if(this.initSeconds) {
if(this.seconds > 0) {
--this.seconds;
this.updateHTML('seconds');
} else if(this.seconds === 0 && this.minutes !== 0) {
this.seconds = 59;
--this.minutes;
this.updateHTML('minutes');
} else if(this.minutes === 0 && this.hours !== 0) {
this.seconds = 59;
this.minutes = 59;
--this.hours;
this.updateHTML('fullTime');
}
} else {
this.getTimeToTime(this.initToTime);
this.updateHTML('fullTime');
}
if(this.hours === 0 && this.minutes === 0 && this.seconds === 0) {
this.stopTimer();
}
}, 1000);
}
pauseTimer() {
this.interval = clearInterval(this.interval);
}
stopTimer() {
clearInterval(this.interval);
console.log('end of countdown');
this.hours = 0;
this.minutes = 0;
this.seconds = 0;
}
resetTimer() {
clearInterval(this.interval);
this.getTimeBySeconds(this.initSeconds);
this.getTimeToTime(this.initToTime);
this.updateHTML('fullTime');
}
}
customElements.define('timer-view', TimerView);