10 lines
18 KiB
JavaScript
10 lines
18 KiB
JavaScript
|
/**
|
||
|
* @fileOverview jquery-autocomplete, the jQuery Autocompleter
|
||
|
* @author <a href="mailto:dylan@dyve.net">Dylan Verheul</a>
|
||
|
* @version 2.4.4
|
||
|
* @requires jQuery 1.6+
|
||
|
* @license MIT | GPL | Apache 2.0, see LICENSE.txt
|
||
|
* @see https://github.com/dyve/jquery-autocomplete
|
||
|
*/
|
||
|
(function($){"use strict";$.fn.autocomplete=function(options){var url;if(arguments.length>1){url=options;options=arguments[1];options.url=url}else if(typeof options==="string"){url=options;options={url:url}}var opts=$.extend({},$.fn.autocomplete.defaults,options);return this.each(function(){var $this=$(this);$this.data("autocompleter",new $.Autocompleter($this,$.meta?$.extend({},opts,$this.data()):opts))})};$.fn.autocomplete.defaults={inputClass:"acInput",loadingClass:"acLoading",resultsClass:"acResults",selectClass:"acSelect",queryParamName:"q",extraParams:{},remoteDataType:false,lineSeparator:"\n",cellSeparator:"|",minChars:2,maxItemsToShow:10,delay:400,useCache:true,maxCacheLength:10,matchSubset:true,matchCase:false,matchInside:true,mustMatch:false,selectFirst:false,selectOnly:false,showResult:null,preventDefaultReturn:1,preventDefaultTab:0,autoFill:false,filterResults:true,filter:true,sortResults:true,sortFunction:null,onItemSelect:null,onNoMatch:null,onFinish:null,matchStringConverter:null,beforeUseConverter:null,autoWidth:"min-width",useDelimiter:false,delimiterChar:",",delimiterKeyCode:188,processData:null,onError:null,enabled:true};var sanitizeResult=function(result){var value,data;var type=typeof result;if(type==="string"){value=result;data={}}else if($.isArray(result)){value=result[0];data=result.slice(1)}else if(type==="object"){value=result.value;data=result.data}value=String(value);if(typeof data!=="object"){data={}}return{value:value,data:data}};var sanitizeInteger=function(value,stdValue,options){var num=parseInt(value,10);options=options||{};if(isNaN(num)||options.min&&num<options.min){num=stdValue}return num};var makeUrlParam=function(name,value){return[name,encodeURIComponent(value)].join("=")};var makeUrl=function(url,params){var urlAppend=[];$.each(params,function(index,value){urlAppend.push(makeUrlParam(index,value))});if(urlAppend.length){url+=url.indexOf("?")===-1?"?":"&";url+=urlAppend.join("&")}return url};var sortValueAlpha=function(a,b,matchCase){a=String(a.value);b=String(b.value);if(!matchCase){a=a.toLowerCase();b=b.toLowerCase()}if(a>b){return 1}if(a<b){return-1}return 0};var plainTextParser=function(text,lineSeparator,cellSeparator){var results=[];var i,j,data,line,value,lines;lines=String(text).replace("\r\n","\n").split(lineSeparator);for(i=0;i<lines.length;i++){line=lines[i].split(cellSeparator);data=[];for(j=0;j<line.length;j++){data.push(decodeURIComponent(line[j]))}value=data.shift();results.push({value:value,data:data})}return results};$.Autocompleter=function($elem,options){if(!$elem||!($elem instanceof $)||$elem.length!==1||$elem.get(0).tagName.toUpperCase()!=="INPUT"){throw new Error("Invalid parameter for jquery.Autocompleter, jQuery object with one element with INPUT tag expected.")}var self=this;this.options=options;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_=false;this.finishOnBlur_=true;this.options.minChars=sanitizeInteger(this.options.minChars,$.fn.autocomplete.defaults.minChars,{min:0});this.options.maxItemsToShow=sanitizeInteger(this.options.maxItemsToShow,$.fn.autocomplete.defaults.maxItemsToShow,{min:0});this.options.maxCacheLength=sanitizeInteger(this.options.maxCacheLength,$.fn.autocomplete.defaults.maxCacheLength,{min:1});this.options.delay=sanitizeInteger(this.options.delay,$.fn.autocomplete.defaults.delay,{min:0});if(this.options.preventDefaultReturn!=2){this.options.preventDefaultReturn=this.options.preventDefaultReturn?1:0}if(this.options.preventDefaultTab!=2){this.options.preventDefaultTab=this.options.preventDefaultTab?1:0}this.dom={};this.dom.$elem=$elem;this.dom.$elem.attr("autocomplete","off").addClass(this.options.inputClass);this.dom.$results=$("<div></div>").hide().addClass(this.options.resultsClass).css({position:"absolute"});$("body").append(this.dom.$results);$elem.keydown(function(e){self.lastKeyPressed_=e.keyCode;switch(self.lastKeyPressed_){case self.options
|