mobius1-selectr
Advanced tools
Comparing version 2.1.5 to 2.1.6
{ | ||
"name": "mobius1-selectr", | ||
"version": "2.1.5", | ||
"version": "2.1.6", | ||
"ignore": [ | ||
@@ -5,0 +5,0 @@ ".gitattributes", |
/*! | ||
* Selectr 2.1.5 | ||
* Selectr 2.1.6 | ||
* 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,b){var c=document,d=c.createElement(a);if(b&&"object"==typeof b){var e;for(e in b)if("html"===e)d.innerHTML=b[e];else if("text"===e){var f=c.createTextNode(b[e]);d.appendChild(f)}else d.setAttribute(e,b[e])}return d},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.width;a&&(b.isInt(a)?a+="px":"auto"===a?a="100%":b.includes(this.settings.width,"%")&&(a=this.settings.width),this.width=a)},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.optsOptions=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.optsOptions,"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.optsOptions.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.optsOptions),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.optsOptions.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.optsOptions,"mousedown",function(a){b.preventDefault(a)}),b.on(a.optsOptions,"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.optsOptions.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.optsOptions,"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.optsOptions.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.optsOptions.scrollTop,n=c.optsRect.top;if(g>0){var r=l.top;o=n,q=m+(r-o),0===c.activeIdx?c.optsOptions.scrollTop=0:r-o<0&&(c.optsOptions.scrollTop=q)}else{var s=l.top+l.height;o=n+c.optsRect.height,q=m+(s-o),0===c.activeIdx?c.optsOptions.scrollTop=0:s>o&&(c.optsOptions.scrollTop=q),c.requiresPagination&&p.call(c)}b.removeClass(c.optsOptions.querySelector(".active"),"active"),b.addClass(f[c.activeIdx],"active")}}},p=function(){var a=this,c=a.optsOptions,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.optsOptions.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.optsOptions)},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.optsOptions),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.optsOptions.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.optsOptions,{"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.optsOptions,{"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(){this.disabled||(this.disabled=!0,this.el.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(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}); |
{ | ||
"name": "mobius1-selectr", | ||
"version": "2.1.5", | ||
"version": "2.1.6", | ||
"description": "A lightweight dependency-free javascript select box replacement.", | ||
@@ -5,0 +5,0 @@ "main": "dist/selectr.min.js", |
# 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) | ||
A lightweight dependency-free select box replacement written in vanilla javascript. Similar to Select2 and Chosen without the dependencies. | ||
A lightweight dependency-free select box replacement written in vanilla javascript. Just 6KB minified and gzipped. Similar to Select2 and Chosen without the dependencies. | ||
@@ -39,5 +39,5 @@ Supports most modern browsers including IE9. | ||
```html | ||
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/selectr/2.1.3/selectr.min.css"> | ||
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/selectr/2.1.4/selectr.min.css"> | ||
<script type="text/javascript" src="https://cdn.jsdelivr.net/selectr/2.1.3/selectr.min.js"></script> | ||
<script type="text/javascript" src="https://cdn.jsdelivr.net/selectr/2.1.4/selectr.min.js"></script> | ||
``` | ||
@@ -44,0 +44,0 @@ |
/*! | ||
* Selectr 2.1.5 | ||
* Selectr 2.1.6 | ||
* http://mobius.ovh/docs/selectr | ||
@@ -44,15 +44,16 @@ * | ||
}, | ||
createElement: function(a, b) { | ||
var c = document, | ||
d = c.createElement(a); | ||
if (b && "object" == typeof b) { | ||
var e; | ||
for (e in b) | ||
if ("html" === e) d.innerHTML = b[e]; | ||
else if ("text" === e) { | ||
var f = c.createTextNode(b[e]); | ||
d.appendChild(f); | ||
} else d.setAttribute(e, b[e]); | ||
createElement: function(e, a) { | ||
var d = document, | ||
el = d.createElement(e); | ||
if (a && util.isObject(a)) { | ||
var i; | ||
for (i in a) | ||
if (i in el) el[i] = a[i]; | ||
else if ("html" === i) el.innerHTML = a[i]; | ||
else if ("text" === i) { | ||
var t = d.createTextNode(a[i]); | ||
el.appendChild(t) | ||
} else el.setAttribute(i, a[i]) | ||
} | ||
return d; | ||
return el; | ||
}, | ||
@@ -275,3 +276,3 @@ hasClass: function(a, b) { | ||
_.optsOptions = util.createElement("ul", { | ||
_.tree = util.createElement("ul", { | ||
class: "selectr-options", | ||
@@ -364,3 +365,3 @@ role: "tree", | ||
var x = 0; | ||
util.addClass(_.optsOptions, "optgroups"); | ||
util.addClass(_.tree, "optgroups"); | ||
} | ||
@@ -379,3 +380,3 @@ | ||
})); | ||
_.optsOptions.appendChild(group); | ||
_.tree.appendChild(group); | ||
@@ -399,3 +400,3 @@ if (opt.children) { | ||
optsContainer.appendChild(_.notice); | ||
optsContainer.appendChild(_.optsOptions); | ||
optsContainer.appendChild(_.tree); | ||
@@ -447,3 +448,3 @@ _.container.appendChild(_.selected); | ||
} else { | ||
this.optsOptions.appendChild(opt); | ||
this.tree.appendChild(opt); | ||
} | ||
@@ -518,11 +519,11 @@ | ||
// Prevent text selection | ||
util.on(_.optsOptions, "mousedown", function(e) { util.preventDefault(e); }); | ||
util.on(_.tree, "mousedown", function(e) { util.preventDefault(e); }); | ||
// Mouseover list items | ||
util.on(_.optsOptions, "mouseover", function(e) { | ||
var t = e.t; | ||
util.on(_.tree, "mouseover", function(e) { | ||
var t = e.target; | ||
if ( util.hasClass(t, "selectr-option") ) { | ||
util.removeClass(_.items[_.activeIdx], "active"); | ||
util.addClass(t, "active"); | ||
_.activeIdx = [].slice.call(_.optsOptions.children).indexOf(t); | ||
_.activeIdx = [].slice.call(_.tree.children).indexOf(t); | ||
} | ||
@@ -560,3 +561,3 @@ }); | ||
util.on(_.optsOptions, "scroll", _.paginateItems); | ||
util.on(_.tree, "scroll", _.paginateItems); | ||
} | ||
@@ -864,3 +865,3 @@ | ||
} | ||
var opt = _.optsOptions.querySelector(".active"); | ||
var opt = _.tree.querySelector(".active"); | ||
var index = _.items.indexOf(opt); | ||
@@ -882,3 +883,3 @@ | ||
var nxtRct = util.getRect(nxtEl); | ||
var opsTop = _.optsOptions.scrollTop; | ||
var opsTop = _.tree.scrollTop; | ||
var offset = _.optsRect.top; | ||
@@ -893,5 +894,5 @@ var curOffset, nxtOffset; | ||
if (_.activeIdx === 0) { | ||
_.optsOptions.scrollTop = 0; | ||
_.tree.scrollTop = 0; | ||
} else if (nxtTp - curOffset < 0) { | ||
_.optsOptions.scrollTop = nxtOffset; | ||
_.tree.scrollTop = nxtOffset; | ||
} | ||
@@ -904,5 +905,5 @@ } else { | ||
if (_.activeIdx === 0) { | ||
_.optsOptions.scrollTop = 0; | ||
_.tree.scrollTop = 0; | ||
} else if (nxtBottom > curOffset) { | ||
_.optsOptions.scrollTop = nxtOffset; | ||
_.tree.scrollTop = nxtOffset; | ||
} | ||
@@ -915,3 +916,3 @@ | ||
util.removeClass(_.optsOptions.querySelector(".active"), "active"); | ||
util.removeClass(_.tree.querySelector(".active"), "active"); | ||
util.addClass(list[_.activeIdx], "active"); | ||
@@ -922,3 +923,3 @@ }; | ||
var _ = this; | ||
var opts = _.optsOptions; | ||
var opts = _.tree; | ||
var scrollTop = opts.scrollTop; | ||
@@ -956,3 +957,3 @@ var scrollHeight = opts.scrollHeight; | ||
var s = util.getRect(this.selected); | ||
var o = this.optsOptions.parentNode.offsetHeight; | ||
var o = this.tree.parentNode.offsetHeight; | ||
var wh = window.innerHeight; | ||
@@ -969,3 +970,3 @@ var doInvert = s.top + s.height + o > wh; | ||
this.optsRect = util.getRect(this.optsOptions); | ||
this.optsRect = util.getRect(this.tree); | ||
}; | ||
@@ -1113,3 +1114,3 @@ | ||
this.optsRect = util.getRect(this.optsOptions); | ||
this.optsRect = util.getRect(this.tree); | ||
@@ -1387,3 +1388,3 @@ this.rendered = true; | ||
var scrollHeight = this.optsOptions.scrollHeight; | ||
var scrollHeight = this.tree.scrollHeight; | ||
@@ -1399,3 +1400,3 @@ if ( scrollHeight <= _.optsRect.height ) { | ||
util.attr(_.selected, "aria-expanded", true); | ||
util.attr(_.optsOptions, { | ||
util.attr(_.tree, { | ||
"aria-hidden": false, | ||
@@ -1442,3 +1443,3 @@ "aria-expanded": true | ||
util.attr(this.selected, "aria-expanded", false); | ||
util.attr(this.optsOptions, { | ||
util.attr(this.tree, { | ||
"aria-hidden": true, | ||
@@ -1541,6 +1542,9 @@ "aria-expanded": false | ||
*/ | ||
Selectr.prototype.disable = function() { | ||
Selectr.prototype.disable = function(container) { | ||
if ( !this.disabled ) { | ||
if ( !container ) { | ||
this.el.disabled = true; | ||
} | ||
this.disabled = true; | ||
this.el.disabled = true; | ||
util.addClass(this.container, "disabled"); | ||
@@ -1547,0 +1551,0 @@ } |
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
1736
77309