-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcaribou-timer.html
729 lines (642 loc) · 26.8 KB
/
caribou-timer.html
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
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../iron-input/iron-input.html">
<link rel="import" href="../paper-icon-button/paper-icon-button.html">
<link rel="import" href="custom-icons.html">
<!--
`<caribou-timer>` is an element used to display a timer (Android style)
This element allow you to set a default time. This time can be edited with the plus and minus button.
Or if the attribute 'input-time' is added, it can be edited with the keyboard and the arrow key.
A maximum and minimum limit can be set.
Moreover you can set the 'remaining-time' property. If set this property will automatically start the timer,
a the specific point according to the whole-time value.
Example:
'<caribou-timer input-time whole-time="60" max-time-limit="3600" min-time-limit="30"></caribou-timer>'
### Styling The following custom properties and mixins are available for styling: Custom property | Description | Default
----------------|-------------|----------
`--editable-text-color` | Time color on the editable field (inputTime=true) | `#64B5F6`
`--editable-text-color-disabled` | Time color on the editable field disabled (inputTime=true) | `#64B5F6`
`--non-editable-text-color`| Time color on the non editable field (inputTime=false)| `#64B5F6`
`--time-button-color`| Plus and minus button color | `#64B5F6`
`--time-button-color-disabled`| Disabled plus and disabled minus button color | `#64B5F6`
`--time-button-color-hover`| Plus and minus button color hover | `#64B5F6`
`--circle-line-passed-color`| Color of the passed circle line | `#64B5F6`
`--circle-line-remaining-color`| Color of the remaining circle line | `#64B5F6`
`--circle-line-endpoint-fill-color`| Fill color or the little circle at the edge of the progression line | `#64B5F6`
`--circle-line-endpoint-stroke-color` | Stroke color or the little circle at the edge of the progression line | `#64B5F6`
`--button-start-color` | Button start color | `#64B5F6`
`--button-pause-color`| Button pause color | `#64B5F6`
`--button-start-color-hover`| Button start color on hover | `#64B5F6`
`--button-pause-color-hover`| Button pause color on hover | `#64B5F6`
-->
<dom-module id="caribou-timer">
<template>
<style>
:host {
--editable-text-color: #64B5F6;
--editable-text-color-disabled: #64B5F6;
--non-editable-text-color: #64B5F6;
--time-button-color: #64B5F6;
--time-button-color-disabled: #64B5F6;
--time-button-color-hover: #64B5F6;
--circle-line-passed-color: #B6B6B6;
--circle-line-remaining-color: #64B5F6;
--circle-line-endpoint-fill-color: #ffffff;
--circle-line-endpoint-stroke-color: #64B5F6;
--button-start-color: #64B5F6;
--button-pause-color: #64B5F6;
--button-start-color-hover: #64B5F6;
--button-pause-color-hover: #64B5F6;
display: block;
}
button[data-setter] {
outline: none;
background: transparent;
border: none;
font-family: 'Roboto';
font-weight: 300;
font-size: 18px;
width: 25px;
height: 30px;
color: var(--time-button-color);
cursor: pointer;
}
button[data-setter]:disabled {
color: var(--time-button-color-disabled);
cursor: default;
}
button[data-setter]:hover {
opacity: 0.5;
color: var(--time-button-color-hover);
}
.container {
position: relative;
top: 30px;
width: 300px;
margin: 0 auto;
}
.setters {
position: absolute;
left: 50px;
top: 75px;
}
.minutesSet,
.hoursSet {
float: left;
margin-right: 22px;
}
.secondsSet {
float: right;
}
.controlls {
position: absolute;
left: 50px;
top: 100px;
text-align: center;
}
#pause {
margin-top: 10px;
}
#pause:hover {
opacity: 0.8;
}
.eCBase {
fill: none;
stroke: var(--circle-line-passed-color);
stroke-width: 4px
}
.eCProgress {
fill: none;
stroke: var(--circle-line-remaining-color);
stroke-width: 4px;
transition: stroke-dashoffset 1s linear;
}
.eCPointer {
fill: var(--circle-line-endpoint-fill-color);
stroke: var(--circle-line-endpoint-stroke-color);
stroke-width: 2px;
}
#ePointer {
transition: transform 1s linear;
}
#buttonGroup {
width: 206px;
}
.displayRemainTime,
#inputTimeGroup {
@apply --layout-horizontal;
}
.displayRemainTime {
font-weight: 100;
font-size: 45px;
color: var(--non-editable-text-color);
}
#inputTimeGroup p,
.displayRemainTime p {
font-weight: 100;
font-size: 30px;
margin: auto;
}
.displayRemainTime p {
color: var(--non-editable-text-color);
}
#inputTimeGroup p {
color: var(--editable-text-color);
}
#inputTimeGroup p:disabled {
color: var(--editable-text-color-disabled);
}
#pause {
color: var(--button-start-color);
--paper-icon-button: {
width: 60px;
height: 60px;
}
}
#pause:hover {
color: var(--button-start-color-hover);
}
#pause.pauseBtn {
color: var(--button-pause-color);
}
#pause.pauseBtn:hover {
color: var(--button-pause-color-hover);
}
input.timeInput {
width: 55px;
font-weight: 100;
font-size: 45px;
color: var(--editable-text-color);
border: none;
outline: none;
font-family: 'Roboto';
}
input.timeInput:disabled {
background: none;
color: var(--editable-text-color-disabled);
}
input.timeInput::-webkit-outer-spin-button,
input.timeInput::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
#eCPointer {
transition: transform 1s linear;
}
input[type=number]::-webkit-outer-spin-button,
input[type=number]::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
input[type=number] {
-moz-appearance: textfield;
}
</style>
<div class="container">
<div class="setters">
<div class="hoursSet">
<button id="hrPlus" disabled$="[[isStarted]]" on-click="changeTime" data-setter="hours-plus">+</button>
<button id="hrMinus" disabled$="[[isStarted]]" on-click="changeTime" data-setter="hours-minus">-</button>
</div>
<div class="minutesSet">
<button id="minPlus" disabled$="[[isStarted]]" on-click="changeTime" data-setter="minutes-plus">+</button>
<button id="minMinus" disabled$="[[isStarted]]" on-click="changeTime" data-setter="minutes-minus">-</button>
</div>
<div class="secondsSet">
<button id="secPlus" disabled$="[[isStarted]]" on-click="changeTime" data-setter="seconds-plus">+</button>
<button id="secMinus" disabled$="[[isStarted]]" on-click="changeTime" data-setter="seconds-minus">-</button>
</div>
</div>
<div class="circle">
<svg width="300" height="300" viewBox="0 0 220 220" xmlns="http://www.w3.org/2000/svg">
<g transform="translate(110,110)">
<circle r="100" class="eCBase" />
<g transform="rotate(-90)">
<circle r="100" class="eCProgress" />
<g id="ePointer">
<circle id="eCPointer" cx="100" cy="0" r="8" class="eCPointer" />
</g>
</g>
</g>
</svg>
</div>
<div class="controlls">
<div id="divTimeGroup" class="displayRemainTime" hidden$="[[inputTime]]">
<div id="hour">{{hour}}</div>
<p>:</p>
<div id="min">{{min}}</div>
<p>:</p>
<div id="sec">{{sec}}</div>
</div>
<div id="inputTimeGroup" hidden$="[[!inputTime]]">
<iron-input bind-value="{{hour}}">
<input is="iron-input" disabled$="[[isStarted]]" class="timeInput" value="{{hour::input}}" min="0" id="hour" type="number"
on-change="_recalculWholeTime" on-keyup="_recalculWholeTime">
</iron-input>
<p disabled$="[[isStarted]]">:</p>
<iron-input bind-value="{{min}}">
<input is="iron-input" disabled$="[[isStarted]]" class="timeInput" value="{{min::input}}" min="0" max="60" id="min" type="number"
on-change="_recalculWholeTime" on-keyup="_recalculWholeTime">
</iron-input>
<p disabled$="[[isStarted]]">:</p>
<iron-input bind-value="{{sec}}">
<input is="iron-input" disabled$="[[isStarted]]" class="timeInput" value="{{sec::input}}" min="0" max="60" id="sec" type="number"
on-change="_recalculWholeTime" on-keyup="_recalculWholeTime">
</iron-input>
</div>
<div id="buttonGroup">
<paper-icon-button id="pause" on-click="pauseTimer" icon="custom-icons:{{_ternary(isStarted,'pause-circle-filled','play-circle-filled')}}"
class$="{{_ternary(isStarted,'pauseBtn','')}}"></paper-icon-button>
<slot></slot>
</div>
</div>
</div>
</template>
<script>
/**
* `caribou-timer`
* Timer element - android style
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class CaribouTimer extends Polymer.Element {
/**
* Fired when the timer is stopped
*
* @event start-timer
* @param {Object} detail empty
*/
/**
* Fired when the timer is started
*
* @event stop-timer
* @param {Object} detail empty
*/
/**
* Fired when the max time limit is reached
*
* @event max-time-limit-reached
* @param {Object} detail empty
*/
/**
* Fired when the min time limit is reached
*
* @event min-time-limit-reached
* @param {Object} detail empty
*/
static get is() {
return 'caribou-timer';
}
static get properties() {
return {
/**
* Property used to display editable time field or just a classic div.
*/
inputTime: {
type: Boolean,
value: false,
observer: "_showTime"
},
/**
* This is the length of the circle timer.
*/
_length: {
type: Number,
value() {
return Math.PI * 2 * 100;
},
readOnly: true
},
/**
* This is the time remaining.
* If you want to start the timer at a specific point, this will represent the time left.
*/
remainingTime: {
type: Number,
observer: "_resumeTimer"
},
/**
* This is the maximum time allowed into the timer.
*/
maxTimeLimit: Number,
/**
* This is the minimum time allowed into the timer.
*/
minTimeLimit: Number,
/**
* This is the interval function element created.
*/
_intervalTimer: Number,
/**
* This is the time left during the decount.
* It will change everytime the timer is working.
*/
_timeLeft: Number,
/**
* This is the time to initialize the timer.
* This property will contain the timer set before the timer start.
*/
wholeTime: {
type: Number,
value() {
return 0.5 * 60;
},
notify: true,
observer: '_displayTimeLeft'
},
/**
* This is to indicate if the timer is paused.
*/
isPaused: {
type: Boolean,
value: false
},
/**
* This is to indicate if the timer is started, and is currently running.
*/
isStarted: {
type: Boolean,
value: false
},
/**
* This property is used to indicate if the timer start right after the start button is clicked,
* Or if annother action need to be done to start the timer.
* If true the timer will start after the function is called.
*/
waitStartFlag: {
type: Boolean,
value: false
}
};
}
/**
* We call the ready function to clear the timer on first load.
*/
ready() {
super.ready();
this._clearTimer();
}
/**
* This function is an observer callback used to display the good time input view.
* @param {Boolean} isInput The Value of the new property
* @private
*/
_showTime(isInput) {
if (isInput) {
this.$.inputTimeGroup.style.display = "flex";
this.$.divTimeGroup.style.display = "none";
} else {
this.$.divTimeGroup.style.display = "flex";
this.$.inputTimeGroup.style.display = "none";
}
}
/**
* This function is called when the 'plus' and 'minus' character are clicked.
* The wholeTime will be redefined according to the parameter.
* It will call the function to change the view.
*
* @param {Number} seconds The Value of the new property
*/
changeWholeTime(seconds) {
if ((this.wholeTime + seconds) > 0) {
if (!this._isTimeValid(this.wholeTime + seconds)) {
this._displayTimeLeft(this.wholeTime);
return;
}
this.set('wholeTime', this.wholeTime + seconds);
this._update(this.wholeTime, this.wholeTime);
}
}
/**
* This is a callback function used to change the time when the input value change.
* We will redefined the 'wholeTime' property and check if the time follow the min/max rules.
*
* @private
*/
_recalculWholeTime() {
var h = Number(this.$.hour.value);
var m = Number(this.$.min.value);
var s = Number(this.$.sec.value);
var tot = h * 60 * 60 + m * 60 + s;
if (!this._isTimeValid(tot)) {
this._displayTimeLeft(this.wholeTime);
return;
}
this.set('wholeTime', tot);
this._update(this.wholeTime, this.wholeTime);
}
/**
* This function is used to check if the new time respect the min/max rules.
* If it doesn't and event will be fired in both case.
* 'max-time-limit-reached' and 'min-time-limit-reached'.
*
* @param {Number} Time This is the time in second
* @return {Boolean} True if it's valid, false otherwise
* @private
*/
_isTimeValid(time) {
//check if a max time is set.
//return true (valid) if not
if (this.maxTimeLimit !== null && this.maxTimeLimit !== undefined) {
if (time > this.maxTimeLimit) {
this.dispatchEvent(new CustomEvent('max-time-limit-reached', {
bubbles: true,
composed: true
}));
return false;
} else if (time < this.minTimeLimit) {
this.dispatchEvent(new CustomEvent('min-time-limit-reached', {
bubbles: true,
composed: true
}));
return false;
}
return true;
} else {
return true;
}
}
/**
* This function is used to update the circle progression.
*
* @param {Number} value
* @param {Number} timePercent
* @private
*/
_update(value, timePercent) {
var offset = -this._length - this._length * value / (timePercent);
this.shadowRoot.querySelector(".eCProgress").style.strokeDashoffset = offset;
//this.$.ePointer.style.transform = `rotate(${360 * value / (timePercent)}deg)`;
this.$.eCPointer.setAttribute("transform", `rotate(${360 * value / (timePercent)})`);
this.$.eCPointer.style.transform = `rotate(${360 * value / (timePercent)}deg)`;
}
/**
* This function is the 'start timer' internal function.
* It will start the interval to decrease the time and redraw the circle.
*
* @param {Number} seconds This is the time
* @private
*/
_timer(seconds) { //counts time, takes seconds
var remainTime = Date.now() + (seconds * 1000);
this._displayTimeLeft(seconds);
this._intervalTimer = setInterval(function () {
this._timeLeft = Math.round((remainTime - Date.now()) / 1000);
if (this._timeLeft < 0) {
clearInterval(this._intervalTimer);
this.isStarted = false;
this.querySelectorAll('button[data-setter]').forEach(function (btn) {
btn.disabled = false;
btn.style.opacity = 1;
});
this._displayTimeLeft(this.wholeTime);
this._fireEvent("stop");
return;
}
this._displayTimeLeft(this._timeLeft);
}.bind(this), 1000);
}
/**
* This function is used to stop the timer.
*/
pauseTimer() {
if (this.isStarted === false) {
this._fireEvent("start");
if (!this.waitStartFlag)
this.launchTimer();
} else {
this._fireEvent("stop");
this.stopTimer();
}
}
/**
* This is function to start the whole process.
* This will call the '_timer' function to start the progress.
*/
launchTimer() {
this._timer(this.wholeTime);
this.isStarted = true;
/*this.querySelectorAll('button[data-setter]').forEach(function (btn) {
btn.disabled = true;
btn.style.opacity = 0.5;
});*/
}
/**
* This is used to set the time that will be display while the countdown is processing.
*
* @param {Number} timeLeft This is the time left that need to be parsed.
* @private
*/
_displayTimeLeft(timeLeft) { //displays time on the input
var realMinutes = Math.floor(timeLeft / 60);
var minutes = Math.floor(realMinutes % 60);
var hours = Math.floor(realMinutes / 60);
var seconds = timeLeft % 60;
var hS = (hours < 10 ? '0' : '') + hours;
var mS = (minutes < 10 ? '0' : '') + minutes;
var sS = (seconds < 10 ? '0' : '') + seconds;
this.set("hour", hS.toString());
this.set("min", mS.toString());
this.set("sec", sS.toString());
this._update(timeLeft, this.wholeTime);
}
/**
* This is a callback function used to call the 'changeWholeTime' function according to the button clicked.
* It will go through the dataset and calcul how muck time need to be added or removed.
* @param {Object} event The event object
*/
changeTime(event) {
var param = event.currentTarget.dataset.setter;
var seconds;
switch (param) {
case 'hours-plus':
this.changeWholeTime(1 * 60 * 60);
break;
case 'hours-minus':
this.changeWholeTime(-1 * 60 * 60);
break;
case 'minutes-plus':
this.changeWholeTime(1 * 60);
break;
case 'minutes-minus':
this.changeWholeTime(-1 * 60);
break;
case 'seconds-plus':
this.changeWholeTime(1);
break;
case 'seconds-minus':
this.changeWholeTime(-1);
break;
}
this._displayTimeLeft(this.wholeTime);
}
/**
* This is a callback observer function called when the remaining time is set.
*
* @param {Number} remaining This is the remaining time in seconds.
* @private
*/
_resumeTimer(remaining) {
this.set("isStarted", true);
this._timer(remaining);
}
/**
* This function is used to clear the timer.
* Enable the input, reset the circle timer.
* Display the 'wholeTime' property value.
* @private
*/
_clearTimer() {
this.shadowRoot.querySelector(".eCProgress").style.strokeDasharray = this._length;
this._update(this.wholeTime, this.wholeTime);
this._displayTimeLeft(this.wholeTime);
/*this.querySelectorAll('button[data-setter]').forEach(function (btn) {
btn.disabled = false;
btn.style.opacity = 1;
});*/
}
/**
* Function used to fire an event according to the type pass in parameter.
* Two events can be fired : 'start-timer' and 'stop-timer'.
*
* @param {String} type Start or Stop
*/
_fireEvent(type) {
if (type === "start") {
this.dispatchEvent(new CustomEvent('start-timer', {
bubbles: true,
composed: true
}));
} else if (type === "stop") {
this.dispatchEvent(new CustomEvent('stop-timer', {
bubbles: true,
composed: true
}));
}
}
/**
* This functio is used to stop the timer.
* This will stop the interval countdown and call the 'clearTimer' function for the circle
* and the display time.
*/
stopTimer() {
this.isStarted = false;
clearInterval(this._intervalTimer);
this._clearTimer();
}
/**
* Used to return the value 'a' or 'b' according to the 's' value.
*
* @param {boolean} s The boolean value to check
* @param {String} a Value returned if true
* @param {String} b Value returned if false
* @return {String} a or b
*/
_ternary(s, a, b) {
return s ? a : b
}
}
window.customElements.define(CaribouTimer.is, CaribouTimer);
</script>
</dom-module>