(function(a,b){if(typeof define==="function"&&define.amd){define(["jquery","sifter","microplugin"],b)}else{if(typeof exports==="object"){module.exports=b(require("jquery"),require("sifter"),require("microplugin"))}else{a.Selectize=b(a.jQuery,a.Sifter,a.MicroPlugin)}}}(this,function(v,m,i){var M=function(P,S){if(typeof S==="string"&&!S.length){return}var R=(typeof S==="string")?new RegExp(S,"i"):S;var Q=function(V){var ab=0;if(V.nodeType===3){var aa=V.data.search(R);if(aa>=0&&V.data.length>0){var Z=V.data.match(R);var Y=document.createElement("span");Y.className="highlight";var W=V.splitText(aa);var T=W.splitText(Z[0].length);var U=W.cloneNode(true);Y.appendChild(U);W.parentNode.replaceChild(Y,W);ab=1}}else{if(V.nodeType===1&&V.childNodes&&!/(script|style)/i.test(V.tagName)){for(var X=0;X/g,">").replace(/"/g,""")};var f=function(P){return(P+"").replace(/\$/g,"$$$$")};var s={};s.before=function(P,S,R){var Q=P[S];P[S]=function(){R.apply(P,arguments);return Q.apply(P,arguments)}};s.after=function(P,S,R){var Q=P[S];P[S]=function(){var T=Q.apply(P,arguments);R.apply(P,arguments);return T}};var r=function(P){var Q=false;return function(){if(Q){return}Q=true;P.apply(this,arguments)}};var g=function(Q,P){var R;return function(){var S=this;var T=arguments;window.clearTimeout(R);R=window.setTimeout(function(){Q.apply(S,T)},P)}};var a=function(P,R,T){var S;var Q=P.trigger;var U={};P.trigger=function(){var V=arguments[0];if(R.indexOf(V)!==-1){U[V]=arguments}else{return Q.apply(P,arguments)}};T.apply(P,[]);P.trigger=Q;for(S in U){if(U.hasOwnProperty(S)){Q.apply(P,U[S])}}};var A=function(S,R,P,Q){S.on(R,P,function(T){var U=T.target;while(U&&U.parentNode!==S[0]){U=U.parentNode}T.currentTarget=U;return Q.apply(this,[T])})};var l=function(R){var Q={};if("selectionStart" in R){Q.start=R.selectionStart;Q.length=R.selectionEnd-Q.start}else{if(document.selection){R.focus();var S=document.selection.createRange();var P=document.selection.createRange().text.length;S.moveStart("character",-R.value.length);Q.start=S.text.length-P;Q.length=P}}return Q};var n=function(S,T,Q){var P,U,R={};if(Q){for(P=0,U=Q.length;P").css({position:"absolute",top:-99999,left:-99999,width:"auto",padding:0,whiteSpace:"pre"}).text(S).appendTo("body");n(R,Q,["letterSpacing","fontSize","fontFamily","fontWeight","textTransform"]);var P=Q.width();Q.remove();return P};var L=function(R){var P=null;var Q=function(V,ab){var Z,aa,W,Y,S;var T,U,X;V=V||window.event||{};ab=ab||{};if(V.metaKey||V.altKey){return}if(!ab.force&&R.data("grow")===false){return}Z=R.val();if(V.type&&V.type.toLowerCase()==="keydown"){aa=V.keyCode;W=((aa>=97&&aa<=122)||(aa>=65&&aa<=90)||(aa>=48&&aa<=57)||aa===32);if(aa===q||aa===o){X=l(R[0]);if(X.length){Z=Z.substring(0,X.start)+Z.substring(X.start+X.length)}else{if(aa===o&&X.start){Z=Z.substring(0,X.start-1)+Z.substring(X.start+1)}else{if(aa===q&&typeof X.start!=="undefined"){Z=Z.substring(0,X.start)+Z.substring(X.start+1)}}}}else{if(W){T=V.shiftKey;U=String.fromCharCode(V.keyCode);if(T){U=U.toUpperCase()}else{U=U.toLowerCase()}Z+=U}}}Y=R.attr("placeholder");if(!Z&&Y){Z=Y}S=K(Z,R)+4;if(S!==P){P=S;R.width(S);R.triggerHandler("resize")}};R.on("keydown keyup update blur",Q);Q()};var E=function(Q){var P=document.createElement("div");P.appendChild(Q.cloneNode(true));return P.innerHTML};var d=function(R,Q){if(!Q){Q={}}var P="Selectize";console.error(P+": "+R);if(Q.explanation){if(console.group){console.group()}console.error(Q.explanation);if(console.group){console.groupEnd()}}};var c=function(U,R){var W,S,P,Q,V,X=this;V=U[0];V.selectize=X;var T=window.getComputedStyle&&window.getComputedStyle(V,null);Q=T?T.getPropertyValue("direction"):V.currentStyle&&V.currentStyle.direction;Q=Q||U.parents("[dir]:first").attr("dir")||"";v.extend(X,{order:0,settings:R,$input:U,tabIndex:U.attr("tabindex")||"",tagType:V.tagName.toLowerCase()==="select"?k:h,rtl:/rtl/i.test(Q),eventNS:".selectize"+(++c.count),highlightedValue:null,isOpen:false,isDisabled:false,isRequired:U.is("[required]"),isInvalid:false,isLocked:false,isFocused:false,isInputHidden:false,isSetup:false,isShiftDown:false,isCmdDown:false,isCtrlDown:false,ignoreFocus:false,ignoreBlur:false,ignoreHover:false,hasOptions:false,currentResults:null,lastValue:"",caretPos:0,loading:0,loadedSearches:{},$activeOption:null,$activeItems:[],optgroups:{},options:{},userOptions:{},items:[],renderCache:{},onSearchChange:R.loadThrottle===null?X.onSearchChange:g(X.onSearchChange,R.loadThrottle)});X.sifter=new m(this.options,{diacritics:R.diacritics});if(X.settings.options){for(S=0,P=X.settings.options.length;S").addClass(ac.wrapperClass).addClass(ad).addClass(ah);ae=v("
").addClass(ac.inputClass).addClass("items").appendTo(aa);Q=v('').appendTo(ae).attr("tabindex",R.is(":disabled")?"-1":V.tabIndex);ab=v(ac.dropdownParent||aa);S=v("
").addClass(ac.dropdownClass).addClass(ah).hide().appendTo(ab);W=v("
").addClass(ac.dropdownContentClass).appendTo(S);if(Z=R.attr("id")){Q.attr("id",Z+"-selectized");v("label[for='"+Z+"']").attr("for",Z+"-selectized")}if(V.settings.copyClassesToDropdown){S.addClass(ad)}aa.css({width:R[0].style.width});if(V.plugins.names.length){X="plugin-"+V.plugins.names.join(" plugin-");aa.addClass(X);S.addClass(X)}if((ac.maxItems===null||ac.maxItems>1)&&V.tagType===k){R.attr("multiple","multiple")}if(V.settings.placeholder){Q.attr("placeholder",ac.placeholder)}if(!V.settings.splitOn&&V.settings.delimiter){var P=V.settings.delimiter.replace(/[-\/\\^$*+?.()|[\]{}]/g,"\\$&");V.settings.splitOn=new RegExp("\\s*"+P+"+\\s*")}if(R.attr("autocorrect")){Q.attr("autocorrect",R.attr("autocorrect"))}if(R.attr("autocapitalize")){Q.attr("autocapitalize",R.attr("autocapitalize"))}V.$wrapper=aa;V.$control=ae;V.$control_input=Q;V.$dropdown=S;V.$dropdown_content=W;S.on("mouseenter","[data-selectable]",function(){return V.onOptionHover.apply(V,arguments)});S.on("mousedown click","[data-selectable]",function(){return V.onOptionSelect.apply(V,arguments)});A(ae,"mousedown","*:not(input)",function(){return V.onItemSelect.apply(V,arguments)});L(Q);ae.on({mousedown:function(){return V.onMouseDown.apply(V,arguments)},click:function(){return V.onClick.apply(V,arguments)}});Q.on({mousedown:function(ai){ai.stopPropagation()},keydown:function(){return V.onKeyDown.apply(V,arguments)},keyup:function(){return V.onKeyUp.apply(V,arguments)},keypress:function(){return V.onKeyPress.apply(V,arguments)},resize:function(){V.positionDropdown.apply(V,[])},blur:function(){return V.onBlur.apply(V,arguments)},focus:function(){V.ignoreBlur=false;return V.onFocus.apply(V,arguments)},paste:function(){return V.onPaste.apply(V,arguments)}});ag.on("keydown"+Y,function(ai){V.isCmdDown=ai[b?"metaKey":"ctrlKey"];V.isCtrlDown=ai[b?"altKey":"ctrlKey"];V.isShiftDown=ai.shiftKey});ag.on("keyup"+Y,function(ai){if(ai.keyCode===e){V.isCtrlDown=false}if(ai.keyCode===j){V.isShiftDown=false}if(ai.keyCode===J){V.isCmdDown=false}});ag.on("mousedown"+Y,function(ai){if(V.isFocused){if(ai.target===V.$dropdown[0]||ai.target.parentNode===V.$dropdown[0]){return false}if(!V.$control.has(ai.target).length&&ai.target!==V.$control[0]){V.blur(ai.target)}}});U.on(["scroll"+Y,"resize"+Y].join(" "),function(){if(V.isOpen){V.positionDropdown.apply(V,arguments)}});U.on("mousemove"+Y,function(){V.ignoreHover=false});this.revertSettings={$children:R.children().detach(),tabindex:R.attr("tabindex")};R.attr("tabindex",-1).hide().after(V.$wrapper);if(v.isArray(ac.items)){V.setValue(ac.items);delete ac.items}if(C){R.on("invalid"+Y,function(ai){ai.preventDefault();V.isInvalid=true;V.refreshState()})}V.updateOriginalInput();V.refreshItems();V.refreshState();V.updatePlaceholder();V.isSetup=true;if(R.is(":disabled")){V.disable()}V.on("change",this.onChange);R.data("selectize",V);R.addClass("selectized");V.trigger("initialize");if(ac.preload===true){V.onSearchChange("")}},setupTemplates:function(){var Q=this;var P=Q.settings.labelField;var R=Q.settings.optgroupLabelField;var S={optgroup:function(T){return'
'+T.html+"
"},optgroup_header:function(U,T){return'
'+T(U[R])+"
"},option:function(U,T){return'
'+T(U[P])+"
"},item:function(U,T){return'
'+T(U[P])+"
"},option_create:function(U,T){return'
Add '+T(U.input)+"
"}};Q.settings.render=v.extend({},S,Q.settings.render)},setupCallbacks:function(){var P,Q,R={initialize:"onInitialize",change:"onChange",item_add:"onItemAdd",item_remove:"onItemRemove",clear:"onClear",option_add:"onOptionAdd",option_remove:"onOptionRemove",option_clear:"onOptionClear",optgroup_add:"onOptionGroupAdd",optgroup_remove:"onOptionGroupRemove",optgroup_clear:"onOptionGroupClear",dropdown_open:"onDropdownOpen",dropdown_close:"onDropdownClose",type:"onType",load:"onLoad",focus:"onFocus",blur:"onBlur"};for(P in R){if(R.hasOwnProperty(P)){Q=this.settings[R[P]];if(Q){this.on(P,Q)}}}},onClick:function(Q){var P=this;if(!P.isFocused){P.focus();Q.preventDefault()}},onMouseDown:function(S){var Q=this;var R=S.isDefaultPrevented();var P=v(S.target);if(Q.isFocused){if(S.target!==Q.$control_input[0]){if(Q.settings.mode==="single"){Q.isOpen?Q.close():Q.open()}else{if(!R){Q.setActiveItem(null)}}return false}}else{if(!R){window.setTimeout(function(){Q.focus()},0)}}},onChange:function(){this.$input.trigger("change")},onPaste:function(Q){var P=this;if(P.isFull()||P.isInputHidden||P.isLocked){Q.preventDefault();return}if(P.settings.splitOn){setTimeout(function(){var S=P.$control_input.val();if(!S.match(P.settings.splitOn)){return}var R=v.trim(S).split(P.settings.splitOn);for(var T=0,U=R.length;TR){Q=P;P=R;R=Q}for(S=P;S<=R;S++){Y=Z.$control[0].childNodes[S];if(Z.$activeItems.indexOf(Y)===-1){v(Y).addClass("active");Z.$activeItems.push(Y)}}U.preventDefault()}else{if((T==="mousedown"&&Z.isCtrlDown)||(T==="keydown"&&this.isShiftDown)){if(W.hasClass("active")){X=Z.$activeItems.indexOf(W[0]);Z.$activeItems.splice(X,1);W.removeClass("active")}else{Z.$activeItems.push(W.addClass("active")[0])}}else{v(Z.$activeItems).removeClass("active");Z.$activeItems=[W.addClass("active")[0]]}}Z.hideInput();if(!this.isFocused){Z.focus()}},setActiveOption:function(P,V,R){var Q,W,U;var T,S;var X=this;if(X.$activeOption){X.$activeOption.removeClass("active")}X.$activeOption=null;P=v(P);if(!P.length){return}X.$activeOption=P.addClass("active");if(V||!O(V)){Q=X.$dropdown_content.height();W=X.$activeOption.outerHeight(true);V=X.$dropdown_content.scrollTop()||0;U=X.$activeOption.offset().top-X.$dropdown_content.offset().top+V;T=U;S=U-Q+W;if(U+W>Q+V){X.$dropdown_content.stop().animate({scrollTop:S},R?X.settings.scrollDuration:0)}else{if(U=0;S--){if(V.items.indexOf(y(X.items[S].id))!==-1){X.items.splice(S,1)}}}return X},refreshOptions:function(Z){var ag,af,ae,ac,aj,P,V,ah,R,ad,T,ai,U;var S,Y,aa;if(typeof Z==="undefined"){Z=true}var X=this;var Q=v.trim(X.$control_input.val());var ab=X.search(Q);var W=X.$dropdown_content;var ak=X.$activeOption&&y(X.$activeOption.attr("data-value"));ac=ab.items.length;if(typeof X.settings.maxOptions==="number"){ac=Math.min(ac,X.settings.maxOptions)}aj={};P=[];for(ag=0;ag0||U;if(X.hasOptions){if(ab.items.length>0){Y=ak&&X.getOption(ak);if(Y&&Y.length){S=Y}else{if(X.settings.mode==="single"&&X.items.length){S=X.getOption(X.items[0])}}if(!S||!S.length){if(aa&&!X.settings.addPrecedence){S=X.getAdjacentOption(aa,1)}else{S=W.find("[data-selectable]:first")}}}else{S=aa}X.setActiveOption(S);if(Z&&!X.isOpen){X.open()}}else{X.setActiveOption(null);if(Z&&X.isOpen){X.close()}}},addOption:function(S){var Q,T,R,P=this;if(v.isArray(S)){for(Q=0,T=S.length;Q=0&&Q0);P.$control_input.data("grow",!Q&&!R)},isFull:function(){return this.settings.maxItems!==null&&this.items.length>=this.settings.maxItems},updateOriginalInput:function(T){var S,U,R,Q,P=this;T=T||{};if(P.tagType===k){R=[];for(S=0,U=P.items.length;S'+D(Q)+"")}if(!R.length&&!this.$input.attr("multiple")){R.push('')}P.$input.html(R.join(""))}else{P.$input.val(P.getValue());P.$input.attr("value",P.$input.val())}if(P.isSetup){if(!T.silent){P.trigger("change",P.$input.val())}}},updatePlaceholder:function(){if(!this.settings.placeholder){return}var P=this.$control_input;if(this.items.length){P.removeAttr("placeholder")}else{P.attr("placeholder",this.settings.placeholder)}P.triggerHandler("update",{force:true})},open:function(){var P=this;if(P.isLocked||P.isOpen||(P.settings.mode==="multi"&&P.isFull())){return}P.focus();P.isOpen=true;P.refreshState();P.$dropdown.css({visibility:"hidden",display:"block"});P.positionDropdown();P.$dropdown.css({visibility:"visible"});P.trigger("dropdown_open",P.$dropdown)},close:function(){var P=this;var Q=P.isOpen;if(P.settings.mode==="single"&&P.items.length){P.hideInput();P.$control_input.blur()}P.isOpen=false;P.$dropdown.hide();P.setActiveOption(null);P.refreshState();if(Q){P.trigger("dropdown_close",P.$dropdown)}},positionDropdown:function(){var P=this.$control;var Q=this.settings.dropdownParent==="body"?P.offset():P.position();Q.top+=P.outerHeight(true);this.$dropdown.css({width:P.outerWidth(),top:Q.top,left:Q.left})},clear:function(Q){var P=this;if(!P.items.length){return}P.$control.children(":not(input)").remove();P.items=[];P.lastQuery=null;P.setCaret(0);P.setActiveItem(null);P.updatePlaceholder();P.updateOriginalInput({silent:Q});P.refreshState();P.showInput();P.trigger("clear")},insertAtCaret:function(P){var Q=Math.min(this.caretPos,this.items.length);if(Q===0){this.$control.prepend(P)}else{v(this.$control[0].childNodes[Q]).before(P)}this.setCaret(Q+1)},deleteSelection:function(S){var Q,P,W,X,Y,U,T,V,R;var Z=this;W=(S&&S.keyCode===o)?-1:1;X=l(Z.$control_input[0]);if(Z.$activeOption&&!Z.settings.hideSelected){T=Z.getAdjacentOption(Z.$activeOption,-1).attr("data-value")}Y=[];if(Z.$activeItems.length){R=Z.$control.children(".active:"+(W>0?"last":"first"));U=Z.$control.children(":not(input)").index(R);if(W>0){U++}for(Q=0,P=Z.$activeItems.length;Q0&&X.start===Z.$control_input.val().length){Y.push(Z.items[Z.caretPos])}}}}if(!Y.length||(typeof Z.settings.onDelete==="function"&&Z.settings.onDelete.apply(Z,[Y])===false)){return false}if(typeof U!=="undefined"){Z.setCaret(U)}while(Y.length){Z.removeItem(Y.pop())}Z.showInput();Z.positionDropdown();Z.refreshOptions(true);if(T){V=Z.getOption(T);if(V.length){Z.setActiveOption(V)}}return true},advanceSelection:function(T,Q){var R,U,V,W,S,P;var X=this;if(T===0){return}if(X.rtl){T*=-1}R=T>0?"last":"first";U=l(X.$control_input[0]);if(X.isFocused&&!X.isInputHidden){W=X.$control_input.val().length;S=T<0?U.start===0&&U.length===0:U.start===W;if(S&&!W){X.advanceCaret(T,Q)}}else{P=X.$control.children(".active:"+R);if(P.length){V=X.$control.children(":not(input)").index(P);X.setActiveItem(null);X.setCaret(T>0?V+1:V)}}},advanceCaret:function(T,S){var P=this,R,Q;if(T===0){return}R=T>0?"next":"prev";if(P.isShiftDown){Q=P.$control_input[R]();if(Q.length){P.hideInput();P.setActiveItem(Q);S&&S.preventDefault()}}else{P.setCaret(P.caretPos+T)}},setCaret:function(S){var Q=this;if(Q.settings.mode==="single"){S=Q.items.length}else{S=Math.max(0,Math.min(Q.items.length,S))}if(!Q.isPending){var R,V,T,P,U;P=Q.$control.children(":not(input)");for(R=0,V=P.length;R
'+R.title+'×
')}},Q);P.setup=(function(){var R=P.setup;return function(){R.apply(P,arguments);P.$dropdown_header=v(Q.html(Q));P.$dropdown.prepend(P.$dropdown_header)}})()});c.define("optgroup_columns",function(R){var Q=this;R=v.extend({equalizeWidth:true,equalizeHeight:true},R);this.getAdjacentOption=function(W,V){var T=W.closest("[data-group]").find("[data-selectable]");var U=T.index(W)+V;return U>=0&&U
';V=V.firstChild;U.body.appendChild(V);T=P.width=V.offsetWidth-V.clientWidth;U.body.removeChild(V)}return T};var S=function(){var U,Z,T,V,Y,X,W;W=v("[data-group]",Q.$dropdown_content);Z=W.length;if(!Z||!Q.$dropdown_content.width()){return}if(R.equalizeHeight){T=0;for(U=0;U1){Y=X-V*(Z-1);W.eq(Z-1).css({width:Y})}}};if(R.equalizeHeight||R.equalizeWidth){s.after(this,"positionDropdown",S);s.after(this,"refreshOptions",S)}});c.define("remove_button",function(P){P=v.extend({label:"×",title:"Remove",className:"remove",append:true},P);var Q=function(W,U){U.className="remove-single";var T=W;var V=''+U.label+"";var S=function(X,Y){return X+Y};W.setup=(function(){var X=T.setup;return function(){if(U.append){var aa=v(T.$input.context).attr("id");var Z=v("#"+aa);var Y=T.settings.render.item;T.settings.render.item=function(ab){return S(Y.apply(W,arguments),V)}}X.apply(W,arguments);W.$control.on("click","."+U.className,function(ab){ab.preventDefault();if(T.isLocked){return}T.clear()})}})()};var R=function(W,U){var T=W;var V=''+U.label+"";var S=function(X,Y){var Z=X.search(/(<\/[^>]+>\s*)$/);return X.substring(0,Z)+Y+X.substring(Z)};W.setup=(function(){var X=T.setup;return function(){if(U.append){var Y=T.settings.render.item;T.settings.render.item=function(Z){return S(Y.apply(W,arguments),V)}}X.apply(W,arguments);W.$control.on("click","."+U.className,function(aa){aa.preventDefault();if(T.isLocked){return}var Z=v(aa.currentTarget).parent();T.setActiveItem(Z);if(T.deleteSelection()){T.setCaret(T.items.length)}})}})()};if(this.settings.mode==="single"){Q(this,P);return}else{R(this,P)}});c.define("restore_on_backspace",function(Q){var P=this;Q.text=Q.text||function(R){return R[this.settings.labelField]};this.onKeyDown=(function(){var R=P.onKeyDown;return function(U){var S,T;if(U.keyCode===o&&this.$control_input.val()===""&&!this.$activeItems.length){S=this.caretPos-1;if(S>=0&&S