jquery-ui/ui/jquery.ui.autocomplete.js

426 lines
11 KiB
JavaScript
Raw Normal View History

2010-01-14 17:23:11 +00:00
/*
* jQuery UI Autocomplete @VERSION
*
2011-01-17 14:13:18 +00:00
* Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
2010-01-14 17:23:11 +00:00
*
* http://docs.jquery.com/UI/Autocomplete
*
* Depends:
* jquery.ui.core.js
* jquery.ui.widget.js
2010-03-11 22:06:29 +00:00
* jquery.ui.position.js
* jquery.ui.menu.js
2010-01-14 17:23:11 +00:00
*/
(function( $, undefined ) {
2010-01-14 17:23:11 +00:00
// used to prevent race conditions with remote data sources
var requestIndex = 0;
$.widget( "ui.autocomplete", {
defaultElement: "<input>",
2010-01-14 17:23:11 +00:00
options: {
appendTo: "body",
delay: 300,
2010-01-14 17:23:11 +00:00
minLength: 1,
position: {
my: "left top",
at: "left bottom",
collision: "none"
},
source: null
2010-01-14 17:23:11 +00:00
},
pending: 0,
_create: function() {
var self = this,
doc = this.element[ 0 ].ownerDocument,
suppressKeyPress;
2010-01-14 17:23:11 +00:00
this.element
.addClass( "ui-autocomplete-input" )
.attr( "autocomplete", "off" )
2010-01-14 17:23:11 +00:00
// TODO verify these actually work as intended
.attr({
role: "textbox",
"aria-autocomplete": "list",
"aria-haspopup": "true"
})
.bind( "keydown.autocomplete", function( event ) {
if ( self.options.disabled || self.element.attr( "readonly" ) ) {
return;
}
suppressKeyPress = false;
2010-01-14 17:23:11 +00:00
var keyCode = $.ui.keyCode;
switch( event.keyCode ) {
2010-01-14 17:23:11 +00:00
case keyCode.PAGE_UP:
self._move( "previousPage", event );
2010-01-14 17:23:11 +00:00
break;
case keyCode.PAGE_DOWN:
self._move( "nextPage", event );
2010-01-14 17:23:11 +00:00
break;
case keyCode.UP:
self._move( "previous", event );
2010-01-14 17:23:11 +00:00
// prevent moving cursor to beginning of text field in some browsers
event.preventDefault();
break;
case keyCode.DOWN:
self._move( "next", event );
2010-01-14 17:23:11 +00:00
// prevent moving cursor to end of text field in some browsers
event.preventDefault();
break;
case keyCode.ENTER:
case keyCode.NUMPAD_ENTER:
// when menu is open and has focus
if ( self.menu.active ) {
// #6055 - Opera still allows the keypress to occur
// which causes forms to submit
suppressKeyPress = true;
2010-01-14 17:23:11 +00:00
event.preventDefault();
}
2010-03-11 22:06:29 +00:00
//passthrough - ENTER and TAB both select the current element
2010-01-14 17:23:11 +00:00
case keyCode.TAB:
if ( !self.menu.active ) {
2010-01-14 17:23:11 +00:00
return;
}
self.menu.select( event );
2010-01-14 17:23:11 +00:00
break;
case keyCode.ESCAPE:
self.element.val( self.term );
self.close( event );
2010-01-14 17:23:11 +00:00
break;
default:
// keypress is triggered before the input value is changed
clearTimeout( self.searching );
2010-01-14 17:23:11 +00:00
self.searching = setTimeout(function() {
// only search if the value has changed
if ( self.term != self.element.val() ) {
self.selectedItem = null;
self.search( null, event );
}
}, self.options.delay );
2010-01-14 17:23:11 +00:00
break;
}
})
.bind( "keypress.autocomplete", function( event ) {
if ( suppressKeyPress ) {
suppressKeyPress = false;
event.preventDefault();
}
})
.bind( "focus.autocomplete", function() {
if ( self.options.disabled ) {
return;
}
self.selectedItem = null;
2010-01-14 17:23:11 +00:00
self.previous = self.element.val();
})
.bind( "blur.autocomplete", function( event ) {
if ( self.options.disabled ) {
return;
}
clearTimeout( self.searching );
2010-01-14 17:23:11 +00:00
// clicks on the menu (or a button to trigger a search) will cause a blur event
self.closing = setTimeout(function() {
self.close( event );
self._change( event );
}, 150 );
2010-01-14 17:23:11 +00:00
});
this._initSource();
this.response = function() {
return self._response.apply( self, arguments );
2010-01-14 17:23:11 +00:00
};
this.menu = $( "<ul></ul>" )
.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( 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 ).closest( ".ui-menu-item" ).length ) {
setTimeout(function() {
$( document ).one( 'mousedown', function( event ) {
if ( event.target !== self.element[ 0 ] &&
event.target !== menuElement &&
!$.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 );
}, 13);
})
.menu({
// custom key handling for now
input: $(),
focus: function( event, ui ) {
var item = ui.item.data( "item.autocomplete" );
if ( false !== self._trigger( "focus", event, { item: item } ) ) {
// use value to match what will end up in the input, if it was a key event
if ( /^key/.test(event.originalEvent.type) ) {
self.element.val( item.value );
}
}
},
select: function( event, ui ) {
var item = ui.item.data( "item.autocomplete" ),
previous = self.previous;
// only trigger when focus was lost (click on menu)
if ( self.element[0] !== doc.activeElement ) {
self.element.focus();
self.previous = previous;
// #6109 - IE triggers two focus events and the second
// is asynchronous, so we need to reset the previous
// term synchronously and asynchronously :-(
setTimeout(function() {
self.previous = previous;
self.selectedItem = item;
}, 1);
}
if ( false !== self._trigger( "select", event, { item: item } ) ) {
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;
},
blur: function( event, ui ) {
// don't set the value of the text field if it's already correct
// this prevents moving the cursor unnecessarily
if ( self.menu.element.is(":visible") &&
( self.element.val() !== self.term ) ) {
self.element.val( self.term );
}
}
})
.zIndex( this.element.zIndex() + 1 )
.hide()
.data( "menu" );
if ( $.fn.bgiframe ) {
this.menu.element.bgiframe();
}
2010-01-14 17:23:11 +00:00
},
_destroy: function() {
2010-01-14 17:23:11 +00:00
this.element
.removeClass( "ui-autocomplete-input" )
.removeAttr( "autocomplete" )
.removeAttr( "role" )
.removeAttr( "aria-autocomplete" )
.removeAttr( "aria-haspopup" );
this.menu.element.remove();
2010-01-14 17:23:11 +00:00
},
_setOption: function( key, value ) {
this._super( "_setOption", key, value );
2010-03-11 22:06:29 +00:00
if ( key === "source" ) {
2010-01-14 17:23:11 +00:00
this._initSource();
}
if ( key === "appendTo" ) {
this.menu.element.appendTo( $( value || "body", this.element[0].ownerDocument )[0] )
}
if ( key === "disabled" && value && this.xhr ) {
this.xhr.abort();
}
2010-01-14 17:23:11 +00:00
},
_initSource: function() {
var self = this,
array,
2010-03-11 22:06:29 +00:00
url;
if ( $.isArray(this.options.source) ) {
2010-03-11 22:06:29 +00:00
array = this.options.source;
this.source = function( request, response ) {
response( $.ui.autocomplete.filter(array, request.term) );
2010-01-14 17:23:11 +00:00
};
2010-03-11 22:06:29 +00:00
} else if ( typeof this.options.source === "string" ) {
url = this.options.source;
this.source = function( request, response ) {
if ( self.xhr ) {
self.xhr.abort();
}
self.xhr = $.ajax({
url: url,
data: request,
dataType: "json",
autocompleteRequest: ++requestIndex,
success: function( data, status ) {
if ( this.autocompleteRequest === requestIndex ) {
response( data );
}
},
error: function() {
if ( this.autocompleteRequest === requestIndex ) {
response( [] );
}
}
});
2010-01-14 17:23:11 +00:00
};
} else {
this.source = this.options.source;
}
},
search: function( value, event ) {
2010-01-14 17:23:11 +00:00
value = value != null ? value : this.element.val();
// always save the actual value, not the one passed as an argument
this.term = this.element.val();
if ( value.length < this.options.minLength ) {
return this.close( event );
2010-01-14 17:23:11 +00:00
}
clearTimeout( this.closing );
if ( this._trigger( "search", event ) === false ) {
2010-01-14 17:23:11 +00:00
return;
}
return this._search( value );
2010-01-14 17:23:11 +00:00
},
_search: function( value ) {
this.pending++;
this.element.addClass( "ui-autocomplete-loading" );
2010-01-14 17:23:11 +00:00
this.source( { term: value }, this.response );
2010-01-14 17:23:11 +00:00
},
_response: function( content ) {
if ( !this.options.disabled && content && content.length ) {
content = this._normalize( content );
this._suggest( content );
this._trigger( "open" );
2010-01-14 17:23:11 +00:00
} else {
this.close();
}
this.pending--;
if ( !this.pending ) {
this.element.removeClass( "ui-autocomplete-loading" );
}
2010-01-14 17:23:11 +00:00
},
close: function( event ) {
clearTimeout( this.closing );
if ( this.menu.element.is(":visible") ) {
this.menu.element.hide();
this.menu.deactivate();
this._trigger( "close", event );
2010-01-14 17:23:11 +00:00
}
},
_change: function( event ) {
2010-03-11 22:06:29 +00:00
if ( this.previous !== this.element.val() ) {
this._trigger( "change", event, { item: this.selectedItem } );
2010-01-14 17:23:11 +00:00
}
},
_normalize: function( items ) {
2010-01-14 17:23:11 +00:00
// assume all items have the right format when the first item is complete
if ( items.length && items[0].label && items[0].value ) {
2010-01-14 17:23:11 +00:00
return items;
}
return $.map( items, function(item) {
2010-03-11 22:06:29 +00:00
if ( typeof item === "string" ) {
2010-01-14 17:23:11 +00:00
return {
label: item,
value: item
};
}
return $.extend({
label: item.label || item.value,
value: item.value || item.label
}, item );
2010-01-14 17:23:11 +00:00
});
},
_suggest: function( items ) {
2010-03-11 22:06:29 +00:00
var ul = this.menu.element
.empty()
.zIndex( this.element.zIndex() + 1 );
this._renderMenu( ul, items );
2010-02-10 15:08:27 +00:00
// TODO refresh should check if the active item is still in the dom, removing the need for a manual deactivate
2010-02-10 15:05:35 +00:00
this.menu.deactivate();
this.menu.refresh();
// size and position menu
ul.show();
this._resizeMenu();
ul.position( $.extend({
of: this.element
}, this.options.position ));
},
_resizeMenu: function() {
var ul = this.menu.element;
ul.outerWidth( Math.max(
ul.width( "" ).outerWidth(),
this.element.outerWidth()
) );
2010-01-14 17:23:11 +00:00
},
_renderMenu: function( ul, items ) {
var self = this;
$.each( items, function( index, item ) {
self._renderItem( ul, item );
});
},
2010-01-14 17:23:11 +00:00
_renderItem: function( ul, item) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( $( "<a></a>" ).text( item.label ) )
.appendTo( ul );
},
_move: function( direction, event ) {
if ( !this.menu.element.is(":visible") ) {
this.search( null, event );
2010-01-14 17:23:11 +00:00
return;
}
if ( this.menu.first() && /^previous/.test(direction) ||
this.menu.last() && /^next/.test(direction) ) {
this.element.val( this.term );
2010-01-14 17:23:11 +00:00
this.menu.deactivate();
return;
}
this.menu[ direction ]( event );
2010-01-14 17:23:11 +00:00
},
widget: function() {
return this.menu.element;
2010-01-14 17:23:11 +00:00
}
});
$.extend( $.ui.autocomplete, {
version: "@VERSION",
escapeRegex: function( value ) {
return value.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
},
filter: function(array, term) {
var matcher = new RegExp( $.ui.autocomplete.escapeRegex(term), "i" );
return $.grep( array, function(value) {
return matcher.test( value.label || value.value || value );
});
2010-01-14 17:23:11 +00:00
}
});
2010-03-11 22:06:29 +00:00
}( jQuery ));