You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 lines
9.9 KiB
1 lines
9.9 KiB
(function(n,t){"use strict";function ut(t){var i,r,f;for(t=n.extend({},s,t||{}),u===null&&(u=n("body")),i=n(this),r=0,f=i.length;r<f;r++)ft(i.eq(r),t);return i}function ft(t,i){var u,f,e,r;if(!t.hasClass("autocompleter-node")){i=n.extend({},i,t.data("autocompleter-options"));u='<div class="autocompleter '+i.customClass.join(" ")+'" id="autocompleter-'+(v+1)+'">';i.hint&&(u+='<div class="autocompleter-hint"><\/div>');u+='<ul class="autocompleter-list"><\/ul>';u+="<\/div>";t.addClass("autocompleter-node").after(u);f=t.next(".autocompleter").eq(0);e=t.attr("autocomplete");t.attr("autocomplete","off");r=n.extend({$node:t,$autocompleter:f,$selected:null,$list:null,index:-1,hintText:!1,source:!1,jqxhr:!1,response:null,focused:!1,query:"",originalAutocomplete:e,guid:v++},i);r.$autocompleter.on("mousedown.autocompleter",".autocompleter-item",r,b).data("autocompleter",r);r.$node.on("keyup.autocompleter",r,st).on("keydown.autocompleter",r,ht).on("focus.autocompleter",r,ct).on("blur.autocompleter",r,lt).on("mousedown.autocompleter",r,at)}}function et(n,t,i){var u=[],f,r;if(n=n.toUpperCase(),t.length)for(f=0;f<2;f++)for(r in t)if("clear,insert,remove".indexOf(r)===-1&&u.length<i)switch(f){case 0:t[r].label.toUpperCase().search(n)===0&&(u.push(t[r]),delete t[r]);break;case 1:t[r].label.toUpperCase().search(n)!==-1&&(u.push(t[r]),delete t[r])}return u}function h(t){var i,r;if(t.query=n.trim(t.$node.val()),t.empty||t.query.length!==0)typeof t.source=="object"?(e(t),i=et(t.query,dt(t.source),t.limit),i.length&&c(i,t)):(t.jqxhr&&t.jqxhr.abort(),r=n.extend({limit:t.limit,query:t.query},t.combine()),t.jqxhr=n.ajax({url:t.source,dataType:"json",data:r,beforeSend:function(n){if(t.$autocompleter.addClass("autocompleter-ajax"),e(t),t.cache){var i=kt(this.url);i&&(n.abort(),c(i,t))}}}).done(function(n){t.offset&&(n=wt(n,t.offset));t.cache&&bt(this.url,n);c(n,t)}).always(function(){t.$autocompleter.removeClass("autocompleter-ajax")}));else{e(t);return}}function e(n){n.response=null;n.$list=null;n.$selected=null;n.index=0;n.$autocompleter.find(".autocompleter-list").empty();n.$autocompleter.find(".autocompleter-hint").removeClass("autocompleter-hint-show").empty();n.hintText=!1;i(null,n)}function c(n,t){ot(n,t);t.$autocompleter.hasClass("autocompleter-focus")&&l(null,t)}function ot(t,i){for(var f,s,e,o,v,h,y,c,l="",r=0,p=t.length;r<p;r++){f=["autocompleter-item"];i.selectFirst&&r===0&&!i.changeWhenSelect&&f.push("autocompleter-item-selected");var w=new RegExp(i.query,"gi"),u=i.customLabel&&t[r][i.customLabel]?t[r][i.customLabel]:t[r].label,a=u;if(u=i.highlightMatches?u.replace(w,"<strong>$&<\/strong>"):u,s=i.customValue&&t[r][i.customValue]?t[r][i.customValue]:t[r].value,i.template){e=i.template.replace(/({{ label }})/gi,u);for(o in t[r])t[r].hasOwnProperty(o)&&(v=new RegExp("{{ "+o+" }}","gi"),e=e.replace(v,t[r][o]));u=e}l+=s?'<li data-value="'+s+'" data-label="'+a+'" class="'+f.join(" ")+'">'+u+"<\/li>":'<li data-label="'+a+'" class="'+f.join(" ")+'">'+u+"<\/li>"}t.length&&i.hint&&(h=t[0].label.substr(0,i.query.length).toUpperCase()===i.query.toUpperCase()?t[0].label:!1,h&&i.query!==t[0].label&&(y=new RegExp(i.query,"i"),c=h.replace(y,"<span>"+i.query+"<\/span>"),i.$autocompleter.find(".autocompleter-hint").addClass("autocompleter-hint-show").html(c),i.hintText=c));i.response=t;i.$autocompleter.find(".autocompleter-list").html(l);i.$selected=i.$autocompleter.find(".autocompleter-item-selected").length?i.$autocompleter.find(".autocompleter-item-selected"):null;i.$list=t.length?i.$autocompleter.find(".autocompleter-item"):null;i.index=i.$selected?i.$list.index(i.$selected):-1;i.$autocompleter.find(".autocompleter-item").each(function(t,r){n(r).data(i.response[t])})}function st(t){var i=t.data,e=t.keyCode?t.keyCode:t.which,f,r,u;(e===40||e===38)&&i.$autocompleter.hasClass("autocompleter-show")?(f=i.$list.length,f&&(f>1?i.index===f-1?(r=i.changeWhenSelect?-1:0,u=i.index-1):i.index===0?(r=i.index+1,u=i.changeWhenSelect?-1:f-1):i.index===-1?(r=0,u=f-1):(r=i.index+1,u=i.index-1):i.index===-1?(r=0,u=0):(u=-1,r=-1),i.index=e===40?r:u,i.$list.removeClass("autocompleter-item-selected"),i.index!==-1&&i.$list.eq(i.index).addClass("autocompleter-item-selected"),i.$selected=i.$autocompleter.find(".autocompleter-item-selected").length?i.$autocompleter.find(".autocompleter-item-selected"):null,i.changeWhenSelect&&a(i))):n.inArray(e,nt)===-1&&n.inArray(e,i.ignoredKeyCode)===-1&&h(i)}function ht(n){var i=n.keyCode?n.keyCode:n.which,t=n.data,r;i===40||i===38?(n.preventDefault(),n.stopPropagation()):i===39?t.hint&&t.hintText&&t.$autocompleter.find(".autocompleter-hint").hasClass("autocompleter-hint-show")&&(n.preventDefault(),n.stopPropagation(),r=t.$autocompleter.find(".autocompleter-item").length?t.$autocompleter.find(".autocompleter-item").eq(0).attr("data-label"):!1,r&&(t.query=r,yt(t))):i===13&&t.$autocompleter.hasClass("autocompleter-show")&&t.$selected&&b(n)}function ct(n,t){if(!t){var i=n.data;i.$autocompleter.addClass("autocompleter-focus");i.$node.prop("disabled")||i.$autocompleter.hasClass("autocompleter-show")||i.focusOpen&&(h(i),i.focused=!0,setTimeout(function(){i.focused=!1},500))}}function lt(n,t){n.preventDefault();n.stopPropagation();var r=n.data;t||(r.$autocompleter.removeClass("autocompleter-focus"),i(n))}function at(r){var u,f,e;(r.type!=="mousedown"||n.inArray(r.which,[2,3])===-1)&&(u=r.data,u.$list&&!u.focused&&(u.$node.is(":disabled")||(p&&!rt?(f=u.$select[0],t.document.createEvent?(e=t.document.createEvent("MouseEvents"),e.initMouseEvent("mousedown",!1,!0,t,0,0,0,0,0,!1,!1,!1,!1,0,null),f.dispatchEvent(e)):f.fireEvent&&f.fireEvent("onmousedown")):u.$autocompleter.hasClass("autocompleter-closed")?l(r):u.$autocompleter.hasClass("autocompleter-show")&&i(r))))}function l(n,t){var i=n?n.data:t;if(!i.$node.prop("disabled")&&!i.$autocompleter.hasClass("autocompleter-show")&&i.$list&&i.$list.length){i.$autocompleter.removeClass("autocompleter-closed").addClass("autocompleter-show");u.on("click.autocompleter-"+i.guid,":not(.autocompleter-item)",i,vt)}}function vt(t){n(t.target).hasClass("autocompleter-node")||n(t.currentTarget).parents(".autocompleter").length===0&&i(t)}function i(n,t){var i=n?n.data:t;i.$autocompleter.hasClass("autocompleter-show")&&(i.$autocompleter.removeClass("autocompleter-show").addClass("autocompleter-closed"),u.off(".autocompleter-"+i.guid))}function b(t){if(t.type!=="mousedown"||n.inArray(t.which,[2,3])===-1){var r=t.data;t.preventDefault();t.stopPropagation();t.type==="mousedown"&&n(this).length&&(r.$selected=n(this),r.index=r.$list.index(r.$selected));r.$node.prop("disabled")||(i(t),pt(r),t.type==="click"&&r.$node.trigger("focus",[!0]))}}function yt(n){a(n);k(n);h(n)}function a(n){if(n.$selected){n.hintText&&n.$autocompleter.find(".autocompleter-hint").hasClass("autocompleter-hint-show")&&n.$autocompleter.find(".autocompleter-hint").removeClass("autocompleter-hint-show");var t=n.$selected.attr("data-value")?n.$selected.attr("data-value"):n.$selected.attr("data-label");n.$node.val(t)}else n.hintText&&!n.$autocompleter.find(".autocompleter-hint").hasClass("autocompleter-hint-show")&&n.$autocompleter.find(".autocompleter-hint").addClass("autocompleter-hint-show"),n.$node.val(n.query)}function pt(n){a(n);k(n);e(n)}function k(n){n.callback.call(n.$autocompleter,n.$node.val(),n.index,n.response[n.index]);n.$node.trigger("change")}function wt(n,t){for(t=t.split(".");n&&t.length;)n=n[t.shift()];return n}function bt(n,t){if(w&&n&&t){r[n]={value:t};try{localStorage.setItem(o,JSON.stringify(r))}catch(i){var u=i.code||i.number||i.message;if(u===22)g();else throw i;}}}function kt(n){if(n)return r[n]&&r[n].value?r[n].value:!1}function d(){if(w){var n=localStorage.getItem(o)||"{}";return JSON.parse(n)}}function g(){try{localStorage.removeItem(o);r=d()}catch(n){throw n;}}function dt(n){var i,t;if(null===n||"object"!=typeof n)return n;i=n.constructor();for(t in n)n.hasOwnProperty(t)&&(i[t]=n[t]);return i}var v=0,nt=[9,13,17,19,20,27,33,34,35,36,37,39,44,92,113,114,115,118,119,120,122,123,144,145],tt=["source","empty","limit","cache","focusOpen","selectFirst","changeWhenSelect","highlightMatches","ignoredKeyCode","customLabel","customValue","template","offset","combine","callback"],y=t.navigator.userAgent||t.navigator.vendor||t.opera,it=/Firefox/i.test(y),p=/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(y),rt=it&&p,u=null,o="autocompleterCache",w=function(){var n=typeof t.localStorage!="undefined";if(n)try{localStorage.setItem("autocompleter","autocompleter");localStorage.removeItem("autocompleter")}catch(i){n=!1}return n}(),s={source:null,empty:!0,limit:10,customClass:[],cache:!0,focusOpen:!0,hint:!1,selectFirst:!1,changeWhenSelect:!0,highlightMatches:!1,ignoredKeyCode:[],customLabel:!1,customValue:!1,template:!1,offset:!1,combine:n.noop,callback:n.noop},f={defaults:function(t){return s=n.extend(s,t||{}),n(this)},option:function(t){return n(this).each(function(i,r){var f=n(r).next(".autocompleter").data("autocompleter");for(var u in t)n.inArray(u,tt)!==-1&&(f[u]=t[u])})},open:function(){return n(this).each(function(t,i){var r=n(i).next(".autocompleter").data("autocompleter");r&&l(null,r)})},close:function(){return n(this).each(function(t,r){var u=n(r).next(".autocompleter").data("autocompleter");u&&i(null,u)})},clearCache:function(){g()},destroy:function(){return n(this).each(function(t,i){var r=n(i).next(".autocompleter").data("autocompleter");r&&(r.jqxhr&&r.jqxhr.abort(),r.$autocompleter.hasClass("open")&&r.$autocompleter.find(".autocompleter-selected").trigger("click.autocompleter"),r.originalAutocomplete?r.$node.attr("autocomplete",r.originalAutocomplete):r.$node.removeAttr("autocomplete"),r.$node.off(".autocompleter").removeClass("autocompleter-node"),r.$autocompleter.off(".autocompleter").remove())})}},r=d();n.fn.autocompleter=function(n){return f[n]?f[n].apply(this,Array.prototype.slice.call(arguments,1)):typeof n=="object"||!n?ut.apply(this,arguments):this};n.autocompleter=function(n){n==="defaults"?f.defaults.apply(this,Array.prototype.slice.call(arguments,1)):n==="clearCache"&&f.clearCache.apply(this,null)}})(jQuery,window);
|