Skip to content

Commit 5a59829

Browse files
accept change
2 parents 452e7e9 + b35a403 commit 5a59829

File tree

1 file changed

+98
-96
lines changed

1 file changed

+98
-96
lines changed

jquery-ui-timepicker-addon.js

Lines changed: 98 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* jQuery timepicker addon
33
* By: Trent Richardson [http://trentrichardson.com]
44
* Version 0.8.1
5-
* Last Modified: 11/15/2010 by Charles Phillips
5+
* Last Modified: 11/18/2010 by Charles Phillips
66
*
77
* Copyright 2010 Trent Richardson
88
* Dual licensed under the MIT and GPL licenses.
@@ -89,6 +89,58 @@ $.extend(Timepicker.prototype, {
8989
return this;
9090
},
9191

92+
//########################################################################
93+
// Create a new Timepicker instance
94+
//########################################################################
95+
_newInst: function($input, o) {
96+
var tp_inst = new Timepicker(),
97+
inlineSettings = {};
98+
99+
for (var attrName in tp_inst._defaults) {
100+
var attrValue = $input.attr('time:' + attrName);
101+
if (attrValue) {
102+
try {
103+
inlineSettings[attrName] = eval(attrValue);
104+
} catch (err) {
105+
inlineSettings[attrName] = attrValue;
106+
}
107+
}
108+
}
109+
tp_inst._defaults = $.extend({}, tp_inst._defaults, inlineSettings, o, {
110+
beforeShow: function(input, dp_inst) {
111+
tp_inst.hour = tp_inst._defaults.hour;
112+
tp_inst.minute = tp_inst._defaults.minute;
113+
tp_inst.second = tp_inst._defaults.second;
114+
tp_inst.ampm = '';
115+
tp_inst.$input = $(input);
116+
if (o.altField)
117+
tp_inst.$altInput = $($.datepicker._get(dp_inst, 'altField'))
118+
.css({ cursor: 'pointer' })
119+
.focus(function(){
120+
$input.trigger("focus");
121+
});
122+
tp_inst.inst = dp_inst;
123+
tp_inst._addTimePicker();
124+
if ($.isFunction(o.beforeShow))
125+
o.beforeShow(input, dp_inst);
126+
},
127+
onChangeMonthYear: function(year, month, dp_inst) {
128+
// Update the time as well : this prevents the time from disappearing from the $input field.
129+
tp_inst._updateDateTime(dp_inst);
130+
if ($.isFunction(o.onChangeMonthYear))
131+
o.onChangeMonthYear(year, month, dp_inst);
132+
},
133+
onClose: function(dateText, dp_inst) {
134+
if (tp_inst.timeDefined === true && $input.val() != '')
135+
tp_inst._updateDateTime(dp_inst);
136+
if ($.isFunction(o.onClose))
137+
o.onClose(dateText, dp_inst);
138+
},
139+
timepicker: tp_inst // add timepicker as a property of datepicker: $.datepicker._get(dp_inst, 'timepicker');
140+
});
141+
return tp_inst;
142+
},
143+
92144
//########################################################################
93145
// add our sliders to the calendar
94146
//########################################################################
@@ -108,14 +160,17 @@ $.extend(Timepicker.prototype, {
108160
order = this._getFormatPositions();
109161

110162
if (!this._defaults.timeOnly) {
111-
//the time should come after x number of characters and a space. x = at least the length of text specified by the date format
163+
// the time should come after x number of characters and a space.
164+
// x = at least the length of text specified by the date format
112165
var dp_dateFormat = $.datepicker._get(this.inst, 'dateFormat');
113166
regstr = '.{' + dp_dateFormat.length + ',}\\s+' + regstr;
114167
}
115168

116169
if (treg) {
117170
if (order.t !== -1)
118-
this.ampm = ((treg[order.t] === undefined || treg[order.t].length === 0) ? '' : (treg[order.t].charAt(0).toUpperCase() == 'A') ? 'AM' : 'PM').toUpperCase();
171+
this.ampm = ((treg[order.t] === undefined || treg[order.t].length === 0) ?
172+
'' :
173+
(treg[order.t].charAt(0).toUpperCase() == 'A') ? 'AM' : 'PM').toUpperCase();
119174

120175
if (order.h !== -1) {
121176
if (this.ampm == 'AM' && treg[order.h] == '12')
@@ -166,11 +221,13 @@ $.extend(Timepicker.prototype, {
166221
// Prevent displaying twice
167222
if ($dp.find("div#ui-timepicker-div-"+ dp_id).length === 0) {
168223
var noDisplay = ' style="display:none;"',
169-
html =
170-
'<div class="ui-timepicker-div" id="ui-timepicker-div-' + dp_id + '"><dl>' +
171-
'<dt class="ui_tpicker_time_label" id="ui_tpicker_time_label_' + dp_id + '"' + ((o.showTime) ? '' : noDisplay) + '>' + o.timeText + '</dt>' +
172-
'<dd class="ui_tpicker_time" id="ui_tpicker_time_' + dp_id + '"' + ((o.showTime) ? '' : noDisplay) + '></dd>' +
173-
'<dt class="ui_tpicker_hour_label" id="ui_tpicker_hour_label_' + dp_id + '"' + ((o.showHour) ? '' : noDisplay) + '>' + o.hourText + '</dt>',
224+
html = '<div class="ui-timepicker-div" id="ui-timepicker-div-' + dp_id + '"><dl>' +
225+
'<dt class="ui_tpicker_time_label" id="ui_tpicker_time_label_' + dp_id + '"' +
226+
((o.showTime) ? '' : noDisplay) + '>' + o.timeText + '</dt>' +
227+
'<dd class="ui_tpicker_time" id="ui_tpicker_time_' + dp_id + '"' +
228+
((o.showTime) ? '' : noDisplay) + '></dd>' +
229+
'<dt class="ui_tpicker_hour_label" id="ui_tpicker_hour_label_' + dp_id + '"' +
230+
((o.showHour) ? '' : noDisplay) + '>' + o.hourText + '</dt>',
174231
hourGridSize = 0,
175232
minuteGridSize = 0,
176233
secondGridSize = 0,
@@ -195,13 +252,16 @@ $.extend(Timepicker.prototype, {
195252

196253
html += '</tr></table></div>' +
197254
'</dd>';
198-
} else html += '<dd class="ui_tpicker_hour" id="ui_tpicker_hour_' + dp_id + '"' + ((o.showHour) ? '' : noDisplay) + '></dd>';
255+
} else html += '<dd class="ui_tpicker_hour" id="ui_tpicker_hour_' + dp_id + '"' +
256+
((o.showHour) ? '' : noDisplay) + '></dd>';
199257

200-
html += '<dt class="ui_tpicker_minute_label" id="ui_tpicker_minute_label_' + dp_id + '"' + ((o.showMinute) ? '' : noDisplay) + '>' + o.minuteText + '</dt>';
258+
html += '<dt class="ui_tpicker_minute_label" id="ui_tpicker_minute_label_' + dp_id + '"' +
259+
((o.showMinute) ? '' : noDisplay) + '>' + o.minuteText + '</dt>';
201260

202261
if (o.showMinute && o.minuteGrid > 0) {
203262
html += '<dd class="ui_tpicker_minute ui_tpicker_minute_' + o.minuteGrid + '">' +
204-
'<div id="ui_tpicker_minute_' + dp_id + '"' + ((o.showMinute) ? '' : noDisplay) + '></div>' +
263+
'<div id="ui_tpicker_minute_' + dp_id + '"' +
264+
((o.showMinute) ? '' : noDisplay) + '></div>' +
205265
'<div style="padding-left: 1px"><table><tr>';
206266

207267
for (var m = 0; m < minMax; m += o.minuteGrid) {
@@ -211,13 +271,16 @@ $.extend(Timepicker.prototype, {
211271

212272
html += '</tr></table></div>' +
213273
'</dd>';
214-
} else html += '<dd class="ui_tpicker_minute" id="ui_tpicker_minute_' + dp_id + '"' + ((o.showMinute) ? '' : noDisplay) + '></dd>';
274+
} else html += '<dd class="ui_tpicker_minute" id="ui_tpicker_minute_' + dp_id + '"' +
275+
((o.showMinute) ? '' : noDisplay) + '></dd>';
215276

216-
html += '<dt class="ui_tpicker_second_label" id="ui_tpicker_second_label_' + dp_id + '"' + ((o.showSecond) ? '' : noDisplay) + '>' + o.secondText + '</dt>';
277+
html += '<dt class="ui_tpicker_second_label" id="ui_tpicker_second_label_' + dp_id + '"' +
278+
((o.showSecond) ? '' : noDisplay) + '>' + o.secondText + '</dt>';
217279

218280
if (o.showSecond && o.secondGrid > 0) {
219281
html += '<dd class="ui_tpicker_second ui_tpicker_second_' + o.secondGrid + '">' +
220-
'<div id="ui_tpicker_second_' + dp_id + '"' + ((o.showSecond) ? '' : noDisplay) + '></div>' +
282+
'<div id="ui_tpicker_second_' + dp_id + '"' +
283+
((o.showSecond) ? '' : noDisplay) + '></div>' +
221284
'<div style="padding-left: 1px"><table><tr>';
222285

223286
for (var s = 0; s < secMax; s += o.secondGrid) {
@@ -227,7 +290,8 @@ $.extend(Timepicker.prototype, {
227290

228291
html += '</tr></table></div>' +
229292
'</dd>';
230-
} else html += '<dd class="ui_tpicker_second" id="ui_tpicker_second_' + dp_id + '"' + ((o.showSecond) ? '' : noDisplay) + '></dd>';
293+
} else html += '<dd class="ui_tpicker_second" id="ui_tpicker_second_' + dp_id + '"' +
294+
((o.showSecond) ? '' : noDisplay) + '></dd>';
231295

232296
html += '</dl></div>';
233297
$tp = $(html);
@@ -348,8 +412,8 @@ $.extend(Timepicker.prototype, {
348412
});
349413
}
350414

351-
var buttonPanel = $dp.find('.ui-datepicker-buttonpane');
352-
if (buttonPanel.length > 0) $dp.find('.ui-datepicker-buttonpane').before($tp);
415+
var $buttonPanel = $dp.find('.ui-datepicker-buttonpane');
416+
if ($buttonPanel.length) $buttonPanel.before($tp);
353417
else $dp.append($tp);
354418

355419
this.$timeObj = $('#ui_tpicker_time_'+ dp_id);
@@ -363,7 +427,7 @@ $.extend(Timepicker.prototype, {
363427
},
364428

365429
//########################################################################
366-
// when a slider moves..
430+
// when a slider moves...
367431
// on time change is also called when the time is updated in the text field
368432
//########################################################################
369433
_onTimeChange: function(force) {
@@ -397,8 +461,8 @@ $.extend(Timepicker.prototype, {
397461
// format the time all pretty...
398462
//########################################################################
399463
_formatTime: function() {
400-
var tmptime = this._defaults.timeFormat.toString();
401-
var hour12 = ((this.ampm == 'AM') ? (this.hour) : (this.hour % 12));
464+
var tmptime = this._defaults.timeFormat.toString(),
465+
hour12 = ((this.ampm == 'AM') ? (this.hour) : (this.hour % 12));
402466
hour12 = (Number(hour12) === 0) ? 12 : hour12;
403467

404468
if (this._defaults.ampm === true) {
@@ -460,13 +524,13 @@ $.fn.extend({
460524
// shorthand just to use timepicker..
461525
//########################################################################
462526
timepicker: function(o) {
527+
o = o || {};
463528
var tmp_args = arguments;
464529

465-
if(o === undefined) o = { timeOnly: true };
466-
else if (typeof o == 'object') o = $.extend(o, { timeOnly: true });
530+
if (typeof o == 'object') tmp_args[0] = $.extend(o, { timeOnly: true });
467531

468532
return this.each(function() {
469-
$(this).datetimepicker(o, tmp_args[1], tmp_args[2], tmp_args[3], tmp_args[4]);
533+
$.fn.datepicker.apply(this, tmp_args);
470534
});
471535
},
472536

@@ -478,75 +542,14 @@ $.fn.extend({
478542
var $input = this,
479543
tmp_args = arguments;
480544

481-
if (typeof(o) == 'string') {
482-
if (o == 'setDate') return this.each(function() {
483-
return $(this).datepicker(o, tmp_args[1]);
484-
});
485-
else if(o == 'option' && typeof(tmp_args[1]) == 'string') return this.each(function() {
486-
return $(this).datepicker(o, tmp_args[1], tmp_args[2]);
487-
});
488-
else if(o == 'dialog') return this.each(function() {
489-
return $(this).datepicker(o, tmp_args[1], tmp_args[2], tmp_args[3], tmp_args[4]);
490-
});
491-
else return this.each(function() {
492-
return $(this).datepicker(o);
493-
});
494-
} else {
495-
var tp_inst = new Timepicker(),
496-
inlineSettings = {};
497-
498-
for (var attrName in tp_inst._defaults) {
499-
var attrValue = $input.attr('time:' + attrName);
500-
if (attrValue) {
501-
try {
502-
inlineSettings[attrName] = eval(attrValue);
503-
} catch (err) {
504-
inlineSettings[attrName] = attrValue;
505-
}
506-
}
507-
}
508-
tp_inst._defaults = $.extend({}, tp_inst._defaults, inlineSettings, o, {
509-
beforeShow: function(input, dp_inst) {
510-
tp_inst.hour = tp_inst._defaults.hour;
511-
tp_inst.minute = tp_inst._defaults.minute;
512-
tp_inst.second = tp_inst._defaults.second;
513-
tp_inst.ampm = '';
514-
tp_inst.$input = $(input);
515-
if (o.altField) {
516-
tp_inst.$altInput = $($.datepicker._get(dp_inst, 'altField'))
517-
.css({ cursor: 'pointer' })
518-
.focus(function(){
519-
$input.trigger("focus");
520-
});
521-
}
522-
tp_inst.inst = dp_inst;
523-
tp_inst._addTimePicker();
524-
if ($.isFunction(o.beforeShow)) {
525-
o.beforeShow(input, dp_inst);
526-
}
527-
},
528-
onChangeMonthYear: function(year, month, dp_inst) {
529-
// Update the time as well : this prevents the time from disappearing from the $input field.
530-
tp_inst._updateDateTime(dp_inst);
531-
if ($.isFunction(o.onChangeMonthYear)) {
532-
o.onChangeMonthYear(year, month, dp_inst);
533-
}
534-
},
535-
onClose: function(dateText, dp_inst) {
536-
if(tp_inst.timeDefined === true && $input.val() != '') {
537-
tp_inst._updateDateTime(dp_inst);
538-
}
539-
if ($.isFunction(o.onClose)) {
540-
o.onClose(dateText, dp_inst);
541-
}
542-
},
543-
timepicker: tp_inst // add timepicker as a property of datepicker: $.datepicker._get(dp_inst, 'timepicker');
545+
if (typeof(o) == 'string')
546+
return this.each(function() {
547+
$.fn.datepicker.apply(this, tmp_args);
544548
});
545-
549+
else
546550
return this.each(function() {
547-
$(this).datepicker(tp_inst._defaults);
551+
$(this).datepicker($.timepicker._newInst($input, o)._defaults);
548552
});
549-
}
550553
}
551554
});
552555

@@ -601,9 +604,9 @@ $.datepicker._doKeyPress = function(event) {
601604

602605
if (tp_inst) {
603606
if ($.datepicker._get(inst, 'constrainInput')) {
604-
var dateChars = $.datepicker._possibleChars($.datepicker._get(inst, 'dateFormat'));
605-
var chr = String.fromCharCode(event.charCode === undefined ? event.keyCode : event.charCode);
606-
var chrl = chr.toLowerCase();
607+
var dateChars = $.datepicker._possibleChars($.datepicker._get(inst, 'dateFormat')),
608+
chr = String.fromCharCode(event.charCode === undefined ? event.keyCode : event.charCode),
609+
chrl = chr.toLowerCase();
607610
// keyCode == 58 => ":"
608611
// keyCode == 32 => " "
609612
return event.ctrlKey || (chr < ' ' || !dateChars || dateChars.indexOf(chr) > -1 || event.keyCode == 58 || event.keyCode == 32 || chr == ':' || chr == ' ' || chrl == 'a' || chrl == 'p' || chrl == 'm');
@@ -649,7 +652,7 @@ $.datepicker._gotoToday = function(id) {
649652
$.datepicker._setTime = function(inst, date) {
650653
var tp_inst = $.datepicker._get(inst, 'timepicker');
651654

652-
if(tp_inst){
655+
if (tp_inst) {
653656
var hour = date.getHours(),
654657
minute = date.getMinutes(),
655658
second = date.getSeconds();
@@ -696,7 +699,7 @@ $.datepicker._base_getDate = $.datepicker._getDate;
696699
$.datepicker._getDate = function(inst) {
697700
var tp_inst = $.datepicker._get(inst, 'timepicker');
698701
if (tp_inst)
699-
return startDate = (!inst.currentYear || (inst.input && inst.input.val() == '')) ?
702+
return (!inst.currentYear || (inst.input && inst.input.val() == '')) ?
700703
null :
701704
(new Date(inst.currentYear, inst.currentMonth, inst.currentDay, tp_inst.hour, tp_inst.minute, tp_inst.second));
702705
else return $.datepicker._base_getDate(inst);
@@ -709,7 +712,7 @@ $.datepicker._getDate = function(inst) {
709712
function extendRemove(target, props) {
710713
$.extend(target, props);
711714
for (var name in props)
712-
if (props[name] == null || props[name] == undefined)
715+
if (props[name] === null || props[name] === undefined)
713716
target[name] = props[name];
714717
return target;
715718
}
@@ -718,4 +721,3 @@ $.timepicker = new Timepicker(); // singleton instance
718721
$.timepicker.version = "0.8.1";
719722

720723
})(jQuery);
721-

0 commit comments

Comments
 (0)