Skip to content

Commit b35a403

Browse files
committed
Created _newInst fn to create 1 Timepicker per input when called on a jQuery collection
Fixed $buttonPanel typo Consolidated .timepicker(), .datetimepicker() fns by using .apply() Fixed == to === when comparing null and undefined in extendRemove fn (thanks JSLint!)
1 parent e402be7 commit b35a403

File tree

1 file changed

+62
-67
lines changed

1 file changed

+62
-67
lines changed

jquery-ui-timepicker-addon.js

Lines changed: 62 additions & 67 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
//########################################################################
@@ -361,7 +413,7 @@ $.extend(Timepicker.prototype, {
361413
}
362414

363415
var $buttonPanel = $dp.find('.ui-datepicker-buttonpane');
364-
if (buttonPanel.length) $buttonPanel.before($tp);
416+
if ($buttonPanel.length) $buttonPanel.before($tp);
365417
else $dp.append($tp);
366418

367419
this.$timeObj = $('#ui_tpicker_time_'+ dp_id);
@@ -475,10 +527,10 @@ $.fn.extend({
475527
o = o || {};
476528
var tmp_args = arguments;
477529

478-
if (typeof o == 'object') o = $.extend(o, { timeOnly: true });
530+
if (typeof o == 'object') tmp_args[0] = $.extend(o, { timeOnly: true });
479531

480532
return this.each(function() {
481-
$(this).datetimepicker(o, tmp_args[1], tmp_args[2], tmp_args[3], tmp_args[4]);
533+
$.fn.datepicker.apply(this, tmp_args);
482534
});
483535
},
484536

@@ -490,70 +542,14 @@ $.fn.extend({
490542
var $input = this,
491543
tmp_args = arguments;
492544

493-
if (typeof(o) == 'string') {
494-
if (o == 'setDate') return this.each(function() {
495-
$(this).datepicker(o, tmp_args[1]);
496-
});
497-
else if(o == 'option' && typeof(tmp_args[1]) == 'string') return this.each(function() {
498-
$(this).datepicker(o, tmp_args[1], tmp_args[2]);
499-
});
500-
else if(o == 'dialog') return this.each(function() {
501-
$(this).datepicker(o, tmp_args[1], tmp_args[2], tmp_args[3], tmp_args[4]);
502-
});
503-
else return this.each(function() {
504-
$(this).datepicker(o);
505-
});
506-
} else {
507-
var tp_inst = new Timepicker(),
508-
inlineSettings = {};
509-
510-
for (var attrName in tp_inst._defaults) {
511-
var attrValue = $input.attr('time:' + attrName);
512-
if (attrValue) {
513-
try {
514-
inlineSettings[attrName] = eval(attrValue);
515-
} catch (err) {
516-
inlineSettings[attrName] = attrValue;
517-
}
518-
}
519-
}
520-
tp_inst._defaults = $.extend({}, tp_inst._defaults, inlineSettings, o, {
521-
beforeShow: function(input, dp_inst) {
522-
tp_inst.hour = tp_inst._defaults.hour;
523-
tp_inst.minute = tp_inst._defaults.minute;
524-
tp_inst.second = tp_inst._defaults.second;
525-
tp_inst.ampm = '';
526-
tp_inst.$input = $(input);
527-
if (o.altField)
528-
tp_inst.$altInput = $($.datepicker._get(dp_inst, 'altField'))
529-
.css({ cursor: 'pointer' })
530-
.focus(function(){
531-
$input.trigger("focus");
532-
});
533-
tp_inst.inst = dp_inst;
534-
tp_inst._addTimePicker();
535-
if ($.isFunction(o.beforeShow))
536-
o.beforeShow(input, dp_inst);
537-
},
538-
onChangeMonthYear: function(year, month, dp_inst) {
539-
// Update the time as well : this prevents the time from disappearing from the $input field.
540-
tp_inst._updateDateTime(dp_inst);
541-
if ($.isFunction(o.onChangeMonthYear))
542-
o.onChangeMonthYear(year, month, dp_inst);
543-
},
544-
onClose: function(dateText, dp_inst) {
545-
if (tp_inst.timeDefined === true && $input.val() != '')
546-
tp_inst._updateDateTime(dp_inst);
547-
if ($.isFunction(o.onClose))
548-
o.onClose(dateText, dp_inst);
549-
},
550-
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);
551548
});
552-
549+
else
553550
return this.each(function() {
554-
$(this).datepicker(tp_inst._defaults);
551+
$(this).datepicker($.timepicker._newInst($input, o)._defaults);
555552
});
556-
}
557553
}
558554
});
559555

@@ -716,7 +712,7 @@ $.datepicker._getDate = function(inst) {
716712
function extendRemove(target, props) {
717713
$.extend(target, props);
718714
for (var name in props)
719-
if (props[name] == null || props[name] == undefined)
715+
if (props[name] === null || props[name] === undefined)
720716
target[name] = props[name];
721717
return target;
722718
}
@@ -725,4 +721,3 @@ $.timepicker = new Timepicker(); // singleton instance
725721
$.timepicker.version = "0.8.1";
726722

727723
})(jQuery);
728-

0 commit comments

Comments
 (0)