Skip to content

Commit 3fc9b00

Browse files
committed
Keep cursor position and selection of text field
1 parent c73678c commit 3fc9b00

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

jquery-ui-timepicker-addon.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,7 @@ $.extend(Timepicker.prototype, {
842842

843843
this.formattedDateTime = formattedDateTime;
844844

845+
var range = getSelectionRange(this);
845846
if(!this._defaults.showTimepicker) {
846847
this.$input.val(this.formattedDate);
847848
} else if (this.$altInput && this._defaults.altFieldTimeOnly === true) {
@@ -853,6 +854,7 @@ $.extend(Timepicker.prototype, {
853854
} else {
854855
this.$input.val(formattedDateTime);
855856
}
857+
restoreSelectionRange(this, range);
856858

857859
this.$input.trigger("change");
858860
}
@@ -1085,10 +1087,12 @@ $.datepicker._selectDate = function (id, dateStr) {
10851087
tp_inst = this._get(inst, 'timepicker');
10861088

10871089
if (tp_inst) {
1090+
var range = getSelectionRange(inst);
10881091
tp_inst._limitMinMaxDateTime(inst, true);
10891092
inst.inline = inst.stay_open = true;
10901093
//This way the onSelect handler called from calendarpicker get the full dateTime
10911094
this._base_selectDate(id, dateStr);
1095+
setSelectionRange(inst, range);
10921096
inst.inline = inst.stay_open = false;
10931097
this._notifyChange(inst);
10941098
this._updateDatepicker(inst);
@@ -1194,11 +1198,13 @@ $.datepicker._base_gotoToday = $.datepicker._gotoToday;
11941198
$.datepicker._gotoToday = function(id) {
11951199
var inst = this._getInst($(id)[0]),
11961200
$dp = inst.dpDiv;
1201+
var range = getSelectionRange(inst);
11971202
this._base_gotoToday(id);
11981203
var tp_inst = this._get(inst, 'timepicker');
11991204
selectLocalTimeZone(tp_inst);
12001205
var now = new Date();
12011206
this._setTime(inst, now);
1207+
restoreSelectionRange(this, range);
12021208
$( '.ui-datepicker-today', $dp).click();
12031209
};
12041210

@@ -1515,6 +1521,31 @@ var selectLocalTimeZone = function(tp_inst, date)
15151521
}
15161522
};
15171523

1524+
//#######################################################################################
1525+
// Helper functions to retreive selection range and restore selection range
1526+
//#######################################################################################
1527+
var getSelectionRange = function(tp_inst)
1528+
{
1529+
var range = {};
1530+
range.input = {
1531+
'selectionStart': tp_inst.$input.prop('selectionStart'),
1532+
'selectionEnd': tp_inst.$input.prop('selectionEnd')};
1533+
if (tp_inst.$altInput) {
1534+
range.altInput = {
1535+
'selectionStart': tp_inst.$altInput.prop('selectionStart'),
1536+
'selectionEnd': tp_inst.$altInput.prop('selectionEnd')};
1537+
}
1538+
return range;
1539+
};
1540+
1541+
var restoreSelectionRange = function(tp_inst, range)
1542+
{
1543+
tp_inst.$input.prop(range.input);
1544+
if (range.altInput !== undefined) {
1545+
tp_inst.$altInput.prop(range.altInput);
1546+
}
1547+
};
1548+
15181549
// Input: Date Object
15191550
// Output: String with timezone offset, e.g. '+0100'
15201551
var timeZoneString = function(date)

0 commit comments

Comments
 (0)