mobius1-selectr
Advanced tools
Comparing version 2.0.2 to 2.0.3
{ | ||
"name": "mobius1-selectr", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"ignore": [ | ||
@@ -5,0 +5,0 @@ ".gitattributes", |
/*! | ||
* Selectr 2.0.2 | ||
* Selectr 2.0.3 | ||
* 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=!1,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(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)}},getBoundingRect:function(a){var b=window,c=document,d=c.body,e=a.getBoundingClientRect(),f=void 0!==b.pageXOffset?b.pageXOffset:(c.documentElement||d.parentNode||d).scrollLeft,g=void 0!==b.pageYOffset?b.pageYOffset:(c.documentElement||d.parentNode||d).scrollTop;return{bottom:e.bottom+g,height:e.height,left:e.left+f,right:e.right+f,top:e.top+g,width:e.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(){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){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 q,r,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 s=l.top+o;q=p,r=n+(s-q),0===c.activeIdx?c.optsOptions.scrollTop=0:s-q<0&&(c.optsOptions.scrollTop=r)}else{var t=l.top+o+l.height;q=p+c.optsRect.height,r=n+t-q,0===c.activeIdx?c.optsOptions.scrollTop=0:t>q&&(c.optsOptions.scrollTop=r),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!=!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))}},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;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 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)}},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=!1,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(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)}},getBoundingRect:function(a){var b=window,c=document,d=c.body,e=a.getBoundingClientRect(),f=void 0!==b.pageXOffset?b.pageXOffset:(c.documentElement||d.parentNode||d).scrollLeft,g=void 0!==b.pageYOffset?b.pageYOffset:(c.documentElement||d.parentNode||d).scrollTop;return{bottom:e.bottom+g,height:e.height,left:e.left+f,right:e.right+f,top:e.top+g,width:e.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(){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){var d=new Option(c.text,c.value,c.selected||!1,c.selected||!1);c.disabled&&(d.disabled=!0),a.el.add(d)}),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"}),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){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 p,q,k=f[c.activeIdx],l=b.getBoundingRect(k),n=c.optsOptions.scrollTop,o=c.optsRect.top;if("up"===g){var r=l.top;p=o,q=n+(r-p),0===c.activeIdx?c.optsOptions.scrollTop=0:r-p<0&&(c.optsOptions.scrollTop=q)}else{var s=l.top+l.height;p=o+c.optsRect.height,q=n+s-p,0===c.activeIdx?c.optsOptions.scrollTop=0:s>p&&(c.optsOptions.scrollTop=q),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!=!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))}},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;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 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)}},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(a.container,"open"),a.optsRect=b.getBoundingRect(a.optsOptions);var c=window.innerHeight,d=a.optsOptions.scrollHeight,e=a.elRect.top+a.elRect.height+a.optsRect.height>c;d<=a.optsRect.height&&a.requiresPagination&&m.call(a),e?(b.addClass(a.container,"inverted"),this.isInverted=!0):(b.removeClass(a.container,"inverted"),this.isInverted=!1),b.removeClass(a.container,"notice"),a.settings.searchable&&setTimeout(function(){a.input.focus()},10),a.optsRect=b.getBoundingRect(a.optsOptions),a.opened=!0,a.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.2", | ||
"version": "2.0.3", | ||
"description": "A lightweight dependency-free javascript select box replacement.", | ||
@@ -5,0 +5,0 @@ "main": "dist/selectr.min.js", |
/*! | ||
* Selectr 2.0.2 | ||
* Selectr 2.0.3 | ||
* http://mobius.ovh/docs/selectr | ||
@@ -168,3 +168,9 @@ * | ||
util.each(data, function(idx, itm) { | ||
_.el.add(new Option(itm.text, itm.value, itm.selected || false, itm.selected || false)); | ||
var option = new Option(itm.text, itm.value, itm.selected || false, itm.selected || false); | ||
if ( itm.disabled ) { | ||
option.disabled = true; | ||
} | ||
_.el.add(option); | ||
}); | ||
@@ -188,2 +194,7 @@ | ||
// Custom className | ||
if (o.customClass) { | ||
util.addClass(_.container, o.customClass); | ||
} | ||
setWidth.call(_); | ||
@@ -651,7 +662,7 @@ | ||
case 38: | ||
dir = "up"; | ||
dir = "up"; | ||
if ( _.activeIdx > 0 ) { _.activeIdx--; } | ||
break; | ||
case 40: | ||
dir = "down"; | ||
dir = "down"; | ||
if ( _.activeIdx < list.length - 1 ) { _.activeIdx++; } | ||
@@ -663,10 +674,9 @@ } | ||
var nextElem = list[_.activeIdx]; | ||
var nextRect = nextElem.getBoundingClientRect(); | ||
var nextRect = util.getBoundingRect(nextElem); | ||
var optsTop = _.optsOptions.scrollTop; | ||
var scrollY = window.scrollY || window.pageYOffset; | ||
var offset = _.optsRect.top + scrollY; | ||
var offset = _.optsRect.top; | ||
var currentOffset, nextOffset; | ||
if (dir === "up") { | ||
var nextTop = nextRect.top + scrollY; | ||
var nextTop = nextRect.top; | ||
currentOffset = offset; | ||
@@ -681,3 +691,3 @@ nextOffset = optsTop + (nextTop - currentOffset); | ||
} else { | ||
var nextBottom = nextRect.top + scrollY + nextRect.height; | ||
var nextBottom = nextRect.top + nextRect.height; | ||
currentOffset = offset + _.optsRect.height; | ||
@@ -1011,21 +1021,27 @@ nextOffset = optsTop + nextBottom - currentOffset; | ||
util.addClass(this.container, "open"); | ||
util.addClass(_.container, "open"); | ||
_.optsRect = util.getBoundingRect(_.optsOptions); | ||
var wh = window.innerHeight; | ||
var scrollHeight = _.optsOptions.scrollHeight; | ||
var doInvert = _.elRect.top + _.elRect.height + _.optsRect.height > wh; | ||
if ( scrollHeight <= _.optsRect.height ) { | ||
paginate.call(_); | ||
if ( _.requiresPagination ) { | ||
paginate.call(_); | ||
} | ||
} | ||
if (_.optsRect.bottom > wh) { | ||
util.addClass(this.container, "inverted"); | ||
if (doInvert) { | ||
util.addClass(_.container, "inverted"); | ||
this.isInverted = true; | ||
} else { | ||
util.removeClass(this.container, "inverted"); | ||
util.removeClass(_.container, "inverted"); | ||
this.isInverted = false; | ||
} | ||
util.removeClass(this.container, "notice"); | ||
util.removeClass(_.container, "notice"); | ||
if (this.settings.searchable) { | ||
if (_.settings.searchable) { | ||
setTimeout(function() { | ||
@@ -1036,5 +1052,7 @@ _.input.focus(); | ||
this.opened = true; | ||
_.optsRect = util.getBoundingRect(_.optsOptions); | ||
this.emit("selectr.open"); | ||
_.opened = true; | ||
_.emit("selectr.open"); | ||
}; | ||
@@ -1114,2 +1132,2 @@ | ||
return Selectr; | ||
})); | ||
})); |
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
60457
1345