]> source.dussan.org Git - jquery-ui.git/commitdiff
Autocomplete: Handle clicks outside the autocomplete after scrolling the results...
authorScott González <scott.gonzalez@gmail.com>
Thu, 5 Aug 2010 12:51:54 +0000 (08:51 -0400)
committerScott González <scott.gonzalez@gmail.com>
Thu, 5 Aug 2010 12:51:54 +0000 (08:51 -0400)
ui/jquery.ui.autocomplete.js

index 98130ab502bb4050b4d0963b3ad78f7b6882ec0e..8380df8ad74eab282edcf4ac97aa7e5cd60df6b5 100644 (file)
@@ -119,7 +119,24 @@ $.widget( "ui.autocomplete", {
                        .addClass( "ui-autocomplete" )
                        .appendTo( $( this.options.appendTo || "body", doc )[0] )
                        // prevent the close-on-blur in case of a "slow" click on the menu (long mousedown)
-                       .mousedown(function() {
+                       .mousedown(function( event ) {
+                               // clicking on the scrollbar causes focus to shift to the body
+                               // but we can't detect a mouseup or a click immediately afterward
+                               // so we have to track the next mousedown and close the menu if
+                               // the user clicks somewhere outside of the autocomplete
+                               var menuElement = self.menu.element[ 0 ];
+                               if ( event.target === menuElement ) {
+                                       setTimeout(function() {
+                                               $( document ).one( 'mousedown', function( event ) {
+                                                       if ( event.target !== self.element[ 0 ] &&
+                                                               event.target !== menuElement &&
+                                                               !$.ui.contains( menuElement, event.target ) ) {
+                                                               self.close();
+                                                       }
+                                               });
+                                       }, 1 );
+                               }
+
                                // use another timeout to make sure the blur-event-handler on the input was already triggered
                                setTimeout(function() {
                                        clearTimeout( self.closing );