/** * jquery.autocomplete.js * Copyright (c) Dylan Verheul * MIT license * http://code.google.com/p/jquery-autocomplete/ */ (function(a){a.Autocompleter=function(b,c){this.cacheData_={},this.cacheLength_=0,this.selectClass_="jquery-autocomplete-selected-item",this.keyTimeout_=null,this.lastKeyPressed_=null,this.lastProcessedValue_=null,this.lastSelectedValue_=null,this.active_=!1,this.finishOnBlur_=!0;if(!!b&&b instanceof jQuery&&b.length===1&&b.get(0).tagName.toUpperCase()==="INPUT"){typeof c=="string"?this.options={url:c}:this.options=c,this.options.maxCacheLength=parseInt(this.options.maxCacheLength,10);if(isNaN(this.options.maxCacheLength)||this.options.maxCacheLength<1)this.options.maxCacheLength=1;this.options.minChars=parseInt(this.options.minChars,10);if(isNaN(this.options.minChars)||this.options.minChars<1)this.options.minChars=1;this.dom={},this.dom.$elem=b,this.options.inputClass&&this.dom.$elem.addClass(this.options.inputClass),this.dom.$results=a("
").hide(),this.options.resultsClass&&this.dom.$results.addClass(this.options.resultsClass),this.dom.$results.css({position:"absolute"}),a("body").append(this.dom.$results);var d=this;b.keydown(function(a){d.lastKeyPressed_=a.keyCode;switch(d.lastKeyPressed_){case 38:a.preventDefault(),d.active_?d.focusPrev():d.activate();return!1;case 40:a.preventDefault(),d.active_?d.focusNext():d.activate();return!1;case 9:case 13:if(d.active_){a.preventDefault(),d.selectCurrent();return!1}break;case 27:if(d.active_){a.preventDefault(),d.finish();return!1}break;default:d.activate()}}),b.blur(function(){d.finishOnBlur_&&setTimeout(function(){d.finish()},200)})}else alert("Invalid parameter for jquery.Autocompleter, jQuery object with one element with INPUT tag expected")},a.Autocompleter.prototype.position=function(){var a=this.dom.$elem.offset();this.dom.$results.css({top:a.top+this.dom.$elem.outerHeight(),left:a.left})},a.Autocompleter.prototype.cacheRead=function(a){var b,c,d,e,f;if(this.options.useCache){a=String(a),b=a.length,this.options.matchSubset?c=1:c=b;while(c<=b){this.options.matchInside?e=b-c:e=0,f=0;while(f<=e){d=a.substr(0,c);if(this.cacheData_[d]!==undefined)return this.cacheData_[d];f++}c++}}return!1},a.Autocompleter.prototype.cacheWrite=function(a,b){if(this.options.useCache){this.cacheLength_>=this.options.maxCacheLength&&this.cacheFlush(),a=String(a),this.cacheData_[a]!==undefined&&this.cacheLength_++;return this.cacheData_[a]=b}return!1},a.Autocompleter.prototype.cacheFlush=function(){this.cacheData_={},this.cacheLength_=0},a.Autocompleter.prototype.callHook=function(b,c){var d=this.options[b];return d&&a.isFunction(d)?d(c,this):!1},a.Autocompleter.prototype.activate=function(){var a=this,b=function(){a.activateNow()},c=parseInt(this.options.delay,10);if(isNaN(c)||c<=0)c=250;this.keyTimeout_&&clearTimeout(this.keyTimeout_),this.keyTimeout_=setTimeout(b,c)},a.Autocompleter.prototype.activateNow=function(){var a=this.dom.$elem.val();a!==this.lastProcessedValue_&&a!==this.lastSelectedValue_&&a.length>=this.options.minChars&&(this.active_=!0,this.lastProcessedValue_=a,this.fetchData(a))},a.Autocompleter.prototype.fetchData=function(a){if(this.options.data)this.filterAndShowResults(this.options.data,a);else{var b=this;this.fetchRemoteData(a,function(c){b.filterAndShowResults(c,a)})}},a.Autocompleter.prototype.fetchRemoteData=function(b,c){var d=this.cacheRead(b);if(d)c(d);else{var e=this;this.dom.$elem.addClass(this.options.loadingClass);var f=function(a){var d=!1;a!==!1&&(d=e.parseRemoteData(a),e.cacheWrite(b,d)),e.dom.$elem.removeClass(e.options.loadingClass),c(d)};a.ajax({url:this.makeUrl(b),success:f,error:function(){f(!1)}})}},a.Autocompleter.prototype.setExtraParam=function(b,c){var d=a.trim(String(b));d&&(this.options.extraParams||(this.options.extraParams={}),this.options.extraParams[d]!==c&&(this.options.extraParams[d]=c,this.cacheFlush()))},a.Autocompleter.prototype.makeUrl=function(b){var c=this,d=this.options.paramName||"q",e=this.options.url,f=a.extend({},this.options.extraParams);this.options.paramName===!1?e+=encodeURIComponent(b):f[d]=b;var g=[];a.each(f,function(a,b){g.push(c.makeUrlParam(a,b))}),g.length&&(e+=e.indexOf("?")==-1?"?":"&",e+=g.join("&"));return e},a.Autocompleter.prototype.makeUrlParam=function(a,b){return String(a)+"="+encodeURIComponent(b)},a.Autocompleter.prototype.parseRemoteData=function(a){var b=[],c=String(a).replace("\r\n","\n"),d,e,f,g,h=c.split("\n"),i;for(d=0;d""&&(typeof f!="object"&&(f={}),j=!this.options.filterResults,j||(l=String(c),l=l.replace(n,"\\$&"),this.options.matchInside||(l="^"+l),this.options.matchCase||(m="i"),k=new RegExp(l,m),j=k.test(e)),j&&d.push({value:e,data:f}));this.options.sortResults&&(d=this.sortResults(d,c)),this.options.maxItemsToShow>0&&this.options.maxItemsToShowb?1:a"),f,g,h,i,j=!1,k=!1,l=b.length;for(f=0;f"+this.showResult(g.value,g.data)+""),h.data("value",g.value),h.data("data",g.data),h.click(function(){var b=a(this);d.selectItem(b)}).mousedown(function(){d.finishOnBlur_=!1}).mouseup(function(){d.finishOnBlur_=!0}),e.append(h),j===!1&&(j=String(g.value),k=h,h.addClass(this.options.firstItemClass)),f==l-1&&h.addClass(this.options.lastItemClass);this.position(),this.dom.$results.html(e).show(),i=this.dom.$results.outerWidth()-this.dom.$results.width(),this.dom.$results.width(this.dom.$elem.outerWidth()-i),a("li",this.dom.$results).hover(function(){d.focusItem(this)},function(){}),this.autoFill(j,c)&&this.focusItem(k)},a.Autocompleter.prototype.showResult=function(b,c){return a.isFunction(this.options.showResult)?this.options.showResult(b,c):b},a.Autocompleter.prototype.autoFill=function(a,b){var c,d,e,f;if(this.options.autoFill&&this.lastKeyPressed_!=8){c=String(a).toLowerCase(),d=String(b).toLowerCase(),e=a.length,f=b.length;if(c.substr(0,f)===d){this.dom.$elem.val(a),this.selectRange(f,e);return!0}}return!1},a.Autocompleter.prototype.focusNext=function(){this.focusMove(1)},a.Autocompleter.prototype.focusPrev=function(){this.focusMove(-1)},a.Autocompleter.prototype.focusMove=function(b){var c,d=a("li",this.dom.$results);b=parseInt(b,10);for(var c=0;c=d.length&&(b=d.length-1),c=a(d[b])):c=a(b),c&&c.addClass(this.selectClass_).addClass(this.options.selectClass))},a.Autocompleter.prototype.selectCurrent=function(){var b=a("li."+this.selectClass_,this.dom.$results);b.length==1?this.selectItem(b):this.finish()},a.Autocompleter.prototype.selectItem=function(a){var b=a.data("value"),c=a.data("data"),d=this.displayValue(b,c);this.lastProcessedValue_=d,this.lastSelectedValue_=d,this.dom.$elem.val(d).focus(),this.setCaret(d.length),this.callHook("onItemSelect",{value:b,data:c}),this.finish()},a.Autocompleter.prototype.displayValue=function(b,c){return a.isFunction(this.options.displayValue)?this.options.displayValue(b,c):b},a.Autocompleter.prototype.finish=function(){this.keyTimeout_&&clearTimeout(this.keyTimeout_),this.dom.$elem.val()!==this.lastSelectedValue_&&(this.options.mustMatch&&this.dom.$elem.val(""),this.callHook("onNoMatch")),this.dom.$results.hide(),this.lastKeyPressed_=null,this.lastProcessedValue_=null,this.active_&&this.callHook("onFinish"),this.active_=!1},a.Autocompleter.prototype.selectRange=function(a,b){var c=this.dom.$elem.get(0);if(c.setSelectionRange)c.focus(),c.setSelectionRange(a,b);else if(this.createTextRange){var d=this.createTextRange();d.collapse(!0),d.moveEnd("character",b),d.moveStart("character",a),d.select()}},a.Autocompleter.prototype.setCaret=function(a){this.selectRange(a,a)},a.fn.autocomplete=function(b){typeof b=="string"&&(b={url:b});var c=a.extend({},a.fn.autocomplete.defaults,b);return this.each(function(){var b=a(this),d=new a.Autocompleter(b,c);b.data("autocompleter",d)})},a.fn.autocomplete.defaults={paramName:"q",minChars:1,loadingClass:"acLoading",resultsClass:"acResults",inputClass:"acInput",selectClass:"acSelect",mustMatch:!1,matchCase:!1,matchInside:!0,matchSubset:!0,useCache:!0,maxCacheLength:10,autoFill:!1,filterResults:!0,sortResults:!0,sortFunction:!1,onItemSelect:!1,onNoMatch:!1,maxItemsToShow:-1}})(jQuery)