forked from jonthornton/jquery-timepicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.timepicker.js
1398 lines (1180 loc) · 35.9 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.12.0 - A jQuery timepicker plugin inspired by Google Calendar. It supports both mouse and keyboard navigation.
* Copyright (c) 2015 Jon Thornton - http://jonthornton.github.com/jquery-timepicker/
* License: MIT
*/
(function(factory) {
if (
typeof exports === "object" &&
exports &&
typeof module === "object" &&
module &&
module.exports === exports
) {
// Browserify. Attach to jQuery module.
factory(require("jquery"));
} else if (typeof define === "function" && define.amd) {
// AMD. Register as an anonymous module.
define(["jquery"], factory);
} else {
// Browser globals
factory(jQuery);
}
})(function($) {
var _ONE_DAY = 86400;
var _lang = {
am: "am",
pm: "pm",
AM: "AM",
PM: "PM",
decimal: ".",
mins: "mins",
hr: "hr",
hrs: "hrs"
};
var _DEFAULTS = {
appendTo: "body",
className: null,
closeOnWindowScroll: false,
disableTextInput: false,
disableTimeRanges: [],
disableTouchKeyboard: false,
durationTime: null,
forceRoundTime: false,
listWidth: null, // Set to 1 to match input width, 2 to double input width, .5 to halve input width, etc
maxTime: null,
minTime: null,
noneOption: false,
orientation: "l",
roundingFunction: function(seconds, settings) {
if (seconds === null) {
return null;
} else if (typeof settings.step !== "number") {
// TODO: nearest fit irregular steps
return seconds;
} else {
var offset = seconds % (settings.step * 60); // step is in minutes
var start = settings.minTime || 0;
// adjust offset by start mod step so that the offset is aligned not to 00:00 but to the start
offset -= start % (settings.step * 60);
if (offset >= settings.step * 30) {
// if offset is larger than a half step, round up
seconds += settings.step * 60 - offset;
} else {
// round down
seconds -= offset;
}
return _moduloSeconds(seconds, settings);
}
},
scrollDefault: null,
selectOnBlur: false,
show2400: false,
showDuration: false,
showOn: ["click", "focus"],
showOnFocus: true,
step: 30,
stopScrollPropagation: false,
timeFormat: "g:ia",
typeaheadHighlight: true,
useSelect: false,
wrapHours: true
};
var methods = {
init: function(options) {
return this.each(function() {
var self = $(this);
// pick up settings from data attributes
var attributeOptions = [];
for (var key in _DEFAULTS) {
if (self.data(key)) {
attributeOptions[key] = self.data(key);
}
}
var settings = $.extend({}, _DEFAULTS, options, attributeOptions);
if (settings.lang) {
_lang = $.extend(_lang, settings.lang);
}
settings = _parseSettings(settings);
self.data("timepicker-settings", settings);
self.addClass("ui-timepicker-input");
if (settings.useSelect) {
_render(self);
} else {
self.prop("autocomplete", "off");
if (settings.showOn) {
for (var i in settings.showOn) {
self.on(settings.showOn[i] + ".timepicker", methods.show);
}
}
self.on("change.timepicker", _formatValue);
self.on("keydown.timepicker", _keydownhandler);
self.on("keyup.timepicker", _keyuphandler);
if (settings.disableTextInput) {
self.on("keydown.timepicker", _disableTextInputHandler);
}
self.on("cut.timepicker", _keyuphandler);
self.on("paste.timepicker", _keyuphandler);
_formatValue.call(self.get(0), null, "initial");
}
});
},
show: function(e) {
var self = $(this);
var settings = self.data("timepicker-settings");
if (e) {
e.preventDefault();
}
if (settings.useSelect) {
self.data("timepicker-list").focus();
return;
}
if (_hideKeyboard(self)) {
// block the keyboard on mobile devices
self.blur();
}
var list = self.data("timepicker-list");
// check if input is readonly
if (self.prop("readonly")) {
return;
}
// check if list needs to be rendered
if (
!list ||
list.length === 0 ||
typeof settings.durationTime === "function"
) {
_render(self);
list = self.data("timepicker-list");
}
if (_isVisible(list)) {
return;
}
self.data("ui-timepicker-value", self.val());
_setSelected(self, list);
// make sure other pickers are hidden
methods.hide();
if (typeof settings.listWidth == 'number') {
console.log(settings.listWidth)
list.width(self.outerWidth() * settings.listWidth);
}
// position the dropdown relative to the input
list.show();
var listOffset = {};
if (settings.orientation.match(/r/)) {
// right-align the dropdown
listOffset.left =
self.offset().left +
self.outerWidth() -
list.outerWidth() +
parseInt(list.css("marginLeft").replace("px", ""), 10);
} else if (settings.orientation.match(/l/)) {
// left-align the dropdown
listOffset.left =
self.offset().left +
parseInt(list.css("marginLeft").replace("px", ""), 10);
} else if (settings.orientation.match(/c/)) {
// center-align the dropdown
listOffset.left =
self.offset().left +
((self.outerWidth() - list.outerWidth()) / 2) +
parseInt(list.css("marginLeft").replace("px", ""), 10);
}
var verticalOrientation;
if (settings.orientation.match(/t/)) {
verticalOrientation = "t";
} else if (settings.orientation.match(/b/)) {
verticalOrientation = "b";
} else if (
self.offset().top + self.outerHeight(true) + list.outerHeight() >
$(window).height() + $(window).scrollTop()
) {
verticalOrientation = "t";
} else {
verticalOrientation = "b";
}
if (verticalOrientation == "t") {
// position the dropdown on top
list.addClass("ui-timepicker-positioned-top");
listOffset.top =
self.offset().top -
list.outerHeight() +
parseInt(list.css("marginTop").replace("px", ""), 10);
} else {
// put it under the input
list.removeClass("ui-timepicker-positioned-top");
listOffset.top =
self.offset().top +
self.outerHeight() +
parseInt(list.css("marginTop").replace("px", ""), 10);
}
list.offset(listOffset);
// position scrolling
var selected = list.find(".ui-timepicker-selected");
if (!selected.length) {
var timeInt = _time2int(_getTimeValue(self));
if (timeInt !== null) {
selected = _findRow(self, list, timeInt);
} else if (settings.scrollDefault) {
selected = _findRow(self, list, settings.scrollDefault());
}
}
// if not found or disabled, intelligently find first selectable element
if (!selected.length || selected.hasClass("ui-timepicker-disabled")) {
selected = list.find("li:not(.ui-timepicker-disabled):first");
}
if (selected && selected.length) {
var topOffset =
list.scrollTop() + selected.position().top - selected.outerHeight();
list.scrollTop(topOffset);
} else {
list.scrollTop(0);
}
// prevent scroll propagation
if (settings.stopScrollPropagation) {
$(
document
).on("wheel.ui-timepicker", ".ui-timepicker-wrapper", function(e) {
e.preventDefault();
var currentScroll = $(this).scrollTop();
$(this).scrollTop(currentScroll + e.originalEvent.deltaY);
});
}
// attach close handlers
$(document).on(
"touchstart.ui-timepicker mousedown.ui-timepicker",
_closeHandler
);
$(window).on("resize.ui-timepicker", _closeHandler);
if (settings.closeOnWindowScroll) {
$(document).on("scroll.ui-timepicker", _closeHandler);
}
self.trigger("showTimepicker");
return this;
},
hide: function(e) {
var self = $(this);
var settings = self.data("timepicker-settings");
if (settings && settings.useSelect) {
self.blur();
}
$(".ui-timepicker-wrapper").each(function() {
var list = $(this);
if (!_isVisible(list)) {
return;
}
var self = list.data("timepicker-input");
var settings = self.data("timepicker-settings");
if (settings && settings.selectOnBlur) {
_selectValue(self);
}
list.hide();
self.trigger("hideTimepicker");
});
return this;
},
option: function(key, value) {
if (typeof key == "string" && typeof value == "undefined") {
return $(this).data("timepicker-settings")[key];
}
return this.each(function() {
var self = $(this);
var settings = self.data("timepicker-settings");
var list = self.data("timepicker-list");
if (typeof key == "object") {
settings = $.extend(settings, key);
} else if (typeof key == "string") {
settings[key] = value;
}
settings = _parseSettings(settings);
self.data("timepicker-settings", settings);
_formatValue.call(self.get(0), { type: "change" }, "initial");
if (list) {
list.remove();
self.data("timepicker-list", false);
}
if (settings.useSelect) {
_render(self);
}
});
},
getSecondsFromMidnight: function() {
return _time2int(_getTimeValue(this));
},
getTime: function(relative_date) {
var self = this;
var time_string = _getTimeValue(self);
if (!time_string) {
return null;
}
var offset = _time2int(time_string);
if (offset === null) {
return null;
}
if (!relative_date) {
relative_date = new Date();
}
// construct a Date from relative date, and offset's time
var time = new Date(relative_date);
time.setHours(offset / 3600);
time.setMinutes((offset % 3600) / 60);
time.setSeconds(offset % 60);
time.setMilliseconds(0);
return time;
},
isVisible: function() {
var self = this;
var list = self.data("timepicker-list");
return !!(list && _isVisible(list));
},
setTime: function(value) {
var self = this;
var settings = self.data("timepicker-settings");
if (settings.forceRoundTime) {
var prettyTime = _roundAndFormatTime(_time2int(value), settings);
} else {
var prettyTime = _int2time(_time2int(value), settings);
}
if (value && prettyTime === null && settings.noneOption) {
prettyTime = value;
}
_setTimeValue(self, prettyTime, "initial");
_formatValue.call(self.get(0), { type: "change" }, "initial");
if (self.data("timepicker-list")) {
_setSelected(self, self.data("timepicker-list"));
}
return this;
},
remove: function() {
var self = this;
// check if this element is a timepicker
if (!self.hasClass("ui-timepicker-input")) {
return;
}
var settings = self.data("timepicker-settings");
self.removeAttr("autocomplete", "off");
self.removeClass("ui-timepicker-input");
self.removeData("timepicker-settings");
self.off(".timepicker");
// timepicker-list won't be present unless the user has interacted with this timepicker
if (self.data("timepicker-list")) {
self.data("timepicker-list").remove();
}
if (settings.useSelect) {
self.show();
}
self.removeData("timepicker-list");
return this;
}
};
// private methods
function _isVisible(elem) {
var el = elem[0];
return el.offsetWidth > 0 && el.offsetHeight > 0;
}
function _parseSettings(settings) {
if (settings.minTime) {
settings.minTime = _time2int(settings.minTime);
}
if (settings.maxTime) {
settings.maxTime = _time2int(settings.maxTime);
}
if (settings.durationTime && typeof settings.durationTime !== "function") {
settings.durationTime = _time2int(settings.durationTime);
}
if (settings.scrollDefault == "now") {
settings.scrollDefault = function() {
return settings.roundingFunction(_time2int(new Date()), settings);
};
} else if (
settings.scrollDefault &&
typeof settings.scrollDefault != "function"
) {
var val = settings.scrollDefault;
settings.scrollDefault = function() {
return settings.roundingFunction(_time2int(val), settings);
};
} else if (settings.minTime) {
settings.scrollDefault = function() {
return settings.roundingFunction(settings.minTime, settings);
};
}
if (
$.type(settings.timeFormat) === "string" &&
settings.timeFormat.match(/[gh]/)
) {
settings._twelveHourTime = true;
}
if (
settings.showOnFocus === false &&
settings.showOn.indexOf("focus") != -1
) {
settings.showOn.splice(settings.showOn.indexOf("focus"), 1);
}
if (settings.disableTimeRanges.length > 0) {
// convert string times to integers
for (var i in settings.disableTimeRanges) {
settings.disableTimeRanges[i] = [
_time2int(settings.disableTimeRanges[i][0]),
_time2int(settings.disableTimeRanges[i][1])
];
}
// sort by starting time
settings.disableTimeRanges = settings.disableTimeRanges.sort(function(
a,
b
) {
return a[0] - b[0];
});
// merge any overlapping ranges
for (var i = settings.disableTimeRanges.length - 1; i > 0; i--) {
if (
settings.disableTimeRanges[i][0] <=
settings.disableTimeRanges[i - 1][1]
) {
settings.disableTimeRanges[i - 1] = [
Math.min(
settings.disableTimeRanges[i][0],
settings.disableTimeRanges[i - 1][0]
),
Math.max(
settings.disableTimeRanges[i][1],
settings.disableTimeRanges[i - 1][1]
)
];
settings.disableTimeRanges.splice(i, 1);
}
}
}
return settings;
}
function _render(self) {
var settings = self.data("timepicker-settings");
var list = self.data("timepicker-list");
if (list && list.length) {
list.remove();
self.data("timepicker-list", false);
}
if (settings.useSelect) {
list = $("<select />", { class: "ui-timepicker-select" });
if (self.attr('name')) {
list.attr('name', 'ui-timepicker-' + self.attr('name'));
}
var wrapped_list = list;
} else {
list = $("<ul />", { class: "ui-timepicker-list" });
var wrapped_list = $("<div />", {
class: "ui-timepicker-wrapper",
tabindex: -1
});
wrapped_list.css({ display: "none", position: "absolute" }).append(list);
}
if (settings.noneOption) {
if (settings.noneOption === true) {
settings.noneOption = settings.useSelect ? "Time..." : "None";
}
if ($.isArray(settings.noneOption)) {
for (var i in settings.noneOption) {
if (parseInt(i, 10) == i) {
var noneElement = _generateNoneElement(
settings.noneOption[i],
settings.useSelect
);
list.append(noneElement);
}
}
} else {
var noneElement = _generateNoneElement(
settings.noneOption,
settings.useSelect
);
list.append(noneElement);
}
}
if (settings.className) {
wrapped_list.addClass(settings.className);
}
if (
(settings.minTime !== null || settings.durationTime !== null) &&
settings.showDuration
) {
var stepval =
typeof settings.step == "function" ? "function" : settings.step;
wrapped_list.addClass("ui-timepicker-with-duration");
wrapped_list.addClass("ui-timepicker-step-" + settings.step);
}
var durStart = settings.minTime;
if (typeof settings.durationTime === "function") {
durStart = _time2int(settings.durationTime());
} else if (settings.durationTime !== null) {
durStart = settings.durationTime;
}
var start = settings.minTime !== null ? settings.minTime : 0;
var end =
settings.maxTime !== null ? settings.maxTime : start + _ONE_DAY - 1;
if (end < start) {
// make sure the end time is greater than start time, otherwise there will be no list to show
end += _ONE_DAY;
}
if (
end === _ONE_DAY - 1 &&
$.type(settings.timeFormat) === "string" &&
settings.show2400
) {
// show a 24:00 option when using military time
end = _ONE_DAY;
}
var dr = settings.disableTimeRanges;
var drCur = 0;
var drLen = dr.length;
var stepFunc = settings.step;
if (typeof stepFunc != "function") {
stepFunc = function() {
return settings.step;
};
}
for (var i = start, j = 0; i <= end; j++, i += stepFunc(j) * 60) {
var timeInt = i;
var timeString = _int2time(timeInt, settings);
if (settings.useSelect) {
var row = $("<option />", { value: timeString });
row.text(timeString);
} else {
var row = $("<li />");
row.addClass(
timeInt % _ONE_DAY < _ONE_DAY / 2
? "ui-timepicker-am"
: "ui-timepicker-pm"
);
row.data("time", _moduloSeconds(timeInt, settings));
row.text(timeString);
}
if (
(settings.minTime !== null || settings.durationTime !== null) &&
settings.showDuration
) {
var durationString = _int2duration(i - durStart, settings.step);
if (settings.useSelect) {
row.text(row.text() + " (" + durationString + ")");
} else {
var duration = $("<span />", { class: "ui-timepicker-duration" });
duration.text(" (" + durationString + ")");
row.append(duration);
}
}
if (drCur < drLen) {
if (timeInt >= dr[drCur][1]) {
drCur += 1;
}
if (dr[drCur] && timeInt >= dr[drCur][0] && timeInt < dr[drCur][1]) {
if (settings.useSelect) {
row.prop("disabled", true);
} else {
row.addClass("ui-timepicker-disabled");
}
}
}
list.append(row);
}
wrapped_list.data("timepicker-input", self);
self.data("timepicker-list", wrapped_list);
if (settings.useSelect) {
if (self.val()) {
list.val(_roundAndFormatTime(_time2int(self.val()), settings));
}
list.on("focus", function() {
$(this)
.data("timepicker-input")
.trigger("showTimepicker");
});
list.on("blur", function() {
$(this)
.data("timepicker-input")
.trigger("hideTimepicker");
});
list.on("change", function() {
_setTimeValue(self, $(this).val(), "select");
});
_setTimeValue(self, list.val(), "initial");
self.hide().after(list);
} else {
var appendTo = settings.appendTo;
if (typeof appendTo === "string") {
appendTo = $(appendTo);
} else if (typeof appendTo === "function") {
appendTo = appendTo(self);
}
appendTo.append(wrapped_list);
_setSelected(self, list);
list.on("mousedown click", "li", function(e) {
// hack: temporarily disable the focus handler
// to deal with the fact that IE fires 'focus'
// events asynchronously
self.off("focus.timepicker");
self.on("focus.timepicker-ie-hack", function() {
self.off("focus.timepicker-ie-hack");
self.on("focus.timepicker", methods.show);
});
if (!_hideKeyboard(self)) {
self[0].focus();
}
// make sure only the clicked row is selected
list.find("li").removeClass("ui-timepicker-selected");
$(this).addClass("ui-timepicker-selected");
if (_selectValue(self)) {
self.trigger("hideTimepicker");
list.on("mouseup.timepicker click.timepicker", "li", function(e) {
list.off("mouseup.timepicker click.timepicker");
wrapped_list.hide();
});
}
});
}
}
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");
}
if (useSelect) {
return $("<option />", {
value: value,
class: className,
text: label
});
} else {
return $("<li />", {
class: className,
text: label
}).data("time", String(value));
}
}
function _roundAndFormatTime(seconds, settings) {
seconds = settings.roundingFunction(seconds, settings);
if (seconds !== null) {
return _int2time(seconds, settings);
}
}
// event handler to decide whether to close timepicker
function _closeHandler(e) {
if (e.target == window) {
// mobile Chrome fires focus events against window for some reason
return;
}
var target = $(e.target);
if (
target.closest(".ui-timepicker-input").length ||
target.closest(".ui-timepicker-wrapper").length
) {
// active timepicker was focused. ignore
return;
}
methods.hide();
$(document).unbind(".ui-timepicker");
$(window).unbind(".ui-timepicker");
}
function _hideKeyboard(self) {
var settings = self.data("timepicker-settings");
return (
(window.navigator.msMaxTouchPoints || "ontouchstart" in document) &&
settings.disableTouchKeyboard
);
}
function _findRow(self, list, value) {
if (!value && value !== 0) {
return false;
}
var settings = self.data("timepicker-settings");
var out = false;
var value = settings.roundingFunction(value, settings);
// loop through the menu items
list.find("li").each(function(i, obj) {
var jObj = $(obj);
if (typeof jObj.data("time") != "number") {
return;
}
if (jObj.data("time") == value) {
out = jObj;
return false;
}
});
return out;
}
function _setSelected(self, list) {
list.find("li").removeClass("ui-timepicker-selected");
var settings = self.data("timepicker-settings");
var timeValue = _time2int(_getTimeValue(self), settings);
if (timeValue === null) {
return;
}
var selected = _findRow(self, list, timeValue);
if (selected) {
var topDelta = selected.offset().top - list.offset().top;
if (
topDelta + selected.outerHeight() > list.outerHeight() ||
topDelta < 0
) {
list.scrollTop(
list.scrollTop() + selected.position().top - selected.outerHeight()
);
}
if (settings.forceRoundTime || selected.data("time") === timeValue) {
selected.addClass("ui-timepicker-selected");
}
}
}
function _formatValue(e, origin) {
if (origin == "timepicker") {
return;
}
var self = $(this);
if (this.value === "") {
_setTimeValue(self, null, origin);
return;
}
if (self.is(":focus") && (!e || e.type != "change")) {
return;
}
var settings = self.data("timepicker-settings");
var seconds = _time2int(this.value, settings);
if (seconds === null) {
self.trigger("timeFormatError");
return;
}
var rangeError = false;
// check that the time in within bounds
if (
settings.minTime !== null &&
settings.maxTime !== null &&
(seconds < settings.minTime || seconds > settings.maxTime)
) {
rangeError = true;
}
// check that time isn't within disabled time ranges
$.each(settings.disableTimeRanges, function() {
if (seconds >= this[0] && seconds < this[1]) {
rangeError = true;
return false;
}
});
if (settings.forceRoundTime) {
var roundSeconds = settings.roundingFunction(seconds, settings);
if (roundSeconds != seconds) {
seconds = roundSeconds;
origin = null;
}
}
var prettyTime = _int2time(seconds, settings);
if (rangeError) {
if (
_setTimeValue(self, prettyTime, "error") ||
(e && e.type == "change")
) {
self.trigger("timeRangeError");
}
} else {
_setTimeValue(self, prettyTime, origin);
}
}
function _getTimeValue(self) {
if (self.is("input")) {
return self.val();
} else {
// use the element's data attributes to store values
return self.data("ui-timepicker-value");
}
}
function _setTimeValue(self, value, source) {
if (self.is("input")) {
self.val(value);
var settings = self.data("timepicker-settings");
if (settings.useSelect && source != "select" && self.data("timepicker-list")) {
self
.data("timepicker-list")
.val(_roundAndFormatTime(_time2int(value), settings));
}
}
if (self.data("ui-timepicker-value") != value) {
self.data("ui-timepicker-value", value);
if (source == "select") {
self
.trigger("selectTime")
.trigger("changeTime")
.trigger("change", "timepicker");
} else if (["error", "initial"].indexOf(source) == -1) {
self.trigger("changeTime");
}
return true;
} else {
if (["error", "initial"].indexOf(source) == -1) {
self.trigger("selectTime");
}
return false;
}
}
/*
* Filter freeform input
*/
function _disableTextInputHandler(e) {
switch (e.keyCode) {
case 13: // return
case 9: //tab
return;
default:
e.preventDefault();
}
}
/*
* Keyboard navigation via arrow keys
*/
function _keydownhandler(e) {
var self = $(this);
var list = self.data("timepicker-list");
if (!list || !_isVisible(list)) {
if (e.keyCode == 40) {
// show the list!
methods.show.call(self.get(0));
list = self.data("timepicker-list");
if (!_hideKeyboard(self)) {
self.focus();
}
} else {