mobius1-selectr
Advanced tools
Comparing version 2.1.6 to 2.1.7
{ | ||
"name": "mobius1-selectr", | ||
"version": "2.1.6", | ||
"version": "2.1.7", | ||
"ignore": [ | ||
@@ -5,0 +5,0 @@ ".gitattributes", |
/*! | ||
* Selectr 2.1.6 | ||
* Selectr 2.1.7 | ||
* http://mobius.ovh/docs/selectr | ||
@@ -7,2 +7,2 @@ * | ||
*/ | ||
!function(a,b){var c="Selectr";"function"==typeof define&&define.amd?define([],b(c)):"object"==typeof exports?module.exports=b(c):a[c]=b(c)}(this,function(a){"use strict";function v(a,b){if(null===a)throw new Error("Selectr requires an element to work.");if(this.el=a,"string"==typeof this.el&&(this.el=document.querySelector(a)),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.");if(!this.el.options.length&&!b.data&&!b.taggable)throw new Error("You don't have any options in your select!");this.originalType=this.el.type,this.render(b)}var b={extend:function(a,b){b=b||{};var c;for(c in a)b.hasOwnProperty(c)||(b[c]=a[c]);return b},each:function(a,b,c){if("[object Object]"===Object.prototype.toString.call(a))for(var d in a)Object.prototype.hasOwnProperty.call(a,d)&&b.call(c,d,a[d],a);else for(var e=0,f=a.length;e<f;e++)b.call(c,e,a[e],a)},createElement:function(a,c){var d=document,e=d.createElement(a);if(c&&b.isObject(c)){var f;for(f in c)if(f in e)e[f]=c[f];else if("html"===f)e.innerHTML=c[f];else if("text"===f){var g=d.createTextNode(c[f]);e.appendChild(g)}else e.setAttribute(f,c[f])}return e},hasClass:function(a,b){return a.classList?a.classList.contains(b):!!a.className&&!!a.className.match(new RegExp("(\\s|^)"+b+"(\\s|$)"))},addClass:function(a,c){b.hasClass(a,c)||(a.classList?a.classList.add(c):a.className=a.className.trim()+" "+c)},removeClass:function(a,c){b.hasClass(a,c)&&(a.classList?a.classList.remove(c):a.className=a.className.replace(new RegExp("(^|\\s)"+c.split(" ").join("|")+"(\\s|$)","gi")," "))},closest:function(a,c){return a&&a!==document.body&&(c(a)?a:b.closest(a.parentNode,c))},on:function(a,b,c,d){a.addEventListener(b,c,d||!1)},off:function(a,b,c){a.removeEventListener(b,c)},attr:function(a,c,d){b.isObject(c)?b.each(c,function(b,c){a.setAttribute(b,c)}):a.setAttribute(c,d)},isObject:function(a){return"[object Object]"===Object.prototype.toString.call(a)},isArray:function(a){return"[object Array]"===Object.prototype.toString.call(a)},isInt:function(a){return!isNaN(a)&&function(a){return(a||0)===a}(parseFloat(a))},debounce:function(a,b,c){var d;return function(){var e=this,f=arguments,g=function(){d=null,c||a.apply(e,f)},h=c&&!d;clearTimeout(d),d=setTimeout(g,b),h&&a.apply(e,f)}},getRect:function(a,b){var c=window,d=a.getBoundingClientRect(),e=b?c.pageXOffset:0,f=b?c.pageYOffset:0;return{bottom:d.bottom+f,height:d.height,left:d.left+e,right:d.right+e,top:d.top+f,width:d.width}},preventDefault:function(a){if(a=a||window.event,a.preventDefault)return a.preventDefault()},includes:function(a,b){return a.indexOf(b)>-1}},c=function(a){var c=this.settings,d=this.el.options;if(c.selectedValue){var e=c.selectedValue.toString();b.each(d,function(a,c){b.includes(e,c.value)&&(c.selected=!0,c.defaultSelected=!0)})}!d[0].defaultSelected&&d[0].selected&&(this.el.selectedIndex=-1),a&&c.data&&b.each(c.data,function(a,c){var e=c.hasOwnProperty("selected")&&c.selected===!0;b.each(d,function(a,b){b.value===c.value&&e&&(b.selected=!0,b.defaultSelected=!0)})})},d=function(){var a=this.settings,c=a.width;c&&(b.isInt(c)?c+="px":"auto"===c?c="100%":b.includes(a.width,"%")&&(c=a.width),this.width=c)},e=function(){var a=this,e=a.settings;if(b.addClass(a.el,"hidden-input"),a.el.tabIndex=-1,b.attr(a.el,{"aria-hidden":!0}),a.disabled&&(a.el.disabled=!0),e.data){a.pageIndex=1;var h=e.pagination?e.data.slice(0,e.pagination):e.data,i=!1;b.each(h,function(b,c){var d=c.hasOwnProperty("selected")&&c.selected===!0,e=new Option(c.text,c.value,d,d);c.disabled&&(e.disabled=!0),0===b&&d&&(i=!0),a.el.add(e)}),i||(this.el.selectedIndex=-1),e.pagination&&(this.pages=e.data.map(function(a,b){return b%e.pagination===0?e.data.slice(b,b+e.pagination):null}).filter(function(a){return a}))}c.call(a),a.customOption=e.hasOwnProperty("renderOption")&&"function"==typeof e.renderOption,a.customSelected=e.hasOwnProperty("renderSelection")&&"function"==typeof e.renderSelection,a.container=b.createElement("div",{class:"selectr-container"}),e.customClass&&b.addClass(a.container,e.customClass),d.call(a),this.container.style.width=this.width,a.selected=b.createElement("div",{class:"selectr-selected",disabled:a.disabled,tabIndex:a.el.tabIndex,"aria-expanded":!1}),a.label=b.createElement(a.el.multiple?"ul":"span",{class:"selectr-label"});var j=b.createElement("div",{class:"selectr-options-container"});if(a.tree=b.createElement("ul",{class:"selectr-options",role:"tree","aria-hidden":!0,"aria-expanded":!1}),a.notice=b.createElement("div",{class:"selectr-notice"}),a.el.multiple&&(b.addClass(a.label,"selectr-tags"),b.addClass(a.container,"multiple"),this.tags=[],this.selectedValues=[],this.selectedIndexes=[]),a.selected.appendChild(a.label),(a.el.multiple&&e.clearable||!a.el.multiple&&e.allowDeselect)&&(a.selectClear=b.createElement("button",{class:"selectr-clear",type:"button"}),a.selected.appendChild(a.selectClear)),e.taggable){var k=b.createElement("li",{class:"input-tag"});a.input=b.createElement("input",{class:"selectr-tag-input",placeholder:"Enter a tag...",tagIndex:0,autocomplete:"off",autocorrect:"off",autocapitalize:"off",spellcheck:"false",role:"textbox",type:"search"}),k.appendChild(a.input),a.label.appendChild(k),b.addClass(a.container,"taggable"),this.tagSeperators=[","],a.settings.tagSeperators&&(this.tagSeperators=this.tagSeperators.concat(a.settings.tagSeperators))}if(e.searchable&&(a.input=b.createElement("input",{class:"selectr-input",tagIndex:-1,autocomplete:"off",autocorrect:"off",autocapitalize:"off",spellcheck:"false",role:"textbox",type:"search"}),a.inputClear=b.createElement("button",{class:"selectr-clear",type:"button"}),a.inputContainer=b.createElement("div",{class:"selectr-input-container"}),a.inputContainer.appendChild(a.input),a.inputContainer.appendChild(a.inputClear),j.appendChild(a.inputContainer)),a.hasOptGroups){var l=0;b.addClass(a.tree,"optgroups")}var m=0;b.each(a.el.children,function(c,d){if("OPTGROUP"===d.nodeName){var g=b.createElement("ul",{class:"selectr-optgroup",role:"group"});g.appendChild(b.createElement("li",{class:"selectr-optgroup--label",text:d.label})),a.tree.appendChild(g),d.children&&b.each(d.children,function(b,c){e.pagination&&e.data&&l>=e.pagination||(f.call(a,l,c,g),l++)})}else{if(e.pagination&&e.data&&m>=e.pagination)return;f.call(a,m,d),m++}}),b.addClass(a.list[a.activeIdx],"active"),j.appendChild(a.notice),j.appendChild(a.tree),a.container.appendChild(a.selected),a.container.appendChild(j);var n=e.placeholder||a.el.getAttribute("placeholder")||"Choose...";a.placeEl=b.createElement("div",{class:"selectr-placeholder",html:n}),a.selected.appendChild(a.placeEl),a.el.parentNode.insertBefore(a.container,a.el),a.container.appendChild(a.el),g.call(this)},f=function(a,c,d){if("OPTION"===c.nodeName&&c.value){var e=this.customOption?this.settings.renderOption(c):c.textContent.trim(),f=b.createElement("li",{class:"selectr-option",html:e,role:"treeitem","aria-selected":!1});f.idx=a,this.items.push(f),c.defaultSelected&&h.call(this,a,!0),c.disabled&&(f.disabled=!0,b.addClass(f,"disabled")),d?d.appendChild(f):this.tree.appendChild(f),c.disabled||this.list.push(f)}},g=function(){var a=this;this.events={},this.events.keyup=n.bind(a),this.events.navigate=o.bind(a),this.events.dismiss=r.bind(a),this.events.reset=this.reset.bind(a),a.requiresPagination=a.settings.data&&a.settings.data.length>a.settings.pagination,b.on(a.container,"click",function(c){if(a.disabled)return!1;c=c||window.event;var d=c.target,e=b.closest(d,function(b){return b&&b==a.selected});if(d!==a.input){if(a.settings.clearable&&d===a.selectClear)return void a.clear();if(b.hasClass(d,"selectr-tag-remove")&&j.call(a,d.parentNode.idx),d!==a.label&&d!==a.placeEl||(d=a.placeEl.parentNode),(e||d===a.selected)&&a.toggle(),b.hasClass(d,"selectr-option")){var f=a.items.indexOf(d);h.call(a,f)}b.preventDefault(c)}}),b.on(a.tree,"mousedown",function(a){b.preventDefault(a)}),b.on(a.tree,"mouseover",function(c){var d=c.target;b.hasClass(d,"selectr-option")&&(b.removeClass(a.items[a.activeIdx],"active"),b.addClass(d,"active"),a.activeIdx=[].slice.call(a.tree.children).indexOf(d))}),a.settings.searchable&&b.on(a.inputClear,"click",function(b){m.call(a)}),b.on(document,"click",this.events.dismiss),b.on(document,"keydown",this.events.navigate),b.on(document,"keyup",this.events.keyup),a.update=b.debounce(function(){a.opened&&a.settings.closeOnScroll&&a.close(),this.width&&(this.container.style.width=this.width),q.call(a)},50),b.on(window,"resize",a.update),b.on(window,"scroll",a.update),a.requiresPagination&&(a.paginateItems=b.debounce(function(){p.call(a)},50),b.on(a.tree,"scroll",a.paginateItems));var c=a.el.form;c&&b.on(c,"reset",this.events.reset)},h=function(a,c){var d=this,e=d.items[a],f=d.el.options[a];if(!f.disabled){if(c)return void i.call(d,a);b.hasClass(e,"selected")?j.call(d,a):i.call(d,a),d.opened&&!d.el.multiple&&d.close()}},i=function(a){var c=this.el.options[a];if(this.el.multiple){if(b.includes(this.selectedIndexes,a))return!1;var d=this.settings.maxSelections;if(d&&this.tags.length==d)return this.setMessage("A maximum of "+d+" items can be selected.",!0),!1;this.selectedValues.push(c.value),this.selectedIndexes.push(a),k.call(this,a)}else this.label.innerHTML=this.customSelected?this.settings.renderSelection(c):c.textContent,this.selectedValue=c.value,this.selectedIndex=a,b.each(this.el.options,function(c,d){var e=this.items[c];c!==a&&(e&&b.removeClass(e,"selected"),d.selected=!1,d.defaultSelected=!1)},this);b.attr(this.items[a],{"aria-selected":!0}),b.addClass(this.items[a],"selected"),b.addClass(this.container,"has-selected"),c.selected=!0,c.defaultSelected=!0,this.emit("selectr.select",c),this.emit("selectr.change",c)},j=function(a){var c=this.el.options[a];if(this.el.multiple){var d=this.selectedIndexes.indexOf(a);this.selectedIndexes.splice(d,1);var e=this.selectedValues.indexOf(c.value);this.selectedValues.splice(e,1),l.call(this,a),this.tags.length||b.removeClass(this.container,"has-selected")}else{if(!this.settings.allowDeselect)return!1;this.label.innerHTML="",this.selectedValue=null,this.el.selectedIndex=-1,b.removeClass(this.container,"has-selected")}b.attr(this.items[a],{"aria-selected":!1}),b.removeClass(this.items[a],"selected"),c.selected=!1,c.defaultSelected=!1,this.emit("selectr.deselect",c),this.emit("selectr.change",c)},k=function(a){var c=this,d=document.createDocumentFragment(),e=c.el.options[a],f=c.customSelected?c.settings.renderSelection(e):e.textContent,g=b.createElement("li",{class:"selectr-tag",html:f}),h=b.createElement("button",{class:"selectr-tag-remove",type:"button"});if(g.appendChild(h),g.idx=a,g.tag=e.value,c.tags.push(g),c.settings.sortSelected){var i=c.tags.slice();i.sort(function(a,b){var f,g,d=[],e=[];for(c.settings.sortSelected===!0?(f=a.tag,g=b.tag):"text"===c.settings.sortSelected&&(f=a.textContent,g=b.textContent),f.replace(/(\d+)|(\D+)/g,function(a,b,c){d.push([b||1/0,c||""])}),g.replace(/(\d+)|(\D+)/g,function(a,b,c){e.push([b||1/0,c||""])});d.length&&e.length;){var h=d.shift(),i=e.shift(),j=h[0]-i[0]||h[1].localeCompare(i[1]);if(j)return j}return d.length-e.length}),b.each(i,function(a,b){d.appendChild(b)}),c.label.innerHTML=""}else d.appendChild(g);c.settings.taggable?c.label.insertBefore(d,c.input.parentNode):c.label.appendChild(d)},l=function(a){var c=!1;if(b.each(this.tags,function(b,d){d.idx===a&&(c=d)}),c){this.label.removeChild(c);var d=this.tags.indexOf(c);this.tags.splice(d,1)}},m=function(){var a=this;(a.settings.searchable||a.settings.taggable)&&(a.input.value=null,a.searching=!1,a.settings.searchable&&b.removeClass(a.inputContainer,"active"),b.hasClass(a.container,"notice")&&(b.removeClass(a.container,"notice"),b.addClass(a.container,"open"),a.input.focus()),b.each(a.list,function(c,d){b.removeClass(d,"match"),b.removeClass(d,"excluded"),a.customOption||(d.innerHTML=d.textContent)}))},n=function(a){var c=this;if(a.target===c.input&&c.search(c.input.value,!0),c.navigating&&13!==a.keyCode&&(c.navigating=!1),c.settings.taggable&&c.input.value.length){var d=c.input.value.trim();if(c.searchItems.length)return!1;if(13===a.which||b.includes(c.tagSeperators,a.key)){b.each(c.tagSeperators,function(a,b){d=d.replace(b,"")});var e=c.addOption({value:d,text:d,selected:!0});e?(c.close(),m.call(c)):(c.input.value="",c.setMessage("That tag is already in use."))}}},o=function(a){a=a||window.event;var c=this,d=a.which,e=[13,38,40];if(c.opened&&b.includes(e,d)){b.preventDefault(a);var g,f=this.searching?this.searchItems:this.list;if(f.length){switch(d){case 13:c.settings.taggable&&c.input===document.activeElement&&(c.input.value="");var i=c.tree.querySelector(".active"),j=c.items.indexOf(i);return void h.call(c,j);case 38:g=1,c.activeIdx>0&&c.activeIdx--;break;case 40:g=-1,c.activeIdx<f.length-1&&c.activeIdx++}this.navigating=!0;var o,q,k=f[c.activeIdx],l=b.getRect(k),m=c.tree.scrollTop,n=c.optsRect.top;if(g>0){var r=l.top;o=n,q=m+(r-o),0===c.activeIdx?c.tree.scrollTop=0:r-o<0&&(c.tree.scrollTop=q)}else{var s=l.top+l.height;o=n+c.optsRect.height,q=m+(s-o),0===c.activeIdx?c.tree.scrollTop=0:s>o&&(c.tree.scrollTop=q),c.requiresPagination&&p.call(c)}b.removeClass(c.tree.querySelector(".active"),"active"),b.addClass(f[c.activeIdx],"active")}}},p=function(){var a=this,c=a.tree,d=c.scrollTop,e=c.scrollHeight,g=c.offsetHeight,h=d>=e-g;if(h&&a.pageIndex<a.pages.length||a.activeIdx===a.list.length-1){var i=document.createDocumentFragment(),j=document.createDocumentFragment();b.each(a.pages[a.pageIndex],function(c,d){var e=b.createElement("option",{value:d.value,text:d.text});i.appendChild(e),f.call(a,c,e,j)}),a.el.appendChild(i),c.appendChild(j),a.pageIndex++,a.emit("selectr.paginate",{items:a.items.length,total:a.settings.data.length,page:a.pageIndex,pages:a.pages.length})}},q=function(){var a=b.getRect(this.selected),c=this.tree.parentNode.offsetHeight,d=window.innerHeight,e=a.top+a.height+c>d;e?(b.addClass(this.container,"inverted"),this.isInverted=!0):(b.removeClass(this.container,"inverted"),this.isInverted=!1),this.optsRect=b.getRect(this.tree)},r=function(a){var b=a.target;this.container.contains(b)||!this.opened&&!this.container.classList.contains("notice")||this.close()},s=function(a,b,c){var d=new RegExp(a,"i").exec(c);return!!d&&b.textContent.replace(d[0],"<span>"+d[0]+"</span>")},t=function(){};t.prototype={on:function(a,b){this._events=this._events||{},this._events[a]=this._events[a]||[],this._events[a].push(b)},off:function(a,b){this._events=this._events||{},a in this._events!=!1&&this._events[a].splice(this._events[a].indexOf(b),1)},emit:function(a){if(this._events=this._events||{},a in this._events!=!1)for(var b=0;b<this._events[a].length;b++)this._events[a][b].apply(this,Array.prototype.slice.call(arguments,1))}},t.mixin=function(a){for(var b=["on","off","emit"],c=0;c<b.length;c++)"function"==typeof a?a.prototype[b[c]]=t.prototype[b[c]]:a[b[c]]=t.prototype[b[c]];return a};var u={width:"auto",disabled:!1,searchable:!0,clearable:!1,sortSelected:!1,allowDeselect:!1,closeOnScroll:!1};return v.prototype.render=function(a){if(!this.rendered){this.disabled=!1,this.settings=b.extend(u,a),(this.settings.multiple||this.settings.taggable)&&(this.el.multiple=!0),this.settings.taggable&&(this.settings.searchable=!1),this.settings.disabled&&(this.disabled=!0),this.hasOptGroups=!1,this.el.getElementsByTagName("optgroup").length&&(this.hasOptGroups=!0),this.items=[],this.list=[],this.activeIdx=0,this.navigating=!1,t.mixin(this),e.call(this),this.update(),this.optsRect=b.getRect(this.tree),this.rendered=!0;var c=this;setTimeout(function(){c.emit("selectr.init")},20)}},v.prototype.destroy=function(){if(this.rendered){if(this.emit("selectr.destroy"),this.settings.data)for(;this.el.hasChildNodes();)this.el.removeChild(this.el.lastChild);"select-one"===this.originalType&&(this.el.multiple=!1),b.removeClass(this.el,"hidden-input");var a=this.el.form;a&&b.off(a,"reset",this.events.reset),b.off(document,"click",this.events.dismiss),b.off(document,"keydown",this.events.navigate),b.off(document,"keyup",this.events.keyup),b.off(window,"resize",this.update),b.off(window,"scroll",this.update),this.container.parentNode.replaceChild(this.el,this.container),this.rendered=!1}},v.prototype.setValue=function(a){var c=this,d=b.isArray(a);return d||(a=a.toString().trim()),!(!c.el.multiple&&d)&&void b.each(this.el.options,function(e,f){(d&&b.includes(a.toString(),f.value)||f.value===a)&&h.call(c,e)})},v.prototype.getValue=function(a,c){var e,d=this;if(d.el.multiple)a?d.selectedIndexes.length&&(e={},e.values=[],b.each(d.selectedIndexes,function(a,b){var c=d.el.options[b];e.values[a]={value:c.value,text:c.textContent}})):e=d.selectedValues.slice();else if(a){var f=d.el.options[d.selectedIndex];e={value:f.value,text:f.textContent}}else e=d.selectedValue;return a&&c&&(e=JSON.stringify(e)),e},v.prototype.addOption=function(a){var c=this;if(a&&b.isObject(a)&&a.value){var d=c.settings.data?c.settings.data:c.el.options,e=!1;if(b.each(d,function(b,c){c.value.toLowerCase()===a.value.toLowerCase()&&(e=!0)}),e){if(!c.settings.taggable)throw new Error("That value is already in use.");return!1}var g=b.createElement("option",a);return c.settings.data&&c.settings.data.push({value:a.value,text:a.text}),a.selected&&(g.selected=!0,g.defaultSelected=!0),g.innerHTML=c.customOption?c.settings.renderOption(g):g.textContent,c.settings.pagination||(c.el.add(g),f.call(c,c.el.options.length-1,g)),g}return!1},v.prototype.search=function(a,c){a=a.trim();var d=this;if(!d.navigating){if(b.hasClass(d.container,"notice")||d.settings.searchable&&(a.length>0?b.addClass(d.inputContainer,"active"):b.removeClass(d.inputContainer,"active")),d.searching=!0,d.searchItems=[],d.searchQuery=a,b.each(d.items,function(e,f){var g=f.textContent.trim(),h=b.includes(g.toLowerCase(),a.toLowerCase());f.disabled||!h?(b.addClass(f,"excluded"),b.removeClass(f,"match")):(d.searchItems.push(f),c&&(d.customOption||(f.innerHTML=s(a,f,g)),b.addClass(f,"match"),b.removeClass(f,"excluded")))}),c)d.searchItems.length||d.settings.taggable?d.open():(d.setMessage("No results."),d.input.focus());else if(d.searchItems.length){var e={};return e.values=[],b.each(d.searchItems,function(a,b){var c=d.el.options[b.idx];e.values[a]={value:c.value,text:c.textContent}}),e}d.emit("selectr.search",a,d.searchItems)}},v.prototype.toggle=function(){if(!this.disabled){var a=b.hasClass(this.container,"open");a?this.close():this.open()}},v.prototype.open=function(){var a=this;b.addClass(a.container,"open"),q.call(a);var c=this.tree.scrollHeight;c<=a.optsRect.height&&a.requiresPagination&&p.call(a),b.removeClass(a.container,"notice"),b.attr(a.selected,"aria-expanded",!0),b.attr(a.tree,{"aria-hidden":!1,"aria-expanded":!0}),a.settings.searchable&&!a.settings.taggable&&setTimeout(function(){a.input.focus(),a.input.tabIndex=0},10),this.opened||a.emit("selectr.open"),a.opened=!0},v.prototype.close=function(){var a=b.hasClass(this.container,"notice");this.settings.searchable&&!a&&(this.input.blur(),this.input.tabIndex=-1,this.searching=!1),a&&(b.removeClass(this.container,"notice"),this.notice.textContent=""),b.removeClass(this.container,"open"),b.attr(this.selected,"aria-expanded",!1),b.attr(this.tree,{"aria-hidden":!0,"aria-expanded":!1}),m.call(this),this.opened&&this.emit("selectr.close"),this.opened=!1},v.prototype.reset=function(){this.clear(),c.call(this,!0),b.each(this.el.options,function(a,b){b.defaultSelected&&h.call(this,a)},this),this.emit("selectr.reset")},v.prototype.clear=function(){if(this.el.multiple){var a=this.selectedIndexes.slice();b.each(a,function(a,b){j.call(this,b)},this)}else j.call(this,this.selectedIndex);this.emit("selectr.clear")},v.prototype.serialise=function(a){var c=[];return b.each(this.el.options,function(a,b){var d={value:b.value,text:b.textContent};b.defaultSelected&&(d.selected=!0),b.disabled&&(d.disabled=!0),c[a]=d}),a?JSON.stringify(c):c},v.prototype.serialize=function(a){return this.serialise(a)},v.prototype.enable=function(){this.disabled&&(this.disabled=!1,this.el.disabled=!1,b.removeClass(this.container,"disabled"))},v.prototype.disable=function(a){this.disabled||(a||(this.el.disabled=!0),this.disabled=!0,b.addClass(this.container,"disabled"))},v.prototype.setMessage=function(a,c){c&&this.close(),b.addClass(this.container,"notice"),this.notice.textContent=a},v}); | ||
!function(a,b){var c="Selectr";"function"==typeof define&&define.amd?define([],b(c)):"object"==typeof exports?module.exports=b(c):a[c]=b(c)}(this,function(a){"use strict";function v(a,b){if(b=b||{},null===a)throw new Error("Selectr requires an element to work.");if(this.el=a,"string"==typeof this.el&&(this.el=document.querySelector(a)),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.");if(!this.el.options.length&&!b.data&&!b.taggable)throw new Error("You don't have any options in your select!");this.sortDirection="asc",this.originalType=this.el.type,this.originalIndex=this.el.tabIndex,this.render(b)}var b={extend:function(a,b){b=b||{};var c;for(c in a)b.hasOwnProperty(c)||(b[c]=a[c]);return b},each:function(a,b,c){if("[object Object]"===Object.prototype.toString.call(a))for(var d in a)Object.prototype.hasOwnProperty.call(a,d)&&b.call(c,d,a[d],a);else for(var e=0,f=a.length;e<f;e++)b.call(c,e,a[e],a)},createElement:function(a,c){var d=document,e=d.createElement(a);if(c&&b.isObject(c)){var f;for(f in c)if(f in e)e[f]=c[f];else if("html"===f)e.innerHTML=c[f];else if("text"===f){var g=d.createTextNode(c[f]);e.appendChild(g)}else e.setAttribute(f,c[f])}return e},hasClass:function(a,b){return a.classList?a.classList.contains(b):!!a.className&&!!a.className.match(new RegExp("(\\s|^)"+b+"(\\s|$)"))},addClass:function(a,c){b.hasClass(a,c)||(a.classList?a.classList.add(c):a.className=a.className.trim()+" "+c)},removeClass:function(a,c){b.hasClass(a,c)&&(a.classList?a.classList.remove(c):a.className=a.className.replace(new RegExp("(^|\\s)"+c.split(" ").join("|")+"(\\s|$)","gi")," "))},closest:function(a,c){return a&&a!==document.body&&(c(a)?a:b.closest(a.parentNode,c))},on:function(a,b,c,d){a.addEventListener(b,c,d||!1)},off:function(a,b,c){a.removeEventListener(b,c)},attr:function(a,c,d){b.isObject(c)?b.each(c,function(b,c){a.setAttribute(b,c)}):a.setAttribute(c,d)},isObject:function(a){return"[object Object]"===Object.prototype.toString.call(a)},isArray:function(a){return"[object Array]"===Object.prototype.toString.call(a)},isInt:function(a){return!isNaN(a)&&function(a){return(a||0)===a}(parseFloat(a))},debounce:function(a,b,c){var d;return function(){var e=this,f=arguments,g=function(){d=null,c||a.apply(e,f)},h=c&&!d;clearTimeout(d),d=setTimeout(g,b),h&&a.apply(e,f)}},getRect:function(a,b){var c=window,d=a.getBoundingClientRect(),e=b?c.pageXOffset:0,f=b?c.pageYOffset:0;return{bottom:d.bottom+f,height:d.height,left:d.left+e,right:d.right+e,top:d.top+f,width:d.width}},preventDefault:function(a){if(a=a||window.event,a.preventDefault)return a.preventDefault()},includes:function(a,b){return a.indexOf(b)>-1}},c=function(a){var c=this.settings,d=this.el.options;if(c.selectedValue){var e=c.selectedValue.toString();b.each(d,function(a,c){b.includes(e,c.value)&&(c.selected=!0,c.defaultSelected=!0)})}!d[0].defaultSelected&&d[0].selected&&(this.el.selectedIndex=-1),a&&c.data&&b.each(c.data,function(a,c){var e=c.hasOwnProperty("selected")&&!0===c.selected;b.each(d,function(a,b){b.value===c.value&&e&&(b.selected=!0,b.defaultSelected=!0)})})},d=function(){var a=this.settings,c=a.width;c&&(b.isInt(c)?c+="px":"auto"===c?c="100%":b.includes(a.width,"%")&&(c=a.width),this.width=c)},e=function(){var a=this,e=a.settings;if(b.addClass(a.el,"hidden-input"),a.data=[],e.data){a.pageIndex=1;var h=e.pagination?e.data.slice(0,e.pagination):e.data,i=!1,j=function(a,b){var c=b.hasOwnProperty("selected")&&!0===b.selected,d=new Option(b.text,b.value,c,c);return b.disabled&&(d.disabled=!0),0===a&&c&&(i=!0),d},k=0;b.each(h,function(c,d){if(d.hasOwnProperty("children")){var e=b.createElement("optgroup",{label:d.text});b.each(d.children,function(a,b){e.appendChild(j(k,b)),k++}),a.el.appendChild(e)}else a.el.appendChild(j(k,d)),k++}),i||(a.el.selectedIndex=-1),e.pagination&&(a.pages=e.data.map(function(a,b){return b%e.pagination==0?e.data.slice(b,b+e.pagination):null}).filter(function(a){return a})),a.data=e.data}c.call(a),b.each(a.data,function(a,b){b.idx=a}),a.customOption=e.hasOwnProperty("renderOption")&&"function"==typeof e.renderOption,a.customSelected=e.hasOwnProperty("renderSelection")&&"function"==typeof e.renderSelection,a.container=b.createElement("div",{class:"selectr-container"}),e.customClass&&b.addClass(a.container,e.customClass),d.call(a),this.container.style.width=this.width,a.selected=b.createElement("div",{class:"selectr-selected",disabled:a.disabled,tabIndex:a.el.tabIndex,"aria-expanded":!1}),a.label=b.createElement(a.el.multiple?"ul":"span",{class:"selectr-label"});var l=b.createElement("div",{class:"selectr-options-container"});if(a.tree=b.createElement("ul",{class:"selectr-options",role:"tree","aria-hidden":!0,"aria-expanded":!1}),a.notice=b.createElement("div",{class:"selectr-notice"}),a.el.tabIndex=-1,b.attr(a.el,{"aria-hidden":!0}),a.disabled&&(a.el.disabled=!0),a.el.multiple&&(b.addClass(a.label,"selectr-tags"),b.addClass(a.container,"multiple"),this.tags=[],this.selectedValues=[],this.selectedIndexes=[]),a.selected.appendChild(a.label),(a.el.multiple&&e.clearable||!a.el.multiple&&e.allowDeselect)&&(a.selectClear=b.createElement("button",{class:"selectr-clear",type:"button"}),a.selected.appendChild(a.selectClear)),e.taggable){var m=b.createElement("li",{class:"input-tag"});a.input=b.createElement("input",{class:"selectr-tag-input",placeholder:"Enter a tag...",tagIndex:0,autocomplete:"off",autocorrect:"off",autocapitalize:"off",spellcheck:"false",role:"textbox",type:"search"}),m.appendChild(a.input),a.label.appendChild(m),b.addClass(a.container,"taggable"),this.tagSeperators=[","],a.settings.tagSeperators&&(this.tagSeperators=this.tagSeperators.concat(a.settings.tagSeperators))}e.searchable&&(a.input=b.createElement("input",{class:"selectr-input",tagIndex:-1,autocomplete:"off",autocorrect:"off",autocapitalize:"off",spellcheck:"false",role:"textbox",type:"search"}),a.inputClear=b.createElement("button",{class:"selectr-clear",type:"button"}),a.inputContainer=b.createElement("div",{class:"selectr-input-container"}),a.inputContainer.appendChild(a.input),a.inputContainer.appendChild(a.inputClear),l.appendChild(a.inputContainer));var n=0;if(a.hasOptGroups){var o=0;b.addClass(a.tree,"optgroups")}b.each(a.el.children,function(c,d){if("OPTGROUP"===d.nodeName){var g=b.createElement("ul",{class:"selectr-optgroup",role:"group"});g.appendChild(b.createElement("li",{class:"selectr-optgroup--label",text:d.label})),a.tree.appendChild(g),d.children&&b.each(d.children,function(b,c){e.pagination&&e.data&&o>=e.pagination||(f.call(a,o,c,g),o++)})}else{if(e.pagination&&e.data&&n>=e.pagination)return;f.call(a,n,d),n++}}),b.addClass(a.list[a.activeIdx],"active"),l.appendChild(a.notice),l.appendChild(a.tree),a.container.appendChild(a.selected),a.container.appendChild(l);var p=e.placeholder||a.el.getAttribute("placeholder")||"Choose...";a.placeEl=b.createElement("div",{class:"selectr-placeholder",html:p}),a.selected.appendChild(a.placeEl),a.el.parentNode.insertBefore(a.container,a.el),a.container.appendChild(a.el),g.call(this)},f=function(a,c,d){if("OPTION"===c.nodeName&&c.value){var e=this.customOption?this.settings.renderOption(c):c.textContent.trim(),f=b.createElement("li",{class:"selectr-option",html:e,role:"treeitem","aria-selected":!1});f.idx=a,this.items.push(f),c.defaultSelected&&h.call(this,a,!0),c.disabled&&(f.disabled=!0,b.addClass(f,"disabled")),d?d.appendChild(f):this.tree.appendChild(f),c.disabled||this.list.push(f)}},g=function(){var a=this,c=a.settings;this.events={},this.events.keyup=n.bind(a),this.events.navigate=o.bind(a),this.events.dismiss=r.bind(a),this.events.reset=this.reset.bind(a),a.requiresPagination=c.data&&c.data.length>c.pagination,b.on(a.container,"click",function(d){if(a.disabled)return!1;d=d||window.event;var e=d.target,f=b.closest(e,function(b){return b&&b==a.selected});if(e!==a.input){if(c.clearable&&e===a.selectClear)return void a.clear();if(b.hasClass(e,"selectr-tag-remove")&&j.call(a,e.parentNode.idx),e!==a.label&&e!==a.placeEl||(e=a.placeEl.parentNode),(f||e===a.selected)&&a.toggle(),b.hasClass(e,"selectr-option")){var g=a.items.indexOf(e);h.call(a,g)}b.preventDefault(d)}}),b.on(a.tree,"mousedown",function(a){b.preventDefault(a)}),b.on(a.tree,"mouseover",function(c){var d=c.target;b.closest(d,function(a){return a&&b.hasClass(a,"selectr-option")})&&(b.removeClass(a.items[a.activeIdx],"active"),b.addClass(d,"active"),a.activeIdx=[].slice.call(a.items).indexOf(d))}),c.searchable&&b.on(a.inputClear,"click",function(b){m.call(a)}),b.on(document,"click",this.events.dismiss),b.on(document,"keydown",this.events.navigate),b.on(document,"keyup",this.events.keyup),a.update=b.debounce(function(){a.opened&&c.closeOnScroll&&a.close(),this.width&&(this.container.style.width=this.width),q.call(a)},50),b.on(window,"resize",a.update),b.on(window,"scroll",a.update),a.requiresPagination&&(a.paginateItems=b.debounce(function(){p.call(a)},50),b.on(a.tree,"scroll",a.paginateItems));var d=a.el.form;d&&b.on(d,"reset",this.events.reset)},h=function(a,c){var d=this,e=d.items[a];if(!d.el.options[a].disabled){if(c)return void i.call(d,a);b.hasClass(e,"selected")?j.call(d,a):i.call(d,a),d.opened&&!d.el.multiple&&d.close()}},i=function(a){var c=this.el.options[a];if(this.el.multiple){if(b.includes(this.selectedIndexes,a))return!1;var d=this.settings.maxSelections;if(d&&this.tags.length==d)return this.setMessage("A maximum of "+d+" items can be selected.",!0),!1;this.selectedValues.push(c.value),this.selectedIndexes.push(a),k.call(this,a)}else this.label.innerHTML=this.customSelected?this.settings.renderSelection(c):c.textContent,this.selectedValue=c.value,this.selectedIndex=a,b.each(this.el.options,function(c,d){var e=this.items[c];c!==a&&(e&&b.removeClass(e,"selected"),d.selected=!1,d.defaultSelected=!1)},this);b.attr(this.items[a],{"aria-selected":!0}),b.addClass(this.items[a],"selected"),b.addClass(this.container,"has-selected"),c.selected=!0,c.defaultSelected=!0,this.emit("selectr.select",c),this.emit("selectr.change",c)},j=function(a){var c=this.el.options[a];if(this.el.multiple){var d=this.selectedIndexes.indexOf(a);this.selectedIndexes.splice(d,1);var e=this.selectedValues.indexOf(c.value);this.selectedValues.splice(e,1),l.call(this,a),this.tags.length||b.removeClass(this.container,"has-selected")}else{if(!this.settings.allowDeselect)return!1;this.label.innerHTML="",this.selectedValue=null,this.el.selectedIndex=-1,b.removeClass(this.container,"has-selected")}b.attr(this.items[a],{"aria-selected":!1}),b.removeClass(this.items[a],"selected"),c.selected=!1,c.defaultSelected=!1,this.emit("selectr.deselect",c),this.emit("selectr.change",c)},k=function(a){var c=this,d=document.createDocumentFragment(),e=c.el.options[a],f=c.customSelected?c.settings.renderSelection(e):e.textContent,g=b.createElement("li",{class:"selectr-tag",html:f}),h=b.createElement("button",{class:"selectr-tag-remove",type:"button"});if(g.appendChild(h),g.idx=a,g.tag=e.value,c.tags.push(g),c.settings.sortSelected){var i=c.tags.slice();i.sort(function(a,b){var f,g,d=[],e=[];for(!0===c.settings.sortSelected?(f=a.tag,g=b.tag):"text"===c.settings.sortSelected&&(f=a.textContent,g=b.textContent),f.replace(/(\d+)|(\D+)/g,function(a,b,c){d.push([b||1/0,c||""])}),g.replace(/(\d+)|(\D+)/g,function(a,b,c){e.push([b||1/0,c||""])});d.length&&e.length;){var h=d.shift(),i=e.shift(),j=h[0]-i[0]||h[1].localeCompare(i[1]);if(j)return j}return d.length-e.length}),b.each(i,function(a,b){d.appendChild(b)}),c.label.innerHTML=""}else d.appendChild(g);c.settings.taggable?c.label.insertBefore(d,c.input.parentNode):c.label.appendChild(d)},l=function(a){var c=!1;if(b.each(this.tags,function(b,d){d.idx===a&&(c=d)}),c){this.label.removeChild(c);var d=this.tags.indexOf(c);this.tags.splice(d,1)}},m=function(){var a=this;(a.settings.searchable||a.settings.taggable)&&(a.input.value=null,a.searching=!1,a.settings.searchable&&b.removeClass(a.inputContainer,"active"),b.hasClass(a.container,"notice")&&(b.removeClass(a.container,"notice"),b.addClass(a.container,"open"),a.input.focus()),b.each(a.list,function(c,d){b.removeClass(d,"match"),b.removeClass(d,"excluded"),a.customOption||(d.innerHTML=d.textContent)}))},n=function(a){var c=this;if(a.target===c.input&&c.search(c.input.value,!0),c.navigating&&13!==a.keyCode&&(c.navigating=!1),c.settings.taggable&&c.input.value.length){var d=c.input.value.trim();if(c.searchItems.length)return!1;if(13===a.which||b.includes(c.tagSeperators,a.key)){b.each(c.tagSeperators,function(a,b){d=d.replace(b,"")});c.addOption({value:d,text:d,selected:!0})?(c.close(),m.call(c)):(c.input.value="",c.setMessage("That tag is already in use."))}}},o=function(a){a=a||window.event;var c=this,d=a.which,e=[13,38,40];if(c.opened&&b.includes(e,d)){b.preventDefault(a);var g,f=this.searching?this.searchItems:this.list;if(f.length){switch(d){case 13:c.settings.taggable&&c.input===document.activeElement&&(c.input.value="");var i=c.tree.querySelector(".active"),j=c.items.indexOf(i);return void h.call(c,j);case 38:g=1,c.activeIdx>0&&c.activeIdx--;break;case 40:g=-1,c.activeIdx<f.length-1&&c.activeIdx++}this.navigating=!0;var o,q,k=f[c.activeIdx],l=b.getRect(k),m=c.tree.scrollTop,n=c.optsRect.top;if(g>0){var r=l.top;o=n,q=m+(r-o),0===c.activeIdx?c.tree.scrollTop=0:r-o<0&&(c.tree.scrollTop=q)}else{var s=l.top+l.height;o=n+c.optsRect.height,q=m+(s-o),0===c.activeIdx?c.tree.scrollTop=0:s>o&&(c.tree.scrollTop=q),c.requiresPagination&&p.call(c)}b.removeClass(c.tree.querySelector(".active"),"active"),b.addClass(f[c.activeIdx],"active")}}},p=function(){var a=this,c=a.tree;if(c.scrollTop>=c.scrollHeight-c.offsetHeight&&a.pageIndex<a.pages.length||a.activeIdx===a.list.length-1){var i=document.createDocumentFragment(),j=document.createDocumentFragment();b.each(a.pages[a.pageIndex],function(c,d){var e=b.createElement("option",{value:d.value,text:d.text});i.appendChild(e),f.call(a,c,e,j)}),a.el.appendChild(i),c.appendChild(j),a.pageIndex++,a.emit("selectr.paginate",{items:a.items.length,total:a.settings.data.length,page:a.pageIndex,pages:a.pages.length})}},q=function(){var a=b.getRect(this.selected),c=this.tree.parentNode.offsetHeight,d=window.innerHeight;a.top+a.height+c>d?(b.addClass(this.container,"inverted"),this.isInverted=!0):(b.removeClass(this.container,"inverted"),this.isInverted=!1),this.optsRect=b.getRect(this.tree)},r=function(a){var b=a.target;this.container.contains(b)||!this.opened&&!this.container.classList.contains("notice")||this.close()},s=function(a,b,c){var d=new RegExp(a,"i").exec(c);return!!d&&b.textContent.replace(d[0],"<span>"+d[0]+"</span>")},t=function(){};t.prototype={on:function(a,b){this._events=this._events||{},this._events[a]=this._events[a]||[],this._events[a].push(b)},off:function(a,b){this._events=this._events||{},a in this._events!=!1&&this._events[a].splice(this._events[a].indexOf(b),1)},emit:function(a){if(this._events=this._events||{},a in this._events!=!1)for(var b=0;b<this._events[a].length;b++)this._events[a][b].apply(this,Array.prototype.slice.call(arguments,1))}},t.mixin=function(a){for(var b=["on","off","emit"],c=0;c<b.length;c++)"function"==typeof a?a.prototype[b[c]]=t.prototype[b[c]]:a[b[c]]=t.prototype[b[c]];return a};var u={width:"auto",disabled:!1,searchable:!0,clearable:!1,sortSelected:!1,allowDeselect:!1,closeOnScroll:!1};return v.prototype.render=function(a){if(!this.rendered){this.disabled=!1,this.settings=b.extend(u,a),(this.settings.multiple||this.settings.taggable)&&(this.el.multiple=!0),this.settings.taggable&&(this.settings.searchable=!1),this.settings.disabled&&(this.disabled=!0),this.hasOptGroups=!1,this.el.getElementsByTagName("optgroup").length&&(this.hasOptGroups=!0),this.items=[],this.list=[],this.activeIdx=0,this.navigating=!1,t.mixin(this),e.call(this),this.update(),this.optsRect=b.getRect(this.tree),this.rendered=!0;var c=this;setTimeout(function(){c.emit("selectr.init")},20)}},v.prototype.destroy=function(){if(this.rendered){if(this.emit("selectr.destroy"),this.settings.data)for(;this.el.hasChildNodes();)this.el.removeChild(this.el.lastChild);"select-one"===this.originalType&&(this.el.multiple=!1),b.removeClass(this.el,"hidden-input");var a=this.el.form;a&&b.off(a,"reset",this.events.reset),b.off(document,"click",this.events.dismiss),b.off(document,"keydown",this.events.navigate),b.off(document,"keyup",this.events.keyup),b.off(window,"resize",this.update),b.off(window,"scroll",this.update),this.container.parentNode.replaceChild(this.el,this.container),this.rendered=!1}},v.prototype.setValue=function(a){var c=this,d=b.isArray(a);if(d||(a=a.toString().trim()),!c.el.multiple&&d)return!1;b.each(this.el.options,function(e,f){(d&&b.includes(a.toString(),f.value)||f.value===a)&&h.call(c,e)})},v.prototype.getValue=function(a,c){var e,d=this;if(d.el.multiple)a?d.selectedIndexes.length&&(e={},e.values=[],b.each(d.selectedIndexes,function(a,b){var c=d.el.options[b];e.values[a]={value:c.value,text:c.textContent}})):e=d.selectedValues.slice();else if(a){var f=d.el.options[d.selectedIndex];e={value:f.value,text:f.textContent}}else e=d.selectedValue;return a&&c&&(e=JSON.stringify(e)),e},v.prototype.addOption=function(a){var c=this;if(a&&b.isObject(a)&&a.value){var d=c.settings.data?c.settings.data:c.el.options,e=!1;if(b.each(d,function(b,c){c.value.toLowerCase()===a.value.toLowerCase()&&(e=!0)}),e){if(!c.settings.taggable)throw new Error("That value is already in use.");return!1}var g=b.createElement("option",a);return c.settings.data&&c.settings.data.push({value:a.value,text:a.text}),a.selected&&(g.selected=!0,g.defaultSelected=!0),g.innerHTML=c.customOption?c.settings.renderOption(g):g.textContent,c.settings.pagination||(c.el.add(g),f.call(c,c.el.options.length-1,g)),g}return!1},v.prototype.search=function(a,c){a=a.trim();var d=this;if(!d.navigating){if(b.hasClass(d.container,"notice")||d.settings.searchable&&(a.length>0?b.addClass(d.inputContainer,"active"):b.removeClass(d.inputContainer,"active")),d.searching=!0,d.searchItems=[],d.searchQuery=a,b.each(d.items,function(e,f){var g=f.textContent.trim(),h=b.includes(g.toLowerCase(),a.toLowerCase());f.disabled||!h?(b.addClass(f,"excluded"),b.removeClass(f,"match")):(d.searchItems.push(f),c&&(d.customOption||(f.innerHTML=s(a,f,g)),b.addClass(f,"match"),b.removeClass(f,"excluded")))}),c)d.searchItems.length||d.settings.taggable?d.open():(d.setMessage("No results."),d.input.focus());else if(d.searchItems.length){var e={};return e.values=[],b.each(d.searchItems,function(a,b){var c=d.el.options[b.idx];e.values[a]={value:c.value,text:c.textContent}}),e}d.emit("selectr.search",a,d.searchItems)}},v.prototype.toggle=function(){if(!this.disabled){b.hasClass(this.container,"open")?this.close():this.open()}},v.prototype.open=function(){var a=this;b.addClass(a.container,"open"),q.call(a),this.tree.scrollHeight<=a.optsRect.height&&a.requiresPagination&&p.call(a),b.removeClass(a.container,"notice"),b.attr(a.selected,"aria-expanded",!0),b.attr(a.tree,{"aria-hidden":!1,"aria-expanded":!0}),a.settings.searchable&&!a.settings.taggable&&setTimeout(function(){a.input.focus(),a.input.tabIndex=0},10),this.opened||a.emit("selectr.open"),a.opened=!0},v.prototype.close=function(){var a=b.hasClass(this.container,"notice");this.settings.searchable&&!a&&(this.input.blur(),this.input.tabIndex=-1,this.searching=!1),a&&(b.removeClass(this.container,"notice"),this.notice.textContent=""),b.removeClass(this.container,"open"),b.attr(this.selected,"aria-expanded",!1),b.attr(this.tree,{"aria-hidden":!0,"aria-expanded":!1}),m.call(this),this.opened&&this.emit("selectr.close"),this.opened=!1},v.prototype.reset=function(){this.disabled||(this.clear(),c.call(this,!0),b.each(this.el.options,function(a,b){b.defaultSelected&&h.call(this,a)},this),this.emit("selectr.reset"))},v.prototype.clear=function(){if(this.el.multiple){var a=this.selectedIndexes.slice();b.each(a,function(a,b){j.call(this,b)},this)}else j.call(this,this.selectedIndex);this.emit("selectr.clear")},v.prototype.serialise=function(a){var c=[];return b.each(this.el.options,function(a,b){var d={value:b.value,text:b.textContent};b.defaultSelected&&(d.selected=!0),b.disabled&&(d.disabled=!0),c[a]=d}),a?JSON.stringify(c):c},v.prototype.serialize=function(a){return this.serialise(a)},v.prototype.enable=function(){this.disabled&&(this.disabled=!1,this.el.disabled=!1,this.selected.tabIndex=this.originalIndex,b.each(this.tags,function(a,b){b.lastElementChild.tabIndex=0}),b.removeClass(this.container,"disabled"))},v.prototype.disable=function(a){this.disabled||(a||(this.el.disabled=!0),this.selected.tabIndex=-1,b.each(this.tags,function(a,b){b.lastElementChild.tabIndex=-1}),this.disabled=!0,b.addClass(this.container,"disabled"))},v.prototype.setMessage=function(a,c){c&&this.close(),b.addClass(this.container,"notice"),this.notice.textContent=a},v}); |
{ | ||
"name": "mobius1-selectr", | ||
"version": "2.1.6", | ||
"version": "2.1.7", | ||
"description": "A lightweight dependency-free javascript select box replacement.", | ||
@@ -5,0 +5,0 @@ "main": "dist/selectr.min.js", |
@@ -39,5 +39,5 @@ # 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) | ||
```html | ||
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/selectr/2.1.4/selectr.min.css"> | ||
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/selectr/2.1.6/selectr.min.css"> | ||
<script type="text/javascript" src="https://cdn.jsdelivr.net/selectr/2.1.4/selectr.min.js"></script> | ||
<script type="text/javascript" src="https://cdn.jsdelivr.net/selectr/2.1.6/selectr.min.js"></script> | ||
``` | ||
@@ -44,0 +44,0 @@ |
/*! | ||
* Selectr 2.1.6 | ||
* Selectr 2.1.7 | ||
* http://mobius.ovh/docs/selectr | ||
@@ -54,4 +54,4 @@ * | ||
var t = d.createTextNode(a[i]); | ||
el.appendChild(t) | ||
} else el.setAttribute(i, a[i]) | ||
el.appendChild(t); | ||
} else el.setAttribute(i, a[i]); | ||
} | ||
@@ -139,2 +139,6 @@ return el; | ||
/** | ||
* Check for selected options | ||
* @param {bool} reset | ||
*/ | ||
var setSelected = function(reset) { | ||
@@ -174,2 +178,5 @@ var o = this.settings; | ||
/** | ||
* Set the custom width | ||
*/ | ||
var setWidth = function() { | ||
@@ -194,2 +201,6 @@ var o = this.settings; | ||
/** | ||
* Render the containers | ||
* @return {void} | ||
*/ | ||
var build = function() { | ||
@@ -199,18 +210,6 @@ var _ = this; | ||
util.addClass(_.el, "hidden-input"); | ||
// We don't want to focus on the native select box | ||
_.el.tabIndex = -1; | ||
util.attr(_.el, { | ||
"aria-hidden": true | ||
}); | ||
if ( _.disabled ) { | ||
_.el.disabled = true; | ||
} | ||
// Check for data | ||
_.data = []; | ||
if ( o.data ) { | ||
@@ -221,7 +220,7 @@ _.pageIndex = 1; | ||
util.each(data, function(idx, itm) { | ||
var selected = itm.hasOwnProperty('selected') && itm.selected === true; | ||
var option = new Option(itm.text, itm.value, selected, selected); | ||
var dataToOption = function (idx, obj) { | ||
var selected = obj.hasOwnProperty('selected') && obj.selected === true; | ||
var option = new Option(obj.text, obj.value, selected, selected); | ||
if ( itm.disabled ) { | ||
if ( obj.disabled ) { | ||
option.disabled = true; | ||
@@ -234,14 +233,36 @@ } | ||
_.el.add(option); | ||
return option; | ||
}; | ||
var i = 0; | ||
util.each(data, function(idx, itm) { | ||
if ( itm.hasOwnProperty('children') ) { | ||
var g = util.createElement("optgroup", { | ||
label: itm.text | ||
}); | ||
util.each(itm.children, function(k,c) { | ||
g.appendChild(dataToOption(i, c)); | ||
i++; | ||
}); | ||
_.el.appendChild(g); | ||
} else { | ||
_.el.appendChild(dataToOption(i, itm)); | ||
i++; | ||
} | ||
}); | ||
if ( !firstSelected ) { | ||
this.el.selectedIndex = -1; | ||
_.el.selectedIndex = -1; | ||
} | ||
if ( o.pagination ) { | ||
this.pages = o.data.map( function(v, i) { | ||
_.pages = o.data.map( function(v, i) { | ||
return i % o.pagination === 0 ? o.data.slice(i, i+o.pagination) : null; | ||
}).filter(function(pages){ return pages; }); | ||
} | ||
_.data = o.data; | ||
} | ||
@@ -251,2 +272,6 @@ | ||
util.each(_.data, function(i,o) { | ||
o.idx = i; | ||
}); | ||
_.customOption = o.hasOwnProperty("renderOption") && typeof o.renderOption === "function"; | ||
@@ -294,2 +319,13 @@ _.customSelected = o.hasOwnProperty("renderSelection") && typeof o.renderSelection === "function"; | ||
// We don't want to focus on the native select box | ||
_.el.tabIndex = -1; | ||
util.attr(_.el, { | ||
"aria-hidden": true | ||
}); | ||
if ( _.disabled ) { | ||
_.el.disabled = true; | ||
} | ||
if (_.el.multiple) { | ||
@@ -369,2 +405,4 @@ util.addClass(_.label, "selectr-tags"); | ||
var j = 0; | ||
// Check for optgroups | ||
@@ -376,3 +414,2 @@ if (_.hasOptGroups) { | ||
var i = 0; | ||
util.each(_.el.children, function(idx, opt) { | ||
@@ -398,5 +435,5 @@ if (opt.nodeName === "OPTGROUP") { | ||
} else { | ||
if ( o.pagination && o.data && i >= o.pagination ) return; | ||
buildOption.call(_, i, opt); | ||
i++; | ||
if ( o.pagination && o.data && j >= o.pagination ) return; | ||
buildOption.call(_, j, opt); | ||
j++; | ||
} | ||
@@ -427,2 +464,9 @@ }); | ||
/** | ||
* Build an list item from the HTMLOptionElement | ||
* @param {int} i HTMLOptionElement index | ||
* @param {HTMLOptionElement} option | ||
* @param {bool} group Has parent optgroup | ||
* @return {void} | ||
*/ | ||
var buildOption = function(i, option, group) { | ||
@@ -464,4 +508,7 @@ if (option.nodeName !== "OPTION" || !option.value) return; | ||
/** | ||
* Add the required event listeners | ||
*/ | ||
var addListeners = function() { | ||
var _ = this; | ||
var _ = this, o = _.settings; | ||
@@ -475,3 +522,3 @@ this.events = {}; | ||
_.requiresPagination = _.settings.data && _.settings.data.length > _.settings.pagination; | ||
_.requiresPagination = o.data && o.data.length > o.pagination; | ||
@@ -497,3 +544,3 @@ // Global listener | ||
// Clear | ||
if ( _.settings.clearable && t === _.selectClear ) { | ||
if ( o.clearable && t === _.selectClear ) { | ||
_.clear(); | ||
@@ -534,10 +581,15 @@ return; | ||
var t = e.target; | ||
if ( util.hasClass(t, "selectr-option") ) { | ||
var option = util.closest(t, function(el) { | ||
return el && util.hasClass(el, "selectr-option"); | ||
}); | ||
if ( option ) { | ||
util.removeClass(_.items[_.activeIdx], "active"); | ||
util.addClass(t, "active"); | ||
_.activeIdx = [].slice.call(_.tree.children).indexOf(t); | ||
_.activeIdx = [].slice.call(_.items).indexOf(t); | ||
} | ||
}); | ||
if (_.settings.searchable) { | ||
if (o.searchable) { | ||
util.on(_.inputClear, "click", function(e) { | ||
@@ -554,3 +606,3 @@ clearSearch.call(_); | ||
// Optionally close dropdown on scroll / resize (#11) | ||
if (_.opened && _.settings.closeOnScroll) { | ||
if (_.opened && o.closeOnScroll) { | ||
_.close(); | ||
@@ -582,2 +634,8 @@ } | ||
/** | ||
* Trigger a change | ||
* @param {int} index | ||
* @param {bool} init | ||
* @return {void} | ||
*/ | ||
var change = function(index, init) { | ||
@@ -608,2 +666,7 @@ var _ = this; | ||
/** | ||
* Select an option | ||
* @param {int} index | ||
* @return {void} | ||
*/ | ||
var select = function(index) { | ||
@@ -660,2 +723,7 @@ var option = this.el.options[index]; | ||
/** | ||
* Deselect an options | ||
* @param {int} index | ||
* @return {void} | ||
*/ | ||
var deselect = function(index) { | ||
@@ -702,2 +770,6 @@ var option = this.el.options[index]; | ||
/** | ||
* Add a tag | ||
* @param {int} index | ||
*/ | ||
var addTag = function(index) { | ||
@@ -772,2 +844,7 @@ var _ = this; | ||
/** | ||
* Remove a tag | ||
* @param {int} | ||
* @return {void} | ||
*/ | ||
var removeTag = function(index) { | ||
@@ -791,2 +868,6 @@ var tag = false; | ||
/** | ||
* Clear a search | ||
* @return {void} | ||
*/ | ||
var clearSearch = function() { | ||
@@ -819,2 +900,7 @@ var _ = this; | ||
/** | ||
* keyup listener | ||
* @param {obj} e | ||
* @return {void} | ||
*/ | ||
var keyup = function(e) { | ||
@@ -860,2 +946,7 @@ var _ = this; | ||
/** | ||
* Navigate through the dropdown | ||
* @param {obj} e | ||
* @return {void} | ||
*/ | ||
var navigate = function(e) { | ||
@@ -934,2 +1025,6 @@ e = e || window.event; | ||
/** | ||
* Paginate the dataset | ||
* @return {void} | ||
*/ | ||
var paginate = function() { | ||
@@ -967,3 +1062,6 @@ var _ = this; | ||
// Make sure dropdown stays on screen | ||
/** | ||
* Keep the dropdown within the window | ||
* @return {void} | ||
*/ | ||
var checkInvert = function() { | ||
@@ -986,2 +1084,7 @@ var s = util.getRect(this.selected); | ||
/** | ||
* Dismiss / close the dropdown | ||
* @param {obj} e | ||
* @return {void} | ||
*/ | ||
var dismiss = function(e) { | ||
@@ -994,2 +1097,9 @@ var target = e.target; | ||
/** | ||
* Query matching for searches | ||
* @param {string} query | ||
* @param {HTMLOptionElement} opt | ||
* @param {string} text | ||
* @return {bool} | ||
*/ | ||
var match = function(query, opt, text) { | ||
@@ -1037,2 +1147,6 @@ var result = new RegExp(query, "i").exec(text); | ||
/** | ||
* Plugin defaults | ||
* @type {Object} | ||
*/ | ||
var defaults = { | ||
@@ -1055,2 +1169,4 @@ width: "auto", | ||
options = options || {}; | ||
// Checks | ||
@@ -1079,3 +1195,5 @@ if (el === null) { | ||
this.sortDirection = "asc"; | ||
this.originalType = this.el.type; | ||
this.originalIndex = this.el.tabIndex; | ||
@@ -1476,13 +1594,15 @@ this.render(options); | ||
Selectr.prototype.reset = function() { | ||
this.clear(); | ||
if ( !this.disabled ) { | ||
this.clear(); | ||
setSelected.call(this, true); | ||
setSelected.call(this, true); | ||
util.each(this.el.options, function(i,opt) { | ||
if ( opt.defaultSelected ) { | ||
change.call(this, i); | ||
} | ||
}, this); | ||
util.each(this.el.options, function(i,opt) { | ||
if ( opt.defaultSelected ) { | ||
change.call(this, i); | ||
} | ||
}, this); | ||
this.emit("selectr.reset"); | ||
this.emit("selectr.reset"); | ||
} | ||
}; | ||
@@ -1548,2 +1668,9 @@ | ||
this.el.disabled = false; | ||
this.selected.tabIndex = this.originalIndex; | ||
util.each(this.tags, function(i,t) { | ||
t.lastElementChild.tabIndex = 0; | ||
}); | ||
util.removeClass(this.container, "disabled"); | ||
@@ -1555,2 +1682,3 @@ } | ||
* Disable the element | ||
* @param {boolean} container Disable the container only (allow value submit with form) | ||
* @return {void} | ||
@@ -1564,2 +1692,8 @@ */ | ||
this.selected.tabIndex = -1; | ||
util.each(this.tags, function(i,t) { | ||
t.lastElementChild.tabIndex = -1; | ||
}); | ||
this.disabled = true; | ||
@@ -1566,0 +1700,0 @@ util.addClass(this.container, "disabled"); |
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
80241
1862