Skip to content

Commit 65d8fa2

Browse files
committed
autocomplete: pass through mouse and key events to menu methods in order to differentiate between key and mouse events in autocomplete
1 parent e4f8f54 commit 65d8fa2

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

ui/jquery.ui.autocomplete.js

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,10 @@ $.widget( "ui.autocomplete", {
102102
focus: function( event, ui ) {
103103
var item = ui.item.data( "item.autocomplete" );
104104
if ( false !== self._trigger( "focus", null, { item: item } ) ) {
105-
// use value to match what will end up in the input
106-
self.element.val( item.value );
105+
// use value to match what will end up in the input, if it was a key event
106+
if ( /^key/.test(event.originalEvent.type) ) {
107+
self.element.val( item.value );
108+
}
107109
}
108110
},
109111
selected: function( event, ui ) {
@@ -286,7 +288,7 @@ $.widget( "ui.autocomplete", {
286288
this.menu.deactivate();
287289
return;
288290
}
289-
this.menu[ direction ]();
291+
this.menu[ direction ]( event );
290292
},
291293

292294
widget: function() {
@@ -350,15 +352,15 @@ $.widget("ui.menu", {
350352
.addClass("ui-corner-all")
351353
.attr("tabindex", -1)
352354
// mouseenter doesn't work with event delegation
353-
.mouseenter(function() {
354-
self.activate($(this).parent());
355+
.mouseenter(function( event ) {
356+
self.activate( event, $(this).parent() );
355357
})
356358
.mouseleave(function() {
357359
self.deactivate();
358360
});
359361
},
360362

361-
activate: function(item) {
363+
activate: function( event, item ) {
362364
this.deactivate();
363365
if (this.hasScroll()) {
364366
var offset = item.offset().top - this.element.offset().top,
@@ -375,7 +377,7 @@ $.widget("ui.menu", {
375377
.addClass("ui-state-hover")
376378
.attr("id", "ui-active-menuitem")
377379
.end();
378-
this._trigger("focus", null, { item: item });
380+
this._trigger("focus", event, { item: item });
379381
},
380382

381383
deactivate: function() {
@@ -388,12 +390,12 @@ $.widget("ui.menu", {
388390
this.active = null;
389391
},
390392

391-
next: function() {
392-
this.move("next", "li:first");
393+
next: function(event) {
394+
this.move("next", "li:first", event);
393395
},
394396

395-
previous: function() {
396-
this.move("prev", "li:last");
397+
previous: function(event) {
398+
this.move("prev", "li:last", event);
397399
},
398400

399401
first: function() {
@@ -404,25 +406,25 @@ $.widget("ui.menu", {
404406
return this.active && !this.active.next().length;
405407
},
406408

407-
move: function(direction, edge) {
409+
move: function(direction, edge, event) {
408410
if (!this.active) {
409-
this.activate(this.element.children(edge));
411+
this.activate(event, this.element.children(edge));
410412
return;
411413
}
412414
var next = this.active[direction]();
413415
if (next.length) {
414-
this.activate(next);
416+
this.activate(event, next);
415417
} else {
416-
this.activate(this.element.children(edge));
418+
this.activate(event, this.element.children(edge));
417419
}
418420
},
419421

420422
// TODO merge with previousPage
421-
nextPage: function() {
423+
nextPage: function(event) {
422424
if (this.hasScroll()) {
423425
// TODO merge with no-scroll-else
424426
if (!this.active || this.last()) {
425-
this.activate(this.element.children(":first"));
427+
this.activate(event, this.element.children(":first"));
426428
return;
427429
}
428430
var base = this.active.offset().top,
@@ -437,18 +439,18 @@ $.widget("ui.menu", {
437439
if (!result.length) {
438440
result = this.element.children(":last");
439441
}
440-
this.activate(result);
442+
this.activate(event, result);
441443
} else {
442-
this.activate(this.element.children(!this.active || this.last() ? ":first" : ":last"));
444+
this.activate(event, this.element.children(!this.active || this.last() ? ":first" : ":last"));
443445
}
444446
},
445447

446448
// TODO merge with nextPage
447-
previousPage: function() {
449+
previousPage: function(event) {
448450
if (this.hasScroll()) {
449451
// TODO merge with no-scroll-else
450452
if (!this.active || this.first()) {
451-
this.activate(this.element.children(":last"));
453+
this.activate(event, this.element.children(":last"));
452454
return;
453455
}
454456

@@ -464,9 +466,9 @@ $.widget("ui.menu", {
464466
if (!result.length) {
465467
result = this.element.children(":first");
466468
}
467-
this.activate(result);
469+
this.activate(event, result);
468470
} else {
469-
this.activate(this.element.children(!this.active || this.first() ? ":last" : ":first"));
471+
this.activate(event, this.element.children(!this.active || this.first() ? ":last" : ":first"));
470472
}
471473
},
472474

0 commit comments

Comments
 (0)