Repository navigation
Expand file tree
/
Copy pathtimepicker.jquery.js
More file actions
327 lines (285 loc) · 11.8 KB
/
Copy pathtimepicker.jquery.js
File metadata and controls
327 lines (285 loc) · 11.8 KB
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
(function ($) {
$.fn.timePicker = function (options) {
var settings = $.extend({}, $.fn.timePicker.defaults, options); // merges defaults and passed-in options without altering either
return this.each(function () {
$.timePicker(this, settings);
});
};
$.timePicker = function (elm, settings) {
var e = $(elm)[0];
return e.timePicker || (e.timePicker = new $._timePicker(e, settings));
};
$._timePicker = function (elm, settings) {
var $callingField = $(elm);
var $timepicker = $('<div class="timepicker"></div>');
var startTime = new Date(0, 0, 0, 0, 0, 0, 0);
var endTime = new Date(0, 0, 0, 23, 60 - settings.step, 0, 0);
//Helper functions
function findElementMatchingTime() {
var time = $callingField.data('time') || settings.firstAppt;
var searchTerm = formatTime(time, settings.militaryTime);
return $('div:contains(' + searchTerm + '):first', $timepicker);
}
function selectElement($el) {
$('div', $timepicker).removeClass('selected');
$el.addClass('selected');
}
function getNextElement(numToSkip) {
var elHeight = $('div:first', $timepicker).height();
var currentPosition = $('.selected', $timepicker).index() * elHeight;
var projectedPosition = currentPosition + (numToSkip * elHeight);
var top = 0;
var bottom = $('div', $timepicker).length * elHeight;
if (projectedPosition <= top) {
return $('div:first', $timepicker);
} else if (projectedPosition >= bottom) {
return $('div:last', $timepicker);
} else {
return $($('div', $timepicker)[projectedPosition / elHeight]);
}
}
function setScrollTop(placeElementAtTop) {
var $selected = $('div.selected:first', $timepicker);
var selectedPosition = $selected.index() * $selected.height();
var above = selectedPosition < $timepicker.scrollTop();
var below = selectedPosition >= $timepicker.scrollTop() + $timepicker.height();
var onscreen = !above && !below;
/* placeElementAtTop is here so that when an onscreen element needs to be placed
at the top, such as when the timepicker is first being shown and we want to
scroll to the first appointment, we can manually override the default behavior */
if (placeElementAtTop) {
$timepicker.scrollTop(selectedPosition);
} else {
if (above) {
$timepicker.scrollTop(selectedPosition);
} else if (onscreen) {
return;
} else if (below) {
$timepicker.scrollTop(selectedPosition + $selected.height() - $timepicker.height());
}
}
}
function showTimepicker() {
if (true) { // if this isn't the start time
// Refresh the duration info
$('div', $timepicker).each(function () {
$this = $(this);
$this
.data('duration', calculateDuration(settings.showDuration.$startTime, $this))
.html(formatTime($this.data('time')) + ' ' + formatDuration($this.data('duration')));
});
}
$('.timepicker, .datepicker').hide();
var offset = $callingField.offset();
var timepickerWidth = 0;
/* this must happen before any divs within the timepicker are referenced;
otherwise, they won't exist and strange things will start happening */
$timepicker.show();
//set width to that of the widest div or the calling field's width, whichever is wider
$('div', $timepicker).each(function () { timepickerWidth = Math.max(timepickerWidth, $(this).outerWidth()); });
timepickerWidth = Math.max(timepickerWidth, $callingField.outerWidth());
$timepicker.css({
'width': timepickerWidth,
'height': settings.linesPerPage * $('div:first', $timepicker).height() + 'px',
'top': offset.top + $callingField.outerHeight(),
'left': offset.left
});
selectElement(findElementMatchingTime());
setScrollTop(true);
}
function hideTimepicker() {
$timepicker.hide();
}
function toggleTimepickerVisibility() {
if (isTimepickerVisible()) {
hideTimepicker();
} else {
showTimepicker();
}
}
function isTimepickerVisible() {
return $('div:visible', $timepicker).size() > 0;
}
function getTime($el) {
return $el.data('time');
}
function setTime(time) {
$callingField.data('time', time);
}
function setTimeFromUserInput() {
var userInput = $callingField.val();
var time = timeToDate(userInput);
setTime(time);
}
function formatTime(time) {
if (time === null) {
return '';
}
if (typeof time === 'string') {
time = timeToDate(time);
}
try {
if (typeof time !== 'object') {
throw "InvalidTypeError";
} else {
return (((time.getHours() + 11) % 12) + 1) +
':' +
(time.getMinutes() < 10 ? '0' : '') + time.getMinutes() +
' ' +
(time.getHours() < 12 ? 'am' : 'pm');
}
}
catch (e) {
if (e === "InvalidTypeError") {
alert("Parameter 'time' must be either Date or String");
}
}
}
function timeToDate(time) {
if (time !== null) {
var timeRegex = /([0-2]?[0-9]):?([0-5][0-9])?\s*([apAP][mM]?)?/;
var results = timeRegex.exec(time);
if (results !== null) {
var hours = results[1] !== undefined && results[1] !== null && results[1] !== '' ? parseFloat(results[1]) : 0,
minutes = results[2] !== undefined && results[2] !== null && results[2] !== '' ? parseFloat(results[2]) : 0,
meridian = results[3] !== undefined && results[3] !== null && results[3] !== '' ? results[3].toUpperCase() : '';
if (meridian === 'PM' && hours < 12) {
hours += 12;
} else if (meridian === 'AM') {
hours %= 12;
}
return new Date(0, 0, 0, hours, minutes, 0, 0);
} else {
return null;
}
}
}
function renderCallingField() {
$callingField.val(formatTime($callingField.data('time')));
}
function calculateDuration($startTime, $endTime) {
/* Takes any two jQuery objects have the data('time') property */
if ($startTime && $endTime) {
var startTime = $startTime.data('time');
var endTime = $endTime.data('time');
if (startTime && endTime) {
return endTime - startTime;
}
}
return null;
};
function formatDuration(duration) {
/* Takes milliseconds and returns a formatted string in hours */
if (duration === null || duration === 0) {
return '';
} else {
duration = duration / (1000 * 60 * 60);
return '(' + duration + ' hours)';
}
}
/* Build list of times -- at some point I need to refactor this using event delegation... */
for (var time = startTime; time <= endTime; time.setMinutes(time.getMinutes() + settings.step)) {
$timepicker
.append(
$('<div></div>')
.data('time', new Date(0, 0, 0, time.getHours(), time.getMinutes(), 0, 0))
.css({
'cursor': 'pointer',
'padding': '0 8px 0 2px',
'text-align': 'left',
'white-space': 'nowrap'
})
.hover(function () { selectElement($(this)); })
.click(function () {
setTime(getTime($(this)));
renderCallingField();
// Keep focus for all but IE (which doesn't like it)
if (!$.browser.msie) {
$callingField.focus();
}
hideTimepicker();
}));
}
// Build timepicker
$timepicker
.css({
'font': $callingField.css('font'),
'position': 'absolute',
'overflow': 'auto',
'background': '#FFFFFF',
'border': '1px solid #ccc',
'z-index': '99'
})
.appendTo('body')
.hide();
/* We set the time once initially just in case there
are any leftover form values in the field */
setTimeFromUserInput();
// Set up focus/click handlers
$callingField
/* focus and click both need to stop propagation so that their
* events don't bubble up to the document level and trigger
* the document.click() behavior (hiding the timepicker, that is) */
.focus(function (e) {
toggleTimepickerVisibility();
e.stopPropagation();
})
.click(function (e) { e.stopPropagation(); })
.blur(function () {
setTimeFromUserInput();
renderCallingField();
});
$(document).click(hideTimepicker);
// Keyboard navigation
$callingField.bind('keydown',
function (e) {
if (e.which in { 38: 'Up',
40: 'Down',
33: 'PageUp',
34: 'PageDown'
}) {
if (!isTimepickerVisible()) {
showTimepicker();
} else {
var $next;
if (e.which === 38) {
$next = getNextElement(-1);
} else if (e.which === 40) {
$next = getNextElement(1);
} else if (e.which === 33) {
$next = getNextElement(-1 * settings.linesPerPage);
} else if (e.which === 34) {
$next = getNextElement(1 * settings.linesPerPage);
}
selectElement($next);
setScrollTop();
}
}
if (e.which in { 13: 'Enter',
9: 'Tab',
27: 'Esc'
}) {
if (e.which === 13) {
if (isTimepickerVisible()) {
$('.selected', $timepicker).click();
} else {
$(this).blur();
}
e.stopPropagation(); // Otherwise we'll end up submitting the form
}
if (e.which == 9) {
$(this).blur();
}
hideTimepicker();
}
});
return this;
};
})(jQuery)
// Defaults
$.fn.timePicker.defaults = {
step: 30,
firstAppt: new Date(0, 0, 0, 8, 0, 0),
showDuration: false,
linesPerPage: 16
};