2012-04-03 11:14:47 +00:00
|
|
|
|
/*!
|
2010-01-14 17:23:11 +00:00
|
|
|
|
* jQuery UI Autocomplete @VERSION
|
|
|
|
|
*
|
2012-03-08 15:53:08 +00:00
|
|
|
|
* Copyright 2012, AUTHORS.txt (http://jqueryui.com/about)
|
2010-07-09 13:01:04 +00:00
|
|
|
|
* 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
|
2010-04-21 19:01:08 +00:00
|
|
|
|
* jquery.ui.menu.js
|
2010-01-14 17:23:11 +00:00
|
|
|
|
*/
|
2010-07-13 13:57:58 +00:00
|
|
|
|
(function( $, undefined ) {
|
2010-01-14 17:23:11 +00:00
|
|
|
|
|
2011-01-27 19:49:59 +00:00
|
|
|
|
// used to prevent race conditions with remote data sources
|
|
|
|
|
var requestIndex = 0;
|
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
|
$.widget( "ui.autocomplete", {
|
2011-05-28 19:39:55 +00:00
|
|
|
|
version: "@VERSION",
|
2011-01-23 22:55:53 +00:00
|
|
|
|
defaultElement: "<input>",
|
2010-01-14 17:23:11 +00:00
|
|
|
|
options: {
|
2010-07-21 18:54:20 +00:00
|
|
|
|
appendTo: "body",
|
2011-03-28 15:13:53 +00:00
|
|
|
|
autoFocus: false,
|
2010-07-19 13:28:04 +00:00
|
|
|
|
delay: 300,
|
2010-01-14 17:23:11 +00:00
|
|
|
|
minLength: 1,
|
2010-07-19 13:28:04 +00:00
|
|
|
|
position: {
|
|
|
|
|
my: "left top",
|
|
|
|
|
at: "left bottom",
|
|
|
|
|
collision: "none"
|
|
|
|
|
},
|
2011-04-25 12:10:39 +00:00
|
|
|
|
source: null,
|
|
|
|
|
|
|
|
|
|
// callbacks
|
|
|
|
|
change: null,
|
|
|
|
|
close: null,
|
|
|
|
|
focus: null,
|
|
|
|
|
open: null,
|
|
|
|
|
response: null,
|
|
|
|
|
search: null,
|
|
|
|
|
select: null
|
2010-01-14 17:23:11 +00:00
|
|
|
|
},
|
2010-12-17 16:15:17 +00:00
|
|
|
|
|
|
|
|
|
pending: 0,
|
|
|
|
|
|
2010-01-15 18:58:20 +00:00
|
|
|
|
_create: function() {
|
2012-05-09 13:12:46 +00:00
|
|
|
|
// Some browsers only repeat keydown events, not keypress events,
|
|
|
|
|
// so we use the suppressKeyPress flag to determine if we've already
|
|
|
|
|
// handled the keydown event. #7269
|
|
|
|
|
// Unfortunately the code for & in keypress is the same as the up arrow,
|
|
|
|
|
// so we use the suppressKeyPressRepeat flag to avoid handling keypress
|
|
|
|
|
// events when we know the keydown event was used to modify the
|
|
|
|
|
// search term. #7799
|
|
|
|
|
var suppressKeyPress, suppressKeyPressRepeat, suppressInput;
|
2010-10-06 14:24:03 +00:00
|
|
|
|
|
2011-11-28 21:33:23 +00:00
|
|
|
|
this.isMultiLine = this.element.is( "textarea,[contenteditable]" );
|
2011-08-31 00:45:48 +00:00
|
|
|
|
this.valueMethod = this.element[ this.element.is( "input,textarea" ) ? "val" : "text" ];
|
2012-05-18 19:55:10 +00:00
|
|
|
|
this.isNewMenu = true;
|
2011-02-24 09:22:51 +00:00
|
|
|
|
|
2010-01-14 17:23:11 +00:00
|
|
|
|
this.element
|
2010-02-20 01:38:30 +00:00
|
|
|
|
.addClass( "ui-autocomplete-input" )
|
2012-05-15 12:07:35 +00:00
|
|
|
|
.attr( "autocomplete", "off" );
|
2012-05-09 13:12:46 +00:00
|
|
|
|
|
|
|
|
|
this._bind({
|
|
|
|
|
keydown: function( event ) {
|
|
|
|
|
if ( this.element.prop( "readOnly" ) ) {
|
2011-05-27 12:35:18 +00:00
|
|
|
|
suppressKeyPress = true;
|
2011-05-11 03:24:21 +00:00
|
|
|
|
suppressInput = true;
|
2011-10-17 12:09:53 +00:00
|
|
|
|
suppressKeyPressRepeat = true;
|
2010-07-30 16:59:33 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-06 14:24:03 +00:00
|
|
|
|
suppressKeyPress = false;
|
2011-05-11 03:24:21 +00:00
|
|
|
|
suppressInput = false;
|
2011-10-17 12:09:53 +00:00
|
|
|
|
suppressKeyPressRepeat = false;
|
2010-01-14 17:23:11 +00:00
|
|
|
|
var keyCode = $.ui.keyCode;
|
2010-01-17 18:51:11 +00:00
|
|
|
|
switch( event.keyCode ) {
|
2010-01-14 17:23:11 +00:00
|
|
|
|
case keyCode.PAGE_UP:
|
2011-05-27 12:35:18 +00:00
|
|
|
|
suppressKeyPress = true;
|
2012-05-09 13:12:46 +00:00
|
|
|
|
this._move( "previousPage", event );
|
2010-01-14 17:23:11 +00:00
|
|
|
|
break;
|
|
|
|
|
case keyCode.PAGE_DOWN:
|
2011-05-27 12:35:18 +00:00
|
|
|
|
suppressKeyPress = true;
|
2012-05-09 13:12:46 +00:00
|
|
|
|
this._move( "nextPage", event );
|
2010-01-14 17:23:11 +00:00
|
|
|
|
break;
|
|
|
|
|
case keyCode.UP:
|
2011-05-27 12:35:18 +00:00
|
|
|
|
suppressKeyPress = true;
|
2012-05-09 13:12:46 +00:00
|
|
|
|
this._keyEvent( "previous", event );
|
2010-01-14 17:23:11 +00:00
|
|
|
|
break;
|
|
|
|
|
case keyCode.DOWN:
|
2011-05-27 12:35:18 +00:00
|
|
|
|
suppressKeyPress = true;
|
2012-05-09 13:12:46 +00:00
|
|
|
|
this._keyEvent( "next", event );
|
2010-01-14 17:23:11 +00:00
|
|
|
|
break;
|
|
|
|
|
case keyCode.ENTER:
|
2010-04-23 17:00:54 +00:00
|
|
|
|
case keyCode.NUMPAD_ENTER:
|
2010-09-27 14:10:00 +00:00
|
|
|
|
// when menu is open and has focus
|
2012-05-09 13:12:46 +00:00
|
|
|
|
if ( this.menu.active ) {
|
2010-10-06 14:24:03 +00:00
|
|
|
|
// #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();
|
2012-05-09 13:12:46 +00:00
|
|
|
|
this.menu.select( event );
|
2010-01-14 17:23:11 +00:00
|
|
|
|
}
|
2012-04-04 08:52:41 +00:00
|
|
|
|
break;
|
2010-01-14 17:23:11 +00:00
|
|
|
|
case keyCode.TAB:
|
2012-05-09 13:12:46 +00:00
|
|
|
|
if ( this.menu.active ) {
|
|
|
|
|
this.menu.select( event );
|
2010-01-14 17:23:11 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case keyCode.ESCAPE:
|
2012-05-18 20:23:03 +00:00
|
|
|
|
if ( this.menu.element.is( ":visible" ) ) {
|
2012-05-09 13:12:46 +00:00
|
|
|
|
this._value( this.term );
|
|
|
|
|
this.close( event );
|
2012-04-30 15:48:37 +00:00
|
|
|
|
// Different browsers have different default behavior for escape
|
|
|
|
|
// Single press can mean undo or clear
|
|
|
|
|
// Double press in IE means clear the whole form
|
|
|
|
|
event.preventDefault();
|
2011-10-12 20:25:59 +00:00
|
|
|
|
}
|
2010-01-14 17:23:11 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
2011-10-17 12:09:53 +00:00
|
|
|
|
suppressKeyPressRepeat = true;
|
2011-05-11 03:24:21 +00:00
|
|
|
|
// search timeout should be triggered before the input value is changed
|
2012-05-09 13:12:46 +00:00
|
|
|
|
this._searchTimeout( event );
|
2010-01-14 17:23:11 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2012-05-09 13:12:46 +00:00
|
|
|
|
},
|
|
|
|
|
keypress: function( event ) {
|
2010-10-06 14:24:03 +00:00
|
|
|
|
if ( suppressKeyPress ) {
|
|
|
|
|
suppressKeyPress = false;
|
|
|
|
|
event.preventDefault();
|
2011-05-27 12:35:18 +00:00
|
|
|
|
return;
|
2010-10-06 14:24:03 +00:00
|
|
|
|
}
|
2011-10-17 12:09:53 +00:00
|
|
|
|
if ( suppressKeyPressRepeat ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2011-04-19 18:04:08 +00:00
|
|
|
|
|
2011-05-10 15:43:21 +00:00
|
|
|
|
// replicate some key handlers to allow them to repeat in Firefox and Opera
|
2011-04-19 18:04:08 +00:00
|
|
|
|
var keyCode = $.ui.keyCode;
|
|
|
|
|
switch( event.keyCode ) {
|
|
|
|
|
case keyCode.PAGE_UP:
|
2012-05-09 13:12:46 +00:00
|
|
|
|
this._move( "previousPage", event );
|
2011-04-19 18:04:08 +00:00
|
|
|
|
break;
|
|
|
|
|
case keyCode.PAGE_DOWN:
|
2012-05-09 13:12:46 +00:00
|
|
|
|
this._move( "nextPage", event );
|
2011-04-19 18:04:08 +00:00
|
|
|
|
break;
|
|
|
|
|
case keyCode.UP:
|
2012-05-09 13:12:46 +00:00
|
|
|
|
this._keyEvent( "previous", event );
|
2011-04-19 18:04:08 +00:00
|
|
|
|
break;
|
|
|
|
|
case keyCode.DOWN:
|
2012-05-09 13:12:46 +00:00
|
|
|
|
this._keyEvent( "next", event );
|
2011-04-19 18:04:08 +00:00
|
|
|
|
break;
|
2011-05-27 12:35:18 +00:00
|
|
|
|
}
|
2012-05-09 13:12:46 +00:00
|
|
|
|
},
|
|
|
|
|
input: function( event ) {
|
2011-05-11 03:24:21 +00:00
|
|
|
|
if ( suppressInput ) {
|
|
|
|
|
suppressInput = false;
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-05-09 13:12:46 +00:00
|
|
|
|
this._searchTimeout( event );
|
|
|
|
|
},
|
|
|
|
|
focus: function() {
|
|
|
|
|
this.selectedItem = null;
|
|
|
|
|
this.previous = this._value();
|
|
|
|
|
},
|
|
|
|
|
blur: function( event ) {
|
|
|
|
|
if ( this.cancelBlur ) {
|
|
|
|
|
delete this.cancelBlur;
|
2010-07-30 16:59:33 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-09 13:12:46 +00:00
|
|
|
|
clearTimeout( this.searching );
|
|
|
|
|
this.close( event );
|
|
|
|
|
this._change( event );
|
|
|
|
|
}
|
|
|
|
|
});
|
2012-02-11 20:43:38 +00:00
|
|
|
|
|
2010-01-14 17:23:11 +00:00
|
|
|
|
this._initSource();
|
2012-05-18 20:23:03 +00:00
|
|
|
|
this.menu = $( "<ul>" )
|
2010-02-02 14:04:50 +00:00
|
|
|
|
.addClass( "ui-autocomplete" )
|
2012-05-18 20:23:03 +00:00
|
|
|
|
.appendTo( this.document.find( this.options.appendTo || "body" )[ 0 ] )
|
2012-05-09 13:12:46 +00:00
|
|
|
|
.menu({
|
|
|
|
|
// custom key handling for now
|
2012-05-15 12:07:35 +00:00
|
|
|
|
input: $(),
|
|
|
|
|
// disable ARIA support, the live region takes care of that
|
|
|
|
|
role: null
|
2012-05-09 13:12:46 +00:00
|
|
|
|
})
|
|
|
|
|
.zIndex( this.element.zIndex() + 1 )
|
|
|
|
|
.hide()
|
|
|
|
|
.data( "menu" );
|
|
|
|
|
this._bind( this.menu.element, {
|
|
|
|
|
mousedown: function( event ) {
|
2012-02-11 20:43:38 +00:00
|
|
|
|
// prevent moving focus out of the text field
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
|
|
// IE doesn't prevent moving focus even with event.preventDefault()
|
|
|
|
|
// so we set a flag to know when we should ignore the blur event
|
2012-05-09 13:12:46 +00:00
|
|
|
|
this.cancelBlur = true;
|
|
|
|
|
this._delay(function() {
|
|
|
|
|
delete this.cancelBlur;
|
|
|
|
|
});
|
2012-02-11 20:43:38 +00:00
|
|
|
|
|
2010-08-05 12:51:54 +00:00
|
|
|
|
// 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
|
2012-05-09 13:12:46 +00:00
|
|
|
|
var menuElement = this.menu.element[ 0 ];
|
2010-09-20 13:27:32 +00:00
|
|
|
|
if ( !$( event.target ).closest( ".ui-menu-item" ).length ) {
|
2012-05-09 13:12:46 +00:00
|
|
|
|
this._delay(function() {
|
|
|
|
|
var that = this;
|
2012-05-18 20:23:03 +00:00
|
|
|
|
this.document.one( "mousedown", function( event ) {
|
2012-05-09 13:12:46 +00:00
|
|
|
|
if ( event.target !== that.element[ 0 ] &&
|
|
|
|
|
event.target !== menuElement &&
|
|
|
|
|
!$.contains( menuElement, event.target ) ) {
|
|
|
|
|
that.close();
|
2010-08-05 12:51:54 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
2012-05-09 13:12:46 +00:00
|
|
|
|
});
|
2010-08-05 12:51:54 +00:00
|
|
|
|
}
|
2012-05-09 13:12:46 +00:00
|
|
|
|
},
|
|
|
|
|
menufocus: function( event, ui ) {
|
2012-05-18 19:55:10 +00:00
|
|
|
|
// #7024 - Prevent accidental activation of menu items in Firefox
|
|
|
|
|
if ( this.isNewMenu ) {
|
|
|
|
|
this.isNewMenu = false;
|
2012-05-18 20:23:03 +00:00
|
|
|
|
if ( event.originalEvent && /^mouse/.test( event.originalEvent.type ) ) {
|
2012-05-18 19:55:10 +00:00
|
|
|
|
this.menu.blur();
|
|
|
|
|
|
|
|
|
|
this.document.one( "mousemove", function() {
|
|
|
|
|
$( event.target ).trigger( event.originalEvent );
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-09 13:12:46 +00:00
|
|
|
|
// back compat for _renderItem using item.autocomplete, via #7810
|
|
|
|
|
// TODO remove the fallback, see #8156
|
|
|
|
|
var item = ui.item.data( "ui-autocomplete-item" ) || ui.item.data( "item.autocomplete" );
|
|
|
|
|
if ( false !== this._trigger( "focus", event, { item: item } ) ) {
|
|
|
|
|
// use value to match what will end up in the input, if it was a key event
|
2012-05-18 20:23:03 +00:00
|
|
|
|
if ( event.originalEvent && /^key/.test( event.originalEvent.type ) ) {
|
2012-05-09 13:12:46 +00:00
|
|
|
|
this._value( item.value );
|
2010-02-02 14:04:50 +00:00
|
|
|
|
}
|
2012-05-16 13:43:49 +00:00
|
|
|
|
} else {
|
|
|
|
|
// Normally the input is populated with the item's value as the
|
|
|
|
|
// menu is navigated, causing screen readers to notice a change and
|
|
|
|
|
// announce the item. Since the focus event was canceled, this doesn't
|
|
|
|
|
// happen, so we update the live region so that screen readers can
|
|
|
|
|
// still notice the change and announce it.
|
|
|
|
|
this.liveRegion.text( item.value );
|
2012-05-09 13:12:46 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
menuselect: function( event, ui ) {
|
|
|
|
|
// back compat for _renderItem using item.autocomplete, via #7810
|
|
|
|
|
// TODO remove the fallback, see #8156
|
|
|
|
|
var item = ui.item.data( "ui-autocomplete-item" ) || ui.item.data( "item.autocomplete" ),
|
|
|
|
|
previous = this.previous;
|
|
|
|
|
|
|
|
|
|
// only trigger when focus was lost (click on menu)
|
|
|
|
|
if ( this.element[0] !== this.document[0].activeElement ) {
|
|
|
|
|
this.element.focus();
|
|
|
|
|
this.previous = previous;
|
|
|
|
|
// #6109 - IE triggers two focus events and the second
|
|
|
|
|
// is asynchronous, so we need to reset the previous
|
|
|
|
|
// term synchronously and asynchronously :-(
|
2012-05-10 16:57:41 +00:00
|
|
|
|
this._delay(function() {
|
2012-05-09 13:12:46 +00:00
|
|
|
|
this.previous = previous;
|
|
|
|
|
this.selectedItem = item;
|
2012-05-10 16:57:41 +00:00
|
|
|
|
});
|
2012-05-09 13:12:46 +00:00
|
|
|
|
}
|
2010-07-20 16:10:58 +00:00
|
|
|
|
|
2012-05-09 13:12:46 +00:00
|
|
|
|
if ( false !== this._trigger( "select", event, { item: item } ) ) {
|
|
|
|
|
this._value( item.value );
|
2010-02-02 14:04:50 +00:00
|
|
|
|
}
|
2012-05-09 13:12:46 +00:00
|
|
|
|
// reset the term after the select event
|
|
|
|
|
// this allows custom select handling to work properly
|
|
|
|
|
this.term = this._value();
|
|
|
|
|
|
|
|
|
|
this.close( event );
|
|
|
|
|
this.selectedItem = item;
|
|
|
|
|
}
|
|
|
|
|
});
|
2011-10-17 13:25:36 +00:00
|
|
|
|
|
2012-05-16 13:43:49 +00:00
|
|
|
|
this.liveRegion = $( "<span>", {
|
|
|
|
|
role: "status",
|
|
|
|
|
"aria-live": "polite"
|
|
|
|
|
})
|
|
|
|
|
.addClass( "ui-helper-hidden-accessible" )
|
|
|
|
|
.insertAfter( this.element );
|
|
|
|
|
|
2010-02-02 14:04:50 +00:00
|
|
|
|
if ( $.fn.bgiframe ) {
|
2012-04-02 23:12:21 +00:00
|
|
|
|
this.menu.element.bgiframe();
|
2010-02-02 14:04:50 +00:00
|
|
|
|
}
|
2011-10-17 13:25:36 +00:00
|
|
|
|
|
|
|
|
|
// turning off autocomplete prevents the browser from remembering the
|
|
|
|
|
// value when navigating through history, so we re-enable autocomplete
|
|
|
|
|
// if the page is unloaded before the widget is destroyed. #7790
|
2011-10-20 20:57:47 +00:00
|
|
|
|
this._bind( this.window, {
|
2011-10-17 13:25:36 +00:00
|
|
|
|
beforeunload: function() {
|
|
|
|
|
this.element.removeAttr( "autocomplete" );
|
|
|
|
|
}
|
|
|
|
|
});
|
2010-01-14 17:23:11 +00:00
|
|
|
|
},
|
|
|
|
|
|
2011-01-14 22:11:22 +00:00
|
|
|
|
_destroy: function() {
|
2011-08-07 06:50:34 +00:00
|
|
|
|
clearTimeout( this.searching );
|
2010-01-14 17:23:11 +00:00
|
|
|
|
this.element
|
2010-03-26 16:42:03 +00:00
|
|
|
|
.removeClass( "ui-autocomplete-input" )
|
2012-05-23 23:28:52 +00:00
|
|
|
|
.removeAttr( "autocomplete" );
|
2010-02-08 01:17:04 +00:00
|
|
|
|
this.menu.element.remove();
|
2012-05-16 13:43:49 +00:00
|
|
|
|
this.liveRegion.remove();
|
2010-01-14 17:23:11 +00:00
|
|
|
|
},
|
|
|
|
|
|
2010-07-21 18:54:20 +00:00
|
|
|
|
_setOption: function( key, value ) {
|
2011-11-18 11:19:32 +00:00
|
|
|
|
this._super( key, value );
|
2010-03-11 22:06:29 +00:00
|
|
|
|
if ( key === "source" ) {
|
2010-01-14 17:23:11 +00:00
|
|
|
|
this._initSource();
|
|
|
|
|
}
|
2010-07-21 18:54:20 +00:00
|
|
|
|
if ( key === "appendTo" ) {
|
2011-10-20 20:57:47 +00:00
|
|
|
|
this.menu.element.appendTo( this.document.find( value || "body" )[0] );
|
2010-07-21 18:54:20 +00:00
|
|
|
|
}
|
2010-12-17 15:48:17 +00:00
|
|
|
|
if ( key === "disabled" && value && this.xhr ) {
|
|
|
|
|
this.xhr.abort();
|
|
|
|
|
}
|
2010-01-14 17:23:11 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_initSource: function() {
|
2012-05-18 20:23:03 +00:00
|
|
|
|
var array, url,
|
|
|
|
|
that = this;
|
2010-01-17 18:51:11 +00:00
|
|
|
|
if ( $.isArray(this.options.source) ) {
|
2010-03-11 22:06:29 +00:00
|
|
|
|
array = this.options.source;
|
2010-01-17 18:51:11 +00:00
|
|
|
|
this.source = function( request, response ) {
|
2012-05-18 20:23:03 +00:00
|
|
|
|
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;
|
2010-01-17 18:51:11 +00:00
|
|
|
|
this.source = function( request, response ) {
|
2012-05-09 13:12:46 +00:00
|
|
|
|
if ( that.xhr ) {
|
|
|
|
|
that.xhr.abort();
|
2010-08-25 01:41:34 +00:00
|
|
|
|
}
|
2012-05-09 13:12:46 +00:00
|
|
|
|
that.xhr = $.ajax({
|
2010-12-01 03:19:20 +00:00
|
|
|
|
url: url,
|
|
|
|
|
data: request,
|
|
|
|
|
dataType: "json",
|
2011-01-27 19:49:59 +00:00
|
|
|
|
success: function( data, status ) {
|
2012-04-03 20:47:14 +00:00
|
|
|
|
response( data );
|
2010-12-01 03:19:20 +00:00
|
|
|
|
},
|
2011-01-27 19:49:59 +00:00
|
|
|
|
error: function() {
|
2012-04-03 20:47:14 +00:00
|
|
|
|
response( [] );
|
2010-08-24 13:14:44 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
2010-01-14 17:23:11 +00:00
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
this.source = this.options.source;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2011-05-11 03:24:21 +00:00
|
|
|
|
_searchTimeout: function( event ) {
|
2012-05-09 13:12:46 +00:00
|
|
|
|
clearTimeout( this.searching );
|
|
|
|
|
this.searching = this._delay(function() {
|
2011-05-11 03:24:21 +00:00
|
|
|
|
// only search if the value has changed
|
2012-05-09 13:12:46 +00:00
|
|
|
|
if ( this.term !== this._value() ) {
|
|
|
|
|
this.selectedItem = null;
|
|
|
|
|
this.search( null, event );
|
2011-05-11 03:24:21 +00:00
|
|
|
|
}
|
2012-05-09 13:12:46 +00:00
|
|
|
|
}, this.options.delay );
|
2011-05-11 03:24:21 +00:00
|
|
|
|
},
|
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
|
search: function( value, event ) {
|
2011-02-24 09:22:51 +00:00
|
|
|
|
value = value != null ? value : this._value();
|
2010-08-10 23:02:50 +00:00
|
|
|
|
|
|
|
|
|
// always save the actual value, not the one passed as an argument
|
2011-02-24 09:22:51 +00:00
|
|
|
|
this.term = this._value();
|
2010-08-10 23:02:50 +00:00
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
|
if ( value.length < this.options.minLength ) {
|
|
|
|
|
return this.close( event );
|
2010-01-14 17:23:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-11 12:51:54 +00:00
|
|
|
|
if ( this._trigger( "search", event ) === false ) {
|
2010-01-14 17:23:11 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
|
return this._search( value );
|
2010-01-14 17:23:11 +00:00
|
|
|
|
},
|
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
|
_search: function( value ) {
|
2010-12-17 16:15:17 +00:00
|
|
|
|
this.pending++;
|
2010-08-10 23:02:50 +00:00
|
|
|
|
this.element.addClass( "ui-autocomplete-loading" );
|
2011-11-23 18:39:45 +00:00
|
|
|
|
this.cancelSearch = false;
|
2010-01-14 17:23:11 +00:00
|
|
|
|
|
2012-04-03 20:47:14 +00:00
|
|
|
|
this.source( { term: value }, this._response() );
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_response: function() {
|
|
|
|
|
var that = this,
|
|
|
|
|
index = ++requestIndex;
|
|
|
|
|
|
|
|
|
|
return function( content ) {
|
|
|
|
|
if ( index === requestIndex ) {
|
|
|
|
|
that.__response( content );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
that.pending--;
|
|
|
|
|
if ( !that.pending ) {
|
|
|
|
|
that.element.removeClass( "ui-autocomplete-loading" );
|
|
|
|
|
}
|
|
|
|
|
};
|
2010-01-14 17:23:11 +00:00
|
|
|
|
},
|
|
|
|
|
|
2012-04-03 20:47:14 +00:00
|
|
|
|
__response: function( content ) {
|
2011-03-22 18:07:49 +00:00
|
|
|
|
if ( content ) {
|
2010-01-17 18:51:11 +00:00
|
|
|
|
content = this._normalize( content );
|
2011-03-22 18:07:49 +00:00
|
|
|
|
}
|
|
|
|
|
this._trigger( "response", null, { content: content } );
|
2011-11-23 18:39:45 +00:00
|
|
|
|
if ( !this.options.disabled && content && content.length && !this.cancelSearch ) {
|
2010-01-17 18:51:11 +00:00
|
|
|
|
this._suggest( content );
|
2010-03-08 16:35:24 +00:00
|
|
|
|
this._trigger( "open" );
|
2010-01-14 17:23:11 +00:00
|
|
|
|
} else {
|
2012-02-13 00:27:20 +00:00
|
|
|
|
// use ._close() instead of .close() so we don't cancel future searches
|
2012-02-11 15:12:51 +00:00
|
|
|
|
this._close();
|
2010-01-14 17:23:11 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
|
close: function( event ) {
|
2012-02-11 15:12:51 +00:00
|
|
|
|
this.cancelSearch = true;
|
|
|
|
|
this._close( event );
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_close: function( event ) {
|
2012-05-18 20:23:03 +00:00
|
|
|
|
if ( this.menu.element.is( ":visible" ) ) {
|
2010-02-02 14:04:50 +00:00
|
|
|
|
this.menu.element.hide();
|
2011-02-24 11:05:20 +00:00
|
|
|
|
this.menu.blur();
|
2012-05-18 19:55:10 +00:00
|
|
|
|
this.isNewMenu = true;
|
2010-10-26 13:05:38 +00:00
|
|
|
|
this._trigger( "close", event );
|
2010-01-14 17:23:11 +00:00
|
|
|
|
}
|
2010-04-08 10:05:52 +00:00
|
|
|
|
},
|
2011-09-24 17:07:08 +00:00
|
|
|
|
|
2010-04-08 10:05:52 +00:00
|
|
|
|
_change: function( event ) {
|
2011-02-24 09:22:51 +00:00
|
|
|
|
if ( this.previous !== this._value() ) {
|
2010-04-08 10:05:52 +00:00
|
|
|
|
this._trigger( "change", event, { item: this.selectedItem } );
|
2010-01-14 17:23:11 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2010-01-17 18:51: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
|
2010-01-17 18:51:11 +00:00
|
|
|
|
if ( items.length && items[0].label && items[0].value ) {
|
2010-01-14 17:23:11 +00:00
|
|
|
|
return items;
|
|
|
|
|
}
|
2012-05-18 20:23:03 +00:00
|
|
|
|
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
|
2010-01-17 18:51:11 +00:00
|
|
|
|
}, item );
|
2010-01-14 17:23:11 +00:00
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
|
_suggest: function( items ) {
|
2010-03-11 22:06:29 +00:00
|
|
|
|
var ul = this.menu.element
|
2010-10-22 02:15:13 +00:00
|
|
|
|
.empty()
|
|
|
|
|
.zIndex( this.element.zIndex() + 1 );
|
2010-02-16 16:20:05 +00:00
|
|
|
|
this._renderMenu( ul, items );
|
2011-02-24 11:05:20 +00:00
|
|
|
|
// TODO refresh should check if the active item is still in the dom, removing the need for a manual blur
|
|
|
|
|
this.menu.blur();
|
2010-02-02 14:04:50 +00:00
|
|
|
|
this.menu.refresh();
|
2010-03-11 04:37:21 +00:00
|
|
|
|
|
2010-11-12 13:41:36 +00:00
|
|
|
|
// size and position menu
|
|
|
|
|
ul.show();
|
2010-10-22 02:15:13 +00:00
|
|
|
|
this._resizeMenu();
|
2010-11-12 13:41:36 +00:00
|
|
|
|
ul.position( $.extend({
|
|
|
|
|
of: this.element
|
|
|
|
|
}, this.options.position ));
|
2011-03-15 16:26:47 +00:00
|
|
|
|
|
|
|
|
|
if ( this.options.autoFocus ) {
|
2012-05-18 19:34:47 +00:00
|
|
|
|
this.menu.next();
|
2011-03-15 16:26:47 +00:00
|
|
|
|
}
|
2010-10-22 02:15:13 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_resizeMenu: function() {
|
|
|
|
|
var ul = this.menu.element;
|
|
|
|
|
ul.outerWidth( Math.max(
|
2011-10-25 21:19:26 +00:00
|
|
|
|
// Firefox wraps long text (possibly a rounding bug)
|
|
|
|
|
// so we add 1px to avoid the wrapping (#7513)
|
|
|
|
|
ul.width( "" ).outerWidth() + 1,
|
2010-10-22 02:15:13 +00:00
|
|
|
|
this.element.outerWidth()
|
|
|
|
|
) );
|
2010-01-14 17:23:11 +00:00
|
|
|
|
},
|
2010-07-22 02:17:52 +00:00
|
|
|
|
|
2010-02-16 16:20:05 +00:00
|
|
|
|
_renderMenu: function( ul, items ) {
|
2012-05-09 13:12:46 +00:00
|
|
|
|
var that = this;
|
2010-02-16 16:20:05 +00:00
|
|
|
|
$.each( items, function( index, item ) {
|
2012-05-09 13:12:46 +00:00
|
|
|
|
that._renderItemData( ul, item );
|
2010-02-16 16:20:05 +00:00
|
|
|
|
});
|
|
|
|
|
},
|
2010-01-14 17:23:11 +00:00
|
|
|
|
|
2012-02-28 14:15:32 +00:00
|
|
|
|
_renderItemData: function( ul, item ) {
|
|
|
|
|
return this._renderItem( ul, item ).data( "ui-autocomplete-item", item );
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_renderItem: function( ul, item ) {
|
2012-05-18 20:23:03 +00:00
|
|
|
|
return $( "<li>" )
|
|
|
|
|
.append( $( "<a>" ).text( item.label ) )
|
2010-02-08 01:31:10 +00:00
|
|
|
|
.appendTo( ul );
|
|
|
|
|
},
|
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
|
_move: function( direction, event ) {
|
2012-05-18 20:23:03 +00:00
|
|
|
|
if ( !this.menu.element.is( ":visible" ) ) {
|
2010-01-17 18:51:11 +00:00
|
|
|
|
this.search( null, event );
|
2010-01-14 17:23:11 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2012-05-18 20:23:03 +00:00
|
|
|
|
if ( this.menu.isFirstItem() && /^previous/.test( direction ) ||
|
|
|
|
|
this.menu.isLastItem() && /^next/.test( direction ) ) {
|
2011-02-24 09:22:51 +00:00
|
|
|
|
this._value( this.term );
|
2011-02-24 11:05:20 +00:00
|
|
|
|
this.menu.blur();
|
2010-01-14 17:23:11 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2010-03-20 14:57:06 +00:00
|
|
|
|
this.menu[ direction ]( event );
|
2010-01-14 17:23:11 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
widget: function() {
|
2010-07-30 16:38:18 +00:00
|
|
|
|
return this.menu.element;
|
2011-02-24 09:22:51 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_value: function( value ) {
|
|
|
|
|
return this.valueMethod.apply( this.element, arguments );
|
2011-11-28 21:33:23 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_keyEvent: function( keyEvent, event ) {
|
|
|
|
|
if ( !this.isMultiLine || this.menu.element.is( ":visible" ) ) {
|
|
|
|
|
this._move( keyEvent, event );
|
|
|
|
|
|
|
|
|
|
// prevents moving cursor to beginning/end of the text field in some browsers
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
}
|
2010-01-14 17:23:11 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
|
$.extend( $.ui.autocomplete, {
|
|
|
|
|
escapeRegex: function( value ) {
|
2012-04-04 08:52:41 +00:00
|
|
|
|
return value.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&");
|
2010-04-16 09:05:35 +00:00
|
|
|
|
},
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2012-05-15 12:07:35 +00:00
|
|
|
|
|
|
|
|
|
// live region extension, adding a `messages` option
|
2012-05-16 13:43:49 +00:00
|
|
|
|
// NOTE: This is an experimental API. We are still investigating
|
|
|
|
|
// a full solution for string manipulation and internationalization.
|
2012-05-15 12:07:35 +00:00
|
|
|
|
$.widget( "ui.autocomplete", $.ui.autocomplete, {
|
|
|
|
|
options: {
|
|
|
|
|
messages: {
|
|
|
|
|
noResults: "No search results.",
|
|
|
|
|
results: function(amount) {
|
2012-05-16 13:43:49 +00:00
|
|
|
|
return amount + ( amount > 1 ? " results are" : " result is" ) +
|
|
|
|
|
" available, use up and down arrow keys to navigate.";
|
2012-05-15 12:07:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
2012-05-16 13:43:49 +00:00
|
|
|
|
|
2012-05-15 12:07:35 +00:00
|
|
|
|
__response: function( content ) {
|
|
|
|
|
var message;
|
|
|
|
|
this._superApply( arguments );
|
|
|
|
|
if ( this.options.disabled || this.cancelSearch) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ( content && content.length ) {
|
|
|
|
|
message = this.options.messages.results( content.length );
|
|
|
|
|
} else {
|
|
|
|
|
message = this.options.messages.noResults;
|
|
|
|
|
}
|
|
|
|
|
this.liveRegion.text( message );
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2010-03-11 22:06:29 +00:00
|
|
|
|
}( jQuery ));
|