2012-04-03 11:14:47 +00:00
|
|
|
/*!
|
2010-01-14 17:23:11 +00:00
|
|
|
* jQuery UI Autocomplete @VERSION
|
2012-07-04 13:08:08 +00:00
|
|
|
* http://jqueryui.com
|
2010-01-14 17:23:11 +00:00
|
|
|
*
|
2014-12-21 18:27:43 +00:00
|
|
|
* Copyright jQuery Foundation and other contributors
|
2012-08-09 14:13:24 +00:00
|
|
|
* Released under the MIT license.
|
2010-07-09 13:01:04 +00:00
|
|
|
* http://jquery.org/license
|
2010-01-14 17:23:11 +00:00
|
|
|
*/
|
2014-10-30 19:55:08 +00:00
|
|
|
|
|
|
|
//>>label: Autocomplete
|
|
|
|
//>>group: Widgets
|
|
|
|
//>>description: Lists suggested words as the user is typing.
|
|
|
|
//>>docs: http://api.jqueryui.com/autocomplete/
|
|
|
|
//>>demos: http://jqueryui.com/autocomplete/
|
2015-10-21 23:08:44 +00:00
|
|
|
//>>css.structure: ../../themes/base/core.css
|
|
|
|
//>>css.structure: ../../themes/base/autocomplete.css
|
|
|
|
//>>css.theme: ../../themes/base/theme.css
|
2014-10-30 19:55:08 +00:00
|
|
|
|
2015-03-17 17:32:13 +00:00
|
|
|
( function( factory ) {
|
2013-07-12 16:40:48 +00:00
|
|
|
if ( typeof define === "function" && define.amd ) {
|
|
|
|
|
|
|
|
// AMD. Register as an anonymous module.
|
2015-03-17 17:32:13 +00:00
|
|
|
define( [
|
2013-07-12 16:40:48 +00:00
|
|
|
"jquery",
|
2015-07-15 02:03:37 +00:00
|
|
|
"./menu",
|
2015-07-15 01:56:36 +00:00
|
|
|
"../keycode",
|
|
|
|
"../position",
|
|
|
|
"../safe-active-element",
|
|
|
|
"../version",
|
|
|
|
"../widget"
|
2013-07-12 16:40:48 +00:00
|
|
|
], factory );
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Browser globals
|
|
|
|
factory( jQuery );
|
|
|
|
}
|
2015-03-17 17:32:13 +00:00
|
|
|
}( function( $ ) {
|
2010-01-14 17:23:11 +00:00
|
|
|
|
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: {
|
2012-12-14 14:10:32 +00:00
|
|
|
appendTo: null,
|
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,
|
|
|
|
|
2015-08-21 04:11:54 +00:00
|
|
|
// Callbacks
|
2011-04-25 12:10:39 +00:00
|
|
|
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
|
|
|
|
2013-05-28 15:04:29 +00:00
|
|
|
requestIndex: 0,
|
2010-12-17 16:15:17 +00:00
|
|
|
pending: 0,
|
|
|
|
|
2010-01-15 18:58:20 +00:00
|
|
|
_create: function() {
|
2015-08-24 13:04:59 +00:00
|
|
|
|
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
|
2013-02-05 14:33:48 +00:00
|
|
|
var suppressKeyPress, suppressKeyPressRepeat, suppressInput,
|
2013-12-07 17:00:20 +00:00
|
|
|
nodeName = this.element[ 0 ].nodeName.toLowerCase(),
|
2013-02-05 14:33:48 +00:00
|
|
|
isTextarea = nodeName === "textarea",
|
|
|
|
isInput = nodeName === "input";
|
|
|
|
|
2015-03-13 16:16:24 +00:00
|
|
|
// Textareas are always multi-line
|
|
|
|
// Inputs are always single-line, even if inside a contentEditable element
|
|
|
|
// IE also treats inputs as contentEditable
|
|
|
|
// All other element types are determined by whether or not they're contentEditable
|
|
|
|
this.isMultiLine = isTextarea || !isInput && this.element.prop( "isContentEditable" );
|
2013-02-05 14:33:48 +00:00
|
|
|
|
|
|
|
this.valueMethod = this.element[ isTextarea || isInput ? "val" : "text" ];
|
2012-05-18 19:55:10 +00:00
|
|
|
this.isNewMenu = true;
|
2011-02-24 09:22:51 +00:00
|
|
|
|
2015-02-27 20:11:33 +00:00
|
|
|
this._addClass( "ui-autocomplete-input" );
|
|
|
|
this.element.attr( "autocomplete", "off" );
|
2012-05-09 13:12:46 +00:00
|
|
|
|
2012-10-24 16:00:03 +00:00
|
|
|
this._on( this.element, {
|
2012-05-09 13:12:46 +00:00
|
|
|
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;
|
2013-10-16 18:43:09 +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:
|
2015-08-24 13:04:59 +00:00
|
|
|
|
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 ) {
|
2015-08-24 13:04:59 +00:00
|
|
|
|
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" ) ) {
|
2014-02-04 21:47:26 +00:00
|
|
|
if ( !this.isMultiLine ) {
|
|
|
|
this._value( this.term );
|
|
|
|
}
|
2012-05-09 13:12:46 +00:00
|
|
|
this.close( event );
|
2015-08-24 13:04:59 +00:00
|
|
|
|
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;
|
2015-08-24 13:04:59 +00:00
|
|
|
|
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;
|
2013-03-20 22:42:34 +00:00
|
|
|
if ( !this.isMultiLine || this.menu.element.is( ":visible" ) ) {
|
|
|
|
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
|
|
|
|
2015-08-21 04:11:54 +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;
|
2013-10-16 18:43:09 +00:00
|
|
|
switch ( event.keyCode ) {
|
2011-04-19 18:04:08 +00:00
|
|
|
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
|
|
|
},
|
2012-08-26 21:09:39 +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 );
|
|
|
|
}
|
2015-03-17 17:32:13 +00:00
|
|
|
} );
|
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>" )
|
2012-11-28 15:33:44 +00:00
|
|
|
.appendTo( this._appendTo() )
|
2015-03-17 17:32:13 +00:00
|
|
|
.menu( {
|
2015-08-24 13:04:59 +00:00
|
|
|
|
2012-05-15 12:07:35 +00:00
|
|
|
// disable ARIA support, the live region takes care of that
|
|
|
|
role: null
|
2015-03-17 17:32:13 +00:00
|
|
|
} )
|
2012-05-09 13:12:46 +00:00
|
|
|
.hide()
|
2013-01-30 14:32:48 +00:00
|
|
|
.menu( "instance" );
|
2012-10-24 16:00:03 +00:00
|
|
|
|
2015-02-27 20:11:33 +00:00
|
|
|
this._addClass( this.menu.element, "ui-autocomplete", "ui-front" );
|
2012-06-13 12:00:45 +00:00
|
|
|
this._on( this.menu.element, {
|
2012-05-09 13:12:46 +00:00
|
|
|
mousedown: function( event ) {
|
2015-08-24 13:04:59 +00:00
|
|
|
|
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;
|
2015-03-17 17:32:13 +00:00
|
|
|
this._delay( function() {
|
2012-05-09 13:12:46 +00:00
|
|
|
delete this.cancelBlur;
|
2015-03-06 21:30:17 +00:00
|
|
|
|
|
|
|
// Support: IE 8 only
|
|
|
|
// Right clicking a menu item or selecting text from the menu items will
|
|
|
|
// result in focus moving out of the input. However, we've already received
|
|
|
|
// and ignored the blur event because of the cancelBlur flag set above. So
|
|
|
|
// we restore focus to ensure that the menu closes properly based on the user's
|
|
|
|
// next actions.
|
|
|
|
if ( this.element[ 0 ] !== $.ui.safeActiveElement( this.document[ 0 ] ) ) {
|
2015-05-14 01:54:08 +00:00
|
|
|
this.element.trigger( "focus" );
|
2015-03-06 21:30:17 +00:00
|
|
|
}
|
2015-03-17 17:32:13 +00:00
|
|
|
} );
|
2012-05-09 13:12:46 +00:00
|
|
|
},
|
|
|
|
menufocus: function( event, ui ) {
|
2013-12-22 16:36:43 +00:00
|
|
|
var label, item;
|
2015-08-24 13:04:59 +00:00
|
|
|
|
2013-02-25 20:36:08 +00:00
|
|
|
// support: Firefox
|
|
|
|
// Prevent accidental activation of menu items in Firefox (#7024 #9118)
|
2012-05-18 19:55:10 +00:00
|
|
|
if ( this.isNewMenu ) {
|
|
|
|
this.isNewMenu = false;
|
2012-05-18 20:23:03 +00:00
|
|
|
if ( event.originalEvent && /^mouse/.test( event.originalEvent.type ) ) {
|
2015-07-01 12:36:08 +00:00
|
|
|
this.menu.blur();
|
2012-05-18 19:55:10 +00:00
|
|
|
|
|
|
|
this.document.one( "mousemove", function() {
|
|
|
|
$( event.target ).trigger( event.originalEvent );
|
2015-03-17 17:32:13 +00:00
|
|
|
} );
|
2012-05-18 19:55:10 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-22 16:36:43 +00:00
|
|
|
item = ui.item.data( "ui-autocomplete-item" );
|
2012-05-09 13:12:46 +00:00
|
|
|
if ( false !== this._trigger( "focus", event, { item: item } ) ) {
|
2015-08-24 13:04:59 +00:00
|
|
|
|
2012-05-09 13:12:46 +00:00
|
|
|
// 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
|
|
|
}
|
2013-12-22 16:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Announce the value in the liveRegion
|
|
|
|
label = ui.item.attr( "aria-label" ) || item.value;
|
2014-08-08 12:47:25 +00:00
|
|
|
if ( label && $.trim( label ).length ) {
|
2013-12-22 16:36:43 +00:00
|
|
|
this.liveRegion.children().hide();
|
|
|
|
$( "<div>" ).text( label ).appendTo( this.liveRegion );
|
2012-05-09 13:12:46 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
menuselect: function( event, ui ) {
|
2012-10-25 10:47:19 +00:00
|
|
|
var item = ui.item.data( "ui-autocomplete-item" ),
|
2012-05-09 13:12:46 +00:00
|
|
|
previous = this.previous;
|
|
|
|
|
2015-08-21 04:11:54 +00:00
|
|
|
// Only trigger when focus was lost (click on menu)
|
2015-03-09 20:03:22 +00:00
|
|
|
if ( this.element[ 0 ] !== $.ui.safeActiveElement( this.document[ 0 ] ) ) {
|
2015-05-14 01:54:08 +00:00
|
|
|
this.element.trigger( "focus" );
|
2012-05-09 13:12:46 +00:00
|
|
|
this.previous = previous;
|
2015-08-24 13:04:59 +00:00
|
|
|
|
2012-05-09 13:12:46 +00:00
|
|
|
// #6109 - IE triggers two focus events and the second
|
|
|
|
// is asynchronous, so we need to reset the previous
|
|
|
|
// term synchronously and asynchronously :-(
|
2015-03-17 17:32:13 +00:00
|
|
|
this._delay( function() {
|
2012-05-09 13:12:46 +00:00
|
|
|
this.previous = previous;
|
|
|
|
this.selectedItem = item;
|
2015-03-17 17:32:13 +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
|
|
|
}
|
2015-08-24 13:04:59 +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;
|
|
|
|
}
|
2015-03-17 17:32:13 +00:00
|
|
|
} );
|
2011-10-17 13:25:36 +00:00
|
|
|
|
2015-09-18 15:19:59 +00:00
|
|
|
this.liveRegion = $( "<div>", {
|
2015-03-17 17:32:13 +00:00
|
|
|
role: "status",
|
|
|
|
"aria-live": "assertive",
|
|
|
|
"aria-relevant": "additions"
|
|
|
|
} )
|
2013-10-18 13:39:20 +00:00
|
|
|
.appendTo( this.document[ 0 ].body );
|
2012-05-16 13:43:49 +00:00
|
|
|
|
2015-02-27 20:11:33 +00:00
|
|
|
this._addClass( this.liveRegion, null, "ui-helper-hidden-accessible" );
|
|
|
|
|
2015-08-21 04:11:54 +00:00
|
|
|
// Turning off autocomplete prevents the browser from remembering the
|
2011-10-17 13:25:36 +00:00
|
|
|
// value when navigating through history, so we re-enable autocomplete
|
|
|
|
// if the page is unloaded before the widget is destroyed. #7790
|
2012-06-13 12:00:45 +00:00
|
|
|
this._on( this.window, {
|
2011-10-17 13:25:36 +00:00
|
|
|
beforeunload: function() {
|
|
|
|
this.element.removeAttr( "autocomplete" );
|
|
|
|
}
|
2015-03-17 17:32:13 +00:00
|
|
|
} );
|
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 );
|
2015-02-27 20:11:33 +00:00
|
|
|
this.element.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" ) {
|
2012-12-07 16:58:58 +00:00
|
|
|
this.menu.element.appendTo( this._appendTo() );
|
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
|
|
|
},
|
|
|
|
|
2015-10-15 14:11:14 +00:00
|
|
|
_isEventTargetInWidget: function( event ) {
|
|
|
|
var menuElement = this.menu.element[ 0 ];
|
|
|
|
|
|
|
|
return event.target === this.element[ 0 ] ||
|
|
|
|
event.target === menuElement ||
|
|
|
|
$.contains( menuElement, event.target );
|
|
|
|
},
|
|
|
|
|
|
|
|
_closeOnClickOutside: function( event ) {
|
|
|
|
if ( !this._isEventTargetInWidget( event ) ) {
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-11-28 15:33:44 +00:00
|
|
|
_appendTo: function() {
|
|
|
|
var element = this.options.appendTo;
|
2012-12-14 14:10:32 +00:00
|
|
|
|
|
|
|
if ( element ) {
|
|
|
|
element = element.jquery || element.nodeType ?
|
|
|
|
$( element ) :
|
|
|
|
this.document.find( element ).eq( 0 );
|
|
|
|
}
|
|
|
|
|
2014-01-20 21:13:55 +00:00
|
|
|
if ( !element || !element[ 0 ] ) {
|
2015-03-23 23:11:37 +00:00
|
|
|
element = this.element.closest( ".ui-front, dialog" );
|
2012-11-28 15:33:44 +00:00
|
|
|
}
|
2012-12-14 14:10:32 +00:00
|
|
|
|
|
|
|
if ( !element.length ) {
|
2013-12-07 17:00:20 +00:00
|
|
|
element = this.document[ 0 ].body;
|
2012-12-14 14:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return element;
|
2012-11-28 15:33:44 +00:00
|
|
|
},
|
|
|
|
|
2010-01-14 17:23:11 +00:00
|
|
|
_initSource: function() {
|
2012-05-18 20:23:03 +00:00
|
|
|
var array, url,
|
|
|
|
that = this;
|
2013-12-07 17:00:20 +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
|
|
|
}
|
2015-03-17 17:32:13 +00:00
|
|
|
that.xhr = $.ajax( {
|
2010-12-01 03:19:20 +00:00
|
|
|
url: url,
|
|
|
|
data: request,
|
|
|
|
dataType: "json",
|
2012-10-23 14:36:42 +00:00
|
|
|
success: function( data ) {
|
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() {
|
2015-03-17 17:32:13 +00:00
|
|
|
response( [] );
|
2010-08-24 13:14:44 +00:00
|
|
|
}
|
2015-03-17 17:32:13 +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 );
|
2015-03-17 17:32:13 +00:00
|
|
|
this.searching = this._delay( function() {
|
2014-05-01 13:57:38 +00:00
|
|
|
|
|
|
|
// Search if the value has changed, or if the user retypes the same value (see #7434)
|
|
|
|
var equalValues = this.term === this._value(),
|
|
|
|
menuVisible = this.menu.element.is( ":visible" ),
|
|
|
|
modifierKey = event.altKey || event.ctrlKey || event.metaKey || event.shiftKey;
|
|
|
|
|
|
|
|
if ( !equalValues || ( equalValues && !menuVisible && !modifierKey ) ) {
|
2012-05-09 13:12:46 +00:00
|
|
|
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
|
|
|
|
2015-08-21 04:11:54 +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++;
|
2015-02-27 20:11:33 +00:00
|
|
|
this._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() );
|
|
|
|
},
|
|
|
|
|
2013-05-28 15:04:29 +00:00
|
|
|
_response: function() {
|
|
|
|
var index = ++this.requestIndex;
|
2012-04-03 20:47:14 +00:00
|
|
|
|
2015-03-17 17:32:13 +00:00
|
|
|
return $.proxy( function( content ) {
|
2013-05-28 15:04:29 +00:00
|
|
|
if ( index === this.requestIndex ) {
|
|
|
|
this.__response( content );
|
|
|
|
}
|
2013-04-25 14:59:20 +00:00
|
|
|
|
2013-05-28 15:04:29 +00:00
|
|
|
this.pending--;
|
|
|
|
if ( !this.pending ) {
|
2015-02-27 20:11:33 +00:00
|
|
|
this._removeClass( "ui-autocomplete-loading" );
|
2013-05-28 15:04:29 +00:00
|
|
|
}
|
|
|
|
}, this );
|
|
|
|
},
|
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 {
|
2015-08-24 13:04:59 +00:00
|
|
|
|
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 ) {
|
2015-10-15 14:11:14 +00:00
|
|
|
|
|
|
|
// Remove the handler that closes the menu on outside clicks
|
|
|
|
this._off( this.document, "mousedown" );
|
|
|
|
|
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 ) {
|
2015-08-24 13:04:59 +00:00
|
|
|
|
2010-01-14 17:23:11 +00:00
|
|
|
// assume all items have the right format when the first item is complete
|
2013-12-07 17:00:20 +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
|
|
|
|
};
|
|
|
|
}
|
2014-01-22 15:40:19 +00:00
|
|
|
return $.extend( {}, item, {
|
2010-01-14 17:23:11 +00:00
|
|
|
label: item.label || item.value,
|
|
|
|
value: item.value || item.label
|
2015-03-17 17:32:13 +00:00
|
|
|
} );
|
|
|
|
} );
|
2010-01-14 17:23:11 +00:00
|
|
|
},
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
_suggest: function( items ) {
|
2013-02-05 18:01:32 +00:00
|
|
|
var ul = this.menu.element.empty();
|
2010-02-16 16:20:05 +00:00
|
|
|
this._renderMenu( ul, items );
|
2013-02-25 20:36:08 +00:00
|
|
|
this.isNewMenu = true;
|
2010-02-02 14:04:50 +00:00
|
|
|
this.menu.refresh();
|
2010-03-11 04:37:21 +00:00
|
|
|
|
2015-08-21 04:11:54 +00:00
|
|
|
// Size and position menu
|
2010-11-12 13:41:36 +00:00
|
|
|
ul.show();
|
2010-10-22 02:15:13 +00:00
|
|
|
this._resizeMenu();
|
2015-03-17 17:32:13 +00:00
|
|
|
ul.position( $.extend( {
|
2010-11-12 13:41:36 +00:00
|
|
|
of: this.element
|
2013-12-07 17:00:20 +00:00
|
|
|
}, 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
|
|
|
}
|
2015-10-15 14:11:14 +00:00
|
|
|
|
|
|
|
// Listen for interactions outside of the widget (#6642)
|
|
|
|
this._on( this.document, {
|
|
|
|
mousedown: "_closeOnClickOutside"
|
|
|
|
} );
|
2010-10-22 02:15:13 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_resizeMenu: function() {
|
|
|
|
var ul = this.menu.element;
|
|
|
|
ul.outerWidth( Math.max(
|
2015-08-24 13:04:59 +00:00
|
|
|
|
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 );
|
2015-03-17 17:32:13 +00:00
|
|
|
} );
|
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 ) {
|
2014-09-24 14:47:29 +00:00
|
|
|
return $( "<li>" )
|
|
|
|
.append( $( "<div>" ).text( item.label ) )
|
|
|
|
.appendTo( ul );
|
2010-02-08 01:31:10 +00:00
|
|
|
},
|
|
|
|
|
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 ) ) {
|
2014-01-25 22:39:27 +00:00
|
|
|
|
|
|
|
if ( !this.isMultiLine ) {
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2012-10-23 14:36:42 +00:00
|
|
|
_value: function() {
|
2011-02-24 09:22:51 +00:00
|
|
|
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 );
|
|
|
|
|
2015-08-21 04:11:54 +00:00
|
|
|
// Prevents moving cursor to beginning/end of the text field in some browsers
|
2011-11-28 21:33:23 +00:00
|
|
|
event.preventDefault();
|
|
|
|
}
|
2010-01-14 17:23:11 +00:00
|
|
|
}
|
2015-03-17 17:32:13 +00:00
|
|
|
} );
|
2010-01-14 17:23:11 +00:00
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
$.extend( $.ui.autocomplete, {
|
|
|
|
escapeRegex: function( value ) {
|
2013-12-07 17:00:20 +00:00
|
|
|
return value.replace( /[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&" );
|
2010-04-16 09:05:35 +00:00
|
|
|
},
|
2013-12-07 17:00:20 +00:00
|
|
|
filter: function( array, term ) {
|
|
|
|
var matcher = new RegExp( $.ui.autocomplete.escapeRegex( term ), "i" );
|
|
|
|
return $.grep( array, function( value ) {
|
2010-04-16 09:05:35 +00:00
|
|
|
return matcher.test( value.label || value.value || value );
|
2015-03-17 17:32:13 +00:00
|
|
|
} );
|
2010-01-14 17:23:11 +00:00
|
|
|
}
|
2015-03-17 17:32:13 +00:00
|
|
|
} );
|
2010-01-14 17:23:11 +00:00
|
|
|
|
2015-08-21 04:11:54 +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.",
|
2012-07-30 16:48:47 +00:00
|
|
|
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 );
|
2012-07-30 16:48:47 +00:00
|
|
|
if ( this.options.disabled || this.cancelSearch ) {
|
2012-05-15 12:07:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( content && content.length ) {
|
|
|
|
message = this.options.messages.results( content.length );
|
|
|
|
} else {
|
|
|
|
message = this.options.messages.noResults;
|
|
|
|
}
|
2013-12-22 16:36:43 +00:00
|
|
|
this.liveRegion.children().hide();
|
|
|
|
$( "<div>" ).text( message ).appendTo( this.liveRegion );
|
2012-05-15 12:07:35 +00:00
|
|
|
}
|
2015-03-17 17:32:13 +00:00
|
|
|
} );
|
2012-05-15 12:07:35 +00:00
|
|
|
|
2013-07-12 16:40:48 +00:00
|
|
|
return $.ui.autocomplete;
|
|
|
|
|
2015-03-17 17:32:13 +00:00
|
|
|
} ) );
|