-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
180 lines (136 loc) · 5.23 KB
/
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
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
import PubSub from './pubsub.js';
class Util {
static arrToClassList = arr => arr.map(v => v.trim()).join(' ');
static pad0 = num => {
return num < 10 ? '0' + num : num;
}
static secsToClock = (time) => {
let min = Math.floor(time/60);
let secs = (time%60).toFixed();
return Util.pad0(min) + ':' + Util.pad0(secs);
}
static updateTextNode = (selector, value) => {
document.querySelector(selector).innerText = value;
}
}
class Timer {
constructor(time){
this.time = time;
this.runningTimer = null;
}
toString = () => {
return 'To be implemented';
}
start = () => {
PubSub.publish('timerStarted');
if (this.runningTimer){
return this.runningTimer;
}
let localTime = this.time;
this.runningTimer = setInterval(function intervalLogic(){
--localTime;
PubSub.publish('timeUpdated', localTime);
if (!!localTime)
this.stop();
}, 1000);
return this.runningTimer;
}
stop = () => {
PubSub.publish('timerStopped');
if (!this.runningTimer){
return this.runningTimer;
}
clearInterval(this.runningTimer);
this.runningTimer = null;
return this.runningTimer;
}
}
class TimerUI {
constructor(node, time){
this.node = node;
this.time = time || 300;
this.createView();
this.timer = new Timer(time);
this.subscribed = null;
PubSub.subscribe('timerStarted', this._updateLabels);
PubSub.subscribe('timerStopped', () => { this._updateLabels(true) });
}
_createHeaderNode = () => {
let header = document.createElement('div');
header.classList = Util.arrToClassList(['timer-header']);
let title = document.createElement('span');
title.innerText = 'Egghead Timer';
title.classList = Util.arrToClassList(['title']);
let subTitle = document.createElement('span');
subTitle.innerText = 'Cook eggs easily';
subTitle.classList = Util.arrToClassList(['subtitle']);
//appending elements to the dom
header.insertAdjacentElement("afterbegin", title);
header.insertAdjacentElement("beforeend", subTitle);
return header;
}
_createContentNode = () => {
let content = document.createElement('div');
content.classList = Util.arrToClassList(['timer-content']);
let clockSpan = document.createElement('span');
clockSpan.classList = Util.arrToClassList(['clock-text']);
clockSpan.insertAdjacentText("afterbegin", Util.secsToClock(this.time));
content.insertAdjacentElement('afterbegin', clockSpan);
return content;
}
_createFooterNode = () => {
let footer = document.createElement('div');
footer.classList = Util.arrToClassList(['timer-footer']);
let startBtn = document.createElement('button');
startBtn.setAttribute('id', 'start');
startBtn.classList = Util.arrToClassList(['start']);
startBtn.insertAdjacentText('afterbegin', 'Start');
let resetBtn = document.createElement('button');
resetBtn.setAttribute('id', 'reset');
resetBtn.classList = Util.arrToClassList(['reset']);
resetBtn.insertAdjacentText('afterbegin', 'Reset');
footer.insertAdjacentElement('afterbegin', startBtn);
footer.insertAdjacentElement('beforeend', resetBtn);
return footer;
}
createView(){
let fragment = document.createDocumentFragment();
let wrapper = document.createElement('section');
wrapper.classList = Util.arrToClassList(['timer-wrapper']);
wrapper.insertAdjacentElement('afterbegin', this._createHeaderNode());
wrapper.insertAdjacentElement('beforeend', this._createContentNode());
wrapper.insertAdjacentElement('beforeend', this._createFooterNode());
fragment.appendChild(wrapper);
while (this.node.firstChild) this.node.removeChild(this.node.firstChild);
this.node.appendChild(fragment);
}
_updateLabels(toReset) {
let startBtn = document.getElementById('start');
let resetBtn = document.getElementById('reset');
if (toReset) {
if (startBtn.textContent === ('Pause' || 'Stop')) {
startBtn.innerText = 'Start';
}
if (resetBtn.textContent === 'Stop') {
resetBtn.innerText = 'Reset';
}
// Util.updateTextNode('span.clock-text', Util.secsToClock(this.time));
return;
}
startBtn.innerText = 'Pause';
resetBtn.innerText = 'Stop';
}
start = () => {
this.timer.start();
this.subscribed = PubSub.subscribe('timeUpdated', (data) => {
Util.updateTextNode('span.clock-text', Util.secsToClock(data));
});
}
stop = () => {
this.subscribed.unsubscribe();
this.timer.stop();
}
}
let timerUI = new TimerUI(document.getElementById('app'),330);
document.getElementById('start').addEventListener('click', timerUI.start);
document.getElementById('reset').addEventListener('click', timerUI.stop);