jquery-ui/ui/jquery.ui.autocomplete.js

499 lines
13 KiB
JavaScript
Raw Normal View History

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-01-14 17:23:11 +00:00
*/
(function( $ ) {
2010-01-14 17:23:11 +00:00
$.widget( "ui.autocomplete", {
2010-01-14 17:23:11 +00:00
options: {
minLength: 1,
delay: 300
},
_create: function() {
var self = this,
doc = this.element[ 0 ].ownerDocument;
2010-01-14 17:23:11 +00:00
this.element
.addClass( "ui-autocomplete-input" )
.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"
})
.bind( "keydown.autocomplete", function( event ) {
2010-01-14 17:23:11 +00:00
var keyCode = $.ui.keyCode;
switch( event.keyCode ) {
2010-01-14 17:23:11 +00:00
case keyCode.PAGE_UP:
self._move( "previousPage", event );
2010-01-14 17:23:11 +00:00
break;
case keyCode.PAGE_DOWN:
self._move( "nextPage", event );
2010-01-14 17:23:11 +00:00
break;
case keyCode.UP:
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:
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
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:
if ( !self.menu.active ) {
2010-01-14 17:23:11 +00:00
return;
}
self.menu.select( event );
2010-01-14 17:23:11 +00:00
break;
case keyCode.ESCAPE:
self.element.val( self.term );
self.close( event );
2010-01-14 17:23:11 +00:00
break;
case keyCode.LEFT:
case keyCode.RIGHT:
2010-03-11 22:06:29 +00:00
case keyCode.SHIFT:
case keyCode.CONTROL:
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
clearTimeout( self.searching );
2010-01-14 17:23:11 +00:00
self.searching = setTimeout(function() {
self.search( null, event );
}, self.options.delay );
2010-01-14 17:23:11 +00:00
break;
}
})
.bind( "focus.autocomplete", function() {
self.selectedItem = null;
2010-01-14 17:23:11 +00:00
self.previous = self.element.val();
})
.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() {
self.close( event );
self._change( event );
}, 150 );
2010-01-14 17:23:11 +00:00
});
this._initSource();
this.response = function() {
return self._response.apply( self, arguments );
2010-01-14 17:23:11 +00:00
};
this.menu = $( "<ul></ul>" )
.addClass( "ui-autocomplete" )
.appendTo( "body", doc )
.menu({
focus: function( event, ui ) {
var item = ui.item.data( "item.autocomplete" );
if ( false !== self._trigger( "focus", null, { item: item } ) ) {
// 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 );
}
}
},
selected: function( event, ui ) {
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)
var previous = self.previous;
if ( self.element[0] !== doc.activeElement ) {
self.element.focus();
self.previous = previous;
}
self.selectedItem = item;
},
blur: function( event, ui ) {
if ( self.menu.element.is(":visible") ) {
self.element.val( self.term );
}
}
})
.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 ) {
this.menu.element.bgiframe();
}
2010-01-14 17:23:11 +00:00
},
destroy: function() {
this.element
.removeClass( "ui-autocomplete-input" )
.removeAttr( "autocomplete" )
.removeAttr( "role" )
.removeAttr( "aria-autocomplete" )
.removeAttr( "aria-haspopup" );
this.menu.element.remove();
$.Widget.prototype.destroy.call( this );
2010-01-14 17:23: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;
if ( $.isArray(this.options.source) ) {
2010-03-11 22:06:29 +00:00
array = this.options.source;
this.source = function( request, response ) {
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;
this.source = function( request, response ) {
$.getJSON( url, request, response );
2010-01-14 17:23:11 +00:00
};
} else {
this.source = this.options.source;
}
},
search: function( value, event ) {
2010-01-14 17:23:11 +00:00
value = value != null ? value : this.element.val();
if ( value.length < this.options.minLength ) {
return this.close( event );
2010-01-14 17:23:11 +00:00
}
clearTimeout( this.closing );
if ( this._trigger("search") === false ) {
2010-01-14 17:23:11 +00:00
return;
}
return this._search( value );
2010-01-14 17:23:11 +00:00
},
_search: function( value ) {
2010-01-14 17:23:11 +00:00
this.term = this.element
.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();
this.source( { term: value }, this.response );
2010-01-14 17:23:11 +00:00
},
_response: function( content ) {
if ( content.length ) {
content = this._normalize( content );
this._suggest( content );
this._trigger( "open" );
2010-01-14 17:23:11 +00:00
} else {
this.close();
}
this.element.removeClass( "ui-autocomplete-loading" );
2010-01-14 17:23:11 +00:00
},
close: function( event ) {
clearTimeout( this.closing );
if ( this.menu.element.is(":visible") ) {
this._trigger( "close", event );
this.menu.element.hide();
this.menu.deactivate();
2010-01-14 17:23:11 +00:00
}
},
_change: function( event ) {
2010-03-11 22:06:29 +00:00
if ( this.previous !== this.element.val() ) {
this._trigger( "change", event, { item: this.selectedItem } );
2010-01-14 17:23: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
if ( items.length && items[0].label && items[0].value ) {
2010-01-14 17:23:11 +00:00
return items;
}
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
}, item );
2010-01-14 17:23:11 +00:00
});
},
_suggest: function( items ) {
2010-03-11 22:06:29 +00:00
var ul = this.menu.element
.empty()
2010-03-11 22:06:29 +00:00
.zIndex( this.element.zIndex() + 1 ),
menuWidth,
textWidth;
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();
this.menu.refresh();
this.menu.element.show().position({
my: "left top",
at: "left bottom",
of: this.element,
collision: "none"
});
2010-03-11 22:06:29 +00:00
menuWidth = ul.width( "" ).width();
textWidth = this.element.width();
ul.width( Math.max( menuWidth, textWidth ) );
2010-01-14 17:23:11 +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
_renderItem: function( ul, item) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + item.label + "</a>" )
.appendTo( ul );
},
_move: function( direction, event ) {
if ( !this.menu.element.is(":visible") ) {
this.search( null, event );
2010-01-14 17:23:11 +00:00
return;
}
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;
}
this.menu[ direction ]( event );
2010-01-14 17:23:11 +00:00
},
widget: function() {
return this.menu.element;
2010-01-14 17:23:11 +00:00
}
});
$.extend( $.ui.autocomplete, {
escapeRegex: function( value ) {
return value.replace( /([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1" );
},
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 ));
2010-01-14 17:23:11 +00:00
/*
* jQuery UI Menu (not officially released)
*
* This widget isn't yet finished and the API is subject to change. We plan to finish
* it for the next release. You're welcome to give it a try anyway and give us feedback,
* as long as you're okay with migrating your code later on. We can help with that, too.
*
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/Menu
*
* Depends:
* jquery.ui.core.js
* jquery.ui.widget.js
*/
(function($) {
$.widget("ui.menu", {
_create: function() {
2010-01-14 17:23:11 +00:00
var self = this;
this.element
.addClass("ui-menu ui-widget ui-widget-content ui-corner-all")
.attr({
role: "listbox",
2010-01-14 17:23:11 +00:00
"aria-activedescendant": "ui-active-menuitem"
})
.click(function( event ) {
if ( !$( event.target ).closest( ".ui-menu-item a" ).length ) {
return;
}
2010-01-14 17:23:11 +00:00
// temporary
event.preventDefault();
self.select( event );
2010-01-14 17:23:11 +00:00
});
this.refresh();
},
refresh: function() {
var self = this;
// don't refresh list items that are already adapted
var items = this.element.children("li:not(.ui-menu-item):has(a)")
2010-01-14 17:23:11 +00:00
.addClass("ui-menu-item")
.attr("role", "menuitem");
items.children("a")
.addClass("ui-corner-all")
.attr("tabindex", -1)
// mouseenter doesn't work with event delegation
.mouseenter(function( event ) {
self.activate( event, $(this).parent() );
})
.mouseleave(function() {
self.deactivate();
2010-01-14 17:23:11 +00:00
});
},
activate: function( event, item ) {
2010-01-14 17:23:11 +00:00
this.deactivate();
if (this.hasScroll()) {
var offset = item.offset().top - this.element.offset().top,
scroll = this.element.attr("scrollTop"),
elementHeight = this.element.height();
if (offset < 0) {
this.element.attr("scrollTop", scroll + offset);
} else if (offset > elementHeight) {
this.element.attr("scrollTop", scroll + offset - elementHeight + item.height());
}
}
this.active = item.eq(0)
.children("a")
.addClass("ui-state-hover")
.attr("id", "ui-active-menuitem")
.end();
this._trigger("focus", event, { item: item });
2010-01-14 17:23:11 +00:00
},
deactivate: function() {
if (!this.active) { return; }
this.active.children("a")
.removeClass("ui-state-hover")
.removeAttr("id");
this._trigger("blur");
2010-01-14 17:23:11 +00:00
this.active = null;
},
next: function(event) {
this.move("next", ".ui-menu-item:first", event);
2010-01-14 17:23:11 +00:00
},
previous: function(event) {
this.move("prev", ".ui-menu-item:last", event);
2010-01-14 17:23:11 +00:00
},
first: function() {
return this.active && !this.active.prev().length;
},
last: function() {
return this.active && !this.active.next().length;
},
move: function(direction, edge, event) {
2010-01-14 17:23:11 +00:00
if (!this.active) {
this.activate(event, this.element.children(edge));
2010-01-14 17:23:11 +00:00
return;
}
var next = this.active[direction + "All"](".ui-menu-item").eq(0);
2010-01-14 17:23:11 +00:00
if (next.length) {
this.activate(event, next);
2010-01-14 17:23:11 +00:00
} else {
this.activate(event, this.element.children(edge));
2010-01-14 17:23:11 +00:00
}
},
// TODO merge with previousPage
nextPage: function(event) {
2010-01-14 17:23:11 +00:00
if (this.hasScroll()) {
// TODO merge with no-scroll-else
if (!this.active || this.last()) {
this.activate(event, this.element.children(":first"));
2010-01-14 17:23:11 +00:00
return;
}
var base = this.active.offset().top,
height = this.element.height(),
result = this.element.children("li").filter(function() {
var close = $(this).offset().top - base - height + $(this).height();
// TODO improve approximation
return close < 10 && close > -10;
});
// TODO try to catch this earlier when scrollTop indicates the last page anyway
if (!result.length) {
result = this.element.children(":last");
}
this.activate(event, result);
2010-01-14 17:23:11 +00:00
} else {
this.activate(event, this.element.children(!this.active || this.last() ? ":first" : ":last"));
2010-01-14 17:23:11 +00:00
}
},
// TODO merge with nextPage
previousPage: function(event) {
2010-01-14 17:23:11 +00:00
if (this.hasScroll()) {
// TODO merge with no-scroll-else
if (!this.active || this.first()) {
this.activate(event, this.element.children(":last"));
2010-01-14 17:23:11 +00:00
return;
}
var base = this.active.offset().top,
height = this.element.height();
result = this.element.children("li").filter(function() {
var close = $(this).offset().top - base + height - $(this).height();
// TODO improve approximation
return close < 10 && close > -10;
});
// TODO try to catch this earlier when scrollTop indicates the last page anyway
if (!result.length) {
result = this.element.children(":first");
}
this.activate(event, result);
2010-01-14 17:23:11 +00:00
} else {
this.activate(event, this.element.children(!this.active || this.first() ? ":last" : ":first"));
2010-01-14 17:23:11 +00:00
}
},
hasScroll: function() {
return this.element.height() < this.element.attr("scrollHeight");
},
select: function( event ) {
this._trigger("selected", event, { item: this.active });
2010-01-14 17:23:11 +00:00
}
});
2010-03-11 22:06:29 +00:00
}(jQuery));