2010-01-14 17:23:11 +00:00
|
|
|
/*
|
|
|
|
* jQuery UI Autocomplete @VERSION
|
|
|
|
*
|
2010-01-20 14:04:26 +00:00
|
|
|
* Copyright (c) 2010 AUTHORS.txt (http://jqueryui.com/about)
|
2010-01-14 17:23:11 +00:00
|
|
|
* Dual licensed under the MIT (MIT-LICENSE.txt)
|
|
|
|
* and GPL (GPL-LICENSE.txt) licenses.
|
|
|
|
*
|
|
|
|
* 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-01-17 18:51:11 +00:00
|
|
|
(function( $ ) {
|
2010-01-14 17:23:11 +00:00
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
$.widget( "ui.autocomplete", {
|
2010-01-14 17:23:11 +00:00
|
|
|
options: {
|
|
|
|
minLength: 1,
|
|
|
|
delay: 300
|
|
|
|
},
|
2010-01-15 18:58:20 +00:00
|
|
|
_create: function() {
|
2010-03-11 03:28:00 +00:00
|
|
|
var self = this,
|
|
|
|
doc = this.element[ 0 ].ownerDocument;
|
2010-01-14 17:23:11 +00:00
|
|
|
this.element
|
2010-02-20 01:38:30 +00:00
|
|
|
.addClass( "ui-autocomplete-input" )
|
2010-01-17 18:51:11 +00:00
|
|
|
.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"
|
|
|
|
})
|
2010-01-17 18:51:11 +00:00
|
|
|
.bind( "keydown.autocomplete", function( event ) {
|
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:
|
2010-01-17 18:51:11 +00:00
|
|
|
self._move( "previousPage", event );
|
2010-01-14 17:23:11 +00:00
|
|
|
break;
|
|
|
|
case keyCode.PAGE_DOWN:
|
2010-01-17 18:51:11 +00:00
|
|
|
self._move( "nextPage", event );
|
2010-01-14 17:23:11 +00:00
|
|
|
break;
|
|
|
|
case keyCode.UP:
|
2010-01-17 18:51:11 +00:00
|
|
|
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:
|
2010-01-17 18:51:11 +00:00
|
|
|
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:
|
|
|
|
// when menu is open or has focus
|
2010-02-20 01:32:55 +00:00
|
|
|
if ( self.menu.active ) {
|
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:
|
2010-02-20 01:32:55 +00:00
|
|
|
if ( !self.menu.active ) {
|
2010-01-14 17:23:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-03-20 20:17:56 +00:00
|
|
|
self.menu.select( event );
|
2010-01-14 17:23:11 +00:00
|
|
|
break;
|
|
|
|
case keyCode.ESCAPE:
|
2010-01-17 18:51:11 +00:00
|
|
|
self.element.val( self.term );
|
|
|
|
self.close( event );
|
2010-01-14 17:23:11 +00:00
|
|
|
break;
|
2010-03-20 18:58:38 +00:00
|
|
|
case keyCode.LEFT:
|
|
|
|
case keyCode.RIGHT:
|
2010-03-11 22:06:29 +00:00
|
|
|
case keyCode.SHIFT:
|
|
|
|
case keyCode.CONTROL:
|
2010-03-26 16:40:46 +00:00
|
|
|
case keyCode.ALT:
|
2010-01-14 17:23:11 +00:00
|
|
|
// ignore metakeys (shift, ctrl, alt)
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// keypress is triggered before the input value is changed
|
2010-01-17 18:51:11 +00:00
|
|
|
clearTimeout( self.searching );
|
2010-01-14 17:23:11 +00:00
|
|
|
self.searching = setTimeout(function() {
|
2010-01-17 18:51:11 +00:00
|
|
|
self.search( null, event );
|
|
|
|
}, self.options.delay );
|
2010-01-14 17:23:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
})
|
2010-01-17 18:51:11 +00:00
|
|
|
.bind( "focus.autocomplete", function() {
|
2010-04-08 10:05:52 +00:00
|
|
|
self.selectedItem = null;
|
2010-01-14 17:23:11 +00:00
|
|
|
self.previous = self.element.val();
|
|
|
|
})
|
2010-01-17 18:51:11 +00:00
|
|
|
.bind( "blur.autocomplete", function( event ) {
|
|
|
|
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
|
|
|
|
// TODO try to implement this without a timeout, see clearTimeout in search()
|
|
|
|
self.closing = setTimeout(function() {
|
2010-01-17 18:51:11 +00:00
|
|
|
self.close( event );
|
2010-04-08 10:05:52 +00:00
|
|
|
self._change( event );
|
2010-01-17 18:51:11 +00:00
|
|
|
}, 150 );
|
2010-01-14 17:23:11 +00:00
|
|
|
});
|
|
|
|
this._initSource();
|
|
|
|
this.response = function() {
|
2010-01-17 18:51:11 +00:00
|
|
|
return self._response.apply( self, arguments );
|
2010-01-14 17:23:11 +00:00
|
|
|
};
|
2010-02-08 01:17:04 +00:00
|
|
|
this.menu = $( "<ul></ul>" )
|
2010-02-02 14:04:50 +00:00
|
|
|
.addClass( "ui-autocomplete" )
|
2010-03-11 03:28:00 +00:00
|
|
|
.appendTo( "body", doc )
|
2010-02-02 14:04:50 +00:00
|
|
|
.menu({
|
2010-04-30 11:04:09 +00:00
|
|
|
// custom key handling for now
|
|
|
|
input: $(),
|
2010-02-02 14:04:50 +00:00
|
|
|
focus: function( event, ui ) {
|
|
|
|
var item = ui.item.data( "item.autocomplete" );
|
|
|
|
if ( false !== self._trigger( "focus", null, { item: item } ) ) {
|
2010-03-20 14:57:06 +00:00
|
|
|
// 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 );
|
|
|
|
}
|
2010-02-02 14:04:50 +00:00
|
|
|
}
|
|
|
|
},
|
2010-04-30 11:18:34 +00:00
|
|
|
select: function( event, ui ) {
|
2010-02-02 14:04:50 +00:00
|
|
|
var item = ui.item.data( "item.autocomplete" );
|
|
|
|
if ( false !== self._trigger( "select", event, { item: item } ) ) {
|
|
|
|
self.element.val( item.value );
|
|
|
|
}
|
|
|
|
self.close( event );
|
|
|
|
// only trigger when focus was lost (click on menu)
|
2010-04-08 10:05:52 +00:00
|
|
|
var previous = self.previous;
|
2010-03-11 03:28:00 +00:00
|
|
|
if ( self.element[0] !== doc.activeElement ) {
|
2010-02-02 14:04:50 +00:00
|
|
|
self.element.focus();
|
2010-04-08 10:05:52 +00:00
|
|
|
self.previous = previous;
|
2010-02-02 14:04:50 +00:00
|
|
|
}
|
2010-04-08 10:05:52 +00:00
|
|
|
self.selectedItem = item;
|
2010-03-15 21:11:21 +00:00
|
|
|
},
|
|
|
|
blur: function( event, ui ) {
|
2010-03-15 21:24:42 +00:00
|
|
|
if ( self.menu.element.is(":visible") ) {
|
|
|
|
self.element.val( self.term );
|
|
|
|
}
|
2010-02-02 14:04:50 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.zIndex( this.element.zIndex() + 1 )
|
|
|
|
// workaround for jQuery bug #5781 http://dev.jquery.com/ticket/5781
|
|
|
|
.css({ top: 0, left: 0 })
|
|
|
|
.hide()
|
|
|
|
.data( "menu" );
|
|
|
|
if ( $.fn.bgiframe ) {
|
2010-02-18 01:23:45 +00:00
|
|
|
this.menu.element.bgiframe();
|
2010-02-02 14:04:50 +00:00
|
|
|
}
|
2010-01-14 17:23:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function() {
|
|
|
|
this.element
|
2010-03-26 16:42:03 +00:00
|
|
|
.removeClass( "ui-autocomplete-input" )
|
2010-01-17 18:51:11 +00:00
|
|
|
.removeAttr( "autocomplete" )
|
|
|
|
.removeAttr( "role" )
|
|
|
|
.removeAttr( "aria-autocomplete" )
|
|
|
|
.removeAttr( "aria-haspopup" );
|
2010-02-08 01:17:04 +00:00
|
|
|
this.menu.element.remove();
|
2010-01-17 18:51:11 +00:00
|
|
|
$.Widget.prototype.destroy.call( this );
|
2010-01-14 17:23:11 +00:00
|
|
|
},
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
_setOption: function( key ) {
|
|
|
|
$.Widget.prototype._setOption.apply( this, arguments );
|
2010-03-11 22:06:29 +00:00
|
|
|
if ( key === "source" ) {
|
2010-01-14 17:23:11 +00:00
|
|
|
this._initSource();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_initSource: function() {
|
2010-03-11 22:06:29 +00:00
|
|
|
var array,
|
|
|
|
url;
|
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 ) {
|
2010-04-16 09:05:35 +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 ) {
|
|
|
|
$.getJSON( url, request, response );
|
2010-01-14 17:23:11 +00:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
this.source = this.options.source;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
search: function( value, event ) {
|
2010-01-14 17:23:11 +00:00
|
|
|
value = value != null ? value : this.element.val();
|
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-01-17 18:51:11 +00:00
|
|
|
clearTimeout( this.closing );
|
|
|
|
if ( this._trigger("search") === 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-01-14 17:23:11 +00:00
|
|
|
this.term = this.element
|
2010-01-17 18:51:11 +00:00
|
|
|
.addClass( "ui-autocomplete-loading" )
|
2010-01-14 17:23:11 +00:00
|
|
|
// always save the actual value, not the one passed as an argument
|
|
|
|
.val();
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
this.source( { term: value }, this.response );
|
2010-01-14 17:23:11 +00:00
|
|
|
},
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
_response: function( content ) {
|
|
|
|
if ( content.length ) {
|
|
|
|
content = this._normalize( content );
|
|
|
|
this._suggest( content );
|
2010-03-08 16:35:24 +00:00
|
|
|
this._trigger( "open" );
|
2010-01-14 17:23:11 +00:00
|
|
|
} else {
|
|
|
|
this.close();
|
|
|
|
}
|
2010-01-17 18:51:11 +00:00
|
|
|
this.element.removeClass( "ui-autocomplete-loading" );
|
2010-01-14 17:23:11 +00:00
|
|
|
},
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
close: function( event ) {
|
|
|
|
clearTimeout( this.closing );
|
2010-02-02 14:04:50 +00:00
|
|
|
if ( this.menu.element.is(":visible") ) {
|
2010-01-17 18:51:11 +00:00
|
|
|
this._trigger( "close", event );
|
2010-02-02 14:04:50 +00:00
|
|
|
this.menu.element.hide();
|
2010-02-20 01:32:55 +00:00
|
|
|
this.menu.deactivate();
|
2010-01-14 17:23:11 +00:00
|
|
|
}
|
2010-04-08 10:05:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_change: function( event ) {
|
2010-03-11 22:06:29 +00:00
|
|
|
if ( this.previous !== this.element.val() ) {
|
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;
|
|
|
|
}
|
2010-01-17 18:51:11 +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-03-11 03:28:00 +00:00
|
|
|
.empty()
|
2010-03-11 22:06:29 +00:00
|
|
|
.zIndex( this.element.zIndex() + 1 ),
|
|
|
|
menuWidth,
|
|
|
|
textWidth;
|
2010-02-16 16:20:05 +00:00
|
|
|
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();
|
2010-02-02 14:04:50 +00:00
|
|
|
this.menu.refresh();
|
2010-02-23 02:57:33 +00:00
|
|
|
this.menu.element.show().position({
|
|
|
|
my: "left top",
|
|
|
|
at: "left bottom",
|
|
|
|
of: this.element,
|
|
|
|
collision: "none"
|
|
|
|
});
|
2010-03-11 04:37:21 +00:00
|
|
|
|
2010-03-11 22:06:29 +00:00
|
|
|
menuWidth = ul.width( "" ).width();
|
|
|
|
textWidth = this.element.width();
|
2010-03-11 04:37:21 +00:00
|
|
|
ul.width( Math.max( menuWidth, textWidth ) );
|
2010-01-14 17:23:11 +00:00
|
|
|
},
|
2010-02-16 16:20:05 +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
|
|
|
|
2010-02-08 01:31:10 +00:00
|
|
|
_renderItem: function( ul, item) {
|
|
|
|
return $( "<li></li>" )
|
|
|
|
.data( "item.autocomplete", item )
|
|
|
|
.append( "<a>" + item.label + "</a>" )
|
|
|
|
.appendTo( ul );
|
|
|
|
},
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
_move: function( direction, event ) {
|
2010-02-08 01:17:04 +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;
|
|
|
|
}
|
2010-01-17 18:51:11 +00:00
|
|
|
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;
|
|
|
|
}
|
2010-03-20 14:57:06 +00:00
|
|
|
this.menu[ direction ]( event );
|
2010-01-14 17:23:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
widget: function() {
|
2010-02-02 14:04:50 +00:00
|
|
|
return this.menu.element;
|
2010-01-14 17:23:11 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2010-01-17 18:51:11 +00:00
|
|
|
$.extend( $.ui.autocomplete, {
|
|
|
|
escapeRegex: function( value ) {
|
|
|
|
return value.replace( /([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1" );
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2010-03-11 22:06:29 +00:00
|
|
|
}( jQuery ));
|