mobius1-selectr
Advanced tools
Comparing version 2.2.4 to 2.3.4
{ | ||
"name": "mobius1-selectr", | ||
"version": "2.2.4", | ||
"version": "2.3.4", | ||
"ignore": [ | ||
@@ -5,0 +5,0 @@ ".gitattributes", |
/*! | ||
* Selectr 2.2.4 | ||
* Selectr 2.3.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)},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}); | ||
!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&&m.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&&m.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)&&m.call(this,e),e++},this):(a(i)&&m.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){m.call(this,e)},this)}else this.el.selectedIndex>-1&&m.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):m.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 v.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},v=function(t){var e=this.items[t],i=this.options[t];i.disabled||(i.selected&&s.hasClass(e,"selected")?x.call(this,t):m.call(this,t),this.opened&&!this.el.multiple&&this.close())},m=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)&&v.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&&m.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.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,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,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){m.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}); |
@@ -0,0 +0,0 @@ module.exports = function (grunt) { |
{ | ||
"name": "mobius1-selectr", | ||
"version": "2.2.4", | ||
"version": "2.3.4", | ||
"description": "A lightweight, dependency-free, mobile-friendly javascript select box replacement.", | ||
@@ -5,0 +5,0 @@ "main": "dist/selectr.min.js", |
# 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. | ||
A lightweight, dependency-free, mobile-friendly select box replacement written in vanilla javascript. Just 6.5KB minified and gzipped. Similar to Select2 and Chosen, but without the dependencies. | ||
Supports most modern browsers including IE9. | ||
Supports most modern mobile and desktop browsers including IE9. | ||
Don't forget to check the [wiki](https://github.com/Mobius1/Selectr/wiki) out and view some [demos](https://s.codepen.io/Mobius1/debug/QgdpLN). | ||
--- | ||
@@ -23,7 +25,7 @@ | ||
* Custom styling | ||
* Native select for mobile devices | ||
* Native select UI for mobile devices | ||
* Dynamically add options | ||
* Much more... | ||
[Documentation](https://github.com/Mobius1/Selectr/wiki) | [Demo](http://codepen.io/Mobius1/full/jBqpze/) | ||
[Documentation](https://github.com/Mobius1/Selectr/wiki) | [Demo](https://s.codepen.io/Mobius1/debug/QgdpLN) | ||
@@ -51,5 +53,5 @@ --- | ||
```html | ||
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/mobius1/selectr@2.2.3/dist/selectr.min.css"> | ||
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/mobius1/selectr@2.3.4/dist/selectr.min.css"> | ||
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/mobius1/selectr@2.2.3/dist/selectr.min.js"></script> | ||
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/mobius1/selectr@2.3.4/dist/selectr.min.js"></script> | ||
``` | ||
@@ -101,2 +103,15 @@ | ||
### 2.3.4 | ||
* Added option `defaultSelected` to enable/disable selecting the first option in the list | ||
* Rename method `addOption` to `add` | ||
* Added new method `remove` | ||
* Added new method `removeAll` | ||
* Prevent opening if there aren't any options to display | ||
* Fixed top search result not highlighting ([#26](https://github.com/Mobius1/Selectr/issues/26)) | ||
* Fixed `clear` method not clearing for select-one elements | ||
* Fixed pagination bug | ||
* Fixed `checkDuplicate` not being used recursively | ||
* Fixed first option in a select-multiple element being selected by default | ||
### 2.2.4 | ||
@@ -126,13 +141,3 @@ * DESKTOP: Fixed incorrect tabIndex on container | ||
### v2.1.5 | ||
* Improved accessibility | ||
* New methods `disable()` and `enable()` (see [docs](http://mobius.ovh/docs/selectr/pages/functions)) | ||
* Fixed navigation bug ([#5](https://github.com/Mobius1/Selectr/issues/5)) | ||
### v2.1.4 | ||
* Fixed form.reset() not resetting Selectr elements ([#13](https://github.com/Mobius1/Selectr/issues/13)) | ||
* Fixed autocomplete bug in Chrome ([#12](https://github.com/Mobius1/Selectr/issues/12)) | ||
* Fixed pagination not incrementing when navigating with the down key | ||
* Fixed `reset()` method not checking `data` | ||
# License | ||
@@ -139,0 +144,0 @@ |
/*! | ||
* Selectr 2.2.4 | ||
* Selectr 2.3.4 | ||
* http://mobius.ovh/docs/selectr | ||
@@ -26,2 +26,8 @@ * | ||
/** | ||
* Emulates browser behaviour by selecting the first option by default | ||
* @type {Boolean} | ||
*/ | ||
defaultSelected: true, | ||
/** | ||
* Sets the width of the container | ||
@@ -222,3 +228,4 @@ * @type {String} | ||
hasClass: function(a, b) { | ||
return a.classList ? a.classList.contains(b) : !!a.className && !!a.className.match(new RegExp("(\\s|^)" + b + "(\\s|$)")); | ||
if ( a ) | ||
return a.classList ? a.classList.contains(b) : !!a.className && !!a.className.match(new RegExp("(\\s|^)" + b + "(\\s|$)")); | ||
}, | ||
@@ -244,2 +251,3 @@ addClass: function(a, b) { | ||
isArray: function(a) { return "[object Array]" === Object.prototype.toString.call(a); }, | ||
isString: function(a) { return typeof a === "string"; }, | ||
isInt: function(val) { | ||
@@ -305,13 +313,33 @@ return typeof val === 'number' && isFinite(val) && Math.floor(val) === val; | ||
// Select first option as with native select - #21, #24 | ||
if ( !this.el.multiple ) { | ||
if ( this.config.data && this.el.selectedIndex < 0 ) { | ||
select.call(this, this.items[0]); | ||
// Select first option as with a native select-one element - #21, #24 | ||
if ( !this.config.data && !this.el.multiple && this.el.options.length ) { | ||
// 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 > -1 ) { | ||
select.call(this, this.items[this.el.selectedIndex]); | ||
this.selectedIndex = this.el.selectedIndex; | ||
} | ||
// If we're changing a select-one to select-multiple via the config | ||
// and there are no selected options, the first option will be selected by the browser | ||
// Let's prevent that here. | ||
if ( this.config.multiple && this.originalType === "select-one" && !this.config.data ) { | ||
if ( this.el.options[0].selected && !this.el.options[0].defaultSelected ) { | ||
this.el.options[0].selected = false; | ||
} | ||
} | ||
util.each(this.options, function(i, option) { | ||
if ( option.selected && option.defaultSelected ) { | ||
select.call(this, option.idx); | ||
} | ||
}, this); | ||
if ( this.config.selectedValue ) { | ||
@@ -322,8 +350,23 @@ this.setValue(this.config.selectedValue); | ||
if ( this.config.data ) { | ||
util.each(this.config.data, function(idx, item) { | ||
util.each(this.options, function(i, option) { | ||
if ( option.value === item.value && selected(item) ) { | ||
select.call(this, this.items[option.idx]); | ||
if ( !this.el.multiple && this.config.defaultSelected && this.el.selectedIndex < 0 ) { | ||
select.call(this, 0); | ||
} | ||
var j = 0; | ||
util.each(this.config.data, function(i, opt) { | ||
// Check for group options | ||
if ( set(opt, "children") ) { | ||
util.each(opt.children, function(x, item) { | ||
if ( selected(item) ) { | ||
select.call(this, j); | ||
} | ||
j++; | ||
}, this); | ||
} else { | ||
if ( selected(opt) ) { | ||
select.call(this, j); | ||
} | ||
}, this); | ||
j++; | ||
} | ||
}, this); | ||
@@ -395,2 +438,8 @@ } | ||
if ( f.childElementCount ) { | ||
util.removeClass(this.items[this.navIndex], "active"); | ||
this.navIndex = f.querySelector(".selectr-option").idx; | ||
util.addClass(this.items[this.navIndex], "active"); | ||
} | ||
this.tree.appendChild(f); | ||
@@ -423,2 +472,35 @@ } | ||
/** | ||
* Build a list item from the HTMLOptionElement | ||
* @param {int} i HTMLOptionElement index | ||
* @param {HTMLOptionElement} option | ||
* @param {bool} group Has parent optgroup | ||
* @return {void} | ||
*/ | ||
var createItem = function(option, data) { | ||
data = data || option; | ||
var content = this.customOption ? this.config.renderOption(data) : option.textContent; | ||
var opt = util.createElement("li", { | ||
class: "selectr-option", | ||
html: content, | ||
role: "treeitem", | ||
"aria-selected": false | ||
}); | ||
opt.idx = option.idx; | ||
this.items.push(opt); | ||
if ( option.defaultSelected ) { | ||
this.defaultSelected.push(option.idx); | ||
} | ||
if (option.disabled) { | ||
opt.disabled = true; | ||
util.addClass(opt, "disabled"); | ||
} | ||
return opt; | ||
}; | ||
/** | ||
* Build the container | ||
@@ -518,3 +600,3 @@ * @return {Void} | ||
if ( this.el.multiple && this.config.clearable || !this.el.multiple && this.config.allowDeselect ) { | ||
if ( this.config.clearable ) { | ||
this.selectClear = util.createElement("button", { | ||
@@ -607,3 +689,3 @@ class: "selectr-clear", | ||
el.idx = j; | ||
group.appendChild(buildOption.call(this, el, group)); | ||
group.appendChild(createItem.call(this, el, group)); | ||
j++; | ||
@@ -613,3 +695,3 @@ }, this); | ||
element.idx = j; | ||
buildOption.call(this, element); | ||
createItem.call(this, element); | ||
j++; | ||
@@ -649,3 +731,3 @@ } | ||
group.appendChild(buildOption.call(this, option, data)); | ||
group.appendChild(createItem.call(this, option, data)); | ||
@@ -665,3 +747,3 @@ this.data[j] = data; | ||
buildOption.call(this, option, opt); | ||
createItem.call(this, option, opt); | ||
@@ -758,2 +840,3 @@ this.data[j] = opt; | ||
var selected = this.el.querySelectorAll('option:checked'); | ||
var values = [].slice.call(selected).map(function(option) { return option.idx; }); | ||
@@ -763,3 +846,3 @@ this.clear(); | ||
util.each(values, function(i, idx) { | ||
select.call(this, this.items[idx]); | ||
select.call(this, idx); | ||
}, this); | ||
@@ -769,3 +852,3 @@ | ||
if ( this.el.selectedIndex > -1 ) { | ||
select.call(this, this.items[this.el.selectedIndex]); | ||
select.call(this, this.el.selectedIndex); | ||
} | ||
@@ -805,3 +888,3 @@ } | ||
if (util.hasClass(e.target, "selectr-tag-remove")) { | ||
deselect.call(this, this.items[e.target.parentNode.idx]); | ||
deselect.call(this, e.target.parentNode.idx); | ||
} | ||
@@ -827,5 +910,7 @@ }, this); | ||
if ( util.hasClass(item, "selected") ) { | ||
deselect.call(this, item); | ||
if ( this.el.multiple || !this.el.multiple && this.config.allowDeselect ) { | ||
deselect.call(this, item.idx); | ||
} | ||
} else { | ||
select.call(this, item); | ||
select.call(this, item.idx); | ||
} | ||
@@ -903,3 +988,3 @@ | ||
var option = this.addOption({ | ||
var option = this.add({ | ||
value: val, | ||
@@ -955,39 +1040,2 @@ text: val, | ||
/** | ||
* Build a list item from the HTMLOptionElement | ||
* @param {int} i HTMLOptionElement index | ||
* @param {HTMLOptionElement} option | ||
* @param {bool} group Has parent optgroup | ||
* @return {void} | ||
*/ | ||
var buildOption = function(option, data) { | ||
data = data || option; | ||
var content = this.customOption ? this.config.renderOption(data) : option.textContent; | ||
var opt = util.createElement("li", { | ||
class: "selectr-option", | ||
html: content, | ||
role: "treeitem", | ||
"aria-selected": false | ||
}); | ||
opt.idx = option.idx; | ||
this.items.push(opt); | ||
if (option.selected || option.hasAttribute("selected")) { | ||
select.call(this, opt); | ||
} | ||
if ( option.defaultSelected ) { | ||
this.defaultSelected.push(option.idx); | ||
} | ||
if (option.disabled) { | ||
opt.disabled = true; | ||
util.addClass(opt, "disabled"); | ||
} | ||
return opt; | ||
}; | ||
/** | ||
* Navigate through the dropdown | ||
@@ -1001,3 +1049,6 @@ * @param {obj} e | ||
// Filter out the keys we don"t want | ||
if (!this.items.length || !this.opened || !util.includes([13, 38, 40], e.which)) return; | ||
if (!this.items.length || !this.opened || !util.includes([13, 38, 40], e.which)) { | ||
this.navigating = false; | ||
return; | ||
} | ||
@@ -1007,3 +1058,3 @@ util.preventDefault(e); | ||
if ( e.which === 13 ) { | ||
return change.call(this, this.items[this.navIndex]); | ||
return change.call(this, this.navIndex); | ||
} | ||
@@ -1028,9 +1079,20 @@ | ||
// with disabled / excluded options omitted, skip them instead | ||
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--; } | ||
while( util.hasClass(this.items[this.navIndex], "disabled") || util.hasClass(this.items[this.navIndex], "excluded") ) { | ||
if ( direction ) { | ||
this.navIndex++; | ||
} else { | ||
this.navIndex--; | ||
} | ||
if ( this.searching ) { | ||
if ( this.navIndex > this.tree.lastElementChild.idx ) { | ||
this.navIndex = this.tree.lastElementChild.idx; | ||
break; | ||
} else if ( this.navIndex < this.tree.firstElementChild.idx ) { | ||
this.navIndex = this.tree.firstElementChild.idx; | ||
break; | ||
} | ||
} | ||
} | ||
// Autoscroll the dropdown during navigation | ||
@@ -1061,2 +1123,3 @@ var r = util.getRect(this.items[this.navIndex]); | ||
} | ||
util.addClass(this.items[this.navIndex], "active"); | ||
@@ -1070,4 +1133,5 @@ }; | ||
*/ | ||
var change = function(item) { | ||
var option = this.options[item.idx]; | ||
var change = function(index) { | ||
var item = this.items[index], | ||
option = this.options[index]; | ||
@@ -1079,5 +1143,5 @@ if (option.disabled) { | ||
if (option.selected && util.hasClass(item, "selected")) { | ||
deselect.call(this, item); | ||
deselect.call(this, index); | ||
} else { | ||
select.call(this, item); | ||
select.call(this, index); | ||
} | ||
@@ -1095,9 +1159,10 @@ | ||
*/ | ||
var select = function(item) { | ||
var select = function(index) { | ||
var options = [].slice.call(this.el.options), | ||
option = this.options[item.idx]; | ||
var item = this.items[index], | ||
options = [].slice.call(this.el.options), | ||
option = this.options[index]; | ||
if ( this.el.multiple ) { | ||
if (util.includes(this.selectedIndexes, item.idx) ) { | ||
if (util.includes(this.selectedIndexes, index) ) { | ||
return false; | ||
@@ -1112,11 +1177,11 @@ } | ||
this.selectedValues.push(option.value); | ||
this.selectedIndexes.push(item.idx); | ||
this.selectedIndexes.push(index); | ||
addTag.call(this, item); | ||
} else { | ||
var data = this.data ? this.data[item.idx] : option; | ||
var data = this.data ? this.data[index] : option; | ||
this.label.innerHTML = this.customSelected ? this.config.renderSelection(data) : option.textContent; | ||
this.selectedValue = option.value; | ||
this.selectedIndex = item.idx; | ||
this.selectedIndex = index; | ||
@@ -1126,3 +1191,3 @@ util.each(this.options, function(i, o) { | ||
if ( i !== item.idx ) { | ||
if ( i !== index ) { | ||
if ( opt ) { | ||
@@ -1158,7 +1223,8 @@ util.removeClass(opt, "selected"); | ||
*/ | ||
var deselect = function(item) { | ||
var option = this.options[item.idx]; | ||
var deselect = function(index, force) { | ||
var item = this.items[index], | ||
option = this.options[index]; | ||
if ( this.el.multiple ) { | ||
var selIndex = this.selectedIndexes.indexOf(item.idx); | ||
var selIndex = this.selectedIndexes.indexOf(index); | ||
this.selectedIndexes.splice(selIndex, 1); | ||
@@ -1175,3 +1241,4 @@ | ||
} else { | ||
if ( !this.config.allowDeselect ) { | ||
if ( !force && !this.config.clearable && !this.config.allowDeselect ) { | ||
return false; | ||
@@ -1183,3 +1250,3 @@ } | ||
this.el.selectedIndex = -1; | ||
this.el.selectedIndex = this.selectedIndex = -1; | ||
@@ -1190,5 +1257,5 @@ util.removeClass(this.container, "has-selected"); | ||
this.items[item.idx].setAttribute( "aria-selected", false ); | ||
this.items[index].setAttribute( "aria-selected", false ); | ||
util.removeClass(this.items[item.idx], "selected"); | ||
util.removeClass(this.items[index], "selected"); | ||
@@ -1340,2 +1407,3 @@ option.selected = false; | ||
this.searching = false; | ||
this.searchBegun = false; | ||
if ( this.config.searchable ) { | ||
@@ -1525,3 +1593,3 @@ util.removeClass(this.inputContainer, "active"); | ||
if (isArray && util.includes(value.toString(), option.value) || option.value === value) { | ||
change.call(this, this.items[option.idx]); | ||
change.call(this, option.idx); | ||
} | ||
@@ -1576,11 +1644,16 @@ }, this); | ||
/** | ||
* Add a new option | ||
* Add a new option or options | ||
* @param {object} data | ||
*/ | ||
Selectr.prototype.addOption = function(data, checkDuplicate) { | ||
Selectr.prototype.add = function(data, checkDuplicate) { | ||
if ( data ) { | ||
this.data = this.data || []; | ||
this.items = this.items || []; | ||
this.options = this.options || []; | ||
if ( util.isArray(data) ) { | ||
// We have an array on items | ||
util.each(data, function(i, obj) { | ||
this.addOption(obj); | ||
this.add(obj, checkDuplicate); | ||
}, this); | ||
@@ -1608,5 +1681,3 @@ } | ||
if ( this.data ) { | ||
this.data.push(data); | ||
} | ||
this.data.push(data); | ||
@@ -1617,10 +1688,10 @@ // Add the new option to the list | ||
// Add the index for later use | ||
option.idx = this.options.length - 1; | ||
option.idx = this.options.length > 0 ? this.options.length - 1 : 0; | ||
// Create a new item | ||
var item = buildOption.call(this, option); | ||
createItem.call(this, option); | ||
// Select the item if required | ||
if ( data.selected ) { | ||
select.call(this, item); | ||
select.call(this, option.idx); | ||
} | ||
@@ -1636,3 +1707,3 @@ | ||
// Recount the pages | ||
if ( this.requiresPagination ) { | ||
if ( this.config.pagination ) { | ||
this.paginate(); | ||
@@ -1646,2 +1717,98 @@ } | ||
/** | ||
* Remove an option or options | ||
* @param {Mixed} o Array, integer (index) or string (value) | ||
* @return {Void} | ||
*/ | ||
Selectr.prototype.remove = function(o) { | ||
var options = []; | ||
if ( util.isArray(o) ) { | ||
util.each(o, function(i, opt) { | ||
if ( util.isInt(opt) ) { | ||
options.push(this.getOptionByIndex(opt)); | ||
} else if ( util.isString(o) ) { | ||
options.push(this.getOptionByValue(opt)); | ||
} | ||
}, this); | ||
} else if ( util.isInt(o) ) { | ||
options.push(this.getOptionByIndex(o)); | ||
} else if ( util.isString(o) ) { | ||
options.push(this.getOptionByValue(o)); | ||
} | ||
if ( options.length ) { | ||
var index; | ||
util.each(options, function(i, option) { | ||
index = option.idx; | ||
// Remove the HTMLOptionElement | ||
this.el.remove(option); | ||
// Remove the reference from the option array | ||
this.options.splice(index, 1); | ||
// If the item has a parentNode (group element) it needs to be removed | ||
// otherwise the render function will still append it to the dropdown | ||
var parentNode = this.items[index].parentNode; | ||
if ( parentNode ) { | ||
parentNode.removeChild(this.items[index]); | ||
} | ||
// Remove reference from the items array | ||
this.items.splice(index, 1); | ||
// Reset the indexes | ||
util.each(this.options, function(i, opt) { | ||
opt.idx = i; | ||
this.items[i].idx = i; | ||
}, this); | ||
}, this); | ||
// We may have had an empty select now so update | ||
// the placeholder to reflect the changes. | ||
this.setPlaceholder(); | ||
// Recount the pages | ||
if ( this.config.pagination ) { | ||
this.paginate(); | ||
} | ||
} | ||
}; | ||
/** | ||
* Remove all options | ||
*/ | ||
Selectr.prototype.removeAll = function() { | ||
// Clear any selected options | ||
this.clear(true); | ||
// Remove the HTMLOptionElements | ||
util.each(this.el.options, function(i, option) { | ||
this.el.remove(option); | ||
}, this); | ||
// Empty the dropdown | ||
flush.call(this); | ||
// Reset variables | ||
this.items = []; | ||
this.options = []; | ||
this.data = []; | ||
this.navIndex = 0; | ||
if ( this.requiresPagination ) { | ||
this.requiresPagination = false; | ||
this.pageIndex = 1; | ||
this.pages = []; | ||
} | ||
// Update the placeholder | ||
this.setPlaceholder(); | ||
}; | ||
/** | ||
* Perform a search | ||
@@ -1651,2 +1818,5 @@ * @param {string} query The query string | ||
Selectr.prototype.search = function(string) { | ||
if ( this.navigating ) return; | ||
string = string || this.input.value; | ||
@@ -1686,2 +1856,12 @@ | ||
this.setMessage("no results."); | ||
} else { | ||
// Highlight top result (@binary-koan #26) | ||
var prevEl = this.items[this.navIndex]; | ||
var firstEl = f.firstElementChild; | ||
util.removeClass(prevEl, "active"); | ||
this.navIndex = firstEl.idx; | ||
util.addClass(firstEl, "active"); | ||
} | ||
@@ -1694,6 +1874,4 @@ | ||
this.tree.appendChild(f); | ||
}; | ||
// Selectr proto | ||
/** | ||
@@ -1721,2 +1899,6 @@ * Toggle the dropdown | ||
if ( !this.options.length ) { | ||
return false; | ||
} | ||
if ( !this.opened ) { | ||
@@ -1748,8 +1930,4 @@ this.emit("selectr.open"); | ||
var scrollHeight = this.tree.scrollHeight; | ||
this.tree.scrollTop = 0; | ||
if ( scrollHeight <= this.optsRect.height && this.requiresPagination ) { | ||
load.call(this); | ||
} | ||
util.removeClass(this.container, "notice"); | ||
@@ -1762,3 +1940,2 @@ | ||
if (this.config.searchable && !this.config.taggable) { | ||
@@ -1866,3 +2043,3 @@ setTimeout(function() { | ||
util.each(this.defaultSelected, function(i,idx) { | ||
select.call(this, this.items[idx]); | ||
select.call(this, idx); | ||
}, this); | ||
@@ -1878,3 +2055,3 @@ | ||
*/ | ||
Selectr.prototype.clear = function() { | ||
Selectr.prototype.clear = function(force) { | ||
@@ -1885,10 +2062,14 @@ if ( this.el.multiple ) { | ||
// Copy the array or we'll get an error | ||
var indexes = this.selectedIndexes.slice(); | ||
if ( this.selectedIndexes.length ) { | ||
// Copy the array or we'll get an error | ||
var indexes = this.selectedIndexes.slice(); | ||
util.each(indexes, function(i, idx) { | ||
deselect.call(this, this.items[idx]); | ||
}, this); | ||
util.each(indexes, function(i, idx) { | ||
deselect.call(this, idx); | ||
}, this); | ||
} | ||
} else { | ||
deselect.call(this, this.items[this.el.selectedIndex]); | ||
if ( this.selectedIndex > -1 ) { | ||
deselect.call(this, this.selectedIndex, force); | ||
} | ||
} | ||
@@ -1964,3 +2145,3 @@ | ||
* Display a message | ||
* @param {string} message The message | ||
* @param {String} message The message | ||
*/ | ||
@@ -1983,4 +2164,30 @@ Selectr.prototype.setMessage = function(message, close) { | ||
/** | ||
* Get an option via it's index | ||
* @param {Integer} index The index of the HTMLOptionElement required | ||
* @return {HTMLOptionElement} | ||
*/ | ||
Selectr.prototype.getOptionByIndex = function(index) { | ||
return this.options[index]; | ||
}; | ||
/** | ||
* Get an option via it's value | ||
* @param {String} value The value of the HTMLOptionElement required | ||
* @return {HTMLOptionElement} | ||
*/ | ||
Selectr.prototype.getOptionByValue = function(value) { | ||
var option = false; | ||
for ( var i = 0, l = this.options.length; i < l; i++ ) { | ||
if ( this.options[i].value.trim() === value.toString().trim() ) { | ||
option = this.options[i]; | ||
break; | ||
} | ||
} | ||
return option; | ||
}; | ||
return Selectr; | ||
})); |
@@ -8,3 +8,3 @@ (function() { | ||
var st = new Selectr(s, { | ||
var selector = new Selectr(s, { | ||
data: [ | ||
@@ -27,4 +27,13 @@ { value: 'value-1', text: 'Value 1' }, | ||
QUnit.test( "init", function( assert ) { | ||
assert.ok( Object.prototype.toString.call(st) === '[object Object]', "Passed!" ); | ||
assert.ok( Object.prototype.toString.call(selector) === '[object Object]', "Passed!" ); | ||
}); | ||
QUnit.test( "selected", function( assert ) { | ||
assert.ok( selector.options[1].selected === true && selector.options[4].selected === true, "Passed!" ); | ||
}); | ||
QUnit.test( "not-selected", function( assert ) { | ||
assert.ok( selector.options[0].selected === false && selector.options[2].selected === false, "Passed!" ); | ||
}); | ||
QUnit.test( "select-one", function( assert ) { | ||
assert.ok( selector.originalType === "select-one", "Passed!" ); | ||
}); | ||
})(); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
105525
2231
148