-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmtr-datepicker.js
executable file
·2131 lines (1758 loc) · 65.1 KB
/
mtr-datepicker.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
/**
* The main class of the MtrDatepicker
* Here inside is covered everything that you need to know
*
* @class MtrDatepicker
* @param {Object} inputConfig used for user configurations
*/
// eslint-disable-next-line no-unused-vars
function MtrDatepicker (inputConfig) {
// The real implementation of the library starts here
// The main configuration properties
// All of them can be overridden by the init method
var config = {
targetElement: null,
defaultValues: {
hours: [],
minutes: [],
dates: [],
datesNames: [],
months: [],
years: []
},
hours: {
min: 1,
max: 12,
step: 1,
maxlength: 2
},
minutes: {
min: 0,
max: 50,
step: 10,
maxlength: 2
},
months: {
min: 0,
max: 11,
step: 1,
maxlength: 2
},
years: {
min: 2000,
max: 2030,
step: 1,
maxlength: 4
},
// Responsible for the transition of the sliders - animated or static
animations: true,
// Make auto switch between AM/PM when moving from 11AM to 12PM or backwards
smartHours: false,
// Validate the date to be only in the future
future: false,
// Disable the 12 hours time format and go to a full 24 hours experience
disableAmPm: false,
// perform the future validation after the date change
validateAfter: true,
// change the local timezone to a specific one
utcTimezone: 0,
transitionDelay: 100,
transitionValidationDelay: 500,
references: {
hours: null // Used to store references to the main elements
},
// Show the fields related with the date
datepicker: true,
// Show the fields related with the time
timepicker: true,
monthsNames: {
0: 'Jan',
1: 'Feb',
2: 'Mar',
3: 'Apr',
4: 'May',
5: 'Jun',
6: 'Jul',
7: 'Aug',
8: 'Sep',
9: 'Oct',
10: 'Nov',
11: 'Dec'
},
daysNames: {
0: 'Sun',
1: 'Mon',
2: 'Tue',
3: 'Wed',
4: 'Thu',
5: 'Fri',
6: 'Sat'
},
timezones: null
};
// The main element which holds the datepicker
var targetElement;
var values = {
date: null,
timestamp: null,
ampm: true
};
var browser = null;
// Here are the attached user events
var defaultChangeEventsCategories = {
all: [],
time: [],
date: [],
hour: [],
minute: [],
ampm: [],
day: [],
month: [],
year: []
};
var events = {
onChange: clone(defaultChangeEventsCategories),
beforeChange: clone(defaultChangeEventsCategories),
afterChange: clone(defaultChangeEventsCategories)
};
var plugins = {
};
// Keep the wheel scroll in a timeout
var wheelTimeout = null;
// Keep the arrow click in a timeout
var arrowTimeout = {};
/**
* The main init function which prepares the datepicker for use
*
* @param {Object} inputConfig used to setup datepicker specific features
*/
var init = function (inputConfig) {
browser = detectBrowser();
if (!validateInputConfig(inputConfig)) {
console.error('Initialization of the datepicker is blocked because of errors in the config.');
return;
}
setConfig(inputConfig);
targetElement = byId(config.targetElement);
setDatesRange();
createMarkup();
};
/**
* Detach all event listeners and clear the markup of the component
*/
var destroy = function () {
// Clear all attached events
Object.keys(events).forEach(function (eventType) {
Object.keys(events[eventType]).forEach(function (fieldEvent) {
events[eventType][fieldEvent] = [];
});
});
// Clear the markup
while (targetElement.firstChild) {
targetElement.removeChild(targetElement.firstChild);
}
}
/**
* Attaching the user input config settings to override the default one
*
* @param {Object} input user input settings
*/
var setConfig = function (input) {
config.targetElement = input.target;
config.animations = input.animations !== undefined ? input.animations : config.animations;
config.future = input.future !== undefined ? input.future : config.future;
config.validateAfter = input.validateAfter !== undefined ? input.validateAfter : config.validateAfter;
config.smartHours = input.smartHours !== undefined ? input.smartHours : config.smartHours;
config.disableAmPm = input.disableAmPm !== undefined ? input.disableAmPm : config.disableAmPm;
config.datepicker = input.datepicker !== undefined ? input.datepicker : config.datepicker;
config.timepicker = input.timepicker !== undefined ? input.timepicker : config.timepicker;
// Change the defaults if the AM/PM is disabled
if (config.disableAmPm) {
config.hours.min = 0;
config.hours.max = 23;
}
values.date = input.timestamp ? new Date(input.timestamp) : new Date();
values.date.setSeconds(0);
if (input.utcTimezone !== undefined) {
// We are sure that the timezones plugin is loaded because we've made a check in the input validation
plugins.timezones = new MtrDatepickerTimezones();
config.utcTimezone = plugins.timezones.getTimezone(input.utcTimezone);
} else {
config.utcTimezone = {
offset: input.utcTimezone !== undefined ? input.utcTimezone : (values.date.getTimezoneOffset() / 60 * -1)
};
}
var localTimezoneOffsetTimestamp = values.date.getTime() + (values.date.getTimezoneOffset() * 60 * 1000);
var timezoneOffsetTimestamp = localTimezoneOffsetTimestamp + (config.utcTimezone.offset * 60 * 60 * 1000);
values.date = new Date(timezoneOffsetTimestamp);
values.timestamp = values.date.getTime();
// Override minutes
config.minutes.min = (input.minutes !== undefined && input.minutes.min !== undefined) ? parseInt(input.minutes.min) : config.minutes.min;
config.minutes.max = (input.minutes !== undefined && input.minutes.max !== undefined) ? parseInt(input.minutes.max) : config.minutes.max;
config.minutes.step = (input.minutes !== undefined && input.minutes.step !== undefined) ? parseInt(input.minutes.step) : config.minutes.step;
// Override months
config.months.min = (input.months !== undefined && input.months.min !== undefined) ? parseInt(input.months.min) : config.months.min;
config.months.max = (input.months !== undefined && input.months.max !== undefined) ? parseInt(input.months.max) : config.months.max;
config.months.step = (input.months !== undefined && input.months.step !== undefined) ? parseInt(input.months.step) : config.months.step;
// Override years
config.years.min = (input.years !== undefined && input.years.min !== undefined) ? parseInt(input.years.min) : config.years.min;
config.years.max = (input.years !== undefined && input.years.max !== undefined) ? parseInt(input.years.max) : config.years.max;
config.years.step = (input.years !== undefined && input.years.step !== undefined) ? parseInt(input.years.step) : config.years.step;
// Init hours
config.defaultValues.hours = createRange(config.hours);
config.defaultValues.minutes = createRange(config.minutes);
config.defaultValues.months = createRange(config.months);
config.defaultValues.years = createRange(config.years);
};
var validateInputConfig = function (input) {
var result = true;
if (input.datepicker !== undefined && input.timepicker !== undefined) {
if (!input.datepicker && !input.timepicker) {
console.error('Invalid configuration: cannot disable both datepicker and timepicker at the same time.');
result = false;
}
}
if (input.minutes) {
// Validate data type
if (input.minutes.min !== undefined && !isNumber(input.minutes.min)) {
console.error('Invalid argument: minutes.min should be a number.');
result = false;
}
if (input.minutes.max !== undefined && !isNumber(input.minutes.max)) {
console.error('Invalid argument: minutes.max should be a number.');
result = false;
}
if (input.minutes.step !== undefined && !isNumber(input.minutes.step)) {
console.error('Invalid argument: minutes.step should be a number.');
result = false;
}
// Validate the range
if (input.minutes.min !== undefined && input.minutes.max !== undefined && input.minutes.max < input.minutes.min) {
console.error('Invalid argument: minutes.max should be larger than minutes.min.');
result = false;
}
if (input.minutes.min !== undefined &&
input.minutes.max !== undefined &&
input.minutes.step !== undefined &&
(input.minutes.step > (input.minutes.max - input.minutes.min))) {
console.error('Invalid argument: minutes.step should be less than minutes.max-minutes.min.');
result = false;
}
}
if (input.hours) {
// Validate data type
if (input.hours.min !== undefined && !isNumber(input.hours.min)) {
console.error('Invalid argument: hours.min should be a number.');
result = false;
}
if (input.hours.max !== undefined && !isNumber(input.hours.max)) {
console.error('Invalid argument: hours.max should be a number.');
result = false;
}
if (input.hours.step !== undefined && !isNumber(input.hours.step)) {
console.error('Invalid argument: hours.step should be a number.');
result = false;
}
// Validate the range
if (input.hours.min !== undefined && input.hours.max !== undefined && input.hours.max < input.hours.min) {
console.error('Invalid argument: hours.max should be larger than hours.min.');
result = false;
}
if (input.hours.min !== undefined &&
input.hours.max !== undefined &&
input.hours.step !== undefined &&
(input.hours.step > (input.hours.max - input.hours.min))) {
console.error('Invalid argument: hours.step should be less than hours.max-hours.min.');
result = false;
}
}
if (input.dates) {
// Validate data type
if (input.dates.min !== undefined && !isNumber(input.dates.min)) {
console.error('Invalid argument: dates.min should be a number.');
result = false;
}
if (input.dates.max !== undefined && !isNumber(input.dates.max)) {
console.error('Invalid argument: dates.max should be a number.');
result = false;
}
if (input.dates.step !== undefined && !isNumber(input.dates.step)) {
console.error('Invalid argument: dates.step should be a number.');
result = false;
}
// Validate the range
if (input.dates.min !== undefined && input.dates.max !== undefined && input.dates.max < input.dates.min) {
console.error('Invalid argument: dates.max should be larger than dates.min.');
result = false;
}
if (input.dates.min !== undefined &&
input.dates.max !== undefined &&
input.dates.step !== undefined &&
(input.dates.step > (input.dates.max - input.dates.min))) {
console.error('Invalid argument: dates.step should be less than dates.max-dates.min.');
result = false;
}
}
if (input.months) {
// Validate data type
if (input.months.min !== undefined && !isNumber(input.months.min)) {
console.error('Invalid argument: months.min should be a number.');
result = false;
}
if (input.months.max !== undefined && !isNumber(input.months.max)) {
console.error('Invalid argument: months.max should be a number.');
result = false;
}
if (input.months.step !== undefined && !isNumber(input.months.step)) {
console.error('Invalid argument: months.step should be a number.');
result = false;
}
// Validate the range
if (input.months.min !== undefined && input.months.max !== undefined && input.months.max < input.months.min) {
console.error('Invalid argument: months.max should be larger than months.min.');
result = false;
}
if (input.months.min !== undefined &&
input.months.max !== undefined &&
input.months.step !== undefined &&
(input.months.step > (input.months.max - input.months.min))) {
console.error('Invalid argument: months.step should be less than months.max-months.min.');
result = false;
}
}
if (input.years) {
// Validate data type
if (input.years.min !== undefined && !isNumber(input.years.min)) {
console.error('Invalid argument: years.min should be a number.');
result = false;
}
if (input.years.max !== undefined && !isNumber(input.years.max)) {
console.error('Invalid argument: years.max should be a number.');
result = false;
}
if (input.years.step !== undefined && !isNumber(input.years.step)) {
console.error('Invalid argument: years.step should be a number.');
result = false;
}
// Validate the range
if (input.years.min !== undefined && input.years.max !== undefined && input.years.max < input.years.min) {
console.error('Invalid argument: years.max should be larger than years.min.');
result = false;
}
if (input.years.min !== undefined &&
input.years.max !== undefined &&
input.years.step !== undefined && (input.years.step > (input.years.max - input.years.min))) {
console.error('Invalid argument: years.step should be less than years.max-years.min.');
result = false;
}
}
// Validate input timestamp
if (input.timestamp) {
// If the future dates is enabled, it will be a good idea to check the input timestamp, maybe it is in the past?
if (input.future) {
var timestampDate = new Date(input.timestamp);
var todayDate = new Date();
if (timestampDate.getTime() < todayDate.getTime()) {
console.error('Invalid argument: timestamp should be in the future if the future check is enabled.');
result = false;
}
}
}
if (input.utcTimezone !== undefined && typeof MtrDatepickerTimezones !== 'function') {
console.error('In order to use the timezones feature you should load the mtr-datepicker-timezones.min.js first.');
result = false;
}
// If there are any errors return a new target element with notice for the users
if (!result) {
targetElement = byId(input.target);
while (targetElement.firstChild) {
targetElement.removeChild(targetElement.firstChild);
}
var errorElement = document.createElement('div');
addClass(errorElement, 'mtr-error-message');
errorElement.appendChild(document.createTextNode('An error has occurred during the initialization of the datepicker.'));
targetElement.appendChild(errorElement);
}
return result;
};
var setDatesRange = function (month, year) {
month = month !== undefined ? month : getMonth();
year = year !== undefined ? year : getYear();
var datesRange = createRangeForDate(month, year);
config.dates = {
min: datesRange.min,
max: datesRange.max,
step: datesRange.step,
maxlength: 2
};
config.defaultValues.dates = datesRange.values;
config.defaultValues.datesNames = datesRange.names;
};
/**
* Generate the main markup used from the datepicker
* This means that here we are generating input sliders for hours, minutes, months, dates and years
* and a radio input for switching the time AM/PM
*/
var createMarkup = function () {
// Clear all of the content of the target element
removeClass(targetElement, 'mtr-datepicker');
addClass(targetElement, 'mtr-datepicker');
while (targetElement.firstChild) {
targetElement.removeChild(targetElement.firstChild);
}
// Create time elements
if (config.timepicker) {
var hoursElement = createSliderInput({
name: 'hours',
values: config.defaultValues.hours,
value: getHours()
});
var minutesElement = createSliderInput({
name: 'minutes',
values: config.defaultValues.minutes,
value: getMinutes()
});
var amPmElement;
if (!config.disableAmPm) {
amPmElement = createRadioInput({
name: 'ampm'
});
}
var rowTime = document.createElement('div');
rowTime.className = 'mtr-row';
var rowClearfixTime = document.createElement('div');
rowClearfixTime.className = 'mtr-clearfix';
rowTime.appendChild(hoursElement);
rowTime.appendChild(minutesElement);
if (!config.disableAmPm) {
rowTime.appendChild(amPmElement);
}
targetElement.appendChild(rowTime);
targetElement.appendChild(rowClearfixTime);
}
// Create date elements
if (config.datepicker) {
var monthElement = createSliderInput({
name: 'months',
values: config.defaultValues.months,
valuesNames: config.monthsNames,
value: getMonth()
});
var dateElement = createSliderInput({
name: 'dates',
values: config.defaultValues.dates,
valuesNames: config.defaultValues.datesNames,
value: getDate()
});
var yearElement = createSliderInput({
name: 'years',
values: config.defaultValues.years,
value: getYear()
});
var rowDate = document.createElement('div');
rowDate.className = 'mtr-row';
var rowClearfixDate = document.createElement('div');
rowClearfixDate.className = 'mtr-clearfix';
if (Array.isArray(config.datepicker)) {
// If provided use the desired order of the date fields
config.datepicker.forEach(function (field) {
switch (field) {
case 'months': rowDate.appendChild(monthElement); break;
case 'dates': rowDate.appendChild(dateElement); break;
case 'years': rowDate.appendChild(yearElement); break;
default: break;
}
});
} else {
rowDate.appendChild(monthElement);
rowDate.appendChild(dateElement);
rowDate.appendChild(yearElement);
}
targetElement.appendChild(rowDate);
targetElement.appendChild(rowClearfixDate);
}
setTimestamp(values.timestamp);
};
/**
* This function is creating a slider input
*
* It is generating the required markup and attaching the needed event listeners
* The returned element is fully functional input field with arrows for navigating
* through the values
*
* @param {object} elementConfig
* @return {HtmlElement}
*/
var createSliderInput = function (elementConfig) {
var element = document.createElement('div');
element.className = 'mtr-input-slider';
config.references[elementConfig.name] = config.targetElement + '-input-' + elementConfig.name;
element.id = config.references[elementConfig.name];
// First, let's init the main elements
var divArrowUp = createUpArrow();
var divArrowDown = createDownArrow();
// Content of the input, holding the input and the available values
var divContent = document.createElement('div');
divContent.className = 'mtr-content';
var inputValue = createInputValue();
var divValues = createValues(inputValue);
// The, let's append them to the element in the correct order
element.appendChild(divArrowUp);
// Append holder of the input and values to the main element
divContent.appendChild(inputValue);
divContent.appendChild(divValues);
element.appendChild(divContent);
element.appendChild(divArrowDown);
// Here are the definitions of the functions which are used to generate the markup
// and to attach the needed event listeners
function createUpArrow () {
var divArrowUp = document.createElement('div');
divArrowUp.className = 'mtr-arrow up';
divArrowUp.appendChild(document.createElement('span'));
// Attach event listener
divArrowUp.addEventListener('click', function () {
// Prevent blur event
// var input = qSelect(inputValue, '.mtr-input');
addClass(inputValue, 'arrow-click');
addClass(divContent, 'mtr-active');
if (arrowTimeout[elementConfig.name]) {
window.clearTimeout(arrowTimeout[elementConfig.name]);
}
arrowTimeout[elementConfig.name] = setTimeout(function () {
removeClass(inputValue, 'arrow-click');
removeClass(divContent, 'mtr-active');
}, 1000);
// Change the value with the next one
var name = elementConfig.name;
var currentValue;
switch (name) {
case 'hours': currentValue = getHours(); break;
case 'minutes': currentValue = getMinutes(); break;
case 'dates': currentValue = getDate(); break;
case 'months': currentValue = getMonth(); break;
case 'years': currentValue = getYear(); break;
}
var indexInArray = config.defaultValues[name].indexOf(currentValue);
indexInArray++;
if (indexInArray >= config.defaultValues[name].length) {
indexInArray = 0;
}
switch (name) {
case 'hours':
// Check is we have to make a transform of the hour
var newHour = config.defaultValues[name][indexInArray];
if (!config.disableAmPm && (getIsPm() && newHour !== 12)) {
newHour += 12;
}
setHours(newHour);
break;
case 'minutes': setMinutes(config.defaultValues[name][indexInArray]); break;
case 'dates': setDate(config.defaultValues[name][indexInArray]); break;
case 'months': setMonth(config.defaultValues[name][indexInArray]); break;
case 'years': setYear(config.defaultValues[name][indexInArray]); break;
}
}, false);
return divArrowUp;
}
function createDownArrow () {
var divArrowDown = document.createElement('div');
divArrowDown.className = 'mtr-arrow down';
divArrowDown.appendChild(document.createElement('span'));
divArrowDown.addEventListener('click', function (e) {
// Prevent blur event
// var input = qSelect(inputValue, '.mtr-input');
addClass(inputValue, 'arrow-click');
addClass(divContent, 'mtr-active');
if (arrowTimeout[elementConfig.name]) {
window.clearTimeout(arrowTimeout[elementConfig.name]);
}
arrowTimeout[elementConfig.name] = setTimeout(function () {
removeClass(inputValue, 'arrow-click');
removeClass(divContent, 'mtr-active');
}, 1000);
// Change the value with the prev one
var name = elementConfig.name;
var currentValue;
switch (name) {
case 'hours': currentValue = getHours(); break;
case 'minutes': currentValue = getMinutes(); break;
case 'dates': currentValue = getDate(); break;
case 'months': currentValue = getMonth(); break;
case 'years': currentValue = getYear(); break;
}
var indexInArray = config.defaultValues[name].indexOf(currentValue);
indexInArray--;
if (indexInArray < 0) {
indexInArray = config.defaultValues[name].length - 1;
}
switch (name) {
case 'hours':
// Check is we have to make a transform of the hour
var newHour = config.defaultValues[name][indexInArray];
if (!config.disableAmPm && (getIsPm() && newHour !== 12)) {
newHour += 12;
}
setHours(newHour);
break;
case 'minutes': setMinutes(config.defaultValues[name][indexInArray]); break;
case 'dates': setDate(config.defaultValues[name][indexInArray]); break;
case 'months': setMonth(config.defaultValues[name][indexInArray]); break;
case 'years': setYear(config.defaultValues[name][indexInArray]); break;
}
}, false);
return divArrowDown;
}
function createInputValue () {
var inputValue = document.createElement('input');
inputValue.value = elementConfig.value;
inputValue.type = 'text';
inputValue.className = 'mtr-input ' + elementConfig.name;
inputValue.style.display = 'none';
// Attach event listeners
inputValue.addEventListener('blur', function (e) {
// Blur event has to be called after specific amount of time
// because it can be caused from an arrow button. In this case
// we shouldn't apple the blur event body
setTimeout(function () {
blurEvent();
}, 500);
function blurEvent () {
if (!targetElement) {
return;
}
var newValue = inputValue.value;
var oldValue = inputValue.getAttribute('data-old-value');
// If the blur is called after click on arrow we shouldn't update the value
if (e.target.className.indexOf('arrow-click') > -1) {
removeClass(e.target, 'arrow-click');
return;
}
// If this is the month input we should decrement it because
// the months are starting from 0
if (inputValue.className.indexOf('months') > -1) {
newValue--;
}
// Validate the value
if (validateValue(elementConfig.name, newValue) === false) {
inputValue.value = oldValue;
inputValue.focus();
return;
}
// Trim the leading zero
newValue = parseInt(newValue);
// If the future detection is ON validate the value again
var target = elementConfig.name.substring(0, elementConfig.name.length - 1);
if (elementConfig.name === 'dates') {
target = 'day';
}
if (config.future && !validateChange(target, newValue, oldValue)) {
if (elementConfig.name === 'months') {
oldValue++;
}
inputValue.value = oldValue;
inputValue.focus();
return;
}
inputValue.style.display = 'none';
switch (elementConfig.name) {
case 'hours': setHours(newValue); break;
case 'minutes': setMinutes(newValue); break;
case 'dates': setDate(newValue); break;
case 'months': setMonth(newValue); break;
case 'years': setYear(newValue); break;
}
}
}, false);
// Accept the new values on <Enter>
inputValue.addEventListener('keyup', function (e) {
if (e.keyCode === 13) {
e.preventDefault();
inputValue.blur();
}
}, false);
// On wheel scroll we should change the value in the input
inputValue.addEventListener('wheel', function (e) {
e.preventDefault();
e.stopPropagation();
// If the user is using the mouse wheel the values should be changed
// var target = e.target;
var wheelData = e.wheelDeltaY ? e.wheelDeltaY : (e.deltaY * -1);
var oldValue = parseInt(inputValue.value);
var newValue;
var configMin = config[elementConfig.name].min;
var configMax = config[elementConfig.name].max;
var configStep = config[elementConfig.name].step;
if (elementConfig.name === 'months') {
// If we are scrolling the months we should increment the value
configMin++;
configMax++;
}
if (wheelData > 0) { // Scroll up
if (oldValue < configMax) {
newValue = oldValue + configStep;
} else {
newValue = configMin;
}
} else { // Scroll down
if (oldValue > configMin) {
newValue = oldValue - configStep;
} else {
newValue = configMax;
}
}
inputValue.value = newValue;
return false;
}, false);
return inputValue;
}
function createValues (inputValue) {
var divValues = createElementValues(elementConfig);
// On swipe, we should change the value in the input
divValues.addEventListener('touchstart', function (e) {
handleTouchStart(e);
}, false);
divValues.addEventListener('touchmove', function (e) {
handleTouchMove(e, function (direction) {
var parent = divValues.parentElement.parentElement;
var arrow;
if (direction > 0) { // Scroll up
arrow = qSelect(parent, '.mtr-arrow.up');
} else { // Scroll down
arrow = qSelect(parent, '.mtr-arrow.down');
}
arrow.click();
});
}, false);
return divValues;
}
return element;
};
/**
* Create HtmlElement with a radio button control
*
* @param {object} elementConfig
* @return {HtmlElement}
*/
var createRadioInput = function (elementConfig) {
var element = document.createElement('div');
element.className = 'mtr-input-radio';
config.references[elementConfig.name] = config.targetElement + '-input-' + elementConfig.name;
element.id = config.references[elementConfig.name];
var formHolder = document.createElement('form');
formHolder.name = config.references[elementConfig.name];
// First create the elements
var radioAm = createInputValue('ampm', 1, 'AM');
var radioPm = createInputValue('ampm', 0, 'PM');
formHolder.appendChild(radioAm);
formHolder.appendChild(radioPm);
formHolder.ampm.value = getIsAm() ? '1' : '0';
element.appendChild(formHolder);
function createInputValue (radioName, radioValue, labelValue) {
var divHolder = document.createElement('div');
var label = document.createElement('label');
var input = document.createElement('input');
var elementId = config.targetElement + '-radio-' + radioName + '-' + labelValue;
var innerHtmlSpanValue = document.createElement('span');
innerHtmlSpanValue.className = 'value';
innerHtmlSpanValue.appendChild(document.createTextNode(labelValue));
var innerHtmlSpanRadio = document.createElement('span');
innerHtmlSpanRadio.className = 'radio';
label.setAttribute('for', elementId);
label.appendChild(innerHtmlSpanValue);
label.appendChild(innerHtmlSpanRadio);
input.className = 'mtr-input ';
input.type = 'radio';
input.name = radioName;
input.id = elementId;
input.value = radioValue;
divHolder.appendChild(input);
divHolder.appendChild(label);
// Attach event listeners
input.addEventListener('change', function (e) {
var result = setAmPm(radioValue);
if (!result && config.future) {
setAmPm(!radioValue);
e.preventDefault();
e.stopPropagation();
return false;
}
}, false);
return divHolder;
}
return element;
};
/**
* This function is creating a new set of HtmlElement which
* contains the default values for a specific input
*
* @param {object} elementConfig
* @return {HtmlElement}
*/
var createElementValues = function (elementConfig) {
var divValues = document.createElement('div');
divValues.className = 'mtr-values';
elementConfig.values.forEach(function (value) {
var innerHTML = elementConfig.name === 'months' ? value + 1 : value;
var divValueHolder = document.createElement('div');
divValueHolder.className = 'mtr-default-value-holder';
divValueHolder.setAttribute('data-value', value);
var divValue = document.createElement('div');
divValue.className = 'mtr-default-value';
divValue.setAttribute('data-value', value);
if (elementConfig.name === 'minutes' && value === 0) {
divValue.appendChild(document.createTextNode('00'));
} else {
divValue.appendChild(document.createTextNode(innerHTML));
}
divValueHolder.appendChild(divValue);
if (elementConfig.valuesNames) {
var divValueName = document.createElement('div');
divValueName.className = 'mtr-default-value-name';
divValueName.appendChild(document.createTextNode(elementConfig.valuesNames[value]));
divValue.className += ' has-name';
divValueHolder.appendChild(divValueName);
}
divValues.appendChild(divValueHolder);
});
// Attach listeners
var inputClickEventListener = function () {
// Show the input field for manual setup
var parent = divValues.parentElement;
var inputValue = qSelect(parent, '.mtr-input');
// If we are working with months we have to increment the value
// because the months are starting from 0
if (inputValue.className.indexOf('months') > -1) {
inputValue.value = parseInt(inputValue.value) + 1;
}
inputValue.style.display = 'block';