-
Notifications
You must be signed in to change notification settings - Fork 0
/
talking-timer.js
1849 lines (1622 loc) · 56.1 KB
/
talking-timer.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
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* globals HTMLElement, SpeechSynthesisUtterance, speechSynthesis, AudioContext, customElements, talkingTimerExternalDefaults */
/**
* @var {object} talkingTimerExternalDefaults (global variable)
*
* This is intended to provide a way of customising some of the
* defaults for `talking-timer` without having to modify the code below
*
* It can be omitted or have any or all of the following properties
* & sub-properties:
*
* {
* priority: string (default: "fraction"),
* pre: {
* 10000: integer (default: 200),
* 15000: integer (default: 600),
* 20000: integer (default: 1200)
* },
* preSpeakStart: integer (default: 2300),
* preSpeakEnd: integer (default: 3300),
* chimeDelay: integer (default: 5000),
* suffixes: {
* first: string (default: " gone." - note the preceeding " "),
* last: string (default: " to go." - note the preceeding " "),
* half: string (default: "Half way."),
* },
* intervalTime: integer (default: 20),
* sayDefault: string (default: "1/2 30s last20 last15 allLast10"),
* endText: string (default: "Time's up!"),
* startText: string (default: "Ready. Set. Go!"),
* }
*
* __NOTE:__ integers represent milliseconds and are used as delays
* between one action and another
*
* PS: This may or may not be the best solution to customising default
* configuration. I'll keep researching to see if I can find a
* better solution. For now this is simple and reliable.
*/
/**
* TalkingTimer is a web component for visual and audio countdown
* timing. (For when my kids need to stop doing a thing they don't
* want to stop. And for when I'm teaching and running a time
* sensitive exercise)
*
* references:
* https://developers.google.com/web/fundamentals/web-components/customelements
* https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements
*/
class TalkingTimer extends HTMLElement {
constructor () {
super()
this.initialValue = {
hours: 0,
minutes: 0,
seconds: 0
}
this.currentValue = {
hours: 0,
minutes: 0,
seconds: 0,
tenths: 0
}
this.endTime = 0
this.initialMilliseconds = 0
this.remainingMilliseconds = 0
this.config = {
autoDestruct: -1,
autoReset: false,
noCloseBtn: false,
noEdit: false,
noEndChime: false,
noPause: false,
noReconfigure: false,
noReset: false,
noRestart: false,
noSayEnd: false,
selfDestruct: false,
sayStart: false,
priority: this.getGlobal('fraction', 'priority')
}
/**
* @var {array} pre defines the time before a spoken interval
* when the `<talking-timer>` component should
* start speaking
*
* This is used to account for the length of time it takes to
* speak the interval. The intention is to have the speaking
* finish as close to the interval time as possible.
*
* `remaining` - represents the time left until the current timer
* completes
* `delay` - the amount of time the text-to-speech is
* estimated to take
*/
this.pre = [
{ remaining: 10000, delay: this.getGlobal(200, 'pre', 10000) },
{ remaining: 19999, delay: this.getGlobal(600, 'pre', 19999) },
{ remaining: 86400, delay: this.getGlobal(1200, 'pre', 86400) }
]
/**
* @var {integer} preSpeakStart the number of millisecond it is
* estimated to complete the text-to-speach intro
* before starting the timer.
*/
this.preSpeakStart = this.getGlobal(2300, 'preSpeakStart')
/**
* @var {integer} preSpeakEnd the number of millisecond it is
* estimated to complete the end text-to-speach
* when the timer finishes.
*
* Used to delay the endChime so it doesn't start while end
* text-to-speach is in progress.
*/
this.preSpeakEnd = this.getGlobal(3300, 'preSpeakEnd')
this.chimeDelay = this.getGlobal(5000, 'chimeDelay')
this.suffixes = {
first: this.getGlobal(' gone.', 'suffixes', 'first'),
last: this.getGlobal(' to go.', 'suffixes', 'last'),
half: this.getGlobal('Half way.', 'suffixes', 'half')
}
this.intervalTime = this.getGlobal(20, 'intervalTime') // milliseconds
this.play = false
this.closeBtn = null
this.closeClick = null
this.editBtn = null
this.editClick = null
this.playPauseBtn = null
this.playPauseClick = null
this.resetBtn = null
this.resetClick = null
this.restartBtn = null
this.restartClick = null
this.numbers = null
this.progressTicker = null
this.h1 = null
this.sayDefault = this.getGlobal('1/2 30s last20 last15 allLast10', 'sayDefault')
this.say = ''
this.sayIntervals = []
this.workingIntervals = []
this.endText = this.getGlobal('Time\'s up!', 'endText')
this.startText = this.getGlobal('Ready. Set. Go!', 'startText')
this.multipliers = { hours: 3600000, minutes: 60000, seconds: 1000, tenths: 100 }
let shadowRoot = this.attachShadow({ mode: 'open' })
shadowRoot.appendChild(this.getDOM())
}
static get observedAttributes () {
return [
'currentValue',
'endTime',
'playing',
'remainingMilliseconds'
]
}
// ======================================================
// START: standard custom element callbacks
connectedCallback () {
this.parseAttributes()
if (this.initialMilliseconds > 10000) {
this.playPauseClick = this.getPlayPauseClick()
this.playPauseBtn.addEventListener('click', this.playPauseClick)
if (this.config.noReset === true) {
this.resetBtn.classList.add('hide')
}
this.resetClick = this.getResetClick()
this.resetBtn.addEventListener('click', this.resetClick)
if (this.config.noReset === true) {
this.resetBtn.classList.add('hide')
}
this.restartClick = this.getRestartClick()
this.restartBtn.addEventListener('click', this.restartClick)
if (this.config.noCloseBtn === false) {
this.closeClick = this.getCloseClick()
this.closeBtn.addEventListener('click', this.closeClick)
} else {
this.h1.classList.add('noclosebtn')
this.closeBtn.remove()
}
this.setTimeText()
this.resetTimerValues()
this.inProgress = false
this.voice = window.speechSynthesis
}
}
disconnectedCallback () {
this.playPauseBtn.removeEventListener('click', this.playPauseClick)
this.closeBtn.removeEventListener('click', this.closeClick)
this.resetBtn.removeEventListener('click', this.resetClick)
this.restartBtn.removeEventListener('click', this.restartClick)
if (this.config.noEdit === false) {
this.editBtn.removeEventListener('click', this.editClick)
}
}
// END: standard custom element callbacks
// ======================================================
// START: getters & setters
// get time () { return this.timeObjToString(this.initialValue) }
// set time (hoursMinutesSeconds) {
// this.validateTimeDuration(hoursMinutesSeconds)
// }
get playing () { return this.playing }
set playing (val) {
if (val) {
this.setAttribute('playing', 'playing')
if (!this.playing) {
this.startPlaying()
}
this.playing = true
} else {
this.removeAttribute('playing')
if (this.playing) {
this.pausePlaying()
}
this.playing = false
}
}
// END: getters & setters
// ======================================================
// START: click handlers
/**
* startPlaying() does all the stuff required to start a timer
* running
*/
startPlaying () {
if (this.endTime === 0 && this.config.sayStart === true) {
this.saySomething(this.startText)
window.setTimeout(this.startPlayingInner, this.preSpeakStart, this)
} else {
this.startPlayingInner(this)
}
}
/**
* startPlayingInner() does all the heavy lifting required
* by startPlaying()
*
* @param {this} obj the "this" context for the class
* (needed because startPlayingInner() may be called
* inside a setTimeout in which case, it will loose
* the appropriate "this" context)
*
* @returns {void}
*/
startPlayingInner (obj) {
obj.remainingMilliseconds = obj.initialMilliseconds
if (obj.config.noPause === true) {
obj.playPauseBtn.classList.add('hide')
if (obj.config.noCloseBtn === false) {
obj.closeBtn.classList.add('hide')
obj.h1.classList.add('noclosebtn')
}
}
if (obj.config.noReset === false && obj.config.noPause === false) {
obj.resetBtn.classList.remove('hide')
}
if (obj.config.noRestart === false && obj.config.noPause === false) {
obj.restartBtn.classList.remove('hide')
}
obj.setProgressTicker(obj.intervalTime)
obj.playPauseBtn.classList.add('playing')
obj.playPauseTxt.innerHTML = 'Pause '
obj.playPauseIcon.innerHTML = '‖'
obj.play = true
}
/**
* pausePlaying() suspends the timer and updates the HTML to show
* timer is stopped
*
* @returns {void}
*/
pausePlaying () {
this.clearTimerInterval()
this.playPauseBtn.classList.remove('playing')
this.playPauseTxt.innerHTML = 'Play '
this.playPauseIcon.innerHTML = '▽'
this.play = false
}
/**
* endPlaying() does all the stuff needed to show the
* timer has ended
*
* removes the interval used by the timer, updates the
* HTML and makes sounds
*
* @returns {void}
*/
endPlaying () {
let delay = 0
if (this.config.noSayEnd === false) {
this.saySomething(this.endText)
delay = this.preSpeakEnd
}
if (this.config.noEndChime === false) {
window.setTimeout(this.endSound, delay)
delay = this.chimeDelay
}
this.numbers.classList.add('finished')
this.playPauseBtn.classList.add('finished')
this.clearTimerInterval()
if (this.config.autoDestruct !== -1) {
const timeout = (this.config.autoDestruct < delay) ? delay : this.config.autoDestruct
window.setTimeout((obj) => { obj.remove() }, timeout, this)
// This timer is going to self destruct.
// Don't bother doing anything more
return
} else if (this.config.autoReset === true) {
this.resetClick()
}
if (this.config.noPause === true) {
this.playPauseBtn.classList.remove('hide')
if (this.config.noCloseBtn === false) {
this.closeBtn.classList.remove('hide')
this.h1.classList.remove('noclosebtn')
}
}
}
/**
* getPlayPauseClick() returns a function for handling click events
* on the Play/Pause buttton
*
* When timer is running, it pause it. When timer is paused or has
* not yet started it starts the timer running.
*
* @returns {function}
*/
getPlayPauseClick () {
const playPauseClick = (event) => {
if (this.play) {
// pausing
this.pausePlaying()
} else {
// start playing
this.startPlaying()
}
}
return playPauseClick
}
/**
* getResetClick() handles getting the timer ready
* to start from begining
*
* @returns {function}
*/
getResetClick () {
const resetClick = () => {
this.pausePlaying()
this.resetTimerValues()
this.numbers.innerHTML = this.timeObjToString(this.initialValue)
this.progress.value = (0)
this.playPauseTxt.innerHTML = 'Start '
this.numbers.classList.remove('finished')
this.playPauseBtn.classList.remove('finished')
this.restartBtn.classList.add('hide')
this.resetBtn.classList.add('hide')
}
return resetClick
}
/**
* getRestartClick() returns a function to be used as a click handler
* for the custom element's restart button.
*
* Click handler resets timer and starts playing.
*
* @returns {function}
*/
getRestartClick () {
const restartClick = () => {
this.resetClick()
this.startPlaying()
}
return restartClick
}
/**
* getCloseClick() returns a function to be used as a click handler
* for the custom element's close button.
*
* Click handler clears all window.intervals set when a timer
* starts.
* Removes all event listeners then removes the custom element
* from the DOM
*
* @returns {function}
*/
getCloseClick () {
const closeClick = (event) => {
if (this.progressTicker !== null) {
window.clearInterval(this.progressTicker)
}
this.playPauseBtn.removeEventListener('click', this.playPauseClick)
this.resetBtn.removeEventListener('click', this.resetClick)
this.restartBtn.removeEventListener('click', this.restartClick)
this.closeBtn.removeEventListener('click', this.closeClick)
this.remove()
}
return closeClick
}
// END: click handlers
// ======================================================
// START: DOM builders
/**
* getDOM builds the shadow DOM for the custom element
*
* Creates the following nodes:
* 1. wrapping div used as the shell of the element
* 2. a style element with all the CSS for the element
* 3. the heading containing the user supplied content for the
* title of the timer
* 4. the progress bar to show where the timer is at visually
* 5. a span containing the numbers for the textual representation
* of the timer's progress
* 6. a wrapping div containing the buttons for
* * pause/play
* * restart ("Start again")
* * reset
* 7. a close button to dismis the timer
*/
getDOM () {
const wrap = document.createElement('div')
wrap.setAttribute('class', 'TalkingTimer-wrapper')
const css = this.initStyle()
const style = document.createElement('style')
style.appendChild(css)
const h1 = document.createElement('h1')
const slot = document.createElement('slot')
h1.appendChild(slot)
wrap.appendChild(h1)
this.h1 = h1
const progress = document.createElement('progress')
progress.setAttribute('max', 1)
progress.setAttribute('value', 0)
wrap.appendChild(progress)
this.progress = progress
const numbers = document.createElement('span')
numbers.setAttribute('class', 'timer-text')
// numbers.appendChild(document.createTextNode(startTime))
const numbersWrap = document.createElement('div')
numbersWrap.setAttribute('class', 'timer-text--wrap')
numbersWrap.appendChild(numbers)
wrap.appendChild(numbersWrap)
this.numbers = numbers
wrap.appendChild(this.initMainBtns())
wrap.appendChild(this.initCloseBtn())
wrap.appendChild(style)
return wrap
}
/**
* initCloseBtn() builds a close button to be inserted into the
* HTML of the custom element
*
* @returns {HTMLElement} simple close button
*/
initCloseBtn () {
const close = document.createElement('button')
const closeSR = document.createElement('span')
const closeIcon = document.createElement('span')
closeSR.setAttribute('class', 'sr-only')
closeSR.appendChild(document.createTextNode('Close'))
closeIcon.setAttribute('class', 'smallBtn__icon')
closeIcon.innerHTML = '⊗'
close.setAttribute('class', 'closeBtn smallBtn')
close.appendChild(closeSR)
close.appendChild(closeIcon)
this.closeBtn = close
return close
}
/**
* initMainBtns() builds three buttons and wraps them in a <div>
*
* Buttons are:
* * pausePlay - used to control the countdown timing process
* * restart - used to trigger a reset, play action
* * reset - used to trigger a stop, reset action
*
* @returns {HTMLElement}
*/
initMainBtns () {
const btnWrap = document.createElement('div')
btnWrap.setAttribute('class', 'btn-wrapper')
const playPauseIcon = document.createElement('span')
playPauseIcon.innerHTML = '▽'
playPauseIcon.setAttribute('class', 'non-sr icon')
const playPauseTxt = document.createElement('span')
playPauseTxt.setAttribute('class', 'playPauseTxt')
playPauseTxt.appendChild(document.createTextNode('Start '))
const playPause = document.createElement('button')
playPause.setAttribute('class', 'playPauseBtn')
playPause.appendChild(playPauseTxt)
playPause.appendChild(playPauseIcon)
const restartIcon = document.createElement('span')
restartIcon.setAttribute('class', 'non-sr icon')
restartIcon.innerHTML = '↻'
const restart = document.createElement('button')
restart.setAttribute('class', 'restartBtn')
restart.appendChild(document.createTextNode('Start again '))
restart.appendChild(restartIcon)
restart.classList.add('hide')
const resetIcon = document.createElement('span')
resetIcon.setAttribute('class', 'non-sr icon')
resetIcon.innerHTML = '↩'
const reset = document.createElement('button')
reset.setAttribute('class', 'resetBtn')
reset.appendChild(document.createTextNode('Reset '))
reset.appendChild(resetIcon)
reset.classList.add('hide')
btnWrap.appendChild(playPause)
btnWrap.appendChild(restart)
btnWrap.appendChild(reset)
this.playPauseBtn = playPause
this.playPauseIcon = playPauseIcon
this.playPauseTxt = playPauseTxt
this.restartBtn = restart
this.resetBtn = reset
return btnWrap
}
/**
* initStyle() returns block of CSS for styling the <talking-timer>
* element's shadow DOM
*
* @returns {textNode} CSS string
*/
initStyle () {
return document.createTextNode(`
:host {
--closebtn-left: 2;
--closebtn-right: 3;
--closebtn-top: 1;
--closebtn-bottom: 2;
--configbtn-left: -2;
--configbtn-right: -1;
--configbtn-top: 3;
--configbtn-bottom: 4;
--btn-color: inherit;
--btn-background: #fff;
--btn-size: 1.25em;
--btn-padding: 0.5em 0;
--btn-border-color: #c0e;
--btn-border-width: 0.05em;
--btn-hover-color: #fff;
--btn-hover-background: #eee;
--btn-hover-border-color: #eee;
--btn-hover-border-width: 0.05em;
--h1-size: 1.5em;
--h1-padding: 0.5em 2.5em 0.5em 0.5em;
--h1-noclosebtn-padding: 0.5em;
--h1-align: center;
--playpause-color: #fff;
--playpause-size: 1.25em;
--playpause-weight: bold;
--playpause-background: #050;
--playpause-border-width: 0.05em;
--playpause-border-color: #040;
--playpause-hover-weight: bold;
--playpause-hover-color: #fff;
--playpause-hover-background: #030;
--playpause-hover-border-width: #fff;
--playpause-hover-border-color: #020;
--progress-background: #fff;
--progress-border-color: #ccc;
--progress-border-width: 0.05em;
--progress-color: #F00;
--progress-height: 2em;
--progress-left: -0.05em;
--progress-right: auto;
--smallbtn-color: inherit;
--smallbtn-background: transparent;
--smallbtn-border-width: 0;
--smallbtn-border-style: none;
--smallbtn-border-color: transparent;
--smallbtn-size: 2em;
--smallbtn-left: 2;
--smallbtn-right: 2;
--smallbtn-position: absolute;
--smallbtn-padding: 0.2em 0.25em;
--smallbtn-weight: normal;
--smallbtn-hover-color: #c00;
--smallbtn-hover-weight: bold;
--smallbtn-hover-background: transparent;
--smallbtn-hover-border-width: 0;
--smallbtn-hover-border-style: none;
--smallbtn-hover-border-color: transparent;
--talkingTimer-columns: auto 2em;
--talkingTimer-rows: 2em auto auto 2em auto auto;
--timertext-color: #222;
--timertext-family: verdana, arial, helvetica, sans-serif;
--timertext-size: 6em;
--timertext-weight: bold;
--timertext-padding: 0.1em 0.25em 0.2em;
--timertext-align: center;
--wrapper-border-width: 0.05em;
--wrapper-border-color: #ccc;
}
:root {
font-family: inherit;
color: inherit;
}
h1 {
grid-column-start: 1;
grid-column-end: 3;
grid-row-start: 1;
grid-row-end: 3;
font-size: var(--h1-size);
margin: 0;
padding: var(--h1-padding);
text-align: var(--h1-align);
}
h1.noclosebtn {
padding: var(--h1-noclosebtn-padding);
}
.btn-wrapper {
grid-column-start: 1;
grid-column-end: 3;
grid-row-start: 6;
grid-row-end: 7;
align-items: stretch;
display: flex;
justify-content: space-between;
}
button {
background-color: var(--btn-background);
border-width: var(--btn-border-width);
border-style: solid;
border-color: var(--btn-border-color);
flex-grow: 1;
font-size: var(--btn-size);
font-variant: small-caps;
padding: var(--btn-padding);
}
button:last-child {
border-right-width: 0.075em;
}
button:hover {
background-color: var(--btn-hover-background);
border-width: var(--btn-hover-border-width);
border-color: var(--btn-hover-border-color);
cursor: pointer;
}
button .icon {
display: inline-block;
font-weight: bold;
font-size: 1.25em;
margin-bottom: -1em;
margin-left: 0.3em;
margin-top: -1em;
transform: translateY(0.15em);
}
.playPauseBtn {
border-color: var(--playpause-border-color);
border-width: var(--playpause-border-width);
background-color: var(--playpause-background);
color: var(--playpause-color);
flex-grow: 3;
font-weight: var(--playpause-weight);
}
.playPauseBtn:hover {
background-color: var(--playpause-hover-background);
border-color: var(--playpause-hover-border-color);
font-weight: var(--playpause-hover-weight);
}
.playPauseBtn .icon {
transform: translateY(0.15em) rotate(30deg);
}
.playPauseBtn.playing .icon {
transform: translateY(-0.05em);
}
.playPauseBtn.finished {
opacity: 0;
}
.restartBtn .icon {
font-size: 1.5em;
transform: translateY(0.15em) rotate(45deg);
}
.resetBtn .icon {
font-weight: normal;
}
@media screen {
.sr-only {
display: inline-block;
height: 1px;
margin: -1px 0 0 -1px;
opacity: 0;
width: 1px;
}
}
.smallBtn {
background: var(--smallbtn-background);
border-width: var(--smallbtn-border-width);
border-style: var(--smallbtn-border-style);
border-color: var(--smallbtn-border-color);
color: var(--smallbtn-color);
font-size: var(--smallbtn-size);
font-weight: var(--smallbtn-weight);
grid-column-start: var(--smallbtn-left);
grid-column-end: var(--smallbtn-right);
line-height: 0em;
margin: 0;
padding: var(--smallbtn-padding);
}
.closeBtn {
grid-row-start: var(--closebtn-top);
grid-row-end: var(--closebtn-bottom);
}
.smallBtn:hover,
.smallBtn:focus {
background-color: var(--smallbtn-hover-background);
border-width: var(--smallbtn-hover-border-width);
border-style: var(--smallbtn-hover-border-style);
border-color: var(--smallbtn-hover-border-color);
color: var(--smallbtn-hover-color);
font-weight: var(--smallbtn-hover-weight);
}
.smallBtn__icon {
position: relative;
top: 0;
left: -0.25em;
}
.timer-text {
color: var(--timertext-color);
display: block;
font-family: var(--timertext-family);
font-size: var(--timertext-size);
font-weight: var(--timertext-weight);
padding: var(--timertext-padding);
text-align: var(--timertext-align);
}
progress {
grid-column-start: 1;
grid-column-end: 3;
grid-row-start: 3;
grid-row-end: 4;
z-index: 10;
background-color: #fff;
border-width: var(--progress-border-width);
border-style: solid;
border-color: var(--progress-border-color);
color: var(--progress-color);
display: block;
height: var(--progress-height);
left: var(--progress-left);
right: var(--progress-right);
position: relative;
width: 100%;
}
.finished {
background-color: #c00;
color: #fff;
}
.tenths {
font-size: 0.5em;
font-weight: normal;
}
.hide {
display: none;
}
.TalkingTimer-wrapper {
display: grid;
grid-template-columns: var(--talkingTimer-columns);
grid-template-rows: var(--talkingTimer-rows);
}
.timer-text--wrap {
grid-column-start: 1;
grid-column-end: 3;
grid-row-start: 4;
grid-row-end: 6;
}
.config-btn {
grid-row-start: var(-configbtn-top);
grid-row-end: var(--configbtn-bottom);
}
.config-wrap {
grid-column-start: 1;
grid-column-end: 3;
grid-row-start: 1;
grid-row-end: 6;
z-index: 10;
overflow-y: auto;
background-color: rgba(255, 255, 255, 0.75);
}
`
)
}
// END: DOM builders
// ======================================================
// START: timer callbacks
/**
* setTimeText() returns a callback function to be passed to
* `window.setInterval()` or `window.clearInterval()`
*
* The callback handles updating the textual representation of the
* time remaining in the countdown
*
* @returns {void}
*/
setTimeText () {
this.numbers.innerHTML = this.timeObjToString(this.currentValue)
}
/**
* setProgressTicker()
*
* @param {integer} interval number of Milliseconds the interval
* should be between when the progress bar is
* updated.
* @returns {void}
*/
setProgressTicker (interval) {
if (this.endTime === 0) {
this.endTime = Date.now() + this.remainingMilliseconds
}
const progressTickTock = () => {
this.remainingMilliseconds = this.endTime - Date.now()
if (this.remainingMilliseconds < 0) {
this.remainingMilliseconds = 0
}
const promise1 = new Promise((resolve, reject) => {
const preOffset = this.getSpeakPreOffset(this.remainingMilliseconds)
this.progress.value = (1 - (this.remainingMilliseconds / this.initialMilliseconds))
this.currentValue = this.millisecondsToTimeObj(this.remainingMilliseconds)
if (Math.floor(this.remainingMilliseconds) <= 0) {
this.endPlaying()
} else if (this.workingIntervals.length > 0 && (this.workingIntervals[0].offset + preOffset) > this.remainingMilliseconds) {
const sayThis = this.workingIntervals.shift()
if (this.posMinus(sayThis.offset, this.remainingMilliseconds) < 2000) {
// This ensures that if for some reason, there is a
// back-log of intervals to be spoken, only intervals
// that should have been spoken within the last
// 2 seconds get spoken
this.saySomething(sayThis.message)
}
}
})
const promise3 = new Promise((resolve, reject) => { this.setTimeText() })
}
this.progressTicker = setInterval(progressTickTock, interval)
}
/**
* getSpeakPreOffset() gets the number of milliseconds the text-to-speech should take
*
* @param {integer} timeRemaining number of Milliseconds
* remaining until the end of the timer
*.
* @returns {integer}
*/
getSpeakPreOffset (timeRemaining) {
const c = this.pre.length
for (let a = 0; a < c; a += 1) {
if (timeRemaining <= this.pre[a].remaining) {
return this.pre[a].delay
}
}
const b = c - 1
return this.pre[b].delay
}
// END: timer callbacks
// ======================================================
// START: utility methods
/**
* onlyGreaterThanZero() ensures that the most significant field in
* the returned timeObj is non-zero
*
* @param {object} currentValue containing seconds, minutes & hours
* representing the timer's duration
*
* @returns {object} object containing only the least significant
* fields greater than zero
*/
onlyGreaterThanZero (currentValue) {
const fields = ['hours', 'minutes', 'seconds', 'tenths']
let tmpValue = {}
let allTheRest = false
for (let a = 0; a < 4; a += 1) {
const field = fields[a]