Change timepicker initialization to support setting/getting time/date before date/timepicker has been opened

This commit is contained in:
doublerebel 2010-12-05 16:51:48 -08:00
parent aef1408c87
commit f31d5ecbeb

View file

@ -97,6 +97,13 @@ $.extend(Timepicker.prototype, {
var tp_inst = new Timepicker(), var tp_inst = new Timepicker(),
inlineSettings = {}; inlineSettings = {};
tp_inst.hour = tp_inst._defaults.hour;
tp_inst.minute = tp_inst._defaults.minute;
tp_inst.second = tp_inst._defaults.second;
tp_inst.ampm = '';
tp_inst.$input = $input;
for (var attrName in this._defaults) { for (var attrName in this._defaults) {
var attrValue = $input.attr('time:' + attrName); var attrValue = $input.attr('time:' + attrName);
if (attrValue) { if (attrValue) {
@ -109,18 +116,12 @@ $.extend(Timepicker.prototype, {
} }
tp_inst._defaults = $.extend({}, this._defaults, inlineSettings, o, { tp_inst._defaults = $.extend({}, this._defaults, inlineSettings, o, {
beforeShow: function(input, dp_inst) { beforeShow: function(input, dp_inst) {
tp_inst.hour = tp_inst._defaults.hour;
tp_inst.minute = tp_inst._defaults.minute;
tp_inst.second = tp_inst._defaults.second;
tp_inst.ampm = '';
tp_inst.$input = $(input);
if (o.altField) if (o.altField)
tp_inst.$altInput = $($.datepicker._get(dp_inst, 'altField')) tp_inst.$altInput = $($.datepicker._get(dp_inst, 'altField'))
.css({ cursor: 'pointer' }) .css({ cursor: 'pointer' })
.focus(function(){ .focus(function(){
$input.trigger("focus"); $input.trigger("focus");
}); });
tp_inst.inst = dp_inst;
if ($.isFunction(o.beforeShow)) if ($.isFunction(o.beforeShow))
o.beforeShow(input, dp_inst); o.beforeShow(input, dp_inst);
}, },
@ -166,6 +167,8 @@ $.extend(Timepicker.prototype, {
order = this._getFormatPositions(), order = this._getFormatPositions(),
treg; treg;
if (!this.inst) this.inst = $.datepicker._getInst(this.$input[0]);
if (withDate || !this._defaults.timeOnly) { if (withDate || !this._defaults.timeOnly) {
// the time should come after x number of characters and a space. // the time should come after x number of characters and a space.
// x = at least the length of text specified by the date format // x = at least the length of text specified by the date format
@ -462,7 +465,6 @@ $.extend(Timepicker.prototype, {
this._formatTime(); this._formatTime();
if (this.$timeObj) this.$timeObj.text(this.formattedTime); if (this.$timeObj) this.$timeObj.text(this.formattedTime);
this.timeDefined = true; this.timeDefined = true;
if (hasChanged) this._updateDateTime(); if (hasChanged) this._updateDateTime();
}, },
@ -506,8 +508,8 @@ $.extend(Timepicker.prototype, {
//######################################################################## //########################################################################
// update our input with the new date time.. // update our input with the new date time..
//######################################################################## //########################################################################
_updateDateTime: function() { _updateDateTime: function(dp_inst) {
var dp_inst = this.inst, dp_inst = this.inst || dp_inst,
dt = new Date(dp_inst.selectedYear, dp_inst.selectedMonth, dp_inst.selectedDay), dt = new Date(dp_inst.selectedYear, dp_inst.selectedMonth, dp_inst.selectedDay),
dateFmt = $.datepicker._get(dp_inst, 'dateFormat'), dateFmt = $.datepicker._get(dp_inst, 'dateFormat'),
formatCfg = $.datepicker._getFormatConfig(dp_inst), formatCfg = $.datepicker._getFormatConfig(dp_inst),
@ -732,10 +734,8 @@ $.datepicker._getDateDatepicker = function(target, noDefault) {
if (tp_inst) { if (tp_inst) {
this._setDateFromField(inst, noDefault); this._setDateFromField(inst, noDefault);
var date = this._getDate(inst); var date = this._getDate(inst);
date.setHours(tp_inst.hour, tp_inst.minute, tp_inst.second); if (date && tp_inst._parseTime($(target).val(), true)) date.setHours(tp_inst.hour, tp_inst.minute, tp_inst.second);
return (!inst.currentYear || (inst.input && inst.input.val() == '')) ? return date;
null :
date;
} }
else return this._base_getDateDatepicker(inst); else return this._base_getDateDatepicker(inst);
}; };