diff --git a/dist/jquery.autocomplete.js b/dist/jquery.autocomplete.js index fac6b9d..1bfca5e 100644 --- a/dist/jquery.autocomplete.js +++ b/dist/jquery.autocomplete.js @@ -8,7 +8,7 @@ */ /*jslint browser: true, white: true, plusplus: true */ -/*global define, window, document, jQuery */ +/*global define, window, document, jQuery, exports */ // Expose plugin as an AMD module if AMD loader is present: (function (factory) { @@ -16,6 +16,9 @@ if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. define(['jquery'], factory); + } else if (typeof exports === 'object' && typeof require === 'function') { + // Browserify + factory(require('jquery')); } else { // Browser globals factory(jQuery); @@ -138,8 +141,7 @@ suggestionSelector = '.' + that.classes.suggestion, selected = that.classes.selected, options = that.options, - container, - noSuggestionsContainer; + container; // Remove autocomplete attribute to prevent native suggestions: that.element.setAttribute('autocomplete', 'off'); @@ -276,34 +278,37 @@ if (orientation == 'auto') { var viewPortHeight = $(window).height(), scrollTop = $(window).scrollTop(), - top_overflow = -scrollTop + offset.top - containerHeight, - bottom_overflow = scrollTop + viewPortHeight - (offset.top + height + containerHeight); + topOverflow = -scrollTop + offset.top - containerHeight, + bottomOverflow = scrollTop + viewPortHeight - (offset.top + height + containerHeight); - if (Math.max(top_overflow, bottom_overflow) === top_overflow) - orientation = 'top'; - else - orientation = 'bottom'; + orientation = (Math.max(topOverflow, bottomOverflow) === topOverflow) + ? 'top' + : 'bottom'; } - if (orientation === 'top') + if (orientation === 'top') { styles.top += -containerHeight; - else + } else { styles.top += height; + } // If container is not positioned to body, // correct its position using offset parent offset if(containerParent !== document.body) { var opacity = $container.css('opacity'), parentOffsetDiff; - if (!that.visible) - $container.css('opacity', 0).show(); + + if (!that.visible){ + $container.css('opacity', 0).show(); + } parentOffsetDiff = $container.offsetParent().offset(); styles.top -= parentOffsetDiff.top; styles.left -= parentOffsetDiff.left; - if (!that.visible) + if (!that.visible){ $container.css('opacity', opacity).hide(); + } } // -2px to account for suggestions border. @@ -444,7 +449,7 @@ query = that.getQuery(value), index; - if (that.selection) { + if (that.selection && that.currentValue !== query) { that.selection = null; (options.onInvalidateSelection || $.noop).call(that.element); } @@ -588,7 +593,14 @@ }, hide: function () { - var that = this; + var that = this, + container = $(that.suggestionsContainer); + + if ($.isFunction(this.options.onHide) && that.visible) { + + this.options.onHide.call(that.element, container); + } + that.visible = false; that.selectedIndex = -1; $(that.suggestionsContainer).hide(); @@ -597,7 +609,7 @@ suggest: function () { if (this.suggestions.length === 0) { - this.options.showNoSuggestionNotice ? this.noSuggestions() : this.hide(); + this.options.showNoSuggestionNotice ? this.noSuggestions() : this.hide(); return; } @@ -611,8 +623,7 @@ noSuggestionsContainer = $(that.noSuggestionsContainer), beforeRender = options.beforeRender, html = '', - index, - width; + index; if (options.triggerSelectOnValidInput) { index = that.findSuggestionIndex(value); @@ -627,7 +638,7 @@ html += '
' + formatResult(suggestion, value) + '
'; }); - this.adjustContainerWidth(); + this.adjustContainerWidth(); noSuggestionsContainer.detach(); container.html(html); @@ -731,10 +742,12 @@ validateOrientation: function(orientation, fallback) { orientation = $.trim(orientation || '').toLowerCase(); + if($.inArray(orientation, ['auto', 'bottom', 'top']) === -1){ orientation = fallback; } - return orientation + + return orientation; }, processResponse: function (result, originalQuery, cacheKey) { @@ -897,7 +910,7 @@ }; // Create chainable jQuery plugin: - $.fn.autocomplete = function (options, args) { + $.fn.autocomplete = $.fn.devbridgeAutocomplete = function (options, args) { var dataKey = 'autocomplete'; // If function invoked without argument return // instance of the first matched element: diff --git a/dist/jquery.autocomplete.min.js b/dist/jquery.autocomplete.min.js index f424d3c..c5371d3 100644 --- a/dist/jquery.autocomplete.min.js +++ b/dist/jquery.autocomplete.min.js @@ -6,26 +6,48 @@ * For details, see the web site: https://github.com/devbridge/jQuery-Autocomplete * */ -(function(d){"function"===typeof define&&define.amd?define(["jquery"],d):d(jQuery)})(function(d){function k(a,b){var c=function(){},c={autoSelectFirst:!1,appendTo:document.body,serviceUrl:null,lookup:null,onSelect:null,width:"auto",minChars:1,maxHeight:300,deferRequestBy:0,params:{},formatResult:k.formatResult,delimiter:null,zIndex:9999,type:"GET",noCache:!1,onSearchStart:c,onSearchComplete:c,onSearchError:c,containerClass:"autocomplete-suggestions",tabDisabled:!1,dataType:"text",currentRequest:null, -triggerSelectOnValidInput:!0,preventBadQueries:!0,lookupFilter:function(a,b,c){return-1!==a.value.toLowerCase().indexOf(c)},paramName:"query",transformResult:function(a){return"string"===typeof a?d.parseJSON(a):a},showNoSuggestionNotice:!1,noSuggestionNotice:"No results",orientation:"bottom",forceFixPosition:!1};this.element=a;this.el=d(a);this.suggestions=[];this.badQueries=[];this.selectedIndex=-1;this.currentValue=this.element.value;this.intervalId=0;this.cachedResponse={};this.onChange=this.onChangeInterval= -null;this.isLocal=!1;this.noSuggestionsContainer=this.suggestionsContainer=null;this.options=d.extend({},c,b);this.classes={selected:"autocomplete-selected",suggestion:"autocomplete-suggestion"};this.hint=null;this.hintValue="";this.selection=null;this.initialize();this.setOptions(b)}var n=function(){return{escapeRegExChars:function(a){return a.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&")},createNode:function(a){var b=document.createElement("div");b.className=a;b.style.position="absolute"; -b.style.display="none";return b}}}();k.utils=n;d.Autocomplete=k;k.formatResult=function(a,b){var c="("+n.escapeRegExChars(b)+")";return a.value.replace(new RegExp(c,"gi"),"$1")};k.prototype={killerFn:null,initialize:function(){var a=this,b="."+a.classes.suggestion,c=a.classes.selected,e=a.options,f;a.element.setAttribute("autocomplete","off");a.killerFn=function(b){0===d(b.target).closest("."+a.options.containerClass).length&&(a.killSuggestions(),a.disableKillerFn())};a.noSuggestionsContainer= -d('
').html(this.options.noSuggestionNotice).get(0);a.suggestionsContainer=k.utils.createNode(e.containerClass);f=d(a.suggestionsContainer);f.appendTo(e.appendTo);"auto"!==e.width&&f.width(e.width);f.on("mouseover.autocomplete",b,function(){a.activate(d(this).data("index"))});f.on("mouseout.autocomplete",function(){a.selectedIndex=-1;f.children("."+c).removeClass(c)});f.on("click.autocomplete",b,function(){a.select(d(this).data("index"))});a.fixPositionCapture= -function(){a.visible&&a.fixPosition()};d(window).on("resize.autocomplete",a.fixPositionCapture);a.el.on("keydown.autocomplete",function(b){a.onKeyPress(b)});a.el.on("keyup.autocomplete",function(b){a.onKeyUp(b)});a.el.on("blur.autocomplete",function(){a.onBlur()});a.el.on("focus.autocomplete",function(){a.onFocus()});a.el.on("change.autocomplete",function(b){a.onKeyUp(b)})},onFocus:function(){this.fixPosition();if(this.options.minChars<=this.el.val().length)this.onValueChange()},onBlur:function(){this.enableKillerFn()}, -setOptions:function(a){var b=this.options;d.extend(b,a);if(this.isLocal=d.isArray(b.lookup))b.lookup=this.verifySuggestionsFormat(b.lookup);b.orientation=this.validateOrientation(b.orientation,"bottom");d(this.suggestionsContainer).css({"max-height":b.maxHeight+"px",width:b.width+"px","z-index":b.zIndex})},clearCache:function(){this.cachedResponse={};this.badQueries=[]},clear:function(){this.clearCache();this.currentValue="";this.suggestions=[]},disable:function(){this.disabled=!0;this.currentRequest&& -this.currentRequest.abort()},enable:function(){this.disabled=!1},fixPosition:function(){var a=d(this.suggestionsContainer),b=a.parent().get(0);if(b===document.body||this.options.forceFixPosition){var c=this.options.orientation,e=a.outerHeight(),f=this.el.outerHeight(),h=this.el.offset(),g={top:h.top,left:h.left};if("auto"==c)var c=d(window).height(),p=d(window).scrollTop(),l=-p+h.top-e,c=Math.max(l,p+c-(h.top+f+e))===l?"top":"bottom";g.top="top"===c?g.top+-e:g.top+f;b!==document.body&&(b=a.css("opacity"), -this.visible||a.css("opacity",0).show(),e=a.offsetParent().offset(),g.top-=e.top,g.left-=e.left,this.visible||a.css("opacity",b).hide());"auto"===this.options.width&&(g.width=this.el.outerWidth()-2+"px");a.css(g)}},enableKillerFn:function(){d(document).on("click.autocomplete",this.killerFn)},disableKillerFn:function(){d(document).off("click.autocomplete",this.killerFn)},killSuggestions:function(){var a=this;a.stopKillSuggestions();a.intervalId=window.setInterval(function(){a.hide();a.stopKillSuggestions()}, -50)},stopKillSuggestions:function(){window.clearInterval(this.intervalId)},isCursorAtEnd:function(){var a=this.el.val().length,b=this.element.selectionStart;return"number"===typeof b?b===a:document.selection?(b=document.selection.createRange(),b.moveStart("character",-a),a===b.text.length):!0},onKeyPress:function(a){if(!this.disabled&&!this.visible&&40===a.which&&this.currentValue)this.suggest();else if(!this.disabled&&this.visible){switch(a.which){case 27:this.el.val(this.currentValue);this.hide(); -break;case 39:if(this.hint&&this.options.onHint&&this.isCursorAtEnd()){this.selectHint();break}return;case 9:if(this.hint&&this.options.onHint){this.selectHint();return}case 13:if(-1===this.selectedIndex){this.hide();return}this.select(this.selectedIndex);if(9===a.which&&!1===this.options.tabDisabled)return;break;case 38:this.moveUp();break;case 40:this.moveDown();break;default:return}a.stopImmediatePropagation();a.preventDefault()}},onKeyUp:function(a){var b=this;if(!b.disabled){switch(a.which){case 38:case 40:return}clearInterval(b.onChangeInterval); -if(b.currentValue!==b.el.val())if(b.findBestHint(),0f&&(b.suggestions=b.suggestions.slice(0,f));return b},getSuggestions:function(a){var b,c=this,e=c.options,f=e.serviceUrl,h,g;e.params[e.paramName]=a;h=e.ignoreParams?null:e.params;c.isLocal?b=c.getSuggestionsLocal(a):(d.isFunction(f)&&(f=f.call(c.element,a)),g=f+"?"+d.param(h||{}),b=c.cachedResponse[g]);b&&d.isArray(b.suggestions)?(c.suggestions=b.suggestions,c.suggest()):c.isBadQuery(a)||!1===e.onSearchStart.call(c.element,e.params)||(c.currentRequest&&c.currentRequest.abort(), -c.currentRequest=d.ajax({url:f,data:h,type:e.type,dataType:e.dataType}).done(function(b){c.currentRequest=null;b=e.transformResult(b);c.processResponse(b,a,g);e.onSearchComplete.call(c.element,a,b.suggestions)}).fail(function(b,d,f){e.onSearchError.call(c.element,a,b,d,f)}))},isBadQuery:function(a){if(!this.options.preventBadQueries)return!1;for(var b=this.badQueries,c=b.length;c--;)if(0===a.indexOf(b[c]))return!0;return!1},hide:function(){this.visible=!1;this.selectedIndex=-1;d(this.suggestionsContainer).hide(); -this.signalHint(null)},suggest:function(){if(0===this.suggestions.length)this.options.showNoSuggestionNotice?this.noSuggestions():this.hide();else{var a=this.options,b=a.formatResult,c=this.getQuery(this.currentValue),e=this.classes.suggestion,f=this.classes.selected,h=d(this.suggestionsContainer),g=d(this.noSuggestionsContainer),k=a.beforeRender,l="",m;if(a.triggerSelectOnValidInput&&(m=this.findSuggestionIndex(c),-1!==m)){this.select(m);return}d.each(this.suggestions,function(a,d){l+='
'+b(d,c)+"
"});this.adjustContainerWidth();g.detach();h.html(l);a.autoSelectFirst&&(this.selectedIndex=0,h.children().first().addClass(f));d.isFunction(k)&&k.call(this.element,h);this.fixPosition();h.show();this.visible=!0;this.findBestHint()}},noSuggestions:function(){var a=d(this.suggestionsContainer),b=d(this.noSuggestionsContainer);this.adjustContainerWidth();b.detach();a.empty();a.append(b);this.fixPosition();a.show();this.visible=!0},adjustContainerWidth:function(){var a= -this.options,b=d(this.suggestionsContainer);"auto"===a.width&&(a=this.el.outerWidth()-2,b.width(0this.selectedIndex?(a=e.get(this.selectedIndex),d(a).addClass(b),a):null},selectHint:function(){var a=d.inArray(this.hint,this.suggestions);this.select(a)},select:function(a){this.hide();this.onSelect(a)},moveUp:function(){-1!==this.selectedIndex&&(0===this.selectedIndex?(d(this.suggestionsContainer).children().first().removeClass(this.classes.selected), -this.selectedIndex=-1,this.el.val(this.currentValue),this.findBestHint()):this.adjustScroll(this.selectedIndex-1))},moveDown:function(){this.selectedIndex!==this.suggestions.length-1&&this.adjustScroll(this.selectedIndex+1)},adjustScroll:function(a){var b=this.activate(a),c,e;b&&(b=b.offsetTop,c=d(this.suggestionsContainer).scrollTop(),e=c+this.options.maxHeight-25,be&&d(this.suggestionsContainer).scrollTop(b-this.options.maxHeight+25),this.el.val(this.getValue(this.suggestions[a].value)), -this.signalHint(null))},onSelect:function(a){var b=this.options.onSelect;a=this.suggestions[a];this.currentValue=this.getValue(a.value);this.currentValue!==this.el.val()&&this.el.val(this.currentValue);this.signalHint(null);this.suggestions=[];this.selection=a;d.isFunction(b)&&b.call(this.element,a)},getValue:function(a){var b=this.options.delimiter,c;if(!b)return a;c=this.currentValue;b=c.split(b);return 1===b.length?a:c.substr(0,c.length-b[b.length-1].length)+a},dispose:function(){this.el.off(".autocomplete").removeData("autocomplete"); -this.disableKillerFn();d(window).off("resize.autocomplete",this.fixPositionCapture);d(this.suggestionsContainer).remove()}};d.fn.autocomplete=function(a,b){return 0===arguments.length?this.first().data("autocomplete"):this.each(function(){var c=d(this),e=c.data("autocomplete");if("string"===typeof a){if(e&&"function"===typeof e[a])e[a](b)}else e&&e.dispose&&e.dispose(),e=new k(this,a),c.data("autocomplete",e)})}}); \ No newline at end of file +(function(factory){"use strict";if(typeof define==="function"&&define.amd){define(["jquery"],factory)}else if(typeof exports==="object"&&typeof require==="function"){factory(require("jquery"))}else{factory(jQuery)}})(function($){"use strict";var utils=function(){return{escapeRegExChars:function(value){return value.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&") +},createNode:function(containerClass){var div=document.createElement("div");div.className=containerClass;div.style.position="absolute";div.style.display="none";return div}}}(),keys={ESC:27,TAB:9,RETURN:13,LEFT:37,UP:38,RIGHT:39,DOWN:40};function Autocomplete(el,options){var noop=function(){},that=this,defaults={ajaxSettings:{},autoSelectFirst:false,appendTo:document.body,serviceUrl:null,lookup:null,onSelect:null,width:"auto",minChars:1,maxHeight:300,deferRequestBy:0,params:{},formatResult:Autocomplete.formatResult,delimiter:null,zIndex:9999,type:"GET",noCache:false,onSearchStart:noop,onSearchComplete:noop,onSearchError:noop,containerClass:"autocomplete-suggestions",tabDisabled:false,dataType:"text",currentRequest:null,triggerSelectOnValidInput:true,preventBadQueries:true,lookupFilter:function(suggestion,originalQuery,queryLowerCase){return suggestion.value.toLowerCase().indexOf(queryLowerCase)!==-1 +},paramName:"query",transformResult:function(response){return typeof response==="string"?$.parseJSON(response):response},showNoSuggestionNotice:false,noSuggestionNotice:"No results",orientation:"bottom",forceFixPosition:false};that.element=el;that.el=$(el);that.suggestions=[];that.badQueries=[];that.selectedIndex=-1; +that.currentValue=that.element.value;that.intervalId=0;that.cachedResponse={};that.onChangeInterval=null;that.onChange=null;that.isLocal=false;that.suggestionsContainer=null;that.noSuggestionsContainer=null;that.options=$.extend({},defaults,options);that.classes={selected:"autocomplete-selected",suggestion:"autocomplete-suggestion"}; +that.hint=null;that.hintValue="";that.selection=null;that.initialize();that.setOptions(options)}Autocomplete.utils=utils;$.Autocomplete=Autocomplete;Autocomplete.formatResult=function(suggestion,currentValue){var pattern="("+utils.escapeRegExChars(currentValue)+")";return suggestion.value.replace(new RegExp(pattern,"gi"),"$1") +};Autocomplete.prototype={killerFn:null,initialize:function(){var that=this,suggestionSelector="."+that.classes.suggestion,selected=that.classes.selected,options=that.options,container;that.element.setAttribute("autocomplete","off");that.killerFn=function(e){if($(e.target).closest("."+that.options.containerClass).length===0){that.killSuggestions(); +that.disableKillerFn()}};that.noSuggestionsContainer=$('
').html(this.options.noSuggestionNotice).get(0);that.suggestionsContainer=Autocomplete.utils.createNode(options.containerClass);container=$(that.suggestionsContainer);container.appendTo(options.appendTo); +if(options.width!=="auto"){container.width(options.width)}container.on("mouseover.autocomplete",suggestionSelector,function(){that.activate($(this).data("index"))});container.on("mouseout.autocomplete",function(){that.selectedIndex=-1;container.children("."+selected).removeClass(selected)});container.on("click.autocomplete",suggestionSelector,function(){that.select($(this).data("index")) +});that.fixPositionCapture=function(){if(that.visible){that.fixPosition()}};$(window).on("resize.autocomplete",that.fixPositionCapture);that.el.on("keydown.autocomplete",function(e){that.onKeyPress(e)});that.el.on("keyup.autocomplete",function(e){that.onKeyUp(e)});that.el.on("blur.autocomplete",function(){that.onBlur() +});that.el.on("focus.autocomplete",function(){that.onFocus()});that.el.on("change.autocomplete",function(e){that.onKeyUp(e)})},onFocus:function(){var that=this;that.fixPosition();if(that.options.minChars<=that.el.val().length){that.onValueChange()}},onBlur:function(){this.enableKillerFn()},setOptions:function(suppliedOptions){var that=this,options=that.options; +$.extend(options,suppliedOptions);that.isLocal=$.isArray(options.lookup);if(that.isLocal){options.lookup=that.verifySuggestionsFormat(options.lookup)}options.orientation=that.validateOrientation(options.orientation,"bottom");$(that.suggestionsContainer).css({"max-height":options.maxHeight+"px",width:options.width+"px","z-index":options.zIndex}) +},clearCache:function(){this.cachedResponse={};this.badQueries=[]},clear:function(){this.clearCache();this.currentValue="";this.suggestions=[]},disable:function(){var that=this;that.disabled=true;if(that.currentRequest){that.currentRequest.abort()}},enable:function(){this.disabled=false},fixPosition:function(){var that=this,$container=$(that.suggestionsContainer),containerParent=$container.parent().get(0); +if(containerParent!==document.body&&!that.options.forceFixPosition)return;var orientation=that.options.orientation,containerHeight=$container.outerHeight(),height=that.el.outerHeight(),offset=that.el.offset(),styles={top:offset.top,left:offset.left};if(orientation=="auto"){var viewPortHeight=$(window).height(),scrollTop=$(window).scrollTop(),topOverflow=-scrollTop+offset.top-containerHeight,bottomOverflow=scrollTop+viewPortHeight-(offset.top+height+containerHeight); +orientation=Math.max(topOverflow,bottomOverflow)===topOverflow?"top":"bottom"}if(orientation==="top"){styles.top+=-containerHeight}else{styles.top+=height}if(containerParent!==document.body){var opacity=$container.css("opacity"),parentOffsetDiff;if(!that.visible){$container.css("opacity",0).show()}parentOffsetDiff=$container.offsetParent().offset(); +styles.top-=parentOffsetDiff.top;styles.left-=parentOffsetDiff.left;if(!that.visible){$container.css("opacity",opacity).hide()}}if(that.options.width==="auto"){styles.width=that.el.outerWidth()-2+"px"}$container.css(styles)},enableKillerFn:function(){var that=this;$(document).on("click.autocomplete",that.killerFn) +},disableKillerFn:function(){var that=this;$(document).off("click.autocomplete",that.killerFn)},killSuggestions:function(){var that=this;that.stopKillSuggestions();that.intervalId=window.setInterval(function(){that.hide();that.stopKillSuggestions()},50)},stopKillSuggestions:function(){window.clearInterval(this.intervalId) +},isCursorAtEnd:function(){var that=this,valLength=that.el.val().length,selectionStart=that.element.selectionStart,range;if(typeof selectionStart==="number"){return selectionStart===valLength}if(document.selection){range=document.selection.createRange();range.moveStart("character",-valLength);return valLength===range.text.length +}return true},onKeyPress:function(e){var that=this;if(!that.disabled&&!that.visible&&e.which===keys.DOWN&&that.currentValue){that.suggest();return}if(that.disabled||!that.visible){return}switch(e.which){case keys.ESC:that.el.val(that.currentValue);that.hide();break;case keys.RIGHT:if(that.hint&&that.options.onHint&&that.isCursorAtEnd()){that.selectHint(); +break}return;case keys.TAB:if(that.hint&&that.options.onHint){that.selectHint();return}case keys.RETURN:if(that.selectedIndex===-1){that.hide();return}that.select(that.selectedIndex);if(e.which===keys.TAB&&that.options.tabDisabled===false){return}break;case keys.UP:that.moveUp();break;case keys.DOWN:that.moveDown(); +break;default:return}e.stopImmediatePropagation();e.preventDefault()},onKeyUp:function(e){var that=this;if(that.disabled){return}switch(e.which){case keys.UP:case keys.DOWN:return}clearInterval(that.onChangeInterval);if(that.currentValue!==that.el.val()){that.findBestHint();if(that.options.deferRequestBy>0){that.onChangeInterval=setInterval(function(){that.onValueChange() +},that.options.deferRequestBy)}else{that.onValueChange()}}},onValueChange:function(){var that=this,options=that.options,value=that.el.val(),query=that.getQuery(value),index;if(that.selection&&that.currentValue!==query){that.selection=null;(options.onInvalidateSelection||$.noop).call(that.element)}clearInterval(that.onChangeInterval); +that.currentValue=value;that.selectedIndex=-1;if(options.triggerSelectOnValidInput){index=that.findSuggestionIndex(query);if(index!==-1){that.select(index);return}}if(query.lengthlimit){data.suggestions=data.suggestions.slice(0,limit)}return data},getSuggestions:function(q){var response,that=this,options=that.options,serviceUrl=options.serviceUrl,params,cacheKey,ajaxSettings; +options.params[options.paramName]=q;params=options.ignoreParams?null:options.params;if(that.isLocal){response=that.getSuggestionsLocal(q)}else{if($.isFunction(serviceUrl)){serviceUrl=serviceUrl.call(that.element,q)}cacheKey=serviceUrl+"?"+$.param(params||{});response=that.cachedResponse[cacheKey]}if(response&&$.isArray(response.suggestions)){that.suggestions=response.suggestions; +that.suggest()}else if(!that.isBadQuery(q)){if(options.onSearchStart.call(that.element,options.params)===false){return}if(that.currentRequest){that.currentRequest.abort()}ajaxSettings={url:serviceUrl,data:params,type:options.type,dataType:options.dataType};$.extend(ajaxSettings,options.ajaxSettings); +that.currentRequest=$.ajax(ajaxSettings).done(function(data){var result;that.currentRequest=null;result=options.transformResult(data);that.processResponse(result,q,cacheKey);options.onSearchComplete.call(that.element,q,result.suggestions)}).fail(function(jqXHR,textStatus,errorThrown){options.onSearchError.call(that.element,q,jqXHR,textStatus,errorThrown) +})}},isBadQuery:function(q){if(!this.options.preventBadQueries){return false}var badQueries=this.badQueries,i=badQueries.length;while(i--){if(q.indexOf(badQueries[i])===0){return true}}return false},hide:function(){var that=this,container=$(that.suggestionsContainer);if($.isFunction(this.options.onHide)&&that.visible){this.options.onHide.call(that.element,container) +}that.visible=false;that.selectedIndex=-1;$(that.suggestionsContainer).hide();that.signalHint(null)},suggest:function(){if(this.suggestions.length===0){this.options.showNoSuggestionNotice?this.noSuggestions():this.hide();return}var that=this,options=that.options,formatResult=options.formatResult,value=that.getQuery(that.currentValue),className=that.classes.suggestion,classSelected=that.classes.selected,container=$(that.suggestionsContainer),noSuggestionsContainer=$(that.noSuggestionsContainer),beforeRender=options.beforeRender,html="",index; +if(options.triggerSelectOnValidInput){index=that.findSuggestionIndex(value);if(index!==-1){that.select(index);return}}$.each(that.suggestions,function(i,suggestion){html+='
'+formatResult(suggestion,value)+"
"});this.adjustContainerWidth();noSuggestionsContainer.detach(); +container.html(html);if(options.autoSelectFirst){that.selectedIndex=0;container.children().first().addClass(classSelected)}if($.isFunction(beforeRender)){beforeRender.call(that.element,container)}that.fixPosition();container.show();that.visible=true;that.findBestHint()},noSuggestions:function(){var that=this,container=$(that.suggestionsContainer),noSuggestionsContainer=$(that.noSuggestionsContainer); +this.adjustContainerWidth();noSuggestionsContainer.detach();container.empty();container.append(noSuggestionsContainer);that.fixPosition();container.show();that.visible=true},adjustContainerWidth:function(){var that=this,options=that.options,width,container=$(that.suggestionsContainer);if(options.width==="auto"){width=that.el.outerWidth()-2; +container.width(width>0?width:300)}},findBestHint:function(){var that=this,value=that.el.val().toLowerCase(),bestMatch=null;if(!value){return}$.each(that.suggestions,function(i,suggestion){var foundMatch=suggestion.value.toLowerCase().indexOf(value)===0;if(foundMatch){bestMatch=suggestion}return!foundMatch +});that.signalHint(bestMatch)},signalHint:function(suggestion){var hintValue="",that=this;if(suggestion){hintValue=that.currentValue+suggestion.value.substr(that.currentValue.length)}if(that.hintValue!==hintValue){that.hintValue=hintValue;that.hint=suggestion;(this.options.onHint||$.noop)(hintValue)}},verifySuggestionsFormat:function(suggestions){if(suggestions.length&&typeof suggestions[0]==="string"){return $.map(suggestions,function(value){return{value:value,data:null} +})}return suggestions},validateOrientation:function(orientation,fallback){orientation=$.trim(orientation||"").toLowerCase();if($.inArray(orientation,["auto","bottom","top"])===-1){orientation=fallback}return orientation},processResponse:function(result,originalQuery,cacheKey){var that=this,options=that.options; +result.suggestions=that.verifySuggestionsFormat(result.suggestions);if(!options.noCache){that.cachedResponse[cacheKey]=result;if(options.preventBadQueries&&result.suggestions.length===0){that.badQueries.push(originalQuery)}}if(originalQuery!==that.getQuery(that.currentValue)){return}that.suggestions=result.suggestions; +that.suggest()},activate:function(index){var that=this,activeItem,selected=that.classes.selected,container=$(that.suggestionsContainer),children=container.children();container.children("."+selected).removeClass(selected);that.selectedIndex=index;if(that.selectedIndex!==-1&&children.length>that.selectedIndex){activeItem=children.get(that.selectedIndex); +$(activeItem).addClass(selected);return activeItem}return null},selectHint:function(){var that=this,i=$.inArray(that.hint,that.suggestions);that.select(i)},select:function(i){var that=this;that.hide();that.onSelect(i)},moveUp:function(){var that=this;if(that.selectedIndex===-1){return}if(that.selectedIndex===0){$(that.suggestionsContainer).children().first().removeClass(that.classes.selected); +that.selectedIndex=-1;that.el.val(that.currentValue);that.findBestHint();return}that.adjustScroll(that.selectedIndex-1)},moveDown:function(){var that=this;if(that.selectedIndex===that.suggestions.length-1){return}that.adjustScroll(that.selectedIndex+1)},adjustScroll:function(index){var that=this,activeItem=that.activate(index),offsetTop,upperBound,lowerBound,heightDelta=25; +if(!activeItem){return}offsetTop=activeItem.offsetTop;upperBound=$(that.suggestionsContainer).scrollTop();lowerBound=upperBound+that.options.maxHeight-heightDelta;if(offsetToplowerBound){$(that.suggestionsContainer).scrollTop(offsetTop-that.options.maxHeight+heightDelta) +}that.el.val(that.getValue(that.suggestions[index].value));that.signalHint(null)},onSelect:function(index){var that=this,onSelectCallback=that.options.onSelect,suggestion=that.suggestions[index];that.currentValue=that.getValue(suggestion.value);if(that.currentValue!==that.el.val()){that.el.val(that.currentValue) +}that.signalHint(null);that.suggestions=[];that.selection=suggestion;if($.isFunction(onSelectCallback)){onSelectCallback.call(that.element,suggestion)}},getValue:function(value){var that=this,delimiter=that.options.delimiter,currentValue,parts;if(!delimiter){return value}currentValue=that.currentValue; +parts=currentValue.split(delimiter);if(parts.length===1){return value}return currentValue.substr(0,currentValue.length-parts[parts.length-1].length)+value},dispose:function(){var that=this;that.el.off(".autocomplete").removeData("autocomplete");that.disableKillerFn();$(window).off("resize.autocomplete",that.fixPositionCapture); +$(that.suggestionsContainer).remove()}};$.fn.autocomplete=$.fn.devbridgeAutocomplete=function(options,args){var dataKey="autocomplete";if(arguments.length===0){return this.first().data(dataKey)}return this.each(function(){var inputElement=$(this),instance=inputElement.data(dataKey);if(typeof options==="string"){if(instance&&typeof instance[options]==="function"){instance[options](args) +}}else{if(instance&&instance.dispose){instance.dispose()}instance=new Autocomplete(this,options);inputElement.data(dataKey,instance)}})}});