Skip to content

Commit 2722c91

Browse files
committed
Stored the beginning and end of the autocomplete term so that it isnt parsed at every event
1 parent 0cbd58a commit 2722c91

File tree

1 file changed

+42
-22
lines changed

1 file changed

+42
-22
lines changed

ui/jquery.ui.autocomplete.js

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,26 @@ $.widget( "ui.autocomplete", {
109109
if ( false !== self._trigger( "focus", null, { item: item } ) ) {
110110
// use value to match what will end up in the input, if it was a key event
111111
if ( /^key/.test(event.originalEvent.type) ) {
112-
self.element.val( self.options.separator ? self._insertValue(item.value) : item.value );
112+
if(self.options.separator){
113+
var val = self._insertValue(item.value);
114+
self.element.val( val );
115+
$.ui.autocomplete.caret( self.element, val.length - self.valEndLength );
116+
} else {
117+
self.element.val( item.value );
118+
}
113119
}
114120
}
115121
},
116122
selected: function( event, ui ) {
117123
var item = ui.item.data( "item.autocomplete" );
118124
if ( false !== self._trigger( "select", event, { item: item } ) ) {
119-
self.element.val( self.options.separator ? self._insertValue(item.value) : item.value );
125+
if(self.options.separator){
126+
var val = self._insertValue(item.value);
127+
self.element.val( val );
128+
$.ui.autocomplete.caret( self.element, val.length - self.valEndLength );
129+
} else {
130+
self.element.val( item.value );
131+
}
120132
}
121133
self.close( event );
122134
// only trigger when focus was lost (click on menu)
@@ -200,40 +212,48 @@ $.widget( "ui.autocomplete", {
200212
.val();
201213

202214
if( this.options.separator ){
203-
value = this._getValue( value );
215+
var caretPos = $.ui.autocomplete.caret(this.element).end;
216+
value = this._getValue( value, caretPos );
217+
this._storeTermBits( caretPos );
204218
}
205219
if( value ){
206220
this.source( { term: value }, this.response );
207221
}
208222
},
209223

210-
_getValue: function( value ){
211-
this.caretPos = $.ui.autocomplete.caret(this.element).end;
224+
_getValue: function( value, caretPos ){
212225
/*
213-
* cut the string in half at the caret, split at the separators,
226+
* Cut the string in half at the caret, split at the separators,
214227
* and then concat the outter array values so that editing the
215228
* middle of a string still correctly triggers the autocomplete
216-
*/
217-
var begin = value.substr( 0, this.caretPos ).split( this.options.separator ),
218-
end = value.substr( this.caretPos ).split( this.options.separator );
219-
return begin[ begin.length-1 ] + end[0];
229+
*/
230+
var begin = value.substr( 0, caretPos ).split( this.options.separator ),
231+
end = value.substr( caretPos );
232+
this.valEndLength = end.length;
233+
234+
return begin[ begin.length-1 ] + end.split( this.options.separator )[0];
220235
},
221236

222-
_insertValue: function( value ){
223-
var begin = this.term.substr( 0, this.caretPos ).split( this.options.separator ),
224-
end = this.term.substr( this.caretPos ).split( this.options.separator ),
225-
result = '';
226-
237+
_storeTermBits: function( caretPos ){
238+
/*
239+
* Store the beginning and end of the original term so that
240+
* the values can quickly be swapped in and out in _insertValue
241+
*/
242+
this.termBegin = this.term.substr( 0, caretPos ).split( this.options.separator );
243+
this.termEnd = this.term.substr( caretPos ).split( this.options.separator );
227244
//clean the arrays of empty entries
228-
begin.pop();
229-
end.shift();
230-
231-
if(begin.length){
232-
result = begin.join( this.options.separator ) + this.options.separator;
245+
this.termBegin.pop();
246+
this.termEnd.shift();
247+
},
248+
249+
_insertValue: function( value ){
250+
var result = '';
251+
if( this.termBegin.length ){
252+
result = this.termBegin.join( this.options.separator ) + this.options.separator;
233253
}
234254
result += value;
235-
if(end.length){
236-
result += this.options.separator + end.join( this.options.separator );
255+
if( this.termEnd.length ){
256+
result += this.options.separator + this.termEnd.join( this.options.separator );
237257
}
238258
return result;
239259
},

0 commit comments

Comments
 (0)