(function(k,u){"function"===typeof define&&define.amd?define(["jquery","sifter","microplugin"],u):"object"===typeof exports?module.exports=u(require("jquery"),require("sifter"),require("microplugin")):k.Selectize=u(k.jQuery,k.Sifter,k.MicroPlugin)})(this,function(k,u,F){var G=function(a,b){if("string"!==typeof b||b.length){var c="string"===typeof b?RegExp(b,"i"):b,d=function(a){var b=0;if(3===a.nodeType){var h=a.data.search(c);if(0<=h&&0<a.data.length){var g=a.data.match(c),b=document.createElement("span");
b.className="highlight";a=a.splitText(h);a.splitText(g[0].length);h=a.cloneNode(!0);b.appendChild(h);a.parentNode.replaceChild(b,a);b=1}}else if(1===a.nodeType&&a.childNodes&&!/(script|style)/i.test(a.tagName))for(h=0;h<a.childNodes.length;++h)h+=d(a.childNodes[h]);return b};return a.each(function(){d(this)})}},y=function(){};y.prototype={on:function(a,b){this._events=this._events||{};this._events[a]=this._events[a]||[];this._events[a].push(b)},off:function(a,b){var c=arguments.length;if(0===c)return delete this._events;
if(1===c)return delete this._events[a];this._events=this._events||{};!1!==a in this._events&&this._events[a].splice(this._events[a].indexOf(b),1)},trigger:function(a){this._events=this._events||{};if(!1!==a in this._events)for(var b=0;b<this._events[a].length;b++)this._events[a][b].apply(this,Array.prototype.slice.call(arguments,1))}};y.mixin=function(a){for(var b=["on","off","trigger"],c=0;c<b.length;c++)a.prototype[b[c]]=y.prototype[b[c]]};var v=/Mac/.test(navigator.userAgent),H=v?91:17,I=v?18:
17,B=!/android/i.test(window.navigator.userAgent)&&!!document.createElement("form").validity,r=function(a){return"undefined"===typeof a||null===a?null:"boolean"===typeof a?a?"1":"0":a+""},w=function(a){return(a+"").replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;")},C=function(a){return(a+"").replace(/\$/g,"$$$$")},D={before:function(a,b,c){var d=a[b];a[b]=function(){c.apply(a,arguments);return d.apply(a,arguments)}},after:function(a,b,c){var d=a[b];a[b]=function(){var b=
d.apply(a,arguments);c.apply(a,arguments);return b}}},J=function(a){var b=!1;return function(){b||(b=!0,a.apply(this,arguments))}},K=function(a,b){var c;return function(){var d=this,e=arguments;window.clearTimeout(c);c=window.setTimeout(function(){a.apply(d,e)},b)}},E=function(a,b,c){var d,e=a.trigger,f={};a.trigger=function(){var c=arguments[0];if(-1!==b.indexOf(c))f[c]=arguments;else return e.apply(a,arguments)};c.apply(a,[]);a.trigger=e;for(d in f)f.hasOwnProperty(d)&&e.apply(a,f[d])},L=function(a,
b,c,d){a.on(b,c,function(b){for(var c=b.target;c&&c.parentNode!==a[0];)c=c.parentNode;b.currentTarget=c;return d.apply(this,[b])})},z=function(a){var b={};if("selectionStart"in a)b.start=a.selectionStart,b.length=a.selectionEnd-b.start;else if(document.selection){a.focus();var c=document.selection.createRange(),d=document.selection.createRange().text.length;c.moveStart("character",-a.value.length);b.start=c.text.length-d;b.length=d}return b},M=function(a){var b=null,c=function(c,e){var f,h,g;c=c||
window.event||{};e=e||{};if(!c.metaKey&&!c.altKey&&(e.force||!1!==a.data("grow"))){f=a.val();c.type&&"keydown"===c.type.toLowerCase()&&(h=c.keyCode,g=97<=h&&122>=h||65<=h&&90>=h||48<=h&&57>=h||32===h,46===h||8===h?(g=z(a[0]),g.length?f=f.substring(0,g.start)+f.substring(g.start+g.length):8===h&&g.start?f=f.substring(0,g.start-1)+f.substring(g.start+1):46===h&&"undefined"!==typeof g.start&&(f=f.substring(0,g.start)+f.substring(g.start+1))):g&&(h=c.shiftKey,g=String.fromCharCode(c.keyCode),g=h?g.toUpperCase():
g.toLowerCase(),f+=g));h=a.attr("placeholder");!f&&h&&(f=h);if(f){f=k("<test>").css({position:"absolute",top:-99999,left:-99999,width:"auto",padding:0,whiteSpace:"pre"}).text(f).appendTo("body");h=["letterSpacing","fontSize","fontFamily","fontWeight","textTransform"];var m,n={};if(h){g=0;for(m=h.length;g<m;g++)n[h[g]]=a.css(h[g])}else n=a.css();f.css(n);h=f.width();f.remove();f=h}else f=0;f+=4;f!==b&&(b=f,a.width(f),a.triggerHandler("resize"))}};a.on("keydown keyup update blur",c);c()},q=function(a,
b){var c,d;c=a[0];c.selectize=this;d=(d=(d=window.getComputedStyle&&window.getComputedStyle(c,null))?d.getPropertyValue("direction"):c.currentStyle&&c.currentStyle.direction)||a.parents("[dir]:first").attr("dir")||"";k.extend(this,{order:0,settings:b,$input:a,tabIndex:a.attr("tabindex")||"",tagType:"select"===c.tagName.toLowerCase()?1:2,rtl:/rtl/i.test(d),eventNS:".selectize"+ ++q.count,highlightedValue:null,isOpen:!1,isDisabled:!1,isRequired:a.is("[required]"),isInvalid:!1,isLocked:!1,isFocused:!1,
isInputHidden:!1,isSetup:!1,isShiftDown:!1,isCmdDown:!1,isCtrlDown:!1,ignoreFocus:!1,ignoreBlur:!1,ignoreHover:!1,hasOptions:!1,currentResults:null,lastValue:"",caretPos:0,loading:0,loadedSearches:{},$activeOption:null,$activeItems:[],optgroups:{},options:{},userOptions:{},items:[],renderCache:{},onSearchChange:null===b.loadThrottle?this.onSearchChange:K(this.onSearchChange,b.loadThrottle)});this.sifter=new u(this.options,{diacritics:b.diacritics});if(this.settings.options){c=0;for(d=this.settings.options.length;c<
d;c++)this.registerOption(this.settings.options[c]);delete this.settings.options}if(this.settings.optgroups){c=0;for(d=this.settings.optgroups.length;c<d;c++)this.registerOptionGroup(this.settings.optgroups[c]);delete this.settings.optgroups}this.settings.mode=this.settings.mode||(1===this.settings.maxItems?"single":"multi");"boolean"!==typeof this.settings.hideSelected&&(this.settings.hideSelected="multi"===this.settings.mode);this.initializePlugins(this.settings.plugins);this.setupCallbacks();this.setupTemplates();
this.setup()};y.mixin(q);F.mixin(q);k.extend(q.prototype,{setup:function(){var a=this,b=a.settings,c=a.eventNS,d=k(window),e=k(document),f=a.$input,h,g,m,n,t,s;t=a.settings.mode;s=f.attr("class")||"";h=k("<div>").addClass(b.wrapperClass).addClass(s).addClass(t);g=k("<div>").addClass(b.inputClass).addClass("items").appendTo(h);m=k('<input type="text" autocomplete="off" />').appendTo(g).attr("tabindex",f.is(":disabled")?"-1":a.tabIndex);n=k(b.dropdownParent||h);n=k("<div>").addClass(b.dropdownClass).addClass(t).hide().appendTo(n);
t=k("<div>").addClass(b.dropdownContentClass).appendTo(n);a.settings.copyClassesToDropdown&&n.addClass(s);h.css({width:f[0].style.width});a.plugins.names.length&&(s="plugin-"+a.plugins.names.join(" plugin-"),h.addClass(s),n.addClass(s));(null===b.maxItems||1<b.maxItems)&&1===a.tagType&&f.attr("multiple","multiple");a.settings.placeholder&&m.attr("placeholder",b.placeholder);!a.settings.splitOn&&a.settings.delimiter&&(s=a.settings.delimiter.replace(/[-\/\\^$*+?.()|[\]{}]/g,"\\$&"),a.settings.splitOn=
RegExp("\\s*"+s+"+\\s*"));f.attr("autocorrect")&&m.attr("autocorrect",f.attr("autocorrect"));f.attr("autocapitalize")&&m.attr("autocapitalize",f.attr("autocapitalize"));a.$wrapper=h;a.$control=g;a.$control_input=m;a.$dropdown=n;a.$dropdown_content=t;n.on("mouseenter","[data-selectable]",function(){return a.onOptionHover.apply(a,arguments)});n.on("mousedown click","[data-selectable]",function(){return a.onOptionSelect.apply(a,arguments)});L(g,"mousedown","*:not(input)",function(){return a.onItemSelect.apply(a,
arguments)});M(m);g.on({mousedown:function(){return a.onMouseDown.apply(a,arguments)},click:function(){return a.onClick.apply(a,arguments)}});m.on({mousedown:function(a){a.stopPropagation()},keydown:function(){return a.onKeyDown.apply(a,arguments)},keyup:function(){return a.onKeyUp.apply(a,arguments)},keypress:function(){return a.onKeyPress.apply(a,arguments)},resize:function(){a.positionDropdown.apply(a,[])},blur:function(){return a.onBlur.apply(a,arguments)},focus:function(){a.ignoreBlur=!1;return a.onFocus.apply(a,
arguments)},paste:function(){return a.onPaste.apply(a,arguments)}});e.on("keydown"+c,function(b){a.isCmdDown=b[v?"metaKey":"ctrlKey"];a.isCtrlDown=b[v?"altKey":"ctrlKey"];a.isShiftDown=b.shiftKey});e.on("keyup"+c,function(b){b.keyCode===I&&(a.isCtrlDown=!1);16===b.keyCode&&(a.isShiftDown=!1);b.keyCode===H&&(a.isCmdDown=!1)});e.on("mousedown"+c,function(b){if(a.isFocused){if(b.target===a.$dropdown[0]||b.target.parentNode===a.$dropdown[0])return!1;!a.$control.has(b.target).length&&b.target!==a.$control[0]&&
a.blur(b.target)}});d.on(["scroll"+c,"resize"+c].join(" "),function(){a.isOpen&&a.positionDropdown.apply(a,arguments)});d.on("mousemove"+c,function(){a.ignoreHover=!1});this.revertSettings={$children:f.children().detach(),tabindex:f.attr("tabindex")};f.attr("tabindex",-1).hide().after(a.$wrapper);k.isArray(b.items)&&(a.setValue(b.items),delete b.items);if(B)f.on("invalid"+c,function(b){b.preventDefault();a.isInvalid=!0;a.refreshState()});a.updateOriginalInput();a.refreshItems();a.refreshState();a.updatePlaceholder();
a.isSetup=!0;f.is(":disabled")&&a.disable();a.on("change",this.onChange);f.data("selectize",a);f.addClass("selectized");a.trigger("initialize");if(!0===b.preload)a.onSearchChange("")},setupTemplates:function(){var a=this.settings.labelField,b=this.settings.optgroupLabelField;this.settings.render=k.extend({},{optgroup:function(a){return'<div class="optgroup">'+a.html+"</div>"},optgroup_header:function(a,d){return'<div class="optgroup-header">'+d(a[b])+"</div>"},option:function(b,d){return'<div class="option">'+
d(b[a])+"</div>"},item:function(b,d){return'<div class="item">'+d(b[a])+"</div>"},option_create:function(a,b){return'<div class="create">Add <strong>'+b(a.input)+"</strong>&hellip;</div>"}},this.settings.render)},setupCallbacks:function(){var a,b,c={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(a in c)if(c.hasOwnProperty(a)&&(b=this.settings[c[a]]))this.on(a,b)},onClick:function(a){this.isFocused||(this.focus(),a.preventDefault())},onMouseDown:function(a){var b=this,c=a.isDefaultPrevented();k(a.target);if(b.isFocused){if(a.target!==b.$control_input[0])return"single"===b.settings.mode?b.isOpen?b.close():b.open():c||b.setActiveItem(null),
!1}else c||window.setTimeout(function(){b.focus()},0)},onChange:function(){this.$input.trigger("change")},onPaste:function(a){var b=this;b.isFull()||b.isInputHidden||b.isLocked?a.preventDefault():b.settings.splitOn&&setTimeout(function(){for(var a=k.trim(b.$control_input.val()||"").split(b.settings.splitOn),d=0,e=a.length;d<e;d++)b.createItem(a[d])},0)},onKeyPress:function(a){if(this.isLocked)return a&&a.preventDefault();var b=String.fromCharCode(a.keyCode||a.which);if(this.settings.create&&"multi"===
this.settings.mode&&b===this.settings.delimiter)return this.createItem(),a.preventDefault(),!1},onKeyDown:function(a){if(this.isLocked)9!==a.keyCode&&a.preventDefault();else{switch(a.keyCode){case 65:if(this.isCmdDown){this.selectAll();return}break;case 27:this.isOpen&&(a.preventDefault(),a.stopPropagation(),this.close());return;case 78:if(!a.ctrlKey||a.altKey)break;case 40:if(!this.isOpen&&this.hasOptions)this.open();else if(this.$activeOption){this.ignoreHover=!0;var b=this.getAdjacentOption(this.$activeOption,
1);b.length&&this.setActiveOption(b,!0,!0)}a.preventDefault();return;case 80:if(!a.ctrlKey||a.altKey)break;case 38:this.$activeOption&&(this.ignoreHover=!0,b=this.getAdjacentOption(this.$activeOption,-1),b.length&&this.setActiveOption(b,!0,!0));a.preventDefault();return;case 13:this.isOpen&&this.$activeOption&&(this.onOptionSelect({currentTarget:this.$activeOption}),a.preventDefault());return;case 37:this.advanceSelection(-1,a);return;case 39:this.advanceSelection(1,a);return;case 9:this.settings.selectOnTab&&
(this.isOpen&&this.$activeOption)&&(this.onOptionSelect({currentTarget:this.$activeOption}),this.isFull()||a.preventDefault());this.settings.create&&this.createItem()&&a.preventDefault();return;case 8:case 46:this.deleteSelection(a);return}(this.isFull()||this.isInputHidden)&&!(v?a.metaKey:a.ctrlKey)&&a.preventDefault()}},onKeyUp:function(a){if(this.isLocked)return a&&a.preventDefault();a=this.$control_input.val()||"";this.lastValue!==a&&(this.lastValue=a,this.onSearchChange(a),this.refreshOptions(),
this.trigger("type",a))},onSearchChange:function(a){var b=this,c=b.settings.load;c&&!b.loadedSearches.hasOwnProperty(a)&&(b.loadedSearches[a]=!0,b.load(function(d){c.apply(b,[a,d])}))},onFocus:function(a){var b=this.isFocused;if(this.isDisabled)return this.blur(),a&&a.preventDefault(),!1;if(!this.ignoreFocus){this.isFocused=!0;if("focus"===this.settings.preload)this.onSearchChange("");b||this.trigger("focus");this.$activeItems.length||(this.showInput(),this.setActiveItem(null),this.refreshOptions(!!this.settings.openOnFocus));
this.refreshState()}},onBlur:function(a,b){var c=this;if(c.isFocused&&(c.isFocused=!1,!c.ignoreFocus))if(!c.ignoreBlur&&document.activeElement===c.$dropdown_content[0])c.ignoreBlur=!0,c.onFocus(a);else{var d=function(){c.close();c.setTextboxValue("");c.setActiveItem(null);c.setActiveOption(null);c.setCaret(c.items.length);c.refreshState();(b||document.body).focus();c.ignoreFocus=!1;c.trigger("blur")};c.ignoreFocus=!0;c.settings.create&&c.settings.createOnBlur?c.createItem(null,!1,d):d()}},onOptionHover:function(a){this.ignoreHover||
this.setActiveOption(a.currentTarget,!1)},onOptionSelect:function(a){var b,c=this;a.preventDefault&&(a.preventDefault(),a.stopPropagation());b=k(a.currentTarget);b.hasClass("create")?c.createItem(null,function(){c.settings.closeAfterSelect&&c.close()}):(b=b.attr("data-value"),"undefined"!==typeof b&&(c.lastQuery=null,c.setTextboxValue(""),c.addItem(b),c.settings.closeAfterSelect?c.close():!c.settings.hideSelected&&(a.type&&/mouse/.test(a.type))&&c.setActiveOption(c.getOption(b))))},onItemSelect:function(a){!this.isLocked&&
"multi"===this.settings.mode&&(a.preventDefault(),this.setActiveItem(a.currentTarget,a))},load:function(a){var b=this,c=b.$wrapper.addClass(b.settings.loadingClass);b.loading++;a.apply(b,[function(a){b.loading=Math.max(b.loading-1,0);a&&a.length&&(b.addOption(a),b.refreshOptions(b.isFocused&&!b.isInputHidden));b.loading||c.removeClass(b.settings.loadingClass);b.trigger("load",a)}])},setTextboxValue:function(a){var b=this.$control_input;b.val()!==a&&(b.val(a).triggerHandler("update"),this.lastValue=
a)},getValue:function(){return 1===this.tagType&&this.$input.attr("multiple")?this.items:this.items.join(this.settings.delimiter)},setValue:function(a,b){E(this,b?[]:["change"],function(){this.clear(b);this.addItems(a,b)})},setActiveItem:function(a,b){var c,d,e;if("single"!==this.settings.mode)if(a=k(a),a.length){c=b&&b.type.toLowerCase();if("mousedown"===c&&this.isShiftDown&&this.$activeItems.length){c=this.$control.children(".active:last");d=Array.prototype.indexOf.apply(this.$control[0].childNodes,
[c[0]]);c=Array.prototype.indexOf.apply(this.$control[0].childNodes,[a[0]]);d>c&&(e=d,d=c,c=e);for(;d<=c;d++)e=this.$control[0].childNodes[d],-1===this.$activeItems.indexOf(e)&&(k(e).addClass("active"),this.$activeItems.push(e));b.preventDefault()}else"mousedown"===c&&this.isCtrlDown||"keydown"===c&&this.isShiftDown?a.hasClass("active")?(c=this.$activeItems.indexOf(a[0]),this.$activeItems.splice(c,1),a.removeClass("active")):this.$activeItems.push(a.addClass("active")[0]):(k(this.$activeItems).removeClass("active"),
this.$activeItems=[a.addClass("active")[0]]);this.hideInput();this.isFocused||this.focus()}else k(this.$activeItems).removeClass("active"),this.$activeItems=[],this.isFocused&&this.showInput()},setActiveOption:function(a,b,c){var d,e,f;this.$activeOption&&this.$activeOption.removeClass("active");this.$activeOption=null;a=k(a);if(a.length&&(this.$activeOption=a.addClass("active"),b||"undefined"===typeof b))a=this.$dropdown_content.height(),d=this.$activeOption.outerHeight(!0),b=this.$dropdown_content.scrollTop()||
0,e=this.$activeOption.offset().top-this.$dropdown_content.offset().top+b,f=e-a+d,e+d>a+b?this.$dropdown_content.stop().animate({scrollTop:f},c?this.settings.scrollDuration:0):e<b&&this.$dropdown_content.stop().animate({scrollTop:e},c?this.settings.scrollDuration:0)},selectAll:function(){"single"!==this.settings.mode&&(this.$activeItems=Array.prototype.slice.apply(this.$control.children(":not(input)").addClass("active")),this.$activeItems.length&&(this.hideInput(),this.close()),this.focus())},hideInput:function(){this.setTextboxValue("");
this.$control_input.css({opacity:0,position:"absolute",left:this.rtl?1E4:-1E4});this.isInputHidden=!0},showInput:function(){this.$control_input.css({opacity:1,position:"relative",left:0});this.isInputHidden=!1},focus:function(){var a=this;a.isDisabled||(a.ignoreFocus=!0,a.$control_input[0].focus(),window.setTimeout(function(){a.ignoreFocus=!1;a.onFocus()},0))},blur:function(a){this.$control_input[0].blur();this.onBlur(null,a)},getScoreFunction:function(a){return this.sifter.getScoreFunction(a,this.getSearchOptions())},
getSearchOptions:function(){var a=this.settings,b=a.sortField;"string"===typeof b&&(b=[{field:b}]);return{fields:a.searchField,conjunction:a.searchConjunction,sort:b}},search:function(a){var b,c;b=this.settings;var d=this.getSearchOptions();if(b.score&&(c=this.settings.score.apply(this,[a]),"function"!==typeof c))throw Error('Selectize "score" setting must be a function that returns a function');a!==this.lastQuery?(this.lastQuery=a,this.currentResults=a=this.sifter.search(a,k.extend(d,{score:c}))):
a=k.extend(!0,{},this.currentResults);if(b.hideSelected)for(b=a.items.length-1;0<=b;b--)-1!==this.items.indexOf(r(a.items[b].id))&&a.items.splice(b,1);return a},refreshOptions:function(a){var b,c,d,e,f,h,g,m,n,t,s,p;"undefined"===typeof a&&(a=!0);var l=this;s=k.trim(l.$control_input.val());var x=l.search(s),A=l.$dropdown_content,q=l.$activeOption&&r(l.$activeOption.attr("data-value"));e=x.items.length;"number"===typeof l.settings.maxOptions&&(e=Math.min(e,l.settings.maxOptions));f={};h=[];for(b=0;b<
e;b++){g=l.options[x.items[b].id];m=l.render("option",g);g=g[l.settings.optgroupField]||"";n=k.isArray(g)?g:[g];c=0;for(d=n&&n.length;c<d;c++)g=n[c],l.optgroups.hasOwnProperty(g)||(g=""),f.hasOwnProperty(g)||(f[g]=[],h.push(g)),f[g].push(m)}this.settings.lockOptgroupOrder&&h.sort(function(a,b){return(l.optgroups[a].$order||0)-(l.optgroups[b].$order||0)});m=[];b=0;for(e=h.length;b<e;b++)g=h[b],l.optgroups.hasOwnProperty(g)&&f[g].length?(c=l.render("optgroup_header",l.optgroups[g])||"",c+=f[g].join(""),
m.push(l.render("optgroup",k.extend({},l.optgroups[g],{html:c})))):m.push(f[g].join(""));A.html(m.join(""));if(l.settings.highlight&&x.query.length&&x.tokens.length){b=0;for(e=x.tokens.length;b<e;b++)G(A,x.tokens[b].regex)}if(!l.settings.hideSelected){b=0;for(e=l.items.length;b<e;b++)l.getOption(l.items[b]).addClass("selected")}if(b=l.canCreate(s))A.prepend(l.render("option_create",{input:s})),p=k(A[0].childNodes[0]);l.hasOptions=0<x.items.length||b;if(l.hasOptions){if(0<x.items.length){if((s=q&&
l.getOption(q))&&s.length?t=s:"single"===l.settings.mode&&l.items.length&&(t=l.getOption(l.items[0])),!t||!t.length)t=p&&!l.settings.addPrecedence?l.getAdjacentOption(p,1):A.find("[data-selectable]:first")}else t=p;l.setActiveOption(t);a&&!l.isOpen&&l.open()}else l.setActiveOption(null),a&&l.isOpen&&l.close()},addOption:function(a){var b,c;if(k.isArray(a)){b=0;for(c=a.length;b<c;b++)this.addOption(a[b])}else if(b=this.registerOption(a))this.userOptions[b]=!0,this.lastQuery=null,this.trigger("option_add",
b,a)},registerOption:function(a){var b=r(a[this.settings.valueField]);if(!b||this.options.hasOwnProperty(b))return!1;a.$order=a.$order||++this.order;this.options[b]=a;return b},registerOptionGroup:function(a){var b=r(a[this.settings.optgroupValueField]);if(!b)return!1;a.$order=a.$order||++this.order;this.optgroups[b]=a;return b},addOptionGroup:function(a,b){b[this.settings.optgroupValueField]=a;(a=this.registerOptionGroup(b))&&this.trigger("optgroup_add",a,b)},removeOptionGroup:function(a){this.optgroups.hasOwnProperty(a)&&
(delete this.optgroups[a],this.renderCache={},this.trigger("optgroup_remove",a))},clearOptionGroups:function(){this.optgroups={};this.renderCache={};this.trigger("optgroup_clear")},updateOption:function(a,b){var c,d,e;a=r(a);c=r(b[this.settings.valueField]);if(null!==a&&this.options.hasOwnProperty(a)){if("string"!==typeof c)throw Error("Value must be set in option data");e=this.options[a].$order;c!==a&&(delete this.options[a],d=this.items.indexOf(a),-1!==d&&this.items.splice(d,1,c));b.$order=b.$order||
e;this.options[c]=b;d=this.renderCache.item;e=this.renderCache.option;d&&(delete d[a],delete d[c]);e&&(delete e[a],delete e[c]);-1!==this.items.indexOf(c)&&(c=this.getItem(a),d=k(this.render("item",b)),c.hasClass("active")&&d.addClass("active"),c.replaceWith(d));this.lastQuery=null;this.isOpen&&this.refreshOptions(!1)}},removeOption:function(a,b){a=r(a);var c=this.renderCache.item,d=this.renderCache.option;c&&delete c[a];d&&delete d[a];delete this.userOptions[a];delete this.options[a];this.lastQuery=
null;this.trigger("option_remove",a);this.removeItem(a,b)},clearOptions:function(){this.loadedSearches={};this.userOptions={};this.renderCache={};this.options=this.sifter.items={};this.lastQuery=null;this.trigger("option_clear");this.clear()},getOption:function(a){return this.getElementWithValue(a,this.$dropdown_content.find("[data-selectable]"))},getAdjacentOption:function(a,b){var c=this.$dropdown.find("[data-selectable]"),d=c.index(a)+b;return 0<=d&&d<c.length?c.eq(d):k()},getElementWithValue:function(a,
b){a=r(a);if("undefined"!==typeof a&&null!==a)for(var c=0,d=b.length;c<d;c++)if(b[c].getAttribute("data-value")===a)return k(b[c]);return k()},getItem:function(a){return this.getElementWithValue(a,this.$control.children())},addItems:function(a,b){for(var c=k.isArray(a)?a:[a],d=0,e=c.length;d<e;d++)this.isPending=d<e-1,this.addItem(c[d],b)},addItem:function(a,b){E(this,b?[]:["change"],function(){var c,d,e,f=this.settings.mode;a=r(a);-1!==this.items.indexOf(a)?"single"===f&&this.close():this.options.hasOwnProperty(a)&&
("single"===f&&this.clear(b),"multi"===f&&this.isFull()||(c=k(this.render("item",this.options[a])),e=this.isFull(),this.items.splice(this.caretPos,0,a),this.insertAtCaret(c),(!this.isPending||!e&&this.isFull())&&this.refreshState(),this.isSetup&&(e=this.$dropdown_content.find("[data-selectable]"),this.isPending||(d=this.getOption(a),d=this.getAdjacentOption(d,1).attr("data-value"),this.refreshOptions(this.isFocused&&"single"!==f),d&&this.setActiveOption(this.getOption(d))),!e.length||this.isFull()?
this.close():this.positionDropdown(),this.updatePlaceholder(),this.trigger("item_add",a,c),this.updateOriginalInput({silent:b}))))})},removeItem:function(a,b){var c,d,e;c="object"===typeof a?a:this.getItem(a);a=r(c.attr("data-value"));d=this.items.indexOf(a);-1!==d&&(c.remove(),c.hasClass("active")&&(e=this.$activeItems.indexOf(c[0]),this.$activeItems.splice(e,1)),this.items.splice(d,1),this.lastQuery=null,!this.settings.persist&&this.userOptions.hasOwnProperty(a)&&this.removeOption(a,b),d<this.caretPos&&
this.setCaret(this.caretPos-1),this.refreshState(),this.updatePlaceholder(),this.updateOriginalInput({silent:b}),this.positionDropdown(),this.trigger("item_remove",a,c))},createItem:function(a,b){var c=this,d=c.caretPos;a=a||k.trim(c.$control_input.val()||"");var e=arguments[arguments.length-1];"function"!==typeof e&&(e=function(){});"boolean"!==typeof b&&(b=!0);if(!c.canCreate(a))return e(),!1;c.lock();var f="function"===typeof c.settings.create?this.settings.create:function(a){var b={};b[c.settings.labelField]=
a;b[c.settings.valueField]=a;return b},h=J(function(a){c.unlock();if(!a||"object"!==typeof a)return e();var f=r(a[c.settings.valueField]);if("string"!==typeof f)return e();c.setTextboxValue("");c.addOption(a);c.setCaret(d);c.addItem(f);c.refreshOptions(b&&"single"!==c.settings.mode);e(a)}),f=f.apply(this,[a,h]);"undefined"!==typeof f&&h(f);return!0},refreshItems:function(){this.lastQuery=null;this.isSetup&&this.addItem(this.items);this.refreshState();this.updateOriginalInput()},refreshState:function(){this.isRequired&&
(this.items.length&&(this.isInvalid=!1),this.$control_input.prop("required",void 0));this.refreshClasses()},refreshClasses:function(){var a=this.isFull(),b=this.isLocked;this.$wrapper.toggleClass("rtl",this.rtl);this.$control.toggleClass("focus",this.isFocused).toggleClass("disabled",this.isDisabled).toggleClass("required",this.isRequired).toggleClass("invalid",this.isInvalid).toggleClass("locked",b).toggleClass("full",a).toggleClass("not-full",!a).toggleClass("input-active",this.isFocused&&!this.isInputHidden).toggleClass("dropdown-active",
this.isOpen).toggleClass("has-options",!k.isEmptyObject(this.options)).toggleClass("has-items",0<this.items.length);this.$control_input.data("grow",!a&&!b)},isFull:function(){return null!==this.settings.maxItems&&this.items.length>=this.settings.maxItems},updateOriginalInput:function(a){var b,c,d,e;a=a||{};if(1===this.tagType){d=[];b=0;for(c=this.items.length;b<c;b++)e=this.options[this.items[b]][this.settings.labelField]||"",d.push('<option value="'+w(this.items[b])+'" selected="selected">'+w(e)+
"</option>");!d.length&&!this.$input.attr("multiple")&&d.push('<option value="" selected="selected"></option>');this.$input.html(d.join(""))}else this.$input.val(this.getValue()),this.$input.attr("value",this.$input.val());this.isSetup&&(a.silent||this.trigger("change",this.$input.val()))},updatePlaceholder:function(){if(this.settings.placeholder){var a=this.$control_input;this.items.length?a.removeAttr("placeholder"):a.attr("placeholder",this.settings.placeholder);a.triggerHandler("update",{force:!0})}},
open:function(){if(!this.isLocked&&!(this.isOpen||"multi"===this.settings.mode&&this.isFull()))this.focus(),this.isOpen=!0,this.refreshState(),this.$dropdown.css({visibility:"hidden",display:"block"}),this.positionDropdown(),this.$dropdown.css({visibility:"visible"}),this.trigger("dropdown_open",this.$dropdown)},close:function(){var a=this.isOpen;"single"===this.settings.mode&&this.items.length&&this.hideInput();this.isOpen=!1;this.$dropdown.hide();this.setActiveOption(null);this.refreshState();a&&
this.trigger("dropdown_close",this.$dropdown)},positionDropdown:function(){var a=this.$control,b="body"===this.settings.dropdownParent?a.offset():a.position();b.top+=a.outerHeight(!0);this.$dropdown.css({width:a.outerWidth(),top:b.top,left:b.left})},clear:function(a){this.items.length&&(this.$control.children(":not(input)").remove(),this.items=[],this.lastQuery=null,this.setCaret(0),this.setActiveItem(null),this.updatePlaceholder(),this.updateOriginalInput({silent:a}),this.refreshState(),this.showInput(),
this.trigger("clear"))},insertAtCaret:function(a){var b=Math.min(this.caretPos,this.items.length);0===b?this.$control.prepend(a):k(this.$control[0].childNodes[b]).before(a);this.setCaret(b+1)},deleteSelection:function(a){var b,c,d,e,f;b=a&&8===a.keyCode?-1:1;c=z(this.$control_input[0]);this.$activeOption&&!this.settings.hideSelected&&(f=this.getAdjacentOption(this.$activeOption,-1).attr("data-value"));d=[];if(this.$activeItems.length){e=this.$control.children(".active:"+(0<b?"last":"first"));e=this.$control.children(":not(input)").index(e);
0<b&&e++;b=0;for(c=this.$activeItems.length;b<c;b++)d.push(k(this.$activeItems[b]).attr("data-value"));a&&(a.preventDefault(),a.stopPropagation())}else if((this.isFocused||"single"===this.settings.mode)&&this.items.length)0>b&&0===c.start&&0===c.length?d.push(this.items[this.caretPos-1]):0<b&&c.start===this.$control_input.val().length&&d.push(this.items[this.caretPos]);if(!d.length||"function"===typeof this.settings.onDelete&&!1===this.settings.onDelete.apply(this,[d]))return!1;for("undefined"!==
typeof e&&this.setCaret(e);d.length;)this.removeItem(d.pop());this.showInput();this.positionDropdown();this.refreshOptions(!0);f&&(a=this.getOption(f),a.length&&this.setActiveOption(a));return!0},advanceSelection:function(a,b){var c,d;0!==a&&(this.rtl&&(a*=-1),c=0<a?"last":"first",d=z(this.$control_input[0]),this.isFocused&&!this.isInputHidden?(c=this.$control_input.val().length,(d=0>a?0===d.start&&0===d.length:d.start===c)&&!c&&this.advanceCaret(a,b)):(d=this.$control.children(".active:"+c),d.length&&
(d=this.$control.children(":not(input)").index(d),this.setActiveItem(null),this.setCaret(0<a?d+1:d))))},advanceCaret:function(a,b){var c;0!==a&&(c=0<a?"next":"prev",this.isShiftDown?(c=this.$control_input[c](),c.length&&(this.hideInput(),this.setActiveItem(c),b&&b.preventDefault())):this.setCaret(this.caretPos+a))},setCaret:function(a){a="single"===this.settings.mode?this.items.length:Math.max(0,Math.min(this.items.length,a));if(!this.isPending){var b,c,d,e;d=this.$control.children(":not(input)");
b=0;for(c=d.length;b<c;b++)e=k(d[b]).detach(),b<a?this.$control_input.before(e):this.$control.append(e)}this.caretPos=a},lock:function(){this.close();this.isLocked=!0;this.refreshState()},unlock:function(){this.isLocked=!1;this.refreshState()},disable:function(){this.$input.prop("disabled",!0);this.$control_input.prop("disabled",!0).prop("tabindex",-1);this.isDisabled=!0;this.lock()},enable:function(){this.$input.prop("disabled",!1);this.$control_input.prop("disabled",!1).prop("tabindex",this.tabIndex);
this.isDisabled=!1;this.unlock()},destroy:function(){var a=this.eventNS,b=this.revertSettings;this.trigger("destroy");this.off();this.$wrapper.remove();this.$dropdown.remove();this.$input.html("").append(b.$children).removeAttr("tabindex").removeClass("selectized").attr({tabindex:b.tabindex}).show();this.$control_input.removeData("grow");this.$input.removeData("selectize");k(window).off(a);k(document).off(a);k(document.body).off(a);delete this.$input[0].selectize},render:function(a,b){var c,d,e="",
f=!1,h=/^[\t \r\n]*<([a-z][a-z0-9\-_]*(?:\:[a-z][a-z0-9\-_]*)?)/i;if("option"===a||"item"===a)c=r(b[this.settings.valueField]),f=!!c;if(f&&("undefined"===typeof this.renderCache[a]&&(this.renderCache[a]={}),this.renderCache[a].hasOwnProperty(c)))return this.renderCache[a][c];e=this.settings.render[a].apply(this,[b,w]);if("option"===a||"option_create"===a)e=e.replace(h,"<$1 data-selectable");"optgroup"===a&&(d=b[this.settings.optgroupValueField]||"",e=e.replace(h,'<$1 data-group="'+C(w(d))+'"'));if("option"===
a||"item"===a)e=e.replace(h,'<$1 data-value="'+C(w(c||""))+'"');f&&(this.renderCache[a][c]=e);return e},clearCache:function(a){"undefined"===typeof a?this.renderCache={}:delete this.renderCache[a]},canCreate:function(a){if(!this.settings.create)return!1;var b=this.settings.createFilter;return a.length&&("function"!==typeof b||b.apply(this,[a]))&&("string"!==typeof b||RegExp(b).test(a))&&(!(b instanceof RegExp)||b.test(a))}});q.count=0;q.defaults={options:[],optgroups:[],plugins:[],delimiter:",",splitOn:null,
persist:!0,diacritics:!0,create:!1,createOnBlur:!1,createFilter:null,highlight:!0,openOnFocus:!0,maxOptions:1E3,maxItems:null,hideSelected:null,addPrecedence:!1,selectOnTab:!1,preload:!1,allowEmptyOption:!1,closeAfterSelect:!1,scrollDuration:60,loadThrottle:300,loadingClass:"loading",dataAttr:"data-data",optgroupField:"optgroup",valueField:"value",labelField:"text",optgroupLabelField:"label",optgroupValueField:"value",lockOptgroupOrder:!1,sortField:"$order",searchField:["text"],searchConjunction:"and",
mode:null,wrapperClass:"selectize-control",inputClass:"selectize-input",dropdownClass:"selectize-dropdown",dropdownContentClass:"selectize-dropdown-content",dropdownParent:null,copyClassesToDropdown:!0,render:{}};k.fn.selectize=function(a){var b=k.fn.selectize.defaults,c=k.extend({},b,a),d=c.dataAttr,e=c.labelField,f=c.valueField,h=c.optgroupField,g=c.optgroupLabelField,m=c.optgroupValueField,n=function(a,b){var p,l,n,q,w=b.options,u={},v=function(a){a=d&&a.attr(d);return"string"===typeof a&&a.length?
JSON.parse(a):null},y=function(a,d){a=k(a);var g=r(a.attr("value"));if(g||c.allowEmptyOption)if(u.hasOwnProperty(g)){if(d){var l=u[g][h];l?k.isArray(l)?l.push(d):u[g][h]=[l,d]:u[g][h]=d}}else l=v(a)||{},l[e]=l[e]||a.text(),l[f]=l[f]||g,l[h]=l[h]||d,u[g]=l,w.push(l),a.is(":selected")&&b.items.push(g)},z=function(a){var c,d,e;a=k(a);if(d=a.attr("label"))c=v(a)||{},c[g]=d,c[m]=d,b.optgroups.push(c);e=k("option",a);a=0;for(c=e.length;a<c;a++)y(e[a],d)};b.maxItems=a.attr("multiple")?null:1;q=a.children();
p=0;for(l=q.length;p<l;p++)n=q[p].tagName.toLowerCase(),"optgroup"===n?z(q[p]):"option"===n&&y(q[p])};return this.each(function(){if(!this.selectize){var g=k(this),h=this.tagName.toLowerCase(),p=g.attr("placeholder")||g.attr("data-placeholder");!p&&!c.allowEmptyOption&&(p=g.children('option[value=""]').text());p={placeholder:p,options:[],optgroups:[],items:[]};if("select"===h)n(g,p);else{var l,m,r;if(h=g.attr(d)){p.options=JSON.parse(h);h=0;for(l=p.options.length;h<l;h++)p.items.push(p.options[h][f])}else if(h=
k.trim(g.val()||""),c.allowEmptyOption||h.length){m=h.split(c.delimiter);h=0;for(l=m.length;h<l;h++)r={},r[e]=m[h],r[f]=m[h],p.options.push(r);p.items=m}}new q(g,k.extend(!0,{},b,p,a))}})};k.fn.selectize.defaults=q.defaults;k.fn.selectize.support={validity:B};q.define("drag_drop",function(a){if(!k.fn.sortable)throw Error('The "drag_drop" plugin requires jQuery UI "sortable".');if("multi"===this.settings.mode){var b=this;b.lock=function(){var a=b.lock;return function(){var d=b.$control.data("sortable");
d&&d.disable();return a.apply(b,arguments)}}();b.unlock=function(){var a=b.unlock;return function(){var d=b.$control.data("sortable");d&&d.enable();return a.apply(b,arguments)}}();b.setup=function(){var a=b.setup;return function(){a.apply(this,arguments);var d=b.$control.sortable({items:"[data-value]",forcePlaceholderSize:!0,disabled:b.isLocked,start:function(a,b){b.placeholder.css("width",b.helper.css("width"));d.css({overflow:"visible"})},stop:function(){d.css({overflow:"hidden"});var a=b.$activeItems?
b.$activeItems.slice():null,c=[];d.children("[data-value]").each(function(){c.push(k(this).attr("data-value"))});b.setValue(c);b.setActiveItem(a)}})}}()}});q.define("dropdown_header",function(a){var b=this;a=k.extend({title:"Untitled",headerClass:"selectize-dropdown-header",titleRowClass:"selectize-dropdown-header-title",labelClass:"selectize-dropdown-header-label",closeClass:"selectize-dropdown-header-close",html:function(a){return'<div class="'+a.headerClass+'"><div class="'+a.titleRowClass+'"><span class="'+
a.labelClass+'">'+a.title+'</span><a href="javascript:void(0)" class="'+a.closeClass+'">&times;</a></div></div>'}},a);b.setup=function(){var c=b.setup;return function(){c.apply(b,arguments);b.$dropdown_header=k(a.html(a));b.$dropdown.prepend(b.$dropdown_header)}}()});q.define("optgroup_columns",function(a){var b=this;a=k.extend({equalizeWidth:!0,equalizeHeight:!0},a);this.getAdjacentOption=function(a,b){var c=a.closest("[data-group]").find("[data-selectable]"),d=c.index(a)+b;return 0<=d&&d<c.length?
c.eq(d):k()};this.onKeyDown=function(){var a=b.onKeyDown;return function(c){var d,g;if(this.isOpen&&(37===c.keyCode||39===c.keyCode))b.ignoreHover=!0,g=this.$activeOption.closest("[data-group]"),d=g.find("[data-selectable]").index(this.$activeOption),g=37===c.keyCode?g.prev("[data-group]"):g.next("[data-group]"),g=g.find("[data-selectable]"),d=g.eq(Math.min(g.length-1,d)),d.length&&this.setActiveOption(d);else return a.apply(this,arguments)}}();var c=function(){var a,b=c.width,d=document;"undefined"===
typeof b&&(a=d.createElement("div"),a.innerHTML='<div style="width:50px;height:50px;position:absolute;left:-50px;top:-50px;overflow:auto;"><div style="width:1px;height:100px;"></div></div>',a=a.firstChild,d.body.appendChild(a),b=c.width=a.offsetWidth-a.clientWidth,d.body.removeChild(a));return b},d=function(){var d,f,h,g;g=k("[data-group]",b.$dropdown_content);if((f=g.length)&&b.$dropdown_content.width()){if(a.equalizeHeight){for(d=h=0;d<f;d++)h=Math.max(h,g.eq(d).height());g.css({height:h})}a.equalizeWidth&&
(h=b.$dropdown_content.innerWidth()-c(),d=Math.round(h/f),g.css({width:d}),1<f&&(d=h-d*(f-1),g.eq(f-1).css({width:d})))}};if(a.equalizeHeight||a.equalizeWidth)D.after(this,"positionDropdown",d),D.after(this,"refreshOptions",d)});q.define("remove_button",function(a){if("single"!==this.settings.mode){a=k.extend({label:"&times;",title:"Remove",className:"remove",append:!0},a);var b=this,c='<a href="javascript:void(0)" class="'+a.className+'" tabindex="-1" title="'+w(a.title)+'">'+a.label+"</a>";this.setup=
function(){var d=b.setup;return function(){if(a.append){var e=b.settings.render.item;b.settings.render.item=function(a){var b=e.apply(this,arguments),d=b.search(/(<\/[^>]+>\s*)$/);return b.substring(0,d)+c+b.substring(d)}}d.apply(this,arguments);this.$control.on("click","."+a.className,function(a){a.preventDefault();b.isLocked||(a=k(a.currentTarget).parent(),b.setActiveItem(a),b.deleteSelection()&&b.setCaret(b.items.length))})}}()}});q.define("restore_on_backspace",function(a){var b=this;a.text=a.text||
function(a){return a[this.settings.labelField]};this.onKeyDown=function(){var c=b.onKeyDown;return function(b){var e;if(8===b.keyCode&&(""===this.$control_input.val()&&!this.$activeItems.length)&&(e=this.caretPos-1,0<=e&&e<this.items.length)){e=this.options[this.items[e]];this.deleteSelection(b)&&(this.setTextboxValue(a.text.apply(this,[e])),this.refreshOptions(!0));b.preventDefault();return}return c.apply(this,arguments)}}()});return q});
