Skip to content

Commit f31d5ec

Browse files
committed
Change timepicker initialization to support setting/getting time/date before date/timepicker has been opened
1 parent aef1408 commit f31d5ec

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

jquery-ui-timepicker-addon.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ $.extend(Timepicker.prototype, {
9797
var tp_inst = new Timepicker(),
9898
inlineSettings = {};
9999

100+
tp_inst.hour = tp_inst._defaults.hour;
101+
tp_inst.minute = tp_inst._defaults.minute;
102+
tp_inst.second = tp_inst._defaults.second;
103+
tp_inst.ampm = '';
104+
tp_inst.$input = $input;
105+
106+
100107
for (var attrName in this._defaults) {
101108
var attrValue = $input.attr('time:' + attrName);
102109
if (attrValue) {
@@ -109,18 +116,12 @@ $.extend(Timepicker.prototype, {
109116
}
110117
tp_inst._defaults = $.extend({}, this._defaults, inlineSettings, o, {
111118
beforeShow: function(input, dp_inst) {
112-
tp_inst.hour = tp_inst._defaults.hour;
113-
tp_inst.minute = tp_inst._defaults.minute;
114-
tp_inst.second = tp_inst._defaults.second;
115-
tp_inst.ampm = '';
116-
tp_inst.$input = $(input);
117119
if (o.altField)
118120
tp_inst.$altInput = $($.datepicker._get(dp_inst, 'altField'))
119121
.css({ cursor: 'pointer' })
120122
.focus(function(){
121123
$input.trigger("focus");
122124
});
123-
tp_inst.inst = dp_inst;
124125
if ($.isFunction(o.beforeShow))
125126
o.beforeShow(input, dp_inst);
126127
},
@@ -166,6 +167,8 @@ $.extend(Timepicker.prototype, {
166167
order = this._getFormatPositions(),
167168
treg;
168169

170+
if (!this.inst) this.inst = $.datepicker._getInst(this.$input[0]);
171+
169172
if (withDate || !this._defaults.timeOnly) {
170173
// the time should come after x number of characters and a space.
171174
// x = at least the length of text specified by the date format
@@ -462,7 +465,6 @@ $.extend(Timepicker.prototype, {
462465
this._formatTime();
463466
if (this.$timeObj) this.$timeObj.text(this.formattedTime);
464467
this.timeDefined = true;
465-
466468
if (hasChanged) this._updateDateTime();
467469
},
468470

@@ -506,8 +508,8 @@ $.extend(Timepicker.prototype, {
506508
//########################################################################
507509
// update our input with the new date time..
508510
//########################################################################
509-
_updateDateTime: function() {
510-
var dp_inst = this.inst,
511+
_updateDateTime: function(dp_inst) {
512+
dp_inst = this.inst || dp_inst,
511513
dt = new Date(dp_inst.selectedYear, dp_inst.selectedMonth, dp_inst.selectedDay),
512514
dateFmt = $.datepicker._get(dp_inst, 'dateFormat'),
513515
formatCfg = $.datepicker._getFormatConfig(dp_inst),
@@ -732,10 +734,8 @@ $.datepicker._getDateDatepicker = function(target, noDefault) {
732734
if (tp_inst) {
733735
this._setDateFromField(inst, noDefault);
734736
var date = this._getDate(inst);
735-
date.setHours(tp_inst.hour, tp_inst.minute, tp_inst.second);
736-
return (!inst.currentYear || (inst.input && inst.input.val() == '')) ?
737-
null :
738-
date;
737+
if (date && tp_inst._parseTime($(target).val(), true)) date.setHours(tp_inst.hour, tp_inst.minute, tp_inst.second);
738+
return date;
739739
}
740740
else return this._base_getDateDatepicker(inst);
741741
};

0 commit comments

Comments
 (0)