Files
galaxy/static/scripts/libs/jquery/jquery.autocomplete.js
T

2 lines
14 KiB
JavaScript

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