-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.timepicker.js
executable file
·431 lines (347 loc) · 14.6 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
/*!
* jQuery UI Timepicker 0.2.2
*
* Copyright (c) 2009 Martin Milesich (http://milesich.com/)
*
* Some parts are
* Copyright (c) 2009 AUTHORS.txt (http://jqueryui.com/about)
*
* $Id: kmtrowbr 14935 May 5 17:15 2012 jquery.timepicker.js
*
* Depends:
* ui.core.js
* ui.datepicker.js
* ui.slider.js
*/
(function ($) {
/**
* Extending default values
*/
$.extend($.datepicker._defaults, {
'stepMinutes':1, // Number of minutes to step up/down
'stepHours':1, // Number of hours to step up/down
'time24h':false, // True if 24h time
'showTime':false, // Show timepicker with datepicker
'altTimeField':'' // Selector for an alternate field to store time into
});
/**
* _hideDatepicker must be called with null
*/
$.datepicker._connectDatepickerOverride = $.datepicker._connectDatepicker;
$.datepicker._connectDatepicker = function (target, inst) {
$.datepicker._connectDatepickerOverride(target, inst);
// showButtonPanel is required with timepicker
if (this._get(inst, 'showTime')) {
inst.settings['showButtonPanel'] = true;
}
var showOn = this._get(inst, 'showOn');
if (showOn == 'button' || showOn == 'both') {
// Unbind all click events
inst.trigger.unbind('click');
// Bind new click event
inst.trigger.click(function () {
if ($.datepicker._datepickerShowing && $.datepicker._lastInput == target)
$.datepicker._hideDatepicker(null); // This override is all about the "null"
else
$.datepicker._showDatepicker(target);
return false;
});
}
};
/**
* Datepicker does not have an onShow event so I need to create it.
* What I actually doing here is copying original _showDatepicker
* method to _showDatepickerOverload method.
*/
$.datepicker._showDatepickerOverride = $.datepicker._showDatepicker;
$.datepicker._showDatepicker = function (input) {
// Call the original method which will show the datepicker
$.datepicker._showDatepickerOverride(input);
input = input.target || input;
// find from button/image trigger
if (input.nodeName.toLowerCase() != 'input') input = $('input', input.parentNode)[0];
// Do not show timepicker if datepicker is disabled
if ($.datepicker._isDisabledDatepicker(input)) return;
// Get instance to datepicker
var inst = $.datepicker._getInst(input);
var showTime = $.datepicker._get(inst, 'showTime');
// If showTime = True show the timepicker
if (showTime) $.timepicker.show(input);
};
/**
* Same as above. Here I need to extend the _checkExternalClick method
* because I don't want to close the datepicker when the sliders get focus.
*/
$.datepicker._checkExternalClickOverride = $.datepicker._checkExternalClick;
$.datepicker._checkExternalClick = function (event) {
if (!$.datepicker._curInst) return;
var $target = $(event.target);
if (($target.parents('#' + $.timepicker._mainDivId).length == 0)) {
$.datepicker._checkExternalClickOverride(event);
}
};
/**
* Datepicker has onHide event but I just want to make it simple for you
* so I hide the timepicker when datepicker hides.
*/
$.datepicker._hideDatepickerOverride = $.datepicker._hideDatepicker;
$.datepicker._hideDatepicker = function (input, duration) {
// Some lines from the original method
var inst = this._curInst;
if (!inst || (input && inst != $.data(input, PROP_NAME))) return;
// Get the value of showTime property
var showTime = this._get(inst, 'showTime');
if (input === undefined && showTime) {
if (inst.input) {
inst.input.val(this._formatDate(inst));
inst.input.trigger('change'); // fire the change event
}
this._updateAlternate(inst);
if (showTime) $.timepicker.update(this._formatDate(inst));
}
// Hide datepicker
$.datepicker._hideDatepickerOverride(input, duration);
// Hide the timepicker if enabled
if (showTime) {
$.timepicker.hide();
}
};
/**
* This is a complete replacement of the _selectDate method.
* If showed with timepicker do not close when date is selected.
*/
$.datepicker._selectDate = function (id, dateStr) {
var target = $(id);
var inst = this._getInst(target[0]);
var showTime = this._get(inst, 'showTime');
dateStr = (dateStr != null ? dateStr : this._formatDate(inst));
if (!showTime) {
if (inst.input)
inst.input.val(dateStr);
this._updateAlternate(inst);
}
var onSelect = this._get(inst, 'onSelect');
if (onSelect)
onSelect.apply((inst.input ? inst.input[0] : null), [dateStr, inst]); // trigger custom callback
else if (inst.input && !showTime)
inst.input.trigger('change'); // fire the change event
if (inst.inline)
this._updateDatepicker(inst);
else if (!inst.stayOpen) {
if (showTime) {
this._updateDatepicker(inst);
} else {
this._hideDatepicker(null, this._get(inst, 'duration'));
this._lastInput = inst.input[0];
if (typeof(inst.input[0]) != 'object')
inst.input[0].focus(); // restore focus
this._lastInput = null;
}
}
$.datepicker._updateInputTextField(target);
};
$.datepicker._updateInputTextField = function (target) {
var inst = this._getInst(target[0]);
if (inst.input) {
inst.input.val(this._formatDate(inst));
inst.input.trigger('change'); // fire the change event
}
this._updateAlternate(inst);
$.timepicker.update(this._formatDate(inst));
}
/**
* We need to resize the timepicker when the datepicker has been changed.
*/
$.datepicker._updateDatepickerOverride = $.datepicker._updateDatepicker;
$.datepicker._updateDatepicker = function (inst) {
$.datepicker._updateDatepickerOverride(inst);
$.timepicker.resize();
};
function Timepicker() {
}
Timepicker.prototype = {
init:function () {
this._mainDivId = 'ui-timepicker-div';
this._inputId = null;
this._orgValue = null;
this._orgHour = null;
this._orgMinute = null;
this._colonPos = -1;
this._visible = false;
this.tpDiv = $('<div id="' + this._mainDivId + '" class="ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all ui-helper-hidden-accessible" style="width: 100px; display: none; position: absolute;"></div>');
this._generateHtml();
},
show:function (input) {
// Get instance to datepicker
var inst = $.datepicker._getInst(input);
this._time24h = $.datepicker._get(inst, 'time24h');
this._altTimeField = $.datepicker._get(inst, 'altTimeField');
var stepMinutes = parseInt($.datepicker._get(inst, 'stepMinutes'), 10) || 1;
var stepHours = parseInt($.datepicker._get(inst, 'stepHours'), 10) || 1;
if (60 % stepMinutes != 0) {
stepMinutes = 1;
}
if (24 % stepHours != 0) {
stepHours = 1;
}
$('#hourSlider').slider('option', 'max', 24 - stepHours);
$('#hourSlider').slider('option', 'step', stepHours);
$('#minuteSlider').slider('option', 'max', 60 - stepMinutes);
$('#minuteSlider').slider('option', 'step', stepMinutes);
this._inputId = input.id;
if (!this._visible) {
this._parseTime();
this._orgValue = $('#' + this._inputId).val();
}
this.resize();
$('#' + this._mainDivId).show();
this._visible = true;
var dpDiv = $('#' + $.datepicker._mainDivId);
var dpDivPos = dpDiv.position();
var viewWidth = (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth) + $(document).scrollLeft();
var tpRight = this.tpDiv.offset().left + this.tpDiv.outerWidth();
if (tpRight > viewWidth) {
dpDiv.css('left', dpDivPos.left - (tpRight - viewWidth) - 5);
this.tpDiv.css('left', dpDiv.offset().left + dpDiv.outerWidth() + 'px');
}
},
update:function (fd) {
var curTime = $('#' + this._mainDivId + ' span.fragHours').text()
+ ':'
+ $('#' + this._mainDivId + ' span.fragMinutes').text();
if (!this._time24h) {
curTime += ' ' + $('#' + this._mainDivId + ' span.fragAmpm').text();
}
var curDate = $('#' + this._inputId).val();
$('#' + this._inputId).val(fd + ' ' + curTime);
if (this._altTimeField) {
$(this._altTimeField).each(function () {
$(this).val(curTime);
});
}
},
hide:function () {
this._visible = false;
$('#' + this._mainDivId).hide();
},
resize:function () {
var dpDiv = $('#' + $.datepicker._mainDivId);
var dpDivPos = dpDiv.position();
var hdrHeight = $('#' + $.datepicker._mainDivId + ' > div.ui-datepicker-header:first-child').height();
$('#' + this._mainDivId + ' > div.ui-datepicker-header:first-child').css('height', hdrHeight);
this.tpDiv.css({
'height':dpDiv.height(),
'top':dpDivPos.top,
'left':dpDivPos.left + dpDiv.outerWidth() + 'px'
});
$('#hourSlider').css('height', this.tpDiv.height() - (3.5 * hdrHeight));
$('#minuteSlider').css('height', this.tpDiv.height() - (3.5 * hdrHeight));
},
_generateHtml:function () {
var html = '';
html += '<div class="ui-datepicker-header ui-widget-header ui-helper-clearfix ui-corner-all">';
html += '<div class="ui-datepicker-title" style="margin:0">';
html += '<span class="fragHours">08</span><span class="delim">:</span><span class="fragMinutes">45</span> <span class="fragAmpm"></span></div></div><table>';
html += '<tr><th>Hour</th><th>Minute</th></tr>';
html += '<tr><td align="center"><div id="hourSlider" class="slider"></div></td><td align="center"><div id="minuteSlider" class="slider"></div></td></tr>';
html += '</table>';
this.tpDiv.empty().append(html);
$('body').append(this.tpDiv);
var self = this;
$('#hourSlider').slider({
orientation:"vertical",
range:'min',
min:0,
max:23,
step:1,
slide:function (event, ui) {
$.datepicker._updateInputTextField($('#' + self._inputId));
self._writeTime('hour', ui.value);
},
stop:function (event, ui) {
$.datepicker._updateInputTextField($('#' + self._inputId));
$('#' + self._inputId).focus();
}
});
$('#minuteSlider').slider({
orientation:"vertical",
range:'min',
min:0,
max:59,
step:1,
slide:function (event, ui) {
$.datepicker._updateInputTextField($('#' + self._inputId));
self._writeTime('minute', ui.value);
},
stop:function (event, ui) {
$.datepicker._updateInputTextField($('#' + self._inputId));
$('#' + self._inputId).focus();
}
});
$('#hourSlider > a').css('padding', 0);
$('#minuteSlider > a').css('padding', 0);
},
_writeTime:function (type, value) {
if (type == 'hour') {
if (!this._time24h) {
if (value < 12) {
$('#' + this._mainDivId + ' span.fragAmpm').text('am');
} else {
$('#' + this._mainDivId + ' span.fragAmpm').text('pm');
value -= 12;
}
if (value == 0) value = 12;
} else {
$('#' + this._mainDivId + ' span.fragAmpm').text('');
}
if (value < 10) value = '0' + value;
$('#' + this._mainDivId + ' span.fragHours').text(value);
}
if (type == 'minute') {
if (value < 10) value = '0' + value;
$('#' + this._mainDivId + ' span.fragMinutes').text(value);
}
},
_parseTime:function () {
var dt = $('#' + this._inputId).val();
this._colonPos = dt.search(':');
var m = 0, h = 0, a = '';
if (this._colonPos != -1) {
h = parseInt(dt.substr(this._colonPos - 2, 2), 10);
m = parseInt(dt.substr(this._colonPos + 1, 2), 10);
a = jQuery.trim(dt.substr(this._colonPos + 3, 3));
}
a = a.toLowerCase();
if (a != 'am' && a != 'pm') {
a = '';
}
if (h < 0) h = 0;
if (m < 0) m = 0;
if (h > 23) h = 23;
if (m > 59) m = 59;
if (a == 'pm' && h < 12) h += 12;
if (a == 'am' && h == 12) h = 0;
this._setTime('hour', h);
this._setTime('minute', m);
this._orgHour = h;
this._orgMinute = m;
},
_setTime:function (type, value) {
if (isNaN(value)) value = 0;
if (value < 0) value = 0;
if (value > 23 && type == 'hour') value = 23;
if (value > 59 && type == 'minute') value = 59;
if (type == 'hour') {
$('#hourSlider').slider('value', value);
}
if (type == 'minute') {
$('#minuteSlider').slider('value', value);
}
this._writeTime(type, value);
}
};
$.timepicker = new Timepicker();
$('document').ready(function () {
$.timepicker.init();
});
})(jQuery);