-
Notifications
You must be signed in to change notification settings - Fork 643
/
jquery.timepicker.js
1820 lines (1472 loc) · 50.2 KB
/
jquery.timepicker.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
/*!
* jquery-timepicker v1.14.1 - A jQuery timepicker plugin inspired by Google Calendar. It supports both mouse and keyboard navigation.
* Copyright (c) 2021 Jon Thornton - https://www.jonthornton.com/jquery-timepicker/
* License: MIT
*/
(function () {
'use strict';
function _typeof(obj) {
"@babel/helpers - typeof";
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
};
} else {
_typeof = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}
return _typeof(obj);
}
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly) symbols = symbols.filter(function (sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
keys.push.apply(keys, symbols);
}
return keys;
}
function _objectSpread2(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
if (i % 2) {
ownKeys(Object(source), true).forEach(function (key) {
_defineProperty(target, key, source[key]);
});
} else if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(Object(source)).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
}
return target;
}
function _unsupportedIterableToArray(o, minLen) {
if (!o) return;
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(o);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
function _arrayLikeToArray(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
return arr2;
}
function _createForOfIteratorHelper(o, allowArrayLike) {
var it;
if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
if (it) o = it;
var i = 0;
var F = function () {};
return {
s: F,
n: function () {
if (i >= o.length) return {
done: true
};
return {
done: false,
value: o[i++]
};
},
e: function (e) {
throw e;
},
f: F
};
}
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
var normalCompletion = true,
didErr = false,
err;
return {
s: function () {
it = o[Symbol.iterator]();
},
n: function () {
var step = it.next();
normalCompletion = step.done;
return step;
},
e: function (e) {
didErr = true;
err = e;
},
f: function () {
try {
if (!normalCompletion && it.return != null) it.return();
} finally {
if (didErr) throw err;
}
}
};
}
var ONE_DAY = 86400;
var EVENT_DEFAULTS = {
bubbles: true,
cancelable: false,
detail: null
};
var roundingFunction = function roundingFunction(seconds, settings) {
if (seconds === null) {
return null;
}
var i = 0;
var nextVal = 0;
while (nextVal < seconds) {
i++;
nextVal += settings.step(i) * 60;
}
var prevVal = nextVal - settings.step(i - 1) * 60;
if (seconds - prevVal < nextVal - seconds) {
return moduloSeconds(prevVal, settings);
} else {
return moduloSeconds(nextVal, settings);
}
};
function moduloSeconds(seconds, settings) {
if (seconds == ONE_DAY && settings.show2400) {
return seconds;
}
return seconds % ONE_DAY;
}
var DEFAULT_SETTINGS = {
appendTo: "body",
className: null,
closeOnWindow: false,
closeOnScroll: false,
disableTextInput: false,
disableTimeRanges: [],
disableTouchKeyboard: false,
durationTime: null,
forceRoundTime: false,
lang: {},
listWidth: null,
maxTime: null,
minTime: null,
noneOption: false,
orientation: "l",
roundingFunction: roundingFunction,
scrollDefault: null,
selectOnBlur: false,
show2400: false,
showDuration: false,
showOn: ["click", "focus"],
step: 30,
stopScrollPropagation: false,
timeFormat: "g:ia",
typeaheadHighlight: true,
useSelect: false,
wrapHours: true
};
var DEFAULT_LANG = {
am: "am",
pm: "pm",
AM: "AM",
PM: "PM",
decimal: ".",
mins: "mins",
hr: "hr",
hrs: "hrs"
};
var Timepicker = /*#__PURE__*/function () {
function Timepicker(targetEl) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
_classCallCheck(this, Timepicker);
this._handleFormatValue = this._handleFormatValue.bind(this);
this._handleKeyUp = this._handleKeyUp.bind(this);
this.targetEl = targetEl;
var attrOptions = Timepicker.extractAttrOptions(targetEl, Object.keys(DEFAULT_SETTINGS));
this.settings = this.parseSettings(_objectSpread2(_objectSpread2(_objectSpread2({}, DEFAULT_SETTINGS), options), attrOptions));
}
_createClass(Timepicker, [{
key: "hideMe",
value: function hideMe() {
if (this.settings.useSelect) {
this.targetEl.blur();
return;
}
if (!this.list || !Timepicker.isVisible(this.list)) {
return;
}
if (this.settings.selectOnBlur) {
this._selectValue();
}
this.list.hide();
var hideTimepickerEvent = new CustomEvent('hideTimepicker', EVENT_DEFAULTS);
this.targetEl.dispatchEvent(hideTimepickerEvent);
}
}, {
key: "_findRow",
value: function _findRow(value) {
if (!value && value !== 0) {
return false;
}
var out = false;
var value = this.settings.roundingFunction(value, this.settings);
if (!this.list) {
return false;
}
this.list.find("li").each(function (i, obj) {
var parsed = parseInt(obj.dataset.time);
if (isNaN(parsed)) {
return;
}
if (parsed == value) {
out = obj;
return false;
}
});
return out;
}
}, {
key: "_hideKeyboard",
value: function _hideKeyboard() {
return (window.navigator.msMaxTouchPoints || "ontouchstart" in document) && this.settings.disableTouchKeyboard;
}
}, {
key: "_setTimeValue",
value: function _setTimeValue(value, source) {
if (this.targetEl.nodeName === "INPUT") {
if (value !== null || this.targetEl.value != "") {
this.targetEl.value = value;
}
var tp = this;
var settings = tp.settings;
if (settings.useSelect && source != "select" && tp.list) {
tp.list.val(tp._roundAndFormatTime(tp.anytime2int(value)));
}
}
var selectTimeEvent = new CustomEvent('selectTime', EVENT_DEFAULTS);
if (this.selectedValue != value) {
this.selectedValue = value;
var changeTimeEvent = new CustomEvent('changeTime', EVENT_DEFAULTS);
var changeEvent = new CustomEvent('change', Object.assign(EVENT_DEFAULTS, {
detail: 'timepicker'
}));
if (source == "select") {
this.targetEl.dispatchEvent(selectTimeEvent);
this.targetEl.dispatchEvent(changeTimeEvent);
this.targetEl.dispatchEvent(changeEvent);
} else if (["error", "initial"].indexOf(source) == -1) {
this.targetEl.dispatchEvent(changeTimeEvent);
}
return true;
} else {
if (["error", "initial"].indexOf(source) == -1) {
this.targetEl.dispatchEvent(selectTimeEvent);
}
return false;
}
}
}, {
key: "_getTimeValue",
value: function _getTimeValue() {
if (this.targetEl.nodeName === "INPUT") {
return this.targetEl.value;
} else {
// use the element's data attributes to store values
return this.selectedValue;
}
}
}, {
key: "_selectValue",
value: function _selectValue() {
var tp = this;
tp.settings;
var list = tp.list;
var cursor = list.find(".ui-timepicker-selected");
if (cursor.hasClass("ui-timepicker-disabled")) {
return false;
}
if (!cursor.length) {
return true;
}
var timeValue = cursor.get(0).dataset.time; // selected value found
if (timeValue) {
var parsedTimeValue = parseInt(timeValue);
if (!isNaN(parsedTimeValue)) {
timeValue = parsedTimeValue;
}
}
if (timeValue !== null) {
if (typeof timeValue != "string") {
timeValue = tp._int2time(timeValue);
}
tp._setTimeValue(timeValue, "select");
}
return true;
}
}, {
key: "anytime2int",
value: function anytime2int(input) {
if (typeof input === 'number') {
return input;
} else if (typeof input === 'string') {
return this.time2int(input);
} else if (_typeof(input) === 'object' && input instanceof Date) {
return input.getHours() * 3600 + input.getMinutes() * 60 + input.getSeconds();
} else if (typeof input == 'function') {
return input();
} else {
return null;
}
}
}, {
key: "time2int",
value: function time2int(timeString) {
if (timeString === "" || timeString === null || timeString === undefined) {
return null;
}
if (timeString === 'now') {
return this.anytime2int(new Date());
}
if (typeof timeString != "string") {
return timeString;
}
timeString = timeString.toLowerCase().replace(/[\s\.]/g, ""); // if the last character is an "a" or "p", add the "m"
if (this.settings.lang.am === "am" && (timeString.slice(-1) == "a" || timeString.slice(-1) == "p")) {
timeString += "m";
}
var pattern = /^(([^0-9]*))?([0-9]?[0-9])(([0-5][0-9]))?(([0-5][0-9]))?(([^0-9]*))$/;
var hasDelimetersMatch = timeString.match(/\W/);
if (hasDelimetersMatch) {
pattern = /^(([^0-9]*))?([0-9]?[0-9])(\W+([0-5][0-9]?))?(\W+([0-5][0-9]))?(([^0-9]*))$/;
}
var time = timeString.match(pattern);
if (!time) {
return null;
}
var hour = parseInt(time[3] * 1, 10);
var ampm = time[2] || time[9];
var minutes = this.parseMinuteString(time[5]);
var seconds = time[7] * 1 || 0;
if (!ampm && time[3].length == 2 && time[3][0] == "0") {
// preceding '0' implies AM
ampm = "am";
}
if (hour > 24 && !minutes) {
// if someone types in something like "83", turn it into "8h 30m"
hour = time[3][0] * 1;
minutes = this.parseMinuteString(time[3][1]);
}
var hours = hour;
if (hour <= 12 && ampm) {
ampm = ampm.trim();
var isPm = ampm == this.settings.lang.pm || ampm == this.settings.lang.PM;
if (hour == 12) {
hours = isPm ? 12 : 0;
} else {
hours = hour + (isPm ? 12 : 0);
}
} else {
var t = hour * 3600 + minutes * 60 + seconds;
if (t >= ONE_DAY + (this.settings.show2400 ? 1 : 0)) {
if (this.settings.wrapHours === false) {
return null;
}
hours = hour % 24;
}
}
var timeInt = hours * 3600 + minutes * 60 + seconds; // if no am/pm provided, intelligently guess based on the scrollDefault
if (hour < 12 && !ampm && this.settings._twelveHourTime && this.settings.scrollDefault()) {
var delta = timeInt - this.settings.scrollDefault();
if (delta < 0 && delta >= ONE_DAY / -2) {
timeInt = (timeInt + ONE_DAY / 2) % ONE_DAY;
}
}
return timeInt;
}
}, {
key: "parseMinuteString",
value: function parseMinuteString(minutesString) {
if (!minutesString) {
minutesString = 0;
}
var multiplier = 1;
if (minutesString.length == 1) {
multiplier = 10;
}
return parseInt(minutesString) * multiplier || 0;
}
}, {
key: "intStringDateOrFunc2func",
value: function intStringDateOrFunc2func(input) {
var _this = this;
if (input === null || input === undefined) {
return function () {
return null;
};
} else if (typeof input === 'function') {
return function () {
return _this.anytime2int(input());
};
} else {
return function () {
return _this.anytime2int(input);
};
}
}
}, {
key: "parseSettings",
value: function parseSettings(settings) {
settings.lang = _objectSpread2(_objectSpread2({}, DEFAULT_LANG), settings.lang); // lang is used by other functions the rest of this depends on
// todo: unwind circular dependency on lang
this.settings = settings;
if (settings.listWidth) {
settings.listWidth = this.anytime2int(settings.listWidth);
}
settings.minTime = this.intStringDateOrFunc2func(settings.minTime);
settings.maxTime = this.intStringDateOrFunc2func(settings.maxTime);
settings.durationTime = this.intStringDateOrFunc2func(settings.durationTime);
if (settings.scrollDefault) {
settings.scrollDefault = this.intStringDateOrFunc2func(settings.scrollDefault);
} else {
settings.scrollDefault = settings.minTime;
}
if (typeof settings.timeFormat === "string" && settings.timeFormat.match(/[gh]/)) {
settings._twelveHourTime = true;
}
if (typeof settings.step != 'function') {
var curryStep = settings.step;
settings.step = function () {
return curryStep;
};
}
settings.disableTimeRanges = this._parseDisableTimeRanges(settings.disableTimeRanges);
if (settings.closeOnWindowScroll && !settings.closeOnScroll) {
settings.closeOnScroll = settings.closeOnWindowScroll;
}
if (settings.closeOnScroll === true) {
settings.closeOnScroll = window.document;
}
return settings;
}
}, {
key: "_parseDisableTimeRanges",
value: function _parseDisableTimeRanges(disableTimeRanges) {
if (!disableTimeRanges || disableTimeRanges.length == 0) {
return [];
} // convert string times to integers
for (var i in disableTimeRanges) {
disableTimeRanges[i] = [this.anytime2int(disableTimeRanges[i][0]), this.anytime2int(disableTimeRanges[i][1])];
} // sort by starting time
disableTimeRanges = disableTimeRanges.sort(function (a, b) {
return a[0] - b[0];
}); // merge any overlapping ranges
for (var i = disableTimeRanges.length - 1; i > 0; i--) {
if (disableTimeRanges[i][0] <= disableTimeRanges[i - 1][1]) {
disableTimeRanges[i - 1] = [Math.min(disableTimeRanges[i][0], disableTimeRanges[i - 1][0]), Math.max(disableTimeRanges[i][1], disableTimeRanges[i - 1][1])];
disableTimeRanges.splice(i, 1);
}
}
return disableTimeRanges;
}
/*
* Filter freeform input
*/
}, {
key: "_disableTextInputHandler",
value: function _disableTextInputHandler(e) {
switch (e.keyCode) {
case 13: // return
case 9:
//tab
return;
default:
e.preventDefault();
}
}
}, {
key: "_int2duration",
value: function _int2duration(seconds, step) {
seconds = Math.abs(seconds);
var minutes = Math.round(seconds / 60),
duration = [],
hours,
mins;
if (minutes < 60) {
// Only show (x mins) under 1 hour
duration = [minutes, this.settings.lang.mins];
} else {
hours = Math.floor(minutes / 60);
mins = minutes % 60; // Show decimal notation (eg: 1.5 hrs) for 30 minute steps
if (step == 30 && mins == 30) {
hours += this.settings.lang.decimal + 5;
}
duration.push(hours);
duration.push(hours == 1 ? this.settings.lang.hr : this.settings.lang.hrs); // Show remainder minutes notation (eg: 1 hr 15 mins) for non-30 minute steps
// and only if there are remainder minutes to show
if (step != 30 && mins) {
duration.push(mins);
duration.push(this.settings.lang.mins);
}
}
return duration.join(" ");
}
}, {
key: "_roundAndFormatTime",
value: function _roundAndFormatTime(seconds) {
// console.log('_roundAndFormatTime')
seconds = this.settings.roundingFunction(seconds, this.settings);
if (seconds !== null) {
return this._int2time(seconds);
}
}
}, {
key: "_int2time",
value: function _int2time(timeInt) {
if (typeof timeInt != "number") {
return null;
}
var seconds = parseInt(timeInt % 60),
minutes = parseInt(timeInt / 60 % 60),
hours = parseInt(timeInt / (60 * 60) % 24);
var time = new Date(1970, 0, 2, hours, minutes, seconds, 0);
if (isNaN(time.getTime())) {
return null;
}
if (typeof this.settings.timeFormat === "function") {
return this.settings.timeFormat(time);
}
var output = "";
var hour, code;
for (var i = 0; i < this.settings.timeFormat.length; i++) {
code = this.settings.timeFormat.charAt(i);
switch (code) {
case "a":
output += time.getHours() > 11 ? this.settings.lang.pm : this.settings.lang.am;
break;
case "A":
output += time.getHours() > 11 ? this.settings.lang.PM : this.settings.lang.AM;
break;
case "g":
hour = time.getHours() % 12;
output += hour === 0 ? "12" : hour;
break;
case "G":
hour = time.getHours();
if (timeInt === ONE_DAY) hour = this.settings.show2400 ? 24 : 0;
output += hour;
break;
case "h":
hour = time.getHours() % 12;
if (hour !== 0 && hour < 10) {
hour = "0" + hour;
}
output += hour === 0 ? "12" : hour;
break;
case "H":
hour = time.getHours();
if (timeInt === ONE_DAY) hour = this.settings.show2400 ? 24 : 0;
output += hour > 9 ? hour : "0" + hour;
break;
case "i":
var minutes = time.getMinutes();
output += minutes > 9 ? minutes : "0" + minutes;
break;
case "s":
seconds = time.getSeconds();
output += seconds > 9 ? seconds : "0" + seconds;
break;
case "\\":
// escape character; add the next character and skip ahead
i++;
output += this.settings.timeFormat.charAt(i);
break;
default:
output += code;
}
}
return output;
}
}, {
key: "_setSelected",
value: function _setSelected() {
var list = this.list;
list.find("li").removeClass("ui-timepicker-selected");
var timeValue = this.anytime2int(this._getTimeValue());
if (timeValue === null) {
return;
}
var selected = this._findRow(timeValue);
if (selected) {
var selectedRect = selected.getBoundingClientRect();
var listRect = list.get(0).getBoundingClientRect();
var topDelta = selectedRect.top - listRect.top;
if (topDelta + selectedRect.height > listRect.height || topDelta < 0) {
var newScroll = list.scrollTop() + (selectedRect.top - listRect.top) - selectedRect.height;
list.scrollTop(newScroll);
}
var parsed = parseInt(selected.dataset.time);
if (this.settings.forceRoundTime || parsed === timeValue) {
selected.classList.add('ui-timepicker-selected');
}
}
}
}, {
key: "_isFocused",
value: function _isFocused(el) {
return el === document.activeElement;
}
}, {
key: "_handleFormatValue",
value: function _handleFormatValue(e) {
if (e && e.detail == "timepicker") {
return;
}
this._formatValue(e);
}
}, {
key: "_formatValue",
value: function _formatValue(e, origin) {
if (this.targetEl.value === "") {
this._setTimeValue(null, origin);
return;
} // IE fires change event before blur
if (this._isFocused(this.targetEl) && (!e || e.type != "change")) {
return;
}
var settings = this.settings;
var seconds = this.anytime2int(this.targetEl.value);
if (seconds === null) {
var timeFormatErrorEvent = new CustomEvent('timeFormatError', EVENT_DEFAULTS);
this.targetEl.dispatchEvent(timeFormatErrorEvent);
return;
}
var rangeError = this._isTimeRangeError(seconds, settings);
if (settings.forceRoundTime) {
var roundSeconds = settings.roundingFunction(seconds, settings);
if (roundSeconds != seconds) {
seconds = roundSeconds;
origin = null;
}
}
var prettyTime = this._int2time(seconds);
if (rangeError) {
this._setTimeValue(prettyTime);
var timeRangeErrorEvent = new CustomEvent('timeRangeError', EVENT_DEFAULTS);
this.targetEl.dispatchEvent(timeRangeErrorEvent);
} else {
this._setTimeValue(prettyTime, origin);
}
}
}, {
key: "_isTimeRangeError",
value: function _isTimeRangeError(seconds, settings) {
// check that the time in within bounds
if (settings.minTime !== null && settings.maxTime !== null && (seconds < settings.minTime() || seconds > settings.maxTime())) {
return true;
} // check that time isn't within disabled time ranges
var _iterator = _createForOfIteratorHelper(settings.disableTimeRanges),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var range = _step.value;
if (seconds >= range[0] && seconds < range[1]) {
return true;
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return false;
}
}, {
key: "_generateNoneElement",
value: function _generateNoneElement(optionValue, useSelect) {
var label, className, value;
if (_typeof(optionValue) == "object") {
label = optionValue.label;
className = optionValue.className;
value = optionValue.value;
} else if (typeof optionValue == "string") {
label = optionValue;
value = "";
} else {
$.error("Invalid noneOption value");
}
var el;
if (useSelect) {
el = document.createElement("option");
el.value = value;
} else {
el = document.createElement("li");
el.dataset.time = String(value);
}
el.innerText = label;
el.classList.add(className);
return el;
}
/*
* Time typeahead
*/
}, {
key: "_handleKeyUp",
value: function _handleKeyUp(e) {
var _this2 = this;
if (!this.list || !Timepicker.isVisible(this.list) || this.settings.disableTextInput) {
return true;
}
if (e.type === "paste" || e.type === "cut") {
var handler = function handler() {
if (_this2.settings.typeaheadHighlight) {
_this2._setSelected();
} else {
_this2.list.hide();
}
};
setTimeout(handler, 0);
return;
}
switch (e.keyCode) {
case 96: // numpad numerals
case 97:
case 98:
case 99:
case 100:
case 101:
case 102:
case 103:
case 104:
case 105:
case 48: // numerals
case 49:
case 50:
case 51:
case 52:
case 53:
case 54:
case 55:
case 56:
case 57:
case 65: // a
case 77: // m
case 80: // p
case 186: // colon
case 8: // backspace
case 46:
// delete
if (this.settings.typeaheadHighlight) {
this._setSelected();
} else {
this.list.hide();
}
break;
}
}
}], [{
key: "extractAttrOptions",
value: function extractAttrOptions(element, keys) {
var output = {};
var _iterator2 = _createForOfIteratorHelper(keys),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var key = _step2.value;
if (key in element.dataset) {
output[key] = element.dataset[key];
}
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
return output;
}
}, {
key: "isVisible",
value: function isVisible(elem) {
var el = elem[0];
return el.offsetWidth > 0 && el.offsetHeight > 0;
}
}, {
key: "hideAll",