From e97d06e554d07063b02cfe8edba5ddf4c89a9cfa Mon Sep 17 00:00:00 2001 From: doublerebel Date: Sun, 5 Dec 2010 13:54:32 -0800 Subject: [PATCH 1/8] Bugfixes for _parseDate() --- jquery-ui-timepicker-addon.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/jquery-ui-timepicker-addon.js b/jquery-ui-timepicker-addon.js index 11834a9f..90dd5c47 100644 --- a/jquery-ui-timepicker-addon.js +++ b/jquery-ui-timepicker-addon.js @@ -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. @@ -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(); }, @@ -165,9 +164,8 @@ $.extend(Timepicker.prototype, { .replace(/s{1,2}/ig, '(\\d?\\d)') .replace(/t{1,2}/ig, '(am|pm|a|p)?') .replace(/\s/g, '\\s?') + '$', - - treg = timeString.match(new RegExp(regstr, 'i')), - order = this._getFormatPositions(); + order = this._getFormatPositions(), + treg; if (withDate || !this._defaults.timeOnly) { // the time should come after x number of characters and a space. @@ -176,6 +174,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) ? @@ -187,12 +187,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; }, //######################################################################## From 496ee3acfd580fece034a0cd3e8a420151d4e836 Mon Sep 17 00:00:00 2001 From: doublerebel Date: Sun, 5 Dec 2010 13:59:22 -0800 Subject: [PATCH 2/8] Bugfix for _onTimeChange to always format time and set timeDefined --- jquery-ui-timepicker-addon.js | 40 +++++++++++++++++------------------ 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/jquery-ui-timepicker-addon.js b/jquery-ui-timepicker-addon.js index 90dd5c47..4b269a4b 100644 --- a/jquery-ui-timepicker-addon.js +++ b/jquery-ui-timepicker-addon.js @@ -440,31 +440,31 @@ $.extend(Timepicker.prototype, { // when a slider moves... // 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); + 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(); }, //######################################################################## From 48620930ab3f5a19413685a391ec90e3e3ee14d2 Mon Sep 17 00:00:00 2001 From: doublerebel Date: Sun, 5 Dec 2010 14:04:57 -0800 Subject: [PATCH 3/8] Bugfix for correct timepicker injection Prevents beforeShow from being called twice, which prevents injectTimepicker from being called twice --- jquery-ui-timepicker-addon.js | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/jquery-ui-timepicker-addon.js b/jquery-ui-timepicker-addon.js index 4b269a4b..6010a18f 100644 --- a/jquery-ui-timepicker-addon.js +++ b/jquery-ui-timepicker-addon.js @@ -121,7 +121,6 @@ $.extend(Timepicker.prototype, { $input.trigger("focus"); }); tp_inst.inst = dp_inst; - tp_inst._addTimePicker(); if ($.isFunction(o.beforeShow)) o.beforeShow(input, dp_inst); }, @@ -437,7 +436,7 @@ $.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() { @@ -597,16 +596,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(); } }; @@ -726,12 +716,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); }; //####################################################################################### From 3ca9079892ece2a1cdfe340c327a7d8cfe4fcd6a Mon Sep 17 00:00:00 2001 From: doublerebel Date: Sun, 5 Dec 2010 14:18:16 -0800 Subject: [PATCH 4/8] Update text field when time is set with setDate or setTime methods (fixes bug I introduced in commit 496ee3a for _onTimeChange) --- jquery-ui-timepicker-addon.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/jquery-ui-timepicker-addon.js b/jquery-ui-timepicker-addon.js index 6010a18f..88e7789e 100644 --- a/jquery-ui-timepicker-addon.js +++ b/jquery-ui-timepicker-addon.js @@ -684,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(); } }; From aef1408c876440dbc00fe1abd18535bbc51e4493 Mon Sep 17 00:00:00 2001 From: doublerebel Date: Sun, 5 Dec 2010 14:47:07 -0800 Subject: [PATCH 5/8] Set datepicker date from field before returning date for getDate method (Fixes Issue 44) --- jquery-ui-timepicker-addon.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/jquery-ui-timepicker-addon.js b/jquery-ui-timepicker-addon.js index 88e7789e..dad0b080 100644 --- a/jquery-ui-timepicker-addon.js +++ b/jquery-ui-timepicker-addon.js @@ -728,10 +728,15 @@ $.datepicker._base_getDateDatepicker = $.datepicker._getDateDatepicker; $.datepicker._getDateDatepicker = function(target, noDefault) { var inst = this._getInst(target), tp_inst = this._get(inst, 'timepicker'); - if (tp_inst) + + if (tp_inst) { + this._setDateFromField(inst, noDefault); + var date = this._getDate(inst); + date.setHours(tp_inst.hour, tp_inst.minute, tp_inst.second); 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)); + date; + } else return this._base_getDateDatepicker(inst); }; From f31d5ecbeb0801bb65003a34a74fb2cac74ab83f Mon Sep 17 00:00:00 2001 From: doublerebel Date: Sun, 5 Dec 2010 16:51:48 -0800 Subject: [PATCH 6/8] Change timepicker initialization to support setting/getting time/date before date/timepicker has been opened --- jquery-ui-timepicker-addon.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/jquery-ui-timepicker-addon.js b/jquery-ui-timepicker-addon.js index dad0b080..62e18945 100644 --- a/jquery-ui-timepicker-addon.js +++ b/jquery-ui-timepicker-addon.js @@ -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) { @@ -109,18 +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; if ($.isFunction(o.beforeShow)) o.beforeShow(input, dp_inst); }, @@ -166,6 +167,8 @@ $.extend(Timepicker.prototype, { order = this._getFormatPositions(), treg; + 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. // x = at least the length of text specified by the date format @@ -462,7 +465,6 @@ $.extend(Timepicker.prototype, { this._formatTime(); if (this.$timeObj) this.$timeObj.text(this.formattedTime); this.timeDefined = true; - if (hasChanged) this._updateDateTime(); }, @@ -506,8 +508,8 @@ $.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), @@ -732,10 +734,8 @@ $.datepicker._getDateDatepicker = function(target, noDefault) { if (tp_inst) { this._setDateFromField(inst, noDefault); var date = this._getDate(inst); - date.setHours(tp_inst.hour, tp_inst.minute, tp_inst.second); - return (!inst.currentYear || (inst.input && inst.input.val() == '')) ? - null : - date; + 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); }; From 454af7bcf01d90212ccb7745bab1d6e7a0e2b4a8 Mon Sep 17 00:00:00 2001 From: doublerebel Date: Sun, 5 Dec 2010 17:19:27 -0800 Subject: [PATCH 7/8] Reset time to defaults when .datepicker('setTime') is called with no arguments --- jquery-ui-timepicker-addon.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/jquery-ui-timepicker-addon.js b/jquery-ui-timepicker-addon.js index 62e18945..5fab2479 100644 --- a/jquery-ui-timepicker-addon.js +++ b/jquery-ui-timepicker-addon.js @@ -516,7 +516,6 @@ $.extend(Timepicker.prototype, { 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; @@ -664,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 @@ -687,7 +685,7 @@ $.datepicker._setTime = function(inst, date) { else tp_inst.second = second; tp_inst._onTimeChange(); - tp_inst._updateDateTime(); + tp_inst._updateDateTime(inst); } }; @@ -699,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") { From 351619dcff616e2d243ccf7c210dee72f3c3ef17 Mon Sep 17 00:00:00 2001 From: doublerebel Date: Sat, 18 Dec 2010 17:49:50 -0800 Subject: [PATCH 8/8] Fix for bug that caused 24h time to be parsed as 12h time --- jquery-ui-timepicker-addon.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jquery-ui-timepicker-addon.js b/jquery-ui-timepicker-addon.js index 5fab2479..3d7b0878 100644 --- a/jquery-ui-timepicker-addon.js +++ b/jquery-ui-timepicker-addon.js @@ -194,7 +194,7 @@ $.extend(Timepicker.prototype, { 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; @@ -456,7 +456,7 @@ $.extend(Timepicker.prototype, { if (hour) { this.hour = parseFloat(hour).toFixed(0); - this.ampm = ampm; + if (this._defaults.ampm) this.ampm = ampm; } if (minute) this.minute = parseFloat(minute).toFixed(0); if (second) this.second = parseFloat(second).toFixed(0);