forked from google/santa-tracker-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountdown-timer.html
More file actions
348 lines (290 loc) · 9.34 KB
/
Copy pathcountdown-timer.html
File metadata and controls
348 lines (290 loc) · 9.34 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
<!--
Copyright 2015 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
-->
<link rel="import" href="../../components/polymer/polymer.html">
<link rel="import" href="../../components/iron-flex-layout/iron-flex-layout.html">
<link rel="import" href="../../js/utils.html">
<!-- TODO(samthor): Upgrade styling for Polymer 1+ (#1165) -->
<link rel="stylesheet" href="countdown-timer.css" />
<!--
Displays a countdown timer.
Example
<countdown-timer count-down-end-date="1450951200000"
finished="{{isFinished}}">
<h3>Days</h3>
<h3>Hours</h3>
<h3>Minutues</h3>
<h3>Seconds</h3>
</countdown-timer>
-->
<dom-module id="countdown-timer">
<template>
<style>
:host {
display: block;
will-change: transform;
transform: translateZ(0);
}
#counter {
@apply(--layout-horizontal);
}
</style>
<div hidden>
<content id="content" select="*"></content>
</div>
<div id="counter">
<template is="dom-repeat" items="{{titles}}">
<div class="counter-box" hidden$="{{_computeHide(index, displayDigits)}}">
<div class="holder active"></div>
<div class="holder prev"></div>
<h2>{{item}}</h2>
</div>
</template>
</div>
</template>
<script>
(function() {
var _TRANSITION_TIME = 400;
Polymer({
is: 'countdown-timer',
properties: {
/**
* True if the countdown should be active.
*/
active: {
type: Boolean,
value: false,
notify: true
},
/**
* True if the countdown should animate.
*/
animate: {
type: Boolean,
value: true
},
/**
* The end time for this countdown, in milliseconds.
*/
countdownEndAt: {
type: Number,
value: 1450951200000, // Dec 24, 2015,
observer: '_countdownEndAtChanged'
},
/**
* Santa service.
*/
santaService: {
type: Object,
value: null
},
/**
* The number of digits to display.
*/
displayDigits: {
type: Number,
value: 8
},
/**
* True when the countdown has reached `countdownEndAt`.
*/
finished: {
value: false,
reflectToAttribute: true,
notify: true,
readOnly: true
},
/**
* The current time as dictated by the `santaService`.
*/
currentTime: {
type: Number,
value: null,
readOnly: true
},
/**
* The DOM nodes containing countdown titles, such as Days, Hours,
* Minutes, Seconds etc.
*/
titles: {
type: Array,
value: function() { return []; },
readOnly: true
},
/**
* The `.counter-box` elements containing countdown nodes.
*/
holders: {
type: Array,
value: function() { return []; },
readOnly: true
}
},
_oldNumbers: [], // replaced in tickTock
_tickInterval: null,
ready: function() {
this._setCurrentTime(this._getCurrentTime());
},
attached: function() {
this.updateLabels();
this.async(function() {
// Needs to wait one rAF so titles has populated the template repeat.
this._setHolders(Array.prototype.slice.call(
this.$.counter.querySelectorAll('.counter-box')));
this.fire('countdown-timer-ready', this);
// Set the initial time but don't start
this.tickTock();
});
},
updateLabels: function() {
var titles = this.getContentChildNodes().map(function(node) {
return node.textContent;
});
this._setTitles(titles);
},
_countdownEndAtChanged: function() {
// string - number type cohesion works, but let's be explicit when
// we get a new countdownEndAt.
this.countdownEndAt = parseInt(this.countdownEndAt);
},
/**
* @return {Object.<string>}
*/
_getCurrentTime: function() {
var now = this.santaService ? this.santaService.now() : Date.now();
var diff = Math.max(0, this.countdownEndAt - now);
var msPerDay = 24 * 60 * 60 * 1000;
var daysX = diff / msPerDay;
var days = Math.floor(daysX);
var hoursX = (daysX - days) * 24;
var hours = Math.floor(hoursX);
var minsX = (hoursX - hours) * 60;
var mins = Math.floor(minsX);
var secondsX = (minsX - mins) * 60;
var seconds = Math.floor(secondsX);
return {diff: diff, days: days, hours: hours, mins: mins, seconds: seconds};
},
tickTock: function() {
this._setCurrentTime(this._getCurrentTime());
var days = this.currentTime.days;
var hours = this.currentTime.hours;
var mins = this.currentTime.mins;
var seconds = this.currentTime.seconds;
var numbers = [padDigits(days), padDigits(hours), padDigits(mins),
padDigits(seconds)];
this.displayDigits = (days == 0) ? 6 : 8
// Ensure holders exist. Solves inconsistent asyncs.
if (this.holders.length) {
for (var i = 0, holder; holder = this.holders[i]; ++i) {
this.setValue(holder, this._oldNumbers[i], numbers[i]);
}
this._oldNumbers = numbers;
}
// TODO: this is not i18n'd.
var msg = [];
if (this.currentTime.diff <= 60 * 1000) {
msg.push(this.currentTime.seconds);
msg = msg.join(' ');
} else {
msg.push(this.currentTime.days, this.currentTime.days == 1 ? 'day' : 'days');
msg.push(this.currentTime.hours, this.currentTime.hours == 1 ? 'hour' : 'hours');
msg.push(this.currentTime.mins, this.currentTime.mins == 1 ? 'minute' : 'minutes');
msg.push(this.currentTime.seconds, this.currentTime.seconds == 1 ? 'second' : 'seconds');
msg = msg.join(' ') + ' until Santa departs'
}
this.fire('countdown-timer-tick', {
currentTime: this.currentTime,
msg: msg
});
if (this.currentTime.diff == 0) {
this.fire('countdown-timer-finish', this);
this._setFinished(true);
this.stop();
}
},
/**
* Sets a value for the passed `.counter-box` node.
*/
setValue: function(node, oldVal, val) {
if (oldVal === val) {
return;
}
// NOTE: Assumes child layout.
node.children[0].textContent = val || '';
if (this.animate) {
node.children[1].textContent = oldVal || '';
node.classList.add('anim');
this.async(function() {
node.classList.remove('anim');
}, _TRANSITION_TIME * 2);
}
},
/**
* Sets a value for the digit node.
*
* @param {Element} el
* @param {string|undefined} oldVal
* @param {string} val
*/
setDigit: function(digit, oldVal, val) {
if (oldVal == val) {
return;
}
var digitTopFront = digit.querySelector('.digit-top.digit-front');
var digitBottomBehind = digit.querySelector('.digit-bottom.digit-behind');
var digitTopBehind = digit.querySelector('.digit-top.digit-behind');
var digitBottomFront = digit.querySelector('.digit-bottom.digit-front');
digitTopBehind.firstElementChild.textContent = val;
digitBottomFront.firstElementChild.textContent = val;
if (oldVal === undefined) {
digitTopFront.firstElementChild.textContent = val;
digitBottomBehind.firstElementChild.textContent = val;
// No animation for the first instance.
return;
}
digit.classList.add('digit-anim');
this.async(function() {
digit.classList.remove('digit-anim');
digitTopFront.firstElementChild.textContent = val;
digitBottomBehind.firstElementChild.textContent = val;
}, _TRANSITION_TIME * 2);
},
/**
* Stop the countdown
*/
stop: function() {
window.clearInterval(this._tickInterval);
this.active = false;
},
/**
* Start the countdown
*/
start: function() {
if (this.active) {
return;
}
this.active = true;
this.tickTock();
this._tickInterval = window.setInterval(this.tickTock.bind(this), 1000);
},
/**
* @return boolean
*/
isFinished: function() {
return this.currentTime.diff <= 0;
},
_computeHide: function(i, displayDigits) {
return i == 0 && displayDigits < 8;
}
});
})();
</script>
</dom-module>