1 line
13 KiB
JavaScript
Executable File
1 line
13 KiB
JavaScript
Executable File
(function(e){"use strict";e.fn.autocomplete=function(t){var n;arguments.length>1?(n=t,t=arguments[1],t.url=n):typeof t=="string"&&(n=t,t={url:n});var r=e.extend({},e.fn.autocomplete.defaults,t);return this.each(function(){var t=e(this);t.data("autocompleter",new e.Autocompleter(t,e.meta?e.extend({},r,t.data()):r))})},e.fn.autocomplete.defaults={inputClass:"acInput",loadingClass:"acLoading",resultsClass:"acResults",selectClass:"acSelect",queryParamName:"q",extraParams:{},remoteDataType:!1,lineSeparator:"\n",cellSeparator:"|",minChars:2,maxItemsToShow:10,delay:400,useCache:!0,maxCacheLength:10,matchSubset:!0,matchCase:!1,matchInside:!0,mustMatch:!1,selectFirst:!1,selectOnly:!1,showResult:null,preventDefaultReturn:1,preventDefaultTab:0,autoFill:!1,filterResults:!0,sortResults:!0,sortFunction:null,onItemSelect:null,onNoMatch:null,onFinish:null,matchStringConverter:null,beforeUseConverter:null,autoWidth:"min-width",useDelimiter:!1,delimiterChar:",",delimiterKeyCode:188,processData:null,onError:null};var t=function(t){var n,r,i=typeof t;return i==="string"?(n=t,r={}):e.isArray(t)?(n=t[0],r=t.slice(1)):i==="object"&&(n=t.value,r=t.data),n=String(n),typeof r!="object"&&(r={}),{value:n,data:r}},n=function(e,t,n){var r=parseInt(e,10);n=n||{};if(isNaN(r)||n.min&&r<n.min)r=t;return r},r=function(e,t){return[e,encodeURIComponent(t)].join("=")},i=function(t,n){var i=[];return e.each(n,function(e,t){i.push(r(e,t))}),i.length&&(t+=t.indexOf("?")===-1?"?":"&",t+=i.join("&")),t},s=function(e,t,n){return e=String(e.value),t=String(t.value),n||(e=e.toLowerCase(),t=t.toLowerCase()),e>t?1:e<t?-1:0},o=function(e,t,n){var r=[],i,s,o,u,a,f;f=String(e).replace("\r\n","\n").split(t);for(i=0;i<f.length;i++){u=f[i].split(n),o=[];for(s=0;s<u.length;s++)o.push(decodeURIComponent(u[s]));a=o.shift(),r.push({value:a,data:o})}return r};e.Autocompleter=function(t,r){if(!t||!(t instanceof e)||t.length!==1||t.get(0).tagName.toUpperCase()!=="INPUT")throw new Error("Invalid parameter for jquery.Autocompleter, jQuery object with one element with INPUT tag expected.");var i=this;this.options=r,this.cacheData_={},this.cacheLength_=0,this.selectClass_="jquery-autocomplete-selected-item",this.keyTimeout_=null,this.finishTimeout_=null,this.lastKeyPressed_=null,this.lastProcessedValue_=null,this.lastSelectedValue_=null,this.active_=!1,this.finishOnBlur_=!0,this.options.minChars=n(this.options.minChars,e.fn.autocomplete.defaults.minChars,{min:0}),this.options.maxItemsToShow=n(this.options.maxItemsToShow,e.fn.autocomplete.defaults.maxItemsToShow,{min:0}),this.options.maxCacheLength=n(this.options.maxCacheLength,e.fn.autocomplete.defaults.maxCacheLength,{min:1}),this.options.delay=n(this.options.delay,e.fn.autocomplete.defaults.delay,{min:0}),this.options.preventDefaultReturn!=2&&(this.options.preventDefaultReturn=this.options.preventDefaultReturn?1:0),this.options.preventDefaultTab!=2&&(this.options.preventDefaultTab=this.options.preventDefaultTab?1:0),this.dom={},this.dom.$elem=t,this.dom.$elem.attr("autocomplete","off").addClass(this.options.inputClass),this.dom.$results=e("<div></div>").hide().addClass(this.options.resultsClass).css({position:"absolute"}),e("body").append(this.dom.$results),t.keydown(function(e){i.lastKeyPressed_=e.keyCode;switch(i.lastKeyPressed_){case i.options.delimiterKeyCode:i.options.useDelimiter&&i.active_&&i.selectCurrent();break;case 35:case 36:case 16:case 17:case 18:case 37:case 39:break;case 38:return e.preventDefault(),i.active_?i.focusPrev():i.activate(),!1;case 40:return e.preventDefault(),i.active_?i.focusNext():i.activate(),!1;case 9:if(i.active_){i.selectCurrent();if(i.options.preventDefaultTab)return e.preventDefault(),!1}if(i.options.preventDefaultTab===2)return e.preventDefault(),!1;break;case 13:if(i.active_){i.selectCurrent();if(i.options.preventDefaultReturn)return e.preventDefault(),!1}if(i.options.preventDefaultReturn===2)return e.preventDefault(),!1;break;case 27:if(i.active_)return e.preventDefault(),i.deactivate(!0),!1;break;default:i.activate()}});var s=function(){i.deactivate(!0)};t.blur(function(){i.finishOnBlur_&&(i.finishTimeout_=setTimeout(s,200))}),t.parents("form").on("submit",s)},e.Autocompleter.prototype.position=function(){var t=this.dom.$elem.offset(),n=this.dom.$results.outerHeight(),r=e(window).outerHeight(),i=t.top+this.dom.$elem.outerHeight(),s=i+n,o={top:i,left:t.left};if(s>r){var u=t.top-n;u>=0&&(o.top=u)}this.dom.$results.css(o)},e.Autocompleter.prototype.cacheRead=function(e){var t,n,r,i,s;if(this.options.useCache){e=String(e),t=e.length,this.options.matchSubset?n=1:n=t;while(n<=t){this.options.matchInside?i=t-n:i=0,s=0;while(s<=i){r=e.substr(0,n);if(this.cacheData_[r]!==undefined)return this.cacheData_[r];s++}n++}}return!1},e.Autocompleter.prototype.cacheWrite=function(e,t){return this.options.useCache?(this.cacheLength_>=this.options.maxCacheLength&&this.cacheFlush(),e=String(e),this.cacheData_[e]!==undefined&&this.cacheLength_++,this.cacheData_[e]=t,this.cacheData_[e]):!1},e.Autocompleter.prototype.cacheFlush=function(){this.cacheData_={},this.cacheLength_=0},e.Autocompleter.prototype.callHook=function(t,n){var r=this.options[t];return r&&e.isFunction(r)?r(n,this):!1},e.Autocompleter.prototype.activate=function(){var e=this;this.keyTimeout_&&clearTimeout(this.keyTimeout_),this.keyTimeout_=setTimeout(function(){e.activateNow()},this.options.delay)},e.Autocompleter.prototype.activateNow=function(){var e=this.beforeUseConverter(this.dom.$elem.val());e!==this.lastProcessedValue_&&e!==this.lastSelectedValue_&&this.fetchData(e)},e.Autocompleter.prototype.fetchData=function(e){var t=this,n=function(e,n){t.options.processData&&(e=t.options.processData(e)),t.showResults(t.filterResults(e,n),n)};this.lastProcessedValue_=e,e.length<this.options.minChars?n([],e):this.options.data?n(this.options.data,e):this.fetchRemoteData(e,function(t){n(t,e)})},e.Autocompleter.prototype.fetchRemoteData=function(t,n){var r=this.cacheRead(t);if(r)n(r);else{var i=this,s=i.options.remoteDataType==="json"?"json":"text",o=function(e){var r=!1;e!==!1&&(r=i.parseRemoteData(e),i.cacheWrite(t,r)),i.dom.$elem.removeClass(i.options.loadingClass),n(r)};this.dom.$elem.addClass(this.options.loadingClass),e.ajax({url:this.makeUrl(t),success:o,error:function(t,n,r){e.isFunction(i.options.onError)?i.options.onError(t,n,r):o(!1)},dataType:s})}},e.Autocompleter.prototype.setExtraParam=function(t,n){var r=e.trim(String(t));r&&(this.options.extraParams||(this.options.extraParams={}),this.options.extraParams[r]!==n&&(this.options.extraParams[r]=n,this.cacheFlush()))},e.Autocompleter.prototype.makeUrl=function(t){var n=this,r=this.options.url,s=e.extend({},this.options.extraParams);return this.options.queryParamName===!1?r+=encodeURIComponent(t):s[this.options.queryParamName]=t,i(r,s)},e.Autocompleter.prototype.parseRemoteData=function(t){var n,r=t;if(this.options.remoteDataType==="json"){n=typeof t;switch(n){case"object":r=t;break;case"string":r=e.parseJSON(t);break;default:throw new Error("Unexpected remote data type: "+n)}return r}return o(r,this.options.lineSeparator,this.options.cellSeparator)},e.Autocompleter.prototype.filterResult=function(e,t){if(!e.value)return!1;if(this.options.filterResults){var n=this.matchStringConverter(t),r=this.matchStringConverter(e.value);this.options.matchCase||(n=n.toLowerCase(),r=r.toLowerCase());var i=r.indexOf(n);return this.options.matchInside?i>-1:i===0}return!0},e.Autocompleter.prototype.filterResults=function(e,n){var r=[],i,s;for(i=0;i<e.length;i++)s=t(e[i]),this.filterResult(s,n)&&r.push(s);return this.options.sortResults&&(r=this.sortResults(r,n)),this.options.maxItemsToShow>0&&this.options.maxItemsToShow<r.length&&(r.length=this.options.maxItemsToShow),r},e.Autocompleter.prototype.sortResults=function(t,n){var r=this,i=this.options.sortFunction;return e.isFunction(i)||(i=function(e,t,n){return s(e,t,r.options.matchCase)}),t.sort(function(e,t){return i(e,t,n,r.options)}),t},e.Autocompleter.prototype.matchStringConverter=function(t,n,r){var i=this.options.matchStringConverter;return e.isFunction(i)&&(t=i(t,n,r)),t},e.Autocompleter.prototype.beforeUseConverter=function(t,n,r){t=this.getValue();var i=this.options.beforeUseConverter;return e.isFunction(i)&&(t=i(t,n,r)),t},e.Autocompleter.prototype.enableFinishOnBlur=function(){this.finishOnBlur_=!0},e.Autocompleter.prototype.disableFinishOnBlur=function(){this.finishOnBlur_=!1},e.Autocompleter.prototype.createItemFromResult=function(t){var n=this,r=e("<li>"+this.showResult(t.value,t.data)+"</li>");return r.data({value:t.value,data:t.data}).click(function(){n.selectItem(r)}).mousedown(n.disableFinishOnBlur).mouseup(n.enableFinishOnBlur),r},e.Autocompleter.prototype.getItems=function(){return e(">ul>li",this.dom.$results)},e.Autocompleter.prototype.showResults=function(t,n){var r=t.length,i=this,s=e("<ul></ul>"),o,u,a,f,l=!1,c=!1;if(r){for(o=0;o<r;o++)u=t[o],a=this.createItemFromResult(u),s.append(a),l===!1&&(l=String(u.value),c=a,a.addClass(this.options.firstItemClass)),o===r-1&&a.addClass(this.options.lastItemClass);this.dom.$results.html(s).show(),this.position(),this.options.autoWidth&&(f=this.dom.$elem.outerWidth()-this.dom.$results.outerWidth()+this.dom.$results.width(),this.dom.$results.css(this.options.autoWidth,f)),this.getItems().hover(function(){i.focusItem(this)},function(){}),(this.autoFill(l,n)||this.options.selectFirst||this.options.selectOnly&&r===1)&&this.focusItem(c),this.active_=!0}else this.hideResults(),this.active_=!1},e.Autocompleter.prototype.showResult=function(t,n){return e.isFunction(this.options.showResult)?this.options.showResult(t,n):t},e.Autocompleter.prototype.autoFill=function(e,t){var n,r,i,s;if(this.options.autoFill&&this.lastKeyPressed_!==8){n=String(e).toLowerCase(),r=String(t).toLowerCase(),i=e.length,s=t.length;if(n.substr(0,s)===r){var o=this.getDelimiterOffsets(),u=o.start?" ":"";this.setValue(u+e);var a=s+o.start+u.length,f=i+o.start+u.length;return this.selectRange(a,f),!0}}return!1},e.Autocompleter.prototype.focusNext=function(){this.focusMove(1)},e.Autocompleter.prototype.focusPrev=function(){this.focusMove(-1)},e.Autocompleter.prototype.focusMove=function(t){var r=this.getItems();t=n(t,0);if(t)for(var i=0;i<r.length;i++)if(e(r[i]).hasClass(this.selectClass_)){this.focusItem(i+t);return}this.focusItem(0)},e.Autocompleter.prototype.focusItem=function(t){var n,r=this.getItems();r.length&&(r.removeClass(this.selectClass_).removeClass(this.options.selectClass),typeof t=="number"?(t<0?t=0:t>=r.length&&(t=r.length-1),n=e(r[t])):n=e(t),n&&n.addClass(this.selectClass_).addClass(this.options.selectClass))},e.Autocompleter.prototype.selectCurrent=function(){var t=e("li."+this.selectClass_,this.dom.$results);t.length===1?this.selectItem(t):this.deactivate(!1)},e.Autocompleter.prototype.selectItem=function(e){var t=e.data("value"),n=e.data("data"),r=this.displayValue(t,n),i=this.beforeUseConverter(r);this.lastProcessedValue_=i,this.lastSelectedValue_=i;var s=this.getDelimiterOffsets(),o=this.options.delimiterChar,u=this.dom.$elem,a=0;this.options.useDelimiter&&(u.val().substring(s.start-1,s.start)==o&&o!=" "&&(r=" "+r),u.val().substring(s.end,s.end+1)!=o&&this.lastKeyPressed_!=this.options.delimiterKeyCode?r+=o:a=1),this.setValue(r),this.setCaret(s.start+r.length+a),this.callHook("onItemSelect",{value:t,data:n}),this.deactivate(!0),u.focus()},e.Autocompleter.prototype.displayValue=function(t,n){return e.isFunction(this.options.displayValue)?this.options.displayValue(t,n):t},e.Autocompleter.prototype.hideResults=function(){this.dom.$results.hide()},e.Autocompleter.prototype.deactivate=function(e){this.finishTimeout_&&clearTimeout(this.finishTimeout_),this.keyTimeout_&&clearTimeout(this.keyTimeout_),e&&(this.lastProcessedValue_!==this.lastSelectedValue_&&(this.options.mustMatch&&this.setValue(""),this.callHook("onNoMatch")),this.active_&&this.callHook("onFinish"),this.lastKeyPressed_=null,this.lastProcessedValue_=null,this.lastSelectedValue_=null,this.active_=!1),this.hideResults()},e.Autocompleter.prototype.selectRange=function(e,t){var n=this.dom.$elem.get(0);if(n.setSelectionRange)n.focus(),n.setSelectionRange(e,t);else if(n.createTextRange){var r=n.createTextRange();r.collapse(!0),r.moveEnd("character",t),r.moveStart("character",e),r.select()}},e.Autocompleter.prototype.setCaret=function(e){this.selectRange(e,e)},e.Autocompleter.prototype.getCaret=function(){var t=this.dom.$elem;if(e.browser.msie){var n=document.selection;if(t[0].tagName.toLowerCase()!="textarea"){var r=t.val(),i=n.createRange().duplicate();i.moveEnd("character",r.length);var s=i.text==""?r.length:r.lastIndexOf(i.text);i=n.createRange().duplicate(),i.moveStart("character",-r.length);var o=i.text.length}else{var i=n.createRange(),u=i.duplicate();u.moveToElementText(t[0]),u.setEndPoint("EndToEnd",i);var s=u.text.length-i.text.length,o=s+i.text.length}}else var s=t[0].selectionStart,o=t[0].selectionEnd;return{start:s,end:o}},e.Autocompleter.prototype.setValue=function(e){if(this.options.useDelimiter){var t=this.dom.$elem.val(),n=this.getDelimiterOffsets(),r=t.substring(0,n.start),i=t.substring(n.end);e=r+e+i}this.dom.$elem.val(e)},e.Autocompleter.prototype.getValue=function(){var e=this.dom.$elem.val();if(this.options.useDelimiter){var t=this.getDelimiterOffsets();return e.substring(t.start,t.end).trim()}return e},e.Autocompleter.prototype.getDelimiterOffsets=function(){var e=this.dom.$elem.val();if(this.options.useDelimiter){var t=e.substring(0,this.getCaret().start),n=t.lastIndexOf(this.options.delimiterChar)+1,r=e.substring(this.getCaret().start),i=r.indexOf(this.options.delimiterChar);i==-1&&(i=e.length),i+=this.getCaret().start}else n=0,i=e.length;return{start:n,end:i}}})(jQuery); |