|
44 | 44 | ESC: 27, |
45 | 45 | TAB: 9, |
46 | 46 | RETURN: 13, |
| 47 | + LEFT: 37, |
47 | 48 | UP: 38, |
| 49 | + RIGHT: 39, |
48 | 50 | DOWN: 40 |
49 | 51 | }; |
50 | 52 |
|
|
99 | 101 | selected: 'autocomplete-selected', |
100 | 102 | suggestion: 'autocomplete-suggestion' |
101 | 103 | }; |
| 104 | + that.hint = null; |
102 | 105 |
|
103 | 106 | // Initialize and set options: |
104 | 107 | that.initialize(); |
|
263 | 266 | window.clearInterval(this.intervalId); |
264 | 267 | }, |
265 | 268 |
|
| 269 | + isCursorAtEnd: function () { |
| 270 | + var that = this, |
| 271 | + valLength = that.el.val().length, |
| 272 | + selectionStart = that.element.selectionStart, |
| 273 | + range; |
| 274 | + |
| 275 | + if (typeof selectionStart === 'number') { |
| 276 | + return selectionStart === valLength; |
| 277 | + } |
| 278 | + if (document.selection) { |
| 279 | + range = document.selection.createRange(); |
| 280 | + range.moveStart('character', -valLength); |
| 281 | + return valLength === range.text.length; |
| 282 | + } |
| 283 | + return true; |
| 284 | + }, |
| 285 | + |
266 | 286 | onKeyPress: function (e) { |
267 | 287 | var that = this; |
268 | 288 |
|
269 | 289 | // If suggestions are hidden and user presses arrow down, display suggestions: |
270 | | - if (!that.disabled && !that.visible && e.keyCode === keys.DOWN && that.currentValue) { |
| 290 | + if (!that.disabled && !that.visible && e.which === keys.DOWN && that.currentValue) { |
271 | 291 | that.suggest(); |
272 | 292 | return; |
273 | 293 | } |
|
281 | 301 | that.el.val(that.currentValue); |
282 | 302 | that.hide(); |
283 | 303 | break; |
| 304 | + case keys.RIGHT: |
| 305 | + if (that.hint && that.options.onHint && that.isCursorAtEnd()) { |
| 306 | + that.selectHint(); |
| 307 | + break; |
| 308 | + } |
| 309 | + return; |
284 | 310 | case keys.TAB: |
285 | 311 | case keys.RETURN: |
| 312 | + if (e.which === keys.TAB && that.hint) { |
| 313 | + that.selectHint(); |
| 314 | + return; |
| 315 | + } |
286 | 316 | if (that.selectedIndex === -1) { |
287 | 317 | that.hide(); |
288 | 318 | return; |
289 | 319 | } |
290 | 320 | that.select(that.selectedIndex); |
291 | | - if (e.keyCode === keys.TAB && this.options.tabDisabled === false) { |
| 321 | + if (e.which === keys.TAB && that.options.tabDisabled === false) { |
292 | 322 | return; |
293 | 323 | } |
294 | 324 | break; |
|
314 | 344 | return; |
315 | 345 | } |
316 | 346 |
|
317 | | - switch (e.keyCode) { |
| 347 | + switch (e.which) { |
318 | 348 | case keys.UP: |
319 | 349 | case keys.DOWN: |
320 | 350 | return; |
|
486 | 516 | }, |
487 | 517 |
|
488 | 518 | signalHint: function (suggestion) { |
489 | | - var hintValue = ''; |
| 519 | + var hintValue = '', |
| 520 | + that = this; |
490 | 521 | if (suggestion) { |
491 | | - hintValue = this.currentValue + suggestion.value.substr(this.currentValue.length); |
| 522 | + hintValue = that.currentValue + suggestion.value.substr(that.currentValue.length); |
| 523 | + } |
| 524 | + if (that.hint !== suggestion) { |
| 525 | + that.hint = suggestion; |
| 526 | + (this.options.onHint || $.noop)(hintValue); |
492 | 527 | } |
493 | | - (this.options.onHint || $.noop)(hintValue); |
494 | 528 | }, |
495 | 529 |
|
496 | 530 | verifySuggestionsFormat: function (suggestions) { |
|
546 | 580 | return null; |
547 | 581 | }, |
548 | 582 |
|
| 583 | + selectHint: function () { |
| 584 | + var that = this, |
| 585 | + i = $.inArray(that.hint, that.suggestions); |
| 586 | + |
| 587 | + that.select(i); |
| 588 | + }, |
| 589 | + |
549 | 590 | select: function (i) { |
550 | 591 | var that = this; |
551 | 592 | that.hide(); |
|
0 commit comments