/
gaia-picker-time.js
338 lines (293 loc) · 7.68 KB
/
gaia-picker-time.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
;(function(define){'use strict';define(function(require,exports,module){
/*jshint esnext:true*/
/*shint node:true*/
require('gaia-picker');
var DEBUG = 0;
/**
* Detects presence of shadow-dom
* CSS selectors.
*
* @return {Boolean}
*/
var hasShadowCSS = (function() {
try { document.querySelector(':host'); return true; }
catch (e) { return false; }
})();
/**
* Element prototype, extends from HTMLElement
*
* @type {Object}
*/
var proto = Object.create(HTMLElement.prototype);
var on = function(el, name, fn, ctx) {
el.addEventListener(name, fn.bind(ctx));
};
/**
* Called when the element is first created.
*
* Here we create the shadow-root and
* inject our template into it.
*
* @private
*/
proto.createdCallback = function() {
var now = new Date();
this.createShadowRoot();
this.shadowRoot.innerHTML = template;
this.shadowStyleHack();
// Get els
this.els = {
inner: this.shadowRoot.querySelector('.inner'),
pickers: {
hours: this.shadowRoot.querySelector('.hours'),
minutes: this.shadowRoot.querySelector('.minutes'),
ampm: this.shadowRoot.querySelector('.ampm')
}
};
this.setPickerHeights();
this.format = this.getAttribute('format') || (navigator.mozHour12 ? '12hr' : '24hr');
this.minutes = this.getAttribute('minutes') || now.getMinutes();
this.hours = this.getAttribute('hours') || now.getHours();
setTimeout(this.addListeners.bind(this));
};
proto.attributeChangedCallback = function(attr, from, to) {
if (this.attrs[attr]) { this[attr] = to; }
};
proto.addListeners = function() {
var pickers = this.els.pickers;
on(pickers.minutes, 'changed', this.onMinutesChanged, this);
on(pickers.hours, 'changed', this.onHoursChanged, this);
on(pickers.ampm, 'changed', this.onAmPmChanged, this);
};
proto.onHoursChanged = function(e) {
e.stopPropagation();
this.hours = this.normalizeHours(e.detail.value);
};
proto.onMinutesChanged = function(e) {
e.stopPropagation();
this.minutes = e.detail.value;
};
proto.onAmPmChanged = function(e) {
e.stopPropagation();
if (!e.detail.value) { return; }
this.ampm = e.detail.value === 'PM' ? 1 : 0;
this.hours = this.normalizeHours(this.hours);
};
proto.setPickerHeights = function() {
var height = this.style.height;
this.els.pickers.hours.style.height = height;
this.els.pickers.minutes.style.height = height;
this.els.pickers.ampm.style.height = height;
debug('set picker heights: %s', height);
};
/**
* Populate the hour and minute
* pickers based on the current
* format.
*
* @private
*/
proto.populate = function() {
var startHour = this.is12 ? 1 : 0;
var endHour = this.is12 ? (startHour + 12) : (startHour + 12 * 2);
this.els.pickers.hours.items = createList(startHour, endHour);
this.els.pickers.minutes.items = createList(0, 60, function(value) {
return value < 10 ? '0' + value : value;
});
};
/**
* Returns a zero padded, human
* readable time string in 24hr
* format.
*
* @return {String}
* @public
*/
proto.getTimeValue = function() {
return (this.hours < 10 ? '0' : '') + this.hours + ':' + this.minutes;
};
/**
* Converts given number of hours
* to 24hr format based on the
* ampm value.
*
* @param {String|Number} hours
* @return {Number}
* @private
*/
proto.normalizeHours = function(hours) {
hours = Number(hours);
if (!this.is12) { return hours; }
var offset = hours % 12;
hours = this.ampm ? offset + 12 : offset;
return hours === 24 ? 0 : hours;
};
/**
* Calulate the correct hour
* index when in 12hr format.
*
* Example:
*
* // 12hr
* this.hourIndex(1); => 0
* this.hourIndex(0); => 11
*
* // 24hr (noop)
* this.hoursIndex(23); => 23
* this.hoursIndex(0); => 0
*
* @param {Number|String} hours
* @return {Number}
* @private
*/
proto.hourIndex = function(hours) {
hours = Number(hours);
if (!this.is12) { return hours; }
var hour = hours % 12;
return (hour === 0 ? 12 : hour) - 1;
};
/**
* It's useful to have attributes duplicated
* on a node inside the shadow-dom so that
* we can use them for style-hooks.
*
* @param {String} name
* @param {String} value
* @public
*/
proto.setAttribute = function(name, value) {
this.els.inner.setAttribute.call(this, name, value);
this.els.inner.setAttribute(name, value);
};
proto.shadowStyleHack = function() {
if (hasShadowCSS) { return; }
var style = this.shadowRoot.querySelector('style').cloneNode(true);
this.classList.add('-content', '-host');
style.setAttribute('scoped', '');
this.appendChild(style);
};
proto.attrs = {
hours: {
get: function() { return this._hours; },
set: function(value) {
debug('set hours: %s', value, this.hours);
value = Number(value);
if (value === this.hours) { return; }
this.ampm = value >= 12 ? 1 : 0;
this._hours = value;
// Update pickers last
this.setAttribute('hours', value);
this.els.pickers.hours.select(this.hourIndex(value));
this.els.pickers.ampm.select(this.ampm);
}
},
minutes: {
get: function() { return this._minutes; },
set: function(value) {
debug('set minutes: %s', value);
value = Number(value);
if (value === this.minutes) { return; }
this._minutes = value;
this.els.pickers.minutes.select(value);
this.setAttribute('minutes', value);
}
},
format: {
get: function() { return this._format; },
set: function(value) {
if (value == this.format) { return; }
this.is12 = value === '12hr';
this.populate();
this.setAttribute('format', value);
this._format = value;
}
}
};
Object.defineProperties(proto, proto.attrs);
var template = `
<style>
:host {
display: block;
position: relative;
overflow: hidden;
-moz-user-select: none;
}
.inner {
display: flex;
height: 100%;
}
gaia-picker {
flex: 1;
}
gaia-picker:not(:first-child):after {
content: '';
position: absolute;
left: 0; top: 0;
z-index: -1;
width: 1px;
height: 100%;
background: var(--border-color);
}
[format='24hr'] .ampm {
display: none;
}
.colon {
position: absolute;
left: 33.33%;
top: 50%;
z-index: 3;
width: 10px;
height: 50px;
font-size: 30px;
font-weight: 500;
font-style: italic;
line-height: 48px;
margin-top: -25px;
margin-left: -5px;
text-align: center;
color: var(--highlight-color);
}
[format='24hr'] .colon {
left: 50%;
}
</style>
<div class="inner">
<gaia-picker class="hours" circular></gaia-picker>
<div class="colon">:</div>
<gaia-picker class="minutes" circular></gaia-picker>
<gaia-picker class="ampm">
<li>AM</li>
<li>PM</li>
</gaia-picker>
</div>`;
// If the browser doesn't support shadow-css
// selectors yet, we update the template
// to use the shim classes instead.
if (!hasShadowCSS) {
template = template
.replace('::content', 'gaia-picker-time.-content', 'g')
.replace(':host', 'gaia-picker-time.-host', 'g');
}
function createList(min, max, format) {
var list = [];
for (var i = min; i < max; ++i) {
list.push(format ? format(i) : i);
}
return list;
}
// Register and return the constructor
// and expose `protoype` (bug 1048339)
module.exports = document.registerElement('gaia-picker-time', { prototype: proto });
module.exports.protoype = module.exports.protoype || proto;
var debug;
module.exports.debug = function(enabled) {
debug = enabled ? function() {
arguments[0] = '[gaia-picker-time] ' + arguments[0];
console.log.apply(console, arguments);
} : function(){};
};
module.exports.debug(DEBUG);
});})(typeof define=='function'&&define.amd?define
:(function(n,w){'use strict';return typeof module=='object'?function(c){
c(require,exports,module);}:function(c){var m={exports:{}};c(function(n){
return w[n];},m.exports,m);w[n]=m.exports;};})('gaia-picker-time',this));