mobius1-selectr
Advanced tools
Comparing version 2.2.3 to 2.2.4
{ | ||
"name": "mobius1-selectr", | ||
"version": "2.2.3", | ||
"version": "2.2.4", | ||
"ignore": [ | ||
@@ -5,0 +5,0 @@ ".gitattributes", |
/*! | ||
* Selectr 2.2.3 | ||
* Selectr 2.2.4 | ||
* 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";var t={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(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))}},i.mixin=function(e){for(var t=["on","off","emit"],s=0;s<t.length;s++)"function"==typeof e?e.prototype[t[s]]=i.prototype[t[s]]:e[t[s]]=i.prototype[t[s]];return e};var s={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,n=i.createElement(e);if(t&&s.isObject(t)){var a;for(a in t)if(a in n)n[a]=t[a];else if("html"===a)n.innerHTML=t[a];else if("text"===a){var l=i.createTextNode(t[a]);n.appendChild(l)}else n.setAttribute(a,t[a])}return n},hasClass:function(e,t){return e.classList?e.classList.contains(t):!!e.className&&!!e.className.match(new RegExp("(\\s|^)"+t+"(\\s|$)"))},addClass:function(e,t){s.hasClass(e,t)||(e.classList?e.classList.add(t):e.className=e.className.trim()+" "+t)},removeClass:function(e,t){s.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:s.closest(e.parentNode,t))},on:function(e,t,i,s){e.addEventListener(t,function(e){s=s||this,i.call(s,e)},!1)},off:function(e,t,i){e.removeEventListener(t,i)},isObject:function(e){return"[object Object]"===Object.prototype.toString.call(e)},isArray:function(e){return"[object Array]"===Object.prototype.toString.call(e)},isInt:function(e){return!isNaN(e)&&function(e){return(e||0)===e}(parseFloat(e))},debounce:function(e,t,i){var s;return function(){var n=this,a=arguments,l=function(){s=null,i||e.apply(n,a)},h=i&&!s;clearTimeout(s),s=setTimeout(l,t),h&&e.apply(n,a)}},getRect: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}},preventDefault:function(e){if((e=e||window.event).preventDefault)return e.preventDefault()},includes:function(e,t){return e.indexOf(t)>-1}},n=function(e){var t=!1;if(e&&"string"==typeof e){var i=document;switch(e.charAt(0)){case".":t=i.getElementsByClassName(e.substr(1))[0];break;case"#":t=i.getElementById(e.substr(1));break;default:t=i.getElementsByTagName(e)[0]}}return t},a=function(e,t){return e.hasOwnProperty(t)&&(!0===e[t]||e[t].length)},l=function(e){return e.hasOwnProperty("selected")&&!0===e.selected},h=function(e){this.el.multiple||(this.config.data&&this.el.selectedIndex<0&&v.call(this,this.items[0]),this.el.selectedIndex>-1&&v.call(this,this.items[this.el.selectedIndex])),this.config.selectedValue&&this.setValue(this.config.selectedValue),this.config.data&&s.each(this.config.data,function(e,t){s.each(this.options,function(e,i){i.value===t.value&&l(t)&&v.call(this,this.items[i.idx])},this)},this)},o=function(){var e=s.getRect(this.selected),t=this.tree.parentNode.offsetHeight,i=window.innerHeight;e.top+e.height+t>i?(s.addClass(this.container,"inverted"),this.isInverted=!0):(s.removeClass(this.container,"inverted"),this.isInverted=!1),this.optsRect=s.getRect(this.tree)},c=function(){if(this.items.length){var e=document.createDocumentFragment();if(this.config.pagination){var t=this.pages.slice(0,this.pageIndex);s.each(t,function(t,i){s.each(i,function(t,i){i.parentNode?i.parentNode.parentNode||e.appendChild(i.parentNode):e.appendChild(i),s.removeClass(i,"excluded"),this.customOption||(i.innerHTML=i.textContent)},this)},this)}else s.each(this.items,function(t,i){i.parentNode?i.parentNode.parentNode||e.appendChild(i.parentNode):e.appendChild(i),s.removeClass(i,"excluded"),this.customOption||(i.innerHTML=i.textContent)},this);this.tree.appendChild(e)}},r=function(){for(;this.tree.firstChild;)this.tree.removeChild(this.tree.firstChild)},d=function(e){var t=e.target;this.container.contains(t)||!this.opened&&!this.container.classList.contains("notice")||this.close()},p=function(){if(this.requiresPagination=this.config.pagination&&this.config.pagination>0,a(this.config,"width")){var e=0;s.isInt(this.config.width)?e=this.config.width+"px":"auto"===this.config.width?e="100%":s.includes(this.config.width,"%")&&(e=this.config.width),this.width=e}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"),this.el.tabIndex=-1,this.config.nativeDropdown||this.mobileDevice?this.el.classList.add("selectr-visible"):this.el.classList.add("selectr-hidden"),this.selected=s.createElement("div",{class:"selectr-selected",disabled:this.disabled,tabIndex:this.el.tabIndex,"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.el.multiple&&this.config.clearable||!this.el.multiple&&this.config.allowDeselect)&&(this.selectClear=s.createElement("button",{class:"selectr-clear",type:"button"}),this.container.appendChild(this.selectClear),s.addClass(this.container,"clearable")),this.config.taggable){var i=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"}),i.appendChild(this.input),this.label.appendChild(i),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 n=!1,o=0;if(this.el.children.length&&s.each(this.el.children,function(e,t){"OPTGROUP"===t.nodeName?(n=s.createElement("ul",{class:"selectr-optgroup",role:"group",html:"<li class='selectr-optgroup--label'>"+t.label+"</li>"}),s.each(t.children,function(e,t){t.idx=o,n.appendChild(f.call(this,t,n)),o++},this)):(t.idx=o,f.call(this,t),o++)},this),this.config.data&&s.isArray(this.config.data)){this.data=[];var c,r=!1;n=!1,o=0,s.each(this.config.data,function(e,t){a(t,"children")?(r=s.createElement("optgroup",{label:t.text}),n=s.createElement("ul",{class:"selectr-optgroup",role:"group",html:"<li class='selectr-optgroup--label'>"+t.text+"</li>"}),s.each(t.children,function(e,t){(c=new Option(t.text,t.value,!1,l(t))).disabled=a(t,"disabled"),this.options.push(c),r.appendChild(c),c.idx=o,n.appendChild(f.call(this,c,t)),this.data[o]=t,o++},this)):((c=new Option(t.text,t.value,!1,l(t))).disabled=a(t,"disabled"),this.options.push(c),c.idx=o,f.call(this,c,t),this.data[o]=t,o++)},this)}h.call(this,!0);var d;this.navIndex=0;for(var p=0;p<this.items.length;p++)if(d=this.items[p],!s.hasClass(d,"disabled")){s.addClass(d,"active"),this.navIndex=p;break}if(this.requiresPagination){var g=this;this.pageIndex=1,this.pages=this.items.map(function(e,t){return t%g.config.pagination==0?g.items.slice(t,t+g.config.pagination):null}).filter(function(e){return e})}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),u.call(this)},u=function(){var e=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)&&(this.ctrlDown=!1,this.mobileDevice?s.on(this.el,"change",function(e){if(this.el.multiple){var t=[];s.each(this.options,function(e,i){i.selected&&t.push(i.idx),b.call(this,this.items[i.idx])},this),s.each(t,function(e,t){v.call(this,this.items[t])},this)}},this):(s.on(document,"keydown",function(e){this.ctrlDown="Control"===e.key},this),s.on(document,"keyup",function(e){this.ctrlDown=!1},this),s.on(this.el,"click",function(e){this.el.multiple&&"OPTION"===e.target.nodeName&&(this.ctrlDown||this.clear(),m.call(this,this.items[e.target.idx]),e.preventDefault())},this)),s.on(this.el,"change",function(e){this.el.multiple||this.el.selectedIndex>-1&&v.call(this,this.items[this.el.selectedIndex])},this)),s.on(this.container,"click",function(e){e.target===this.el&&this.toggle()},this),s.on(this.selected,"click",function(e){this.disabled||this.toggle(),s.preventDefault(e)},this),s.on(this.label,"click",function(e){var t=e.target;s.hasClass(t,"selectr-tag-remove")&&b.call(this,this.items[t.parentNode.idx])},this),(this.el.multiple&&this.config.clearable||!this.el.multiple&&this.config.allowDeselect)&&s.on(this.selectClear,"click",this.clear.bind(this)),s.on(this.tree,"mousedown",function(e){s.preventDefault(e)}),s.on(this.tree,"click",function(e){var t=s.closest(e.target,function(e){return e&&s.hasClass(e,"selectr-option")});t&&(s.hasClass(t,"disabled")||(s.hasClass(t,"selected")?b.call(this,t):v.call(this,t),this.opened&&!this.el.multiple&&this.close()))},this),s.on(document,"click",this.events.dismiss),s.on(document,"keydown",this.events.navigate),s.on(this.tree,"mouseover",function(e){var t=e.target;t.classList.contains("selectr-option")&&(t.classList.contains("disabled")||(this.items[this.navIndex].classList.remove("active"),t.classList.add("active"),this.navIndex=[].slice.call(this.items).indexOf(t)))},this),this.config.searchable&&(s.on(this.input,"keyup",function(e){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(e){this.input.value=null,w.call(this),this.tree.childElementCount||c.call(this)},this)),this.config.taggable&&s.on(this.input,"keyup",function(e){if(this.search(),this.config.taggable&&this.input.value.length){var t=this.input.value.trim();(13===e.which||s.includes(this.tagSeperators,e.key))&&(s.each(this.tagSeperators,function(e,i){t=t.replace(i,"")}),this.addOption({value:t,text:t,selected:!0},!0)?(this.close(),w.call(this)):(this.input.value="",this.setMessage("That tag is already in use.")))}},this),this.update=s.debounce(function(){e.opened&&e.config.closeOnScroll&&e.close(),e.width&&(e.container.style.width=e.width),o.call(e)},50),s.on(window,"resize",this.update),s.on(window,"scroll",this.update),this.requiresPagination&&(this.paginateItems=s.debounce(function(){y.call(this)},50),s.on(this.tree,"scroll",this.paginateItems.bind(this))),this.el.form&&s.on(this.el.form,"reset",this.events.reset)},f=function(e,t){t=t||e;var i=this.customOption?this.config.renderOption(t):e.textContent,n=s.createElement("li",{class:"selectr-option",html:i,role:"treeitem","aria-selected":!1});return n.idx=e.idx,this.items.push(n),(e.selected||e.hasAttribute("selected"))&&v.call(this,n),e.defaultSelected&&this.defaultSelected.push(e.idx),e.disabled&&(n.disabled=!0,s.addClass(n,"disabled")),n},g=function(e){var t=(e=e||window.event).which,i=[13,38,40];if(this.items.length&&this.opened&&s.includes(i,t))if(s.preventDefault(e),13!==t){var n,a=this.items[this.navIndex];switch(t){case 38:n=0,this.navIndex>0&&this.navIndex--;break;case 40:n=1,this.navIndex<this.items.length-1&&this.navIndex++}if(this.navigating=!0,this.items[this.navIndex].classList.contains("disabled")||this.items[this.navIndex].classList.contains("excluded"))for(;this.items[this.navIndex].classList.contains("disabled")||this.items[this.navIndex].classList.contains("excluded");)n?this.navIndex++:this.navIndex--;var l,h,o=this.items[this.navIndex],c=s.getRect(this.items[this.navIndex]),r=this.tree.scrollTop,d=this.optsRect.top;if(n){var p=c.top+c.height;h=r+(p-(l=d+this.optsRect.height)),0===this.navIndex?this.tree.scrollTop=0:p>l&&(this.tree.scrollTop=h),this.navIndex===this.tree.childElementCount-1&&this.requiresPagination&&y.call(this)}else{var u=c.top;h=r+(u-(l=d)),0===this.navIndex?this.tree.scrollTop=0:u-l<0&&(this.tree.scrollTop=h)}a&&s.removeClass(a,"active"),s.addClass(o,"active")}else m.call(this,this.items[this.navIndex])},m=function(e){this.options[e.idx].disabled||(s.hasClass(e,"selected")?b.call(this,e):v.call(this,e),this.opened&&!this.el.multiple&&this.close())},v=function(e){var t=[].slice.call(this.el.options),i=this.options[e.idx];if(this.el.multiple){if(s.includes(this.selectedIndexes,e.idx))return!1;var n=this.config.maxSelections;if(n&&this.tags.length==n)return this.setMessage("A maximum of "+n+" items can be selected.",!0),!1;this.selectedValues.push(i.value),this.selectedIndexes.push(e.idx),x.call(this,e)}else{var a=this.data?this.data[e.idx]:i;this.label.innerHTML=this.customSelected?this.config.renderSelection(a):i.textContent,this.selectedValue=i.value,this.selectedIndex=e.idx,s.each(this.options,function(t,i){var n=this.items[t];t!==e.idx&&(n&&s.removeClass(n,"selected"),i.selected=!1,i.removeAttribute("selected"))},this)}s.includes(t,i)||this.el.add(i),e.setAttribute("aria-selected",!0),s.addClass(e,"selected"),s.addClass(this.container,"has-selected"),i.selected=!0,i.setAttribute("selected",""),this.emit("selectr.select",i),this.emit("selectr.change",i)},b=function(e){var t=this.options[e.idx];if(this.el.multiple){var i=this.selectedIndexes.indexOf(e.idx);this.selectedIndexes.splice(i,1);var n=this.selectedValues.indexOf(t.value);this.selectedValues.splice(n,1),C.call(this,e),this.tags.length||s.removeClass(this.container,"has-selected")}else{if(!this.config.allowDeselect)return!1;this.label.innerHTML="",this.selectedValue=null,this.el.selectedIndex=-1,s.removeClass(this.container,"has-selected")}this.items[e.idx].setAttribute("aria-selected",!1),s.removeClass(this.items[e.idx],"selected"),t.selected=!1,t.removeAttribute("selected"),this.emit("selectr.deselect",t),this.emit("selectr.change",t)},x=function(e){var t=this,i=document.createDocumentFragment(),n=this.options[e.idx],a=this.data?this.data[e.idx]:n,l=this.customSelected?this.config.renderSelection(a):n.textContent,h=s.createElement("li",{class:"selectr-tag",html:l}),o=s.createElement("button",{class:"selectr-tag-remove",type:"button"});if(h.appendChild(o),h.idx=e.idx,h.tag=n.value,this.tags.push(h),this.config.sortSelected){var c=this.tags.slice();c.sort(function(e,i){var s,n,a=[],l=[];for(!0===t.config.sortSelected?(s=e.tag,n=i.tag):"text"===t.config.sortSelected&&(s=e.textContent,n=i.textContent),s.replace(/(\d+)|(\D+)/g,function(e,t,i){a.push([t||1/0,i||""])}),n.replace(/(\d+)|(\D+)/g,function(e,t,i){l.push([t||1/0,i||""])});a.length&&l.length;){var h=a.shift(),o=l.shift(),c=h[0]-o[0]||h[1].localeCompare(o[1]);if(c)return c}return a.length-l.length}),s.each(c,function(e,t){i.appendChild(t)}),this.label.innerHTML=""}else i.appendChild(h);this.config.taggable?this.label.insertBefore(i,this.input.parentNode):this.label.appendChild(i)},C=function(e){var t=!1;s.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))},y=function(){var e=this.tree;if(e.scrollTop>=e.scrollHeight-e.offsetHeight&&this.pageIndex<this.pages.length){var t=document.createDocumentFragment();s.each(this.pages[this.pageIndex],function(e,i){i.parentNode?i.parentNode.parentNode||t.appendChild(i.parentNode):t.appendChild(i)},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})}},w=function(){(this.config.searchable||this.config.taggable)&&(this.input.value=null,this.searching=!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(e,t){s.removeClass(t,"excluded"),this.customOption||(t.innerHTML=t.textContent)},this))},I=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>")},O=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=n(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 O.prototype.render=function(e){if(!this.rendered){this.config=s.extend(t,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=a(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),p.call(this),this.update(),this.optsRect=s.getRect(this.tree),this.rendered=!0;var n=this;setTimeout(function(){n.emit("selectr.init")},20)}},O.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)},O.prototype.setValue=function(e){var t=s.isArray(e);if(t||(e=e.toString().trim()),!this.el.multiple&&t)return!1;s.each(this.options,function(i,n){(t&&s.includes(e.toString(),n.value)||n.value===e)&&m.call(this,this.items[n.idx])},this)},O.prototype.getValue=function(e,t){var i;if(this.el.multiple)e?this.selectedIndexes.length&&((i={}).values=[],s.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 n=this.options[this.selectedIndex];i={value:n.value,text:n.textContent}}else i=this.selectedValue;return e&&t&&(i=JSON.stringify(i)),i},O.prototype.addOption=function(e,t){if(e){if(s.isArray(e))s.each(e,function(e,t){this.addOption(t)},this);else if(s.isObject(e)){if(t){var i=!1;if(s.each(this.options,function(t,s){s.value.toLowerCase()===e.value.toLowerCase()&&(i=!0)}),i)return!1}var n=s.createElement("option",e);this.data&&this.data.push(e),this.options.push(n),n.idx=this.options.length-1;var a=f.call(this,n);return e.selected&&v.call(this,a),n}if(this.setPlaceholder(),this.requiresPagination){var l=this;this.pages=this.items.map(function(e,t){return t%l.config.pagination==0?l.items.slice(t,t+l.config.pagination):null}).filter(function(e){return e})}return!0}},O.prototype.search=function(e){e=e||this.input.value;var t=document.createDocumentFragment();this.removeMessage(),r.call(this),e.length>1?(s.each(this.options,function(i,n){var a=this.items[n.idx];s.includes(n.textContent.toLowerCase(),e.toLowerCase())&&!n.disabled?(a.parentNode?a.parentNode.parentNode||t.appendChild(a.parentNode):t.appendChild(a),s.removeClass(a,"excluded"),this.customOption||(a.innerHTML=I(e,n))):s.addClass(a,"excluded")},this),t.childElementCount||this.setMessage("no results.")):c.call(this),this.tree.appendChild(t)},O.prototype.toggle=function(){this.disabled||(this.opened?this.close():this.open())},O.prototype.open=function(){var e=this;if(this.opened||this.emit("selectr.open"),this.opened=!0,this.mobileDevice||this.config.nativeDropdown)return s.addClass(this.container,"native-open"),void(this.config.data&&s.each(this.options,function(e,t){this.el.add(t)},this));s.addClass(this.container,"open"),c.call(this),o.call(this),this.tree.scrollHeight<=this.optsRect.height&&this.requiresPagination&&y.call(this),s.removeClass(this.container,"notice"),this.selected.setAttribute("aria-expanded",!0),this.tree.setAttribute("aria-hidden",!1),this.tree.setAttribute("aria-expanded",!0),this.config.searchable&&!this.config.taggable&&setTimeout(function(){e.input.focus(),e.input.tabIndex=0},10)},O.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 e=s.hasClass(this.container,"notice");this.config.searchable&&!e&&(this.input.blur(),this.input.tabIndex=-1,this.searching=!1),e&&(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),w.call(this)}},O.prototype.enable=function(){this.disabled=!1,this.el.disabled=!1,this.selected.tabIndex=this.originalIndex,s.each(this.tags,function(e,t){t.lastElementChild.tabIndex=0}),s.removeClass(this.container,"selectr-disabled")},O.prototype.disable=function(e){e||(this.el.disabled=!0),this.selected.tabIndex=-1,s.each(this.tags,function(e,t){t.lastElementChild.tabIndex=-1}),this.disabled=!0,s.addClass(this.container,"selectr-disabled")},O.prototype.reset=function(){this.disabled||(this.clear(),h.call(this,!0),s.each(this.defaultSelected,function(e,t){v.call(this,this.items[t])},this),this.emit("selectr.reset"))},O.prototype.clear=function(){if(this.el.multiple){var e=this.selectedIndexes.slice();s.each(e,function(e,t){b.call(this,this.items[t])},this)}else b.call(this,this.items[this.el.selectedIndex]);this.emit("selectr.clear")},O.prototype.serialise=function(e){var t=[];return s.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},O.prototype.serialize=function(e){return this.serialise(e)},O.prototype.setPlaceholder=function(e){e=e||this.config.placeholder||this.el.getAttribute("placeholder"),this.options.length||(e="No options available"),this.placeEl.innerHTML=e},O.prototype.setMessage=function(e,t){t&&this.close(),s.addClass(this.container,"notice"),this.notice.textContent=e},O.prototype.removeMessage=function(){s.removeClass(this.container,"notice"),this.notice.innerHTML=""},O}); | ||
!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";var t={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(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))}},i.mixin=function(e){for(var t=["on","off","emit"],s=0;s<t.length;s++)"function"==typeof e?e.prototype[t[s]]=i.prototype[t[s]]:e[t[s]]=i.prototype[t[s]];return e};var s={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)},querySelect:function(e){var t=!1;if(e&&"string"==typeof e){var i=document;switch(e.charAt(0)){case".":t=i.getElementsByClassName(e.substr(1))[0];break;case"#":t=i.getElementById(e.substr(1));break;default:t=i.getElementsByTagName(e)[0]}}return t},createElement:function(e,t){var i=document,n=i.createElement(e);if(t&&s.isObject(t)){var a;for(a in t)if(a in n)n[a]=t[a];else if("html"===a)n.innerHTML=t[a];else if("text"===a){var l=i.createTextNode(t[a]);n.appendChild(l)}else n.setAttribute(a,t[a])}return n},hasClass:function(e,t){return e.classList?e.classList.contains(t):!!e.className&&!!e.className.match(new RegExp("(\\s|^)"+t+"(\\s|$)"))},addClass:function(e,t){s.hasClass(e,t)||(e.classList?e.classList.add(t):e.className=e.className.trim()+" "+t)},removeClass:function(e,t){s.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:s.closest(e.parentNode,t))},on:function(e,t,i,s){e.addEventListener(t,function(e){s=s||this,i.call(s,e)},!1)},off:function(e,t,i){e.removeEventListener(t,i)},isObject:function(e){return"[object Object]"===Object.prototype.toString.call(e)},isArray:function(e){return"[object Array]"===Object.prototype.toString.call(e)},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)},h=i&&!s;clearTimeout(s),s=setTimeout(l,t),h&&e.apply(n,a)}},getRect: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}},preventDefault:function(e){if((e=e||window.event).preventDefault)return e.preventDefault()},includes:function(e,t){return e.indexOf(t)>-1}},n=function(e,t){return e.hasOwnProperty(t)&&(!0===e[t]||e[t].length)},a=function(e){return e.hasOwnProperty("selected")&&!0===e.selected},l=function(e){this.el.multiple||(this.config.data&&this.el.selectedIndex<0&&v.call(this,this.items[0]),this.el.selectedIndex>-1&&v.call(this,this.items[this.el.selectedIndex])),this.config.selectedValue&&this.setValue(this.config.selectedValue),this.config.data&&s.each(this.config.data,function(e,t){s.each(this.options,function(e,i){i.value===t.value&&a(t)&&v.call(this,this.items[i.idx])},this)},this)},h=function(){var e=s.getRect(this.selected),t=this.tree.parentNode.offsetHeight,i=window.innerHeight;e.top+e.height+t>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(e,t,i){e.parentNode?e.parentNode.parentNode||t.appendChild(e.parentNode):t.appendChild(e),s.removeClass(e,"excluded"),i||(e.innerHTML=e.textContent)},c=function(){if(this.items.length){var e=document.createDocumentFragment();if(this.config.pagination){var t=this.pages.slice(0,this.pageIndex);s.each(t,function(t,i){s.each(i,function(t,i){o(i,e,this.customOption)},this)},this)}else s.each(this.items,function(t,i){o(i,e,this.customOption)},this);this.tree.appendChild(e)}},r=function(){for(;this.tree.firstChild;)this.tree.removeChild(this.tree.firstChild)},d=function(e){var t=e.target;this.container.contains(t)||!this.opened&&!s.hasClass(this.container,"notice")||this.close()},p=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 e=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.el.multiple&&this.config.clearable||!this.el.multiple&&this.config.allowDeselect)&&(this.selectClear=s.createElement("button",{class:"selectr-clear",type:"button"}),this.container.appendChild(this.selectClear),s.addClass(this.container,"clearable")),this.config.taggable){var t=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"}),t.appendChild(this.input),this.label.appendChild(t),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),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 i=!1,h=0;if(this.el.children.length&&s.each(this.el.children,function(e,t){"OPTGROUP"===t.nodeName?(i=s.createElement("ul",{class:"selectr-optgroup",role:"group",html:"<li class='selectr-optgroup--label'>"+t.label+"</li>"}),s.each(t.children,function(e,t){t.idx=h,i.appendChild(f.call(this,t,i)),h++},this)):(t.idx=h,f.call(this,t),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(e,t){n(t,"children")?(c=s.createElement("optgroup",{label:t.text}),i=s.createElement("ul",{class:"selectr-optgroup",role:"group",html:"<li class='selectr-optgroup--label'>"+t.text+"</li>"}),s.each(t.children,function(e,t){(o=new Option(t.text,t.value,!1,a(t))).disabled=n(t,"disabled"),this.options.push(o),c.appendChild(o),o.idx=h,i.appendChild(f.call(this,o,t)),this.data[h]=t,h++},this)):((o=new Option(t.text,t.value,!1,a(t))).disabled=n(t,"disabled"),this.options.push(o),o.idx=h,f.call(this,o,t),this.data[h]=t,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(e),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),u.call(this)},u=function(){var e=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(e){e.changedTouches[0].target===this.el&&this.toggle()},this),(this.config.nativeDropdown||this.mobileDevice)&&s.on(this.container,"click",function(e){e.target===this.el&&this.toggle()},this),s.on(this.el,"change",function(e){if(this.el.multiple){var t=this.el.querySelectorAll("option:checked"),i=[].slice.call(t).map(function(e){return e.idx});this.clear(),s.each(i,function(e,t){v.call(this,this.items[t])},this)}else this.el.selectedIndex>-1&&v.call(this,this.items[this.el.selectedIndex])},this)),this.config.nativeDropdown&&s.on(this.container,"keydown",function(t){"Enter"===t.key&&this.selected===document.activeElement&&(this.toggle(),setTimeout(function(){e.el.focus()},200))},this),s.on(this.selected,"click",function(e){this.disabled||this.toggle(),s.preventDefault(e)},this),s.on(this.label,"click",function(e){s.hasClass(e.target,"selectr-tag-remove")&&b.call(this,this.items[e.target.parentNode.idx])},this),this.selectClear&&s.on(this.selectClear,"click",this.clear.bind(this)),s.on(this.tree,"mousedown",function(e){s.preventDefault(e)}),s.on(this.tree,"click",function(e){var t=s.closest(e.target,function(e){return e&&s.hasClass(e,"selectr-option")});t&&(s.hasClass(t,"disabled")||(s.hasClass(t,"selected")?b.call(this,t):v.call(this,t),this.opened&&!this.el.multiple&&this.close()))},this),s.on(this.tree,"mouseover",function(e){s.hasClass(e.target,"selectr-option")&&(s.hasClass(e.target,"disabled")||(s.removeClass(this.items[this.navIndex],"active"),s.addClass(e.target,"active"),this.navIndex=[].slice.call(this.items).indexOf(e.target)))},this),this.config.searchable&&(s.on(this.input,"focus",function(e){this.searching=!0},this),s.on(this.input,"blur",function(e){this.searching=!1},this),s.on(this.input,"keyup",function(e){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(e){this.input.value=null,w.call(this),this.tree.childElementCount||c.call(this)},this)),this.config.taggable&&s.on(this.input,"keyup",function(e){if(this.search(),this.config.taggable&&this.input.value.length){var t=this.input.value.trim();(13===e.which||s.includes(this.tagSeperators,e.key))&&(s.each(this.tagSeperators,function(e,i){t=t.replace(i,"")}),this.addOption({value:t,text:t,selected:!0},!0)?(this.close(),w.call(this)):(this.input.value="",this.setMessage("That tag is already in use.")))}},this),this.update=s.debounce(function(){e.opened&&e.config.closeOnScroll&&e.close(),e.width&&(e.container.style.width=e.width),h.call(e)},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)},f=function(e,t){t=t||e;var i=this.customOption?this.config.renderOption(t):e.textContent,n=s.createElement("li",{class:"selectr-option",html:i,role:"treeitem","aria-selected":!1});return n.idx=e.idx,this.items.push(n),(e.selected||e.hasAttribute("selected"))&&v.call(this,n),e.defaultSelected&&this.defaultSelected.push(e.idx),e.disabled&&(n.disabled=!0,s.addClass(n,"disabled")),n},g=function(e){if(e=e||window.event,this.items.length&&this.opened&&s.includes([13,38,40],e.which)){if(s.preventDefault(e),13===e.which)return m.call(this,this.items[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++}if(this.navigating=!0,s.hasClass(this.items[this.navIndex],"disabled")||s.hasClass(this.items[this.navIndex],"excluded"))for(;s.hasClass(this.items[this.navIndex],"disabled")||s.hasClass(this.items[this.navIndex],"excluded");)t?this.navIndex++:this.navIndex--;var n=s.getRect(this.items[this.navIndex]);t?(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")}},m=function(e){var t=this.options[e.idx];t.disabled||(t.selected&&s.hasClass(e,"selected")?b.call(this,e):v.call(this,e),this.opened&&!this.el.multiple&&this.close())},v=function(e){var t=[].slice.call(this.el.options),i=this.options[e.idx];if(this.el.multiple){if(s.includes(this.selectedIndexes,e.idx))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(i.value),this.selectedIndexes.push(e.idx),x.call(this,e)}else{var n=this.data?this.data[e.idx]:i;this.label.innerHTML=this.customSelected?this.config.renderSelection(n):i.textContent,this.selectedValue=i.value,this.selectedIndex=e.idx,s.each(this.options,function(t,i){var n=this.items[t];t!==e.idx&&(n&&s.removeClass(n,"selected"),i.selected=!1,i.removeAttribute("selected"))},this)}s.includes(t,i)||this.el.add(i),e.setAttribute("aria-selected",!0),s.addClass(e,"selected"),s.addClass(this.container,"has-selected"),i.selected=!0,i.setAttribute("selected",""),this.emit("selectr.select",i),this.emit("selectr.change",i)},b=function(e){var t=this.options[e.idx];if(this.el.multiple){var i=this.selectedIndexes.indexOf(e.idx);this.selectedIndexes.splice(i,1);var n=this.selectedValues.indexOf(t.value);this.selectedValues.splice(n,1),C.call(this,e),this.tags.length||s.removeClass(this.container,"has-selected")}else{if(!this.config.allowDeselect)return!1;this.label.innerHTML="",this.selectedValue=null,this.el.selectedIndex=-1,s.removeClass(this.container,"has-selected")}this.items[e.idx].setAttribute("aria-selected",!1),s.removeClass(this.items[e.idx],"selected"),t.selected=!1,t.removeAttribute("selected"),this.emit("selectr.deselect",t),this.emit("selectr.change",t)},x=function(e){var t,i=this,n=document.createDocumentFragment(),a=this.options[e.idx],l=this.data?this.data[e.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=e.idx,o.tag=a.value,this.tags.push(o),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=[],h=[];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,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(e,t){n.appendChild(t)}),this.label.innerHTML=""}else n.appendChild(o);this.config.taggable?this.label.insertBefore(n,this.input.parentNode):this.label.appendChild(n)},C=function(e){var t=!1;s.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))},y=function(){var e=this.tree;if(e.scrollTop>=e.scrollHeight-e.offsetHeight&&this.pageIndex<this.pages.length){var t=document.createDocumentFragment();s.each(this.pages[this.pageIndex],function(e,i){o(i,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})}},w=function(){(this.config.searchable||this.config.taggable)&&(this.input.value=null,this.searching=!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(e,t){s.removeClass(t,"excluded"),this.customOption||(t.innerHTML=t.textContent)},this))},I=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>")},O=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=s.querySelect(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 O.prototype.render=function(e){if(!this.rendered){this.config=s.extend(t,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=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),p.call(this),this.update(),this.optsRect=s.getRect(this.tree),this.rendered=!0;var a=this;setTimeout(function(){a.emit("selectr.init")},20)}},O.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)},O.prototype.setValue=function(e){var t=s.isArray(e);if(t||(e=e.toString().trim()),!this.el.multiple&&t)return!1;s.each(this.options,function(i,n){(t&&s.includes(e.toString(),n.value)||n.value===e)&&m.call(this,this.items[n.idx])},this)},O.prototype.getValue=function(e,t){var i;if(this.el.multiple)e?this.selectedIndexes.length&&((i={}).values=[],s.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 n=this.options[this.selectedIndex];i={value:n.value,text:n.textContent}}else i=this.selectedValue;return e&&t&&(i=JSON.stringify(i)),i},O.prototype.addOption=function(e,t){if(e){if(s.isArray(e))s.each(e,function(e,t){this.addOption(t)},this);else if(s.isObject(e)){if(t){var i=!1;if(s.each(this.options,function(t,s){s.value.toLowerCase()===e.value.toLowerCase()&&(i=!0)}),i)return!1}var n=s.createElement("option",e);this.data&&this.data.push(e),this.options.push(n),n.idx=this.options.length-1;var a=f.call(this,n);return e.selected&&v.call(this,a),n}return this.setPlaceholder(),this.requiresPagination&&this.paginate(),!0}},O.prototype.search=function(e){e=e||this.input.value;var t=document.createDocumentFragment();this.removeMessage(),r.call(this),e.length>1?(s.each(this.options,function(i,n){var a=this.items[n.idx];s.includes(n.textContent.toLowerCase(),e.toLowerCase())&&!n.disabled?(o(a,t,this.customOption),s.removeClass(a,"excluded"),this.customOption||(a.innerHTML=I(e,n))):s.addClass(a,"excluded")},this),t.childElementCount||this.setMessage("no results.")):c.call(this),this.tree.appendChild(t)},O.prototype.toggle=function(){this.disabled||(this.opened?this.close():this.open())},O.prototype.open=function(){var e=this;if(this.opened||this.emit("selectr.open"),this.opened=!0,this.mobileDevice||this.config.nativeDropdown)return s.addClass(this.container,"native-open"),void(this.config.data&&s.each(this.options,function(e,t){this.el.add(t)},this));s.addClass(this.container,"open"),c.call(this),h.call(this),this.tree.scrollHeight<=this.optsRect.height&&this.requiresPagination&&y.call(this),s.removeClass(this.container,"notice"),this.selected.setAttribute("aria-expanded",!0),this.tree.setAttribute("aria-hidden",!1),this.tree.setAttribute("aria-expanded",!0),this.config.searchable&&!this.config.taggable&&setTimeout(function(){e.input.focus(),e.input.tabIndex=0},10)},O.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 e=s.hasClass(this.container,"notice");this.config.searchable&&!e&&(this.input.blur(),this.input.tabIndex=-1,this.searching=!1),e&&(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),w.call(this)}},O.prototype.enable=function(){this.disabled=!1,this.el.disabled=!1,this.selected.tabIndex=this.originalIndex,s.each(this.tags,function(e,t){t.lastElementChild.tabIndex=0}),s.removeClass(this.container,"selectr-disabled")},O.prototype.disable=function(e){e||(this.el.disabled=!0),this.selected.tabIndex=-1,s.each(this.tags,function(e,t){t.lastElementChild.tabIndex=-1}),this.disabled=!0,s.addClass(this.container,"selectr-disabled")},O.prototype.reset=function(){this.disabled||(this.clear(),l.call(this,!0),s.each(this.defaultSelected,function(e,t){v.call(this,this.items[t])},this),this.emit("selectr.reset"))},O.prototype.clear=function(){if(this.el.multiple){var e=this.selectedIndexes.slice();s.each(e,function(e,t){b.call(this,this.items[t])},this)}else b.call(this,this.items[this.el.selectedIndex]);this.emit("selectr.clear")},O.prototype.serialise=function(e){var t=[];return s.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},O.prototype.serialize=function(e){return this.serialise(e)},O.prototype.setPlaceholder=function(e){e=e||this.config.placeholder||this.el.getAttribute("placeholder"),this.options.length||(e="No options available"),this.placeEl.innerHTML=e},O.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}},O.prototype.setMessage=function(e,t){t&&this.close(),s.addClass(this.container,"notice"),this.notice.textContent=e},O.prototype.removeMessage=function(){s.removeClass(this.container,"notice"),this.notice.innerHTML=""},O}); |
{ | ||
"name": "mobius1-selectr", | ||
"version": "2.2.3", | ||
"description": "A lightweight dependency-free javascript select box replacement.", | ||
"version": "2.2.4", | ||
"description": "A lightweight, dependency-free, mobile-friendly javascript select box replacement.", | ||
"main": "dist/selectr.min.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -1,2 +0,3 @@ | ||
# 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) | ||
# Selectr | ||
A lightweight dependency-free select box replacement written in vanilla javascript. Just 6KB minified and gzipped. Similar to Select2 and Chosen without the dependencies. | ||
@@ -6,2 +7,9 @@ | ||
--- | ||
[![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) | ||
--- | ||
Features: | ||
@@ -43,5 +51,5 @@ | ||
```html | ||
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/mobius1/selectr@latest/dist/selectr.min.css"> | ||
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/mobius1/selectr@2.2.3/dist/selectr.min.css"> | ||
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/mobius1/selectr@latest/dist/selectr.min.js"></script> | ||
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/mobius1/selectr@2.2.3/dist/selectr.min.js"></script> | ||
``` | ||
@@ -93,2 +101,13 @@ | ||
### 2.2.4 | ||
* DESKTOP: Fixed incorrect tabIndex on container | ||
* MOBILE: Improved accessibility | ||
* MOBILE: Fixed multiselect requiring more than one click to open | ||
### 2.2.3 | ||
* Fixed `defaultSelected` options not selecting | ||
* Fixed form reset bug | ||
### 2.2.0 | ||
@@ -95,0 +114,0 @@ * Empty select elements can now be used ([#23](https://github.com/Mobius1/Selectr/issues/23)) |
/*! | ||
* Selectr 2.2.3 | ||
* Selectr 2.2.4 | ||
* http://mobius.ovh/docs/selectr | ||
@@ -166,8 +166,8 @@ * | ||
for (p in src) { | ||
if (src.hasOwnProperty(p)) { | ||
if (!props.hasOwnProperty(p)) { | ||
props[p] = src[p]; | ||
if (src.hasOwnProperty(p)) { | ||
if (!props.hasOwnProperty(p)) { | ||
props[p] = src[p]; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return props; | ||
@@ -188,2 +188,20 @@ }, | ||
}, | ||
querySelect: function(a) { | ||
var b = !1; | ||
if (a && "string" === typeof a) { | ||
var c = document, | ||
e = a.charAt(0); | ||
switch (e) { | ||
case ".": | ||
b = c.getElementsByClassName(a.substr(1))[0]; | ||
break; | ||
case "#": | ||
b = c.getElementById(a.substr(1)); | ||
break; | ||
default: | ||
b = c.getElementsByTagName(a)[0]; | ||
} | ||
} | ||
return b; | ||
}, | ||
createElement: function(e, a) { | ||
@@ -227,5 +245,3 @@ var d = document, | ||
isInt: function(val) { | ||
return !isNaN(val) && (function(x) { | ||
return (x || 0) === x; | ||
})(parseFloat(val)); | ||
return typeof val === 'number' && isFinite(val) && Math.floor(val) === val; | ||
}, | ||
@@ -274,25 +290,2 @@ debounce: function(a, b, c) { | ||
/** | ||
* Search for an element (faster than querySelector) | ||
* @param {[type]} selector [description] | ||
* @return {[type]} [description] | ||
*/ | ||
var querySelect = function(a) { | ||
var b = !1; | ||
if (a && "string" == typeof a) { | ||
var c = document, | ||
e = a.charAt(0); | ||
switch (e) { | ||
case ".": | ||
b = c.getElementsByClassName(a.substr(1))[0]; | ||
break; | ||
case "#": | ||
b = c.getElementById(a.substr(1)); | ||
break; | ||
default: | ||
b = c.getElementsByTagName(a)[0]; | ||
} | ||
} | ||
return b; | ||
}; | ||
@@ -359,2 +352,23 @@ var set = function(obj, prop) { | ||
/** | ||
* Append an item to the list | ||
* @param {[type]} item [description] | ||
* @param {[type]} custom [description] | ||
* @return {[type]} [description] | ||
*/ | ||
var appendItem = function(item, parent, custom) { | ||
if ( item.parentNode ) { | ||
if ( !item.parentNode.parentNode ) { | ||
parent.appendChild(item.parentNode); | ||
} | ||
} else { | ||
parent.appendChild(item); | ||
} | ||
util.removeClass(item, "excluded"); | ||
if ( !custom ) { | ||
item.innerHTML = item.textContent; | ||
} | ||
}; | ||
/** | ||
* Render the item list | ||
@@ -365,3 +379,2 @@ * @return {Void} | ||
if ( this.items.length ) { | ||
var f = document.createDocumentFragment(); | ||
@@ -374,14 +387,3 @@ | ||
util.each(items, function(j, item) { | ||
if ( item.parentNode ) { | ||
if ( !item.parentNode.parentNode ) { | ||
f.appendChild(item.parentNode); | ||
} | ||
} else { | ||
f.appendChild(item); | ||
} | ||
util.removeClass(item, "excluded"); | ||
if ( !this.customOption ) { | ||
item.innerHTML = item.textContent; | ||
} | ||
appendItem(item, f, this.customOption); | ||
}, this); | ||
@@ -391,13 +393,3 @@ }, this); | ||
util.each(this.items, function(i, item) { | ||
if ( item.parentNode ) { | ||
if ( !item.parentNode.parentNode ) { | ||
f.appendChild(item.parentNode); | ||
} | ||
} else { | ||
f.appendChild(item); | ||
} | ||
util.removeClass(item, "excluded"); | ||
if ( !this.customOption ) { | ||
item.innerHTML = item.textContent; | ||
} | ||
appendItem(item, f, this.customOption); | ||
}, this); | ||
@@ -427,3 +419,3 @@ } | ||
var target = e.target; | ||
if (!this.container.contains(target) && (this.opened || this.container.classList.contains("notice"))) { | ||
if (!this.container.contains(target) && (this.opened || util.hasClass(this.container, "notice"))) { | ||
this.close(); | ||
@@ -443,13 +435,11 @@ } | ||
if (set(this.config, "width")) { | ||
var width = 0; | ||
if (util.isInt(this.config.width)) { | ||
width = this.config.width + "px"; | ||
this.width = this.config.width + "px"; | ||
} else { | ||
if (this.config.width === "auto") { | ||
width = "100%"; | ||
this.width = "100%"; | ||
} else if (util.includes(this.config.width, "%")) { | ||
width = this.config.width; | ||
this.width = this.config.width; | ||
} | ||
} | ||
this.width = width; | ||
} | ||
@@ -469,2 +459,4 @@ | ||
util.addClass(this.container, "selectr-mobile"); | ||
} else { | ||
util.addClass(this.container, "selectr-desktop"); | ||
} | ||
@@ -477,5 +469,5 @@ | ||
if ( this.config.nativeDropdown || this.mobileDevice ) { | ||
this.el.classList.add("selectr-visible"); | ||
util.addClass(this.el, "selectr-visible"); | ||
} else { | ||
this.el.classList.add("selectr-hidden"); | ||
util.addClass(this.el, "selectr-hidden"); | ||
} | ||
@@ -486,3 +478,3 @@ | ||
disabled: this.disabled, | ||
tabIndex: this.el.tabIndex, // enable tabIndex (#9) | ||
tabIndex: 1, // enable tabIndex (#9) | ||
"aria-expanded": false | ||
@@ -701,9 +693,6 @@ }); | ||
if ( this.requiresPagination ) { | ||
var that = this; | ||
this.pageIndex = 1; | ||
// Create the pages | ||
this.pages = this.items.map( function(v, i) { | ||
return i % that.config.pagination === 0 ? that.items.slice(i, i+that.config.pagination) : null; | ||
}).filter(function(pages){ return pages; }); | ||
this.paginate(); | ||
} | ||
@@ -750,52 +739,31 @@ | ||
if ( this.config.nativeDropdown || this.mobileDevice ) { | ||
this.ctrlDown = false; | ||
// Listen for change event on mobile devices | ||
if ( this.mobileDevice ) { | ||
util.on(this.el, 'change', function(e) { | ||
if ( this.el.multiple ) { | ||
util.on(this.container, "touchstart", function(e) { | ||
if ( e.changedTouches[0].target === this.el ) { | ||
this.toggle(); | ||
} | ||
}, this); | ||
var values = []; | ||
util.each(this.options, function(i, option) { | ||
if ( option.selected ) { | ||
values.push(option.idx); | ||
if ( this.config.nativeDropdown || this.mobileDevice ) { | ||
util.on(this.container, "click", function(e) { | ||
if ( e.target === this.el ) { | ||
this.toggle(); | ||
} | ||
deselect.call(this, this.items[option.idx]); | ||
}, this); | ||
}, this); | ||
} | ||
util.each(values, function(i, idx) { | ||
select.call(this, this.items[idx]); | ||
}, this); | ||
// Listen for the change on the native select | ||
// and update accordingly | ||
util.on(this.el, "change", function(e) { | ||
if ( this.el.multiple ) { | ||
var selected = this.el.querySelectorAll('option:checked'); | ||
} | ||
}, this); | ||
} else { | ||
this.clear(); | ||
// Emulate CTRL + Select | ||
util.on(document, "keydown", function(e) { | ||
this.ctrlDown = e.key === "Control"; | ||
}, this); | ||
util.each(values, function(i, idx) { | ||
select.call(this, this.items[idx]); | ||
}, this); | ||
util.on(document, "keyup", function(e) { | ||
this.ctrlDown = false; | ||
}, this); | ||
util.on(this.el, "click", function(e) { | ||
if ( this.el.multiple ) { | ||
if ( e.target.nodeName === "OPTION" ) { | ||
if ( !this.ctrlDown ) { | ||
this.clear(); | ||
} | ||
change.call(this, this.items[e.target.idx]); | ||
e.preventDefault(); | ||
} | ||
} | ||
}, this); | ||
} | ||
util.on(this.el, "change", function(e) { | ||
if ( !this.el.multiple ) { | ||
} else { | ||
if ( this.el.selectedIndex > -1 ) { | ||
@@ -806,10 +774,21 @@ select.call(this, this.items[this.el.selectedIndex]); | ||
}, this); | ||
} | ||
util.on(this.container, "click", function(e) { | ||
if ( e.target === this.el ) { | ||
// Open the dropdown with Enter key if focused | ||
if ( this.config.nativeDropdown ) { | ||
util.on(this.container, "keydown", function(e) { | ||
if ( e.key === "Enter" && this.selected === document.activeElement ) { | ||
// Show the native | ||
this.toggle(); | ||
} | ||
}, this); | ||
// Focus on the native multiselect | ||
setTimeout(function() { | ||
that.el.focus(); | ||
}, 200); | ||
} | ||
}, this); | ||
} | ||
// Non-native dropdown | ||
util.on(this.selected, "click", function(e) { | ||
@@ -824,11 +803,11 @@ | ||
// Remove tag | ||
util.on(this.label, "click", function(e) { | ||
var target = e.target; | ||
// Remove tag button | ||
if (util.hasClass(target, "selectr-tag-remove")) { | ||
deselect.call(this, this.items[target.parentNode.idx]); | ||
if (util.hasClass(e.target, "selectr-tag-remove")) { | ||
deselect.call(this, this.items[e.target.parentNode.idx]); | ||
} | ||
}, this); | ||
if ( this.el.multiple && this.config.clearable || !this.el.multiple && this.config.allowDeselect ) { | ||
// Clear input | ||
if ( this.selectClear ) { | ||
util.on(this.selectClear, "click", this.clear.bind(this)); | ||
@@ -840,2 +819,3 @@ } | ||
// Select / deselect items | ||
util.on(this.tree, "click", function(e) { | ||
@@ -861,19 +841,11 @@ var item = util.closest(e.target, function(el) { | ||
// Dismiss when clicking outside the container | ||
util.on(document, "click", this.events.dismiss); | ||
util.on(document, "keydown", this.events.navigate); | ||
// util.on(document, "keyup", this.events.keyup); | ||
// Mouseover list items | ||
util.on(this.tree, "mouseover", function(e) { | ||
var target = e.target; | ||
if ( target.classList.contains("selectr-option") ) { | ||
if ( !target.classList.contains("disabled") ) { | ||
this.items[this.navIndex].classList.remove("active"); | ||
if ( util.hasClass(e.target, "selectr-option") ) { | ||
if ( !util.hasClass(e.target, "disabled") ) { | ||
util.removeClass(this.items[this.navIndex], "active"); | ||
target.classList.add("active"); | ||
util.addClass(e.target, "active"); | ||
this.navIndex = [].slice.call(this.items).indexOf(target); | ||
this.navIndex = [].slice.call(this.items).indexOf(e.target); | ||
} | ||
@@ -884,5 +856,13 @@ } | ||
// Searchable | ||
if ( this.config.searchable ) { | ||
// Show / hide the search input clear button | ||
util.on(this.input, "focus", function(e) { | ||
this.searching = true; | ||
}, this); | ||
util.on(this.input, "blur", function(e) { | ||
this.searching = false; | ||
}, this); | ||
util.on(this.input, "keyup", function(e) { | ||
@@ -955,5 +935,2 @@ this.search(); | ||
util.on(window, "resize", this.update); | ||
util.on(window, "scroll", this.update); | ||
if (this.requiresPagination) { | ||
@@ -967,3 +944,9 @@ this.paginateItems = util.debounce(function() { | ||
// Dismiss when clicking outside the container | ||
util.on(document, "click", this.events.dismiss); | ||
util.on(window, "keydown", this.events.navigate); | ||
util.on(window, "resize", this.update); | ||
util.on(window, "scroll", this.update); | ||
// Listen for form.reset() (@ambrooks, #13) | ||
@@ -997,3 +980,3 @@ if ( this.el.form ) { | ||
if (option.selected || option.hasAttribute("selected")) { | ||
select.call(this, opt) | ||
select.call(this, opt); | ||
} | ||
@@ -1021,12 +1004,9 @@ | ||
var key = e.which, | ||
keys = [13, 38, 40]; | ||
// Filter out the keys we don"t want | ||
if (!this.items.length || !this.opened || !util.includes(keys, key)) return; | ||
if (!this.items.length || !this.opened || !util.includes([13, 38, 40], e.which)) return; | ||
util.preventDefault(e); | ||
if ( key === 13 ) { | ||
return void change.call(this, this.items[this.navIndex]); | ||
if ( e.which === 13 ) { | ||
return change.call(this, this.items[this.navIndex]); | ||
} | ||
@@ -1036,3 +1016,3 @@ | ||
switch (key) { | ||
switch (e.which) { | ||
case 38: | ||
@@ -1052,6 +1032,6 @@ direction = 0; | ||
// with disabled / excluded options omitted, skip them instead | ||
if (this.items[this.navIndex].classList.contains("disabled") || this.items[this.navIndex].classList.contains("excluded") ) { | ||
while(this.items[this.navIndex].classList.contains("disabled") || this.items[this.navIndex].classList.contains("excluded") ) { | ||
if ( direction ) { this.navIndex++; } else { this.navIndex--; } | ||
} | ||
if ( util.hasClass(this.items[this.navIndex], "disabled") || util.hasClass(this.items[this.navIndex], "excluded") ) { | ||
while(util.hasClass(this.items[this.navIndex], "disabled") || util.hasClass(this.items[this.navIndex], "excluded") ) { | ||
if ( direction ) { this.navIndex++; } else { this.navIndex--; } | ||
} | ||
} | ||
@@ -1061,29 +1041,18 @@ | ||
// Autoscroll the dropdown during navigation | ||
var nxtEl = this.items[this.navIndex]; | ||
var nxtRct = util.getRect(this.items[this.navIndex]); | ||
var opsTop = this.tree.scrollTop; | ||
var offset = this.optsRect.top; | ||
var curOffset, nxtOffset; | ||
var r = util.getRect(this.items[this.navIndex]); | ||
if (!direction) { | ||
var nxtTp = nxtRct.top; | ||
curOffset = offset; | ||
nxtOffset = opsTop + (nxtTp - curOffset); | ||
if (this.navIndex === 0) { | ||
this.tree.scrollTop = 0; | ||
} else if (nxtTp - curOffset < 0) { | ||
this.tree.scrollTop = nxtOffset; | ||
} else if (r.top - this.optsRect.top < 0) { | ||
this.tree.scrollTop = this.tree.scrollTop + (r.top - this.optsRect.top); | ||
} | ||
} else { | ||
var nxtBottom = nxtRct.top + nxtRct.height; | ||
curOffset = offset + this.optsRect.height; | ||
nxtOffset = opsTop + (nxtBottom - curOffset); | ||
if (this.navIndex === 0) { | ||
this.tree.scrollTop = 0; | ||
} else if (nxtBottom > curOffset) { | ||
this.tree.scrollTop = nxtOffset; | ||
} else if ((r.top + r.height) > (this.optsRect.top + this.optsRect.height)) { | ||
this.tree.scrollTop = this.tree.scrollTop + ((r.top + r.height) - (this.optsRect.top + this.optsRect.height)); | ||
} | ||
// Load another page if needed | ||
if ( this.navIndex === this.tree.childElementCount - 1 && this.requiresPagination ) { | ||
@@ -1097,3 +1066,3 @@ load.call(this); | ||
} | ||
util.addClass(nxtEl, "active"); | ||
util.addClass(this.items[this.navIndex], "active"); | ||
}; | ||
@@ -1113,3 +1082,3 @@ | ||
if (util.hasClass(item, "selected")) { | ||
if (option.selected && util.hasClass(item, "selected")) { | ||
deselect.call(this, item); | ||
@@ -1133,3 +1102,3 @@ } else { | ||
var options = [].slice.call(this.el.options), | ||
option = this.options[item.idx]; | ||
option = this.options[item.idx]; | ||
@@ -1141,5 +1110,4 @@ if ( this.el.multiple ) { | ||
var max = this.config.maxSelections; | ||
if ( max && this.tags.length == max ) { | ||
this.setMessage("A maximum of " + max + " items can be selected.", true); | ||
if ( this.config.maxSelections && this.tags.length === this.config.maxSelections ) { | ||
this.setMessage("A maximum of " + this.config.maxSelections + " items can be selected.", true); | ||
return false; | ||
@@ -1239,3 +1207,3 @@ } | ||
var addTag = function(item) { | ||
var that = this; | ||
var that = this, r; | ||
@@ -1268,2 +1236,7 @@ var docFrag = document.createDocumentFragment(); | ||
// Deal with values that contain numbers | ||
r = function(val, arr) { | ||
val.replace(/(\d+)|(\D+)/g, function(that, $1, $2) { arr.push([$1 || Infinity, $2 || ""]); }); | ||
}; | ||
tags.sort(function(a, b) { | ||
@@ -1279,5 +1252,4 @@ var x = [], y = [], ac, bc; | ||
// Deal with values that contain numbers | ||
ac.replace(/(\d+)|(\D+)/g, function(that, $1, $2) { x.push([$1 || Infinity, $2 || ""]); }); | ||
bc.replace(/(\d+)|(\D+)/g, function(that, $1, $2) { y.push([$1 || Infinity, $2 || ""]); }); | ||
r(ac, x); | ||
r(bc, y); | ||
@@ -1346,9 +1318,3 @@ while(x.length && y.length) { | ||
util.each(this.pages[this.pageIndex], function(i, item) { | ||
if ( item.parentNode ) { | ||
if ( !item.parentNode.parentNode ) { | ||
f.appendChild(item.parentNode); | ||
} | ||
} else { | ||
f.appendChild(item); | ||
} | ||
appendItem(item, f, this.customOption); | ||
}, this); | ||
@@ -1426,3 +1392,3 @@ | ||
if ( typeof el === "string" ) { | ||
this.el = querySelect(el); | ||
this.el = util.querySelect(el); | ||
} | ||
@@ -1670,6 +1636,3 @@ | ||
if ( this.requiresPagination ) { | ||
var that = this; | ||
this.pages = this.items.map( function(v, i) { | ||
return i % that.config.pagination === 0 ? that.items.slice(i, i+that.config.pagination) : null; | ||
}).filter(function(pages){ return pages; }); | ||
this.paginate(); | ||
} | ||
@@ -1704,9 +1667,3 @@ | ||
if ( item.parentNode ) { | ||
if ( !item.parentNode.parentNode ) { | ||
f.appendChild(item.parentNode); | ||
} | ||
} else { | ||
f.appendChild(item); | ||
} | ||
appendItem(item, f, this.customOption); | ||
@@ -1788,6 +1745,4 @@ util.removeClass(item, "excluded"); | ||
if ( scrollHeight <= this.optsRect.height ) { | ||
if ( this.requiresPagination ) { | ||
load.call(this); | ||
} | ||
if ( scrollHeight <= this.optsRect.height && this.requiresPagination ) { | ||
load.call(this); | ||
} | ||
@@ -1983,2 +1938,18 @@ | ||
/** | ||
* Paginate the option list | ||
* @return {Array} | ||
*/ | ||
Selectr.prototype.paginate = function() { | ||
if ( this.items.length ) { | ||
var that = this; | ||
this.pages = this.items.map( function(v, i) { | ||
return i % that.config.pagination === 0 ? that.items.slice(i, i+that.config.pagination) : null; | ||
}).filter(function(pages){ return pages; }); | ||
return this.pages; | ||
} | ||
}; | ||
/** | ||
* Display a message | ||
@@ -1985,0 +1956,0 @@ * @param {string} message The message |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
94785
15
143
2019