diff options
Diffstat (limited to 'ui/jquery.ui.autocomplete.js')
-rw-r--r-- | ui/jquery.ui.autocomplete.js | 54 |
1 files changed, 39 insertions, 15 deletions
diff --git a/ui/jquery.ui.autocomplete.js b/ui/jquery.ui.autocomplete.js index 427ad1a5a..73d50ee6e 100644 --- a/ui/jquery.ui.autocomplete.js +++ b/ui/jquery.ui.autocomplete.js @@ -26,6 +26,9 @@ $.widget( "ui.autocomplete", { }, source: null }, + + pending: 0, + _create: function() { var self = this, doc = this.element[ 0 ].ownerDocument, @@ -142,7 +145,7 @@ $.widget( "ui.autocomplete", { $( document ).one( 'mousedown', function( event ) { if ( event.target !== self.element[ 0 ] && event.target !== menuElement && - !$.ui.contains( menuElement, event.target ) ) { + !$.contains( menuElement, event.target ) ) { self.close(); } }); @@ -177,13 +180,16 @@ $.widget( "ui.autocomplete", { // term synchronously and asynchronously :-( setTimeout(function() { self.previous = previous; + self.selectedItem = item; }, 1); } if ( false !== self._trigger( "select", event, { item: item } ) ) { - self.term = item.value; self.element.val( item.value ); } + // reset the term after the select event + // this allows custom select handling to work properly + self.term = self.element.val(); self.close( event ); self.selectedItem = item; @@ -198,8 +204,6 @@ $.widget( "ui.autocomplete", { } }) .zIndex( this.element.zIndex() + 1 ) - // workaround for jQuery bug #5781 http://dev.jquery.com/ticket/5781 - .css({ top: 0, left: 0 }) .hide() .data( "menu" ); if ( $.fn.bgiframe ) { @@ -226,6 +230,9 @@ $.widget( "ui.autocomplete", { if ( key === "appendTo" ) { this.menu.element.appendTo( $( value || "body", this.element[0].ownerDocument )[0] ) } + if ( key === "disabled" && value && this.xhr ) { + this.xhr.abort(); + } }, _initSource: function() { @@ -240,14 +247,25 @@ $.widget( "ui.autocomplete", { } else if ( typeof this.options.source === "string" ) { url = this.options.source; this.source = function( request, response ) { - if (self.xhr) { + if ( self.xhr ) { self.xhr.abort(); } - self.xhr = $.getJSON( url, request, function( data, status, xhr ) { - if ( xhr === self.xhr ) { - response( data ); + self.xhr = $.ajax({ + url: url, + data: request, + dataType: "json", + success: function( data, status, xhr ) { + if ( xhr === self.xhr ) { + response( data ); + } + self.xhr = null; + }, + error: function( xhr ) { + if ( xhr === self.xhr ) { + response( [] ); + } + self.xhr = null; } - self.xhr = null; }); }; } else { @@ -274,28 +292,32 @@ $.widget( "ui.autocomplete", { }, _search: function( value ) { + this.pending++; this.element.addClass( "ui-autocomplete-loading" ); this.source( { term: value }, this.response ); }, _response: function( content ) { - if ( content && content.length ) { + if ( !this.options.disabled && content && content.length ) { content = this._normalize( content ); this._suggest( content ); this._trigger( "open" ); } else { this.close(); } - this.element.removeClass( "ui-autocomplete-loading" ); + this.pending--; + if ( !this.pending ) { + this.element.removeClass( "ui-autocomplete-loading" ); + } }, close: function( event ) { clearTimeout( this.closing ); if ( this.menu.element.is(":visible") ) { - this._trigger( "close", event ); this.menu.element.hide(); this.menu.deactivate(); + this._trigger( "close", event ); } }, @@ -332,11 +354,13 @@ $.widget( "ui.autocomplete", { // TODO refresh should check if the active item is still in the dom, removing the need for a manual deactivate this.menu.deactivate(); this.menu.refresh(); - this.menu.element.show().position( $.extend({ - of: this.element - }, this.options.position )); + // size and position menu + ul.show(); this._resizeMenu(); + ul.position( $.extend({ + of: this.element + }, this.options.position )); }, _resizeMenu: function() { |