mobius1-selectr
Advanced tools
Comparing version 2.3.5 to 2.3.6
{ | ||
"name": "mobius1-selectr", | ||
"version": "2.3.5", | ||
"version": "2.3.6", | ||
"ignore": [ | ||
@@ -5,0 +5,0 @@ ".gitattributes", |
/*! | ||
* Selectr 2.3.4 | ||
* Selectr 2.3.6 | ||
* http://mobius.ovh/docs/selectr | ||
@@ -7,2 +7,2 @@ * | ||
*/ | ||
!function(t,e){"function"==typeof define&&define.amd?define([],e("Selectr")):"object"==typeof exports?module.exports=e("Selectr"):t.Selectr=e("Selectr")}(this,function(t){"use strict";var e={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..."},i=function(){};i.prototype={on:function(t,e){this._events=this._events||{},this._events[t]=this._events[t]||[],this._events[t].push(e)},off:function(t,e){this._events=this._events||{},t in this._events!=!1&&this._events[t].splice(this._events[t].indexOf(e),1)},emit:function(t){if(this._events=this._events||{},t in this._events!=!1)for(var e=0;e<this._events[t].length;e++)this._events[t][e].apply(this,Array.prototype.slice.call(arguments,1))}},i.mixin=function(t){for(var e=["on","off","emit"],s=0;s<e.length;s++)"function"==typeof t?t.prototype[e[s]]=i.prototype[e[s]]:t[e[s]]=i.prototype[e[s]];return t};var s={extend:function(t,e){e=e||{};var i;for(i in t)t.hasOwnProperty(i)&&(e.hasOwnProperty(i)||(e[i]=t[i]));return e},each:function(t,e,i){if("[object Object]"===Object.prototype.toString.call(t))for(var s in t)Object.prototype.hasOwnProperty.call(t,s)&&e.call(i,s,t[s],t);else for(var n=0,a=t.length;n<a;n++)e.call(i,n,t[n],t)},querySelect:function(t){var e=!1;if(t&&"string"==typeof t){var i=document;switch(t.charAt(0)){case".":e=i.getElementsByClassName(t.substr(1))[0];break;case"#":e=i.getElementById(t.substr(1));break;default:e=i.getElementsByTagName(t)[0]}}return e},createElement:function(t,e){var i=document,n=i.createElement(t);if(e&&s.isObject(e)){var a;for(a in e)if(a in n)n[a]=e[a];else if("html"===a)n.innerHTML=e[a];else if("text"===a){var l=i.createTextNode(e[a]);n.appendChild(l)}else n.setAttribute(a,e[a])}return n},hasClass:function(t,e){if(t)return t.classList?t.classList.contains(e):!!t.className&&!!t.className.match(new RegExp("(\\s|^)"+e+"(\\s|$)"))},addClass:function(t,e){s.hasClass(t,e)||(t.classList?t.classList.add(e):t.className=t.className.trim()+" "+e)},removeClass:function(t,e){s.hasClass(t,e)&&(t.classList?t.classList.remove(e):t.className=t.className.replace(new RegExp("(^|\\s)"+e.split(" ").join("|")+"(\\s|$)","gi")," "))},closest:function(t,e){return t&&t!==document.body&&(e(t)?t:s.closest(t.parentNode,e))},on:function(t,e,i,s){t.addEventListener(e,function(t){s=s||this,i.call(s,t)},!1)},off:function(t,e,i){t.removeEventListener(e,i)},isObject:function(t){return"[object Object]"===Object.prototype.toString.call(t)},isArray:function(t){return"[object Array]"===Object.prototype.toString.call(t)},isString:function(t){return"string"==typeof t},isInt:function(t){return"number"==typeof t&&isFinite(t)&&Math.floor(t)===t},debounce:function(t,e,i){var s;return function(){var n=this,a=arguments,l=function(){s=null,i||t.apply(n,a)},h=i&&!s;clearTimeout(s),s=setTimeout(l,e),h&&t.apply(n,a)}},getRect:function(t,e){var i=window,s=t.getBoundingClientRect(),n=e?i.pageXOffset:0,a=e?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}},preventDefault:function(t){if((t=t||window.event).preventDefault)return t.preventDefault()},includes:function(t,e){return t.indexOf(e)>-1}},n=function(t,e){return t.hasOwnProperty(e)&&(!0===t[e]||t[e].length)},a=function(t){return t.hasOwnProperty("selected")&&!0===t.selected},l=function(t){if(this.config.data||this.el.multiple||!this.el.options.length||(this.el.options[0].selected&&(this.el.options[0].defaultSelected||(this.config.defaultSelected?this.el.options[0].defaultSelected=!0: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),s.each(this.options,function(t,e){e.selected&&e.defaultSelected&&v.call(this,e.idx)},this),this.config.selectedValue&&this.setValue(this.config.selectedValue),this.config.data){!this.el.multiple&&this.config.defaultSelected&&this.el.selectedIndex<0&&v.call(this,0);var e=0;s.each(this.config.data,function(t,i){n(i,"children")?s.each(i.children,function(t,i){a(i)&&v.call(this,e),e++},this):(a(i)&&v.call(this,e),e++)},this)}},h=function(){var t=s.getRect(this.selected),e=this.tree.parentNode.offsetHeight,i=window.innerHeight;t.top+t.height+e>i?(s.addClass(this.container,"inverted"),this.isInverted=!0):(s.removeClass(this.container,"inverted"),this.isInverted=!1),this.optsRect=s.getRect(this.tree)},o=function(t,e,i){t.parentNode?t.parentNode.parentNode||e.appendChild(t.parentNode):e.appendChild(t),s.removeClass(t,"excluded"),i||(t.innerHTML=t.textContent)},c=function(){if(this.items.length){var t=document.createDocumentFragment();if(this.config.pagination){var e=this.pages.slice(0,this.pageIndex);s.each(e,function(e,i){s.each(i,function(e,i){o(i,t,this.customOption)},this)},this)}else s.each(this.items,function(e,i){o(i,t,this.customOption)},this);t.childElementCount&&(s.removeClass(this.items[this.navIndex],"active"),this.navIndex=t.querySelector(".selectr-option").idx,s.addClass(this.items[this.navIndex],"active")),this.tree.appendChild(t)}},r=function(){for(;this.tree.firstChild;)this.tree.removeChild(this.tree.firstChild)},d=function(t){var e=t.target;this.container.contains(e)||!this.opened&&!s.hasClass(this.container,"notice")||this.close()},p=function(t,e){e=e||t;var i=this.customOption?this.config.renderOption(e):t.textContent,n=s.createElement("li",{class:"selectr-option",html:i,role:"treeitem","aria-selected":!1});return n.idx=t.idx,this.items.push(n),t.defaultSelected&&this.defaultSelected.push(t.idx),t.disabled&&(n.disabled=!0,s.addClass(n,"disabled")),n},u=function(){this.requiresPagination=this.config.pagination&&this.config.pagination>0,n(this.config,"width")&&(s.isInt(this.config.width)?this.width=this.config.width+"px":"auto"===this.config.width?this.width="100%":s.includes(this.config.width,"%")&&(this.width=this.config.width)),this.container=s.createElement("div",{class:"selectr-container"}),this.config.customClass&&s.addClass(this.container,this.config.customClass),this.mobileDevice?s.addClass(this.container,"selectr-mobile"):s.addClass(this.container,"selectr-desktop"),this.el.tabIndex=-1,this.config.nativeDropdown||this.mobileDevice?s.addClass(this.el,"selectr-visible"):s.addClass(this.el,"selectr-hidden"),this.selected=s.createElement("div",{class:"selectr-selected",disabled:this.disabled,tabIndex:1,"aria-expanded":!1}),this.label=s.createElement(this.el.multiple?"ul":"span",{class:"selectr-label"});var t=s.createElement("div",{class:"selectr-options-container"});if(this.tree=s.createElement("ul",{class:"selectr-options",role:"tree","aria-hidden":!0,"aria-expanded":!1}),this.notice=s.createElement("div",{class:"selectr-notice"}),this.el.setAttribute("aria-hidden",!0),this.disabled&&(this.el.disabled=!0),this.el.multiple&&(s.addClass(this.label,"selectr-tags"),s.addClass(this.container,"multiple"),this.tags=[],this.selectedValues=[],this.selectedIndexes=[]),this.selected.appendChild(this.label),this.config.clearable&&(this.selectClear=s.createElement("button",{class:"selectr-clear",type:"button"}),this.container.appendChild(this.selectClear),s.addClass(this.container,"clearable")),this.config.taggable){var e=s.createElement("li",{class:"input-tag"});this.input=s.createElement("input",{class:"selectr-tag-input",placeholder:this.config.tagPlaceholder,tagIndex:0,autocomplete:"off",autocorrect:"off",autocapitalize:"off",spellcheck:"false",role:"textbox",type:"search"}),e.appendChild(this.input),this.label.appendChild(e),s.addClass(this.container,"taggable"),this.tagSeperators=[","],this.config.tagSeperators&&(this.tagSeperators=this.tagSeperators.concat(this.config.tagSeperators))}this.config.searchable&&(this.input=s.createElement("input",{class:"selectr-input",tagIndex:-1,autocomplete:"off",autocorrect:"off",autocapitalize:"off",spellcheck:"false",role:"textbox",type:"search"}),this.inputClear=s.createElement("button",{class:"selectr-input-clear",type:"button"}),this.inputContainer=s.createElement("div",{class:"selectr-input-container"}),this.inputContainer.appendChild(this.input),this.inputContainer.appendChild(this.inputClear),t.appendChild(this.inputContainer)),t.appendChild(this.notice),t.appendChild(this.tree),this.items=[],this.options=[],this.el.options.length&&(this.options=[].slice.call(this.el.options));var i=!1,h=0;if(this.el.children.length&&s.each(this.el.children,function(t,e){"OPTGROUP"===e.nodeName?(i=s.createElement("ul",{class:"selectr-optgroup",role:"group",html:"<li class='selectr-optgroup--label'>"+e.label+"</li>"}),s.each(e.children,function(t,e){e.idx=h,i.appendChild(p.call(this,e,i)),h++},this)):(e.idx=h,p.call(this,e),h++)},this),this.config.data&&s.isArray(this.config.data)){this.data=[];var o,c=!1;i=!1,h=0,s.each(this.config.data,function(t,e){n(e,"children")?(c=s.createElement("optgroup",{label:e.text}),i=s.createElement("ul",{class:"selectr-optgroup",role:"group",html:"<li class='selectr-optgroup--label'>"+e.text+"</li>"}),s.each(e.children,function(t,e){(o=new Option(e.text,e.value,!1,a(e))).disabled=n(e,"disabled"),this.options.push(o),c.appendChild(o),o.idx=h,i.appendChild(p.call(this,o,e)),this.data[h]=e,h++},this)):((o=new Option(e.text,e.value,!1,a(e))).disabled=n(e,"disabled"),this.options.push(o),o.idx=h,p.call(this,o,e),this.data[h]=e,h++)},this)}l.call(this,!0);var r;this.navIndex=0;for(var d=0;d<this.items.length;d++)if(r=this.items[d],!s.hasClass(r,"disabled")){s.addClass(r,"active"),this.navIndex=d;break}this.requiresPagination&&(this.pageIndex=1,this.paginate()),this.container.appendChild(this.selected),this.container.appendChild(t),this.placeEl=s.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),f.call(this)},f=function(){var t=this;this.events={},this.events.dismiss=d.bind(this),this.events.navigate=g.bind(this),this.events.reset=this.reset.bind(this),(this.config.nativeDropdown||this.mobileDevice)&&(s.on(this.container,"touchstart",function(t){t.changedTouches[0].target===this.el&&this.toggle()},this),(this.config.nativeDropdown||this.mobileDevice)&&s.on(this.container,"click",function(t){t.target===this.el&&this.toggle()},this),s.on(this.el,"change",function(t){if(this.el.multiple){var e=this.el.querySelectorAll("option:checked"),i=[].slice.call(e).map(function(t){return t.idx});this.clear(),s.each(i,function(t,e){v.call(this,e)},this)}else this.el.selectedIndex>-1&&v.call(this,this.el.selectedIndex)},this)),this.config.nativeDropdown&&s.on(this.container,"keydown",function(e){"Enter"===e.key&&this.selected===document.activeElement&&(this.toggle(),setTimeout(function(){t.el.focus()},200))},this),s.on(this.selected,"click",function(t){this.disabled||this.toggle(),s.preventDefault(t)},this),s.on(this.label,"click",function(t){s.hasClass(t.target,"selectr-tag-remove")&&x.call(this,t.target.parentNode.idx)},this),this.selectClear&&s.on(this.selectClear,"click",this.clear.bind(this)),s.on(this.tree,"mousedown",function(t){s.preventDefault(t)}),s.on(this.tree,"click",function(t){var e=s.closest(t.target,function(t){return t&&s.hasClass(t,"selectr-option")});e&&(s.hasClass(e,"disabled")||(s.hasClass(e,"selected")?(this.el.multiple||!this.el.multiple&&this.config.allowDeselect)&&x.call(this,e.idx):v.call(this,e.idx),this.opened&&!this.el.multiple&&this.close()))},this),s.on(this.tree,"mouseover",function(t){s.hasClass(t.target,"selectr-option")&&(s.hasClass(t.target,"disabled")||(s.removeClass(this.items[this.navIndex],"active"),s.addClass(t.target,"active"),this.navIndex=[].slice.call(this.items).indexOf(t.target)))},this),this.config.searchable&&(s.on(this.input,"focus",function(t){this.searching=!0},this),s.on(this.input,"blur",function(t){this.searching=!1},this),s.on(this.input,"keyup",function(t){this.search(),this.config.taggable||(this.input.value.length?s.addClass(this.input.parentNode,"active"):s.removeClass(this.input.parentNode,"active"))},this),s.on(this.inputClear,"click",function(t){this.input.value=null,I.call(this),this.tree.childElementCount||c.call(this)},this)),this.config.taggable&&s.on(this.input,"keyup",function(t){if(this.search(),this.config.taggable&&this.input.value.length){var e=this.input.value.trim();(13===t.which||s.includes(this.tagSeperators,t.key))&&(s.each(this.tagSeperators,function(t,i){e=e.replace(i,"")}),this.add({value:e,text:e,selected:!0},!0)?(this.close(),I.call(this)):(this.input.value="",this.setMessage("That tag is already in use.")))}},this),this.update=s.debounce(function(){t.opened&&t.config.closeOnScroll&&t.close(),t.width&&(t.container.style.width=t.width),h.call(t)},50),this.requiresPagination&&(this.paginateItems=s.debounce(function(){y.call(this)},50),s.on(this.tree,"scroll",this.paginateItems.bind(this))),s.on(document,"click",this.events.dismiss),s.on(window,"keydown",this.events.navigate),s.on(window,"resize",this.update),s.on(window,"scroll",this.update),this.el.form&&s.on(this.el.form,"reset",this.events.reset)},g=function(t){if(t=t||window.event,this.items.length&&this.opened&&s.includes([13,38,40],t.which)){if(s.preventDefault(t),13===t.which)return!this.config.taggable&&m.call(this,this.navIndex);var e,i=this.items[this.navIndex];switch(t.which){case 38:e=0,this.navIndex>0&&this.navIndex--;break;case 40:e=1,this.navIndex<this.items.length-1&&this.navIndex++}for(this.navigating=!0;s.hasClass(this.items[this.navIndex],"disabled")||s.hasClass(this.items[this.navIndex],"excluded");)if(e?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 n=s.getRect(this.items[this.navIndex]);e?(0===this.navIndex?this.tree.scrollTop=0:n.top+n.height>this.optsRect.top+this.optsRect.height&&(this.tree.scrollTop=this.tree.scrollTop+(n.top+n.height-(this.optsRect.top+this.optsRect.height))),this.navIndex===this.tree.childElementCount-1&&this.requiresPagination&&y.call(this)):0===this.navIndex?this.tree.scrollTop=0:n.top-this.optsRect.top<0&&(this.tree.scrollTop=this.tree.scrollTop+(n.top-this.optsRect.top)),i&&s.removeClass(i,"active"),s.addClass(this.items[this.navIndex],"active")}else this.navigating=!1},m=function(t){var e=this.items[t],i=this.options[t];i.disabled||(i.selected&&s.hasClass(e,"selected")?x.call(this,t):v.call(this,t),this.opened&&!this.el.multiple&&this.close())},v=function(t){var e=this.items[t],i=[].slice.call(this.el.options),n=this.options[t];if(this.el.multiple){if(s.includes(this.selectedIndexes,t))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(n.value),this.selectedIndexes.push(t),b.call(this,e)}else{var a=this.data?this.data[t]:n;this.label.innerHTML=this.customSelected?this.config.renderSelection(a):n.textContent,this.selectedValue=n.value,this.selectedIndex=t,s.each(this.options,function(e,i){var n=this.items[e];e!==t&&(n&&s.removeClass(n,"selected"),i.selected=!1,i.removeAttribute("selected"))},this)}s.includes(i,n)||this.el.add(n),e.setAttribute("aria-selected",!0),s.addClass(e,"selected"),s.addClass(this.container,"has-selected"),n.selected=!0,n.setAttribute("selected",""),this.emit("selectr.select",n),this.emit("selectr.change",n)},x=function(t,e){var i=this.items[t],n=this.options[t];if(this.el.multiple){var a=this.selectedIndexes.indexOf(t);this.selectedIndexes.splice(a,1);var l=this.selectedValues.indexOf(n.value);this.selectedValues.splice(l,1),C.call(this,i),this.tags.length||s.removeClass(this.container,"has-selected")}else{if(!e&&!this.config.clearable&&!this.config.allowDeselect)return!1;this.label.innerHTML="",this.selectedValue=null,this.el.selectedIndex=this.selectedIndex=-1,s.removeClass(this.container,"has-selected")}this.items[t].setAttribute("aria-selected",!1),s.removeClass(this.items[t],"selected"),n.selected=!1,n.removeAttribute("selected"),this.emit("selectr.deselect",n),this.emit("selectr.change",n)},b=function(t){var e,i=this,n=document.createDocumentFragment(),a=this.options[t.idx],l=this.data?this.data[t.idx]:a,h=this.customSelected?this.config.renderSelection(l):a.textContent,o=s.createElement("li",{class:"selectr-tag",html:h}),c=s.createElement("button",{class:"selectr-tag-remove",type:"button"});if(o.appendChild(c),o.idx=t.idx,o.tag=a.value,this.tags.push(o),this.config.sortSelected){var r=this.tags.slice();e=function(t,e){t.replace(/(\d+)|(\D+)/g,function(t,i,s){e.push([i||1/0,s||""])})},r.sort(function(t,s){var n,a,l=[],h=[];for(!0===i.config.sortSelected?(n=t.tag,a=s.tag):"text"===i.config.sortSelected&&(n=t.textContent,a=s.textContent),e(n,l),e(a,h);l.length&&h.length;){var o=l.shift(),c=h.shift(),r=o[0]-c[0]||o[1].localeCompare(c[1]);if(r)return r}return l.length-h.length}),s.each(r,function(t,e){n.appendChild(e)}),this.label.innerHTML=""}else n.appendChild(o);this.config.taggable?this.label.insertBefore(n,this.input.parentNode):this.label.appendChild(n)},C=function(t){var e=!1;s.each(this.tags,function(i,s){s.idx===t.idx&&(e=s)},this),e&&(this.label.removeChild(e),this.tags.splice(this.tags.indexOf(e),1))},y=function(){var t=this.tree;if(t.scrollTop>=t.scrollHeight-t.offsetHeight&&this.pageIndex<this.pages.length){var e=document.createDocumentFragment();s.each(this.pages[this.pageIndex],function(t,i){o(i,e,this.customOption)},this),t.appendChild(e),this.pageIndex++,this.emit("selectr.paginate",{items:this.items.length,total:this.data.length,page:this.pageIndex,pages:this.pages.length})}},I=function(){(this.config.searchable||this.config.taggable)&&(this.input.value=null,this.searching=!1,this.searchBegun=!1,this.config.searchable&&s.removeClass(this.inputContainer,"active"),s.hasClass(this.container,"notice")&&(s.removeClass(this.container,"notice"),s.addClass(this.container,"open"),this.input.focus()),s.each(this.items,function(t,e){s.removeClass(e,"excluded"),this.customOption||(e.innerHTML=e.textContent)},this))},w=function(t,e){var i=new RegExp(t,"i").exec(e.textContent);return!!i&&e.textContent.replace(i[0],"<span class='selectr-match'>"+i[0]+"</span>")},S=function(t,e){if(e=e||{},!t)throw new Error("You must supply either a HTMLSelectElement or a CSS3 selector string.");if(this.el=t,"string"==typeof t&&(this.el=s.querySelect(t)),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(e)};return S.prototype.render=function(t){if(!this.rendered){this.config=s.extend(e,t),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=n(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,i.mixin(this),u.call(this),this.update(),this.optsRect=s.getRect(this.tree),this.rendered=!0;var a=this;setTimeout(function(){a.emit("selectr.init")},20)}},S.prototype.destroy=function(){this.rendered&&(this.emit("selectr.destroy"),"select-one"===this.originalType&&(this.el.multiple=!1),this.config.data&&(this.el.innerHTML=""),s.removeClass(this.el,"selectr-hidden"),this.el.form&&s.off(this.el.form,"reset",this.events.reset),s.off(document,"click",this.events.dismiss),s.off(document,"keydown",this.events.navigate),s.off(window,"resize",this.update),s.off(window,"scroll",this.update),this.container.parentNode.replaceChild(this.el,this.container),this.rendered=!1)},S.prototype.setValue=function(t){var e=s.isArray(t);if(e||(t=t.toString().trim()),!this.el.multiple&&e)return!1;s.each(this.options,function(i,n){(e&&s.includes(t.toString(),n.value)||n.value===t)&&m.call(this,n.idx)},this)},S.prototype.getValue=function(t,e){var i;if(this.el.multiple)t?this.selectedIndexes.length&&((i={}).values=[],s.each(this.selectedIndexes,function(t,e){var s=this.options[e];i.values[t]={value:s.value,text:s.textContent}},this)):i=this.selectedValues.slice();else if(t){var n=this.options[this.selectedIndex];i={value:n.value,text:n.textContent}}else i=this.selectedValue;return t&&e&&(i=JSON.stringify(i)),i},S.prototype.add=function(t,e){if(t){if(this.data=this.data||[],this.items=this.items||[],this.options=this.options||[],s.isArray(t))s.each(t,function(t,i){this.add(i,e)},this);else if(s.isObject(t)){if(e){var i=!1;if(s.each(this.options,function(e,s){s.value.toLowerCase()===t.value.toLowerCase()&&(i=!0)}),i)return!1}var n=s.createElement("option",t);return this.data.push(t),this.options.push(n),n.idx=this.options.length>0?this.options.length-1:0,p.call(this,n),t.selected&&v.call(this,n.idx),n}return this.setPlaceholder(),this.config.pagination&&this.paginate(),!0}},S.prototype.remove=function(t){var e=[];if(s.isArray(t)?s.each(t,function(i,n){s.isInt(n)?e.push(this.getOptionByIndex(n)):s.isString(t)&&e.push(this.getOptionByValue(n))},this):s.isInt(t)?e.push(this.getOptionByIndex(t)):s.isString(t)&&e.push(this.getOptionByValue(t)),e.length){var i;s.each(e,function(t,e){i=e.idx,this.el.remove(e),this.options.splice(i,1);var n=this.items[i].parentNode;n&&n.removeChild(this.items[i]),this.items.splice(i,1),s.each(this.options,function(t,e){e.idx=t,this.items[t].idx=t},this)},this),this.setPlaceholder(),this.config.pagination&&this.paginate()}},S.prototype.removeAll=function(){this.clear(!0),s.each(this.el.options,function(t,e){this.el.remove(e)},this),r.call(this),this.items=[],this.options=[],this.data=[],this.navIndex=0,this.requiresPagination&&(this.requiresPagination=!1,this.pageIndex=1,this.pages=[]),this.setPlaceholder()},S.prototype.search=function(t){if(!this.navigating){t=t||this.input.value;var e=document.createDocumentFragment();if(this.removeMessage(),r.call(this),t.length>1)if(s.each(this.options,function(i,n){var a=this.items[n.idx];s.includes(n.textContent.toLowerCase(),t.toLowerCase())&&!n.disabled?(o(a,e,this.customOption),s.removeClass(a,"excluded"),this.customOption||(a.innerHTML=w(t,n))):s.addClass(a,"excluded")},this),e.childElementCount){var i=this.items[this.navIndex],n=e.firstElementChild;s.removeClass(i,"active"),this.navIndex=n.idx,s.addClass(n,"active")}else this.config.taggable||this.setMessage("no results.");else c.call(this);this.tree.appendChild(e)}},S.prototype.toggle=function(){this.disabled||(this.opened?this.close():this.open())},S.prototype.open=function(){var t=this;return!!this.options.length&&(this.opened||this.emit("selectr.open"),this.opened=!0,this.mobileDevice||this.config.nativeDropdown?(s.addClass(this.container,"native-open"),void(this.config.data&&s.each(this.options,function(t,e){this.el.add(e)},this))):(s.addClass(this.container,"open"),c.call(this),h.call(this),this.tree.scrollTop=0,s.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(){t.input.focus(),t.input.tabIndex=0},10))))},S.prototype.close=function(){if(this.opened&&this.emit("selectr.close"),this.opened=!1,this.mobileDevice||this.config.nativeDropdown)s.removeClass(this.container,"native-open");else{var t=s.hasClass(this.container,"notice");this.config.searchable&&!t&&(this.input.blur(),this.input.tabIndex=-1,this.searching=!1),t&&(s.removeClass(this.container,"notice"),this.notice.textContent=""),s.removeClass(this.container,"open"),s.removeClass(this.container,"native-open"),this.selected.setAttribute("aria-expanded",!1),this.tree.setAttribute("aria-hidden",!0),this.tree.setAttribute("aria-expanded",!1),r.call(this),I.call(this)}},S.prototype.enable=function(){this.disabled=!1,this.el.disabled=!1,this.selected.tabIndex=this.originalIndex,this.el.multiple&&s.each(this.tags,function(t,e){e.lastElementChild.tabIndex=0}),s.removeClass(this.container,"selectr-disabled")},S.prototype.disable=function(t){t||(this.el.disabled=!0),this.selected.tabIndex=-1,this.el.multiple&&s.each(this.tags,function(t,e){e.lastElementChild.tabIndex=-1}),this.disabled=!0,s.addClass(this.container,"selectr-disabled")},S.prototype.reset=function(){this.disabled||(this.clear(),l.call(this,!0),s.each(this.defaultSelected,function(t,e){v.call(this,e)},this),this.emit("selectr.reset"))},S.prototype.clear=function(t){if(this.el.multiple){if(this.selectedIndexes.length){var e=this.selectedIndexes.slice();s.each(e,function(t,e){x.call(this,e)},this)}}else this.selectedIndex>-1&&x.call(this,this.selectedIndex,t);this.emit("selectr.clear")},S.prototype.serialise=function(t){var e=[];return s.each(this.options,function(t,i){var s={value:i.value,text:i.textContent};i.selected&&(s.selected=!0),i.disabled&&(s.disabled=!0),e[t]=s}),t?JSON.stringify(e):e},S.prototype.serialize=function(t){return this.serialise(t)},S.prototype.setPlaceholder=function(t){t=t||this.config.placeholder||this.el.getAttribute("placeholder"),this.options.length||(t="No options available"),this.placeEl.innerHTML=t},S.prototype.paginate=function(){if(this.items.length){var t=this;return this.pages=this.items.map(function(e,i){return i%t.config.pagination==0?t.items.slice(i,i+t.config.pagination):null}).filter(function(t){return t}),this.pages}},S.prototype.setMessage=function(t,e){e&&this.close(),s.addClass(this.container,"notice"),this.notice.textContent=t},S.prototype.removeMessage=function(){s.removeClass(this.container,"notice"),this.notice.innerHTML=""},S.prototype.getOptionByIndex=function(t){return this.options[t]},S.prototype.getOptionByValue=function(t){for(var e=!1,i=0,s=this.options.length;i<s;i++)if(this.options[i].value.trim()===t.toString().trim()){e=this.options[i];break}return e},S}); | ||
!function(t,e){"function"==typeof define&&define.amd?define([],e("Selectr")):"object"==typeof exports?module.exports=e("Selectr"):t.Selectr=e("Selectr")}(this,function(t){"use strict";var e={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..."},i=function(){};i.prototype={on:function(t,e){this._events=this._events||{},this._events[t]=this._events[t]||[],this._events[t].push(e)},off:function(t,e){this._events=this._events||{},t in this._events!=!1&&this._events[t].splice(this._events[t].indexOf(e),1)},emit:function(t){if(this._events=this._events||{},t in this._events!=!1)for(var e=0;e<this._events[t].length;e++)this._events[t][e].apply(this,Array.prototype.slice.call(arguments,1))}},i.mixin=function(t){for(var e=["on","off","emit"],s=0;s<e.length;s++)"function"==typeof t?t.prototype[e[s]]=i.prototype[e[s]]:t[e[s]]=i.prototype[e[s]];return t};var s={extend:function(t,e){e=e||{};var i;for(i in t)t.hasOwnProperty(i)&&(e.hasOwnProperty(i)||(e[i]=t[i]));return e},each:function(t,e,i){if("[object Object]"===Object.prototype.toString.call(t))for(var s in t)Object.prototype.hasOwnProperty.call(t,s)&&e.call(i,s,t[s],t);else for(var n=0,l=t.length;n<l;n++)e.call(i,n,t[n],t)},querySelect:function(t){var e=!1;if(t&&"string"==typeof t){var i=document;switch(t.charAt(0)){case".":e=i.getElementsByClassName(t.substr(1))[0];break;case"#":e=i.getElementById(t.substr(1));break;default:e=i.getElementsByTagName(t)[0]}}return e},createElement:function(t,e){var i=document,n=i.createElement(t);if(e&&s.isObject(e)){var l;for(l in e)if(l in n)n[l]=e[l];else if("html"===l)n.innerHTML=e[l];else if("text"===l){var a=i.createTextNode(e[l]);n.appendChild(a)}else n.setAttribute(l,e[l])}return n},hasClass:function(t,e){if(t)return t.classList?t.classList.contains(e):!!t.className&&!!t.className.match(new RegExp("(\\s|^)"+e+"(\\s|$)"))},addClass:function(t,e){s.hasClass(t,e)||(t.classList?t.classList.add(e):t.className=t.className.trim()+" "+e)},removeClass:function(t,e){s.hasClass(t,e)&&(t.classList?t.classList.remove(e):t.className=t.className.replace(new RegExp("(^|\\s)"+e.split(" ").join("|")+"(\\s|$)","gi")," "))},closest:function(t,e){return t&&t!==document.body&&(e(t)?t:s.closest(t.parentNode,e))},on:function(t,e,i,s){t.addEventListener(e,function(t){s=s||this,i.call(s,t)},!1)},off:function(t,e,i){t.removeEventListener(e,i)},isObject:function(t){return"[object Object]"===Object.prototype.toString.call(t)},isArray:function(t){return"[object Array]"===Object.prototype.toString.call(t)},isString:function(t){return"string"==typeof t},isInt:function(t){return"number"==typeof t&&isFinite(t)&&Math.floor(t)===t},debounce:function(t,e,i){var s;return function(){var n=this,l=arguments,a=function(){s=null,i||t.apply(n,l)},h=i&&!s;clearTimeout(s),s=setTimeout(a,e),h&&t.apply(n,l)}},getRect:function(t,e){var i=window,s=t.getBoundingClientRect(),n=e?i.pageXOffset:0,l=e?i.pageYOffset:0;return{bottom:s.bottom+l,height:s.height,left:s.left+n,right:s.right+n,top:s.top+l,width:s.width}},preventDefault:function(t){if((t=t||window.event).preventDefault)return t.preventDefault()},includes:function(t,e){return t.indexOf(e)>-1}},n=function(t,e){return t.hasOwnProperty(e)&&(!0===t[e]||t[e].length)},l=function(t){return t.hasOwnProperty("selected")&&!0===t.selected},a=function(t){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),s.each(this.options,function(t,e){e.selected&&e.defaultSelected&&v.call(this,e.idx)},this),this.config.selectedValue&&this.setValue(this.config.selectedValue),this.config.data){!this.el.multiple&&this.config.defaultSelected&&this.el.selectedIndex<0&&v.call(this,0);var e=0;s.each(this.config.data,function(t,i){n(i,"children")?s.each(i.children,function(t,i){l(i)&&v.call(this,e),e++},this):(l(i)&&v.call(this,e),e++)},this)}},h=function(){var t=s.getRect(this.selected),e=this.tree.parentNode.offsetHeight,i=window.innerHeight;t.top+t.height+e>i?(s.addClass(this.container,"inverted"),this.isInverted=!0):(s.removeClass(this.container,"inverted"),this.isInverted=!1),this.optsRect=s.getRect(this.tree)},o=function(t,e,i){t.parentNode?t.parentNode.parentNode||e.appendChild(t.parentNode):e.appendChild(t),s.removeClass(t,"excluded"),i||(t.innerHTML=t.textContent)},c=function(){if(this.items.length){var t=document.createDocumentFragment();if(this.config.pagination){var e=this.pages.slice(0,this.pageIndex);s.each(e,function(e,i){s.each(i,function(e,i){o(i,t,this.customOption)},this)},this)}else s.each(this.items,function(e,i){o(i,t,this.customOption)},this);t.childElementCount&&(s.removeClass(this.items[this.navIndex],"active"),this.navIndex=t.querySelector(".selectr-option").idx,s.addClass(this.items[this.navIndex],"active")),this.tree.appendChild(t)}},r=function(){for(;this.tree.firstChild;)this.tree.removeChild(this.tree.firstChild)},d=function(t){var e=t.target;this.container.contains(e)||!this.opened&&!s.hasClass(this.container,"notice")||this.close()},p=function(t,e){e=e||t;var i=this.customOption?this.config.renderOption(e):t.textContent,n=s.createElement("li",{class:"selectr-option",html:i,role:"treeitem","aria-selected":!1});return n.idx=t.idx,this.items.push(n),t.defaultSelected&&this.defaultSelected.push(t.idx),t.disabled&&(n.disabled=!0,s.addClass(n,"disabled")),n},u=function(){this.requiresPagination=this.config.pagination&&this.config.pagination>0,n(this.config,"width")&&(s.isInt(this.config.width)?this.width=this.config.width+"px":"auto"===this.config.width?this.width="100%":s.includes(this.config.width,"%")&&(this.width=this.config.width)),this.container=s.createElement("div",{class:"selectr-container"}),this.config.customClass&&s.addClass(this.container,this.config.customClass),this.mobileDevice?s.addClass(this.container,"selectr-mobile"):s.addClass(this.container,"selectr-desktop"),this.el.tabIndex=-1,this.config.nativeDropdown||this.mobileDevice?s.addClass(this.el,"selectr-visible"):s.addClass(this.el,"selectr-hidden"),this.selected=s.createElement("div",{class:"selectr-selected",disabled:this.disabled,tabIndex:1,"aria-expanded":!1}),this.label=s.createElement(this.el.multiple?"ul":"span",{class:"selectr-label"});var t=s.createElement("div",{class:"selectr-options-container"});if(this.tree=s.createElement("ul",{class:"selectr-options",role:"tree","aria-hidden":!0,"aria-expanded":!1}),this.notice=s.createElement("div",{class:"selectr-notice"}),this.el.setAttribute("aria-hidden",!0),this.disabled&&(this.el.disabled=!0),this.el.multiple&&(s.addClass(this.label,"selectr-tags"),s.addClass(this.container,"multiple"),this.tags=[],this.selectedValues=[],this.selectedIndexes=[]),this.selected.appendChild(this.label),this.config.clearable&&(this.selectClear=s.createElement("button",{class:"selectr-clear",type:"button"}),this.container.appendChild(this.selectClear),s.addClass(this.container,"clearable")),this.config.taggable){var e=s.createElement("li",{class:"input-tag"});this.input=s.createElement("input",{class:"selectr-tag-input",placeholder:this.config.tagPlaceholder,tagIndex:0,autocomplete:"off",autocorrect:"off",autocapitalize:"off",spellcheck:"false",role:"textbox",type:"search"}),e.appendChild(this.input),this.label.appendChild(e),s.addClass(this.container,"taggable"),this.tagSeperators=[","],this.config.tagSeperators&&(this.tagSeperators=this.tagSeperators.concat(this.config.tagSeperators))}this.config.searchable&&(this.input=s.createElement("input",{class:"selectr-input",tagIndex:-1,autocomplete:"off",autocorrect:"off",autocapitalize:"off",spellcheck:"false",role:"textbox",type:"search"}),this.inputClear=s.createElement("button",{class:"selectr-input-clear",type:"button"}),this.inputContainer=s.createElement("div",{class:"selectr-input-container"}),this.inputContainer.appendChild(this.input),this.inputContainer.appendChild(this.inputClear),t.appendChild(this.inputContainer)),t.appendChild(this.notice),t.appendChild(this.tree),this.items=[],this.options=[],this.el.options.length&&(this.options=[].slice.call(this.el.options));var i=!1,h=0;if(this.el.children.length&&s.each(this.el.children,function(t,e){"OPTGROUP"===e.nodeName?(i=s.createElement("ul",{class:"selectr-optgroup",role:"group",html:"<li class='selectr-optgroup--label'>"+e.label+"</li>"}),s.each(e.children,function(t,e){e.idx=h,i.appendChild(p.call(this,e,i)),h++},this)):(e.idx=h,p.call(this,e),h++)},this),this.config.data&&s.isArray(this.config.data)){this.data=[];var o,c=!1;i=!1,h=0,s.each(this.config.data,function(t,e){n(e,"children")?(c=s.createElement("optgroup",{label:e.text}),i=s.createElement("ul",{class:"selectr-optgroup",role:"group",html:"<li class='selectr-optgroup--label'>"+e.text+"</li>"}),s.each(e.children,function(t,e){(o=new Option(e.text,e.value,!1,l(e))).disabled=n(e,"disabled"),this.options.push(o),c.appendChild(o),o.idx=h,i.appendChild(p.call(this,o,e)),this.data[h]=e,h++},this)):((o=new Option(e.text,e.value,!1,l(e))).disabled=n(e,"disabled"),this.options.push(o),o.idx=h,p.call(this,o,e),this.data[h]=e,h++)},this)}a.call(this,!0);var r;this.navIndex=0;for(var d=0;d<this.items.length;d++)if(r=this.items[d],!s.hasClass(r,"disabled")){s.addClass(r,"active"),this.navIndex=d;break}this.requiresPagination&&(this.pageIndex=1,this.paginate()),this.container.appendChild(this.selected),this.container.appendChild(t),this.placeEl=s.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),f.call(this)},f=function(){var t=this;this.events={},this.events.dismiss=d.bind(this),this.events.navigate=g.bind(this),this.events.reset=this.reset.bind(this),(this.config.nativeDropdown||this.mobileDevice)&&(s.on(this.container,"touchstart",function(t){t.changedTouches[0].target===this.el&&this.toggle()},this),(this.config.nativeDropdown||this.mobileDevice)&&s.on(this.container,"click",function(t){t.target===this.el&&this.toggle()},this),s.on(this.el,"change",function(t){if(this.el.multiple){var e=this.el.querySelectorAll("option:checked"),i=[].slice.call(e).map(function(t){return t.idx});this.clear(),s.each(i,function(t,e){v.call(this,e)},this)}else this.el.selectedIndex>-1&&v.call(this,this.el.selectedIndex)},this)),this.config.nativeDropdown&&s.on(this.container,"keydown",function(e){"Enter"===e.key&&this.selected===document.activeElement&&(this.toggle(),setTimeout(function(){t.el.focus()},200))},this),s.on(this.selected,"click",function(t){this.disabled||this.toggle(),s.preventDefault(t)},this),s.on(this.label,"click",function(t){s.hasClass(t.target,"selectr-tag-remove")&&x.call(this,t.target.parentNode.idx)},this),this.selectClear&&s.on(this.selectClear,"click",this.clear.bind(this)),s.on(this.tree,"mousedown",function(t){s.preventDefault(t)}),s.on(this.tree,"click",function(t){var e=s.closest(t.target,function(t){return t&&s.hasClass(t,"selectr-option")});e&&(s.hasClass(e,"disabled")||(s.hasClass(e,"selected")?(this.el.multiple||!this.el.multiple&&this.config.allowDeselect)&&x.call(this,e.idx):v.call(this,e.idx),this.opened&&!this.el.multiple&&this.close()))},this),s.on(this.tree,"mouseover",function(t){s.hasClass(t.target,"selectr-option")&&(s.hasClass(t.target,"disabled")||(s.removeClass(this.items[this.navIndex],"active"),s.addClass(t.target,"active"),this.navIndex=[].slice.call(this.items).indexOf(t.target)))},this),this.config.searchable&&(s.on(this.input,"focus",function(t){this.searching=!0},this),s.on(this.input,"blur",function(t){this.searching=!1},this),s.on(this.input,"keyup",function(t){this.search(),this.config.taggable||(this.input.value.length?s.addClass(this.input.parentNode,"active"):s.removeClass(this.input.parentNode,"active"))},this),s.on(this.inputClear,"click",function(t){this.input.value=null,I.call(this),this.tree.childElementCount||c.call(this)},this)),this.config.taggable&&s.on(this.input,"keyup",function(t){if(this.search(),this.config.taggable&&this.input.value.length){var e=this.input.value.trim();(13===t.which||s.includes(this.tagSeperators,t.key))&&(s.each(this.tagSeperators,function(t,i){e=e.replace(i,"")}),this.add({value:e,text:e,selected:!0},!0)?(this.close(),I.call(this)):(this.input.value="",this.setMessage("That tag is already in use.")))}},this),this.update=s.debounce(function(){t.opened&&t.config.closeOnScroll&&t.close(),t.width&&(t.container.style.width=t.width),h.call(t)},50),this.requiresPagination&&(this.paginateItems=s.debounce(function(){y.call(this)},50),s.on(this.tree,"scroll",this.paginateItems.bind(this))),s.on(document,"click",this.events.dismiss),s.on(window,"keydown",this.events.navigate),s.on(window,"resize",this.update),s.on(window,"scroll",this.update),this.el.form&&s.on(this.el.form,"reset",this.events.reset)},g=function(t){if(t=t||window.event,this.items.length&&this.opened&&s.includes([13,38,40],t.which)){if(s.preventDefault(t),13===t.which)return!(this.config.taggable&&this.input.value.length>0)&&m.call(this,this.navIndex);var e,i=this.items[this.navIndex];switch(t.which){case 38:e=0,this.navIndex>0&&this.navIndex--;break;case 40:e=1,this.navIndex<this.items.length-1&&this.navIndex++}for(this.navigating=!0;s.hasClass(this.items[this.navIndex],"disabled")||s.hasClass(this.items[this.navIndex],"excluded");)if(e?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 n=s.getRect(this.items[this.navIndex]);e?(0===this.navIndex?this.tree.scrollTop=0:n.top+n.height>this.optsRect.top+this.optsRect.height&&(this.tree.scrollTop=this.tree.scrollTop+(n.top+n.height-(this.optsRect.top+this.optsRect.height))),this.navIndex===this.tree.childElementCount-1&&this.requiresPagination&&y.call(this)):0===this.navIndex?this.tree.scrollTop=0:n.top-this.optsRect.top<0&&(this.tree.scrollTop=this.tree.scrollTop+(n.top-this.optsRect.top)),i&&s.removeClass(i,"active"),s.addClass(this.items[this.navIndex],"active")}else this.navigating=!1},m=function(t){var e=this.items[t],i=this.options[t];i.disabled||(i.selected&&s.hasClass(e,"selected")?x.call(this,t):v.call(this,t),this.opened&&!this.el.multiple&&this.close())},v=function(t){var e=this.items[t],i=[].slice.call(this.el.options),n=this.options[t];if(this.el.multiple){if(s.includes(this.selectedIndexes,t))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(n.value),this.selectedIndexes.push(t),b.call(this,e)}else{var l=this.data?this.data[t]:n;this.label.innerHTML=this.customSelected?this.config.renderSelection(l):n.textContent,this.selectedValue=n.value,this.selectedIndex=t,s.each(this.options,function(e,i){var n=this.items[e];e!==t&&(n&&s.removeClass(n,"selected"),i.selected=!1,i.removeAttribute("selected"))},this)}s.includes(i,n)||this.el.add(n),e.setAttribute("aria-selected",!0),s.addClass(e,"selected"),s.addClass(this.container,"has-selected"),n.selected=!0,n.setAttribute("selected",""),this.emit("selectr.select",n),this.emit("selectr.change",n)},x=function(t,e){var i=this.items[t],n=this.options[t];if(this.el.multiple){var l=this.selectedIndexes.indexOf(t);this.selectedIndexes.splice(l,1);var a=this.selectedValues.indexOf(n.value);this.selectedValues.splice(a,1),C.call(this,i),this.tags.length||s.removeClass(this.container,"has-selected")}else{if(!e&&!this.config.clearable&&!this.config.allowDeselect)return!1;this.label.innerHTML="",this.selectedValue=null,this.el.selectedIndex=this.selectedIndex=-1,s.removeClass(this.container,"has-selected")}this.items[t].setAttribute("aria-selected",!1),s.removeClass(this.items[t],"selected"),n.selected=!1,n.removeAttribute("selected"),this.emit("selectr.deselect",n),this.emit("selectr.change",n)},b=function(t){var e,i=this,n=document.createDocumentFragment(),l=this.options[t.idx],a=this.data?this.data[t.idx]:l,h=this.customSelected?this.config.renderSelection(a):l.textContent,o=s.createElement("li",{class:"selectr-tag",html:h}),c=s.createElement("button",{class:"selectr-tag-remove",type:"button"});if(o.appendChild(c),o.idx=t.idx,o.tag=l.value,this.tags.push(o),this.config.sortSelected){var r=this.tags.slice();e=function(t,e){t.replace(/(\d+)|(\D+)/g,function(t,i,s){e.push([i||1/0,s||""])})},r.sort(function(t,s){var n,l,a=[],h=[];for(!0===i.config.sortSelected?(n=t.tag,l=s.tag):"text"===i.config.sortSelected&&(n=t.textContent,l=s.textContent),e(n,a),e(l,h);a.length&&h.length;){var o=a.shift(),c=h.shift(),r=o[0]-c[0]||o[1].localeCompare(c[1]);if(r)return r}return a.length-h.length}),s.each(r,function(t,e){n.appendChild(e)}),this.label.innerHTML=""}else n.appendChild(o);this.config.taggable?this.label.insertBefore(n,this.input.parentNode):this.label.appendChild(n)},C=function(t){var e=!1;s.each(this.tags,function(i,s){s.idx===t.idx&&(e=s)},this),e&&(this.label.removeChild(e),this.tags.splice(this.tags.indexOf(e),1))},y=function(){var t=this.tree;if(t.scrollTop>=t.scrollHeight-t.offsetHeight&&this.pageIndex<this.pages.length){var e=document.createDocumentFragment();s.each(this.pages[this.pageIndex],function(t,i){o(i,e,this.customOption)},this),t.appendChild(e),this.pageIndex++,this.emit("selectr.paginate",{items:this.items.length,total:this.data.length,page:this.pageIndex,pages:this.pages.length})}},I=function(){(this.config.searchable||this.config.taggable)&&(this.input.value=null,this.searching=!1,this.searchBegun=!1,this.config.searchable&&s.removeClass(this.inputContainer,"active"),s.hasClass(this.container,"notice")&&(s.removeClass(this.container,"notice"),s.addClass(this.container,"open"),this.input.focus()),s.each(this.items,function(t,e){s.removeClass(e,"excluded"),this.customOption||(e.innerHTML=e.textContent)},this))},w=function(t,e){var i=new RegExp(t,"i").exec(e.textContent);return!!i&&e.textContent.replace(i[0],"<span class='selectr-match'>"+i[0]+"</span>")},S=function(t,e){if(e=e||{},!t)throw new Error("You must supply either a HTMLSelectElement or a CSS3 selector string.");if(this.el=t,"string"==typeof t&&(this.el=s.querySelect(t)),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(e)};return S.prototype.render=function(t){if(!this.rendered){this.config=s.extend(e,t),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=n(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,i.mixin(this),u.call(this),this.update(),this.optsRect=s.getRect(this.tree),this.rendered=!0,this.el.multiple||(this.el.selectedIndex=this.selectedIndex);var l=this;setTimeout(function(){l.emit("selectr.init")},20)}},S.prototype.destroy=function(){this.rendered&&(this.emit("selectr.destroy"),"select-one"===this.originalType&&(this.el.multiple=!1),this.config.data&&(this.el.innerHTML=""),s.removeClass(this.el,"selectr-hidden"),this.el.form&&s.off(this.el.form,"reset",this.events.reset),s.off(document,"click",this.events.dismiss),s.off(document,"keydown",this.events.navigate),s.off(window,"resize",this.update),s.off(window,"scroll",this.update),this.container.parentNode.replaceChild(this.el,this.container),this.rendered=!1)},S.prototype.setValue=function(t){var e=s.isArray(t);if(e||(t=t.toString().trim()),!this.el.multiple&&e)return!1;s.each(this.options,function(i,n){(e&&s.includes(t.toString(),n.value)||n.value===t)&&m.call(this,n.idx)},this)},S.prototype.getValue=function(t,e){var i;if(this.el.multiple)t?this.selectedIndexes.length&&((i={}).values=[],s.each(this.selectedIndexes,function(t,e){var s=this.options[e];i.values[t]={value:s.value,text:s.textContent}},this)):i=this.selectedValues.slice();else if(t){var n=this.options[this.selectedIndex];i={value:n.value,text:n.textContent}}else i=this.selectedValue;return t&&e&&(i=JSON.stringify(i)),i},S.prototype.add=function(t,e){if(t){if(this.data=this.data||[],this.items=this.items||[],this.options=this.options||[],s.isArray(t))s.each(t,function(t,i){this.add(i,e)},this);else if(s.isObject(t)){if(e){var i=!1;if(s.each(this.options,function(e,s){s.value.toLowerCase()===t.value.toLowerCase()&&(i=!0)}),i)return!1}var n=s.createElement("option",t);return this.data.push(t),this.options.push(n),n.idx=this.options.length>0?this.options.length-1:0,p.call(this,n),t.selected&&v.call(this,n.idx),n}return this.setPlaceholder(),this.config.pagination&&this.paginate(),!0}},S.prototype.remove=function(t){var e=[];if(s.isArray(t)?s.each(t,function(i,n){s.isInt(n)?e.push(this.getOptionByIndex(n)):s.isString(t)&&e.push(this.getOptionByValue(n))},this):s.isInt(t)?e.push(this.getOptionByIndex(t)):s.isString(t)&&e.push(this.getOptionByValue(t)),e.length){var i;s.each(e,function(t,e){i=e.idx,this.el.remove(e),this.options.splice(i,1);var n=this.items[i].parentNode;n&&n.removeChild(this.items[i]),this.items.splice(i,1),s.each(this.options,function(t,e){e.idx=t,this.items[t].idx=t},this)},this),this.setPlaceholder(),this.config.pagination&&this.paginate()}},S.prototype.removeAll=function(){this.clear(!0),s.each(this.el.options,function(t,e){this.el.remove(e)},this),r.call(this),this.items=[],this.options=[],this.data=[],this.navIndex=0,this.requiresPagination&&(this.requiresPagination=!1,this.pageIndex=1,this.pages=[]),this.setPlaceholder()},S.prototype.search=function(t){if(!this.navigating){t=t||this.input.value;var e=document.createDocumentFragment();if(this.removeMessage(),r.call(this),t.length>1)if(s.each(this.options,function(i,n){var l=this.items[n.idx];s.includes(n.textContent.toLowerCase(),t.toLowerCase())&&!n.disabled?(o(l,e,this.customOption),s.removeClass(l,"excluded"),this.customOption||(l.innerHTML=w(t,n))):s.addClass(l,"excluded")},this),e.childElementCount){var i=this.items[this.navIndex],n=e.firstElementChild;s.removeClass(i,"active"),this.navIndex=n.idx,s.addClass(n,"active")}else this.config.taggable||this.setMessage("no results.");else c.call(this);this.tree.appendChild(e)}},S.prototype.toggle=function(){this.disabled||(this.opened?this.close():this.open())},S.prototype.open=function(){var t=this;return!!this.options.length&&(this.opened||this.emit("selectr.open"),this.opened=!0,this.mobileDevice||this.config.nativeDropdown?(s.addClass(this.container,"native-open"),void(this.config.data&&s.each(this.options,function(t,e){this.el.add(e)},this))):(s.addClass(this.container,"open"),c.call(this),h.call(this),this.tree.scrollTop=0,s.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(){t.input.focus(),t.input.tabIndex=0},10))))},S.prototype.close=function(){if(this.opened&&this.emit("selectr.close"),this.opened=!1,this.mobileDevice||this.config.nativeDropdown)s.removeClass(this.container,"native-open");else{var t=s.hasClass(this.container,"notice");this.config.searchable&&!t&&(this.input.blur(),this.input.tabIndex=-1,this.searching=!1),t&&(s.removeClass(this.container,"notice"),this.notice.textContent=""),s.removeClass(this.container,"open"),s.removeClass(this.container,"native-open"),this.selected.setAttribute("aria-expanded",!1),this.tree.setAttribute("aria-hidden",!0),this.tree.setAttribute("aria-expanded",!1),r.call(this),I.call(this)}},S.prototype.enable=function(){this.disabled=!1,this.el.disabled=!1,this.selected.tabIndex=this.originalIndex,this.el.multiple&&s.each(this.tags,function(t,e){e.lastElementChild.tabIndex=0}),s.removeClass(this.container,"selectr-disabled")},S.prototype.disable=function(t){t||(this.el.disabled=!0),this.selected.tabIndex=-1,this.el.multiple&&s.each(this.tags,function(t,e){e.lastElementChild.tabIndex=-1}),this.disabled=!0,s.addClass(this.container,"selectr-disabled")},S.prototype.reset=function(){this.disabled||(this.clear(),a.call(this,!0),s.each(this.defaultSelected,function(t,e){v.call(this,e)},this),this.emit("selectr.reset"))},S.prototype.clear=function(t){if(this.el.multiple){if(this.selectedIndexes.length){var e=this.selectedIndexes.slice();s.each(e,function(t,e){x.call(this,e)},this)}}else this.selectedIndex>-1&&x.call(this,this.selectedIndex,t);this.emit("selectr.clear")},S.prototype.serialise=function(t){var e=[];return s.each(this.options,function(t,i){var s={value:i.value,text:i.textContent};i.selected&&(s.selected=!0),i.disabled&&(s.disabled=!0),e[t]=s}),t?JSON.stringify(e):e},S.prototype.serialize=function(t){return this.serialise(t)},S.prototype.setPlaceholder=function(t){t=t||this.config.placeholder||this.el.getAttribute("placeholder"),this.options.length||(t="No options available"),this.placeEl.innerHTML=t},S.prototype.paginate=function(){if(this.items.length){var t=this;return this.pages=this.items.map(function(e,i){return i%t.config.pagination==0?t.items.slice(i,i+t.config.pagination):null}).filter(function(t){return t}),this.pages}},S.prototype.setMessage=function(t,e){e&&this.close(),s.addClass(this.container,"notice"),this.notice.textContent=t},S.prototype.removeMessage=function(){s.removeClass(this.container,"notice"),this.notice.innerHTML=""},S.prototype.getOptionByIndex=function(t){return this.options[t]},S.prototype.getOptionByValue=function(t){for(var e=!1,i=0,s=this.options.length;i<s;i++)if(this.options[i].value.trim()===t.toString().trim()){e=this.options[i];break}return e},S}); |
{ | ||
"name": "mobius1-selectr", | ||
"version": "2.3.5", | ||
"version": "2.3.6", | ||
"description": "A lightweight, dependency-free, mobile-friendly javascript select box replacement.", | ||
@@ -5,0 +5,0 @@ "main": "dist/selectr.min.js", |
@@ -11,4 +11,6 @@ # Selectr | ||
[![Build Status](https://travis-ci.org/Mobius1/Selectr.svg?branch=master)](https://travis-ci.org/Mobius1/Selectr) [![npm version](https://badge.fury.io/js/mobius1-selectr.svg)](https://badge.fury.io/js/mobius1-selectr) [![license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/Mobius1/Selectr/blob/master/LICENSE) [![Browser Status](https://badges.herokuapp.com/browsers?firefox=26&iexplore=9µsoftedge=12&opera=12&safari=5.1&googlechrome=53)](https://saucelabs.com/u/wml-little-loader) | ||
[![Build Status](https://travis-ci.org/Mobius1/Selectr.svg?branch=master)](https://travis-ci.org/Mobius1/Selectr) [![release](http://github-release-version.herokuapp.com/github/Mobius1/Selectr/release.svg?style=flat)](https://github.com/Mobius1/Selectr/releases/tag/2.3.5) [![npm version](https://badge.fury.io/js/mobius1-selectr.svg)](https://badge.fury.io/js/mobius1-selectr) [![license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/Mobius1/Selectr/blob/master/LICENSE) ![](http://img.badgesize.io/Mobius1/selectr/master/dist/selectr.min.js) ![](http://img.badgesize.io/Mobius1/selectr/master/dist/selectr.min.js?compression=gzip&label=gzipped) [![Average time to resolve an issue](http://isitmaintained.com/badge/resolution/mobius1/selectr.svg)](http://isitmaintained.com/project/mobius1/selectr "Average time to resolve an issue") [![Percentage of issues still open](http://isitmaintained.com/badge/open/mobius1/selectr.svg)](http://isitmaintained.com/project/mobius1/selectr "Percentage of issues still open") | ||
[![Sauce Test Status](https://saucelabs.com/browser-matrix/M0bius01.svg)](https://saucelabs.com/u/M0bius01) | ||
--- | ||
@@ -102,2 +104,7 @@ | ||
### 2.3.6 | ||
* Fixed dropdown positioning bug in Safari on macOS [#28](https://github.com/Mobius1/Selectr/issues/28) | ||
* Fixed first option always being selected in Safari on macOS [#28](https://github.com/Mobius1/Selectr/issues/28) | ||
* Fixed endless dropdown height in IE [#29](https://github.com/Mobius1/Selectr/pull/29) | ||
### 2.3.5 | ||
@@ -104,0 +111,0 @@ * Fixed bug causing first option to be selected when submitting new tag |
/*! | ||
* Selectr 2.3.4 | ||
* Selectr 2.3.6 | ||
* http://mobius.ovh/docs/selectr | ||
@@ -313,9 +313,5 @@ * | ||
// Browser has selected the first option by default | ||
if ( this.el.options[0].selected ) { | ||
if ( !this.el.options[0].defaultSelected ) { | ||
if ( this.config.defaultSelected ) { | ||
this.el.options[0].defaultSelected = true; | ||
} else { | ||
this.el.selectedIndex = -1; | ||
} | ||
if ( this.el.selectedIndex === 0 ) { | ||
if ( !this.el.options[0].defaultSelected && !this.config.defaultSelected ) { | ||
this.el.selectedIndex = -1; | ||
} | ||
@@ -348,2 +344,3 @@ } | ||
if ( !this.el.multiple && this.config.defaultSelected && this.el.selectedIndex < 0 ) { | ||
@@ -1040,7 +1037,7 @@ select.call(this, 0); | ||
if ( e.which === 13 ) { | ||
if ( this.config.taggable ) { | ||
if ( this.config.taggable && this.input.value.length > 0 ) { | ||
return false; | ||
} | ||
return change.call(this, this.navIndex); | ||
@@ -1505,5 +1502,10 @@ } | ||
var _ = this; | ||
// Fixes macOS Safari bug #28 | ||
if ( !this.el.multiple ) { | ||
this.el.selectedIndex = this.selectedIndex; | ||
} | ||
var that = this; | ||
setTimeout(function() { | ||
_.emit("selectr.init"); | ||
that.emit("selectr.init"); | ||
}, 20); | ||
@@ -1510,0 +1512,0 @@ }; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
107289
158
2219