mobius1-selectr
Advanced tools
Comparing version 2.3.8 to 2.3.9
{ | ||
"name": "mobius1-selectr", | ||
"version": "2.3.8", | ||
"version": "2.3.9", | ||
"ignore": [ | ||
@@ -5,0 +5,0 @@ ".gitattributes", |
/*! | ||
* Selectr 2.3.8 | ||
* Selectr 2.3.9 | ||
* http://mobius.ovh/docs/selectr | ||
@@ -7,2 +7,2 @@ * | ||
*/ | ||
!function(e,t){"function"==typeof define&&define.amd?define([],t("Selectr")):"object"==typeof exports?module.exports=t("Selectr"):e.Selectr=t("Selectr")}(this,function(e){"use strict";function t(e,t){return e.hasOwnProperty(t)&&(!0===e[t]||e[t].length)}function i(e,t,i){e.parentNode?e.parentNode.parentNode||t.appendChild(e.parentNode):t.appendChild(e),a.removeClass(e,"excluded"),i||(e.innerHTML=e.textContent)}var s={defaultSelected:!0,width:"auto",disabled:!1,searchable:!0,clearable:!1,sortSelected:!1,allowDeselect:!1,closeOnScroll:!1,nativeDropdown:!1,placeholder:"Select an option...",taggable:!1,tagPlaceholder:"Enter a tag..."},n=function(){};n.prototype={on:function(e,t){this._events=this._events||{},this._events[e]=this._events[e]||[],this._events[e].push(t)},off:function(e,t){this._events=this._events||{},e in this._events!=!1&&this._events[e].splice(this._events[e].indexOf(t),1)},emit:function(e){if(this._events=this._events||{},e in this._events!=!1)for(var t=0;t<this._events[e].length;t++)this._events[e][t].apply(this,Array.prototype.slice.call(arguments,1))}},n.mixin=function(e){for(var t=["on","off","emit"],i=0;i<t.length;i++)"function"==typeof e?e.prototype[t[i]]=n.prototype[t[i]]:e[t[i]]=n.prototype[t[i]];return e};var a={extend:function(e,t){t=t||{};var i;for(i in e)e.hasOwnProperty(i)&&(t.hasOwnProperty(i)||(t[i]=e[i]));return t},each:function(e,t,i){if("[object Object]"===Object.prototype.toString.call(e))for(var s in e)Object.prototype.hasOwnProperty.call(e,s)&&t.call(i,s,e[s],e);else for(var n=0,a=e.length;n<a;n++)t.call(i,n,e[n],e)},createElement:function(e,t){var i=document,s=i.createElement(e);if(t&&"[object Object]"===Object.prototype.toString.call(t)){var n;for(n in t)if(n in s)s[n]=t[n];else if("html"===n)s.innerHTML=t[n];else if("text"===n){var a=i.createTextNode(t[n]);s.appendChild(a)}else s.setAttribute(n,t[n])}return s},hasClass:function(e,t){if(e)return e.classList?e.classList.contains(t):!!e.className&&!!e.className.match(new RegExp("(\\s|^)"+t+"(\\s|$)"))},addClass:function(e,t){a.hasClass(e,t)||(e.classList?e.classList.add(t):e.className=e.className.trim()+" "+t)},removeClass:function(e,t){a.hasClass(e,t)&&(e.classList?e.classList.remove(t):e.className=e.className.replace(new RegExp("(^|\\s)"+t.split(" ").join("|")+"(\\s|$)","gi")," "))},closest:function(e,t){return e&&e!==document.body&&(t(e)?e:a.closest(e.parentNode,t))},isInt:function(e){return"number"==typeof e&&isFinite(e)&&Math.floor(e)===e},debounce:function(e,t,i){var s;return function(){var n=this,a=arguments,l=function(){s=null,i||e.apply(n,a)},o=i&&!s;clearTimeout(s),s=setTimeout(l,t),o&&e.apply(n,a)}},rect:function(e,t){var i=window,s=e.getBoundingClientRect(),n=t?i.pageXOffset:0,a=t?i.pageYOffset:0;return{bottom:s.bottom+a,height:s.height,left:s.left+n,right:s.right+n,top:s.top+a,width:s.width}},includes:function(e,t){return e.indexOf(t)>-1},truncate:function(e){for(;e.firstChild;)e.removeChild(e.firstChild)}},l=function(){if(this.items.length){var e=document.createDocumentFragment();if(this.config.pagination){var t=this.pages.slice(0,this.pageIndex);a.each(t,function(t,s){a.each(s,function(t,s){i(s,e,this.customOption)},this)},this)}else a.each(this.items,function(t,s){i(s,e,this.customOption)},this);e.childElementCount&&(a.removeClass(this.items[this.navIndex],"active"),this.navIndex=e.querySelector(".selectr-option").idx,a.addClass(this.items[this.navIndex],"active")),this.tree.appendChild(e)}},o=function(e){var t=e.target;this.container.contains(t)||!this.opened&&!a.hasClass(this.container,"notice")||this.close()},h=function(e,t){t=t||e;var i=this.customOption?this.config.renderOption(t):e.textContent,s=a.createElement("li",{class:"selectr-option",html:i,role:"treeitem","aria-selected":!1});return s.idx=e.idx,this.items.push(s),e.defaultSelected&&this.defaultSelected.push(e.idx),e.disabled&&(s.disabled=!0,a.addClass(s,"disabled")),s},c=function(){this.requiresPagination=this.config.pagination&&this.config.pagination>0,t(this.config,"width")&&(a.isInt(this.config.width)?this.width=this.config.width+"px":"auto"===this.config.width?this.width="100%":a.includes(this.config.width,"%")&&(this.width=this.config.width)),this.container=a.createElement("div",{class:"selectr-container"}),this.config.customClass&&a.addClass(this.container,this.config.customClass),this.mobileDevice?a.addClass(this.container,"selectr-mobile"):a.addClass(this.container,"selectr-desktop"),this.el.tabIndex=-1,this.config.nativeDropdown||this.mobileDevice?a.addClass(this.el,"selectr-visible"):a.addClass(this.el,"selectr-hidden"),this.selected=a.createElement("div",{class:"selectr-selected",disabled:this.disabled,tabIndex:1,"aria-expanded":!1}),this.label=a.createElement(this.el.multiple?"ul":"span",{class:"selectr-label"});var e=a.createElement("div",{class:"selectr-options-container"});if(this.tree=a.createElement("ul",{class:"selectr-options",role:"tree","aria-hidden":!0,"aria-expanded":!1}),this.notice=a.createElement("div",{class:"selectr-notice"}),this.el.setAttribute("aria-hidden",!0),this.disabled&&(this.el.disabled=!0),this.el.multiple&&(a.addClass(this.label,"selectr-tags"),a.addClass(this.container,"multiple"),this.tags=[],this.selectedValues=[],this.selectedIndexes=[]),this.selected.appendChild(this.label),this.config.clearable&&(this.selectClear=a.createElement("button",{class:"selectr-clear",type:"button"}),this.container.appendChild(this.selectClear),a.addClass(this.container,"clearable")),this.config.taggable){var i=a.createElement("li",{class:"input-tag"});this.input=a.createElement("input",{class:"selectr-tag-input",placeholder:this.config.tagPlaceholder,tagIndex:0,autocomplete:"off",autocorrect:"off",autocapitalize:"off",spellcheck:"false",role:"textbox",type:"search"}),i.appendChild(this.input),this.label.appendChild(i),a.addClass(this.container,"taggable"),this.tagSeperators=[","],this.config.tagSeperators&&(this.tagSeperators=this.tagSeperators.concat(this.config.tagSeperators))}this.config.searchable&&(this.input=a.createElement("input",{class:"selectr-input",tagIndex:-1,autocomplete:"off",autocorrect:"off",autocapitalize:"off",spellcheck:"false",role:"textbox",type:"search"}),this.inputClear=a.createElement("button",{class:"selectr-input-clear",type:"button"}),this.inputContainer=a.createElement("div",{class:"selectr-input-container"}),this.inputContainer.appendChild(this.input),this.inputContainer.appendChild(this.inputClear),e.appendChild(this.inputContainer)),e.appendChild(this.notice),e.appendChild(this.tree),this.items=[],this.options=[],this.el.options.length&&(this.options=[].slice.call(this.el.options));var s=!1,n=0;if(this.el.children.length&&a.each(this.el.children,function(e,t){"OPTGROUP"===t.nodeName?(s=a.createElement("ul",{class:"selectr-optgroup",role:"group",html:"<li class='selectr-optgroup--label'>"+t.label+"</li>"}),a.each(t.children,function(e,t){t.idx=n,s.appendChild(h.call(this,t,s)),n++},this)):(t.idx=n,h.call(this,t),n++)},this),this.config.data&&Array.isArray(this.config.data)){this.data=[];var l,o=!1;s=!1,n=0,a.each(this.config.data,function(e,i){t(i,"children")?(o=a.createElement("optgroup",{label:i.text}),s=a.createElement("ul",{class:"selectr-optgroup",role:"group",html:"<li class='selectr-optgroup--label'>"+i.text+"</li>"}),a.each(i.children,function(e,i){(l=new Option(i.text,i.value,!1,i.hasOwnProperty("selected")&&!0===i.selected)).disabled=t(i,"disabled"),this.options.push(l),o.appendChild(l),l.idx=n,s.appendChild(h.call(this,l,i)),this.data[n]=i,n++},this)):((l=new Option(i.text,i.value,!1,i.hasOwnProperty("selected")&&!0===i.selected)).disabled=t(i,"disabled"),this.options.push(l),l.idx=n,h.call(this,l,i),this.data[n]=i,n++)},this)}this.setSelected(!0);var c;this.navIndex=0;for(var r=0;r<this.items.length;r++)if(c=this.items[r],!a.hasClass(c,"disabled")){a.addClass(c,"active"),this.navIndex=r;break}this.requiresPagination&&(this.pageIndex=1,this.paginate()),this.container.appendChild(this.selected),this.container.appendChild(e),this.placeEl=a.createElement("div",{class:"selectr-placeholder"}),this.setPlaceholder(),this.selected.appendChild(this.placeEl),this.disabled&&this.disable(),this.el.parentNode.insertBefore(this.container,this.el),this.container.appendChild(this.el)},r=function(e){if(e=e||window.event,this.items.length&&this.opened&&a.includes([13,38,40],e.which)){if(e.preventDefault(),13===e.which)return!(this.config.taggable&&this.input.value.length>0)&&this.change(this.navIndex);var t,i=this.items[this.navIndex];switch(e.which){case 38:t=0,this.navIndex>0&&this.navIndex--;break;case 40:t=1,this.navIndex<this.items.length-1&&this.navIndex++}for(this.navigating=!0;a.hasClass(this.items[this.navIndex],"disabled")||a.hasClass(this.items[this.navIndex],"excluded");)if(t?this.navIndex++:this.navIndex--,this.searching){if(this.navIndex>this.tree.lastElementChild.idx){this.navIndex=this.tree.lastElementChild.idx;break}if(this.navIndex<this.tree.firstElementChild.idx){this.navIndex=this.tree.firstElementChild.idx;break}}var s=a.rect(this.items[this.navIndex]);t?(0===this.navIndex?this.tree.scrollTop=0:s.top+s.height>this.optsRect.top+this.optsRect.height&&(this.tree.scrollTop=this.tree.scrollTop+(s.top+s.height-(this.optsRect.top+this.optsRect.height))),this.navIndex===this.tree.childElementCount-1&&this.requiresPagination&&u.call(this)):0===this.navIndex?this.tree.scrollTop=0:s.top-this.optsRect.top<0&&(this.tree.scrollTop=this.tree.scrollTop+(s.top-this.optsRect.top)),i&&a.removeClass(i,"active"),a.addClass(this.items[this.navIndex],"active")}else this.navigating=!1},d=function(e){var t,i=this,s=document.createDocumentFragment(),n=this.options[e.idx],l=this.data?this.data[e.idx]:n,o=this.customSelected?this.config.renderSelection(l):n.textContent,h=a.createElement("li",{class:"selectr-tag",html:o}),c=a.createElement("button",{class:"selectr-tag-remove",type:"button"});if(h.appendChild(c),h.idx=e.idx,h.tag=n.value,this.tags.push(h),this.config.sortSelected){var r=this.tags.slice();t=function(e,t){e.replace(/(\d+)|(\D+)/g,function(e,i,s){t.push([i||1/0,s||""])})},r.sort(function(e,s){var n,a,l=[],o=[];for(!0===i.config.sortSelected?(n=e.tag,a=s.tag):"text"===i.config.sortSelected&&(n=e.textContent,a=s.textContent),t(n,l),t(a,o);l.length&&o.length;){var h=l.shift(),c=o.shift(),r=h[0]-c[0]||h[1].localeCompare(c[1]);if(r)return r}return l.length-o.length}),a.each(r,function(e,t){s.appendChild(t)}),this.label.innerHTML=""}else s.appendChild(h);this.config.taggable?this.label.insertBefore(s,this.input.parentNode):this.label.appendChild(s)},p=function(e){var t=!1;a.each(this.tags,function(i,s){s.idx===e.idx&&(t=s)},this),t&&(this.label.removeChild(t),this.tags.splice(this.tags.indexOf(t),1))},u=function(){var e=this.tree;if(e.scrollTop>=e.scrollHeight-e.offsetHeight&&this.pageIndex<this.pages.length){var t=document.createDocumentFragment();a.each(this.pages[this.pageIndex],function(e,s){i(s,t,this.customOption)},this),e.appendChild(t),this.pageIndex++,this.emit("selectr.paginate",{items:this.items.length,total:this.data.length,page:this.pageIndex,pages:this.pages.length})}},f=function(){(this.config.searchable||this.config.taggable)&&(this.input.value=null,this.searching=!1,this.config.searchable&&a.removeClass(this.inputContainer,"active"),a.hasClass(this.container,"notice")&&(a.removeClass(this.container,"notice"),a.addClass(this.container,"open"),this.input.focus()),a.each(this.items,function(e,t){a.removeClass(t,"excluded"),this.customOption||(t.innerHTML=t.textContent)},this))},g=function(e,t){var i=new RegExp(e,"i").exec(t.textContent);return!!i&&t.textContent.replace(i[0],"<span class='selectr-match'>"+i[0]+"</span>")},v=function(e,t){if(t=t||{},!e)throw new Error("You must supply either a HTMLSelectElement or a CSS3 selector string.");if(this.el=e,"string"==typeof e&&(this.el=document.querySelector(e)),null===this.el)throw new Error("The element you passed to Selectr can not be found.");if("select"!==this.el.nodeName.toLowerCase())throw new Error("The element you passed to Selectr is not a HTMLSelectElement.");this.render(t)};return v.prototype.render=function(e){if(!this.rendered){this.config=a.extend(s,e),this.originalType=this.el.type,this.originalIndex=this.el.tabIndex,this.defaultSelected=[],this.originalOptionCount=this.el.options.length,(this.config.multiple||this.config.taggable)&&(this.el.multiple=!0),this.disabled=t(this.config,"disabled"),this.opened=!1,this.config.taggable&&(this.config.searchable=!1),this.navigating=!1,this.mobileDevice=!1,/Android|webOS|iPhone|iPad|BlackBerry|Windows Phone|Opera Mini|IEMobile|Mobile/i.test(navigator.userAgent)&&(this.mobileDevice=!0),this.customOption=this.config.hasOwnProperty("renderOption")&&"function"==typeof this.config.renderOption,this.customSelected=this.config.hasOwnProperty("renderSelection")&&"function"==typeof this.config.renderSelection,n.mixin(this),c.call(this),this.bindEvents(),this.update(),this.optsRect=a.rect(this.tree),this.rendered=!0,this.el.multiple||(this.el.selectedIndex=this.selectedIndex);var i=this;setTimeout(function(){i.emit("selectr.init")},20)}},v.prototype.bindEvents=function(){var e=this;this.events={},this.events.dismiss=o.bind(this),this.events.navigate=r.bind(this),this.events.reset=this.reset.bind(this),(this.config.nativeDropdown||this.mobileDevice)&&(this.container.addEventListener("touchstart",function(t){t.changedTouches[0].target===e.el&&e.toggle()}),(this.config.nativeDropdown||this.mobileDevice)&&this.container.addEventListener("click",function(t){t.target===e.el&&e.toggle()}),this.el.addEventListener("change",function(t){if(!e.opened)return e.open(),!1;if(e.el.multiple){var i=e.el.querySelectorAll("option:checked"),s=[].slice.call(i).map(function(e){return e.idx});e.clear(),a.each(s,function(t,i){e.select(i)},e)}else e.el.selectedIndex>-1&&e.select(e.el.selectedIndex)})),this.config.nativeDropdown&&this.container.addEventListener("keydown",function(t){"Enter"===t.key&&e.selected===document.activeElement&&(e.toggle(),setTimeout(function(){e.el.focus()},200))}),this.selected.addEventListener("click",function(t){e.disabled||e.toggle(),t.preventDefault()},this),this.label.addEventListener("click",function(t){a.hasClass(t.target,"selectr-tag-remove")&&e.deselect(t.target.parentNode.idx)},this),this.selectClear&&this.selectClear.addEventListener("click",this.clear.bind(this)),this.tree.addEventListener("mousedown",function(e){e.preventDefault()}),this.tree.addEventListener("click",function(t){var i=a.closest(t.target,function(e){return e&&a.hasClass(e,"selectr-option")});i&&(a.hasClass(i,"disabled")||(a.hasClass(i,"selected")?(e.el.multiple||!e.el.multiple&&e.config.allowDeselect)&&e.deselect(i.idx):e.select(i.idx),e.opened&&!e.el.multiple&&e.close()))}),this.tree.addEventListener("mouseover",function(t){a.hasClass(t.target,"selectr-option")&&(a.hasClass(t.target,"disabled")||(a.removeClass(e.items[e.navIndex],"active"),a.addClass(t.target,"active"),e.navIndex=[].slice.call(e.items).indexOf(t.target)))}),this.config.searchable&&(this.input.addEventListener("focus",function(t){e.searching=!0}),this.input.addEventListener("blur",function(t){e.searching=!1}),this.input.addEventListener("keyup",function(t){e.search(),e.config.taggable||(this.value.length?a.addClass(this.parentNode,"active"):a.removeClass(this.parentNode,"active"))}),this.inputClear.addEventListener("click",function(t){e.input.value=null,f.call(e),e.tree.childElementCount||l.call(e)})),this.config.taggable&&this.input.addEventListener("keyup",function(t){if(e.search(),e.config.taggable&&this.value.length){var i=this.value.trim();(13===t.which||a.includes(e.tagSeperators,t.key))&&(a.each(e.tagSeperators,function(e,t){i=i.replace(t,"")}),e.add({value:i,text:i,selected:!0},!0)?(e.close(),f.call(e)):(this.value="",e.setMessage("That tag is already in use.")))}}),this.update=a.debounce(function(){e.opened&&e.config.closeOnScroll&&e.close(),e.width&&(e.container.style.width=e.width),e.invert()},50),this.requiresPagination&&(this.paginateItems=a.debounce(function(){u.call(this)},50),this.tree.addEventListener("scroll",this.paginateItems.bind(this))),document.addEventListener("click",this.events.dismiss),window.addEventListener("keydown",this.events.navigate),window.addEventListener("resize",this.update),window.addEventListener("scroll",this.update),this.el.form&&this.el.form.addEventListener("reset",this.events.reset)},v.prototype.setSelected=function(e){if(this.config.data||this.el.multiple||!this.el.options.length||(0===this.el.selectedIndex&&(this.el.options[0].defaultSelected||this.config.defaultSelected||(this.el.selectedIndex=-1)),this.selectedIndex=this.el.selectedIndex),this.config.multiple&&"select-one"===this.originalType&&!this.config.data&&this.el.options[0].selected&&!this.el.options[0].defaultSelected&&(this.el.options[0].selected=!1),a.each(this.options,function(e,t){t.selected&&t.defaultSelected&&this.select(t.idx)},this),this.config.selectedValue&&this.setValue(this.config.selectedValue),this.config.data){!this.el.multiple&&this.config.defaultSelected&&this.el.selectedIndex<0&&this.select(0);var i=0;a.each(this.config.data,function(e,s){t(s,"children")?a.each(s.children,function(e,t){t.hasOwnProperty("selected")&&!0===t.selected&&this.select(i),i++},this):(s.hasOwnProperty("selected")&&!0===s.selected&&this.select(i),i++)},this)}},v.prototype.destroy=function(){this.rendered&&(this.emit("selectr.destroy"),"select-one"===this.originalType&&(this.el.multiple=!1),this.config.data&&(this.el.innerHTML=""),a.removeClass(this.el,"selectr-hidden"),this.el.form&&a.off(this.el.form,"reset",this.events.reset),a.off(document,"click",this.events.dismiss),a.off(document,"keydown",this.events.navigate),a.off(window,"resize",this.update),a.off(window,"scroll",this.update),this.container.parentNode.replaceChild(this.el,this.container),this.rendered=!1)},v.prototype.change=function(e){var t=this.items[e],i=this.options[e];i.disabled||(i.selected&&a.hasClass(t,"selected")?this.deselect(e):this.select(e),this.opened&&!this.el.multiple&&this.close())},v.prototype.select=function(e){var t=this.items[e],i=[].slice.call(this.el.options),s=this.options[e];if(this.el.multiple){if(a.includes(this.selectedIndexes,e))return!1;if(this.config.maxSelections&&this.tags.length===this.config.maxSelections)return this.setMessage("A maximum of "+this.config.maxSelections+" items can be selected.",!0),!1;this.selectedValues.push(s.value),this.selectedIndexes.push(e),d.call(this,t)}else{var n=this.data?this.data[e]:s;this.label.innerHTML=this.customSelected?this.config.renderSelection(n):s.textContent,this.selectedValue=s.value,this.selectedIndex=e,a.each(this.options,function(t,i){var s=this.items[t];t!==e&&(s&&a.removeClass(s,"selected"),i.selected=!1,i.removeAttribute("selected"))},this)}a.includes(i,s)||this.el.add(s),t.setAttribute("aria-selected",!0),a.addClass(t,"selected"),a.addClass(this.container,"has-selected"),s.selected=!0,s.setAttribute("selected",""),this.emit("selectr.select",s),this.emit("selectr.change",s)},v.prototype.deselect=function(e,t){var i=this.items[e],s=this.options[e];if(this.el.multiple){var n=this.selectedIndexes.indexOf(e);this.selectedIndexes.splice(n,1);var l=this.selectedValues.indexOf(s.value);this.selectedValues.splice(l,1),p.call(this,i),this.tags.length||a.removeClass(this.container,"has-selected")}else{if(!t&&!this.config.clearable&&!this.config.allowDeselect)return!1;this.label.innerHTML="",this.selectedValue=null,this.el.selectedIndex=this.selectedIndex=-1,a.removeClass(this.container,"has-selected")}this.items[e].setAttribute("aria-selected",!1),a.removeClass(this.items[e],"selected"),s.selected=!1,s.removeAttribute("selected"),this.emit("selectr.deselect",s),this.emit("selectr.change",s)},v.prototype.setValue=function(e){var t=Array.isArray(e);if(t||(e=e.toString().trim()),!this.el.multiple&&t)return!1;a.each(this.options,function(i,s){(t&&a.includes(e.toString(),s.value)||s.value===e)&&this.change(s.idx)},this)},v.prototype.getValue=function(e,t){var i;if(this.el.multiple)e?this.selectedIndexes.length&&((i={}).values=[],a.each(this.selectedIndexes,function(e,t){var s=this.options[t];i.values[e]={value:s.value,text:s.textContent}},this)):i=this.selectedValues.slice();else if(e){var s=this.options[this.selectedIndex];i={value:s.value,text:s.textContent}}else i=this.selectedValue;return e&&t&&(i=JSON.stringify(i)),i},v.prototype.add=function(e,t){if(e){if(this.data=this.data||[],this.items=this.items||[],this.options=this.options||[],Array.isArray(e))a.each(e,function(e,i){this.add(i,t)},this);else if("[object Object]"===Object.prototype.toString.call(e)){if(t){var i=!1;if(a.each(this.options,function(t,s){s.value.toLowerCase()===e.value.toLowerCase()&&(i=!0)}),i)return!1}var s=a.createElement("option",e);return this.data.push(e),this.options.push(s),s.idx=this.options.length>0?this.options.length-1:0,h.call(this,s),e.selected&&this.select(s.idx),s}return this.setPlaceholder(),this.config.pagination&&this.paginate(),!0}},v.prototype.remove=function(e){var t=[];if(Array.isArray(e)?a.each(e,function(i,s){a.isInt(s)?t.push(this.getOptionByIndex(s)):"string"==typeof e&&t.push(this.getOptionByValue(s))},this):a.isInt(e)?t.push(this.getOptionByIndex(e)):"string"==typeof e&&t.push(this.getOptionByValue(e)),t.length){var i;a.each(t,function(e,t){i=t.idx,this.el.remove(t),this.options.splice(i,1);var s=this.items[i].parentNode;s&&s.removeChild(this.items[i]),this.items.splice(i,1),a.each(this.options,function(e,t){t.idx=e,this.items[e].idx=e},this)},this),this.setPlaceholder(),this.config.pagination&&this.paginate()}},v.prototype.removeAll=function(){this.clear(!0),a.each(this.el.options,function(e,t){this.el.remove(t)},this),a.truncate(this.tree),this.items=[],this.options=[],this.data=[],this.navIndex=0,this.requiresPagination&&(this.requiresPagination=!1,this.pageIndex=1,this.pages=[]),this.setPlaceholder()},v.prototype.search=function(e){if(!this.navigating){e=e||this.input.value;var t=document.createDocumentFragment();if(this.removeMessage(),a.truncate(this.tree),e.length>1)if(a.each(this.options,function(s,n){var l=this.items[n.idx];a.includes(n.textContent.toLowerCase(),e.toLowerCase())&&!n.disabled?(i(l,t,this.customOption),a.removeClass(l,"excluded"),this.customOption||(l.innerHTML=g(e,n))):a.addClass(l,"excluded")},this),t.childElementCount){var s=this.items[this.navIndex],n=t.firstElementChild;a.removeClass(s,"active"),this.navIndex=n.idx,a.addClass(n,"active")}else this.config.taggable||this.setMessage("no results.");else l.call(this);this.tree.appendChild(t)}},v.prototype.toggle=function(){this.disabled||(this.opened?this.close():this.open())},v.prototype.open=function(){var e=this;return!!this.options.length&&(this.opened||this.emit("selectr.open"),this.opened=!0,this.mobileDevice||this.config.nativeDropdown?(a.addClass(this.container,"native-open"),void(this.config.data&&a.each(this.options,function(e,t){this.el.add(t)},this))):(a.addClass(this.container,"open"),l.call(this),this.invert(),this.tree.scrollTop=0,a.removeClass(this.container,"notice"),this.selected.setAttribute("aria-expanded",!0),this.tree.setAttribute("aria-hidden",!1),this.tree.setAttribute("aria-expanded",!0),void(this.config.searchable&&!this.config.taggable&&setTimeout(function(){e.input.focus(),e.input.tabIndex=0},10))))},v.prototype.close=function(){if(this.opened&&this.emit("selectr.close"),this.opened=!1,this.mobileDevice||this.config.nativeDropdown)a.removeClass(this.container,"native-open");else{var e=a.hasClass(this.container,"notice");this.config.searchable&&!e&&(this.input.blur(),this.input.tabIndex=-1,this.searching=!1),e&&(a.removeClass(this.container,"notice"),this.notice.textContent=""),a.removeClass(this.container,"open"),a.removeClass(this.container,"native-open"),this.selected.setAttribute("aria-expanded",!1),this.tree.setAttribute("aria-hidden",!0),this.tree.setAttribute("aria-expanded",!1),a.truncate(this.tree),f.call(this)}},v.prototype.enable=function(){this.disabled=!1,this.el.disabled=!1,this.selected.tabIndex=this.originalIndex,this.el.multiple&&a.each(this.tags,function(e,t){t.lastElementChild.tabIndex=0}),a.removeClass(this.container,"selectr-disabled")},v.prototype.disable=function(e){e||(this.el.disabled=!0),this.selected.tabIndex=-1,this.el.multiple&&a.each(this.tags,function(e,t){t.lastElementChild.tabIndex=-1}),this.disabled=!0,a.addClass(this.container,"selectr-disabled")},v.prototype.reset=function(){this.disabled||(this.clear(),this.setSelected(!0),a.each(this.defaultSelected,function(e,t){this.select(t)},this),this.emit("selectr.reset"))},v.prototype.clear=function(e){if(this.el.multiple){if(this.selectedIndexes.length){var t=this.selectedIndexes.slice();a.each(t,function(e,t){this.deselect(t)},this)}}else this.selectedIndex>-1&&this.deselect(this.selectedIndex,e);this.emit("selectr.clear")},v.prototype.serialise=function(e){var t=[];return a.each(this.options,function(e,i){var s={value:i.value,text:i.textContent};i.selected&&(s.selected=!0),i.disabled&&(s.disabled=!0),t[e]=s}),e?JSON.stringify(t):t},v.prototype.serialize=function(e){return this.serialise(e)},v.prototype.setPlaceholder=function(e){e=e||this.config.placeholder||this.el.getAttribute("placeholder"),this.options.length||(e="No options available"),this.placeEl.innerHTML=e},v.prototype.paginate=function(){if(this.items.length){var e=this;return this.pages=this.items.map(function(t,i){return i%e.config.pagination==0?e.items.slice(i,i+e.config.pagination):null}).filter(function(e){return e}),this.pages}},v.prototype.setMessage=function(e,t){t&&this.close(),a.addClass(this.container,"notice"),this.notice.textContent=e},v.prototype.removeMessage=function(){a.removeClass(this.container,"notice"),this.notice.innerHTML=""},v.prototype.invert=function(){var e=a.rect(this.selected),t=this.tree.parentNode.offsetHeight,i=window.innerHeight;e.top+e.height+t>i?(a.addClass(this.container,"inverted"),this.isInverted=!0):(a.removeClass(this.container,"inverted"),this.isInverted=!1),this.optsRect=a.rect(this.tree)},v.prototype.getOptionByIndex=function(e){return this.options[e]},v.prototype.getOptionByValue=function(e){for(var t=!1,i=0,s=this.options.length;i<s;i++)if(this.options[i].value.trim()===e.toString().trim()){t=this.options[i];break}return t},v}); | ||
!function(e,t){"function"==typeof define&&define.amd?define([],t("Selectr")):"object"==typeof exports?module.exports=t("Selectr"):e.Selectr=t("Selectr")}(this,function(e){"use strict";function t(e,t){return e.hasOwnProperty(t)&&(!0===e[t]||e[t].length)}function i(e,t,i){e.parentNode?e.parentNode.parentNode||t.appendChild(e.parentNode):t.appendChild(e),a.removeClass(e,"excluded"),i||(e.innerHTML=e.textContent)}var s={defaultSelected:!0,width:"auto",disabled:!1,searchable:!0,clearable:!1,sortSelected:!1,allowDeselect:!1,closeOnScroll:!1,nativeDropdown:!1,placeholder:"Select an option...",taggable:!1,tagPlaceholder:"Enter a tag..."},n=function(){};n.prototype={on:function(e,t){this._events=this._events||{},this._events[e]=this._events[e]||[],this._events[e].push(t)},off:function(e,t){this._events=this._events||{},e in this._events!=!1&&this._events[e].splice(this._events[e].indexOf(t),1)},emit:function(e){if(this._events=this._events||{},e in this._events!=!1)for(var t=0;t<this._events[e].length;t++)this._events[e][t].apply(this,Array.prototype.slice.call(arguments,1))}},n.mixin=function(e){for(var t=["on","off","emit"],i=0;i<t.length;i++)"function"==typeof e?e.prototype[t[i]]=n.prototype[t[i]]:e[t[i]]=n.prototype[t[i]];return e};var a={extend:function(e,t){t=t||{};var i;for(i in e)e.hasOwnProperty(i)&&(t.hasOwnProperty(i)||(t[i]=e[i]));return t},each:function(e,t,i){if("[object Object]"===Object.prototype.toString.call(e))for(var s in e)Object.prototype.hasOwnProperty.call(e,s)&&t.call(i,s,e[s],e);else for(var n=0,a=e.length;n<a;n++)t.call(i,n,e[n],e)},createElement:function(e,t){var i=document,s=i.createElement(e);if(t&&"[object Object]"===Object.prototype.toString.call(t)){var n;for(n in t)if(n in s)s[n]=t[n];else if("html"===n)s.innerHTML=t[n];else if("text"===n){var a=i.createTextNode(t[n]);s.appendChild(a)}else s.setAttribute(n,t[n])}return s},hasClass:function(e,t){if(e)return e.classList?e.classList.contains(t):!!e.className&&!!e.className.match(new RegExp("(\\s|^)"+t+"(\\s|$)"))},addClass:function(e,t){a.hasClass(e,t)||(e.classList?e.classList.add(t):e.className=e.className.trim()+" "+t)},removeClass:function(e,t){a.hasClass(e,t)&&(e.classList?e.classList.remove(t):e.className=e.className.replace(new RegExp("(^|\\s)"+t.split(" ").join("|")+"(\\s|$)","gi")," "))},closest:function(e,t){return e&&e!==document.body&&(t(e)?e:a.closest(e.parentNode,t))},isInt:function(e){return"number"==typeof e&&isFinite(e)&&Math.floor(e)===e},debounce:function(e,t,i){var s;return function(){var n=this,a=arguments,l=i&&!s;clearTimeout(s),s=setTimeout(function(){s=null,i||e.apply(n,a)},t),l&&e.apply(n,a)}},rect:function(e,t){var i=window,s=e.getBoundingClientRect(),n=t?i.pageXOffset:0,a=t?i.pageYOffset:0;return{bottom:s.bottom+a,height:s.height,left:s.left+n,right:s.right+n,top:s.top+a,width:s.width}},includes:function(e,t){return e.indexOf(t)>-1},truncate:function(e){for(;e.firstChild;)e.removeChild(e.firstChild)}},l=function(){if(this.items.length){var e=document.createDocumentFragment();if(this.config.pagination){var t=this.pages.slice(0,this.pageIndex);a.each(t,function(t,s){a.each(s,function(t,s){i(s,e,this.customOption)},this)},this)}else a.each(this.items,function(t,s){i(s,e,this.customOption)},this);e.childElementCount&&(a.removeClass(this.items[this.navIndex],"active"),this.navIndex=e.querySelector(".selectr-option").idx,a.addClass(this.items[this.navIndex],"active")),this.tree.appendChild(e)}},o=function(e){var t=e.target;this.container.contains(t)||!this.opened&&!a.hasClass(this.container,"notice")||this.close()},h=function(e,t){t=t||e;var i=this.customOption?this.config.renderOption(t):e.textContent,s=a.createElement("li",{class:"selectr-option",html:i,role:"treeitem","aria-selected":!1});return s.idx=e.idx,this.items.push(s),e.defaultSelected&&this.defaultSelected.push(e.idx),e.disabled&&(s.disabled=!0,a.addClass(s,"disabled")),s},c=function(){this.requiresPagination=this.config.pagination&&this.config.pagination>0,t(this.config,"width")&&(a.isInt(this.config.width)?this.width=this.config.width+"px":"auto"===this.config.width?this.width="100%":a.includes(this.config.width,"%")&&(this.width=this.config.width)),this.container=a.createElement("div",{class:"selectr-container"}),this.config.customClass&&a.addClass(this.container,this.config.customClass),this.mobileDevice?a.addClass(this.container,"selectr-mobile"):a.addClass(this.container,"selectr-desktop"),this.el.tabIndex=-1,this.config.nativeDropdown||this.mobileDevice?a.addClass(this.el,"selectr-visible"):a.addClass(this.el,"selectr-hidden"),this.selected=a.createElement("div",{class:"selectr-selected",disabled:this.disabled,tabIndex:1,"aria-expanded":!1}),this.label=a.createElement(this.el.multiple?"ul":"span",{class:"selectr-label"});var e=a.createElement("div",{class:"selectr-options-container"});if(this.tree=a.createElement("ul",{class:"selectr-options",role:"tree","aria-hidden":!0,"aria-expanded":!1}),this.notice=a.createElement("div",{class:"selectr-notice"}),this.el.setAttribute("aria-hidden",!0),this.disabled&&(this.el.disabled=!0),this.el.multiple&&(a.addClass(this.label,"selectr-tags"),a.addClass(this.container,"multiple"),this.tags=[],this.selectedValues=[],this.selectedIndexes=[]),this.selected.appendChild(this.label),this.config.clearable&&(this.selectClear=a.createElement("button",{class:"selectr-clear",type:"button"}),this.container.appendChild(this.selectClear),a.addClass(this.container,"clearable")),this.config.taggable){var i=a.createElement("li",{class:"input-tag"});this.input=a.createElement("input",{class:"selectr-tag-input",placeholder:this.config.tagPlaceholder,tagIndex:0,autocomplete:"off",autocorrect:"off",autocapitalize:"off",spellcheck:"false",role:"textbox",type:"search"}),i.appendChild(this.input),this.label.appendChild(i),a.addClass(this.container,"taggable"),this.tagSeperators=[","],this.config.tagSeperators&&(this.tagSeperators=this.tagSeperators.concat(this.config.tagSeperators))}this.config.searchable&&(this.input=a.createElement("input",{class:"selectr-input",tagIndex:-1,autocomplete:"off",autocorrect:"off",autocapitalize:"off",spellcheck:"false",role:"textbox",type:"search"}),this.inputClear=a.createElement("button",{class:"selectr-input-clear",type:"button"}),this.inputContainer=a.createElement("div",{class:"selectr-input-container"}),this.inputContainer.appendChild(this.input),this.inputContainer.appendChild(this.inputClear),e.appendChild(this.inputContainer)),e.appendChild(this.notice),e.appendChild(this.tree),this.items=[],this.options=[],this.el.options.length&&(this.options=[].slice.call(this.el.options));var s=!1,n=0;if(this.el.children.length&&a.each(this.el.children,function(e,t){"OPTGROUP"===t.nodeName?(s=a.createElement("ul",{class:"selectr-optgroup",role:"group",html:"<li class='selectr-optgroup--label'>"+t.label+"</li>"}),a.each(t.children,function(e,t){t.idx=n,s.appendChild(h.call(this,t,s)),n++},this)):(t.idx=n,h.call(this,t),n++)},this),this.config.data&&Array.isArray(this.config.data)){this.data=[];var l,o=!1;s=!1,n=0,a.each(this.config.data,function(e,i){t(i,"children")?(o=a.createElement("optgroup",{label:i.text}),s=a.createElement("ul",{class:"selectr-optgroup",role:"group",html:"<li class='selectr-optgroup--label'>"+i.text+"</li>"}),a.each(i.children,function(e,i){(l=new Option(i.text,i.value,!1,i.hasOwnProperty("selected")&&!0===i.selected)).disabled=t(i,"disabled"),this.options.push(l),o.appendChild(l),l.idx=n,s.appendChild(h.call(this,l,i)),this.data[n]=i,n++},this)):((l=new Option(i.text,i.value,!1,i.hasOwnProperty("selected")&&!0===i.selected)).disabled=t(i,"disabled"),this.options.push(l),l.idx=n,h.call(this,l,i),this.data[n]=i,n++)},this)}this.setSelected(!0);var c;this.navIndex=0;for(var r=0;r<this.items.length;r++)if(c=this.items[r],!a.hasClass(c,"disabled")){a.addClass(c,"active"),this.navIndex=r;break}this.requiresPagination&&(this.pageIndex=1,this.paginate()),this.container.appendChild(this.selected),this.container.appendChild(e),this.placeEl=a.createElement("div",{class:"selectr-placeholder"}),this.setPlaceholder(),this.selected.appendChild(this.placeEl),this.disabled&&this.disable(),this.el.parentNode.insertBefore(this.container,this.el),this.container.appendChild(this.el)},r=function(e){if(e=e||window.event,this.items.length&&this.opened&&a.includes([13,38,40],e.which)){if(e.preventDefault(),13===e.which)return!(this.config.taggable&&this.input.value.length>0)&&this.change(this.navIndex);var t,i=this.items[this.navIndex];switch(e.which){case 38:t=0,this.navIndex>0&&this.navIndex--;break;case 40:t=1,this.navIndex<this.items.length-1&&this.navIndex++}for(this.navigating=!0;a.hasClass(this.items[this.navIndex],"disabled")||a.hasClass(this.items[this.navIndex],"excluded");)if(t?this.navIndex++:this.navIndex--,this.searching){if(this.navIndex>this.tree.lastElementChild.idx){this.navIndex=this.tree.lastElementChild.idx;break}if(this.navIndex<this.tree.firstElementChild.idx){this.navIndex=this.tree.firstElementChild.idx;break}}var s=a.rect(this.items[this.navIndex]);t?(0===this.navIndex?this.tree.scrollTop=0:s.top+s.height>this.optsRect.top+this.optsRect.height&&(this.tree.scrollTop=this.tree.scrollTop+(s.top+s.height-(this.optsRect.top+this.optsRect.height))),this.navIndex===this.tree.childElementCount-1&&this.requiresPagination&&u.call(this)):0===this.navIndex?this.tree.scrollTop=0:s.top-this.optsRect.top<0&&(this.tree.scrollTop=this.tree.scrollTop+(s.top-this.optsRect.top)),i&&a.removeClass(i,"active"),a.addClass(this.items[this.navIndex],"active")}else this.navigating=!1},d=function(e){var t,i=this,s=document.createDocumentFragment(),n=this.options[e.idx],l=this.data?this.data[e.idx]:n,o=this.customSelected?this.config.renderSelection(l):n.textContent,h=a.createElement("li",{class:"selectr-tag",html:o}),c=a.createElement("button",{class:"selectr-tag-remove",type:"button"});if(h.appendChild(c),h.idx=e.idx,h.tag=n.value,this.tags.push(h),this.config.sortSelected){var r=this.tags.slice();t=function(e,t){e.replace(/(\d+)|(\D+)/g,function(e,i,s){t.push([i||1/0,s||""])})},r.sort(function(e,s){var n,a,l=[],o=[];for(!0===i.config.sortSelected?(n=e.tag,a=s.tag):"text"===i.config.sortSelected&&(n=e.textContent,a=s.textContent),t(n,l),t(a,o);l.length&&o.length;){var h=l.shift(),c=o.shift(),r=h[0]-c[0]||h[1].localeCompare(c[1]);if(r)return r}return l.length-o.length}),a.each(r,function(e,t){s.appendChild(t)}),this.label.innerHTML=""}else s.appendChild(h);this.config.taggable?this.label.insertBefore(s,this.input.parentNode):this.label.appendChild(s)},p=function(e){var t=!1;a.each(this.tags,function(i,s){s.idx===e.idx&&(t=s)},this),t&&(this.label.removeChild(t),this.tags.splice(this.tags.indexOf(t),1))},u=function(){var e=this.tree;if(e.scrollTop>=e.scrollHeight-e.offsetHeight&&this.pageIndex<this.pages.length){var t=document.createDocumentFragment();a.each(this.pages[this.pageIndex],function(e,s){i(s,t,this.customOption)},this),e.appendChild(t),this.pageIndex++,this.emit("selectr.paginate",{items:this.items.length,total:this.data.length,page:this.pageIndex,pages:this.pages.length})}},f=function(){(this.config.searchable||this.config.taggable)&&(this.input.value=null,this.searching=!1,this.config.searchable&&a.removeClass(this.inputContainer,"active"),a.hasClass(this.container,"notice")&&(a.removeClass(this.container,"notice"),a.addClass(this.container,"open"),this.input.focus()),a.each(this.items,function(e,t){a.removeClass(t,"excluded"),this.customOption||(t.innerHTML=t.textContent)},this))},g=function(e,t){var i=new RegExp(e,"i").exec(t.textContent);return!!i&&t.textContent.replace(i[0],"<span class='selectr-match'>"+i[0]+"</span>")},v=function(e,t){if(t=t||{},!e)throw new Error("You must supply either a HTMLSelectElement or a CSS3 selector string.");if(this.el=e,"string"==typeof e&&(this.el=document.querySelector(e)),null===this.el)throw new Error("The element you passed to Selectr can not be found.");if("select"!==this.el.nodeName.toLowerCase())throw new Error("The element you passed to Selectr is not a HTMLSelectElement.");this.render(t)};return v.prototype.render=function(e){if(!this.rendered){this.config=a.extend(s,e),this.originalType=this.el.type,this.originalIndex=this.el.tabIndex,this.defaultSelected=[],this.originalOptionCount=this.el.options.length,(this.config.multiple||this.config.taggable)&&(this.el.multiple=!0),this.disabled=t(this.config,"disabled"),this.opened=!1,this.config.taggable&&(this.config.searchable=!1),this.navigating=!1,this.mobileDevice=!1,/Android|webOS|iPhone|iPad|BlackBerry|Windows Phone|Opera Mini|IEMobile|Mobile/i.test(navigator.userAgent)&&(this.mobileDevice=!0),this.customOption=this.config.hasOwnProperty("renderOption")&&"function"==typeof this.config.renderOption,this.customSelected=this.config.hasOwnProperty("renderSelection")&&"function"==typeof this.config.renderSelection,n.mixin(this),c.call(this),this.bindEvents(),this.update(),this.optsRect=a.rect(this.tree),this.rendered=!0,this.el.multiple||(this.el.selectedIndex=this.selectedIndex);var i=this;setTimeout(function(){i.emit("selectr.init")},20)}},v.prototype.bindEvents=function(){var e=this;this.events={},this.events.dismiss=o.bind(this),this.events.navigate=r.bind(this),this.events.reset=this.reset.bind(this),(this.config.nativeDropdown||this.mobileDevice)&&(this.container.addEventListener("touchstart",function(t){t.changedTouches[0].target===e.el&&e.toggle()}),(this.config.nativeDropdown||this.mobileDevice)&&this.container.addEventListener("click",function(t){t.target===e.el&&e.toggle()}),this.el.addEventListener("change",function(t){if(!e.opened)return e.open(),!1;if(e.el.multiple){var i=e.el.querySelectorAll("option:checked"),s=[].slice.call(i).map(function(e){return e.idx});e.clear(),a.each(s,function(t,i){e.select(i)},e)}else e.el.selectedIndex>-1&&e.select(e.el.selectedIndex)})),this.config.nativeDropdown&&this.container.addEventListener("keydown",function(t){"Enter"===t.key&&e.selected===document.activeElement&&(e.toggle(),setTimeout(function(){e.el.focus()},200))}),this.selected.addEventListener("click",function(t){e.disabled||e.toggle(),t.preventDefault()}),this.label.addEventListener("click",function(t){a.hasClass(t.target,"selectr-tag-remove")&&e.deselect(t.target.parentNode.idx)}),this.selectClear&&this.selectClear.addEventListener("click",this.clear.bind(this)),this.tree.addEventListener("mousedown",function(e){e.preventDefault()}),this.tree.addEventListener("click",function(t){var i=a.closest(t.target,function(e){return e&&a.hasClass(e,"selectr-option")});i&&(a.hasClass(i,"disabled")||(a.hasClass(i,"selected")?(e.el.multiple||!e.el.multiple&&e.config.allowDeselect)&&e.deselect(i.idx):e.select(i.idx),e.opened&&!e.el.multiple&&e.close()))}),this.tree.addEventListener("mouseover",function(t){a.hasClass(t.target,"selectr-option")&&(a.hasClass(t.target,"disabled")||(a.removeClass(e.items[e.navIndex],"active"),a.addClass(t.target,"active"),e.navIndex=[].slice.call(e.items).indexOf(t.target)))}),this.config.searchable&&(this.input.addEventListener("focus",function(t){e.searching=!0}),this.input.addEventListener("blur",function(t){e.searching=!1}),this.input.addEventListener("keyup",function(t){e.search(),e.config.taggable||(this.value.length?a.addClass(this.parentNode,"active"):a.removeClass(this.parentNode,"active"))}),this.inputClear.addEventListener("click",function(t){e.input.value=null,f.call(e),e.tree.childElementCount||l.call(e)})),this.config.taggable&&this.input.addEventListener("keyup",function(t){if(e.search(),e.config.taggable&&this.value.length){var i=this.value.trim();(13===t.which||a.includes(e.tagSeperators,t.key))&&(a.each(e.tagSeperators,function(e,t){i=i.replace(t,"")}),e.add({value:i,text:i,selected:!0},!0)?(e.close(),f.call(e)):(this.value="",e.setMessage("That tag is already in use.")))}}),this.update=a.debounce(function(){e.opened&&e.config.closeOnScroll&&e.close(),e.width&&(e.container.style.width=e.width),e.invert()},50),this.requiresPagination&&(this.paginateItems=a.debounce(function(){u.call(this)},50),this.tree.addEventListener("scroll",this.paginateItems.bind(this))),document.addEventListener("click",this.events.dismiss),window.addEventListener("keydown",this.events.navigate),window.addEventListener("resize",this.update),window.addEventListener("scroll",this.update),this.el.form&&this.el.form.addEventListener("reset",this.events.reset)},v.prototype.setSelected=function(e){if(this.config.data||this.el.multiple||!this.el.options.length||(0===this.el.selectedIndex&&(this.el.options[0].defaultSelected||this.config.defaultSelected||(this.el.selectedIndex=-1)),this.selectedIndex=this.el.selectedIndex,this.selectedIndex>-1&&this.select(this.selectedIndex)),this.config.multiple&&"select-one"===this.originalType&&!this.config.data&&this.el.options[0].selected&&!this.el.options[0].defaultSelected&&(this.el.options[0].selected=!1),a.each(this.options,function(e,t){t.selected&&t.defaultSelected&&this.select(t.idx)},this),this.config.selectedValue&&this.setValue(this.config.selectedValue),this.config.data){!this.el.multiple&&this.config.defaultSelected&&this.el.selectedIndex<0&&this.select(0);var i=0;a.each(this.config.data,function(e,s){t(s,"children")?a.each(s.children,function(e,t){t.hasOwnProperty("selected")&&!0===t.selected&&this.select(i),i++},this):(s.hasOwnProperty("selected")&&!0===s.selected&&this.select(i),i++)},this)}},v.prototype.destroy=function(){this.rendered&&(this.emit("selectr.destroy"),"select-one"===this.originalType&&(this.el.multiple=!1),this.config.data&&(this.el.innerHTML=""),a.removeClass(this.el,"selectr-hidden"),this.el.form&&a.off(this.el.form,"reset",this.events.reset),a.off(document,"click",this.events.dismiss),a.off(document,"keydown",this.events.navigate),a.off(window,"resize",this.update),a.off(window,"scroll",this.update),this.container.parentNode.replaceChild(this.el,this.container),this.rendered=!1)},v.prototype.change=function(e){var t=this.items[e],i=this.options[e];i.disabled||(i.selected&&a.hasClass(t,"selected")?this.deselect(e):this.select(e),this.opened&&!this.el.multiple&&this.close())},v.prototype.select=function(e){var t=this.items[e],i=[].slice.call(this.el.options),s=this.options[e];if(this.el.multiple){if(a.includes(this.selectedIndexes,e))return!1;if(this.config.maxSelections&&this.tags.length===this.config.maxSelections)return this.setMessage("A maximum of "+this.config.maxSelections+" items can be selected.",!0),!1;this.selectedValues.push(s.value),this.selectedIndexes.push(e),d.call(this,t)}else{var n=this.data?this.data[e]:s;this.label.innerHTML=this.customSelected?this.config.renderSelection(n):s.textContent,this.selectedValue=s.value,this.selectedIndex=e,a.each(this.options,function(t,i){var s=this.items[t];t!==e&&(s&&a.removeClass(s,"selected"),i.selected=!1,i.removeAttribute("selected"))},this)}a.includes(i,s)||this.el.add(s),t.setAttribute("aria-selected",!0),a.addClass(t,"selected"),a.addClass(this.container,"has-selected"),s.selected=!0,s.setAttribute("selected",""),this.emit("selectr.change",s),this.emit("selectr.select",s)},v.prototype.deselect=function(e,t){var i=this.items[e],s=this.options[e];if(this.el.multiple){var n=this.selectedIndexes.indexOf(e);this.selectedIndexes.splice(n,1);var l=this.selectedValues.indexOf(s.value);this.selectedValues.splice(l,1),p.call(this,i),this.tags.length||a.removeClass(this.container,"has-selected")}else{if(!t&&!this.config.clearable&&!this.config.allowDeselect)return!1;this.label.innerHTML="",this.selectedValue=null,this.el.selectedIndex=this.selectedIndex=-1,a.removeClass(this.container,"has-selected")}this.items[e].setAttribute("aria-selected",!1),a.removeClass(this.items[e],"selected"),s.selected=!1,s.removeAttribute("selected"),this.emit("selectr.change",null),this.emit("selectr.deselect",s)},v.prototype.setValue=function(e){var t=Array.isArray(e);if(t||(e=e.toString().trim()),!this.el.multiple&&t)return!1;a.each(this.options,function(i,s){(t&&a.includes(e.toString(),s.value)||s.value===e)&&this.change(s.idx)},this)},v.prototype.getValue=function(e,t){var i;if(this.el.multiple)e?this.selectedIndexes.length&&((i={}).values=[],a.each(this.selectedIndexes,function(e,t){var s=this.options[t];i.values[e]={value:s.value,text:s.textContent}},this)):i=this.selectedValues.slice();else if(e){var s=this.options[this.selectedIndex];i={value:s.value,text:s.textContent}}else i=this.selectedValue;return e&&t&&(i=JSON.stringify(i)),i},v.prototype.add=function(e,t){if(e){if(this.data=this.data||[],this.items=this.items||[],this.options=this.options||[],Array.isArray(e))a.each(e,function(e,i){this.add(i,t)},this);else if("[object Object]"===Object.prototype.toString.call(e)){if(t){var i=!1;if(a.each(this.options,function(t,s){s.value.toLowerCase()===e.value.toLowerCase()&&(i=!0)}),i)return!1}var s=a.createElement("option",e);return this.data.push(e),this.options.push(s),s.idx=this.options.length>0?this.options.length-1:0,h.call(this,s),e.selected&&this.select(s.idx),s}return this.setPlaceholder(),this.config.pagination&&this.paginate(),!0}},v.prototype.remove=function(e){var t=[];if(Array.isArray(e)?a.each(e,function(i,s){a.isInt(s)?t.push(this.getOptionByIndex(s)):"string"==typeof e&&t.push(this.getOptionByValue(s))},this):a.isInt(e)?t.push(this.getOptionByIndex(e)):"string"==typeof e&&t.push(this.getOptionByValue(e)),t.length){var i;a.each(t,function(e,t){i=t.idx,this.el.remove(t),this.options.splice(i,1);var s=this.items[i].parentNode;s&&s.removeChild(this.items[i]),this.items.splice(i,1),a.each(this.options,function(e,t){t.idx=e,this.items[e].idx=e},this)},this),this.setPlaceholder(),this.config.pagination&&this.paginate()}},v.prototype.removeAll=function(){this.clear(!0),a.each(this.el.options,function(e,t){this.el.remove(t)},this),a.truncate(this.tree),this.items=[],this.options=[],this.data=[],this.navIndex=0,this.requiresPagination&&(this.requiresPagination=!1,this.pageIndex=1,this.pages=[]),this.setPlaceholder()},v.prototype.search=function(e){if(!this.navigating){e=e||this.input.value;var t=document.createDocumentFragment();if(this.removeMessage(),a.truncate(this.tree),e.length>1)if(a.each(this.options,function(s,n){var l=this.items[n.idx];a.includes(n.textContent.toLowerCase(),e.toLowerCase())&&!n.disabled?(i(l,t,this.customOption),a.removeClass(l,"excluded"),this.customOption||(l.innerHTML=g(e,n))):a.addClass(l,"excluded")},this),t.childElementCount){var s=this.items[this.navIndex],n=t.firstElementChild;a.removeClass(s,"active"),this.navIndex=n.idx,a.addClass(n,"active")}else this.config.taggable||this.setMessage("no results.");else l.call(this);this.tree.appendChild(t)}},v.prototype.toggle=function(){this.disabled||(this.opened?this.close():this.open())},v.prototype.open=function(){var e=this;return!!this.options.length&&(this.opened||this.emit("selectr.open"),this.opened=!0,this.mobileDevice||this.config.nativeDropdown?(a.addClass(this.container,"native-open"),void(this.config.data&&a.each(this.options,function(e,t){this.el.add(t)},this))):(a.addClass(this.container,"open"),l.call(this),this.invert(),this.tree.scrollTop=0,a.removeClass(this.container,"notice"),this.selected.setAttribute("aria-expanded",!0),this.tree.setAttribute("aria-hidden",!1),this.tree.setAttribute("aria-expanded",!0),void(this.config.searchable&&!this.config.taggable&&setTimeout(function(){e.input.focus(),e.input.tabIndex=0},10))))},v.prototype.close=function(){if(this.opened&&this.emit("selectr.close"),this.opened=!1,this.mobileDevice||this.config.nativeDropdown)a.removeClass(this.container,"native-open");else{var e=a.hasClass(this.container,"notice");this.config.searchable&&!e&&(this.input.blur(),this.input.tabIndex=-1,this.searching=!1),e&&(a.removeClass(this.container,"notice"),this.notice.textContent=""),a.removeClass(this.container,"open"),a.removeClass(this.container,"native-open"),this.selected.setAttribute("aria-expanded",!1),this.tree.setAttribute("aria-hidden",!0),this.tree.setAttribute("aria-expanded",!1),a.truncate(this.tree),f.call(this)}},v.prototype.enable=function(){this.disabled=!1,this.el.disabled=!1,this.selected.tabIndex=this.originalIndex,this.el.multiple&&a.each(this.tags,function(e,t){t.lastElementChild.tabIndex=0}),a.removeClass(this.container,"selectr-disabled")},v.prototype.disable=function(e){e||(this.el.disabled=!0),this.selected.tabIndex=-1,this.el.multiple&&a.each(this.tags,function(e,t){t.lastElementChild.tabIndex=-1}),this.disabled=!0,a.addClass(this.container,"selectr-disabled")},v.prototype.reset=function(){this.disabled||(this.clear(),this.setSelected(!0),a.each(this.defaultSelected,function(e,t){this.select(t)},this),this.emit("selectr.reset"))},v.prototype.clear=function(e){if(this.el.multiple){if(this.selectedIndexes.length){var t=this.selectedIndexes.slice();a.each(t,function(e,t){this.deselect(t)},this)}}else this.selectedIndex>-1&&this.deselect(this.selectedIndex,e);this.emit("selectr.clear")},v.prototype.serialise=function(e){var t=[];return a.each(this.options,function(e,i){var s={value:i.value,text:i.textContent};i.selected&&(s.selected=!0),i.disabled&&(s.disabled=!0),t[e]=s}),e?JSON.stringify(t):t},v.prototype.serialize=function(e){return this.serialise(e)},v.prototype.setPlaceholder=function(e){e=e||this.config.placeholder||this.el.getAttribute("placeholder"),this.options.length||(e="No options available"),this.placeEl.innerHTML=e},v.prototype.paginate=function(){if(this.items.length){var e=this;return this.pages=this.items.map(function(t,i){return i%e.config.pagination==0?e.items.slice(i,i+e.config.pagination):null}).filter(function(e){return e}),this.pages}},v.prototype.setMessage=function(e,t){t&&this.close(),a.addClass(this.container,"notice"),this.notice.textContent=e},v.prototype.removeMessage=function(){a.removeClass(this.container,"notice"),this.notice.innerHTML=""},v.prototype.invert=function(){var e=a.rect(this.selected),t=this.tree.parentNode.offsetHeight,i=window.innerHeight;e.top+e.height+t>i?(a.addClass(this.container,"inverted"),this.isInverted=!0):(a.removeClass(this.container,"inverted"),this.isInverted=!1),this.optsRect=a.rect(this.tree)},v.prototype.getOptionByIndex=function(e){return this.options[e]},v.prototype.getOptionByValue=function(e){for(var t=!1,i=0,s=this.options.length;i<s;i++)if(this.options[i].value.trim()===e.toString().trim()){t=this.options[i];break}return t},v}); |
{ | ||
"name": "mobius1-selectr", | ||
"version": "2.3.8", | ||
"version": "2.3.9", | ||
"description": "A lightweight, dependency-free, mobile-friendly javascript select box replacement.", | ||
@@ -5,0 +5,0 @@ "main": "dist/selectr.min.js", |
@@ -51,12 +51,18 @@ # Selectr | ||
Grab the files from the CDN and include them in your page: | ||
Grab the files from one of the CDNs and include them in your page: | ||
```html | ||
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/mobius1/selectr@2.3.6/dist/selectr.min.css"> | ||
<link href="https://unpkg.com/mobius1-selectr@latest/dist/selectr.min.css" rel="stylesheet" type="text/css"> | ||
<script src="https://unpkg.com/mobius1-selectr@latest/dist/selectr.min.js" type="text/javascript"></script> | ||
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/mobius1/selectr@2.3.6/dist/selectr.min.js"></script> | ||
//or | ||
<link href="https://cdn.jsdelivr.net/gh/mobius1/selectr@latest/dist/selectr.min.css" rel="stylesheet" type="text/css"> | ||
<script src="https://cdn.jsdelivr.net/gh/mobius1/selectr@latest/dist/selectr.min.js" type="text/javascript"></script> | ||
``` | ||
CDN courtesy of [jsDelivr](http://www.jsdelivr.com/) | ||
You can replace `latest` with the required release number if needed. | ||
CDNs courtesy of [unpkg](https://unpkg.com/#/) and [jsDelivr](http://www.jsdelivr.com/) | ||
--- | ||
@@ -63,0 +69,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
532806
4355
122
60