/
gaia-picker-date.js
972 lines (878 loc) · 22.9 KB
/
gaia-picker-date.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
;(function(define,n){'use strict';define(function(require,exports,module){
/*jshint laxbreak:true*/
/*jshint esnext:true*/
/*shint node:true*/
require('gaia-picker');
var DEBUG = 0;
/**
* Element prototype, extends from HTMLElement
*
* @type {Object}
*/
var proto = Object.create(HTMLElement.prototype);
/**
* Default max/min dates.
*
* @type {Object}
*/
var defaults = {
min: new Date('1900', '0', '01'),
max: new Date('2099', '11', '31')
};
/**
* Called when the element is first created.
*
* Here we create the shadow-root and
* inject our template into it.
*
* @private
*/
proto.createdCallback = function() {
this.createShadowRoot();
this.shadowRoot.innerHTML = template;
this.shadowStyleHack();
// Get els
this.els = {
inner: this.shadowRoot.querySelector('.inner'),
pickers: {
day: this.shadowRoot.querySelector('.days'),
month: this.shadowRoot.querySelector('.months'),
year: this.shadowRoot.querySelector('.years')
}
};
this.setInitialValues();
this.updatePickerOrder();
this.setPickerHeights();
this.updatePickers();
this.created = true;
// Bind listeners later to avoid strange
// things happening during setup.
setTimeout(this.addListeners.bind(this));
};
proto.attachedCallback = function() {
debug('attached');
};
proto.detachedCallback = function() {
debug('detached');
};
/**
* Derives an initial value for the picker.
*
* Use `value` attribute if given.
* Default to using today's date.
*
* @private
*/
proto.setInitialValues = function() {
var value = this.getAttribute('value');
var min = this.getAttribute('min');
var max = this.getAttribute('max');
this.min = min || defaults.min;
this.max = max || defaults.max;
this.value = value || new Date();
debug('initial value: %s', this.value);
};
/**
* Adds change listeners to allow
* the date picker to respond when
* pickers change.
*
* (Pickers change when users
* interact with them)
*
* @private
*/
proto.addListeners = function() {
var pickers = this.els.pickers;
pickers.year.addEventListener('changed', this.onYearChanged.bind(this));
pickers.month.addEventListener('changed', this.onMonthChanged.bind(this));
pickers.day.addEventListener('changed', this.onDayChanged.bind(this));
};
/**
* Sets the year to match the value
* currently shown on the picker.
*
* @param {Event} e
* @private
*/
proto.onYearChanged = function(e) {
debug('year changed to index: %s', e.detail.index);
this.setYear(this.yearIndexToValue(e.detail.index));
};
/**
* Sets the month to match the value
* currently shown on the picker.
*
* @param {Event} e
* @private
*/
proto.onMonthChanged = function(e) {
debug('month changed: %s', e.detail.index);
this.setMonth(this.monthIndexToValue(e.detail.index));
};
/**
* Sets the day to match the value
* currently shown on the picker.
*
* @param {Event} e
* @private
*/
proto.onDayChanged = function(e) {
debug('day changed: %s', e.detail.index);
this.setDay(this.dayIndexToValue(e.detail.index));
};
proto.yearIndexToValue = function(index) {
return this.min.getFullYear() + index;
};
proto.yearValueToIndex = function(value) {
return value - this.min.getFullYear();
};
/**
* Converts a given month picker
* item index to month value.
*
* @param {Number} index
* @return {Number}
*/
proto.monthIndexToValue = function(index) {
return this.isMinYear() ? (this.min.getMonth() + index) : index;
};
/**
* Converts a given month value
* to the picker item index.
*
* @param {Number} value
* @return {Number}
*/
proto.monthValueToIndex = function(value) {
return this.isMinYear() ? (value - this.min.getMonth()) : value;
};
/**
* Converts a given day picker
* item index to day value.
*
* @param {Number} index
* @return {Number}
*/
proto.dayIndexToValue = function(index) {
return this.isMinMonth() ? this.min.getDate() + index : (index + 1);
};
/**
* Converts a given day value
* to the picker item index.
*
* @param {Number} value
* @return {Number}
*/
proto.dayValueToIndex = function(value) {
return this.isMinMonth() ? value - this.min.getDate() : value - 1;
};
/**
* Set the date-picker to a given year.
*
* Changing the year has several dependencies.
* We normalize the month and day values to
* new values that fit within the context
* of the new year.
*
* We manually update the year, month and
* day values, then udate the pickers.
*
* The day and month pickers need to fully
* updated as the list contents may have
* changed due to entering max/min date
* range year or leap year changing
* number of days in the month.
*
* @param {Number} year
* @public
*/
proto.setYear = function(year) {
debug('set year: %s', year);
if (!year) { return; }
year = this.normalizeYear(year);
if (year === this.value.getFullYear()) { return debug('didn\'t change'); }
var month = this.normalizeMonth(this.value.getMonth(), { year: year });
var day = this.normalizeDay(this.value.getDate(), { month: month, year: year });
this.value.setDate(day);
this.value.setMonth(month);
this.value.setFullYear(year);
this.updateYearPickerValue();
this.updateMonthPicker();
this.updateDayPicker();
};
/**
* Set the date-picker to a given month.
*
* The day picker is dependent on the month
* value so we must normalize the current
* day value to a value that fits into
* the new date context.
*
* We then set the day first, followed by
* the month. The order is important as
* setting a day that doesn't exist will
* cause the month to roll-over.
*
* @param {Number} month
* @public
*/
proto.setMonth = function(month) {
debug('set month: %s', month);
if (isNaN(month)) { return; }
month = this.normalizeMonth(month);
if (month === this.value.getMonth()) { return debug('didn\'t change'); }
var day = this.normalizeDay(this.value.getDate(), { month: month });
this.value.setDate(day);
this.value.setMonth(month);
this.updateMonthPickerValue();
this.updateDayPicker();
};
/**
* Sets the underlying day value
* and updates the day picker.
*
* If the day didn't change,
* nothing is done.
*
* Normalizing the day prevents a day
* being set that is either out of range
* or doesn't exist in the calendar.
*
* @param {Number} day
* @public
*/
proto.setDay = function(day) {
debug('set day: %s');
if (!day) { return; }
day = this.normalizeDay(day);
if (day === this.value.getDate()) { return debug('didn\'t change'); }
this.value.setDate(day);
this.updateDayPickerValue();
};
/**
* Normalizes value to a 'valid' year.
*
* A 'valid' year is a year within
* the picker's date-range.
*
* @param {Number} year
* @return {Number}
* @private
*/
proto.normalizeYear = function(year) {
var max = this.max.getFullYear();
var min = this.min.getFullYear();
return Math.max(min, Math.min(max, Number(year)));
};
/**
* Normalizes value to a 'valid' month.
*
* A 'valid' month is a month within the
* current (or given) year, and be within
* the picker's current date-range.
*
* Options:
*
* - `year` {Number} - The year to normalize to
*
* @param {Number} month
* @param {Object} options
* @return {Number}
* @private
*/
proto.normalizeMonth = function(month, options) {
var year = options && options.year;
month = Number(month);
month = Math.min(11, Math.max(month, 0));
if (this.isMinYear(year)) { month = Math.max(this.min.getMonth(), month); }
if (this.isMaxYear(year)) { month = Math.min(this.max.getMonth(), month); }
return month;
};
/**
* Normalizes a value to a 'valid' day.
*
* A 'valid' day is a day that exist
* in the current (or given) month of
* the current (or given) year, and
* within the picker's date-range.
*
* Options:
*
* - `year` {Number} - The year to normalize to
* - `month` {Number} - The month to normalize to
*
* @param {Number} day
* @param {Object} options
* @return {Number}
* @private
*/
proto.normalizeDay = function(day, options) {
var year = (options && options.year) || this.value.getFullYear();
var month = (options && options.month) || this.value.getMonth();
var total = getDaysInMonth(year, month);
day = Math.max(0, Math.min(Number(day), total));
if (this.isMinMonth(month, year)) { day = Math.max(this.min.getDate(), day); }
if (this.isMaxMonth(month, year)) { day = Math.min(this.max.getDate(), day); }
return day;
};
/**
* Update all the picker's content
* and values to match the current
* date-picker's date value.
*
* @private
*/
proto.updatePickers = function() {
this.updateYearPicker();
this.updateMonthPicker();
this.updateDayPicker();
};
/**
* Refreshes the year list based on
* the current max/min dates, only
* if it changed.
*
* @private
*/
proto.updateYearPicker = function() {
debug('update year picker');
if (!this.min || !this.max) { return; }
var list = this.createYearList();
var picker = this.els.pickers.year;
var firstItem = picker.items[0];
var changed = list.length !== picker.length
|| firstItem !== list[0];
if (!changed) { return debug('didn\'t change'); }
picker.items = list;
this.updateYearPickerValue();
debug('year picker updated', list);
};
/**
* Create a list of month, fill the month
* picker and update the picker value.
*
* We make an assumption that the list
* didn't change if the length remains
* the same as the picker's and the
* `textContent` of the first item
* is the same.
*
* @private
*/
proto.updateMonthPicker = function() {
debug('update months picker');
if (!this.value) { return; }
var picker = this.els.pickers.month;
var list = this.createMonthList();
var firstItem = picker.items[0];
var trimmed = list.length < 12;
var changed = list.length !== picker.length
|| firstItem !== list[0];
if (!changed) { return debug('didn\'t change'); }
picker.items = list;
picker.circular = !trimmed;
this.updateMonthPickerValue();
debug('months picker updated', list);
};
/**
* Create a list of days, fill the day
* picker and update the picker value.
*
* We make an assumption that the list
* didn't change if the length remains
* the same and the first value is the
* same.
*
* @private
*/
proto.updateDayPicker = function() {
debug('update days');
if (!this.value) { return; }
var picker = this.els.pickers.day;
var days = this.createDayList();
var list = days.value;
var firstItem = picker.items[0];
var changed = list.length !== picker.length
|| firstItem !== list[0];
if (!changed) { return debug('days didn\'t change'); }
picker.circular = !days.trimmed;
picker.items = list;
this.updateDayPickerValue();
debug('day picker updated', days);
};
/**
* Updates the year picker to match
* the current year value.
*
* @private
*/
proto.updateYearPickerValue = function() {
debug('update year picker value');
if (!this.value) { return; }
var year = this.value.getFullYear();
var index = this.yearValueToIndex(year);
this.els.pickers.year.select(index);
debug('year picker index updated: %s', index);
};
/**
* Updates the month picker to match
* the current month value.
*
* If the year is the min year, then Jan
* may not be the first month, therefore
* indexes need to be adjusted.
*
* We only animate the selection if the
*
* @private
*/
proto.updateMonthPickerValue = function() {
debug('update month picker value');
if (!this.value) { return; }
var month = this.value.getMonth();
var picker = this.els.pickers.month;
var index = this.monthValueToIndex(month);
var animate = picker.value !== picker.items[index];
picker.select(index, { animate: animate });
debug('month picker index updated: %s', index);
};
/**
* Updates the month picker to match
* the current month value.
*
* This won't do anything if triggered
* from the 'changed' callback.
*
* @private
*/
proto.updateDayPickerValue = function() {
if (!this.value) { return; }
var value = this.value.getDate();
var picker = this.els.pickers.day;
var index = this.dayValueToIndex(value);
var animate = picker.value !== picker.items[index];
picker.select(index, { animate: animate });
debug('day picker index updated: %s', index, animate);
};
/**
* States if the current (or given) year
* is the year of the min date-range.
*
* @param {Number} year (optional)
* @return {Boolean}
* @private
*/
proto.isMinYear = function(year) {
return (year || this.value.getFullYear()) === this.min.getFullYear();
};
/**
* States if the current (or given) year
* is the year of the max date-range.
*
* @param {Number} year (optional)
* @return {Boolean}
* @private
*/
proto.isMaxYear = function(year) {
return (year || this.value.getFullYear()) === this.max.getFullYear();
};
/**
* States if the current (or given) month
* is the month of the max date-range.
*
* @param {Number} month (optional)
* @param {Number} year (optional)
* @return {Boolean}
* @private
*/
proto.isMinMonth = function(month, year) {
return this.isMinYear(year) && (month || this.value.getMonth()) === this.min.getMonth();
};
/**
* States if the current (or given) month
* is the month of the max date-range.
*
* @param {Number} month (optional)
* @param {Number} year (optional)
* @return {Boolean}
* @private
*/
proto.isMaxMonth = function(month, year) {
return this.isMaxYear(year) && (month || this.value.getMonth()) === this.max.getMonth();
};
/**
* Create a list of year strings
* based on the date-picker's current
* date-range.
*
* @return {Array} ['2000', '2001', '2002', ...]
*/
proto.createYearList = function() {
var min = this.min.getFullYear();
var max = this.max.getFullYear();
var list = [];
var date;
for (var year = min; year <= max; year++) {
date = new Date(year, 0 , 1, 1);
list.push(localeFormat(date, '%Y'));
}
return list;
};
/**
* Create a list of localized month strings
* based on the date range of the current year.
*
* Usually this will be 12 months, unless
* the year is currently the max/min year,
* then not all the months will be available.
*
* @return {Array} ['Jan', 'Feb', 'Mar', ...]
*/
proto.createMonthList = function() {
var date = new Date(this.value.getFullYear(), 0, 1, 1);
var min = this.isMinYear() ? this.min.getMonth() : 0;
var max = this.isMaxYear() ? this.max.getMonth() : 11;
var list = [];
for (var month = min; month <= max; month++) {
date.setMonth(month);
list.push(localeFormat(date, '%b'));
}
return list;
};
/**
* Create a list of day strings based
* on the days available in the current
* month and date-range.
*
* @return {Array} ['1', '2', '3', ...]
*/
proto.createDayList = function() {
var month = this.value.getMonth();
var year = this.value.getFullYear();
var daysInMonth = getDaysInMonth(year, month);
var last = this.isMaxMonth() ? this.max.getDate() : daysInMonth;
var first = this.isMinMonth() ? this.min.getDate() : 1;
var list = [];
for (var i = first; i <= last; i++) {
var date = new Date(year, month, i);
list.push(localeFormat(date, '%d'));
}
var trimmed = daysInMonth !== list.length;
return {
value: list,
trimmed: trimmed
};
};
/**
* Explictly set's the picker heights based
* on the `style.height` of the component.
*
* We give users an opportunity to define
* an explicit height to optimize setup
* so that we don't have to read
* from the DOM.
*
* Example:
*
* <gaia-picker-date style="height:300px">
*
* @private
*/
proto.setPickerHeights = function() {
var height = this.style.height;
this.els.pickers.day.style.height = height;
this.els.pickers.month.style.height = height;
this.els.pickers.year.style.height = height;
debug('set picker heights: %s', height);
};
/**
* Updates the order of the three
* pickers to reflect the current
* date format.
*
* @private
*/
proto.updatePickerOrder = function() {
var order = getDateComponentOrder();
order.forEach(function(type, i) {
this.els.pickers[type].style.order = i;
}, this);
};
/**
* 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);
};
/**
* Clamp's given date between
* max/min date range.
*
* @param {Date} date
* @return {Date}
* @private
*/
proto.clampDate = function(date) {
if (date > this.max) { return new Date(this.max.getTime()); }
else if (date < this.min) { return new Date(this.min.getTime()); }
else { return date; }
};
/**
* When an attribute changes, we check
* to see if it was one of the component's
* defined attributes, and then set it
* to the newly changed value.
*
* @param {String} key
* @param {String} from
* @param {String} to
* @private
*/
proto.attributeChangedCallback = function(key, from, to) {
if (attributes[key]) { this[key] = to; }
};
/**
* List of public attributes.
*
* @type {Object}
*/
var attributes = {
value: {
get: function() { return this._value; },
set: function(value) {
if (!value) { return; }
var date = typeof value === 'string' ? stringToDate(value) : value;
var clamped = this.clampDate(date);
this._value = clamped;
// Only update pickers if fully created
if (this.created) { this.updatePickers(); }
}
},
min: {
get: function() { return this._min; },
set: function(value) {
if (!value) { return; }
var date = typeof value === 'string' ? stringToDate(value) : value;
this._min = date;
// Only update pickers if fully created
if (this.created) { this.updatePickers(); }
}
},
max: {
get: function() { return this._max; },
set: function(value) {
if (!value) { return; }
var date = typeof value === 'string' ? stringToDate(value) : value;
this._max = date;
// Only update pickers if fully created
if (this.created) { this.updatePickers(); }
}
}
};
// Define the public attributes as properties
Object.defineProperties(proto, attributes);
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);
};
var template = `
<style>
:host {
display: block;
position: relative;
overflow: hidden;
-moz-user-select: none;
}
.inner {
display: flex;
height: 100%;
}
gaia-picker {
flex: 1;
}
.divider {
content: '';
position: absolute;
top: 0;
z-index: -1;
width: 1px;
height: 100%;
background: var(--border-color);
}
.divider-1 { left: 33.33%; }
.divider-2 { left: 66.66%; }
</style>
<div class="inner">
<div class="divider divider-1"></div>
<div class="divider divider-2"></div>
<gaia-picker class="days" circular></gaia-picker>
<gaia-picker class="months" circular></gaia-picker>
<gaia-picker class="years"></gaia-picker>
</div>`;
/**
* Detects presence of shadow-dom
* CSS selectors.
*
* @return {Boolean}
*/
var hasShadowCSS = (function() {
try { document.querySelector(':host'); return true; }
catch (e) { return false; }
})();
// 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-date.-content', 'g')
.replace(':host', 'gaia-picker-date.-host', 'g');
}
/**
* Utils
*/
/**
* Get the number of days in a month.
*
* @param {Number} year Full year
* @param {Number} month Month index
* @return {Number}
*/
function getDaysInMonth(year, month) {
return new Date(year, month + 1, 0).getDate();
}
/**
* Format a `Date` based on the given token.
*
* `navigator.mozL10n.DateTimeFormat` is used
* if available, falling back to simple en-us
* solution.
*
* Example:
*
* localeFormat(new Date(2014, 09, 21), '%A'); //=> Tuesday
* localeFormat(new Date(2014, 09, 21), '%b'); //=> Oct
*
* @param {Date} date
* @param {String} token ['%b','%A','%Y','%d']
* @return {String}
* @private
*/
function localeFormat(date, token) {
return localeFormat.mozL10n(date, token)
|| localeFormat.fallback(date, token);
}
/**
* Wrapper around `navigator.mozL10n.DateTimeFormat`
*
* @param {Date} date
* @param {String} token ['%b','%A','%Y','%d']
* @return {String}
* @private
*/
localeFormat.mozL10n = function(date, token) {
var formatter = navigator.mozL10n && navigator.mozL10n.DateTimeFormat;
if (formatter) { return formatter().localeFormat(date, token); }
};
/**
* A fallback for `navigator.mozL10n.DateTimeFormat`
*
* @param {Date} date
* @param {String} token ['%b','%A','%Y','%d']
* @return {String}
* @private
*/
localeFormat.fallback = function(date, token) {
var strings = {
days: ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
months: ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
};
switch (token) {
case '%b': return strings.months[date.getMonth()];
case '%A': return strings.days[date.getDay()];
case '%Y': return String(date.getFullYear());
case '%d': return String(date.getDate());
}
};
/**
* Convert a string to a `Date` object.
*
* @param {String} string 1970-01-01
* @return {Date}
*/
function stringToDate(string) {
if (!string) { return null; }
var parts = string.split('-');
var date = new Date(parts[0], parseInt(parts[1]) - 1, parts[2]);
if (isNaN(date.getTime())) { date = null; }
return date;
}
/**
* Derives the date picker order based
* on the current date format.
*
* @return {Array}
*/
function getDateComponentOrder() {
var format = getDateTimeFormat();
var tokens = format.match(/(%E.|%O.|%.)/g);
var fallback = ['day', 'month', 'year'];
var order = [];
if (tokens) {
tokens.forEach(function(token) {
switch (token) {
case '%Y':
case '%y':
case '%Oy':
case 'Ey':
case 'EY':
order.push('year');
break;
case '%B':
case '%b':
case '%m':
case '%Om':
order.push('month');
break;
case '%d':
case '%e':
case '%Od':
case '%Oe':
order.push('day');
break;
}
});
}
return order.length === 3 ? order : fallback;
}
function getDateTimeFormat() {
return navigator.mozL10n
&& navigator.mozL10n.get
&& navigator.mozL10n.get('dateTimeFormat_%x') || '%m/%d/%Y';
}
// Register and return the constructor
// and expose `protoype` (bug 1048339)
module.exports = document.registerElement('gaia-picker-date', { prototype: proto });
module.exports.proto = proto;
var debug;
module.exports.debug = function(enabled) {
debug = enabled ? function() {
arguments[0] = '[gaia-picker-date] ' + arguments[0];
console.log.apply(console, arguments);
} : function(){};
};
module.exports.debug(DEBUG);
},n);})(typeof define=='function'&&define.amd?define
:(function(w){'use strict';return typeof module=='object'?function(c){
c(require,exports,module);}:function(c,n){var m={exports:{}};c(function(n){
return w[n];},m.exports,m);w[n]=m.exports;};})(this), 'gaia-picker-date');