Skip to content

Commit b129814

Browse files
Merged changes by doublerebel, fixes get/setDate issues
2 parents 077e416 + f8cdfde commit b129814

File tree

1 file changed

+61
-66
lines changed

1 file changed

+61
-66
lines changed

jquery-ui-timepicker-addon.js

Lines changed: 61 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
22
* jQuery timepicker addon
33
* By: Trent Richardson [http://trentrichardson.com]
4-
* Version 0.9.1 dev
5-
* Last Modified: 12/2/2010
4+
* Version 0.9.1-dev
5+
* Last Modified: 12/13/2010 by Charles Phillips
66
*
77
* Copyright 2010 Trent Richardson
88
* Dual licensed under the MIT and GPL licenses.
@@ -97,6 +97,13 @@ $.extend(Timepicker.prototype, {
9797
var tp_inst = new Timepicker(),
9898
inlineSettings = {};
9999

100+
tp_inst.hour = tp_inst._defaults.hour;
101+
tp_inst.minute = tp_inst._defaults.minute;
102+
tp_inst.second = tp_inst._defaults.second;
103+
tp_inst.ampm = '';
104+
tp_inst.$input = $input;
105+
106+
100107
for (var attrName in this._defaults) {
101108
var attrValue = $input.attr('time:' + attrName);
102109
if (attrValue) {
@@ -109,19 +116,12 @@ $.extend(Timepicker.prototype, {
109116
}
110117
tp_inst._defaults = $.extend({}, this._defaults, inlineSettings, o, {
111118
beforeShow: function(input, dp_inst) {
112-
tp_inst.hour = tp_inst._defaults.hour;
113-
tp_inst.minute = tp_inst._defaults.minute;
114-
tp_inst.second = tp_inst._defaults.second;
115-
tp_inst.ampm = '';
116-
tp_inst.$input = $(input);
117119
if (o.altField)
118120
tp_inst.$altInput = $($.datepicker._get(dp_inst, 'altField'))
119121
.css({ cursor: 'pointer' })
120122
.focus(function(){
121123
$input.trigger("focus");
122124
});
123-
tp_inst.inst = dp_inst;
124-
tp_inst._addTimePicker();
125125
if ($.isFunction(o.beforeShow))
126126
o.beforeShow(input, dp_inst);
127127
},
@@ -148,10 +148,9 @@ $.extend(Timepicker.prototype, {
148148
_addTimePicker: function() {
149149
var currDT = (this.$altInput) ?
150150
this.$input.val() + ' ' + this.$altInput.val() :
151-
this.$input.val(),
152-
parsedDT = this._parseTime(currDT);
151+
this.$input.val();
153152

154-
this.timeDefined = (parsedDT) ? true : false;
153+
this.timeDefined = this._parseTime(currDT);
155154
this._injectTimePicker();
156155
},
157156

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

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

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

179+
treg = timeString.match(new RegExp(regstr, 'i'));
180+
179181
if (treg) {
180182
if (order.t !== -1)
181183
this.ampm = ((treg[order.t] === undefined || treg[order.t].length === 0) ?
@@ -187,12 +189,15 @@ $.extend(Timepicker.prototype, {
187189
this.hour = 0; // 12am = 0 hour
188190
else if (this.ampm == 'PM' && treg[order.h] != '12')
189191
this.hour = (parseFloat(treg[order.h]) + 12).toFixed(0); // 12pm = 12 hour, any other pm = hour + 12
190-
else this.hour = treg[order.h];
192+
else this.hour = Number(treg[order.h]);
191193
}
192194

193-
if (order.m !== -1) this.minute = treg[order.m];
194-
if (order.s !== -1) this.second = treg[order.s];
195-
}
195+
if (order.m !== -1) this.minute = Number(treg[order.m]);
196+
if (order.s !== -1) this.second = Number(treg[order.s]);
197+
198+
return true;
199+
200+
} else return false;
196201
},
197202

198203
//########################################################################
@@ -434,34 +439,33 @@ $.extend(Timepicker.prototype, {
434439
},
435440

436441
//########################################################################
437-
// when a slider moves...
442+
// when a slider moves, set the internal time...
438443
// on time change is also called when the time is updated in the text field
439444
//########################################################################
440-
_onTimeChange: function(force) {
441-
var hour = (this.hour_slider) ? this.hour_slider.slider('value') : this.hour,
442-
minute = (this.minute_slider) ? this.minute_slider.slider('value') : this.minute,
443-
second = (this.second_slider) ? this.second_slider.slider('value') : this.second,
444-
ampm = (hour < 11.5) ? 'AM' : 'PM',
445-
hasChanged = false;
446-
hour = (hour >= 11.5 && hour < 12) ? 12 : hour;
447-
448-
// If the update was done in the input field, this field should not be updated.
445+
_onTimeChange: function() {
446+
var hour = (this.hour_slider) ? this.hour_slider.slider('value') : false,
447+
minute = (this.minute_slider) ? this.minute_slider.slider('value') : false,
448+
second = (this.second_slider) ? this.second_slider.slider('value') : false,
449+
ampm = (hour < 12) ? 'AM' : 'PM';
450+
451+
// If the update was done in the input field, the input field should not be updated.
449452
// If the update was done using the sliders, update the input field.
450-
if (force || this.hour != hour || this.minute != minute || this.second != second || (this.ampm.length > 0 && this.ampm != ampm))
451-
hasChanged = true;
453+
var hasChanged = (hour != this.hour || minute != this.minute || second != this.second || (this.ampm.length > 0 && this.ampm != ampm));
454+
455+
if (hasChanged) {
452456

453-
this.hour = parseFloat(hour).toFixed(0);
454-
this.minute = parseFloat(minute).toFixed(0);
455-
this.second = parseFloat(second).toFixed(0);
456-
this.ampm = ampm;
457+
if (hour) {
458+
this.hour = parseFloat(hour).toFixed(0);
459+
this.ampm = ampm;
460+
}
461+
if (minute) this.minute = parseFloat(minute).toFixed(0);
462+
if (second) this.second = parseFloat(second).toFixed(0);
457463

464+
}
458465
this._formatTime();
459466
if (this.$timeObj) this.$timeObj.text(this.formattedTime);
460-
461-
if (hasChanged) {
462-
this._updateDateTime();
463-
this.timeDefined = true;
464-
}
467+
this.timeDefined = true;
468+
if (hasChanged) this._updateDateTime();
465469
},
466470

467471
//########################################################################
@@ -504,15 +508,14 @@ $.extend(Timepicker.prototype, {
504508
//########################################################################
505509
// update our input with the new date time..
506510
//########################################################################
507-
_updateDateTime: function() {
508-
var dp_inst = this.inst,
511+
_updateDateTime: function(dp_inst) {
512+
dp_inst = this.inst || dp_inst,
509513
dt = new Date(dp_inst.selectedYear, dp_inst.selectedMonth, dp_inst.selectedDay),
510514
dateFmt = $.datepicker._get(dp_inst, 'dateFormat'),
511515
formatCfg = $.datepicker._getFormatConfig(dp_inst),
512516
timeAvailable = dt !== null && this.timeDefined;
513517
this.formattedDate = $.datepicker.formatDate(dateFmt, (dt === null ? new Date() : dt), formatCfg);
514518
var formattedDateTime = this.formattedDate;
515-
516519
if (dp_inst.lastVal !== undefined && (dp_inst.lastVal.length > 0 && this.$input.val().length === 0))
517520
return;
518521

@@ -561,7 +564,8 @@ $.fn.extend({
561564
}
562565
else
563566
return this.each(function() {
564-
$(this).datepicker($.timepicker._newInst($input, o)._defaults);
567+
var $t = $(this);
568+
$t.datepicker($.timepicker._newInst($t, o)._defaults);
565569
});
566570
}
567571
});
@@ -594,16 +598,8 @@ $.datepicker._updateDatepicker = function(inst) {
594598
if (typeof(inst.stay_open) !== 'boolean' || inst.stay_open === false) {
595599
this._base_updateDatepicker(inst);
596600
// Reload the time control when changing something in the input text field.
597-
this._beforeShow(inst.input, inst);
598-
}
599-
};
600-
601-
$.datepicker._beforeShow = function(input, inst) {
602-
var beforeShow = this._get(inst, 'beforeShow');
603-
if (beforeShow) {
604-
inst.stay_open = true;
605-
beforeShow.apply((inst.input ? inst.input[0] : null), [inst.input, inst]);
606-
inst.stay_open = false;
601+
var tp_inst = this._get(inst, 'timepicker');
602+
if(tp_inst) tp_inst._addTimePicker();
607603
}
608604
};
609605

@@ -669,7 +665,6 @@ $.datepicker._gotoToday = function(id) {
669665
//#######################################################################################
670666
$.datepicker._setTime = function(inst, date) {
671667
var tp_inst = this._get(inst, 'timepicker');
672-
673668
if (tp_inst) {
674669
var defaults = tp_inst._defaults,
675670
// calling _setTime with no date sets time to defaults
@@ -691,7 +686,8 @@ $.datepicker._setTime = function(inst, date) {
691686
if (tp_inst.second_slider) tp_inst.second_slider.slider('value', second);
692687
else tp_inst.second = second;
693688

694-
tp_inst._onTimeChange(true);
689+
tp_inst._onTimeChange();
690+
//tp_inst._updateDateTime(inst);
695691
}
696692
};
697693

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

705701
if (tp_inst) {
702+
this._setDateFromField(inst);
706703
var tp_date;
707704
if (date) {
708705
if (typeof date == "string") {
@@ -724,9 +721,9 @@ $.datepicker._setTimeDatepicker = function(target, date, withDate) {
724721
$.datepicker._base_setDateDatepicker = $.datepicker._setDateDatepicker;
725722
$.datepicker._setDateDatepicker = function(target, date) {
726723
var inst = this._getInst(target),
727-
tp_date = !!date ? new Date(date.getTime()) : date;
728-
729-
this._updateDatepicker(inst);
724+
tp_date = !!date ? new Date(date.getTime()) : date;
725+
726+
this._updateDatepicker(inst);
730727
this._base_setDateDatepicker.apply(this, arguments);
731728
this._setTimeDatepicker(target, tp_date, true);
732729
};
@@ -738,16 +735,14 @@ $.datepicker._base_getDateDatepicker = $.datepicker._getDateDatepicker;
738735
$.datepicker._getDateDatepicker = function(target, noDefault) {
739736
var inst = this._getInst(target),
740737
tp_inst = this._get(inst, 'timepicker');
741-
742-
if (tp_inst){
738+
739+
if (tp_inst) {
743740
this._setDateFromField(inst, noDefault);
744-
this._updateDatepicker(inst);
745-
746-
return (!inst.currentYear || (inst.input && inst.input.val() == '')) ?
747-
null :
748-
(new Date(inst.currentYear, inst.currentMonth, inst.currentDay, tp_inst.hour, tp_inst.minute, tp_inst.second));
741+
var date = this._getDate(inst);
742+
if (date && tp_inst._parseTime($(target).val(), true)) date.setHours(tp_inst.hour, tp_inst.minute, tp_inst.second);
743+
return date;
749744
}
750-
return this._base_getDateDatepicker(target, noDefault);
745+
else return this._base_getDateDatepicker(target, noDefault);
751746
};
752747

753748
//#######################################################################################

0 commit comments

Comments
 (0)