-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
263 lines (211 loc) · 7.24 KB
/
Copy pathindex.js
File metadata and controls
263 lines (211 loc) · 7.24 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// I refer views as scene got catched to Java programming still
// scene with the big play button
const startScene = document.querySelector(".start-scene");
// scene with all the other controls and the main timer
const mainScene = document.querySelector(".main-scene");
// scene where we set the time for each session default is 25
const settingsScene = document.querySelector(".settings-scene");
const playBtn = document.querySelector(".play");
// edit button for each scene. easier for layout.
const settingsBtnStart = document.querySelector(".start-scene .edit");
const settingsBtnSettings = document.querySelector(".settings-scene .edit");
const settingsBtnMain = document.querySelector(".main-scene .edit");
const stopBtn = document.querySelector(".stop");
const pauseBtn = document.querySelector(".pause");
const resetBtn = document.querySelector(".reset");
const adjustBtn = document.querySelector(".adjust");
const minusBtns = document.querySelectorAll(".minus");
const plusBtns = document.querySelectorAll(".plus");
// switch variable to monitor if paused button is pressed before any other process
let switchPause = false;
let currentSession = "";
let sessionNumber = 0;
const SESSION = "session";
const BREAK = "break";
const REST = "rest";
settingsBtnStart.addEventListener("click", showSettings);
settingsBtnSettings.addEventListener("click", closeSettings);
settingsBtnMain.addEventListener("click", showSettings);
// Starting the timer
playBtn.addEventListener("click", () => {
startTimer();
resetAll();
showMain();
});
// stop the timer
stopBtn.addEventListener("click", () => {
clearInterval(decrement);
showStart();
resetAll();
});
// pause the timer and toggle pause button
pauseBtn.addEventListener("click", (e) => {
const eClassList = e.target.classList;
updatePauseBtn(eClassList);
});
// resets the timer back 0 remembering current session
resetBtn.addEventListener("click", () => {
initTimerParam(currentSession);
timer.textContent = secToTimeStr(seconds);
});
// button in settings. restarts everthing to main screen
adjustBtn.addEventListener("click", () => {
clearInterval(decrement);
showStart();
resetAll();
});
// button for minus buttons in settings
minusBtns.forEach((btn) => {
btn.addEventListener("click", () => {
settingChangeValue(btn.nextElementSibling, "minus");
})
});
// button for plus buttons in settings
plusBtns.forEach((btn) => {
btn.addEventListener("click", () => {
settingChangeValue(btn.previousElementSibling, "plus");
})
});
// resets all states and switch monitoring variables
function resetAll(){
sessionNumber = 0;
initTimerParam(SESSION);
updateSessionText();
// for the pause/play button
const eClassList = pauseBtn.classList;
eClassList.add("fa-pause");
eClassList.remove("fa-play");
switchPause = false; // pause
}
// updates the pause button's icon and at the same time manage the timer
function updatePauseBtn() {
const eClassList = pauseBtn.classList;
eClassList.toggle("fa-pause");
eClassList.toggle("fa-play");
if (eClassList.contains("fa-pause")) {
switchPause = true; // play
startTimer();
// console.log("Playing from pause");
} else if (eClassList.contains("fa-play")) {
switchPause = false; // pause
clearInterval(decrement);
// console.log("Pause from playing");
}
}
function showStart() {
mainScene.classList.remove("fade");
startScene.classList.add("fade");
settingsScene.classList.remove("fade");
}
function showSettings() {
settingsScene.classList.add("fade");
}
function closeSettings() {
settingsScene.classList.remove("fade");
}
function showMain() {
mainScene.classList.add("fade");
startScene.classList.remove("fade");
settingsScene.classList.remove("fade");
}
// updates the value of parameters in settings, depends on what is passed
function settingChangeValue(valueTarget, method) {
let currentValue = Number(valueTarget.textContent);
if (currentValue < 25) {
if (method === "plus") {
currentValue++;
valueTarget.textContent = currentValue;
}
}
if (currentValue > 1) {
if (method === "minus") {
currentValue--;
valueTarget.textContent = currentValue.toString();
}
}
}
// End of UI codes
const timer = document.querySelector(".timer");
const sessionValue = document.querySelector(".session-value");
const breakValue = document.querySelector(".break-value");
const restValue = document.querySelector(".rest-value");
const sessionText = document.querySelector(".session");
const beepAudio = document.querySelector(".beep-audio");
// global params
let seconds = 0;
let decrement = true;
// for testing purposes. turning on code is found in initTimerParam function
function testMode() {
seconds = 3;
}
// returns seconds equivalent of a value in the settings. also sets the currentSession variable for monitoring
function initTimerParam(stringType) {
switch (stringType) {
case SESSION:
seconds = sessionValue.textContent * 60;
break;
case BREAK:
seconds = breakValue.textContent * 60;
break;
case REST:
seconds = restValue.textContent * 60;
break;
}
currentSession = stringType;
// un-comment only if testing
// testMode();
return seconds;
}
// converts seconds to string of time hh:mm:ss. puts 0 on seconds < 10
function secToTimeStr(sec) {
let hour = 0, minute = 0;
hour = Math.floor(sec / 3600);
sec = sec - (hour * 3600);
minutes = Math.floor(sec / 60);
sec = sec - (minutes * 60);
function format(n) {
return n < 10 ? "0" + n : n;
}
return hour > 0 ? `${format(hour)}:${format(minutes)}:${format(sec)}` :
`${format(minutes)}:${format(sec)}`;
}
// starts a job (setInterval()) to decrement seconds global variable and display formatted equivalent stops at 0
function startTimer() {
decrement = setInterval (function(){
if (seconds === -1){
clearInterval(decrement);
sessionMonitorUpdate();
}
timer.textContent = secToTimeStr(seconds--);
}, 1000);
}
// updates timer and other variables depending on current session and sessionNumber value. plays sound.
function sessionMonitorUpdate() {
// console.log(currentSession + " sesh:" + (sessionNumber));
if (currentSession === SESSION && sessionNumber < 3) {
initTimerParam(BREAK);
} else if (currentSession === BREAK && sessionNumber < 3) {
sessionNumber++;
initTimerParam(SESSION);
} else if (currentSession === SESSION && sessionNumber === 3) {
initTimerParam(REST);
} else if (currentSession === REST) {
clearInterval(decrement);
showStart();
resetAll();
}
updatePauseBtn();
updateSessionText();
beepAudio.play();
}
// updates session text
function updateSessionText() {
if (currentSession === REST) {
sessionText.textContent = "Congratulations! Please Rest :)";
return;
} else if (currentSession === BREAK) {
sessionText.textContent = `Break ${sessionNumber + 1}`;
return;
}
sessionText.textContent = `Session ${sessionNumber + 1}`;
}