2 * jQuery UI Autocomplete 1.10.2
5 * Copyright 2013 jQuery Foundation and other contributors
6 * Released under the MIT license.
7 * http://jquery.org/license
9 * http://api.jqueryui.com/autocomplete/
14 * jquery.ui.position.js
17 (function( $, undefined ) {
19 // used to prevent race conditions with remote data sources
22 $.widget( "ui.autocomplete", {
24 defaultElement: "<input>",
50 // Some browsers only repeat keydown events, not keypress events,
51 // so we use the suppressKeyPress flag to determine if we've already
52 // handled the keydown event. #7269
53 // Unfortunately the code for & in keypress is the same as the up arrow,
54 // so we use the suppressKeyPressRepeat flag to avoid handling keypress
55 // events when we know the keydown event was used to modify the
57 var suppressKeyPress, suppressKeyPressRepeat, suppressInput,
58 nodeName = this.element[0].nodeName.toLowerCase(),
59 isTextarea = nodeName === "textarea",
60 isInput = nodeName === "input";
63 // Textareas are always multi-line
65 // Inputs are always single-line, even if inside a contentEditable element
66 // IE also treats inputs as contentEditable
68 // All other element types are determined by whether or not they're contentEditable
69 this.element.prop( "isContentEditable" );
71 this.valueMethod = this.element[ isTextarea || isInput ? "val" : "text" ];
72 this.isNewMenu = true;
75 .addClass( "ui-autocomplete-input" )
76 .attr( "autocomplete", "off" );
78 this._on( this.element, {
79 keydown: function( event ) {
80 /*jshint maxcomplexity:15*/
81 if ( this.element.prop( "readOnly" ) ) {
82 suppressKeyPress = true;
84 suppressKeyPressRepeat = true;
88 suppressKeyPress = false;
89 suppressInput = false;
90 suppressKeyPressRepeat = false;
91 var keyCode = $.ui.keyCode;
92 switch( event.keyCode ) {
94 suppressKeyPress = true;
95 this._move( "previousPage", event );
97 case keyCode.PAGE_DOWN:
98 suppressKeyPress = true;
99 this._move( "nextPage", event );
102 suppressKeyPress = true;
103 this._keyEvent( "previous", event );
106 suppressKeyPress = true;
107 this._keyEvent( "next", event );
110 case keyCode.NUMPAD_ENTER:
111 // when menu is open and has focus
112 if ( this.menu.active ) {
113 // #6055 - Opera still allows the keypress to occur
114 // which causes forms to submit
115 suppressKeyPress = true;
116 event.preventDefault();
117 this.menu.select( event );
121 if ( this.menu.active ) {
122 this.menu.select( event );
126 if ( this.menu.element.is( ":visible" ) ) {
127 this._value( this.term );
129 // Different browsers have different default behavior for escape
130 // Single press can mean undo or clear
131 // Double press in IE means clear the whole form
132 event.preventDefault();
136 suppressKeyPressRepeat = true;
137 // search timeout should be triggered before the input value is changed
138 this._searchTimeout( event );
142 keypress: function( event ) {
143 if ( suppressKeyPress ) {
144 suppressKeyPress = false;
145 event.preventDefault();
148 if ( suppressKeyPressRepeat ) {
152 // replicate some key handlers to allow them to repeat in Firefox and Opera
153 var keyCode = $.ui.keyCode;
154 switch( event.keyCode ) {
155 case keyCode.PAGE_UP:
156 this._move( "previousPage", event );
158 case keyCode.PAGE_DOWN:
159 this._move( "nextPage", event );
162 this._keyEvent( "previous", event );
165 this._keyEvent( "next", event );
169 input: function( event ) {
170 if ( suppressInput ) {
171 suppressInput = false;
172 event.preventDefault();
175 this._searchTimeout( event );
178 this.selectedItem = null;
179 this.previous = this._value();
181 blur: function( event ) {
182 if ( this.cancelBlur ) {
183 delete this.cancelBlur;
187 clearTimeout( this.searching );
189 this._change( event );
194 this.menu = $( "<ul>" )
195 .addClass( "ui-autocomplete ui-front" )
196 .appendTo( this._appendTo() )
198 // custom key handling for now
200 // disable ARIA support, the live region takes care of that
206 this._on( this.menu.element, {
207 mousedown: function( event ) {
208 // prevent moving focus out of the text field
209 event.preventDefault();
211 // IE doesn't prevent moving focus even with event.preventDefault()
212 // so we set a flag to know when we should ignore the blur event
213 this.cancelBlur = true;
214 this._delay(function() {
215 delete this.cancelBlur;
218 // clicking on the scrollbar causes focus to shift to the body
219 // but we can't detect a mouseup or a click immediately afterward
220 // so we have to track the next mousedown and close the menu if
221 // the user clicks somewhere outside of the autocomplete
222 var menuElement = this.menu.element[ 0 ];
223 if ( !$( event.target ).closest( ".ui-menu-item" ).length ) {
224 this._delay(function() {
226 this.document.one( "mousedown", function( event ) {
227 if ( event.target !== that.element[ 0 ] &&
228 event.target !== menuElement &&
229 !$.contains( menuElement, event.target ) ) {
236 menufocus: function( event, ui ) {
238 // Prevent accidental activation of menu items in Firefox (#7024 #9118)
239 if ( this.isNewMenu ) {
240 this.isNewMenu = false;
241 if ( event.originalEvent && /^mouse/.test( event.originalEvent.type ) ) {
244 this.document.one( "mousemove", function() {
245 $( event.target ).trigger( event.originalEvent );
252 var item = ui.item.data( "ui-autocomplete-item" );
253 if ( false !== this._trigger( "focus", event, { item: item } ) ) {
254 // use value to match what will end up in the input, if it was a key event
255 if ( event.originalEvent && /^key/.test( event.originalEvent.type ) ) {
256 this._value( item.value );
259 // Normally the input is populated with the item's value as the
260 // menu is navigated, causing screen readers to notice a change and
261 // announce the item. Since the focus event was canceled, this doesn't
262 // happen, so we update the live region so that screen readers can
263 // still notice the change and announce it.
264 this.liveRegion.text( item.value );
267 menuselect: function( event, ui ) {
268 var item = ui.item.data( "ui-autocomplete-item" ),
269 previous = this.previous;
271 // only trigger when focus was lost (click on menu)
272 if ( this.element[0] !== this.document[0].activeElement ) {
273 this.element.focus();
274 this.previous = previous;
275 // #6109 - IE triggers two focus events and the second
276 // is asynchronous, so we need to reset the previous
277 // term synchronously and asynchronously :-(
278 this._delay(function() {
279 this.previous = previous;
280 this.selectedItem = item;
284 if ( false !== this._trigger( "select", event, { item: item } ) ) {
285 this._value( item.value );
287 // reset the term after the select event
288 // this allows custom select handling to work properly
289 this.term = this._value();
292 this.selectedItem = item;
296 this.liveRegion = $( "<span>", {
298 "aria-live": "polite"
300 .addClass( "ui-helper-hidden-accessible" )
301 .insertAfter( this.element );
303 // turning off autocomplete prevents the browser from remembering the
304 // value when navigating through history, so we re-enable autocomplete
305 // if the page is unloaded before the widget is destroyed. #7790
306 this._on( this.window, {
307 beforeunload: function() {
308 this.element.removeAttr( "autocomplete" );
313 _destroy: function() {
314 clearTimeout( this.searching );
316 .removeClass( "ui-autocomplete-input" )
317 .removeAttr( "autocomplete" );
318 this.menu.element.remove();
319 this.liveRegion.remove();
322 _setOption: function( key, value ) {
323 this._super( key, value );
324 if ( key === "source" ) {
327 if ( key === "appendTo" ) {
328 this.menu.element.appendTo( this._appendTo() );
330 if ( key === "disabled" && value && this.xhr ) {
335 _appendTo: function() {
336 var element = this.options.appendTo;
339 element = element.jquery || element.nodeType ?
341 this.document.find( element ).eq( 0 );
345 element = this.element.closest( ".ui-front" );
348 if ( !element.length ) {
349 element = this.document[0].body;
355 _initSource: function() {
358 if ( $.isArray(this.options.source) ) {
359 array = this.options.source;
360 this.source = function( request, response ) {
361 response( $.ui.autocomplete.filter( array, request.term ) );
363 } else if ( typeof this.options.source === "string" ) {
364 url = this.options.source;
365 this.source = function( request, response ) {
373 success: function( data ) {
382 this.source = this.options.source;
386 _searchTimeout: function( event ) {
387 clearTimeout( this.searching );
388 this.searching = this._delay(function() {
389 // only search if the value has changed
390 if ( this.term !== this._value() ) {
391 this.selectedItem = null;
392 this.search( null, event );
394 }, this.options.delay );
397 search: function( value, event ) {
398 value = value != null ? value : this._value();
400 // always save the actual value, not the one passed as an argument
401 this.term = this._value();
403 if ( value.length < this.options.minLength ) {
404 return this.close( event );
407 if ( this._trigger( "search", event ) === false ) {
411 return this._search( value );
414 _search: function( value ) {
416 this.element.addClass( "ui-autocomplete-loading" );
417 this.cancelSearch = false;
419 this.source( { term: value }, this._response() );
422 _response: function() {
424 index = ++requestIndex;
426 return function( content ) {
427 if ( index === requestIndex ) {
428 that.__response( content );
432 if ( !that.pending ) {
433 that.element.removeClass( "ui-autocomplete-loading" );
438 __response: function( content ) {
440 content = this._normalize( content );
442 this._trigger( "response", null, { content: content } );
443 if ( !this.options.disabled && content && content.length && !this.cancelSearch ) {
444 this._suggest( content );
445 this._trigger( "open" );
447 // use ._close() instead of .close() so we don't cancel future searches
452 close: function( event ) {
453 this.cancelSearch = true;
454 this._close( event );
457 _close: function( event ) {
458 if ( this.menu.element.is( ":visible" ) ) {
459 this.menu.element.hide();
461 this.isNewMenu = true;
462 this._trigger( "close", event );
466 _change: function( event ) {
467 if ( this.previous !== this._value() ) {
468 this._trigger( "change", event, { item: this.selectedItem } );
472 _normalize: function( items ) {
473 // assume all items have the right format when the first item is complete
474 if ( items.length && items[0].label && items[0].value ) {
477 return $.map( items, function( item ) {
478 if ( typeof item === "string" ) {
485 label: item.label || item.value,
486 value: item.value || item.label
491 _suggest: function( items ) {
492 var ul = this.menu.element.empty();
493 this._renderMenu( ul, items );
494 this.isNewMenu = true;
497 // size and position menu
500 ul.position( $.extend({
502 }, this.options.position ));
504 if ( this.options.autoFocus ) {
509 _resizeMenu: function() {
510 var ul = this.menu.element;
511 ul.outerWidth( Math.max(
512 // Firefox wraps long text (possibly a rounding bug)
513 // so we add 1px to avoid the wrapping (#7513)
514 ul.width( "" ).outerWidth() + 1,
515 this.element.outerWidth()
519 _renderMenu: function( ul, items ) {
521 $.each( items, function( index, item ) {
522 that._renderItemData( ul, item );
526 _renderItemData: function( ul, item ) {
527 return this._renderItem( ul, item ).data( "ui-autocomplete-item", item );
530 _renderItem: function( ul, item ) {
532 .append( $( "<a>" ).text( item.label ) )
536 _move: function( direction, event ) {
537 if ( !this.menu.element.is( ":visible" ) ) {
538 this.search( null, event );
541 if ( this.menu.isFirstItem() && /^previous/.test( direction ) ||
542 this.menu.isLastItem() && /^next/.test( direction ) ) {
543 this._value( this.term );
547 this.menu[ direction ]( event );
551 return this.menu.element;
555 return this.valueMethod.apply( this.element, arguments );
558 _keyEvent: function( keyEvent, event ) {
559 if ( !this.isMultiLine || this.menu.element.is( ":visible" ) ) {
560 this._move( keyEvent, event );
562 // prevents moving cursor to beginning/end of the text field in some browsers
563 event.preventDefault();
568 $.extend( $.ui.autocomplete, {
569 escapeRegex: function( value ) {
570 return value.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&");
572 filter: function(array, term) {
573 var matcher = new RegExp( $.ui.autocomplete.escapeRegex(term), "i" );
574 return $.grep( array, function(value) {
575 return matcher.test( value.label || value.value || value );
581 // live region extension, adding a `messages` option
582 // NOTE: This is an experimental API. We are still investigating
583 // a full solution for string manipulation and internationalization.
584 $.widget( "ui.autocomplete", $.ui.autocomplete, {
587 noResults: "No search results.",
588 results: function( amount ) {
589 return amount + ( amount > 1 ? " results are" : " result is" ) +
590 " available, use up and down arrow keys to navigate.";
595 __response: function( content ) {
597 this._superApply( arguments );
598 if ( this.options.disabled || this.cancelSearch ) {
601 if ( content && content.length ) {
602 message = this.options.messages.results( content.length );
604 message = this.options.messages.noResults;
606 this.liveRegion.text( message );