Merged changes by doublerebel, fixes get/setDate issues

This commit is contained in:
Trent Richardson 2010-12-13 14:36:59 -05:00
commit b129814e0f

View file

@ -1,8 +1,8 @@
/* /*
* jQuery timepicker addon * jQuery timepicker addon
* By: Trent Richardson [http://trentrichardson.com] * By: Trent Richardson [http://trentrichardson.com]
* Version 0.9.1 dev * Version 0.9.1-dev
* Last Modified: 12/2/2010 * Last Modified: 12/13/2010 by Charles Phillips
* *
* Copyright 2010 Trent Richardson * Copyright 2010 Trent Richardson
* Dual licensed under the MIT and GPL licenses. * Dual licensed under the MIT and GPL licenses.
@ -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,19 +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;
tp_inst._addTimePicker();
if ($.isFunction(o.beforeShow)) if ($.isFunction(o.beforeShow))
o.beforeShow(input, dp_inst); o.beforeShow(input, dp_inst);
}, },
@ -148,10 +148,9 @@ $.extend(Timepicker.prototype, {
_addTimePicker: function() { _addTimePicker: function() {
var currDT = (this.$altInput) ? var currDT = (this.$altInput) ?
this.$input.val() + ' ' + this.$altInput.val() : this.$input.val() + ' ' + this.$altInput.val() :
this.$input.val(), this.$input.val();
parsedDT = this._parseTime(currDT);
this.timeDefined = (parsedDT) ? true : false; this.timeDefined = this._parseTime(currDT);
this._injectTimePicker(); this._injectTimePicker();
}, },
@ -165,9 +164,10 @@ $.extend(Timepicker.prototype, {
.replace(/s{1,2}/ig, '(\\d?\\d)') .replace(/s{1,2}/ig, '(\\d?\\d)')
.replace(/t{1,2}/ig, '(am|pm|a|p)?') .replace(/t{1,2}/ig, '(am|pm|a|p)?')
.replace(/\s/g, '\\s?') + '$', .replace(/\s/g, '\\s?') + '$',
order = this._getFormatPositions(),
treg;
treg = timeString.match(new RegExp(regstr, 'i')), if (!this.inst) this.inst = $.datepicker._getInst(this.$input[0]);
order = this._getFormatPositions();
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.
@ -176,6 +176,8 @@ $.extend(Timepicker.prototype, {
regstr = '.{' + dp_dateFormat.length + ',}\\s+' + regstr; regstr = '.{' + dp_dateFormat.length + ',}\\s+' + regstr;
} }
treg = timeString.match(new RegExp(regstr, 'i'));
if (treg) { if (treg) {
if (order.t !== -1) if (order.t !== -1)
this.ampm = ((treg[order.t] === undefined || treg[order.t].length === 0) ? this.ampm = ((treg[order.t] === undefined || treg[order.t].length === 0) ?
@ -187,12 +189,15 @@ $.extend(Timepicker.prototype, {
this.hour = 0; // 12am = 0 hour this.hour = 0; // 12am = 0 hour
else if (this.ampm == 'PM' && treg[order.h] != '12') else if (this.ampm == 'PM' && treg[order.h] != '12')
this.hour = (parseFloat(treg[order.h]) + 12).toFixed(0); // 12pm = 12 hour, any other pm = hour + 12 this.hour = (parseFloat(treg[order.h]) + 12).toFixed(0); // 12pm = 12 hour, any other pm = hour + 12
else this.hour = treg[order.h]; else this.hour = Number(treg[order.h]);
} }
if (order.m !== -1) this.minute = treg[order.m]; if (order.m !== -1) this.minute = Number(treg[order.m]);
if (order.s !== -1) this.second = treg[order.s]; if (order.s !== -1) this.second = Number(treg[order.s]);
}
return true;
} else return false;
}, },
//######################################################################## //########################################################################
@ -434,34 +439,33 @@ $.extend(Timepicker.prototype, {
}, },
//######################################################################## //########################################################################
// when a slider moves... // when a slider moves, set the internal time...
// on time change is also called when the time is updated in the text field // on time change is also called when the time is updated in the text field
//######################################################################## //########################################################################
_onTimeChange: function(force) { _onTimeChange: function() {
var hour = (this.hour_slider) ? this.hour_slider.slider('value') : this.hour, var hour = (this.hour_slider) ? this.hour_slider.slider('value') : false,
minute = (this.minute_slider) ? this.minute_slider.slider('value') : this.minute, minute = (this.minute_slider) ? this.minute_slider.slider('value') : false,
second = (this.second_slider) ? this.second_slider.slider('value') : this.second, second = (this.second_slider) ? this.second_slider.slider('value') : false,
ampm = (hour < 11.5) ? 'AM' : 'PM', ampm = (hour < 12) ? 'AM' : 'PM';
hasChanged = false;
hour = (hour >= 11.5 && hour < 12) ? 12 : hour; // If the update was done in the input field, the input field should not be updated.
// If the update was done in the input field, this field should not be updated.
// If the update was done using the sliders, update the input field. // If the update was done using the sliders, update the input field.
if (force || this.hour != hour || this.minute != minute || this.second != second || (this.ampm.length > 0 && this.ampm != ampm)) var hasChanged = (hour != this.hour || minute != this.minute || second != this.second || (this.ampm.length > 0 && this.ampm != ampm));
hasChanged = true;
if (hasChanged) {
this.hour = parseFloat(hour).toFixed(0); if (hour) {
this.minute = parseFloat(minute).toFixed(0); this.hour = parseFloat(hour).toFixed(0);
this.second = parseFloat(second).toFixed(0); this.ampm = ampm;
this.ampm = ampm; }
if (minute) this.minute = parseFloat(minute).toFixed(0);
if (second) this.second = parseFloat(second).toFixed(0);
}
this._formatTime(); this._formatTime();
if (this.$timeObj) this.$timeObj.text(this.formattedTime); if (this.$timeObj) this.$timeObj.text(this.formattedTime);
this.timeDefined = true;
if (hasChanged) { if (hasChanged) this._updateDateTime();
this._updateDateTime();
this.timeDefined = true;
}
}, },
//######################################################################## //########################################################################
@ -504,15 +508,14 @@ $.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),
timeAvailable = dt !== null && this.timeDefined; timeAvailable = dt !== null && this.timeDefined;
this.formattedDate = $.datepicker.formatDate(dateFmt, (dt === null ? new Date() : dt), formatCfg); this.formattedDate = $.datepicker.formatDate(dateFmt, (dt === null ? new Date() : dt), formatCfg);
var formattedDateTime = this.formattedDate; var formattedDateTime = this.formattedDate;
if (dp_inst.lastVal !== undefined && (dp_inst.lastVal.length > 0 && this.$input.val().length === 0)) if (dp_inst.lastVal !== undefined && (dp_inst.lastVal.length > 0 && this.$input.val().length === 0))
return; return;
@ -561,7 +564,8 @@ $.fn.extend({
} }
else else
return this.each(function() { return this.each(function() {
$(this).datepicker($.timepicker._newInst($input, o)._defaults); var $t = $(this);
$t.datepicker($.timepicker._newInst($t, o)._defaults);
}); });
} }
}); });
@ -594,16 +598,8 @@ $.datepicker._updateDatepicker = function(inst) {
if (typeof(inst.stay_open) !== 'boolean' || inst.stay_open === false) { if (typeof(inst.stay_open) !== 'boolean' || inst.stay_open === false) {
this._base_updateDatepicker(inst); this._base_updateDatepicker(inst);
// Reload the time control when changing something in the input text field. // Reload the time control when changing something in the input text field.
this._beforeShow(inst.input, inst); var tp_inst = this._get(inst, 'timepicker');
} if(tp_inst) tp_inst._addTimePicker();
};
$.datepicker._beforeShow = function(input, inst) {
var beforeShow = this._get(inst, 'beforeShow');
if (beforeShow) {
inst.stay_open = true;
beforeShow.apply((inst.input ? inst.input[0] : null), [inst.input, inst]);
inst.stay_open = false;
} }
}; };
@ -669,7 +665,6 @@ $.datepicker._gotoToday = function(id) {
//####################################################################################### //#######################################################################################
$.datepicker._setTime = function(inst, date) { $.datepicker._setTime = function(inst, date) {
var tp_inst = this._get(inst, 'timepicker'); var tp_inst = this._get(inst, 'timepicker');
if (tp_inst) { if (tp_inst) {
var defaults = tp_inst._defaults, var defaults = tp_inst._defaults,
// calling _setTime with no date sets time to defaults // calling _setTime with no date sets time to defaults
@ -691,7 +686,8 @@ $.datepicker._setTime = function(inst, date) {
if (tp_inst.second_slider) tp_inst.second_slider.slider('value', second); if (tp_inst.second_slider) tp_inst.second_slider.slider('value', second);
else tp_inst.second = second; else tp_inst.second = second;
tp_inst._onTimeChange(true); tp_inst._onTimeChange();
//tp_inst._updateDateTime(inst);
} }
}; };
@ -703,6 +699,7 @@ $.datepicker._setTimeDatepicker = function(target, date, withDate) {
tp_inst = this._get(inst, 'timepicker'); tp_inst = this._get(inst, 'timepicker');
if (tp_inst) { if (tp_inst) {
this._setDateFromField(inst);
var tp_date; var tp_date;
if (date) { if (date) {
if (typeof date == "string") { if (typeof date == "string") {
@ -724,9 +721,9 @@ $.datepicker._setTimeDatepicker = function(target, date, withDate) {
$.datepicker._base_setDateDatepicker = $.datepicker._setDateDatepicker; $.datepicker._base_setDateDatepicker = $.datepicker._setDateDatepicker;
$.datepicker._setDateDatepicker = function(target, date) { $.datepicker._setDateDatepicker = function(target, date) {
var inst = this._getInst(target), var inst = this._getInst(target),
tp_date = !!date ? new Date(date.getTime()) : date; tp_date = !!date ? new Date(date.getTime()) : date;
this._updateDatepicker(inst); this._updateDatepicker(inst);
this._base_setDateDatepicker.apply(this, arguments); this._base_setDateDatepicker.apply(this, arguments);
this._setTimeDatepicker(target, tp_date, true); this._setTimeDatepicker(target, tp_date, true);
}; };
@ -738,16 +735,14 @@ $.datepicker._base_getDateDatepicker = $.datepicker._getDateDatepicker;
$.datepicker._getDateDatepicker = function(target, noDefault) { $.datepicker._getDateDatepicker = function(target, noDefault) {
var inst = this._getInst(target), var inst = this._getInst(target),
tp_inst = this._get(inst, 'timepicker'); tp_inst = this._get(inst, 'timepicker');
if (tp_inst){ if (tp_inst) {
this._setDateFromField(inst, noDefault); this._setDateFromField(inst, noDefault);
this._updateDatepicker(inst); var date = this._getDate(inst);
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 :
(new Date(inst.currentYear, inst.currentMonth, inst.currentDay, tp_inst.hour, tp_inst.minute, tp_inst.second));
} }
return this._base_getDateDatepicker(target, noDefault); else return this._base_getDateDatepicker(target, noDefault);
}; };
//####################################################################################### //#######################################################################################