-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.index.html
More file actions
610 lines (566 loc) · 19.4 KB
/
timer.index.html
File metadata and controls
610 lines (566 loc) · 19.4 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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Workout Timer</title>
<meta name="description" content="Workout timer">
<meta name="author" content="Szabolcs Szilagyi">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<style>
body {
transition: background-color 0.5s ease;
}
textarea {
height: 200px;
}
.hidden {
display: none !important;
}
#timer {
line-height: 0.8;
margin-top: 1rem;
font-size: 25vmin;
font-weight: bold;
font-variant-numeric: tabular-nums;
display: inline-block;
max-width: 100%;
overflow: hidden;
}
#currentExercise {
line-height: 1.1;
margin-top: 0.5rem;
font-size: 10vmin;
overflow: hidden;
}
#nextExercise {
margin-top: 0.5rem;
font-size: 6vmin;
opacity: 0.7;
overflow: hidden;
}
.workoutDuration {
color: #198754;
}
.restBetweenWorkouts {
color: #dc3545;
}
.container-main {
margin-top: 1rem;
margin-bottom: 1rem;
padding: 1.5rem;
background: rgba(255, 255, 255, 0.75);
border-radius: 1rem;
}
#main-body {
display: flex;
align-items: stretch;
justify-content: space-between;
}
#timer-and-exercise {
min-width: 0;
overflow: hidden;
}
.skip-btn {
width: 45px;
display: flex;
align-items: center;
justify-content: center;
font-size: 2rem;
background: rgba(255, 255, 255, 0.45);
padding: 0;
}
.action-btn i {
font-size: 2.5rem;
}
@media (orientation: landscape) and (max-height: 500px) {
.container-main {
margin-top: 0.5rem;
margin-bottom: 0.5rem;
padding: 0.75rem;
}
#timer {
margin-top: 0.25rem;
font-size: 20vmin;
}
#currentExercise {
margin-top: 0.25rem;
font-size: 12vmin;
}
#nextExercise {
margin-top: 0.25rem;
font-size: 5vmin;
}
.action-btn i {
font-size: 1.5rem;
}
.skip-btn {
font-size: 1.5rem;
width: 35px;
}
#main-body {
margin-top: 1rem !important;
}
h1 {
font-size: 1.5rem;
margin-bottom: 0.5rem !important;
}
}
</style>
</head>
<body>
<script>
document.addEventListener('DOMContentLoaded', function(event) {
readUrlParameters()
})
function readUrlParameters() {
var queryString = window.location.search
var urlParams = new URLSearchParams(queryString)
if (urlParams.has('workoutDuration')) {
var workoutDuration = parseInt(urlParams.get('workoutDuration'))
document.getElementById("workoutDuration").value = workoutDuration
}
if (urlParams.has('restBetweenWorkouts')) {
var restBetweenWorkouts = parseInt(urlParams.get('restBetweenWorkouts'))
document.getElementById("restBetweenWorkouts").value = restBetweenWorkouts
}
if (urlParams.has('restBetweenRounds')) {
var restBetweenRounds = parseInt(urlParams.get('restBetweenRounds'))
document.getElementById("restBetweenRounds").value = restBetweenRounds
}
if (urlParams.has('exercises')) {
var exercises = decodeURIComponent(urlParams.get('exercises')).replace(/,/g, ",\n")
document.getElementById("exercises").value = exercises
}
}
var commenceRestSound = new sound("171578__leszek-szary__3-down-1.wav");
var commenceNextExerciseSound = new sound("171580__leszek-szary__3-up-1.wav");
function toPercentage(current, max) {
if (max === 0) return 1
return 1 - (current / max)
}
function getColour(value) {
// value: progress from 0 (start) to 1 (finish)
// hue: 20 (red-orange) to 70 (yellow-green)
// 50 is the range (70–20)
const startHue = 20
const endHue = 70
const range = endHue - startHue
var hue = (value * range + startHue).toString(10);
return ["hsl(", hue, ",80%,50%)"].join("");
}
function workoutLink() {
var workoutDuration = document.getElementById("workoutDuration").value
var restBetweenWorkouts = document.getElementById("restBetweenWorkouts").value
var restBetweenRounds = document.getElementById("restBetweenRounds").value
var exercises = document.getElementById("exercises").value
var url = new URL(window.location.href)
url.searchParams.set('workoutDuration', encodeURIComponent(workoutDuration))
url.searchParams.set('restBetweenWorkouts', encodeURIComponent(restBetweenWorkouts))
url.searchParams.set('restBetweenRounds', encodeURIComponent(restBetweenRounds))
url.searchParams.set('exercises', encodeURIComponent(exercises.replace(/\n/g, "")))
return url
}
function copyLink() {
const link = workoutLink()
const el = document.createElement('textarea'); // Create a <textarea> element
el.value = link; // Set its value to the string that you want copied
el.setAttribute('readonly', ''); // Make it readonly to be tamper-proof
el.style.position = 'absolute';
el.style.left = '-9999px'; // Move outside the screen to make it invisible
document.body.appendChild(el); // Append the <textarea> element to the HTML document
el.select(); // Select the <textarea> content
document.execCommand('copy'); // Copy - only works as a result of a user action (e.g. click events)
document.body.removeChild(el); // Remove the <textarea> element
alert("I like the way you roll!\nCopied: " + link);
}
function sound(src) {
this.sound = document.createElement("audio");
this.sound.src = src;
this.sound.setAttribute("preload", "auto");
this.sound.setAttribute("controls", "none");
this.sound.style.display = "none";
document.body.appendChild(this.sound);
this.play = function(){
this.sound.play();
}
this.stop = function(){
this.sound.pause();
}
}
function setWorkoutPlan(name) {
const plans = {
'Test Workout': {
duration: 10,
rest: 5,
roundRest: 8,
exercises: 'push up, plank, lunges'
},
'Core Workout': {
duration: 45,
rest: 0,
roundRest: 120,
exercises: 'glute bridge, plank, deadbug, dorsal raise, scissor legs, plank reach, side plank, side plank, aleknas, bird-dog left, bird-dog right, penguin, spiderman plank, dish hold'
},
'Tissue Conditioning': {
duration: 45,
rest: 0,
roundRest: 120,
exercises: 'left lunge, right lunge, bent over row, squats, military press, rdl, high pull-ups, press up'
}
}
const plan = plans[name]
if (plan) {
document.getElementById("workoutDuration").value = plan.duration
document.getElementById("restBetweenWorkouts").value = plan.rest
document.getElementById("restBetweenRounds").value = plan.roundRest
document.getElementById("exercises").value = plan.exercises.replace(/, /g, ",\n")
const url = workoutLink()
window.history.replaceState({}, '', url)
}
}
var state = new StateMachine()
var tickingTimer = new Timer()
var tickWatcher = new TickWatcher()
var workout = new WorkoutPlan()
var stateWatcher = new StateWatcher()
document.addEventListener(tickingTimer.eventNameTick, tickingTimer)
document.addEventListener(tickingTimer.eventNameTick, tickWatcher)
document.addEventListener(state.eventNameState, workout)
document.addEventListener(state.eventNameState, stateWatcher)
function StateMachine() {
this.eventNameState = 'state'
this.current = 'Idle'
this.next = function() {
switch (this.current) {
case 'Idle':
console.log('Idle => Workout')
this.current = 'Workout'
document.getElementById("nonTimer").classList.add("hidden")
document.querySelector("h1").classList.add("hidden")
var workoutDuration = parseInt(document.getElementById("workoutDuration").value)
var raw = document.getElementById("exercises").value
var exercises = raw.split(/,|\n/).map(s => s.trim()).filter(Boolean)
document.body.style.backgroundColor = getColour(toPercentage(workoutDuration, workoutDuration))
commenceNextExerciseSound.play()
workout.start(exercises)
tickingTimer.start(workoutDuration)
this.signalState(this.current)
break;
case 'Workout':
if (workout.hasNext()) {
var restBetweenWorkouts = parseInt(document.getElementById("restBetweenWorkouts").value)
if (restBetweenWorkouts > 0) {
console.log('Workout => Rest')
this.current = 'Rest'
commenceRestSound.play()
tickingTimer.start(restBetweenWorkouts)
this.signalState(this.current)
} else {
console.log('Workout => Workout (Rest skipped)')
this.current = 'Workout'
commenceNextExerciseSound.play()
var workoutDuration = parseInt(document.getElementById("workoutDuration").value)
tickingTimer.start(workoutDuration)
this.signalState(this.current)
}
} else {
var restBetweenRounds = parseInt(document.getElementById("restBetweenRounds").value)
console.log('Workout => NextRoundRest')
this.current = 'NextRoundRest'
commenceRestSound.play()
tickingTimer.start(restBetweenRounds)
this.signalState(this.current)
}
break;
case 'Rest':
console.log('Rest => Workout')
this.current = 'Workout'
commenceNextExerciseSound.play()
var workoutDuration = parseInt(document.getElementById("workoutDuration").value)
tickingTimer.start(workoutDuration)
this.signalState(this.current)
break;
case 'NextRoundRest':
console.log('NextRoundRest => Workout')
this.current = 'Workout'
commenceNextExerciseSound.play()
var exercises = document.getElementById("exercises").value.split(",")
workout.start(exercises)
var workoutDuration = parseInt(document.getElementById("workoutDuration").value)
tickingTimer.start(workoutDuration)
this.signalState(this.current)
break;
}
}
this.stop = function() {
console.log('... => Idle')
this.current = 'Idle'
tickingTimer.stop()
document.getElementById("nonTimer").classList.remove("hidden")
document.querySelector("h1").classList.remove("hidden")
document.body.style.backgroundColor = ""
}
this.skipNext = function() {
if (this.current === 'Idle') return
if (workout.currentIndex >= workout.exercises.length) {
this.current = 'NextRoundRest'
this.signalState(this.current)
return
}
tickingTimer.stop()
this.current = 'Workout'
this.signalState(this.current)
commenceNextExerciseSound.play()
var workoutDuration = parseInt(document.getElementById("workoutDuration").value)
tickingTimer.start(workoutDuration)
}
this.skipPrevious = function() {
if (this.current === 'Idle') return
tickingTimer.stop()
this.current = 'Workout'
// workout.next() is called when 'Workout' state starts.
// So when we are in Workout, currentIndex is already pointing to the NEXT one.
// If we want to go to PREVIOUS exercise:
// currentIndex points to CURRENT+1.
// To go to PREVIOUS: we need to set index to CURRENT-1.
// So we need to decrement by 2.
workout.previous() // back to current
workout.previous() // back to previous
if (workout.currentIndex < 0) workout.currentIndex = 0
this.signalState(this.current)
commenceNextExerciseSound.play()
var workoutDuration = parseInt(document.getElementById("workoutDuration").value)
tickingTimer.start(workoutDuration)
}
this.signalState = function(name) {
var tick = new CustomEvent(this.eventNameState, {
bubbles: true,
detail: { name: name }
})
document.dispatchEvent(tick)
}
}
function Timer() {
this.eventNameTick = 'tick'
this.remainingSeconds = 0
this.timeoutRef
this.isActive = false
this.start = function(seconds) {
this.stop()
this.isActive = true
this.remainingSeconds = seconds
this.refreshDisplay()
this.prepareForNextTick()
}
this.stop = function() {
this.isActive = false
console.log('Clearing timeout: ' + this.timeoutRef)
clearTimeout(this.timeoutRef)
}
this.refreshDisplay = function() {
document.getElementById("timer").innerHTML = this.remainingSeconds
}
this.sleepingBeauty = function() {
function whenIWakeUp(name, seconds) {
var tick = new CustomEvent(name, {
bubbles: true,
detail: { remainingSeconds: seconds }
})
document.dispatchEvent(tick)
// console.log('Sleeping beauty is active: ' + tick.detail.remainingSeconds)
}
this.timeoutRef = setTimeout(() => whenIWakeUp(this.eventNameTick, this.remainingSeconds), 1000)
}
this.prepareForNextTick = function() {
this.remainingSeconds--
this.sleepingBeauty()
}
this.handleEvent = function(tick) {
if (!this.isActive) return
// console.log('Waking up now! ' + tick.detail.remainingSeconds)
this.refreshDisplay()
this.prepareForNextTick()
// console.log('Complition ' + tick.detail.remainingSeconds)
}
}
function WorkoutPlan() {
this.exercises = []
this.currentIndex = 0
this.start = function(ex) {
this.exercises = ex
this.currentIndex = 0
}
this.next = function() {
this.currentIndex++
}
this.previous = function() {
if (this.currentIndex > 0) {
this.currentIndex--
}
}
this.hasNext = function() {
return this.currentIndex < this.exercises.length
}
this.refreshDisplay = function() {
var current = this.exercises[this.currentIndex]
var next = this.exercises[this.currentIndex + 1]
var totalExercises = this.exercises.length
var currentNumber = this.currentIndex + 1
var nextNumber = this.currentIndex + 2
if (current) {
document.getElementById("currentExercise").innerHTML = currentNumber + ". " + current.toUpperCase()
} else {
document.getElementById("currentExercise").innerHTML = "{Missing Exercise}"
}
if (next) {
document.getElementById("nextExercise").innerHTML = "up next: " + nextNumber + ". " + next.toLowerCase()
} else {
document.getElementById("nextExercise").innerHTML = "up next: rest until next round"
}
}
this.handleEvent = function(stateEvent) {
if (stateEvent.detail.name === 'Workout') {
this.refreshDisplay()
this.next()
}
}
}
function TickWatcher() {
this.handleEvent = function(tick) {
if (state.current === 'Workout') {
var workoutDuration = parseInt(document.getElementById("workoutDuration").value)
document.body.style.backgroundColor = getColour(toPercentage(tick.detail.remainingSeconds, workoutDuration))
}
if (tick.detail.remainingSeconds == 0) {
console.log('Stoping timer as time has come. Next state....')
tickingTimer.stop()
state.next()
}
}
}
function StateWatcher() {
this.handleEvent = function(stateEvent) {
switch (stateEvent.detail.name) {
case 'Workout':
document.getElementById("timer").classList.remove("restBetweenWorkouts")
document.getElementById("timer").classList.add("workoutDuration")
break;
case 'Rest':
document.body.style.backgroundColor = "hsl(100,80%,50%)"
document.getElementById("timer").classList.remove("workoutDuration")
document.getElementById("timer").classList.add("restBetweenWorkouts")
document.getElementById("currentExercise").innerHTML = "REST"
var nextExercise = workout.exercises[workout.currentIndex]
var nextNumber = workout.currentIndex + 1
var totalExercises = workout.exercises.length
if (nextExercise) {
document.getElementById("nextExercise").innerHTML = "up next: " + nextNumber + ". " + nextExercise.toLowerCase()
} else {
document.getElementById("nextExercise").innerHTML = "up next: end of round"
}
break;
case 'NextRoundRest':
document.body.style.backgroundColor = "hsl(160,80%,50%)"
document.getElementById("timer").classList.remove("workoutDuration")
document.getElementById("timer").classList.add("restBetweenWorkouts")
document.getElementById("currentExercise").innerHTML = "REST UNTIL NEXT ROUND"
document.getElementById("nextExercise").innerHTML = "get ready!"
break;
}
}
}
</script>
<div class="container container-main shadow-lg">
<h1 class="text-center mb-4">Workout Timer</h1>
<div id="nonTimer">
<div id="workoutPlans" class="row mb-4">
<div class="col-md-4">
<button type="button" class="btn btn-outline-primary w-100" onclick="setWorkoutPlan('Test Workout')">
Test Workout
</button>
</div>
<div class="col-md-4">
<button type="button" class="btn btn-outline-primary w-100" onclick="setWorkoutPlan('Core Workout')">
Core Workout
</button>
</div>
<div class="col-md-4">
<button type="button" class="btn btn-outline-primary w-100" onclick="setWorkoutPlan('Tissue Conditioning')">
Tissue Conditioning
</button>
</div>
</div>
<div class="row mb-4">
<div class="col-12 text-center">
<button type="button" class="btn btn-secondary w-100" onclick="copyLink()" id="copyLink">
Click to Copy the link of this workout!
</button>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="mb-3">
<label for="workoutDuration" class="form-label">Workout duration in seconds</label>
<input type="number" id="workoutDuration" class="form-control" name="workoutDuration" placeholder="45" value="45" />
</div>
<div class="mb-3">
<label for="restBetweenWorkouts" class="form-label">Rest between workouts in seconds</label>
<input type="number" id="restBetweenWorkouts" class="form-control" name="restBetweenWorkouts" placeholder="15" value="15" />
</div>
<div class="mb-3">
<label for="restBetweenRounds" class="form-label">Rest between rounds in seconds</label>
<input type="number" id="restBetweenRounds" class="form-control" name="restBetweenRounds" placeholder="120" value="120" />
</div>
</div>
<div class="col-md-6">
<div class="mb-3">
<label for="exercises" class="form-label">List of Exercises (comma separated)</label>
<textarea id="exercises" name="exercises" class="form-control" placeholder="burpees, push up, plank, sculler sit up, superman, mountain climbers, caterpillars, squat jumps, lunges, triceps dips">burpees,
push up,
plank,
sculler sit up,
superman,
mountain climbers,
caterpillars,
squat jumps,
lunges,
triceps dips</textarea>
</div>
</div>
</div>
</div>
<div class="row g-2 mt-2">
<div class="col-6">
<button type="button" class="btn btn-success btn-lg w-100 action-btn" onclick="state.next()">
<i class="bi bi-play-fill"></i>
</button>
</div>
<div class="col-6">
<button type="button" class="btn btn-danger btn-lg w-100 action-btn" onclick="state.stop()">
<i class="bi bi-stop-fill"></i>
</button>
</div>
</div>
<div id="main-body" class="row mt-5 text-center flex-nowrap">
<div class="col-auto px-0">
<button type="button" class="btn btn-light h-100 skip-btn" onclick="state.skipPrevious()">
<i class="bi bi-chevron-double-left"></i>
</button>
</div>
<div id="timer-and-exercise" class="col">
<p id="timer" class="mb-0">00</p>
<p id="currentExercise" class="text-uppercase font-weight-bold mb-0">{current exercise}</p>
<p id="nextExercise" class="text-lowercase">{next exercise}</p>
</div>
<div class="col-auto px-0">
<button type="button" class="btn btn-light h-100 skip-btn" onclick="state.skipNext()">
<i class="bi bi-chevron-double-right"></i>
</button>
</div>
</div>
</div>
</body>
</html>