autocomplete: pass through mouse and key events to menu methods in order to differentiate between key and mouse events in autocomplete

This commit is contained in:
Jörn Zaefferer 2010-03-20 14:57:06 +00:00
parent e4f8f54607
commit 65d8fa2b0c

View File

@ -102,8 +102,10 @@ $.widget( "ui.autocomplete", {
focus: function( event, ui ) { focus: function( event, ui ) {
var item = ui.item.data( "item.autocomplete" ); var item = ui.item.data( "item.autocomplete" );
if ( false !== self._trigger( "focus", null, { item: item } ) ) { if ( false !== self._trigger( "focus", null, { item: item } ) ) {
// use value to match what will end up in the input // use value to match what will end up in the input, if it was a key event
self.element.val( item.value ); if ( /^key/.test(event.originalEvent.type) ) {
self.element.val( item.value );
}
} }
}, },
selected: function( event, ui ) { selected: function( event, ui ) {
@ -286,7 +288,7 @@ $.widget( "ui.autocomplete", {
this.menu.deactivate(); this.menu.deactivate();
return; return;
} }
this.menu[ direction ](); this.menu[ direction ]( event );
}, },
widget: function() { widget: function() {
@ -350,15 +352,15 @@ $.widget("ui.menu", {
.addClass("ui-corner-all") .addClass("ui-corner-all")
.attr("tabindex", -1) .attr("tabindex", -1)
// mouseenter doesn't work with event delegation // mouseenter doesn't work with event delegation
.mouseenter(function() { .mouseenter(function( event ) {
self.activate($(this).parent()); self.activate( event, $(this).parent() );
}) })
.mouseleave(function() { .mouseleave(function() {
self.deactivate(); self.deactivate();
}); });
}, },
activate: function(item) { activate: function( event, item ) {
this.deactivate(); this.deactivate();
if (this.hasScroll()) { if (this.hasScroll()) {
var offset = item.offset().top - this.element.offset().top, var offset = item.offset().top - this.element.offset().top,
@ -375,7 +377,7 @@ $.widget("ui.menu", {
.addClass("ui-state-hover") .addClass("ui-state-hover")
.attr("id", "ui-active-menuitem") .attr("id", "ui-active-menuitem")
.end(); .end();
this._trigger("focus", null, { item: item }); this._trigger("focus", event, { item: item });
}, },
deactivate: function() { deactivate: function() {
@ -388,12 +390,12 @@ $.widget("ui.menu", {
this.active = null; this.active = null;
}, },
next: function() { next: function(event) {
this.move("next", "li:first"); this.move("next", "li:first", event);
}, },
previous: function() { previous: function(event) {
this.move("prev", "li:last"); this.move("prev", "li:last", event);
}, },
first: function() { first: function() {
@ -404,25 +406,25 @@ $.widget("ui.menu", {
return this.active && !this.active.next().length; return this.active && !this.active.next().length;
}, },
move: function(direction, edge) { move: function(direction, edge, event) {
if (!this.active) { if (!this.active) {
this.activate(this.element.children(edge)); this.activate(event, this.element.children(edge));
return; return;
} }
var next = this.active[direction](); var next = this.active[direction]();
if (next.length) { if (next.length) {
this.activate(next); this.activate(event, next);
} else { } else {
this.activate(this.element.children(edge)); this.activate(event, this.element.children(edge));
} }
}, },
// TODO merge with previousPage // TODO merge with previousPage
nextPage: function() { nextPage: function(event) {
if (this.hasScroll()) { if (this.hasScroll()) {
// TODO merge with no-scroll-else // TODO merge with no-scroll-else
if (!this.active || this.last()) { if (!this.active || this.last()) {
this.activate(this.element.children(":first")); this.activate(event, this.element.children(":first"));
return; return;
} }
var base = this.active.offset().top, var base = this.active.offset().top,
@ -437,18 +439,18 @@ $.widget("ui.menu", {
if (!result.length) { if (!result.length) {
result = this.element.children(":last"); result = this.element.children(":last");
} }
this.activate(result); this.activate(event, result);
} else { } else {
this.activate(this.element.children(!this.active || this.last() ? ":first" : ":last")); this.activate(event, this.element.children(!this.active || this.last() ? ":first" : ":last"));
} }
}, },
// TODO merge with nextPage // TODO merge with nextPage
previousPage: function() { previousPage: function(event) {
if (this.hasScroll()) { if (this.hasScroll()) {
// TODO merge with no-scroll-else // TODO merge with no-scroll-else
if (!this.active || this.first()) { if (!this.active || this.first()) {
this.activate(this.element.children(":last")); this.activate(event, this.element.children(":last"));
return; return;
} }
@ -464,9 +466,9 @@ $.widget("ui.menu", {
if (!result.length) { if (!result.length) {
result = this.element.children(":first"); result = this.element.children(":first");
} }
this.activate(result); this.activate(event, result);
} else { } else {
this.activate(this.element.children(!this.active || this.first() ? ":last" : ":first")); this.activate(event, this.element.children(!this.active || this.first() ? ":last" : ":first"));
} }
}, },