-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.js
233 lines (199 loc) · 5.26 KB
/
testing.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
class Duration {
constructor(milliseconds) {
this.milliseconds = milliseconds;
}
addMilliseconds(milliseconds) {
this.milliseconds = this.milliseconds + milliseconds;
}
addSeconds(seconds) {
this.milliseconds += seconds * 1000;
}
addMinutes(minutes) {
this.milliseconds += minutes * 60000;
}
static minutes(minute) {
return minute * 60000;
}
addHours(hours) {
this.milliseconds += hours * 3600000;
}
getHours() {
return Math.floor(this.milliseconds / 3600000);
}
getMinutes() {
return Math.floor(this.milliseconds / 60000);
}
getSeconds() {
return Math.floor(this.milliseconds / 1000);
}
getMilliseconds() {
return this.milliseconds % 1000;
}
toFormattedString() {
var hours, minutes, seconds, milliseconds;
hours = Math.floor(this.milliseconds / 3600000);
var ms = this.milliseconds%3600000;
minutes = Math.floor(ms / 60000);
ms = ms%60000;
seconds = Math.floor(ms / 1000);
return `${("0" + hours).slice(-2)}:${("0" + minutes).slice(-2)}:${(
"0" + seconds
).slice(-2)}`;
}
value() {
return {
hours: this.getHours(),
minutes: this.getMinutes(),
seconds: this.getSeconds(),
milliseconds: this.getMilliseconds(),
};
}
}
class Timer {
// setup the timer
constructor(duration, timeUpCallback) {
this.duration = duration;
this.topCallBack = timeUpCallback;
this.counting = false;
this._offset = new Duration(0);
}
start() {
this.startTime = new Date();
var currentObject = this;
this.mainTimeout = setTimeout(function () {
currentObject.stopTime = new Date();
currentObject.counting = false;
currentObject.startTime = null;
currentObject.topCallBack();
}, this.duration.milliseconds - this._offset.milliseconds);
this.counting = true;
}
pause() {
this.stopTime = new Date();
clearInterval(this.mainTimeout);
this._offset = new Duration(
this.stopTime.getTime() -
this.startTime.getTime() +
this._offset.milliseconds
);
this.counting = false;
}
stop() {
this.stopTime = null;
this._offset = new Duration(0);
this.counting = false;
clearInterval(this.mainTimeout);
}
getCount() {
if (this.counting) {
// this is counting so we start from now back to starting time + the offset
return new Duration(
new Date().getTime() -
this.startTime.getTime() +
this._offset.milliseconds
);
} else {
// this isn't counting so we use the stopped Time
return this._offset;
}
}
}
class Pomodoro{
static start(){
if(!Pomodoro.cycleindex){
Pomodoro.cycleindex = 0;
}
Pomodoro.counting = true
Pomodoro.Cycles[Pomodoro.cycleindex].timer.start();
}
static Cycles = [
{
timer: new Timer(new Duration(Duration.minutes(25)),Pomodoro.done),
type:"Work"
},
{
timer: new Timer(new Duration(Duration.minutes(5)),Pomodoro.done),
type:"Break"
},
{
timer: new Timer(new Duration(Duration.minutes(25)),Pomodoro.done),
type:"Work"
},
{
timer: new Timer(new Duration(Duration.minutes(5)),Pomodoro.done),
type:"Break"
},
{
timer: new Timer(new Duration(Duration.minutes(25)),Pomodoro.done),
type:"Work"
},
{
timer: new Timer(new Duration(Duration.minutes(5)),Pomodoro.done),
type:"Break"
},
{
timer: new Timer(new Duration(Duration.minutes(25)),Pomodoro.done),
type:"Work"
},
{
timer: new Timer(new Duration(Duration.minutes(20)),Pomodoro.done),
type:"Break"
},
];
static done(){
let nextIndex = Pomodoro.cycleindex >= Pomodoro.Cycles.length ? 0:Pomodoro.cycleindex+=1;
let current = Pomodoro.Cycles[Pomodoro.cycleindex];
current.timer.stop();
Pomodoro.cycleindex = nextIndex;
Pomodoro.start()
}
static pause(){
Pomodoro.Cycles[Pomodoro.cycleindex].timer.pause();
Pomodoro.counting = false;
}
static reset(){
let current = Pomodoro.Cycles[Pomodoro.cycleindex];
current.timer.stop();
Pomodoro.cycleindex = 0;
}
static getCurrent(){
return Pomodoro.Cycles[Pomodoro.cycleindex];
}
}
const timerElement = document.getElementById('timer');
const mainButton = document.getElementById('main-button');
const resetButton = document.getElementById('reset-button');
const cycleIndicator = document.getElementById('cycle-inidcator');
const remainingIndicator = document.getElementById('remaining-time');
const typeIndicator = document.getElementById('type-indicator');
function setButtonToStop() {
mainButton.classList.remove('btn-primary')
mainButton.classList.add('btn-danger')
mainButton.innerText = "Stop timer";
}
function setButtonToStart() {
mainButton.classList.add('btn-primary')
mainButton.classList.remove('btn-danger')
mainButton.innerText = "Start timer";
}
mainButton.addEventListener("click",function () {
if(Pomodoro.counting){
Pomodoro.pause();
setButtonToStart();
}else{
setButtonToStop();
Pomodoro.start();
cycleIndicator.innerText = "Cycle: "+(Pomodoro.cycleindex+1)
typeIndicator.innerText = Pomodoro.getCurrent().type
setInterval(function () {
let timer = Pomodoro.getCurrent().timer;
timerElement.innerText = timer.getCount().toFormattedString();
remainingIndicator.innerHTML = "Remaining <br>"+new Duration(timer.duration.milliseconds - timer.getCount().milliseconds).toFormattedString()
},1000);
}
});
resetButton.addEventListener("click",function () {
Pomodoro.reset();
cycleIndicator.innerText = "";
typeIndicator.innerText = "";
})