1
|
/*! UIkit 2.26.4 | http://www.getuikit.com | (c) 2014 YOOtheme | MIT License */
|
2
|
(function(addon) {
|
3
|
|
4
|
var component;
|
5
|
|
6
|
if (window.UIkit) {
|
7
|
component = addon(UIkit);
|
8
|
}
|
9
|
|
10
|
if (typeof define == "function" && define.amd) {
|
11
|
define("uikit-timepicker", ["uikit"], function(){
|
12
|
return component || addon(UIkit);
|
13
|
});
|
14
|
}
|
15
|
|
16
|
})(function(UI){
|
17
|
|
18
|
"use strict";
|
19
|
|
20
|
|
21
|
UI.component('timepicker', {
|
22
|
|
23
|
defaults: {
|
24
|
format : '24h',
|
25
|
delay : 0,
|
26
|
start : 0,
|
27
|
end : 24
|
28
|
},
|
29
|
|
30
|
boot: function() {
|
31
|
|
32
|
// init code
|
33
|
UI.$html.on("focus.timepicker.uikit", "[data-uk-timepicker]", function(e) {
|
34
|
|
35
|
var ele = UI.$(this);
|
36
|
|
37
|
if (!ele.data("timepicker")) {
|
38
|
var obj = UI.timepicker(ele, UI.Utils.options(ele.attr("data-uk-timepicker")));
|
39
|
|
40
|
setTimeout(function(){
|
41
|
obj.autocomplete.input.focus();
|
42
|
}, 40);
|
43
|
}
|
44
|
});
|
45
|
},
|
46
|
|
47
|
init: function() {
|
48
|
|
49
|
var $this = this, times = getTimeRange(this.options.start, this.options.end), container;
|
50
|
|
51
|
this.options.minLength = 0;
|
52
|
this.options.template = '<ul class="uk-nav uk-nav-autocomplete uk-autocomplete-results">{{~items}}<li data-value="{{$item.value}}"><a class="needsclick">{{$item.value}}</a></li>{{/items}}</ul>';
|
53
|
|
54
|
this.options.source = function(release) {
|
55
|
release(times[$this.options.format] || times['12h']);
|
56
|
};
|
57
|
|
58
|
if (this.element.is('input')) {
|
59
|
this.element.wrap('<div class="uk-autocomplete"></div>');
|
60
|
container = this.element.parent();
|
61
|
} else {
|
62
|
container = this.element.addClass('uk-autocomplete');
|
63
|
}
|
64
|
|
65
|
this.autocomplete = UI.autocomplete(container, this.options);
|
66
|
this.autocomplete.dropdown.addClass('uk-dropdown-small uk-dropdown-scrollable');
|
67
|
|
68
|
this.autocomplete.on('show.uk.autocomplete', function() {
|
69
|
|
70
|
var selected = $this.autocomplete.dropdown.find('[data-value="'+$this.autocomplete.input.val()+'"]');
|
71
|
|
72
|
setTimeout(function(){
|
73
|
$this.autocomplete.pick(selected, true);
|
74
|
}, 10);
|
75
|
});
|
76
|
|
77
|
this.autocomplete.input.on('focus', function(){
|
78
|
|
79
|
$this.autocomplete.value = Math.random();
|
80
|
$this.autocomplete.triggercomplete();
|
81
|
|
82
|
}).on('blur', UI.Utils.debounce(function() {
|
83
|
$this.checkTime();
|
84
|
}, 100));
|
85
|
|
86
|
this.element.data("timepicker", this);
|
87
|
},
|
88
|
|
89
|
checkTime: function() {
|
90
|
|
91
|
var arr, timeArray, meridian = 'AM', hour, minute, time = this.autocomplete.input.val();
|
92
|
|
93
|
if (this.options.format == '12h') {
|
94
|
arr = time.split(' ');
|
95
|
timeArray = arr[0].split(':');
|
96
|
meridian = arr[1];
|
97
|
} else {
|
98
|
timeArray = time.split(':');
|
99
|
}
|
100
|
|
101
|
hour = parseInt(timeArray[0], 10);
|
102
|
minute = parseInt(timeArray[1], 10);
|
103
|
|
104
|
if (isNaN(hour)) hour = 0;
|
105
|
if (isNaN(minute)) minute = 0;
|
106
|
|
107
|
if (this.options.format == '12h') {
|
108
|
if (hour > 12) {
|
109
|
hour = 12;
|
110
|
} else if (hour < 0) {
|
111
|
hour = 12;
|
112
|
}
|
113
|
|
114
|
if (meridian === 'am' || meridian === 'a') {
|
115
|
meridian = 'AM';
|
116
|
} else if (meridian === 'pm' || meridian === 'p') {
|
117
|
meridian = 'PM';
|
118
|
}
|
119
|
|
120
|
if (meridian !== 'AM' && meridian !== 'PM') {
|
121
|
meridian = 'AM';
|
122
|
}
|
123
|
|
124
|
} else {
|
125
|
|
126
|
if (hour >= 24) {
|
127
|
hour = 23;
|
128
|
} else if (hour < 0) {
|
129
|
hour = 0;
|
130
|
}
|
131
|
}
|
132
|
|
133
|
if (minute < 0) {
|
134
|
minute = 0;
|
135
|
} else if (minute >= 60) {
|
136
|
minute = 0;
|
137
|
}
|
138
|
|
139
|
this.autocomplete.input.val(this.formatTime(hour, minute, meridian)).trigger('change');
|
140
|
},
|
141
|
|
142
|
formatTime: function(hour, minute, meridian) {
|
143
|
hour = hour < 10 ? '0' + hour : hour;
|
144
|
minute = minute < 10 ? '0' + minute : minute;
|
145
|
return hour + ':' + minute + (this.options.format == '12h' ? ' ' + meridian : '');
|
146
|
}
|
147
|
});
|
148
|
|
149
|
// helper
|
150
|
|
151
|
function getTimeRange(start, end) {
|
152
|
|
153
|
start = start || 0;
|
154
|
end = end || 24;
|
155
|
|
156
|
var times = {'12h':[], '24h':[]}, i, h;
|
157
|
|
158
|
for (i = start, h=''; i<end; i++) {
|
159
|
|
160
|
h = ''+i;
|
161
|
|
162
|
if (i<10) h = '0'+h;
|
163
|
|
164
|
times['24h'].push({value: (h+':00')});
|
165
|
times['24h'].push({value: (h+':30')});
|
166
|
|
167
|
if (i === 0) {
|
168
|
h = 12;
|
169
|
times['12h'].push({value: (h+':00 AM')});
|
170
|
times['12h'].push({value: (h+':30 AM')});
|
171
|
}
|
172
|
|
173
|
if (i > 0 && i<13 && i!==12) {
|
174
|
times['12h'].push({value: (h+':00 AM')});
|
175
|
times['12h'].push({value: (h+':30 AM')});
|
176
|
}
|
177
|
|
178
|
if (i >= 12) {
|
179
|
|
180
|
h = h-12;
|
181
|
if (h === 0) h = 12;
|
182
|
if (h < 10) h = '0'+String(h);
|
183
|
|
184
|
times['12h'].push({value: (h+':00 PM')});
|
185
|
times['12h'].push({value: (h+':30 PM')});
|
186
|
}
|
187
|
}
|
188
|
|
189
|
return times;
|
190
|
}
|
191
|
|
192
|
});
|