mirror of
https://github.com/devbridge/jQuery-Autocomplete.git
synced 2024-11-22 21:05:12 +00:00
2 lines
7.5 KiB
JavaScript
2 lines
7.5 KiB
JavaScript
(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.currentValue=this.element.value;var n=this.getQuery(this.currentValue);if(this.selectedIndex=-1,this.ignoreValueChange){this.ignoreValueChange=!1;return}n===""||n.length<this.options.minChars?this.hide():this.getSuggestions(n)},getQuery:function(t){var r=this.options.delimiter,i;return r?(i=t.split(r),n.trim(i[i.length-1])):n.trim(t)},getSuggestionsLocal:function(t){return t=t.toLowerCase(),{suggestions:n.grep(this.options.lookup,function(n){return n.value.toLowerCase().indexOf(t)!==-1})}},getSuggestions:function(t){var r,i=this,u=i.options;r=i.isLocal?i.getSuggestionsLocal(t):i.cachedResponse[t],r&&n.isArray(r.suggestions)?(i.suggestions=r.suggestions,i.suggest()):i.isBadQuery(t)||(i.options.params.query=t,n.ajax({url:u.serviceUrl,data:u.params,type:u.type,dataType:"text"}).done(function(n){i.processResponse(n)}))},isBadQuery:function(n){for(var t=this.badQueries,i=t.length;i--;)if(n.indexOf(t[i])===0)return!0;return!1},hide:function(){this.visible=!1,this.selectedIndex=-1,n(this.suggestionsContainer).hide()},suggest:function(){if(this.suggestions.length===0){this.hide();return}for(var f=this.suggestions.length,e=this.options.formatResult,o=this.getQuery(this.currentValue),i,s=this.classes.suggestion,h=this.classes.selected,r=n(this.suggestionsContainer),u="",t=0;t<f;t++)i=this.suggestions[t],u+='<div class="'+s+'" data-index="'+t+'">'+e(i,o)+"<\/div>";r.html(u).show(),this.visible=!0,this.selectedIndex=0,r.children().first().addClass(h)},processResponse:function(t){var i=n.parseJSON(t);typeof i.suggestions[0]=="string"&&(i.suggestions=n.map(i.suggestions,function(n){return{value:n,data:null}})),this.options.noCache||(this.cachedResponse[i.query]=i,i.suggestions.length===0&&this.badQueries.push(i.query)),i.query===this.getQuery(this.currentValue)&&(this.suggestions=i.suggestions,this.suggest())},activate:function(t){var i,r=this.classes.selected,u=n(this.suggestionsContainer),f=u.children();return(u.children("."+r).removeClass(r),this.selectedIndex=t,this.selectedIndex!==-1&&f.length>this.selectedIndex)?(i=f.get(this.selectedIndex),n(i).addClass(r),i):null},select:function(n){var t=this.suggestions[n];if(t){this.el.val(t),this.ignoreValueChange=!0,this.hide();this.onSelect(n)}},change:function(t){var r,i=this,f=this.suggestions[t],u;f&&(u=i.suggestions[t],i.el.val(i.getValue(u.value)),r=i.options.onChange,n.isFunction(r)&&r(u,i.el))},moveUp:function(){if(this.selectedIndex!==-1){if(this.selectedIndex===0){n(this.suggestionsContainer).children().first().removeClass(this.classes.selected),this.selectedIndex=-1,this.el.val(this.currentValue);return}this.adjustScroll(this.selectedIndex-1)}},moveDown:function(){this.selectedIndex!==this.suggestions.length-1&&this.adjustScroll(this.selectedIndex+1)},adjustScroll:function(t){var u=this.activate(t),i,r,f,e=25;u&&(i=u.offsetTop,r=n(this.suggestionsContainer).scrollTop(),f=r+this.options.maxHeight-e,i<r?n(this.suggestionsContainer).scrollTop(i):i>f&&n(this.suggestionsContainer).scrollTop(i-this.options.maxHeight+e),this.el.val(this.getValue(this.suggestions[t].value)))},onSelect:function(t){var i=this,r=i.options.onSelect,u=i.suggestions[t];i.el.val(i.getValue(u.value)),n.isFunction(r)&&r.call(i.element,u)},getValue:function(n){var r=this,u=r.options.delimiter,t,i;return u?(t=r.currentValue,i=t.split(u),i.length===1)?n:t.substr(0,t.length-i[i.length-1].length)+n:n}},n.fn.autocomplete=function(i,r){return this.each(function(){var f="autocomplete",e=n(this),u;typeof i=="string"?(u=e.data(f),typeof u[i]=="function"&&u[i](r)):(u=new t(this,i),e.data(f,u))})}})(jQuery);
|
|
//@ sourceMappingURL=jquery.autocomplete.min.js.map
|