2 * jQuery UI Autocomplete 1.8.24
4 * Copyright 2012, AUTHORS.txt (http://jqueryui.com/about)
5 * Dual licensed under the MIT or GPL Version 2 licenses.
6 * http://jquery.org/license
8 * http://docs.jquery.com/UI/Autocomplete
13 * jquery.ui.position.js
15 (function( $, undefined ) {
17 // used to prevent race conditions with remote data sources
20 $.widget( "ui.autocomplete", {
38 doc = this.element[ 0 ].ownerDocument,
40 this.isMultiLine = this.element.is( "textarea" );
43 .addClass( "ui-autocomplete-input" )
44 .attr( "autocomplete", "off" )
45 // TODO verify these actually work as intended
48 "aria-autocomplete": "list",
49 "aria-haspopup": "true"
51 .bind( "keydown.autocomplete", function( event ) {
52 if ( self.options.disabled || self.element.propAttr( "readOnly" ) ) {
56 suppressKeyPress = false;
57 var keyCode = $.ui.keyCode;
58 switch( event.keyCode ) {
60 self._move( "previousPage", event );
62 case keyCode.PAGE_DOWN:
63 self._move( "nextPage", event );
66 self._keyEvent( "previous", event );
69 self._keyEvent( "next", event );
72 case keyCode.NUMPAD_ENTER:
73 // when menu is open and has focus
74 if ( self.menu.active ) {
75 // #6055 - Opera still allows the keypress to occur
76 // which causes forms to submit
77 suppressKeyPress = true;
78 event.preventDefault();
80 //passthrough - ENTER and TAB both select the current element
82 if ( !self.menu.active ) {
85 self.menu.select( event );
88 self.element.val( self.term );
92 // keypress is triggered before the input value is changed
93 clearTimeout( self.searching );
94 self.searching = setTimeout(function() {
95 // only search if the value has changed
96 if ( self.term != self.element.val() ) {
97 self.selectedItem = null;
98 self.search( null, event );
100 }, self.options.delay );
104 .bind( "keypress.autocomplete", function( event ) {
105 if ( suppressKeyPress ) {
106 suppressKeyPress = false;
107 event.preventDefault();
110 .bind( "focus.autocomplete", function() {
111 if ( self.options.disabled ) {
115 self.selectedItem = null;
116 self.previous = self.element.val();
118 .bind( "blur.autocomplete", function( event ) {
119 if ( self.options.disabled ) {
123 clearTimeout( self.searching );
124 // clicks on the menu (or a button to trigger a search) will cause a blur event
125 self.closing = setTimeout(function() {
127 self._change( event );
131 this.menu = $( "<ul></ul>" )
132 .addClass( "ui-autocomplete" )
133 .appendTo( $( this.options.appendTo || "body", doc )[0] )
134 // prevent the close-on-blur in case of a "slow" click on the menu (long mousedown)
135 .mousedown(function( event ) {
136 // clicking on the scrollbar causes focus to shift to the body
137 // but we can't detect a mouseup or a click immediately afterward
138 // so we have to track the next mousedown and close the menu if
139 // the user clicks somewhere outside of the autocomplete
140 var menuElement = self.menu.element[ 0 ];
141 if ( !$( event.target ).closest( ".ui-menu-item" ).length ) {
142 setTimeout(function() {
143 $( document ).one( 'mousedown', function( event ) {
144 if ( event.target !== self.element[ 0 ] &&
145 event.target !== menuElement &&
146 !$.ui.contains( menuElement, event.target ) ) {
153 // use another timeout to make sure the blur-event-handler on the input was already triggered
154 setTimeout(function() {
155 clearTimeout( self.closing );
159 focus: function( event, ui ) {
160 var item = ui.item.data( "item.autocomplete" );
161 if ( false !== self._trigger( "focus", event, { item: item } ) ) {
162 // use value to match what will end up in the input, if it was a key event
163 if ( /^key/.test(event.originalEvent.type) ) {
164 self.element.val( item.value );
168 selected: function( event, ui ) {
169 var item = ui.item.data( "item.autocomplete" ),
170 previous = self.previous;
172 // only trigger when focus was lost (click on menu)
173 if ( self.element[0] !== doc.activeElement ) {
174 self.element.focus();
175 self.previous = previous;
176 // #6109 - IE triggers two focus events and the second
177 // is asynchronous, so we need to reset the previous
178 // term synchronously and asynchronously :-(
179 setTimeout(function() {
180 self.previous = previous;
181 self.selectedItem = item;
185 if ( false !== self._trigger( "select", event, { item: item } ) ) {
186 self.element.val( item.value );
188 // reset the term after the select event
189 // this allows custom select handling to work properly
190 self.term = self.element.val();
193 self.selectedItem = item;
195 blur: function( event, ui ) {
196 // don't set the value of the text field if it's already correct
197 // this prevents moving the cursor unnecessarily
198 if ( self.menu.element.is(":visible") &&
199 ( self.element.val() !== self.term ) ) {
200 self.element.val( self.term );
204 .zIndex( this.element.zIndex() + 1 )
205 // workaround for jQuery bug #5781 http://dev.jquery.com/ticket/5781
206 .css({ top: 0, left: 0 })
209 if ( $.fn.bgiframe ) {
210 this.menu.element.bgiframe();
212 // turning off autocomplete prevents the browser from remembering the
213 // value when navigating through history, so we re-enable autocomplete
214 // if the page is unloaded before the widget is destroyed. #7790
215 self.beforeunloadHandler = function() {
216 self.element.removeAttr( "autocomplete" );
218 $( window ).bind( "beforeunload", self.beforeunloadHandler );
221 destroy: function() {
223 .removeClass( "ui-autocomplete-input" )
224 .removeAttr( "autocomplete" )
225 .removeAttr( "role" )
226 .removeAttr( "aria-autocomplete" )
227 .removeAttr( "aria-haspopup" );
228 this.menu.element.remove();
229 $( window ).unbind( "beforeunload", this.beforeunloadHandler );
230 $.Widget.prototype.destroy.call( this );
233 _setOption: function( key, value ) {
234 $.Widget.prototype._setOption.apply( this, arguments );
235 if ( key === "source" ) {
238 if ( key === "appendTo" ) {
239 this.menu.element.appendTo( $( value || "body", this.element[0].ownerDocument )[0] )
241 if ( key === "disabled" && value && this.xhr ) {
246 _initSource: function() {
250 if ( $.isArray(this.options.source) ) {
251 array = this.options.source;
252 this.source = function( request, response ) {
253 response( $.ui.autocomplete.filter(array, request.term) );
255 } else if ( typeof this.options.source === "string" ) {
256 url = this.options.source;
257 this.source = function( request, response ) {
265 success: function( data, status ) {
274 this.source = this.options.source;
278 search: function( value, event ) {
279 value = value != null ? value : this.element.val();
281 // always save the actual value, not the one passed as an argument
282 this.term = this.element.val();
284 if ( value.length < this.options.minLength ) {
285 return this.close( event );
288 clearTimeout( this.closing );
289 if ( this._trigger( "search", event ) === false ) {
293 return this._search( value );
296 _search: function( value ) {
298 this.element.addClass( "ui-autocomplete-loading" );
300 this.source( { term: value }, this._response() );
303 _response: function() {
305 index = ++requestIndex;
307 return function( content ) {
308 if ( index === requestIndex ) {
309 that.__response( content );
313 if ( !that.pending ) {
314 that.element.removeClass( "ui-autocomplete-loading" );
319 __response: function( content ) {
320 if ( !this.options.disabled && content && content.length ) {
321 content = this._normalize( content );
322 this._suggest( content );
323 this._trigger( "open" );
329 close: function( event ) {
330 clearTimeout( this.closing );
331 if ( this.menu.element.is(":visible") ) {
332 this.menu.element.hide();
333 this.menu.deactivate();
334 this._trigger( "close", event );
338 _change: function( event ) {
339 if ( this.previous !== this.element.val() ) {
340 this._trigger( "change", event, { item: this.selectedItem } );
344 _normalize: function( items ) {
345 // assume all items have the right format when the first item is complete
346 if ( items.length && items[0].label && items[0].value ) {
349 return $.map( items, function(item) {
350 if ( typeof item === "string" ) {
357 label: item.label || item.value,
358 value: item.value || item.label
363 _suggest: function( items ) {
364 var ul = this.menu.element
366 .zIndex( this.element.zIndex() + 1 );
367 this._renderMenu( ul, items );
368 // TODO refresh should check if the active item is still in the dom, removing the need for a manual deactivate
369 this.menu.deactivate();
372 // size and position menu
375 ul.position( $.extend({
377 }, this.options.position ));
379 if ( this.options.autoFocus ) {
380 this.menu.next( new $.Event("mouseover") );
384 _resizeMenu: function() {
385 var ul = this.menu.element;
386 ul.outerWidth( Math.max(
387 // Firefox wraps long text (possibly a rounding bug)
388 // so we add 1px to avoid the wrapping (#7513)
389 ul.width( "" ).outerWidth() + 1,
390 this.element.outerWidth()
394 _renderMenu: function( ul, items ) {
396 $.each( items, function( index, item ) {
397 self._renderItem( ul, item );
401 _renderItem: function( ul, item) {
402 return $( "<li></li>" )
403 .data( "item.autocomplete", item )
404 .append( $( "<a></a>" ).text( item.label ) )
408 _move: function( direction, event ) {
409 if ( !this.menu.element.is(":visible") ) {
410 this.search( null, event );
413 if ( this.menu.first() && /^previous/.test(direction) ||
414 this.menu.last() && /^next/.test(direction) ) {
415 this.element.val( this.term );
416 this.menu.deactivate();
419 this.menu[ direction ]( event );
423 return this.menu.element;
425 _keyEvent: function( keyEvent, event ) {
426 if ( !this.isMultiLine || this.menu.element.is( ":visible" ) ) {
427 this._move( keyEvent, event );
429 // prevents moving cursor to beginning/end of the text field in some browsers
430 event.preventDefault();
435 $.extend( $.ui.autocomplete, {
436 escapeRegex: function( value ) {
437 return value.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
439 filter: function(array, term) {
440 var matcher = new RegExp( $.ui.autocomplete.escapeRegex(term), "i" );
441 return $.grep( array, function(value) {
442 return matcher.test( value.label || value.value || value );
450 * jQuery UI Menu (not officially released)
452 * This widget isn't yet finished and the API is subject to change. We plan to finish
453 * it for the next release. You're welcome to give it a try anyway and give us feedback,
454 * as long as you're okay with migrating your code later on. We can help with that, too.
456 * Copyright 2010, AUTHORS.txt (http://jqueryui.com/about)
457 * Dual licensed under the MIT or GPL Version 2 licenses.
458 * http://jquery.org/license
460 * http://docs.jquery.com/UI/Menu
464 * jquery.ui.widget.js
468 $.widget("ui.menu", {
469 _create: function() {
472 .addClass("ui-menu ui-widget ui-widget-content ui-corner-all")
475 "aria-activedescendant": "ui-active-menuitem"
477 .click(function( event ) {
478 if ( !$( event.target ).closest( ".ui-menu-item a" ).length ) {
482 event.preventDefault();
483 self.select( event );
488 refresh: function() {
491 // don't refresh list items that are already adapted
492 var items = this.element.children("li:not(.ui-menu-item):has(a)")
493 .addClass("ui-menu-item")
494 .attr("role", "menuitem");
497 .addClass("ui-corner-all")
498 .attr("tabindex", -1)
499 // mouseenter doesn't work with event delegation
500 .mouseenter(function( event ) {
501 self.activate( event, $(this).parent() );
503 .mouseleave(function() {
508 activate: function( event, item ) {
510 if (this.hasScroll()) {
511 var offset = item.offset().top - this.element.offset().top,
512 scroll = this.element.scrollTop(),
513 elementHeight = this.element.height();
515 this.element.scrollTop( scroll + offset);
516 } else if (offset >= elementHeight) {
517 this.element.scrollTop( scroll + offset - elementHeight + item.height());
520 this.active = item.eq(0)
522 .addClass("ui-state-hover")
523 .attr("id", "ui-active-menuitem")
525 this._trigger("focus", event, { item: item });
528 deactivate: function() {
529 if (!this.active) { return; }
531 this.active.children("a")
532 .removeClass("ui-state-hover")
534 this._trigger("blur");
538 next: function(event) {
539 this.move("next", ".ui-menu-item:first", event);
542 previous: function(event) {
543 this.move("prev", ".ui-menu-item:last", event);
547 return this.active && !this.active.prevAll(".ui-menu-item").length;
551 return this.active && !this.active.nextAll(".ui-menu-item").length;
554 move: function(direction, edge, event) {
556 this.activate(event, this.element.children(edge));
559 var next = this.active[direction + "All"](".ui-menu-item").eq(0);
561 this.activate(event, next);
563 this.activate(event, this.element.children(edge));
567 // TODO merge with previousPage
568 nextPage: function(event) {
569 if (this.hasScroll()) {
570 // TODO merge with no-scroll-else
571 if (!this.active || this.last()) {
572 this.activate(event, this.element.children(".ui-menu-item:first"));
575 var base = this.active.offset().top,
576 height = this.element.height(),
577 result = this.element.children(".ui-menu-item").filter(function() {
578 var close = $(this).offset().top - base - height + $(this).height();
579 // TODO improve approximation
580 return close < 10 && close > -10;
583 // TODO try to catch this earlier when scrollTop indicates the last page anyway
584 if (!result.length) {
585 result = this.element.children(".ui-menu-item:last");
587 this.activate(event, result);
589 this.activate(event, this.element.children(".ui-menu-item")
590 .filter(!this.active || this.last() ? ":first" : ":last"));
594 // TODO merge with nextPage
595 previousPage: function(event) {
596 if (this.hasScroll()) {
597 // TODO merge with no-scroll-else
598 if (!this.active || this.first()) {
599 this.activate(event, this.element.children(".ui-menu-item:last"));
603 var base = this.active.offset().top,
604 height = this.element.height(),
605 result = this.element.children(".ui-menu-item").filter(function() {
606 var close = $(this).offset().top - base + height - $(this).height();
607 // TODO improve approximation
608 return close < 10 && close > -10;
611 // TODO try to catch this earlier when scrollTop indicates the last page anyway
612 if (!result.length) {
613 result = this.element.children(".ui-menu-item:first");
615 this.activate(event, result);
617 this.activate(event, this.element.children(".ui-menu-item")
618 .filter(!this.active || this.first() ? ":last" : ":first"));
622 hasScroll: function() {
623 return this.element.height() < this.element[ $.fn.prop ? "prop" : "attr" ]("scrollHeight");
626 select: function( event ) {
627 this._trigger("selected", event, { item: this.active });