Whitespace and getDate/setDate fixes

Extra tabs removed and line endings cleaned
getDate is now properly accessible from $(target).datepicker('getDate') and correctly returns date object with time
setDate is now properly accessible from $(target).datepicker('setDate') and can set both date and time
added new method setTime, accessible from $(target).datepicker('setTime')
calling .datepicker('setDate') with no date now sets the default date as in original datepicker code
time is set to default when calling .datepicker('setTime') with no time or calling 'setDate' with a date and no time
added new internal function _parseTime to parse string times from any source
This commit is contained in:
doublerebel 2010-11-21 15:48:55 -08:00
parent badcc050c4
commit 689ba11c35

View file

@ -2,7 +2,7 @@
* jQuery timepicker addon
* By: Trent Richardson [http://trentrichardson.com]
* Version 0.8.1
* Last Modified: 11/20/2010 by Charles Phillips
* Last Modified: 11/21/2010 by Charles Phillips
*
* Copyright 2010 Trent Richardson
* Dual licensed under the MIT and GPL licenses.
@ -148,18 +148,27 @@ $.extend(Timepicker.prototype, {
var currDT = (this.$altInput) ?
this.$input.val() + ' ' + this.$altInput.val() :
this.$input.val(),
parsedDT = this._parseTime(currDT);
regstr = this._defaults.timeFormat.toString()
this.timeDefined = (parsedDT) ? true : false;
this._injectTimePicker();
},
//########################################################################
// parse the time string from input value or _setTime
//########################################################################
_parseTime: function(timeString, withDate) {
var regstr = this._defaults.timeFormat.toString()
.replace(/h{1,2}/ig, '(\\d?\\d)')
.replace(/m{1,2}/ig, '(\\d?\\d)')
.replace(/s{1,2}/ig, '(\\d?\\d)')
.replace(/t{1,2}/ig, '(am|pm|a|p)?')
.replace(/\s/g, '\\s?') + '$',
treg = currDT.match(new RegExp(regstr, 'i')),
treg = timeString.match(new RegExp(regstr, 'i')),
order = this._getFormatPositions();
if (!this._defaults.timeOnly) {
if (withDate || !this._defaults.timeOnly) {
// the time should come after x number of characters and a space.
// x = at least the length of text specified by the date format
var dp_dateFormat = $.datepicker._get(this.inst, 'dateFormat');
@ -183,9 +192,6 @@ $.extend(Timepicker.prototype, {
if (order.m !== -1) this.minute = treg[order.m];
if (order.s !== -1) this.second = treg[order.s];
}
this.timeDefined = (treg) ? true : false;
this._injectTimePicker();
},
//########################################################################
@ -344,10 +350,12 @@ $.extend(Timepicker.prototype, {
}
});
// Add grid functionality
if (o.showHour && o.hourGrid > 0) {
size = 100 * hourGridSize * o.hourGrid / (hourMax - o.hourMin);
$tp.find(".ui_tpicker_hour table").css({
width: size + "%",
marginLeft: (size / (-2 * hourGridSize)) + "%",
@ -375,6 +383,7 @@ $.extend(Timepicker.prototype, {
});
}
if (o.showMinute && o.minuteGrid > 0) {
size = 100 * minuteGridSize * o.minuteGrid / (minMax - o.minuteMin);
$tp.find(".ui_tpicker_minute table").css({
@ -537,6 +546,7 @@ $.fn.extend({
//########################################################################
// extend timepicker to datepicker
//########################################################################
datetimepicker: function(o) {
o = o || {};
var $input = this,
@ -564,16 +574,16 @@ $.fn.extend({
$.datepicker._base_selectDate = $.datepicker._selectDate;
$.datepicker._selectDate = function (id, dateStr) {
var inst = this._getInst($(id)[0]),
tp_inst = $.datepicker._get(inst, 'timepicker');
tp_inst = this._get(inst, 'timepicker');
if (tp_inst) {
inst.inline = inst.stay_open = true;
inst.stay_open = inst.inline = false;
$.datepicker._base_selectDate(id, dateStr);
this._base_selectDate(id, dateStr);
this._notifyChange(inst);
this._updateDatepicker(inst);
}
else $.datepicker._base_selectDate(id, dateStr);
else this._base_selectDate(id, dateStr);
};
//#############################################################################################
@ -651,7 +661,7 @@ $.datepicker._doKeyUp = function (event) {
//#######################################################################################
$.datepicker._base_gotoToday = $.datepicker._gotoToday;
$.datepicker._gotoToday = function(id) {
$.datepicker._base_gotoToday(id);
this._base_gotoToday(id);
this._setTime(this._getInst($(id)[0]), new Date());
};
@ -659,62 +669,79 @@ $.datepicker._gotoToday = function(id) {
// Create our own set time function
//#######################################################################################
$.datepicker._setTime = function(inst, date) {
var tp_inst = $.datepicker._get(inst, 'timepicker');
var tp_inst = this._get(inst, 'timepicker');
if (tp_inst) {
var hour = date.getHours(),
minute = date.getMinutes(),
second = date.getSeconds();
var defaults = tp_inst._defaults,
// calling _setTime with no date sets time to defaults
hour = date ? date.getHours() : defaults.hour,
minute = date ? date.getMinutes() : defaults.minute,
second = date ? date.getSeconds() : defaults.second;
//check if within min/max times..
if ((hour < tp_inst._defaults.hourMin || hour > tp_inst._defaults.hourMax) || (minute < tp_inst._defaults.minuteMin || minute > tp_inst._defaults.minuteMax) || (second < tp_inst._defaults.secondMin || second > tp_inst._defaults.secondMax)) {
hour = tp_inst._defaults.hourMin;
minute = tp_inst._defaults.minuteMin;
second = tp_inst._defaults.secondMin;
if ((hour < defaults.hourMin || hour > defaults.hourMax) || (minute < defaults.minuteMin || minute > defaults.minuteMax) || (second < defaults.secondMin || second > defaults.secondMax)) {
hour = defaults.hourMin;
minute = defaults.minuteMin;
second = defaults.secondMin;
}
if (tp_inst.hour_slider && tp_inst.minute_slider && tp_inst.second_slider) {
tp_inst.hour_slider.slider('value', hour);
tp_inst.minute_slider.slider('value', minute);
tp_inst.second_slider.slider('value', second);
} else {
tp_inst.hour = hour;
tp_inst.minute = minute;
tp_inst.second = second;
}
if (tp_inst.hour_slider) tp_inst.hour_slider.slider('value', hour);
else tp_inst.hour = hour;
if (tp_inst.minute_slider) tp_inst.minute_slider.slider('value', minute);
else tp_inst.minute = minute;
if (tp_inst.second_slider) tp_inst.second_slider.slider('value', second);
else tp_inst.second = second;
tp_inst._onTimeChange(true);
}
};
//#######################################################################################
// override setDate() to allow getting time too within Date object
// Create new public method to set only time, callable as $().datepicker('setTime', date)
//#######################################################################################
$.datepicker._base_setDate = $.datepicker._setDate;
$.datepicker._setDate = function(inst, date, noChange) {
date = date || new Date();
var tp_inst = $.datepicker._get(inst, 'timepicker'),
tp_date = new Date(date.getTime());
$.datepicker._setTimeDatepicker = function(target, date, withDate) {
var inst = this._getInst(target),
tp_inst = this._get(inst, 'timepicker');
$.datepicker._updateDatepicker(inst);
$.datepicker._base_setDate(inst, date, noChange);
if (tp_inst) this._setTime(inst, tp_date);
if (tp_inst) {
var tp_date;
if (date) {
if (typeof date == "string") {
tp_inst._parseTime(date, withDate);
tp_date = new Date();
tp_date.setHours(tp_inst.hour, tp_inst.minute, tp_inst.second);
}
else tp_date = new Date(date.getTime());
if (tp_date.toString() == 'Invalid Date') tp_date = undefined;
}
this._setTime(inst, tp_date);
}
};
//#######################################################################################
// override setDate() to allow setting time too within Date object
//#######################################################################################
$.datepicker._base_setDateDatepicker = $.datepicker._setDateDatepicker;
$.datepicker._setDateDatepicker = function(target, date) {
this._base_setDateDatepicker.apply(this, arguments);
this._setTimeDatepicker(target, date, true);
};
//#######################################################################################
// override getDate() to allow getting time too within Date object
//#######################################################################################
$.datepicker._base_getDate = $.datepicker._getDate;
$.datepicker._getDate = function(inst) {
var tp_inst = $.datepicker._get(inst, 'timepicker');
$.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));
else return $.datepicker._base_getDate(inst);
else return this._base_getDateDatepicker(inst);
};
//#######################################################################################
// jQuery extend now ignores nulls!
//#######################################################################################