Skip to content
120 changes: 57 additions & 63 deletions jquery-ui-timepicker-addon.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* jQuery timepicker addon
* By: Trent Richardson [http://trentrichardson.com]
* Version 0.9.1
* Last Modified: 12/2/2010
* Version 0.9.1-dev
* Last Modified: 12/5/2010 by Charles Phillips
*
* Copyright 2010 Trent Richardson
* Dual licensed under the MIT and GPL licenses.
Expand Down Expand Up @@ -97,6 +97,13 @@ $.extend(Timepicker.prototype, {
var tp_inst = new Timepicker(),
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) {
var attrValue = $input.attr('time:' + attrName);
if (attrValue) {
Expand All @@ -109,19 +116,12 @@ $.extend(Timepicker.prototype, {
}
tp_inst._defaults = $.extend({}, this._defaults, inlineSettings, o, {
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)
tp_inst.$altInput = $($.datepicker._get(dp_inst, 'altField'))
.css({ cursor: 'pointer' })
.focus(function(){
$input.trigger("focus");
});
tp_inst.inst = dp_inst;
tp_inst._addTimePicker();
if ($.isFunction(o.beforeShow))
o.beforeShow(input, dp_inst);
},
Expand All @@ -148,10 +148,9 @@ $.extend(Timepicker.prototype, {
_addTimePicker: function() {
var currDT = (this.$altInput) ?
this.$input.val() + ' ' + this.$altInput.val() :
this.$input.val(),
parsedDT = this._parseTime(currDT);
this.$input.val();

this.timeDefined = (parsedDT) ? true : false;
this.timeDefined = this._parseTime(currDT);
this._injectTimePicker();
},

Expand All @@ -165,9 +164,10 @@ $.extend(Timepicker.prototype, {
.replace(/s{1,2}/ig, '(\\d?\\d)')
.replace(/t{1,2}/ig, '(am|pm|a|p)?')
.replace(/\s/g, '\\s?') + '$',
order = this._getFormatPositions(),
treg;

treg = timeString.match(new RegExp(regstr, 'i')),
order = this._getFormatPositions();
if (!this.inst) this.inst = $.datepicker._getInst(this.$input[0]);

if (withDate || !this._defaults.timeOnly) {
// the time should come after x number of characters and a space.
Expand All @@ -176,6 +176,8 @@ $.extend(Timepicker.prototype, {
regstr = '.{' + dp_dateFormat.length + ',}\\s+' + regstr;
}

treg = timeString.match(new RegExp(regstr, 'i'));

if (treg) {
if (order.t !== -1)
this.ampm = ((treg[order.t] === undefined || treg[order.t].length === 0) ?
Expand All @@ -187,12 +189,15 @@ $.extend(Timepicker.prototype, {
this.hour = 0; // 12am = 0 hour
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
else this.hour = treg[order.h];
else this.hour = Number(treg[order.h]);
}

if (order.m !== -1) this.minute = treg[order.m];
if (order.s !== -1) this.second = treg[order.s];
}
if (order.m !== -1) this.minute = Number(treg[order.m]);
if (order.s !== -1) this.second = Number(treg[order.s]);

return true;

} else return false;
},

//########################################################################
Expand Down Expand Up @@ -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
//########################################################################
_onTimeChange: function(force) {
var hour = (this.hour_slider) ? this.hour_slider.slider('value') : this.hour,
minute = (this.minute_slider) ? this.minute_slider.slider('value') : this.minute,
second = (this.second_slider) ? this.second_slider.slider('value') : this.second,
ampm = (hour < 11.5) ? 'AM' : 'PM',
hasChanged = false;
hour = (hour >= 11.5 && hour < 12) ? 12 : hour;

// If the update was done in the input field, this field should not be updated.
_onTimeChange: function() {
var hour = (this.hour_slider) ? this.hour_slider.slider('value') : false,
minute = (this.minute_slider) ? this.minute_slider.slider('value') : false,
second = (this.second_slider) ? this.second_slider.slider('value') : false,
ampm = (hour < 12) ? 'AM' : 'PM';

// If the update was done in the input field, the input field should not be updated.
// 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))
hasChanged = true;
var hasChanged = (hour != this.hour || minute != this.minute || second != this.second || (this.ampm.length > 0 && this.ampm != ampm));

if (hasChanged) {

this.hour = parseFloat(hour).toFixed(0);
this.minute = parseFloat(minute).toFixed(0);
this.second = parseFloat(second).toFixed(0);
this.ampm = ampm;
if (hour) {
this.hour = parseFloat(hour).toFixed(0);
if (this._defaults.ampm) this.ampm = ampm;
}
if (minute) this.minute = parseFloat(minute).toFixed(0);
if (second) this.second = parseFloat(second).toFixed(0);

}
this._formatTime();
if (this.$timeObj) this.$timeObj.text(this.formattedTime);

if (hasChanged) {
this._updateDateTime();
this.timeDefined = true;
}
this.timeDefined = true;
if (hasChanged) this._updateDateTime();
},

//########################################################################
Expand Down Expand Up @@ -504,15 +508,14 @@ $.extend(Timepicker.prototype, {
//########################################################################
// update our input with the new date time..
//########################################################################
_updateDateTime: function() {
var dp_inst = this.inst,
_updateDateTime: function(dp_inst) {
dp_inst = this.inst || dp_inst,
dt = new Date(dp_inst.selectedYear, dp_inst.selectedMonth, dp_inst.selectedDay),
dateFmt = $.datepicker._get(dp_inst, 'dateFormat'),
formatCfg = $.datepicker._getFormatConfig(dp_inst),
timeAvailable = dt !== null && this.timeDefined;
this.formattedDate = $.datepicker.formatDate(dateFmt, (dt === null ? new Date() : dt), formatCfg);
var formattedDateTime = this.formattedDate;

if (dp_inst.lastVal !== undefined && (dp_inst.lastVal.length > 0 && this.$input.val().length === 0))
return;

Expand Down Expand Up @@ -594,16 +597,7 @@ $.datepicker._updateDatepicker = function(inst) {
if (typeof(inst.stay_open) !== 'boolean' || inst.stay_open === false) {
this._base_updateDatepicker(inst);
// Reload the time control when changing something in the input text field.
this._beforeShow(inst.input, inst);
}
};

$.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;
this._get(inst, 'timepicker')._addTimePicker();
}
};

Expand Down Expand Up @@ -669,7 +663,6 @@ $.datepicker._gotoToday = function(id) {
//#######################################################################################
$.datepicker._setTime = function(inst, date) {
var tp_inst = this._get(inst, 'timepicker');

if (tp_inst) {
var defaults = tp_inst._defaults,
// calling _setTime with no date sets time to defaults
Expand All @@ -691,7 +684,8 @@ $.datepicker._setTime = function(inst, date) {
if (tp_inst.second_slider) tp_inst.second_slider.slider('value', second);
else tp_inst.second = second;

tp_inst._onTimeChange(true);
tp_inst._onTimeChange();
tp_inst._updateDateTime(inst);
}
};

Expand All @@ -703,6 +697,7 @@ $.datepicker._setTimeDatepicker = function(target, date, withDate) {
tp_inst = this._get(inst, 'timepicker');

if (tp_inst) {
this._setDateFromField(inst);
var tp_date;
if (date) {
if (typeof date == "string") {
Expand All @@ -723,12 +718,8 @@ $.datepicker._setTimeDatepicker = function(target, date, withDate) {
//#######################################################################################
$.datepicker._base_setDateDatepicker = $.datepicker._setDateDatepicker;
$.datepicker._setDateDatepicker = function(target, date) {
var inst = this._getInst(target),
tp_date = new Date(date.getTime());

this._updateDatepicker(inst);
this._base_setDateDatepicker.apply(this, arguments);
this._setTimeDatepicker(target, tp_date, true);
this._setTimeDatepicker(target, date, true);
};

//#######################################################################################
Expand All @@ -738,10 +729,13 @@ $.datepicker._base_getDateDatepicker = $.datepicker._getDateDatepicker;
$.datepicker._getDateDatepicker = function(target, noDefault) {
var inst = this._getInst(target),
tp_inst = this._get(inst, 'timepicker');
if (tp_inst)
return (!inst.currentYear || (inst.input && inst.input.val() == '')) ?
null :
(new Date(inst.currentYear, inst.currentMonth, inst.currentDay, tp_inst.hour, tp_inst.minute, tp_inst.second));

if (tp_inst) {
this._setDateFromField(inst, noDefault);
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 date;
}
else return this._base_getDateDatepicker(inst);
};

Expand Down