forked from perifer/timePicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.timePicker.js
277 lines (249 loc) · 8.82 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
/*
* A time picker for jQuery
*
* Dual licensed under the MIT and GPL licenses.
* Copyright (c) 2009 Anders Fajerson
* @name timePicker
* @author Anders Fajerson (http://perifer.se)
* @example $("#mytime").timePicker();
* @example $("#mytime").timePicker({step:30, startTime:"15:00", endTime:"18:00"});
*
* Based on timePicker by Sam Collet (http://www.texotela.co.uk/code/jquery/timepicker/)
*
* Options:
* step: # of minutes to step the time by
* startTime: beginning of the range of acceptable times
* endTime: end of the range of acceptable times
* separator: separator string to use between hours and minutes (e.g. ':')
* show24Hours: use a 24-hour scheme
*/
(function($){
$.fn.timePicker = function(options) {
// Build main options before element iteration
var settings = $.extend({}, $.fn.timePicker.defaults, options);
return this.each(function() {
$.timePicker(this, settings);
});
};
$.timePicker = function (elm, settings) {
var e = $(elm)[0];
return e.timePicker || (e.timePicker = new jQuery._timePicker(e, settings));
};
$.timePicker.version = '0.3';
$._timePicker = function(elm, settings) {
var tpOver = false;
var keyDown = false;
var startTime = timeToDate(settings.startTime, settings);
var endTime = timeToDate(settings.endTime, settings);
var selectedClass = "selected";
var selectedSelector = "li." + selectedClass;
$(elm).attr('autocomplete', 'OFF'); // Disable browser autocomplete
var times = [];
var time = new Date(startTime); // Create a new date object.
while(time <= endTime) {
times[times.length] = formatTime(time, settings);
time = new Date(time.setMinutes(time.getMinutes() + settings.step));
}
var $tpDiv = $('<div class="time-picker'+ (settings.show24Hours ? '' : ' time-picker-12hours') +'"></div>');
var $tpList = $('<ul></ul>');
// Build the list.
for(var i = 0; i < times.length; i++) {
$tpList.append("<li>" + times[i] + "</li>");
}
$tpDiv.append($tpList);
// Append the timPicker to the body and position it.
$tpDiv.appendTo('body').hide();
// Store the mouse state, used by the blur event. Use mouseover instead of
// mousedown since Opera fires blur before mousedown.
$tpDiv.mouseover(function() {
tpOver = true;
}).mouseout(function() {
tpOver = false;
});
$("li", $tpList).mouseover(function() {
if (!keyDown) {
$(selectedSelector, $tpDiv).removeClass(selectedClass);
$(this).addClass(selectedClass);
}
}).mousedown(function() {
tpOver = true;
}).click(function() {
setTimeVal(elm, this, $tpDiv, settings);
tpOver = false;
});
var showPicker = function() {
if ($tpDiv.is(":visible")) {
return false;
}
$("li", $tpDiv).removeClass(selectedClass);
// Position
var elmOffset = $(elm).offset();
$tpDiv.css({'top':elmOffset.top + elm.offsetHeight, 'left':elmOffset.left});
// Show picker. This has to be done before scrollTop is set since that
// can't be done on hidden elements.
$tpDiv.show();
// Try to find a time in the list that matches the entered time.
var time = elm.value ? timeStringToDate(elm.value, settings) : startTime;
var startMin = startTime.getHours() * 60 + startTime.getMinutes();
var min = (time.getHours() * 60 + time.getMinutes()) - startMin;
var steps = Math.round(min / settings.step);
var roundTime = normaliseTime(new Date(0, 0, 0, 0, (steps * settings.step + startMin), 0));
roundTime = (startTime < roundTime && roundTime <= endTime) ? roundTime : startTime;
var $matchedTime = $("li:contains(" + formatTime(roundTime, settings) + ")", $tpDiv);
if ($matchedTime.length) {
$matchedTime.addClass(selectedClass);
// Scroll to matched time.
$tpDiv[0].scrollTop = $matchedTime[0].offsetTop;
}
return true;
};
// Attach to click as well as focus so timePicker can be shown again when
// clicking on the input when it already has focus.
$(elm).focus(showPicker).click(showPicker);
// Hide timepicker on blur
$(elm).blur(function() {
if (!tpOver) {
$tpDiv.hide();
}
});
// Keypress doesn't repeat on Safari for non-text keys.
// Keydown doesn't repeat on Firefox and Opera on Mac.
// Using kepress for Opera and Firefox and keydown for the rest seems to
// work with up/down/enter/esc.
var event = ($.browser.opera || $.browser.mozilla) ? 'keypress' : 'keydown';
$(elm)[event](function(e) {
var $selected;
keyDown = true;
var top = $tpDiv[0].scrollTop;
switch (e.keyCode) {
case 38: // Up arrow.
// Just show picker if it's hidden.
if (showPicker()) {
return false;
};
$selected = $(selectedSelector, $tpList);
var prev = $selected.prev().addClass(selectedClass)[0];
if (prev) {
$selected.removeClass(selectedClass);
// Scroll item into view.
if (prev.offsetTop < top) {
$tpDiv[0].scrollTop = top - prev.offsetHeight;
}
}
else {
// Loop to next item.
$selected.removeClass(selectedClass);
prev = $("li:last", $tpList).addClass(selectedClass)[0];
$tpDiv[0].scrollTop = prev.offsetTop - prev.offsetHeight;
}
return false;
break;
case 40: // Down arrow, similar in behaviour to up arrow.
if (showPicker()) {
return false;
};
$selected = $(selectedSelector, $tpList);
var next = $selected.next().addClass(selectedClass)[0];
if (next) {
$selected.removeClass(selectedClass);
if (next.offsetTop + next.offsetHeight > top + $tpDiv[0].offsetHeight) {
$tpDiv[0].scrollTop = top + next.offsetHeight;
}
}
else {
$selected.removeClass(selectedClass);
next = $("li:first", $tpList).addClass(selectedClass)[0];
$tpDiv[0].scrollTop = 0;
}
return false;
break;
case 13: // Enter
if ($tpDiv.is(":visible")) {
var sel = $(selectedSelector, $tpList)[0];
setTimeVal(elm, sel, $tpDiv, settings);
}
return false;
break;
case 27: // Esc
$tpDiv.hide();
return false;
break;
}
return true;
});
$(elm).keyup(function(e) {
keyDown = false;
});
// Helper function to get an inputs current time as Date object.
// Returns a Date object.
this.getTime = function() {
return timeStringToDate(elm.value, settings);
};
// Helper function to set a time input.
// Takes a Date object or string.
this.setTime = function(time) {
elm.value = formatTime(timeToDate(time, settings), settings);
// Trigger element's change events.
$(elm).change();
};
}; // End fn;
// Plugin defaults.
$.fn.timePicker.defaults = {
step:30,
startTime: new Date(0, 0, 0, 0, 0, 0),
endTime: new Date(0, 0, 0, 23, 30, 0),
separator: ':',
show24Hours: true
};
// Private functions.
function setTimeVal(elm, sel, $tpDiv, settings) {
// Update input field
elm.value = $(sel).text();
// Trigger element's change events.
$(elm).change();
// Keep focus for all but IE (which doesn't like it)
if (!$.browser.msie) {
elm.focus();
}
// Hide picker
$tpDiv.hide();
}
function formatTime(time, settings) {
var h = time.getHours();
var hours = settings.show24Hours ? h : (((h + 11) % 12) + 1);
var minutes = time.getMinutes();
return formatNumber(hours) + settings.separator + formatNumber(minutes) + (settings.show24Hours ? '' : ((h < 12) ? ' AM' : ' PM'));
}
function formatNumber(value) {
return (value < 10 ? '0' : '') + value;
}
function timeToDate(input, settings) {
return (typeof input == 'object') ? normaliseTime(input) : timeStringToDate(input, settings);
}
function timeStringToDate(input, settings) {
if (input) {
var array = input.split(settings.separator);
var hours = parseFloat(array[0]);
var minutes = parseFloat(array[1]);
// Convert AM/PM hour to 24-hour format.
if (!settings.show24Hours) {
if (hours === 12 && input.indexOf('AM') !== -1) {
hours = 0;
}
else if (hours !== 12 && input.indexOf('PM') !== -1) {
hours += 12;
}
}
var time = new Date(0, 0, 0, hours, minutes, 0);
return normaliseTime(time);
}
return null;
}
/* Normalise time object to a common date. */
function normaliseTime(time) {
time.setFullYear(2001);
time.setMonth(0);
time.setDate(0);
return time;
}
})(jQuery);