Skip to content
This repository was archived by the owner on Dec 11, 2024. It is now read-only.

Commit adfbf7a

Browse files
author
Tomas Kirda
committed
Rename keyCode to which for consistency. Select hint on TAB or RIGHT arrow if hinting listener is set.
1 parent 835a039 commit adfbf7a

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

content/styles.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
body { font-family: sans-serif; font-size: 14px; line-height: 1.6em; margin: 0; padding: 0; }
22
.container { width: 800px; margin: 0 auto; }
33

4-
.autocomplete-suggestions { border: 1px solid #999; background: #FFF; cursor: default; overflow: auto; }
4+
.autocomplete-suggestions { border: 1px solid #999; background: #FFF; cursor: default; overflow: auto; -webkit-box-shadow: 1px 4px 3px rgba(50, 50, 50, 0.64); -moz-box-shadow: 1px 4px 3px rgba(50, 50, 50, 0.64); box-shadow: 1px 4px 3px rgba(50, 50, 50, 0.64); }
55
.autocomplete-suggestion { padding: 2px 5px; white-space: nowrap; overflow: hidden; }
66
.autocomplete-selected { background: #F0F0F0; }
77
.autocomplete-suggestions strong { font-weight: normal; color: #3399FF; }

src/jquery.autocomplete.js

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@
4444
ESC: 27,
4545
TAB: 9,
4646
RETURN: 13,
47+
LEFT: 37,
4748
UP: 38,
49+
RIGHT: 39,
4850
DOWN: 40
4951
};
5052

@@ -99,6 +101,7 @@
99101
selected: 'autocomplete-selected',
100102
suggestion: 'autocomplete-suggestion'
101103
};
104+
that.hint = null;
102105

103106
// Initialize and set options:
104107
that.initialize();
@@ -263,11 +266,28 @@
263266
window.clearInterval(this.intervalId);
264267
},
265268

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+
266286
onKeyPress: function (e) {
267287
var that = this;
268288

269289
// 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) {
271291
that.suggest();
272292
return;
273293
}
@@ -281,14 +301,24 @@
281301
that.el.val(that.currentValue);
282302
that.hide();
283303
break;
304+
case keys.RIGHT:
305+
if (that.hint && that.options.onHint && that.isCursorAtEnd()) {
306+
that.selectHint();
307+
break;
308+
}
309+
return;
284310
case keys.TAB:
285311
case keys.RETURN:
312+
if (e.which === keys.TAB && that.hint) {
313+
that.selectHint();
314+
return;
315+
}
286316
if (that.selectedIndex === -1) {
287317
that.hide();
288318
return;
289319
}
290320
that.select(that.selectedIndex);
291-
if (e.keyCode === keys.TAB && this.options.tabDisabled === false) {
321+
if (e.which === keys.TAB && that.options.tabDisabled === false) {
292322
return;
293323
}
294324
break;
@@ -314,7 +344,7 @@
314344
return;
315345
}
316346

317-
switch (e.keyCode) {
347+
switch (e.which) {
318348
case keys.UP:
319349
case keys.DOWN:
320350
return;
@@ -486,11 +516,15 @@
486516
},
487517

488518
signalHint: function (suggestion) {
489-
var hintValue = '';
519+
var hintValue = '',
520+
that = this;
490521
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);
492527
}
493-
(this.options.onHint || $.noop)(hintValue);
494528
},
495529

496530
verifySuggestionsFormat: function (suggestions) {
@@ -546,6 +580,13 @@
546580
return null;
547581
},
548582

583+
selectHint: function () {
584+
var that = this,
585+
i = $.inArray(that.hint, that.suggestions);
586+
587+
that.select(i);
588+
},
589+
549590
select: function (i) {
550591
var that = this;
551592
that.hide();

0 commit comments

Comments
 (0)