|
17 | 17 | $.widget( "ui.autocomplete", { |
18 | 18 | options: { |
19 | 19 | minLength: 1, |
20 | | - delay: 300 |
| 20 | + delay: 300, |
| 21 | + separator: false |
21 | 22 | }, |
22 | 23 | _create: function() { |
23 | 24 | var self = this, |
@@ -108,14 +109,14 @@ $.widget( "ui.autocomplete", { |
108 | 109 | if ( false !== self._trigger( "focus", null, { item: item } ) ) { |
109 | 110 | // use value to match what will end up in the input, if it was a key event |
110 | 111 | if ( /^key/.test(event.originalEvent.type) ) { |
111 | | - self.element.val( item.value ); |
| 112 | + self.element.val( self.options.separator ? self._insertValue(item.value) : item.value ); |
112 | 113 | } |
113 | 114 | } |
114 | 115 | }, |
115 | 116 | selected: function( event, ui ) { |
116 | 117 | var item = ui.item.data( "item.autocomplete" ); |
117 | 118 | if ( false !== self._trigger( "select", event, { item: item } ) ) { |
118 | | - self.element.val( item.value ); |
| 119 | + self.element.val( self.options.separator ? self._insertValue(item.value) : item.value ); |
119 | 120 | } |
120 | 121 | self.close( event ); |
121 | 122 | // only trigger when focus was lost (click on menu) |
@@ -198,7 +199,35 @@ $.widget( "ui.autocomplete", { |
198 | 199 | // always save the actual value, not the one passed as an argument |
199 | 200 | .val(); |
200 | 201 |
|
201 | | - this.source( { term: value }, this.response ); |
| 202 | + if( this.options.separator ){ |
| 203 | + value = this._getValue( value ); |
| 204 | + } |
| 205 | + if( value ){ |
| 206 | + this.source( { term: value }, this.response ); |
| 207 | + } |
| 208 | + }, |
| 209 | + |
| 210 | + _getValue: function( value ){ |
| 211 | + this.caretPos = this.element[0].selectionStart; |
| 212 | + var begin = value.substr( 0, this.caretPos ).split( this.options.separator ), |
| 213 | + end = value.substr( this.caretPos ).split( this.options.separator ); |
| 214 | + return begin[ begin.length-1 ] + end[0]; |
| 215 | + }, |
| 216 | + |
| 217 | + _insertValue: function( value ){ |
| 218 | + var begin = this.term.substr( 0, this.caretPos ).split( this.options.separator ), |
| 219 | + end = this.term.substr( this.caretPos ).split( this.options.separator ), |
| 220 | + result = ''; |
| 221 | + begin.pop(); |
| 222 | + end.shift(); |
| 223 | + if(begin.length){ |
| 224 | + result = begin.join( this.options.separator ) + this.options.separator; |
| 225 | + } |
| 226 | + result += value; |
| 227 | + if(end.length){ |
| 228 | + result += this.options.separator + end.join( this.options.separator ); |
| 229 | + } |
| 230 | + return result; |
202 | 231 | }, |
203 | 232 |
|
204 | 233 | _response: function( content ) { |
|
0 commit comments