Skip to content

Commit 3e276d3

Browse files
Pull #861 - Add check for setSelectionRange for older IE
1 parent 8fe6ab2 commit 3e276d3

File tree

3 files changed

+154
-131
lines changed

3 files changed

+154
-131
lines changed

dist/jquery-ui-timepicker-addon.js

+85-64
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
}
2727
});
2828

29-
/*
29+
/*
3030
* Timepicker manager.
3131
* Use the singleton instance of this class, $.timepicker, to interact with the time picker.
3232
* Settings for (groups of) time pickers are maintained in an instance object,
@@ -153,7 +153,7 @@
153153
support: {},
154154
control: null,
155155

156-
/*
156+
/*
157157
* Override the default settings for all instances of the time picker.
158158
* @param {Object} settings object - the new settings to use as defaults (anonymous object)
159159
* @return {Object} the manager object
@@ -226,7 +226,7 @@
226226

227227
// detect which units are supported
228228
tp_inst.support = detectSupport(
229-
tp_inst._defaults.timeFormat +
229+
tp_inst._defaults.timeFormat +
230230
(tp_inst._defaults.pickerTimeFormat ? tp_inst._defaults.pickerTimeFormat : '') +
231231
(tp_inst._defaults.altTimeFormat ? tp_inst._defaults.altTimeFormat : ''));
232232

@@ -425,7 +425,7 @@
425425
}
426426
html += '</dd>';
427427
}
428-
428+
429429
// Timezone
430430
var showTz = o.showTimezone !== null ? o.showTimezone : this.support.timezone;
431431
html += '<dt class="ui_tpicker_timezone_label' + (showTz ? '' : noDisplay) + '">' + o.timezoneText + '</dt>';
@@ -440,7 +440,7 @@
440440
$tp.prepend('<div class="ui-widget-header ui-helper-clearfix ui-corner-all">' + '<div class="ui-datepicker-title">' + o.timeOnlyTitle + '</div>' + '</div>');
441441
$dp.find('.ui-datepicker-header, .ui-datepicker-calendar').hide();
442442
}
443-
443+
444444
// add sliders, adjust grids, add events
445445
for (i = 0, l = tp_inst.units.length; i < l; i++) {
446446
litem = tp_inst.units[i];
@@ -475,7 +475,7 @@
475475
}
476476
}
477477
}
478-
478+
479479
tp_inst.control.value(tp_inst, tp_inst[f + '_slider'], litem, n);
480480

481481
tp_inst._onTimeChange();
@@ -515,7 +515,7 @@
515515
tp_inst._afterInject();
516516
});
517517
// End timezone options
518-
518+
519519
// inject timepicker into datepicker
520520
var $buttonPanel = $dp.find('.ui-datepicker-buttonpane');
521521
if ($buttonPanel.length) {
@@ -551,7 +551,7 @@
551551
var sliderAccessArgs = this._defaults.sliderAccessArgs,
552552
rtl = this._defaults.isRTL;
553553
sliderAccessArgs.isRTL = rtl;
554-
554+
555555
setTimeout(function () { // fix for inline mode
556556
if ($tp.find('.ui-slider-access').length === 0) {
557557
$tp.find('.ui-slider:visible').sliderAccess(sliderAccessArgs);
@@ -703,44 +703,44 @@
703703
}
704704
}
705705

706-
if (dp_inst.settings.minTime!==null) {
707-
var tempMinTime=new Date("01/01/1970 " + dp_inst.settings.minTime);
706+
if (dp_inst.settings.minTime!==null) {
707+
var tempMinTime=new Date("01/01/1970 " + dp_inst.settings.minTime);
708708
if (this.hour<tempMinTime.getHours()) {
709709
this.hour=this._defaults.hourMin=tempMinTime.getHours();
710-
this.minute=this._defaults.minuteMin=tempMinTime.getMinutes();
710+
this.minute=this._defaults.minuteMin=tempMinTime.getMinutes();
711711
} else if (this.hour===tempMinTime.getHours() && this.minute<tempMinTime.getMinutes()) {
712712
this.minute=this._defaults.minuteMin=tempMinTime.getMinutes();
713-
} else {
713+
} else {
714714
if (this._defaults.hourMin<tempMinTime.getHours()) {
715715
this._defaults.hourMin=tempMinTime.getHours();
716-
this._defaults.minuteMin=tempMinTime.getMinutes();
716+
this._defaults.minuteMin=tempMinTime.getMinutes();
717717
} else if (this._defaults.hourMin===tempMinTime.getHours()===this.hour && this._defaults.minuteMin<tempMinTime.getMinutes()) {
718-
this._defaults.minuteMin=tempMinTime.getMinutes();
718+
this._defaults.minuteMin=tempMinTime.getMinutes();
719719
} else {
720720
this._defaults.minuteMin=0;
721721
}
722-
}
722+
}
723723
}
724-
725-
if (dp_inst.settings.maxTime!==null) {
724+
725+
if (dp_inst.settings.maxTime!==null) {
726726
var tempMaxTime=new Date("01/01/1970 " + dp_inst.settings.maxTime);
727727
if (this.hour>tempMaxTime.getHours()) {
728-
this.hour=this._defaults.hourMax=tempMaxTime.getHours();
728+
this.hour=this._defaults.hourMax=tempMaxTime.getHours();
729+
this.minute=this._defaults.minuteMax=tempMaxTime.getMinutes();
730+
} else if (this.hour===tempMaxTime.getHours() && this.minute>tempMaxTime.getMinutes()) {
729731
this.minute=this._defaults.minuteMax=tempMaxTime.getMinutes();
730-
} else if (this.hour===tempMaxTime.getHours() && this.minute>tempMaxTime.getMinutes()) {
731-
this.minute=this._defaults.minuteMax=tempMaxTime.getMinutes();
732732
} else {
733733
if (this._defaults.hourMax>tempMaxTime.getHours()) {
734734
this._defaults.hourMax=tempMaxTime.getHours();
735-
this._defaults.minuteMax=tempMaxTime.getMinutes();
735+
this._defaults.minuteMax=tempMaxTime.getMinutes();
736736
} else if (this._defaults.hourMax===tempMaxTime.getHours()===this.hour && this._defaults.minuteMax>tempMaxTime.getMinutes()) {
737-
this._defaults.minuteMax=tempMaxTime.getMinutes();
737+
this._defaults.minuteMax=tempMaxTime.getMinutes();
738738
} else {
739739
this._defaults.minuteMax=59;
740740
}
741-
}
741+
}
742742
}
743-
743+
744744
if (adjustSliders !== undefined && adjustSliders === true) {
745745
var hourMax = parseInt((this._defaults.hourMax - ((this._defaults.hourMax - this._defaults.hourMin) % this._defaults.stepHour)), 10),
746746
minMax = parseInt((this._defaults.minuteMax - ((this._defaults.minuteMax - this._defaults.minuteMin) % this._defaults.stepMinute)), 10),
@@ -834,11 +834,11 @@
834834
// If the update was done using the sliders, update the input field.
835835
var hasChanged = (
836836
hour !== parseInt(this.hour,10) || // sliders should all be numeric
837-
minute !== parseInt(this.minute,10) ||
838-
second !== parseInt(this.second,10) ||
839-
millisec !== parseInt(this.millisec,10) ||
840-
microsec !== parseInt(this.microsec,10) ||
841-
(this.ampm.length > 0 && (hour < 12) !== ($.inArray(this.ampm.toUpperCase(), this.amNames) !== -1)) ||
837+
minute !== parseInt(this.minute,10) ||
838+
second !== parseInt(this.second,10) ||
839+
millisec !== parseInt(this.millisec,10) ||
840+
microsec !== parseInt(this.microsec,10) ||
841+
(this.ampm.length > 0 && (hour < 12) !== ($.inArray(this.ampm.toUpperCase(), this.amNames) !== -1)) ||
842842
(this.timezone !== null && timezone !== this.timezone.toString()) // could be numeric or "EST" format, so use toString()
843843
);
844844

@@ -876,15 +876,17 @@
876876
// Updates the time within the timepicker
877877
this.formattedTime = $.datepicker.formatTime(o.timeFormat, this, o);
878878
if (this.$timeObj) {
879-
var sPos = this.$timeObj[0].selectionStart;
880-
var ePos = this.$timeObj[0].selectionEnd;
881879
if (pickerTimeFormat === o.timeFormat) {
882880
this.$timeObj.val(this.formattedTime + pickerTimeSuffix);
883881
}
884882
else {
885883
this.$timeObj.val($.datepicker.formatTime(pickerTimeFormat, this, o) + pickerTimeSuffix);
886884
}
887-
this.$timeObj[0].setSelectionRange(sPos, ePos);
885+
if (this.$timeObj[0].setSelectionRange) {
886+
var sPos = this.$timeObj[0].selectionStart;
887+
var ePos = this.$timeObj[0].selectionEnd;
888+
this.$timeObj[0].setSelectionRange(sPos, ePos);
889+
}
888890
}
889891

890892
this.timeDefined = true;
@@ -911,8 +913,8 @@
911913
*/
912914
_updateDateTime: function (dp_inst) {
913915
dp_inst = this.inst || dp_inst;
914-
var dtTmp = (dp_inst.currentYear > 0?
915-
new Date(dp_inst.currentYear, dp_inst.currentMonth, dp_inst.currentDay) :
916+
var dtTmp = (dp_inst.currentYear > 0?
917+
new Date(dp_inst.currentYear, dp_inst.currentMonth, dp_inst.currentDay) :
916918
new Date(dp_inst.selectedYear, dp_inst.selectedMonth, dp_inst.selectedDay)),
917919
dt = $.datepicker._daylightSavingAdjust(dtTmp),
918920
//dt = $.datepicker._daylightSavingAdjust(new Date(dp_inst.selectedYear, dp_inst.selectedMonth, dp_inst.selectedDay)),
@@ -922,7 +924,7 @@
922924
timeAvailable = dt !== null && this.timeDefined;
923925
this.formattedDate = $.datepicker.formatDate(dateFmt, (dt === null ? new Date() : dt), formatCfg);
924926
var formattedDateTime = this.formattedDate;
925-
927+
926928
// if a slider was changed but datepicker doesn't have a value yet, set it
927929
if (dp_inst.lastVal === "") {
928930
dp_inst.currentYear = dp_inst.selectedYear;
@@ -932,7 +934,7 @@
932934

933935
/*
934936
* remove following lines to force every changes in date picker to change the input value
935-
* Bug descriptions: when an input field has a default value, and click on the field to pop up the date picker.
937+
* Bug descriptions: when an input field has a default value, and click on the field to pop up the date picker.
936938
* If the user manually empty the value in the input field, the date picker will never change selected value.
937939
*/
938940
//if (dp_inst.lastVal !== undefined && (dp_inst.lastVal.length > 0 && this.$input.val().length === 0)) {
@@ -957,7 +959,7 @@
957959
var altFormattedDateTime = '',
958960
altSeparator = this._defaults.altSeparator !== null ? this._defaults.altSeparator : this._defaults.separator,
959961
altTimeSuffix = this._defaults.altTimeSuffix !== null ? this._defaults.altTimeSuffix : this._defaults.timeSuffix;
960-
962+
961963
if (!this._defaults.timeOnly) {
962964
if (this._defaults.altFormat) {
963965
altFormattedDateTime = $.datepicker.formatDate(this._defaults.altFormat, (dt === null ? new Date() : dt), formatCfg);
@@ -1024,7 +1026,7 @@
10241026
stop: function (event, ui) {
10251027
tp_inst._onSelectHandler();
10261028
}
1027-
});
1029+
});
10281030
},
10291031
options: function (tp_inst, obj, unit, opts, val) {
10301032
if (tp_inst._defaults.isRTL) {
@@ -1037,7 +1039,7 @@
10371039
}
10381040
return obj.slider(opts);
10391041
}
1040-
var min = opts.min,
1042+
var min = opts.min,
10411043
max = opts.max;
10421044
opts.min = opts.max = null;
10431045
if (min !== undefined) {
@@ -1100,7 +1102,7 @@
11001102
if (val === undefined) {
11011103
return $t.data(opts);
11021104
}
1103-
o[opts] = val;
1105+
o[opts] = val;
11041106
}
11051107
else { o = opts; }
11061108
return tp_inst.control.create(tp_inst, obj, $t.data('unit'), $t.val(), o.min>=0 ? o.min : $t.data('min'), o.max || $t.data('max'), o.step || $t.data('step'));
@@ -1335,11 +1337,11 @@
13351337
}
13361338
catch (err2) {
13371339
$.timepicker.log("Unable to parse \ntimeString: " + s + "\ntimeFormat: " + f);
1338-
}
1340+
}
13391341
}
13401342
return false;
13411343
}; // end looseParse
1342-
1344+
13431345
if (typeof o.parse === "function") {
13441346
return o.parse(timeFormat, timeString, o);
13451347
}
@@ -1488,11 +1490,11 @@
14881490
.replace(/tT/g, ampm ? 'AaPpMm' : '')
14891491
.replace(/T/g, ampm ? 'AP' : '')
14901492
.replace(/tt/g, ampm ? 'apm' : '')
1491-
.replace(/t/g, ampm ? 'ap' : '') +
1492-
" " + tp_inst._defaults.separator +
1493-
tp_inst._defaults.timeSuffix +
1494-
(tz ? tp_inst._defaults.timezoneList.join('') : '') +
1495-
(tp_inst._defaults.amNames.join('')) + (tp_inst._defaults.pmNames.join('')) +
1493+
.replace(/t/g, ampm ? 'ap' : '') +
1494+
" " + tp_inst._defaults.separator +
1495+
tp_inst._defaults.timeSuffix +
1496+
(tz ? tp_inst._defaults.timezoneList.join('') : '') +
1497+
(tp_inst._defaults.amNames.join('')) + (tp_inst._defaults.pmNames.join('')) +
14961498
dateChars,
14971499
chr = String.fromCharCode(event.charCode === undefined ? event.keyCode : event.charCode);
14981500
return event.ctrlKey || (chr < ' ' || !dateChars || datetimeChars.indexOf(chr) > -1);
@@ -1515,11 +1517,11 @@
15151517
var altFormat = tp_inst._defaults.altFormat || tp_inst._defaults.dateFormat,
15161518
date = this._getDate(inst),
15171519
formatCfg = $.datepicker._getFormatConfig(inst),
1518-
altFormattedDateTime = '',
1519-
altSeparator = tp_inst._defaults.altSeparator ? tp_inst._defaults.altSeparator : tp_inst._defaults.separator,
1520+
altFormattedDateTime = '',
1521+
altSeparator = tp_inst._defaults.altSeparator ? tp_inst._defaults.altSeparator : tp_inst._defaults.separator,
15201522
altTimeSuffix = tp_inst._defaults.altTimeSuffix ? tp_inst._defaults.altTimeSuffix : tp_inst._defaults.timeSuffix,
15211523
altTimeFormat = tp_inst._defaults.altTimeFormat !== null ? tp_inst._defaults.altTimeFormat : tp_inst._defaults.timeFormat;
1522-
1524+
15231525
altFormattedDateTime += $.datepicker.formatTime(altTimeFormat, tp_inst, tp_inst._defaults) + altTimeSuffix;
15241526
if (!tp_inst._defaults.timeOnly && !tp_inst._defaults.altFieldTimeOnly && date !== null) {
15251527
if (tp_inst._defaults.altFormat) {
@@ -1533,7 +1535,7 @@
15331535
}
15341536
}
15351537
else {
1536-
$.datepicker._base_updateAlternate(inst);
1538+
$.datepicker._base_updateAlternate(inst);
15371539
}
15381540
};
15391541

@@ -1623,7 +1625,7 @@
16231625
tp_inst.millisec = date ? date.getMilliseconds() : defaults.millisec;
16241626
tp_inst.microsec = date ? date.getMicroseconds() : defaults.microsec;
16251627

1626-
//check if within min/max times..
1628+
//check if within min/max times..
16271629
tp_inst._limitMinMaxDateTime(inst, true);
16281630

16291631
tp_inst._onTimeChange();
@@ -1691,9 +1693,9 @@
16911693
} else {
16921694
tp_date = date;
16931695
}
1694-
1695-
// This is important if you are using the timezone option, javascript's Date
1696-
// object will only return the timezone offset for the current locale, so we
1696+
1697+
// This is important if you are using the timezone option, javascript's Date
1698+
// object will only return the timezone offset for the current locale, so we
16971699
// adjust it accordingly. If not using timezone option this won't matter..
16981700
// If a timezone is different in tp, keep the timezone as is
16991701
if (tp_inst && tp_date) {
@@ -1729,13 +1731,32 @@
17291731
}
17301732

17311733
var date = this._getDate(inst);
1732-
var currDT = $.trim((tp_inst.$altInput && tp_inst._defaults.altFieldTimeOnly) ? tp_inst.$input.val() + ' ' + tp_inst.$altInput.val() : tp_inst.$input.val());
1734+
1735+
var currDT = null;
1736+
1737+
if (tp_inst.$altInput && tp_inst._defaults.altFieldTimeOnly) {
1738+
currDT = tp_inst.$input.val() + ' ' + tp_inst.$altInput.val();
1739+
}
1740+
else if (tp_inst.$input.get(0).tagName !== 'INPUT' && tp_inst.$altInput) {
1741+
/**
1742+
* in case the datetimepicker has been applied to a non-input tag for inline UI,
1743+
* and the user has not configured the plugin to display only time in altInput,
1744+
* pick current date time from the altInput (and hope for the best, for now, until "ER1" is applied)
1745+
*
1746+
* @todo ER1. Since altInput can have a totally difference format, convert it to standard format by reading input format from "altFormat" and "altTimeFormat" option values
1747+
*/
1748+
currDT = tp_inst.$altInput.val();
1749+
}
1750+
else {
1751+
currDT = tp_inst.$input.val();
1752+
}
1753+
17331754
if (date && tp_inst._parseTime(currDT, !inst.settings.timeOnly)) {
17341755
date.setHours(tp_inst.hour, tp_inst.minute, tp_inst.second, tp_inst.millisec);
17351756
date.setMicroseconds(tp_inst.microsec);
17361757

1737-
// This is important if you are using the timezone option, javascript's Date
1738-
// object will only return the timezone offset for the current locale, so we
1758+
// This is important if you are using the timezone option, javascript's Date
1759+
// object will only return the timezone offset for the current locale, so we
17391760
// adjust it accordingly. If not using timezone option this won't matter..
17401761
if (tp_inst.timezone != null) {
17411762
// look out for DST if tz wasn't specified
@@ -1866,8 +1887,8 @@
18661887
tp_inst._defaults.onSelect = onselect;
18671888
}
18681889

1869-
// Datepicker will override our date when we call _base_optionDatepicker when
1870-
// calling minDate/maxDate, so we will first grab the value, call
1890+
// Datepicker will override our date when we call _base_optionDatepicker when
1891+
// calling minDate/maxDate, so we will first grab the value, call
18711892
// _base_optionDatepicker, then set our value back.
18721893
if(min || max){
18731894
$target = $(target);
@@ -1882,7 +1903,7 @@
18821903
}
18831904
return this._base_optionDatepicker.call($.datepicker, target, name_clone || name, value);
18841905
};
1885-
1906+
18861907
/*
18871908
* jQuery isEmptyObject does not check hasOwnProperty - if someone has added to the object prototype,
18881909
* it will return false for all objects
@@ -2040,7 +2061,7 @@
20402061
hours = (off - minutes) / 60,
20412062
iso = iso8601 ? ':' : '',
20422063
tz = (off >= 0 ? '+' : '-') + ('0' + Math.abs(hours)).slice(-2) + iso + ('0' + Math.abs(minutes)).slice(-2);
2043-
2064+
20442065
if (tz === '+00:00') {
20452066
return 'Z';
20462067
}
@@ -2184,7 +2205,7 @@
21842205
date.setMilliseconds(date.getMilliseconds() - options.minInterval);
21852206
}
21862207
}
2187-
2208+
21882209
if (date.getTime) {
21892210
other[method].call(other, 'option', option, date);
21902211
}
@@ -2210,7 +2231,7 @@
22102231
}, options, options.end));
22112232

22122233
checkDates(startTime, endTime);
2213-
2234+
22142235
selected(startTime, endTime, 'minDate');
22152236
selected(endTime, startTime, 'maxDate');
22162237

0 commit comments

Comments
 (0)