-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
421 lines (356 loc) · 14 KB
/
script.js
File metadata and controls
421 lines (356 loc) · 14 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
class WodTimer {
constructor() {
this.audio = new AudioManager();
// DOM Elements
this.display = document.querySelector('.timer-display');
this.statusLabel = document.querySelector('.status-label');
this.roundInfo = document.querySelector('.round-info');
this.startBtn = document.getElementById('btn-start');
this.resetBtn = document.getElementById('btn-reset');
this.settingsPanel = document.querySelector('.settings-panel');
// State
this.mode = 'clock';
this.state = 'idle';
this.interval = null;
this.startTime = null;
this.endTime = null;
this.remainingTime = 0;
this.elapsedTime = 0; // For stopwatch accumulation
// Multi-State Persistence
this.savedStates = {}; // Stores { mode: { state, remaining, elapsed, round, paused } }
// Settings defaults
this.settings = {
timerTime: 5,
amrapTime: 10,
emomInterval: 60,
emomRounds: 10,
tabataWork: 20,
tabataRest: 10,
tabataRounds: 8
};
// Runtime variables
this.currentRound = 1;
this.prepareTime = 10;
this.isPaused = false;
this.bindEvents();
this.updateClock();
setInterval(() => {
if (this.mode === 'clock' && this.state === 'idle') this.updateClock();
}, 1000);
}
bindEvents() {
this.startBtn.addEventListener('click', () => this.toggleStart());
this.resetBtn.addEventListener('click', () => this.reset());
document.querySelectorAll('.mode-btn').forEach(btn => {
btn.addEventListener('click', (e) => this.switchMode(e.target.dataset.mode));
});
document.getElementById('input-timer-time')?.addEventListener('change', (e) => this.settings.timerTime = parseInt(e.target.value));
document.getElementById('input-amrap-time')?.addEventListener('change', (e) => this.settings.amrapTime = parseInt(e.target.value));
document.getElementById('input-emom-interval')?.addEventListener('change', (e) => this.settings.emomInterval = parseInt(e.target.value));
document.getElementById('input-emom-rounds')?.addEventListener('change', (e) => this.settings.emomRounds = parseInt(e.target.value));
}
switchMode(newMode) {
if (this.mode === newMode) return;
// 1. Save current state (if not clock)
if (this.mode !== 'clock') {
this.saveCurrentState();
}
this.mode = newMode;
// Update UI Tabs
document.querySelectorAll('.mode-btn').forEach(btn => btn.classList.remove('active'));
document.querySelector(`[data-mode="${newMode}"]`).classList.add('active');
// 2. Try to restore state
if (this.savedStates[newMode]) {
this.restoreState(newMode);
} else {
// No saved state, reset to fresh
this.reset();
}
this.updateSettingsVisibility();
}
saveCurrentState() {
// If it's running, pause it first
if (this.state !== 'idle' && !this.isPaused) {
this.pause();
}
// Store vital stats
this.savedStates[this.mode] = {
state: this.state,
remainingTime: this.remainingTime,
elapsedTime: this.elapsedTime, // For stopwatch
currentRound: this.currentRound,
isPaused: true // Always saved as paused
};
}
restoreState(mode) {
const saved = this.savedStates[mode];
if (!saved || saved.state === 'idle') {
this.reset();
return;
}
clearInterval(this.interval); // Safety
this.state = saved.state;
this.remainingTime = saved.remainingTime;
this.elapsedTime = saved.elapsedTime || 0;
this.currentRound = saved.currentRound;
this.isPaused = true; // Always restore as paused
// UI Update
this.startBtn.textContent = 'RESUME';
this.startBtn.classList.remove('running');
this.updateUIState(this.state);
// Display Time Update
if (this.mode === 'fortime') {
this.display.textContent = this.formatTime(Math.floor(this.elapsedTime));
} else {
this.display.textContent = this.formatTime(Math.ceil(this.remainingTime));
}
// Visual warning check
if (this.remainingTime <= 5 && this.remainingTime > 0 && this.mode !== 'fortime') {
document.body.classList.add('state-warning');
} else {
document.body.classList.remove('state-warning');
}
}
updateSettingsVisibility() {
const inputs = document.querySelectorAll('.input-group');
inputs.forEach(el => el.style.display = 'none');
this.settingsPanel.classList.remove('active');
// Only show settings if in IDLE state (fresh start)
// If resumed (PAUSED/WORK), hide settings to avoid confusion
if (this.state !== 'idle') return;
if (this.mode === 'timer') {
document.querySelector('#group-timer-time').style.display = 'flex';
this.settingsPanel.classList.add('active');
this.display.textContent = this.formatTime(this.settings.timerTime * 60);
} else if (this.mode === 'amrap') {
document.querySelector('#group-amrap-time').style.display = 'flex';
this.settingsPanel.classList.add('active');
this.display.textContent = this.formatTime(this.settings.amrapTime * 60);
} else if (this.mode === 'emom') {
document.querySelector('#group-emom-interval').style.display = 'flex';
document.querySelector('#group-emom-rounds').style.display = 'flex';
this.settingsPanel.classList.add('active');
this.display.textContent = this.formatTime(this.settings.emomInterval);
} else if (this.mode === 'tabata') {
this.display.textContent = "20 / 10";
} else if (this.mode === 'fortime') {
this.display.textContent = "00:00";
} else if (this.mode === 'clock') {
this.updateClock();
}
}
toggleStart() {
if (this.mode === 'clock') return;
this.audio.init();
if (this.state === 'idle' || this.state === 'finished') {
this.startWorkout();
} else {
if (this.isPaused) {
this.resume();
} else {
this.pause();
}
}
}
startWorkout() {
this.resetRuntime();
this.state = 'prepare';
this.remainingTime = this.prepareTime;
this.elapsedTime = 0; // Reset stopwatch
this.updateUIState('prepare');
this.startLoop();
}
pause() {
this.isPaused = true;
clearInterval(this.interval);
this.startBtn.textContent = 'RESUME';
this.startBtn.classList.remove('running');
}
resume() {
this.isPaused = false;
this.startBtn.textContent = 'PAUSE';
this.startBtn.classList.add('running');
this.lastFrameTime = performance.now();
if (this.mode === 'fortime') {
this.startTime = performance.now(); // Reset start reference
this.runForTimeLoop();
} else {
this.loop();
this.interval = setInterval(() => this.loop(), 100);
}
}
startLoop() {
this.isPaused = false;
this.startBtn.textContent = 'PAUSE';
this.startBtn.classList.add('running');
this.lastFrameTime = performance.now();
this.interval = setInterval(() => this.loop(), 100);
}
loop() {
const now = performance.now();
const delta = (now - this.lastFrameTime) / 1000;
this.lastFrameTime = now;
if (this.remainingTime > 0) {
this.checkAudioCues(this.remainingTime, this.remainingTime - delta);
this.remainingTime -= delta;
if (this.remainingTime < 0) this.remainingTime = 0;
// Visual Warning for last 5 seconds
if (this.remainingTime <= 5 && this.remainingTime > 0) {
document.body.classList.add('state-warning');
} else {
document.body.classList.remove('state-warning');
}
this.display.textContent = this.formatTime(Math.ceil(this.remainingTime));
} else {
this.handlePhaseTransition();
}
}
checkAudioCues(prevTime, newTime) {
const currentSec = Math.ceil(newTime);
const prevSec = Math.ceil(prevTime);
if (currentSec !== prevSec) {
if (currentSec <= 5 && currentSec > 0) {
this.audio.playCountdown();
}
}
}
handlePhaseTransition() {
if (this.state === 'prepare') {
this.audio.playGo();
if (this.mode === 'fortime') {
this.state = 'work';
this.startTime = performance.now();
this.elapsedTime = 0;
this.runForTimeLoop();
return;
}
this.startNextRound();
}
else if (this.state === 'work') {
if (this.mode === 'tabata') {
if (this.currentRound >= this.settings.tabataRounds) {
this.finish();
} else {
this.state = 'rest';
this.remainingTime = this.settings.tabataRest;
this.audio.playRest();
this.updateUIState('rest');
}
}
else if (this.mode === 'emom') {
if (this.currentRound >= this.settings.emomRounds) {
this.finish();
} else {
this.currentRound++;
this.remainingTime = this.settings.emomInterval;
this.audio.playGo();
this.updateUIState('work');
}
}
else if (this.mode === 'amrap' || this.mode === 'timer') {
this.finish();
}
}
else if (this.state === 'rest') {
if (this.mode === 'tabata') {
this.currentRound++;
this.state = 'work';
this.remainingTime = this.settings.tabataWork;
this.audio.playGo();
this.updateUIState('work');
}
}
}
runForTimeLoop() {
clearInterval(this.interval);
this.updateUIState('work');
this.interval = setInterval(() => {
if (this.isPaused) return;
const now = performance.now();
const currentDelta = (now - this.startTime) / 1000;
const totalElapsed = this.elapsedTime + currentDelta;
// Note: We don't update this.elapsedTime continuously, only on pause.
// But to save state correctly on "switchMode" without pausing explicitly in UI,
// saveCurrentState() calls pause(), which updates elapsedTime.
this.display.textContent = this.formatTime(Math.floor(totalElapsed));
}, 100);
}
// Override pause to capture elapsed time for stopwatch
pause() {
this.isPaused = true;
clearInterval(this.interval);
this.startBtn.textContent = 'RESUME';
this.startBtn.classList.remove('running');
if (this.mode === 'fortime' && this.state === 'work') {
const now = performance.now();
this.elapsedTime += (now - this.startTime) / 1000;
}
}
startNextRound() {
this.state = 'work';
this.updateUIState('work');
if (this.mode === 'timer') {
this.remainingTime = this.settings.timerTime * 60;
} else if (this.mode === 'amrap') {
this.remainingTime = this.settings.amrapTime * 60;
} else if (this.mode === 'emom') {
this.remainingTime = this.settings.emomInterval;
} else if (this.mode === 'tabata') {
this.remainingTime = this.settings.tabataWork;
}
}
finish() {
this.state = 'finished';
clearInterval(this.interval);
this.display.textContent = 'DONE';
this.audio.playComplete();
this.startBtn.textContent = 'START';
this.startBtn.classList.remove('running');
this.statusLabel.textContent = 'FINISHED';
this.statusLabel.className = 'status-label visible';
document.body.className = 'state-finished';
// Clear saved state for this mode on finish
delete this.savedStates[this.mode];
}
reset() {
clearInterval(this.interval);
this.state = 'idle';
this.isPaused = false;
this.currentRound = 1;
this.elapsedTime = 0;
this.remainingTime = 0;
this.startBtn.textContent = 'START';
this.startBtn.classList.remove('running');
this.updateSettingsVisibility();
this.roundInfo.textContent = '';
this.statusLabel.classList.remove('visible');
document.body.className = '';
document.body.classList.remove('state-warning');
delete this.savedStates[this.mode]; // Clear memory
}
resetRuntime() {
this.currentRound = 1;
this.elapsedTime = 0;
}
updateUIState(phase) {
document.body.className = `state-${phase}`;
this.statusLabel.textContent = phase.toUpperCase();
this.statusLabel.classList.add('visible');
if (this.mode === 'emom' || this.mode === 'tabata') {
const total = this.mode === 'tabata' ? this.settings.tabataRounds : this.settings.emomRounds;
this.roundInfo.textContent = `ROUND ${this.currentRound} / ${total}`;
} else {
this.roundInfo.textContent = '';
}
}
formatTime(seconds) {
const m = Math.floor(seconds / 60);
const s = Math.floor(seconds % 60);
return `${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}`;
}
updateClock() {
const now = new Date();
this.display.textContent = now.toLocaleTimeString([], { hour12: false });
}
}
const timer = new WodTimer();