mobius1-selectr
Advanced tools
Comparing version 2.0.0 to 2.0.1
{ | ||
"name": "mobius1-selectr", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"ignore": [ | ||
@@ -5,0 +5,0 @@ ".gitattributes", |
/*! | ||
* Selectr 2.0.0 | ||
* Selectr 2.0.1 | ||
* 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 q(a,c){var d={width:"auto",searchable:!0,clearable:!1,sortSelected:!1};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&&!c.data)throw new Error("You don't have any options in your select!");this.settings=b.extend(d,c),p.mixin(this),this.settings.multiple&&(this.el.multiple=!0),this.multiple="select-multiple"==this.el.type,this.hasOptGroups=!1,this.el.getElementsByTagName("optgroup").length&&(this.hasOptGroups=!0),this.items=[],this.list=[],this.activeIdx=0,this.navigating,this.elRect=this.el.getBoundingClientRect(),e.call(this),this.update();var f=this;setTimeout(function(){f.emit("selectr.init")},20)}var b={extend:function(a,b){var c;for(c in b)b.hasOwnProperty(c)&&(a[c]=b[c]);return a},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.match(new RegExp("(\\s|^)"+b+"(\\s|$)"))},addClass:function(a,b){a.classList?a.classList.add(b):utils.hasClass(b)||(a.className=a.className.trim()+" "+b)},removeClass:function(a,b){a.classList?a.classList.remove(b):utils.hasClass(b)&&(a.className=a.className.replace(new RegExp("(^|\\b)"+b.split(" ").join("|")+"(\\b|$)","gi")," "))},closest:function(a,c){return a&&a!==document.body&&(c(a)?a:b.closest(a.parentNode,c))},append:function(a,b){return a&&b&&a.appendChild(b)},listen:function(a,b,c,d){a.addEventListener(b,c,d||!1)},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(0|a)===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)}},getBoundingRect:function(a){var b=window,c=a.getBoundingClientRect(),d=void 0!==b.pageXOffset?b.pageXOffset:(doc.documentElement||body.parentNode||body).scrollLeft,e=void 0!==b.pageYOffset?b.pageYOffset:(doc.documentElement||body.parentNode||body).scrollTop;return{bottom:c.bottom+e,height:c.height,left:c.left+d,right:c.right+d,top:c.top+e,width:c.width}},preventDefault:function(a){return a=a||window.event,a.preventDefault?a.preventDefault():a.returnValue=!1},includes:function(a,b){return a.indexOf(b)>-1}},c=function(){var a=this.settings,c=this.multiple;b.each(this.el.options,function(d,e){(c&&a.selectedValue&&b.includes(a.selectedValue,e.value)||!c&&a.selectedValue&&e.value==a.selectedValue)&&(e.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"),e.data){a.pageIndex=1;var h=e.pagination?e.data.slice(0,e.pagination):e.data;b.each(h,function(b,c){a.el.add(new Option(c.text,c.value,c.selected||!1,c.selected||!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"}),d.call(a),this.container.style.width=this.width,a.selected=b.createElement("div",{class:"selectr-selected"}),a.label=b.createElement(a.multiple?"ul":"span",{class:"selectr-label"});var i=b.createElement("div",{class:"selectr-options-container"});if(a.optsOptions=b.createElement("ul",{class:"selectr-options"}),a.notice=b.createElement("div",{class:"selectr-notice"}),a.multiple&&(b.addClass(a.label,"selectr-tags"),b.addClass(a.container,"multiple"),this.tags=[],this.selectedValues=[],this.selectedIndexes=[]),a.hasOptGroups){var j=0;b.addClass(a.optsOptions,"optgroups")}var k=0;b.each(a.el.children,function(c,d){if("OPTGROUP"===d.nodeName){var g=b.createElement("ul",{class:"selectr-optgroup"});b.append(g,b.createElement("li",{class:"selectr-optgroup--label",text:d.label})),b.append(a.optsOptions,g),d.children&&b.each(d.children,function(b,c){e.pagination&&e.data&&j>=e.pagination||(f.call(a,j,c,g),j++)})}else{if(e.pagination&&e.data&&k>=e.pagination)return;f.call(a,k,d),k++}}),b.addClass(a.list[a.activeIdx],"active"),a.selected.appendChild(a.label),e.clearable&&(a.selectClear=b.createElement("button",{class:"selectr-clear",type:"button"}),b.append(a.selected,a.selectClear)),e.searchable&&(a.input=b.createElement("input",{class:"selectr-input"}),a.inputClear=b.createElement("button",{class:"selectr-clear",type:"button"}),a.inputContainer=b.createElement("div",{class:"selectr-input-container"}),b.append(a.inputContainer,a.input),b.append(a.inputContainer,a.inputClear),b.append(i,a.inputContainer)),b.append(i,a.notice),b.append(i,a.optsOptions),b.append(a.container,a.selected),b.append(a.container,i);var l=e.placeholder||a.el.getAttribute("placeholder")||"Choose...";a.placeEl=b.createElement("div",{class:"selectr-placeholder",html:l}),b.append(a.selected,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});f.idx=a,this.items.push(f),c.defaultSelected&&h.call(this,a,!0),c.disabled&&b.addClass(f,"disabled"),d?b.append(d,f):b.append(this.optsOptions,f),c.disabled||this.list.push(f)}},g=function(){var a=this;a.requiresPagination=a.settings.data&&a.settings.data.length>a.settings.pagination,a.handleDismiss=n.bind(a),b.listen(a.container,"click",function(c){c=c||window.event;var d=c.target,e=b.closest(d,function(b){return b&&b==a.selected});if(a.settings.clearable&&d===a.selectClear)return void a.clear();if(b.hasClass(d,"selectr-tag-remove")&&j.call(a,d.parentNode.tag),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.listen(a.selected,"mousedown",function(a){b.preventDefault(a)}),b.listen(a.optsOptions,"mousedown",function(a){b.preventDefault(a)}),a.settings.searchable&&b.listen(a.inputClear,"click",function(b){k.call(a)}),b.listen(document,"click",a.handleDismiss),b.listen(document,"keydown",l.bind(a)),b.listen(document,"keyup",function(b){a.settings.searchable&&b.target==a.input&&a.search(a.input.value,!0),a.navigating&&13!=b.keyCode&&(a.navigating=!1)}),a.update=b.debounce(function(){a.opened&&a.close(),this.width&&(this.container.style.width=this.width)},50),b.listen(window,"resize",a.update),b.listen(window,"scroll",a.update),a.requiresPagination&&(a.paginateItems=b.debounce(function(){m.call(a)},50),b.listen(a.optsOptions,"scroll",a.paginateItems))},h=function(a,c){var d=this,e=d.items[a],f=d.el.options[a];if(!f.disabled){if(c)return void(d.multiple?i.call(d,a):(b.addClass(d.items[a],"selected"),b.addClass(d.container,"has-selected"),d.label.innerHTML=d.customSelected?d.settings.renderSelection(f):f.textContent,d.selectedValue=f.value,d.selectedIndex=a));d.multiple?b.hasClass(e,"selected")?j.call(d,f.value):i.call(d,a):(b.hasClass(e,"selected")?(f.defaultSelected=!1,b.removeClass(d.items[a],"selected"),b.removeClass(d.container,"has-selected"),d.label.innerHTML="",d.selectedValue=null,d.emit("selectr.deselect",f)):(f.defaultSelected=!0,b.addClass(d.items[a],"selected"),b.addClass(d.container,"has-selected"),d.label.innerHTML=d.customSelected?d.settings.renderSelection(f):f.textContent,d.selectedValue=f.value,d.selectedIndex=a,d.emit("selectr.select",f)),b.each(d.items,function(c,e){c!=a&&(b.removeClass(e,"selected"),d.el.options[c].defaultSelected=!1)})),d.opened&&!d.multiple&&d.close()}},i=function(a){var c=this;if(!b.includes(c.selectedIndexes,a)){if(c.multiple){var d=c.settings.maxSelections;if(d&&c.tags.length==d)return void c.setMessage("A maximum of "+d+" items can be selected.",!0)}var e=c.el.options[a],f=document.createDocumentFragment(),g=c.customSelected?c.settings.renderSelection(e):e.textContent,h=b.createElement("li",{class:"selectr-tag",html:g}),i=b.createElement("button",{class:"selectr-tag-remove",type:"button"});if(b.append(h,i),h.idx=a,h.tag=e.value,c.selectedValues.push(h.tag),c.selectedIndexes.push(a),c.tags.push(h),c.settings.sortSelected){var j=c.tags.slice();j.sort(function(a,b){var c=[],d=[];for(a.tag.replace(/(\d+)|(\D+)/g,function(a,b,d){c.push([b||1/0,d||""])}),b.tag.replace(/(\d+)|(\D+)/g,function(a,b,c){d.push([b||1/0,c||""])});c.length&&d.length;){var e=c.shift(),f=d.shift(),g=e[0]-f[0]||e[1].localeCompare(f[1]);if(g)return g}return c.length-d.length}),b.each(j,function(a,b){f.appendChild(b)}),c.label.innerHTML=""}else b.append(f,h);b.append(c.label,f),e.defaultSelected=!0,b.addClass(c.items[a],"selected"),c.emit("selectr.select",e),c.label.children.length?b.addClass(c.container,"has-selected"):b.removeClass(c.container,"has-selected")}},j=function(a){var e,f,c=this,d=this.selectedValues.indexOf(a);if(b.each(this.el.options,function(b,c){a==c.value&&(e=b)}),e>-1){var f=c.el.options[e];c.tags[d].parentNode.removeChild(c.tags[d]),c.selectedValues.splice(d,1),f.defaultSelected=!1,c.tags.splice(d,1),b.removeClass(c.items[e],"selected");var g=c.selectedIndexes.indexOf(e);c.selectedIndexes.splice(g,1),c.emit("selectr.deselect",f)}this.tags.length||b.removeClass(this.container,"has-selected")},k=function(){var a=this;a.input.value=null,a.searching=!1,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)})},l=function(a){a=a||window.event;var c=this,d=a.keyCode,e=[13,38,40];if(c.opened&&!(e.indexOf(d)<0)){b.preventDefault(a);var g,f=this.searching?this.searchItems:this.list;switch(d){case 13:var i=c.optsOptions.querySelector(".active"),j=c.items.indexOf(i);return void h.call(c,j);case 38:g="up",c.activeIdx>0&&c.activeIdx--;break;case 40:g="down",c.activeIdx<f.length-1&&c.activeIdx++}this.navigating=!0;var k=f[c.activeIdx],l=k.getBoundingClientRect(),n=c.optsOptions.scrollTop,o=window.scrollY||window.pageYOffset,p=c.optsRect.top+o;if("up"===g){var q=p,r=l.top+o,s=n+(r-q);0===c.activeIdx?c.optsOptions.scrollTop=0:r-q<0&&(c.optsOptions.scrollTop=s)}else{var q=p+c.optsRect.height,t=l.top+o+l.height,s=n+t-q;0===c.activeIdx?c.optsOptions.scrollTop=0:t>q&&(c.optsOptions.scrollTop=s),c.requiresPagination&&m.call(c)}b.removeClass(c.optsOptions.querySelector(".active"),"active"),b.addClass(f[c.activeIdx],"active")}},m=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){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})}},n=function(a){var b=a.target;this.container.contains(b)||!this.opened&&!this.container.classList.contains("notice")||this.close()},o=function(a,b,c){var d=new RegExp(a,"i").exec(c);return!!d&&b.textContent.replace(d[0],"<span>"+d[0]+"</span>")},p=function(){};return p.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!=0&&this._events[a].splice(this._events[a].indexOf(b),1)},emit:function(a){if(this._events=this._events||{},a in this._events!=0)for(var b=0;b<this._events[a].length;b++)this._events[a][b].apply(this,Array.prototype.slice.call(arguments,1))}},p.mixin=function(a){for(var b=["on","off","emit"],c=0;c<b.length;c++)"function"==typeof a?a.prototype[b[c]]=p.prototype[b[c]]:a[b[c]]=p.prototype[b[c]];return a},q.prototype.setValue=function(a){var c=this,d=b.isArray(a);d||(a=a.toString().trim()),!c.multiple&&d||b.each(this.el.options,function(e,f){(d&&b.includes(a.toString(),f.value)||f.value===a)&&h.call(c,e)})},q.prototype.getValue=function(a,c){var e,d=this;if(d.multiple)a?d.selectedIndexes.length&&(e=[],b.each(d.selectedIndexes,function(a,b){var c=d.el.options[b];e[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},q.prototype.search=function(a,c){a=a.trim();var d=this;a.length;if(!d.navigating){if(b.hasClass(d.container,"notice")||(a.length>0?b.addClass(d.inputContainer,"active"):b.removeClass(d.inputContainer,"active")),d.searching=!0,d.searchItems=[],d.searchQuery=a,b.each(d.list,function(e,f){var g=f.textContent.trim();b.includes(g.toLowerCase(),a.toLowerCase())?(d.searchItems.push(f),c&&(d.customOption||(f.innerHTML=o(a,f,g)),b.addClass(f,"match"),b.removeClass(f,"excluded"))):(b.addClass(f,"excluded"),b.removeClass(f,"match"))}),c)d.searchItems.length?d.open():(d.setMessage("No results."),d.input.focus());else if(d.searchItems.length){var f={};return f.values=[],b.each(d.searchItems,function(a,b){var c=d.el.options[b.idx];f.values[a]={value:c.value,text:c.textContent}}),f}d.emit("selectr.search",a,d.searchItems)}},q.prototype.toggle=function(){if(!this.disabled){var a=b.hasClass(this.container,"open");a?this.close():this.open()}},q.prototype.open=function(){var a=this;b.addClass(this.container,"open"),a.optsRect=b.getBoundingRect(a.optsOptions);var c=window.innerHeight,d=a.optsOptions.scrollHeight;d<=a.optsRect.height&&m.call(a),a.optsRect.bottom>c?b.addClass(this.container,"inverted"):b.removeClass(this.container,"inverted"),b.removeClass(this.container,"notice"),this.settings.searchable&&setTimeout(function(){a.input.focus()},10),this.opened=!0,this.emit("selectr.open")},q.prototype.close=function(){var a=b.hasClass(this.container,"notice");this.settings.searchable&&!a&&(this.input.blur(),this.searching=!1),a&&(b.removeClass(this.container,"notice"),this.notice.textContent=""),b.removeClass(this.container,"open"),this.opened=!1,this.emit("selectr.close")},q.prototype.reset=function(a){this.clear(),c.call(this),b.each(this.el.options,function(a,b){b.defaultSelected&&h.call(this,a)},this),this.emit("selectr.reset")},q.prototype.clear=function(a){if(this.multiple){var c=this.selectedIndexes.slice();b.each(c,function(a,b){h.call(this,b)},this)}else h.call(this,this.selectedIndex);k.call(this),this.emit("selectr.clear")},q.prototype.setMessage=function(a,c){c&&this.close(),b.addClass(this.container,"notice"),this.notice.textContent=a},q}); | ||
!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 q(a,c){var d={width:"auto",searchable:!0,clearable:!1,sortSelected:!1};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&&!c.data)throw new Error("You don't have any options in your select!");this.settings=b.extend(d,c),p.mixin(this),this.settings.multiple&&(this.el.multiple=!0),this.multiple="select-multiple"==this.el.type,this.hasOptGroups=!1,this.el.getElementsByTagName("optgroup").length&&(this.hasOptGroups=!0),this.items=[],this.list=[],this.activeIdx=0,this.navigating,this.elRect=this.el.getBoundingClientRect(),e.call(this),this.update();var f=this;setTimeout(function(){f.emit("selectr.init")},20)}var b={extend:function(a,b){var c;for(c in b)b.hasOwnProperty(c)&&(a[c]=b[c]);return a},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))},append:function(a,b){return a&&b&&a.appendChild(b)},listen:function(a,b,c,d){a.addEventListener(b,c,d||!1)},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(0|a)===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)}},getBoundingRect:function(a){var b=window,c=a.getBoundingClientRect(),d=void 0!==b.pageXOffset?b.pageXOffset:(doc.documentElement||body.parentNode||body).scrollLeft,e=void 0!==b.pageYOffset?b.pageYOffset:(doc.documentElement||body.parentNode||body).scrollTop;return{bottom:c.bottom+e,height:c.height,left:c.left+d,right:c.right+d,top:c.top+e,width:c.width}},preventDefault:function(a){return a=a||window.event,a.preventDefault?a.preventDefault():a.returnValue=!1},includes:function(a,b){return a.indexOf(b)>-1}},c=function(){var a=this.settings,c=this.multiple;b.each(this.el.options,function(d,e){(c&&a.selectedValue&&b.includes(a.selectedValue,e.value)||!c&&a.selectedValue&&e.value==a.selectedValue)&&(e.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"),e.data){a.pageIndex=1;var h=e.pagination?e.data.slice(0,e.pagination):e.data;b.each(h,function(b,c){a.el.add(new Option(c.text,c.value,c.selected||!1,c.selected||!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"}),d.call(a),this.container.style.width=this.width,a.selected=b.createElement("div",{class:"selectr-selected"}),a.label=b.createElement(a.multiple?"ul":"span",{class:"selectr-label"});var i=b.createElement("div",{class:"selectr-options-container"});if(a.optsOptions=b.createElement("ul",{class:"selectr-options"}),a.notice=b.createElement("div",{class:"selectr-notice"}),a.multiple&&(b.addClass(a.label,"selectr-tags"),b.addClass(a.container,"multiple"),this.tags=[],this.selectedValues=[],this.selectedIndexes=[]),a.hasOptGroups){var j=0;b.addClass(a.optsOptions,"optgroups")}var k=0;b.each(a.el.children,function(c,d){if("OPTGROUP"===d.nodeName){var g=b.createElement("ul",{class:"selectr-optgroup"});b.append(g,b.createElement("li",{class:"selectr-optgroup--label",text:d.label})),b.append(a.optsOptions,g),d.children&&b.each(d.children,function(b,c){e.pagination&&e.data&&j>=e.pagination||(f.call(a,j,c,g),j++)})}else{if(e.pagination&&e.data&&k>=e.pagination)return;f.call(a,k,d),k++}}),b.addClass(a.list[a.activeIdx],"active"),a.selected.appendChild(a.label),e.clearable&&(a.selectClear=b.createElement("button",{class:"selectr-clear",type:"button"}),b.append(a.selected,a.selectClear)),e.searchable&&(a.input=b.createElement("input",{class:"selectr-input"}),a.inputClear=b.createElement("button",{class:"selectr-clear",type:"button"}),a.inputContainer=b.createElement("div",{class:"selectr-input-container"}),b.append(a.inputContainer,a.input),b.append(a.inputContainer,a.inputClear),b.append(i,a.inputContainer)),b.append(i,a.notice),b.append(i,a.optsOptions),b.append(a.container,a.selected),b.append(a.container,i);var l=e.placeholder||a.el.getAttribute("placeholder")||"Choose...";a.placeEl=b.createElement("div",{class:"selectr-placeholder",html:l}),b.append(a.selected,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});f.idx=a,this.items.push(f),c.defaultSelected&&h.call(this,a,!0),c.disabled&&b.addClass(f,"disabled"),d?b.append(d,f):b.append(this.optsOptions,f),c.disabled||this.list.push(f)}},g=function(){var a=this;a.requiresPagination=a.settings.data&&a.settings.data.length>a.settings.pagination,a.handleDismiss=n.bind(a),b.listen(a.container,"click",function(c){c=c||window.event;var d=c.target,e=b.closest(d,function(b){return b&&b==a.selected});if(a.settings.clearable&&d===a.selectClear)return void a.clear();if(b.hasClass(d,"selectr-tag-remove")&&j.call(a,d.parentNode.tag),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.listen(a.selected,"mousedown",function(a){b.preventDefault(a)}),b.listen(a.optsOptions,"mousedown",function(a){b.preventDefault(a)}),a.settings.searchable&&b.listen(a.inputClear,"click",function(b){k.call(a)}),b.listen(document,"click",a.handleDismiss),b.listen(document,"keydown",l.bind(a)),b.listen(document,"keyup",function(b){a.settings.searchable&&b.target==a.input&&a.search(a.input.value,!0),a.navigating&&13!=b.keyCode&&(a.navigating=!1)}),a.update=b.debounce(function(){a.opened&&a.close(),this.width&&(this.container.style.width=this.width)},50),b.listen(window,"resize",a.update),b.listen(window,"scroll",a.update),a.requiresPagination&&(a.paginateItems=b.debounce(function(){m.call(a)},50),b.listen(a.optsOptions,"scroll",a.paginateItems))},h=function(a,c){var d=this,e=d.items[a],f=d.el.options[a];if(!f.disabled){if(c)return void(d.multiple?i.call(d,a):(b.addClass(d.items[a],"selected"),b.addClass(d.container,"has-selected"),d.label.innerHTML=d.customSelected?d.settings.renderSelection(f):f.textContent,d.selectedValue=f.value,d.selectedIndex=a));d.multiple?b.hasClass(e,"selected")?j.call(d,f.value):i.call(d,a):(b.hasClass(e,"selected")?(f.defaultSelected=!1,b.removeClass(d.items[a],"selected"),b.removeClass(d.container,"has-selected"),d.label.innerHTML="",d.selectedValue=null,d.emit("selectr.deselect",f)):(f.defaultSelected=!0,b.addClass(d.items[a],"selected"),b.addClass(d.container,"has-selected"),d.label.innerHTML=d.customSelected?d.settings.renderSelection(f):f.textContent,d.selectedValue=f.value,d.selectedIndex=a,d.emit("selectr.select",f)),b.each(d.items,function(c,e){c!=a&&(b.removeClass(e,"selected"),d.el.options[c].defaultSelected=!1)})),d.opened&&!d.multiple&&d.close()}},i=function(a){var c=this;if(!b.includes(c.selectedIndexes,a)){if(c.multiple){var d=c.settings.maxSelections;if(d&&c.tags.length==d)return void c.setMessage("A maximum of "+d+" items can be selected.",!0)}var e=c.el.options[a],f=document.createDocumentFragment(),g=c.customSelected?c.settings.renderSelection(e):e.textContent,h=b.createElement("li",{class:"selectr-tag",html:g}),i=b.createElement("button",{class:"selectr-tag-remove",type:"button"});if(b.append(h,i),h.idx=a,h.tag=e.value,c.selectedValues.push(h.tag),c.selectedIndexes.push(a),c.tags.push(h),c.settings.sortSelected){var j=c.tags.slice();j.sort(function(a,b){var c=[],d=[];for(a.tag.replace(/(\d+)|(\D+)/g,function(a,b,d){c.push([b||1/0,d||""])}),b.tag.replace(/(\d+)|(\D+)/g,function(a,b,c){d.push([b||1/0,c||""])});c.length&&d.length;){var e=c.shift(),f=d.shift(),g=e[0]-f[0]||e[1].localeCompare(f[1]);if(g)return g}return c.length-d.length}),b.each(j,function(a,b){f.appendChild(b)}),c.label.innerHTML=""}else b.append(f,h);b.append(c.label,f),e.defaultSelected=!0,b.addClass(c.items[a],"selected"),c.emit("selectr.select",e),c.label.children.length?b.addClass(c.container,"has-selected"):b.removeClass(c.container,"has-selected")}},j=function(a){var e,f,c=this,d=this.selectedValues.indexOf(a);if(b.each(this.el.options,function(b,c){a==c.value&&(e=b)}),e>-1){var f=c.el.options[e];c.tags[d].parentNode.removeChild(c.tags[d]),c.selectedValues.splice(d,1),f.defaultSelected=!1,c.tags.splice(d,1),b.removeClass(c.items[e],"selected");var g=c.selectedIndexes.indexOf(e);c.selectedIndexes.splice(g,1),c.emit("selectr.deselect",f)}this.tags.length||b.removeClass(this.container,"has-selected")},k=function(){var a=this;a.input.value=null,a.searching=!1,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)})},l=function(a){a=a||window.event;var c=this,d=a.keyCode,e=[13,38,40];if(c.opened&&!(e.indexOf(d)<0)){b.preventDefault(a);var g,f=this.searching?this.searchItems:this.list;switch(d){case 13:var i=c.optsOptions.querySelector(".active"),j=c.items.indexOf(i);return void h.call(c,j);case 38:g="up",c.activeIdx>0&&c.activeIdx--;break;case 40:g="down",c.activeIdx<f.length-1&&c.activeIdx++}this.navigating=!0;var k=f[c.activeIdx],l=k.getBoundingClientRect(),n=c.optsOptions.scrollTop,o=window.scrollY||window.pageYOffset,p=c.optsRect.top+o;if("up"===g){var q=p,r=l.top+o,s=n+(r-q);0===c.activeIdx?c.optsOptions.scrollTop=0:r-q<0&&(c.optsOptions.scrollTop=s)}else{var q=p+c.optsRect.height,t=l.top+o+l.height,s=n+t-q;0===c.activeIdx?c.optsOptions.scrollTop=0:t>q&&(c.optsOptions.scrollTop=s),c.requiresPagination&&m.call(c)}b.removeClass(c.optsOptions.querySelector(".active"),"active"),b.addClass(f[c.activeIdx],"active")}},m=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){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})}},n=function(a){var b=a.target;this.container.contains(b)||!this.opened&&!this.container.classList.contains("notice")||this.close()},o=function(a,b,c){var d=new RegExp(a,"i").exec(c);return!!d&&b.textContent.replace(d[0],"<span>"+d[0]+"</span>")},p=function(){};return p.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!=0&&this._events[a].splice(this._events[a].indexOf(b),1)},emit:function(a){if(this._events=this._events||{},a in this._events!=0)for(var b=0;b<this._events[a].length;b++)this._events[a][b].apply(this,Array.prototype.slice.call(arguments,1))}},p.mixin=function(a){for(var b=["on","off","emit"],c=0;c<b.length;c++)"function"==typeof a?a.prototype[b[c]]=p.prototype[b[c]]:a[b[c]]=p.prototype[b[c]];return a},q.prototype.setValue=function(a){var c=this,d=b.isArray(a);d||(a=a.toString().trim()),!c.multiple&&d||b.each(this.el.options,function(e,f){(d&&b.includes(a.toString(),f.value)||f.value===a)&&h.call(c,e)})},q.prototype.getValue=function(a,c){var e,d=this;if(d.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},q.prototype.search=function(a,c){a=a.trim();var d=this;a.length;if(!d.navigating){if(b.hasClass(d.container,"notice")||(a.length>0?b.addClass(d.inputContainer,"active"):b.removeClass(d.inputContainer,"active")),d.searching=!0,d.searchItems=[],d.searchQuery=a,b.each(d.list,function(e,f){var g=f.textContent.trim();b.includes(g.toLowerCase(),a.toLowerCase())?(d.searchItems.push(f),c&&(d.customOption||(f.innerHTML=o(a,f,g)),b.addClass(f,"match"),b.removeClass(f,"excluded"))):(b.addClass(f,"excluded"),b.removeClass(f,"match"))}),c)d.searchItems.length?d.open():(d.setMessage("No results."),d.input.focus());else if(d.searchItems.length){var f={};return f.values=[],b.each(d.searchItems,function(a,b){var c=d.el.options[b.idx];f.values[a]={value:c.value,text:c.textContent}}),f}d.emit("selectr.search",a,d.searchItems)}},q.prototype.toggle=function(){if(!this.disabled){var a=b.hasClass(this.container,"open");a?this.close():this.open()}},q.prototype.open=function(){var a=this;b.addClass(this.container,"open"),a.optsRect=b.getBoundingRect(a.optsOptions);var c=window.innerHeight,d=a.optsOptions.scrollHeight;d<=a.optsRect.height&&m.call(a),a.optsRect.bottom>c?b.addClass(this.container,"inverted"):b.removeClass(this.container,"inverted"),b.removeClass(this.container,"notice"),this.settings.searchable&&setTimeout(function(){a.input.focus()},10),this.opened=!0,this.emit("selectr.open")},q.prototype.close=function(){var a=b.hasClass(this.container,"notice");this.settings.searchable&&!a&&(this.input.blur(),this.searching=!1),a&&(b.removeClass(this.container,"notice"),this.notice.textContent=""),b.removeClass(this.container,"open"),this.opened=!1,this.emit("selectr.close")},q.prototype.reset=function(a){this.clear(),c.call(this),b.each(this.el.options,function(a,b){b.defaultSelected&&h.call(this,a)},this),this.emit("selectr.reset")},q.prototype.clear=function(a){if(this.multiple){var c=this.selectedIndexes.slice();b.each(c,function(a,b){h.call(this,b)},this)}else h.call(this,this.selectedIndex);k.call(this),this.emit("selectr.clear")},q.prototype.setMessage=function(a,c){c&&this.close(),b.addClass(this.container,"notice"),this.notice.textContent=a},q}); |
{ | ||
"name": "mobius1-selectr", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "A lightweight dependency-free javascript select box replacement.", | ||
@@ -5,0 +5,0 @@ "main": "dist/selectr.min.js", |
# Selectr | ||
A lightweight dependency-free select box replacement written in vanilla javascript. 2.8kb minified and gzipped. | ||
A lightweight dependency-free select box replacement written in vanilla javascript. Similar to Select2 and Chosen without the dependencies. | ||
@@ -13,10 +13,44 @@ Features: | ||
[Demos & Documentation](http://mobius.ovh/docs/selectr) (under-construction) | ||
Supports most modern browsers including IE9 and above. | ||
## NOTICE | ||
[Demos & Documentation](http://mobius.ovh/docs/selectr) | ||
Selectr started out as a plugin I wrote for personal use to replace Select2 as I no longer use jQuery on my projects. It was never meant for uses other than my projects so at the moment it's a mess of bugs, dodgy fixes and workarounds to make it usable for other people's projects. Due to an increasing number of people using the plugin I've decided to rebuild Selectr from the ground up as a proper plugin instead of a script bodged to work with other projects. | ||
#Change Log | ||
v2.0.0 of Selectr is currently scheduled for release this week along with completed documentation. Therefore, versions 1.x.x will be deprecated shortly thereafter and no longer supported. | ||
## v.2.0.0 | ||
* Complete rewrite | ||
* New options added | ||
* New methods added | ||
* New events added | ||
Updated functions: | ||
* `getValue()` (see [docs](http://mobius.ovh/docs/selectr/pages/functions)) | ||
* `setValue()` (see [docs](http://mobius.ovh/docs/selectr/pages/functions)) | ||
Added options: | ||
* `clearable` (see [docs](http://mobius.ovh/docs/selectr/pages/options-2)) | ||
* `sortSelected` (see [docs](http://mobius.ovh/docs/selectr/pages/options-2)) | ||
Updated options: | ||
* `selectedValue` is now used for both single and multi selects (see [docs](http://mobius.ovh/docs/selectr/pages/options-2)) | ||
Removed options: | ||
* `ajax` | ||
* `minChars` | ||
* `emptyOption` | ||
* `selectedValues` | ||
* `selectedIndex, selectedIndexes` | ||
Removed events: | ||
* `selectr.change` (see [docs](http://mobius.ovh/docs/selectr/pages/events-2)) | ||
# License | ||
Copyright 2016 Karl Saunders | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
/*! | ||
* Selectr 2.0.0 | ||
* Selectr 2.0.1 | ||
* http://mobius.ovh/docs/selectr | ||
@@ -47,10 +47,10 @@ * | ||
}, | ||
hasClass: function(e, c) { | ||
return e.classList ? e.classList.contains(c) : !!e.className.match(new RegExp("(\\s|^)" + c + "(\\s|$)")); | ||
hasClass: function(a, b) { | ||
return a.classList ? a.classList.contains(b) : !!a.className && !!a.className.match(new RegExp("(\\s|^)" + b + "(\\s|$)")) | ||
}, | ||
addClass: function(a, b) { | ||
a.classList ? a.classList.add(b) : utils.hasClass(b) || (a.className = a.className.trim() + " " + b) | ||
util.hasClass(a, b) || (a.classList ? a.classList.add(b) : a.className = a.className.trim() + " " + b) | ||
}, | ||
removeClass: function(a, b) { | ||
a.classList ? a.classList.remove(b) : utils.hasClass(b) && (a.className = a.className.replace(new RegExp("(^|\\b)" + b.split(" ").join("|") + "(\\b|$)", "gi"), " ")) | ||
util.hasClass(a, b) && (a.classList ? a.classList.remove(b) : a.className = a.className.replace(new RegExp("(^|\\s)" + b.split(" ").join("|") + "(\\s|$)", "gi"), " ")) | ||
}, | ||
@@ -858,6 +858,7 @@ closest: function(el, fn) { | ||
if ( _.selectedIndexes.length ) { | ||
value = []; | ||
value = {}; | ||
value.values = []; | ||
util.each(_.selectedIndexes, function(i,index) { | ||
var option = _.el.options[index]; | ||
value[i] = { | ||
value.values[i] = { | ||
value: option.value, | ||
@@ -864,0 +865,0 @@ text: option.textContent, |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
57651
1205
56
0