//extend ui auto-complete to include categories and errors
$.widget( "custom.catcomplete", $.ui.autocomplete, {
_renderMenu: function( ul, items ) {
var self = this, currentCategory = "";
$.each( items, function( index, item ) {
if ( item.category != undefined && item.category != currentCategory ) {
ul.append( "
" + item.category + "" );
currentCategory = item.category;
}
if ( item.error != undefined ) {
ul.append( "" + item.error + "" );
}
self._renderItemData( ul, item );
});
},
_renderItem: function( ul, item ) {
return $( "" )
.append( " " + item.completed + "" + item.part + "" )
.appendTo( ul );
}
});
$.fn.scopedSearch = function(){
var options = arguments[0] || {};
$(this).each(function(i,el){
var target = $(el);
var clearFn = options.onClear || function(){ target.val(''); };
target.catcomplete({
source: options.source || function( request, response ) {
$.getJSON( target.data("url"), { search: request.term }, response );
},
minLength: options.minLength || 0,
delay: options.delay || 100,
select: function(event, ui) {
target.val( ui.item.value );
},
search: function(event, ui) {
$(".autocomplete-clear").hide();
},
response: function(event, ui) {
$(".autocomplete-clear").show();
},
close: function(event, ui) {
$(".autocomplete-input:focus").catcomplete( target.attr('id'));
}
});
target.bind("focus", function( event ){
$(this).catcomplete( target.attr('id'));
});
target.after('×')
target.next().on("click", clearFn)
})
};