/** * Ajax Autocomplete for jQuery, version 1.1.3 * (c) 2010 Tomas Kirda * * Ajax Autocomplete for jQuery is freely distributable under the terms of an MIT-style license. * For details, see the web site: http://www.devbridge.com/projects/autocomplete/jquery/ * * Last Review: 04/19/2010 */ /*jslint onevar: true, evil: true, nomen: true, eqeqeq: true, bitwise: true, regexp: true, newcap: true, immed: true */ /*global window: true, document: true, clearInterval: true, setInterval: true, jQuery: true */ (function($){function fnFormatResult(a,b,c){if(c==="and"||c==="&"||c==="n'")var d="(and|&|n')";else var d="("+c.replace(reEscape,"\\$1")+")";return a.replace(new RegExp(d,"gi"),"$1")}function Autocomplete(a,b){this.el=$(a),this.el.attr("autocomplete","off"),this.suggestions=[],this.data=[],this.badQueries=[],this.selectedIndex=-1,this.currentValue=this.el.val(),this.intervalId=0,this.cachedResponse=[],this.onChangeInterval=null,this.ignoreValueChange=!1,this.serviceUrl=b.serviceUrl,this.isLocal=!1,this.containerId=b.containerId,this.containerItemsId=b.containerItemsId,this.options={autoSubmit:!1,minChars:1,maxHeight:300,deferRequestBy:0,width:0,highlight:!0,params:{},fnFormatResult:fnFormatResult,delimiter:null,zIndex:9999},this.initialize(),this.setOptions(b)}var reEscape=new RegExp("(\\"+["/",".","*","+","?","|","(",")","[","]","{","}","\\"].join("|\\")+")","g");$.fn.bh_autocomplete=function(a){return new Autocomplete(this.get(0)||$(""),a)},Autocomplete.prototype={killerFn:null,initialize:function(){var a,b,c;a=this,b=Math.floor(Math.random()*1048576).toString(16),c="Autocomplete_"+b,this.killerFn=function(b){$(b.target).parents(".autocomplete").size()===0&&(a.killSuggestions(),a.disableKillerFn())},this.options.width||(this.options.width=this.el.width()),this.mainContainerId="AutocompleteContainter_"+b,this.mainContainerId=this.containerId,this.container=$("#"+c),this.container_items=$("#"+this.containerItemsId),this.fixPosition(),window.opera?this.el.keypress(function(b){a.onKeyPress(b)}):this.el.keydown(function(b){a.onKeyPress(b)}),this.el.keyup(function(b){a.onKeyUp(b)}),this.el.blur(function(){a.enableKillerFn()}),this.el.focus(function(){a.fixPosition()})},setOptions:function(a){var b=this.options;$.extend(b,a),b.lookup&&(this.isLocal=!0,$.isArray(b.lookup)&&(b.lookup={suggestions:b.lookup,data:[]}))},clearCache:function(){this.cachedResponse=[],this.badQueries=[]},disable:function(){this.disabled=!0},enable:function(){this.disabled=!1},fixPosition:function(){var a=this.el.offset()},enableKillerFn:function(){var a=this;$(document).bind("click",a.killerFn)},disableKillerFn:function(){var a=this;$(document).unbind("click",a.killerFn)},killSuggestions:function(){var a=this;this.stopKillSuggestions(),this.intervalId=window.setInterval(function(){a.hide(),a.stopKillSuggestions()},300)},stopKillSuggestions:function(){window.clearInterval(this.intervalId)},onKeyPress:function(a){if(this.disabled||!this.enabled)return;switch(a.keyCode){case 27:this.el.val(this.currentValue),this.hide();break;case 9:case 13:if(this.selectedIndex===-1){this.hide();return}this.select(this.selectedIndex);if(a.keyCode===9)return;break;case 38:this.moveUp();break;case 40:this.moveDown();break;default:return}a.stopImmediatePropagation(),a.preventDefault()},onKeyUp:function(a){if(this.disabled)return;switch(a.keyCode){case 38:case 40:return}clearInterval(this.onChangeInterval);if(this.currentValue!==this.el.val())if(this.options.deferRequestBy>0){var b=this;this.onChangeInterval=setInterval(function(){b.onValueChange()},this.options.deferRequestBy)}else this.onValueChange()},onValueChange:function(){clearInterval(this.onChangeInterval),this.currentValue=this.el.val();var a=this.getQuery(this.currentValue);this.selectedIndex=-1;if(this.ignoreValueChange){this.ignoreValueChange=!1;return}a===""||a.length'+d(g,this.data[f],e)+""),c.mouseover(h(f)),c.click(i(f)),this.container_items.append(c);this.enabled=!0,this.container.show(),$(".popover-wrapper").show()},processResponse:function(text){var response;try{response=eval("("+text+")")}catch(err){return}$.isArray(response.data)||(response.data=[]),this.options.noCache||(this.cachedResponse[response.query]=response,response.suggestions.length===0&&this.badQueries.push(response.query)),response.query===this.getQuery(this.currentValue)&&(this.suggestions=response.suggestions,this.data=response.data,this.suggest())},activate:function(a){var b,c;return b=this.container_items.children(),this.selectedIndex!==-1&&b.length>this.selectedIndex&&$(b.get(this.selectedIndex)).removeClass(),this.selectedIndex=a,this.selectedIndex!==-1&&b.length>this.selectedIndex&&(c=b.get(this.selectedIndex),$(c).addClass("selected")),c},deactivate:function(a,b){a.className="",this.selectedIndex===b&&(this.selectedIndex=-1)},select:function(a){var b,c;b=this.suggestions[a],b&&(this.el.val(b),this.options.autoSubmit&&(c=this.el.parents("form"),c.length>0&&c.get(0).submit()),this.ignoreValueChange=!0,this.hide(),this.onSelect(a))},moveUp:function(){if(this.selectedIndex===-1)return;if(this.selectedIndex===0){this.container_items.children().get(0).className="",this.selectedIndex=-1,this.el.val(this.currentValue);return}this.adjustScroll(this.selectedIndex-1)},moveDown:function(){if(this.selectedIndex===this.suggestions.length-1)return;this.adjustScroll(this.selectedIndex+1)},adjustScroll:function(a){var b,c,d,e;b=this.activate(a),c=b.offsetTop,d=this.container_items.scrollTop(),e=d+this.options.maxHeight-25,ce&&this.container_items.scrollTop(c-this.options.maxHeight+25),this.el.val(this.getValue(this.suggestions[a]))},onSelect:function(a){var b,c,d,e;b=this,c=b.options.onSelect,d=b.suggestions[a],e=b.data[a],b.el.val(b.getValue(d)),$.isFunction(c)&&c(d,e,b.el)},getValue:function(a){var b,c,d,e;return e=this,b=e.options.delimiter,b?(c=e.currentValue,d=c.split(b),d.length===1?a:c.substr(0,c.length-d[d.length-1].length)+a):a}}})(jQuery)