-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscript.js
More file actions
222 lines (201 loc) · 4.71 KB
/
script.js
File metadata and controls
222 lines (201 loc) · 4.71 KB
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
/*--------------------------------*/
/* Pomodoro Clock */
/*--------------------------------*/
/* by Stephen Bau */
/*--------------------------------*/
var display = document.getElementById("display");
var displayTime = document.getElementById("time-left");
var timerLabel = document.getElementById("timer-label");
var displaySession = document.getElementById("session-length");
var displayBreak = document.getElementById("break-length");
var startStopButton = document.getElementById("start_stop");
var resetButton = document.getElementById("reset");
var buttons = document.getElementsByClassName("button");
var alarm = document.getElementById("beep");
var mode = "session";
var sessionLength = 25;
var breakLength = 5;
var time = 0;
var status = 0;
var minutes = sessionLength;
var seconds = 0;
var display = updateDisplay();
var countdown = null;
init();
function init() {
for (var i = 0; i < buttons.length; i++) {
var button = buttons[i];
button.addEventListener("click", function( event ) {
getInput(this);
});
}
keyboard();
}
function inputType(button) {
var id = button.id;
type = id;
return type;
}
function getInput(button) {
switch (inputType(button)) {
case "start_stop":
startStop();
break;
case "reset":
reset();
break;
case "session-increment":
changeSession(1);
break;
case "session-decrement":
changeSession(-1);
break;
case "break-increment":
changeBreak(1);
break;
case "break-decrement":
changeBreak(-1);
}
}
function keyboard() {
keyboardEvents("keydown");
keyboardEvents("keyup");
}
function keyboardEvents(keyEvent) {
document.addEventListener(keyEvent, function (event) {
if (event.defaultPrevented) {
return;
}
var key = event.key || event.keyCode;
for (var i = 0; i < buttons.length; i++) {
var button = buttons[i];
if (button.dataset.key == key) {
handleKeyboardEvent(button, keyEvent);
}
}
});
}
function handleKeyboardEvent(button, keyEvent) {
if (keyEvent == "keydown") {
button.classList.add("select");
getInput(button);
}
if (keyEvent == "keyup") {
button.classList.remove("select");
}
}
function startStop() {
if (status == 1) {
timerSwitch(0);
} else {
timerSwitch(1);
}
}
function timerSwitch(on) {
if (minutes == 0 && seconds == 0) {
return;
}
if (on == 1) {
countdown = setInterval(timer, 1000);
status = 1;
startStopButton.innerText = "Stop";
startStopButton.classList.remove('start');
startStopButton.classList.add('stop');
console.log("Timer started");
} else {
clearInterval(countdown);
status = 0;
startStopButton.innerText = "Start";
startStopButton.classList.remove('stop');
startStopButton.classList.add('start');
console.log("Timer stopped");
}
}
function timer() {
if (minutes == 0 && seconds == 0) {
updateDisplay();
return zero();
}
if (minutes >= 0) {
if (seconds > 0) {
seconds -= 1;
updateDisplay()
} else {
minutes -= 1;
seconds = 59;
updateDisplay()
}
}
}
function zero() {
beep.play();
modeSwitch();
}
function modeSwitch() {
if (mode == "session") {
console.log("Session finished");
timerLabel.innerText = "Break";
minutes = breakLength;
updateDisplay();
mode = "break";
return;
} else {
console.log("Break finished");
timerLabel.innerText = "Session";
minutes = sessionLength;
updateDisplay();
mode = "session";
return;
}
}
function updateDisplay() {
display = minutes + ":" + formatSeconds(seconds);
displayTime.innerText = display;
console.log(display);
return display;
}
function formatSeconds(num) {
var str = num.toString();
if (str.length == 1) {
str = "0" + str;
}
return str;
}
function reset() {
if (status == 1) {
timerSwitch(0);
}
mode = "session";
timerLabel.innerText = "Session";
minutes = sessionLength;
seconds = 0;
updateDisplay();
}
function changeSession(value) {
if (sessionLength + value > 0 && sessionLength + value <= 60) {
sessionLength += value;
if (mode == "session") {
minutes = sessionLength;
updateDisplay();
}
displaySession.innerText = sessionLength;
}
return sessionLength;
}
function changeBreak(value) {
if (breakLength + value > 0 && breakLength + value <= 60) {
breakLength += value;
if (mode == "break") {
minutes = breakLength;
updateDisplay();
}
displayBreak.innerText = breakLength;
}
return breakLength;
}
function debugClock() {
console.log("mode: " + mode);
console.log("time: " + time);
console.log("display: " + display);
console.log("status: " + status);
}