2
0
mirror of https://github.com/devbridge/jQuery-Autocomplete.git synced 2024-12-30 13:30:32 +00:00
jQuery-Autocomplete/dist/jquery.autocomplete.min.js

2 lines
7.5 KiB
JavaScript
Raw Normal View History

(function(n){"use strict";function t(i,r){var u=this,f={minChars:1,maxHeight:300,deferRequestBy:0,width:0,highlight:!0,params:{},formatResult:t.formatResult,delimiter:null,zIndex:9999,type:"GET",noCache:!1,enforce:!1};u.element=i,u.el=n(i),u.suggestions=[],u.badQueries=[],u.selectedIndex=-1,u.currentValue=u.element.value,u.intervalId=0,u.cachedResponse=[],u.onChangeInterval=null,u.onChange=null,u.ignoreValueChange=!1,u.isLocal=!1,u.suggestionsContainer=null,u.options=f,u.classes={selected:"autocomplete-selected",suggestion:"autocomplete-suggestion"},u.initialize(),u.setOptions(r)}var i=function(){return{extend:function(t,i){return n.extend(t,i)},addEvent:function(n,t,i){if(n.addEventListener)n.addEventListener(t,i,!1);else if(n.attachEvent)n.attachEvent("on"+t,i);else throw new Error("Browser doesn't support addEventListener or attachEvent");},removeEvent:function(n,t,i){n.removeEventListener?n.removeEventListener(t,i,!1):n.detachEvent&&n.detachEvent("on"+t,i)},createNode:function(n){var t=document.createElement("div");return t.innerHTML=n,t.firstChild}}}();t.utils=i,n.Autocomplete=t,t.formatResult=function(n,t){var i=new RegExp("(\\/|\\.|\\*|\\+|\\?|\\||\\(|\\)|\\[|\\]|\\{|\\}|\\\\)","g"),r="("+t.replace(i,"\\$1")+")";return n.value.replace(new RegExp(r,"gi"),"<strong>$1<\/strong>")},t.prototype={killerFn:null,initialize:function(){var i=this,u="."+i.classes.suggestion,r;this.element.setAttribute("autocomplete","off"),this.killerFn=function(t){n(t.target).closest(".autocomplete").length===0&&(i.killSuggestions(),i.disableKillerFn())},this.options.width&&this.options.width!=="auto"||(this.options.width=this.el.outerWidth()),this.suggestionsContainer=t.utils.createNode('<div class="autocomplete" style="position: absolute; display: none;"><\/div>'),r=n(this.suggestionsContainer),r.appendTo("body").width(this.options.width);r.on("mouseover",u,function(){i.activate(n(this).data("index"))});r.on("click",u,function(){i.select(n(this).data("index"))});if(this.fixPosition(),window.opera)this.el.on("keypress",function(n){i.onKeyPress(n)});else this.el.on("keydown",function(n){i.onKeyPress(n)});this.el.on("keyup",function(n){i.onKeyUp(n)});this.el.on("blur",function(){i.onBlur()});this.el.on("focus",function(){i.fixPosition()})},onBlur:function(){this.enableKillerFn()},setOptions:function(t){var r=this.options;i.extend(r,t),this.isLocal=n.isArray(r.lookup),this.isLocal&&typeof r.lookup[0]=="string"&&(r.lookup=n.map(r.lookup,function(n){return{value:n,data:null}})),n(this.suggestionsContainer).css({"max-height":r.maxHeight+"px",width:r.width,"z-index":r.zIndex})},clearCache:function(){this.cachedResponse=[],this.badQueries=[]},disable:function(){this.disabled=!0},enable:function(){this.disabled=!1},fixPosition:function(){var t=this.el.offset();n(this.suggestionsContainer).css({top:t.top+this.el.outerHeight()+"px",left:t.left+"px"})},enableKillerFn:function(){var t=this;n(document).on("click",t.killerFn)},disableKillerFn:function(){var t=this;n(document).off("click",t.killerFn)},killSuggestions:function(){var n=this;n.stopKillSuggestions(),n.intervalId=window.setInterval(function(){n.hide(),n.stopKillSuggestions()},300)},stopKillSuggestions:function(){window.clearInterval(this.intervalId)},onKeyPress:function(n){if(!this.disabled&&!this.visible&&n.keyCode===40&&this.currentValue){this.suggest();return}if(!this.disabled&&this.visible){switch(n.keyCode){case 27:this.el.val(this.currentValue),this.hide();break;case 9:case 13:if(this.selectedIndex===-1){this.hide();return}if(this.select(this.selectedIndex),n.keyCode===9)return;break;case 38:this.moveUp();break;case 40:this.moveDown();break;default:return}n.stopImmediatePropagation(),n.preventDefault()}},onKeyUp:function(n){if(!this.disabled){switch(n.keyCode){case 38:case 40:return}if(clearInterval(this.onChangeInterval),this.currentValue!==this.el.val())if(this.options.deferRequestBy>0){var t=this;this.onChangeInterval=setInterval(function(){t.onValueChange()},this.options.deferRequestBy)}else this.onValueChange()}},onValueChange:function(){clearInterval(this.onChangeInterval),this.cu
//@ sourceMappingURL=jquery.autocomplete.min.js.map