@@ -109,14 +109,26 @@ $.widget( "ui.autocomplete", {
109
109
if ( false !== self . _trigger ( "focus" , null , { item : item } ) ) {
110
110
// use value to match what will end up in the input, if it was a key event
111
111
if ( / ^ k e y / . 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
+ }
113
119
}
114
120
}
115
121
} ,
116
122
selected : function ( event , ui ) {
117
123
var item = ui . item . data ( "item.autocomplete" ) ;
118
124
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
+ }
120
132
}
121
133
self . close ( event ) ;
122
134
// only trigger when focus was lost (click on menu)
@@ -200,40 +212,48 @@ $.widget( "ui.autocomplete", {
200
212
. val ( ) ;
201
213
202
214
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 ) ;
204
218
}
205
219
if ( value ) {
206
220
this . source ( { term : value } , this . response ) ;
207
221
}
208
222
} ,
209
223
210
- _getValue : function ( value ) {
211
- this . caretPos = $ . ui . autocomplete . caret ( this . element ) . end ;
224
+ _getValue : function ( value , caretPos ) {
212
225
/*
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,
214
227
* and then concat the outter array values so that editing the
215
228
* 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 ] ;
220
235
} ,
221
236
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 ) ;
227
244
//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 ;
233
253
}
234
254
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 ) ;
237
257
}
238
258
return result ;
239
259
} ,
0 commit comments