Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mobius1-selectr

Package Overview
Dependencies
Maintainers
1
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mobius1-selectr - npm Package Compare versions

Comparing version 1.0.3 to 1.0.4

2

bower.json
{
"name": "mobius1-selectr",
"version": "1.0.3",
"version": "1.0.4",
"ignore": [

@@ -5,0 +5,0 @@ ".gitattributes",

{
"name": "mobius1-selectr",
"version": "1.0.3",
"version": "1.0.4",
"description": "A lightweight dependency-free javascript select box replacement.",

@@ -5,0 +5,0 @@ "main": "selectr.min.js",

@@ -7,6 +7,10 @@ # Selectr

* Supports single and multiple select boxes
* Supports optgroups
* Tagging support
* Searchable options
* Ajax options
* Custom events
* Custom styling
* Much more...
[Demos & Documentation](https://mobius1.github.io/Selectr/)
Selectr is so feature-packed that it needs it's own documentation site so why not head over there now - [Demos & Documentation](https://mobius1.github.io/Selectr/)
/*!
* Selectr 1.0.3
* Selectr 1.0.4
* http://mobiuswebdesign.co.uk/plugins/selectr

@@ -120,3 +120,4 @@ *

this.hasTemplate = this.options.hasOwnProperty('render') && typeof this.options.render === 'function';
this.customOption = this.options.hasOwnProperty('renderOption') && typeof this.options.renderOption === 'function';
this.customSelected = this.options.hasOwnProperty('renderSelection') && typeof this.options.renderSelection === 'function';

@@ -339,3 +340,3 @@ this.initialise();

var content = this.hasTemplate ? this.options.render(option) : option.textContent.trim();
var content = this.customOption ? this.options.renderOption(option) : option.textContent.trim();
var opt = _newElement('li', { class: 'selectr-option', html: content });

@@ -526,3 +527,3 @@

if ( _this.hasTemplate ) {
if ( _this.customOption ) {
_addClass(opt, 'match');

@@ -617,3 +618,3 @@ } else {

_this.txt.innerHTML = _this.hasTemplate ? _this.options.render(option) : option.textContent;
_this.txt.innerHTML = _this.customSelected ? _this.options.renderSelection(option) : option.textContent;

@@ -799,3 +800,3 @@ option.selected = true;

} else {
content = this.hasTemplate ? this.options.render(option) : option.textContent
content = this.customSelected ? this.options.renderSelection(option) : option.textContent
}

@@ -892,3 +893,3 @@

let option = _this.opts[i];
elem.innerHTML = _this.hasTemplate ? _this.options.render(option) : option.textContent;
elem.innerHTML = _this.customOption ? _this.options.renderOption(option) : option.textContent;
_removeClass(elem, 'match');

@@ -979,3 +980,3 @@ _removeClass(elem, 'excluded');

} else {
_this.txt.innerHTML = _this.hasTemplate ? _this.options.render(_this.opts[index]) : _this.opts[index].textContent;
_this.txt.innerHTML = _this.customOption ? _this.options.renderOption(_this.opts[index]) : _this.opts[index].textContent;

@@ -1026,3 +1027,3 @@ let old = _this.optsOptions.getElementsByClassName('selected')[0];

} else {
this.txt.innerHTML = this.hasTemplate ? this.options.render(option) : option.textContent;
this.txt.innerHTML = this.customOption ? this.options.renderOption(option) : option.textContent;
if ( this.elem.selectedIndex !== null ) {

@@ -1029,0 +1030,0 @@ _removeClass(this.list[this.elem.selectedIndex], 'selected');

/*!
* Selectr 1.0.3
* Selectr 1.0.4
* http://mobiuswebdesign.co.uk/plugins/selectr

@@ -7,2 +7,2 @@ *

*/
String.prototype.includes||(String.prototype.includes=function(a,b){"use strict";return"number"!=typeof b&&(b=0),!(b+a.length>this.length)&&this.indexOf(a,b)!==-1}),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 k(a,c){if(null===a)throw new Error("Selectr requires an element to work.");var d={minChars:1,width:"auto",emptyOption:!0,searchable:!0,selectedIndex:null,selectedValue:null,selectedIndexes:[],selectedValues:[],containerClass:"",maxSelections:null};this.elem=a,this.elemRect=this.elem.getBoundingClientRect(),this.selectedVal=null,this.selectedVals=[],this.ajaxOpts=!1,this.tags=[],this.opts=[],this.values=[],this.list=[],this.lastLen=0,this.disabled=!1,this.opened=!1,this.searching=!1,this.searchList=[],this.activeIdx=0,this.remote=!1,this.options=b(d,c),this.hasTemplate=this.options.hasOwnProperty("render")&&"function"==typeof this.options.render,this.initialise()}var b=function(a,b){var c;for(c in b)b.hasOwnProperty(c)&&(a[c]=b[c]);return a},c=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},d=function(a,b,c){if("[object Object]"===Object.prototype.toString.call(a)){var d;for(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)},e=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)}},f=function(a,b){return a.classList?a.classList.contains(b):!!a.className.match(new RegExp("(\\s|^)"+b+"(\\s|$)"))},g=function(a,b){a.classList?a.classList.add(b):f(b)||(a.className=a.className.trim()+" "+b)},h=function(a,b){a.classList?a.classList.remove(b):f(b)&&(a.className=a.className.replace(new RegExp("(^|\\b)"+b.split(" ").join("|")+"(\\b|$)","gi")," "))},i=function(a,b){a.appendChild(b)},j=function(a,b,c,d){a.addEventListener(b,c,d||!1)};return k.prototype={initialise:function(){if(!this.initialised){var a=this;if(this.on=function(b,c){this.elem.addEventListener(b,function(b){c.call(this,b,a)})},a.options.multiple&&(a.elem.setAttribute("multiple",!0),a.elem.multiple=!0),this.options.data){var b=document.createDocumentFragment();_forEach(this.options.data,function(a,d){let e=c("option",{value:d.value});e.textContent=d.text,b.appendChild(e)}),this.elem.appendChild(b)}a.options.ajax&&"object"==typeof a.options.ajax?a.setAjaxUrl():this.setSelections(),this.build(),this.initialised=!0,setTimeout(function(){a.emit("selectr.init")},100)}},setSelections:function(){var a=this;"OPTGROUP"===this.elem.options[0].parentNode.nodeName&&(this.hasOptGroups=!0),this.elem.multiple?(this.options.emptyOption=!1,(this.options.selectedIndexes.length||this.options.selectedValues.length)&&d(this.elem.options,function(b,c){c.selected=!1,(a.options.selectedIndexes.indexOf(b)>-1||a.options.selectedValues.indexOf(c.value)>-1)&&(c.selected=!0)})):(this.options.emptyOption&&(this.emptyOpt=c("option",{value:"",selected:!0}),this.hasOptGroups?this.elem.insertBefore(this.emptyOpt,this.elem.options[0].parentNode):this.elem.insertBefore(this.emptyOpt,this.elem.options[0])),null!==this.options.selectedIndex?(this.options.emptyOption&&this.options.selectedIndex++,this.elem.value=this.elem.options[this.options.selectedIndex].value):null!==this.options.selectedValue&&(this.elem.value=this.options.selectedValue,this.selectedVal=this.options.selectedValue))},setAjaxUrl:function(){this.ajaxOpts=!0;var a=this,b=a.options.ajax;a.ajax_url=b.url,b.queryParam&&(a.ajax_url+="?",b.params&&d(b.params,function(b,c){a.ajax_url+=b+"="+c+"&"}),a.ajax_url+=b.queryParam+"="),"function"!=typeof a.options.ajax.formatSelected&&(a.options.ajax.formatSelected=function(a){return a.text})},build:function(){var a=this;this.optsFrag=document.createDocumentFragment(),g(this.elem,"hidden-input"),this.container=c("div",{id:"selectr-"+a.elem.id,class:"selectr-container "+this.options.containerClass}),this.selected=c("div",{class:"selectr-selected"}),this.txt=c(this.elem.multiple?"ul":"span",{class:"selectr-text"}),this.optsContainer=c("div",{class:"selectr-options-container"}),this.optsOptions=c("ul",{class:"selectr-options"}),this.notice=c("div",{class:"selectr-notice"}),this.elem.multiple&&(g(this.txt,"selectr-tags"),g(this.container,"multiple")),this.options.searchable&&(this.input=c("input",{class:"selectr-input"}),this.clear=c("button",{class:"selectr-clear",type:"button"}),this.inputContainer=c("div",{class:"selectr-input-container"})),a.options.ajax||(this.hasOptGroups?(g(this.optsOptions,"optgroups"),d(this.elem.children,function(b,e){if("OPTGROUP"===e.nodeName){let b=c("li",{class:"selectr-optgroup",html:e.label});i(a.optsFrag,b),e.children&&d(e.children,function(b,c){a.buildOption(b,c)})}})):d(this.elem.options,function(b,c){a.buildOption(b,c)}),g(this.list[this.activeIdx],"active")),i(this.optsOptions,this.optsFrag),i(this.selected,this.txt),i(this.container,this.selected),this.options.searchable&&(i(this.inputContainer,this.input),i(this.inputContainer,this.clear),i(this.optsContainer,this.inputContainer)),i(this.optsContainer,this.optsOptions),i(this.optsContainer,this.notice),i(this.container,this.optsContainer);var b=this.options.placeholder||this.elem.getAttribute("placeholder")||"Choose...";this.placeElem=c("div",{class:"selectr-placeholder",html:b}),i(this.selected,this.placeElem),(!this.elem.multiple&&this.elem.value.length||this.elem.multiple&&this.txt.children.length)&&(g(this.container,"has-selected"),!this.elem.multiple&&this.emptyOpt&&(this.emptyOpt.selected=!1)),this.elem.parentNode.insertBefore(this.container,this.elem),i(this.container,this.elem),this.setDimensions(),this.attachEventListeners()},buildOption:function(a,b){if(b!==this.emptyOpt&&"OPTION"===b.nodeName&&b.value){var d=this.hasTemplate?this.options.render(b):b.textContent.trim(),e=c("li",{class:"selectr-option",html:d});if(b.hasAttribute("selected")&&(b.selected=!0),i(this.optsFrag,e),b.selected&&(g(e,"selected"),this.elem.multiple?this.createTag(b):(this.txt.innerHTML=d,this.selectedIndex=a),this.selectedVals.push(b.value)),b.disabled)return void g(e,"disabled");this.opts.push(b),this.values.push(b.value),this.list.push(e)}},attachEventListeners:function(){var a=this;this.handleClickEvents=this.handleEvents.bind(this),this.handleDismiss=this.dismiss.bind(this),this.handleNavigate=this.navigate.bind(this),j(this.container,"click",this.handleClickEvents),j(this.selected,"mousedown",function(a){a.preventDefault()}),j(this.optsOptions,"mousedown",function(a){a.preventDefault()}),this.options.searchable&&(j(this.input,"keyup",this.search.bind(this)),j(this.clear,"click",this.clearOptions.bind(this))),j(document,"click",this.handleDismiss),j(document,"keydown",this.handleNavigate),this.update=e(function(){a.setDimensions()},50),j(window,"resize",this.update),j(window,"scroll",this.update)},handleEvents:function(a){a=a||window.event;var b=a.target;b!==this.txt&&b!==this.placeElem||(b=this.placeElem.parentNode),b===this.selected&&(this.disabled||this.toggleOptions()),f(b,"selectr-option")&&this.selectOption(a),f(b,"selectr-tag-remove")&&this.removeTags(a),a.preventDefault()},navigate:function(a){a=a||window.e;var b=this,c=a.keyCode;if(b.opened&&(13===c||38===c||40===c)){a.preventDefault();var e,d=this.searching?this.searchList:this.list;switch(c){case 13:return void b.selectOption(a);case 38:e="up",b.activeIdx>0&&b.activeIdx--;break;case 40:e="down",b.activeIdx<d.length-1&&b.activeIdx++}var f=d[b.activeIdx],i=f.getBoundingClientRect(),j=b.optsOptions.scrollTop,k=window.scrollY||window.pageYOffset,l=b.optsRect.top+k;if("up"===e){var m=l,n=i.top+k,o=j+(n-m);0===b.activeIdx?b.optsOptions.scrollTop=0:n-m<0&&(b.optsOptions.scrollTop=o)}else{var m=l+b.optsRect.height,p=i.top+k+i.height,o=j+p-m;0===b.activeIdx?b.optsOptions.scrollTop=0:p>m&&(b.optsOptions.scrollTop=o)}h(b.optsOptions.getElementsByClassName("active")[0],"active"),g(d[b.activeIdx],"active")}},search:function(a){var b=this,c=b.input.value,e=c.length,f=38===a.keyCode||40===a.keyCode;if(!(e<this.options.minChars&&e>=this.lastLen||f)){if(this.ajaxOpts)return void this.ajaxSearch();this.container.classList.contains("notice")||(c.length>0?g(this.inputContainer,"active"):h(this.inputContainer,"active")),b.searching=!0,b.searchList=[],d(b.opts,function(a,d){let e=b.list[a],f=d.textContent.trim(),i=c.trim();if(f.toLowerCase().includes(i.toLowerCase())){if(b.searchList.push(e),b.hasTemplate)g(e,"match");else{let a=new RegExp(i,"i").exec(f);e.innerHTML=d.textContent.replace(a[0],"<span>"+a[0]+"</span>")}h(e,"excluded")}else g(e,"excluded"),h(e,"match")}),b.searchList.length?this.open():(b.notify("No results."),this.input.focus()),this.lastLen=this.input.value.length}},selectOption:function(a){var b=this,c=a.target,e=this.searching?this.searchList:this.list;if("keydown"===a.type&&(c=e[b.activeIdx]),"LI"===c.nodeName&&!c.classList.contains("disabled")){if(b.ajaxOpts)return void b.selectRemoteOption(c);var f=b.list.indexOf(c),i=b.opts[f],j=!1;if(b.elem.multiple){if(c.classList.contains("selected")){var k;d(b.tags,function(a,b){b.getAttribute("data-value")===i.value&&(k=b)}),k&&b.removeTag(k)}else{if(null!==b.options.maxSelections&&b.selectedVals.length>=b.options.maxSelections)return void b.notify("A maximum of "+b.options.maxSelections+" items can be selected.");b.selectedVals.push(i.value),b.createTag(i),i.selected=!0,g(c,"selected"),b.emit("selectr.select"),b.input.value=""}b.txt.children.length&&(j=!0)}else if(b.selectedIndex===f)b.txt.innerHTML="",i.selected=!1,h(c,"selected"),b.selectedVal=null,b.selectedIndex=null,b.elem.value=null,b.emit("selectr.deselect");else{let a=b.optsOptions.getElementsByClassName("selected")[0];a&&h(a,"selected"),b.txt.innerHTML=b.hasTemplate?b.options.render(i):i.textContent,i.selected=!0,g(c,"selected"),b.selectedVal=i.value,b.selectedIndex=f,b.emit("selectr.select"),j=!0}j?g(b.container,"has-selected"):h(b.container,"has-selected"),b.reset(),b.elem.multiple||b.close(),b.emit("selectr.change")}},ajaxSearch:function(){function f(e){var f=document.createDocumentFragment(),g=document.createDocumentFragment();b.searchList=[],b.opts=[],d(e,function(d,e){let h=a.formatResults(e)||e.text,j=c("li",{class:"selectr-option","data-value":e.value,"data-text":e.text||"",html:h}),k=c("option",{value:e.value});k.textContent=e.text,b.searchList.push(j),b.opts.push(k),i(f,j),i(g,k)}),b.optsOptions.innerHTML="",i(b.optsOptions,f),b.elem.innerHTML="",i(b.elem,g)}this.searching=!0,g(this.inputContainer,"loading");var a=this.options.ajax,b=this,e=new XMLHttpRequest;e.onload=function(){if(4===e.readyState&&200===e.status){var c=JSON.parse(e.responseText);if("function"!=typeof a.parseResults)return;var d=a.parseResults(c);f(d),h(b.inputContainer,"loading"),b.remoteItems=d}},e.open("GET",b.ajax_url+b.input.value,!0),e.send()},selectRemoteOption:function(a){var c,e,f,b=this,i=a.getAttribute("data-value");if(d(b.remoteItems,function(a,d){d.value==i&&(e=d,f=a,c=b.opts[a])}),this.elem.multiple)if(a.classList.contains("selected")&&b.selectedVals.indexOf(i)>-1){var j;d(b.tags,function(a,b){b.getAttribute("data-value")===i&&(j=b)}),j&&b.removeTag(j)}else b.createTag(a,e),b.selectedVals.push(i),b.opts[f].selected=!0,b.emit("selectr.select"),g(a,"selected");else if(b.selectedIndex===f)b.txt.innerHTML="",c.selected=!1,h(a,"selected"),b.selectedVal=null,b.selectedIndex=null,b.elem.value=null,b.emit("selectr.deselect");else{let d=b.optsOptions.getElementsByClassName("selected")[0];d&&h(d,"selected"),b.txt.innerHTML=this.options.ajax.formatSelected(e)||c.getAttribute("data-text")||c.textContent,c.selected=!0,g(a,"selected"),b.selectedVal=c.value,b.selectedIndex=f,b.emit("selectr.select")}this.elem.value?g(this.container,"has-selected"):h(this.container,"has-selected"),this.emit("selectr.select"),this.emit("selectr.change"),this.close()},createTag:function(a,b){var f,e=document.createDocumentFragment();f=b?this.options.ajax.formatSelected(b)||a.getAttribute("data-text")||a.textContent:this.hasTemplate?this.options.render(a):a.textContent;let j=c("li",{class:"selectr-tag",html:f}),k=c("button",{class:"selectr-tag-remove",type:"button"});i(j,k),i(e,j),i(this.txt,e),this.tags.push(j),this.txt.children.length?g(this.container,"has-selected"):h(this.container,"has-selected"),b?j.setAttribute("data-value",a.getAttribute("data-value")):j.setAttribute("data-value",a.value)},removeTags:function(a){this.disabled||(a.preventDefault(),a.stopPropagation(),this.removeTag(a.target.parentNode))},removeTag:function(a){var e,f,b=this,c=a.getAttribute("data-value");d(this.opts,function(a,d){d.value==c&&(f=a,e=b.opts[f])}),e.selected=!1,h(this.list[f],"selected"),this.selectedVals.splice(this.selectedVals.indexOf(e.value),1),this.tags.splice(this.tags.indexOf(a),1),this.txt.removeChild(a),this.tags.length||h(this.container,"has-selected"),this.emit("selectr.deselect")},toggleOptions:function(){var b=this.container.classList.contains("open");b?this.close():this.open()},clearOptions:function(){this.options.searchable&&(this.input.value=null,this.searching=!1,h(this.inputContainer,"active"),h(this.container,"notice"),g(this.container,"open"),this.input.focus()),this.reset()},reset:function(){var a=this;d(this.list,function(b,c){let d=a.opts[b];c.innerHTML=a.hasTemplate?a.options.render(d):d.textContent,h(c,"match"),h(c,"excluded")})},open:function(){var a=this,b=this.elemRect.top+this.elemRect.height+230,c=window.innerHeight;b>c?g(this.container,"inverted"):h(this.container,"inverted"),g(this.container,"open"),h(this.container,"notice"),this.options.searchable&&setTimeout(function(){a.input.focus()},10),this.optsRect=this.optsOptions.getBoundingClientRect(),this.opened=!0,this.ajaxOpts&&(this.searching=!0),this.emit("selectr.open")},close:function(){var a=this.container.classList.contains("notice");this.options.searchable&&!a&&(this.input.blur(),this.searching=!1),a&&(this.container.classList.remove("notice"),this.notice.textContent=""),h(this.container,"open"),this.opened=!1,this.emit("selectr.close")},dismiss:function(a){var b=a.target;this.container.contains(b)||!this.opened&&!this.container.classList.contains("notice")||this.close()},notify:function(a){this.close(),this.container.classList.add("notice"),this.notice.textContent=a},setValue:function(a){var b=this,c=[].slice.call(b.values).indexOf(a);if(!(c<0)){if(b.elem.multiple)b.selectedVals.indexOf(a)<0&&b.createTag(b.opts[c]);else{b.txt.innerHTML=b.hasTemplate?b.options.render(b.opts[c]):b.opts[c].textContent;let a=b.optsOptions.getElementsByClassName("selected")[0];a&&h(a,"selected")}b.selectedVals.push(a),g(b.list[c],"selected"),g(b.container,"has-selected"),b.opts[c].selected=!0,b.emit("selectr.select")}},getValue:function(){return this.elem.multiple?this.selectedVals:this.selectedVal},removeValue:function(a){if(this.tags.length){var b=this,c=[].slice.call(this.values).indexOf(a);if(!(c<0)){var e=this.list[c],f=this.opts[c];if(this.elem.multiple){var g;d(this.tags,function(b,c){c.getAttribute("data-value")===a&&(g=c)}),g&&b.removeTag(g)}else this.txt.innerHTML=this.hasTemplate?this.options.render(f):f.textContent,null!==this.elem.selectedIndex&&h(this.list[this.elem.selectedIndex],"selected");f.selected=!1,h(e,"selected"),this.emit("selectr.select")}}},setDimensions:function(){var a=this.options.width||this.elemRect.width;if("auto"===this.options.width?a="100%":a+="px",this.opened){this.elemRect=this.elem.getBoundingClientRect();var b=this.elemRect.top+this.elemRect.height+230,c=window.innerHeight;this.close(),b>c?g(this.container,"inverted"):h(this.container,"inverted")}this.container.style.cssText+="width: "+a+"; "},emit:function(a){this.elem.dispatchEvent(new Event(a))},enable:function(){this.disabled=!1,h(this.container,"disabled")},disable:function(){this.disabled=!0,g(this.container,"disabled")},destroy:function(){if(this.initialised){var a=this,b=a.container.parentNode;b.insertBefore(a.elem,a.container),b.removeChild(a.container),h(a.elem,"hidden-input"),!a.elem.multiple&&a.options.emptyOption&&a.elem.removeChild(a.elem.options[0]),a.container=null,a.selected=null,a.txt=null,a.optsContainer=null,a.optsOptions=null,a.input=null,a.clear=null,a.inputContainer=null,window.removeEventListener("resize",a.resize),document.removeEventListener("click",a.handleDismiss),document.removeEventListener("keydown",a.handleNavigate),a.initialised=!1}}},k});
String.prototype.includes||(String.prototype.includes=function(a,b){"use strict";return"number"!=typeof b&&(b=0),!(b+a.length>this.length)&&this.indexOf(a,b)!==-1}),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 k(a,c){if(null===a)throw new Error("Selectr requires an element to work.");var d={minChars:1,width:"auto",emptyOption:!0,searchable:!0,selectedIndex:null,selectedValue:null,selectedIndexes:[],selectedValues:[],containerClass:"",maxSelections:null};this.elem=a,this.elemRect=this.elem.getBoundingClientRect(),this.selectedVal=null,this.selectedVals=[],this.ajaxOpts=!1,this.tags=[],this.opts=[],this.values=[],this.list=[],this.lastLen=0,this.disabled=!1,this.opened=!1,this.searching=!1,this.searchList=[],this.activeIdx=0,this.remote=!1,this.options=b(d,c),this.customOption=this.options.hasOwnProperty("renderOption")&&"function"==typeof this.options.renderOption,this.customSelected=this.options.hasOwnProperty("renderSelection")&&"function"==typeof this.options.renderSelection,this.initialise()}var b=function(a,b){var c;for(c in b)b.hasOwnProperty(c)&&(a[c]=b[c]);return a},c=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},d=function(a,b,c){if("[object Object]"===Object.prototype.toString.call(a)){var d;for(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)},e=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)}},f=function(a,b){return a.classList?a.classList.contains(b):!!a.className.match(new RegExp("(\\s|^)"+b+"(\\s|$)"))},g=function(a,b){a.classList?a.classList.add(b):f(b)||(a.className=a.className.trim()+" "+b)},h=function(a,b){a.classList?a.classList.remove(b):f(b)&&(a.className=a.className.replace(new RegExp("(^|\\b)"+b.split(" ").join("|")+"(\\b|$)","gi")," "))},i=function(a,b){a.appendChild(b)},j=function(a,b,c,d){a.addEventListener(b,c,d||!1)};return k.prototype={initialise:function(){if(!this.initialised){var a=this;if(this.on=function(b,c){this.elem.addEventListener(b,function(b){c.call(this,b,a)})},a.options.multiple&&(a.elem.setAttribute("multiple",!0),a.elem.multiple=!0),this.options.data){var b=document.createDocumentFragment();_forEach(this.options.data,function(a,d){let e=c("option",{value:d.value});e.textContent=d.text,b.appendChild(e)}),this.elem.appendChild(b)}a.options.ajax&&"object"==typeof a.options.ajax?a.setAjaxUrl():this.setSelections(),this.build(),this.initialised=!0,setTimeout(function(){a.emit("selectr.init")},100)}},setSelections:function(){var a=this;"OPTGROUP"===this.elem.options[0].parentNode.nodeName&&(this.hasOptGroups=!0),this.elem.multiple?(this.options.emptyOption=!1,(this.options.selectedIndexes.length||this.options.selectedValues.length)&&d(this.elem.options,function(b,c){c.selected=!1,(a.options.selectedIndexes.indexOf(b)>-1||a.options.selectedValues.indexOf(c.value)>-1)&&(c.selected=!0)})):(this.options.emptyOption&&(this.emptyOpt=c("option",{value:"",selected:!0}),this.hasOptGroups?this.elem.insertBefore(this.emptyOpt,this.elem.options[0].parentNode):this.elem.insertBefore(this.emptyOpt,this.elem.options[0])),null!==this.options.selectedIndex?(this.options.emptyOption&&this.options.selectedIndex++,this.elem.value=this.elem.options[this.options.selectedIndex].value):null!==this.options.selectedValue&&(this.elem.value=this.options.selectedValue,this.selectedVal=this.options.selectedValue))},setAjaxUrl:function(){this.ajaxOpts=!0;var a=this,b=a.options.ajax;a.ajax_url=b.url,b.queryParam&&(a.ajax_url+="?",b.params&&d(b.params,function(b,c){a.ajax_url+=b+"="+c+"&"}),a.ajax_url+=b.queryParam+"="),"function"!=typeof a.options.ajax.formatSelected&&(a.options.ajax.formatSelected=function(a){return a.text})},build:function(){var a=this;this.optsFrag=document.createDocumentFragment(),g(this.elem,"hidden-input"),this.container=c("div",{id:"selectr-"+a.elem.id,class:"selectr-container "+this.options.containerClass}),this.selected=c("div",{class:"selectr-selected"}),this.txt=c(this.elem.multiple?"ul":"span",{class:"selectr-text"}),this.optsContainer=c("div",{class:"selectr-options-container"}),this.optsOptions=c("ul",{class:"selectr-options"}),this.notice=c("div",{class:"selectr-notice"}),this.elem.multiple&&(g(this.txt,"selectr-tags"),g(this.container,"multiple")),this.options.searchable&&(this.input=c("input",{class:"selectr-input"}),this.clear=c("button",{class:"selectr-clear",type:"button"}),this.inputContainer=c("div",{class:"selectr-input-container"})),a.options.ajax||(this.hasOptGroups?(g(this.optsOptions,"optgroups"),d(this.elem.children,function(b,e){if("OPTGROUP"===e.nodeName){let b=c("li",{class:"selectr-optgroup",html:e.label});i(a.optsFrag,b),e.children&&d(e.children,function(b,c){a.buildOption(b,c)})}})):d(this.elem.options,function(b,c){a.buildOption(b,c)}),g(this.list[this.activeIdx],"active")),i(this.optsOptions,this.optsFrag),i(this.selected,this.txt),i(this.container,this.selected),this.options.searchable&&(i(this.inputContainer,this.input),i(this.inputContainer,this.clear),i(this.optsContainer,this.inputContainer)),i(this.optsContainer,this.optsOptions),i(this.optsContainer,this.notice),i(this.container,this.optsContainer);var b=this.options.placeholder||this.elem.getAttribute("placeholder")||"Choose...";this.placeElem=c("div",{class:"selectr-placeholder",html:b}),i(this.selected,this.placeElem),(!this.elem.multiple&&this.elem.value.length||this.elem.multiple&&this.txt.children.length)&&(g(this.container,"has-selected"),!this.elem.multiple&&this.emptyOpt&&(this.emptyOpt.selected=!1)),this.elem.parentNode.insertBefore(this.container,this.elem),i(this.container,this.elem),this.setDimensions(),this.attachEventListeners()},buildOption:function(a,b){if(b!==this.emptyOpt&&"OPTION"===b.nodeName&&b.value){var d=this.customOption?this.options.renderOption(b):b.textContent.trim(),e=c("li",{class:"selectr-option",html:d});if(b.hasAttribute("selected")&&(b.selected=!0),i(this.optsFrag,e),b.selected&&(g(e,"selected"),this.elem.multiple?this.createTag(b):(this.txt.innerHTML=d,this.selectedIndex=a),this.selectedVals.push(b.value)),b.disabled)return void g(e,"disabled");this.opts.push(b),this.values.push(b.value),this.list.push(e)}},attachEventListeners:function(){var a=this;this.handleClickEvents=this.handleEvents.bind(this),this.handleDismiss=this.dismiss.bind(this),this.handleNavigate=this.navigate.bind(this),j(this.container,"click",this.handleClickEvents),j(this.selected,"mousedown",function(a){a.preventDefault()}),j(this.optsOptions,"mousedown",function(a){a.preventDefault()}),this.options.searchable&&(j(this.input,"keyup",this.search.bind(this)),j(this.clear,"click",this.clearOptions.bind(this))),j(document,"click",this.handleDismiss),j(document,"keydown",this.handleNavigate),this.update=e(function(){a.setDimensions()},50),j(window,"resize",this.update),j(window,"scroll",this.update)},handleEvents:function(a){a=a||window.event;var b=a.target;b!==this.txt&&b!==this.placeElem||(b=this.placeElem.parentNode),b===this.selected&&(this.disabled||this.toggleOptions()),f(b,"selectr-option")&&this.selectOption(a),f(b,"selectr-tag-remove")&&this.removeTags(a),a.preventDefault()},navigate:function(a){a=a||window.e;var b=this,c=a.keyCode;if(b.opened&&(13===c||38===c||40===c)){a.preventDefault();var e,d=this.searching?this.searchList:this.list;switch(c){case 13:return void b.selectOption(a);case 38:e="up",b.activeIdx>0&&b.activeIdx--;break;case 40:e="down",b.activeIdx<d.length-1&&b.activeIdx++}var f=d[b.activeIdx],i=f.getBoundingClientRect(),j=b.optsOptions.scrollTop,k=window.scrollY||window.pageYOffset,l=b.optsRect.top+k;if("up"===e){var m=l,n=i.top+k,o=j+(n-m);0===b.activeIdx?b.optsOptions.scrollTop=0:n-m<0&&(b.optsOptions.scrollTop=o)}else{var m=l+b.optsRect.height,p=i.top+k+i.height,o=j+p-m;0===b.activeIdx?b.optsOptions.scrollTop=0:p>m&&(b.optsOptions.scrollTop=o)}h(b.optsOptions.getElementsByClassName("active")[0],"active"),g(d[b.activeIdx],"active")}},search:function(a){var b=this,c=b.input.value,e=c.length,f=38===a.keyCode||40===a.keyCode;if(!(e<this.options.minChars&&e>=this.lastLen||f)){if(this.ajaxOpts)return void this.ajaxSearch();this.container.classList.contains("notice")||(c.length>0?g(this.inputContainer,"active"):h(this.inputContainer,"active")),b.searching=!0,b.searchList=[],d(b.opts,function(a,d){let e=b.list[a],f=d.textContent.trim(),i=c.trim();if(f.toLowerCase().includes(i.toLowerCase())){if(b.searchList.push(e),b.customOption)g(e,"match");else{let a=new RegExp(i,"i").exec(f);e.innerHTML=d.textContent.replace(a[0],"<span>"+a[0]+"</span>")}h(e,"excluded")}else g(e,"excluded"),h(e,"match")}),b.searchList.length?this.open():(b.notify("No results."),this.input.focus()),this.lastLen=this.input.value.length}},selectOption:function(a){var b=this,c=a.target,e=this.searching?this.searchList:this.list;if("keydown"===a.type&&(c=e[b.activeIdx]),"LI"===c.nodeName&&!c.classList.contains("disabled")){if(b.ajaxOpts)return void b.selectRemoteOption(c);var f=b.list.indexOf(c),i=b.opts[f],j=!1;if(b.elem.multiple){if(c.classList.contains("selected")){var k;d(b.tags,function(a,b){b.getAttribute("data-value")===i.value&&(k=b)}),k&&b.removeTag(k)}else{if(null!==b.options.maxSelections&&b.selectedVals.length>=b.options.maxSelections)return void b.notify("A maximum of "+b.options.maxSelections+" items can be selected.");b.selectedVals.push(i.value),b.createTag(i),i.selected=!0,g(c,"selected"),b.emit("selectr.select"),b.input.value=""}b.txt.children.length&&(j=!0)}else if(b.selectedIndex===f)b.txt.innerHTML="",i.selected=!1,h(c,"selected"),b.selectedVal=null,b.selectedIndex=null,b.elem.value=null,b.emit("selectr.deselect");else{let a=b.optsOptions.getElementsByClassName("selected")[0];a&&h(a,"selected"),b.txt.innerHTML=b.customSelected?b.options.renderSelection(i):i.textContent,i.selected=!0,g(c,"selected"),b.selectedVal=i.value,b.selectedIndex=f,b.emit("selectr.select"),j=!0}j?g(b.container,"has-selected"):h(b.container,"has-selected"),b.reset(),b.elem.multiple||b.close(),b.emit("selectr.change")}},ajaxSearch:function(){function f(e){var f=document.createDocumentFragment(),g=document.createDocumentFragment();b.searchList=[],b.opts=[],d(e,function(d,e){let h=a.formatResults(e)||e.text,j=c("li",{class:"selectr-option","data-value":e.value,"data-text":e.text||"",html:h}),k=c("option",{value:e.value});k.textContent=e.text,b.searchList.push(j),b.opts.push(k),i(f,j),i(g,k)}),b.optsOptions.innerHTML="",i(b.optsOptions,f),b.elem.innerHTML="",i(b.elem,g)}this.searching=!0,g(this.inputContainer,"loading");var a=this.options.ajax,b=this,e=new XMLHttpRequest;e.onload=function(){if(4===e.readyState&&200===e.status){var c=JSON.parse(e.responseText);if("function"!=typeof a.parseResults)return;var d=a.parseResults(c);f(d),h(b.inputContainer,"loading"),b.remoteItems=d}},e.open("GET",b.ajax_url+b.input.value,!0),e.send()},selectRemoteOption:function(a){var c,e,f,b=this,i=a.getAttribute("data-value");if(d(b.remoteItems,function(a,d){d.value==i&&(e=d,f=a,c=b.opts[a])}),this.elem.multiple)if(a.classList.contains("selected")&&b.selectedVals.indexOf(i)>-1){var j;d(b.tags,function(a,b){b.getAttribute("data-value")===i&&(j=b)}),j&&b.removeTag(j)}else b.createTag(a,e),b.selectedVals.push(i),b.opts[f].selected=!0,b.emit("selectr.select"),g(a,"selected");else if(b.selectedIndex===f)b.txt.innerHTML="",c.selected=!1,h(a,"selected"),b.selectedVal=null,b.selectedIndex=null,b.elem.value=null,b.emit("selectr.deselect");else{let d=b.optsOptions.getElementsByClassName("selected")[0];d&&h(d,"selected"),b.txt.innerHTML=this.options.ajax.formatSelected(e)||c.getAttribute("data-text")||c.textContent,c.selected=!0,g(a,"selected"),b.selectedVal=c.value,b.selectedIndex=f,b.emit("selectr.select")}this.elem.value?g(this.container,"has-selected"):h(this.container,"has-selected"),this.emit("selectr.select"),this.emit("selectr.change"),this.close()},createTag:function(a,b){var f,e=document.createDocumentFragment();f=b?this.options.ajax.formatSelected(b)||a.getAttribute("data-text")||a.textContent:this.customSelected?this.options.renderSelection(a):a.textContent;let j=c("li",{class:"selectr-tag",html:f}),k=c("button",{class:"selectr-tag-remove",type:"button"});i(j,k),i(e,j),i(this.txt,e),this.tags.push(j),this.txt.children.length?g(this.container,"has-selected"):h(this.container,"has-selected"),b?j.setAttribute("data-value",a.getAttribute("data-value")):j.setAttribute("data-value",a.value)},removeTags:function(a){this.disabled||(a.preventDefault(),a.stopPropagation(),this.removeTag(a.target.parentNode))},removeTag:function(a){var e,f,b=this,c=a.getAttribute("data-value");d(this.opts,function(a,d){d.value==c&&(f=a,e=b.opts[f])}),e.selected=!1,h(this.list[f],"selected"),this.selectedVals.splice(this.selectedVals.indexOf(e.value),1),this.tags.splice(this.tags.indexOf(a),1),this.txt.removeChild(a),this.tags.length||h(this.container,"has-selected"),this.emit("selectr.deselect")},toggleOptions:function(){var b=this.container.classList.contains("open");b?this.close():this.open()},clearOptions:function(){this.options.searchable&&(this.input.value=null,this.searching=!1,h(this.inputContainer,"active"),h(this.container,"notice"),g(this.container,"open"),this.input.focus()),this.reset()},reset:function(){var a=this;d(this.list,function(b,c){let d=a.opts[b];c.innerHTML=a.customOption?a.options.renderOption(d):d.textContent,h(c,"match"),h(c,"excluded")})},open:function(){var a=this,b=this.elemRect.top+this.elemRect.height+230,c=window.innerHeight;b>c?g(this.container,"inverted"):h(this.container,"inverted"),g(this.container,"open"),h(this.container,"notice"),this.options.searchable&&setTimeout(function(){a.input.focus()},10),this.optsRect=this.optsOptions.getBoundingClientRect(),this.opened=!0,this.ajaxOpts&&(this.searching=!0),this.emit("selectr.open")},close:function(){var a=this.container.classList.contains("notice");this.options.searchable&&!a&&(this.input.blur(),this.searching=!1),a&&(this.container.classList.remove("notice"),this.notice.textContent=""),h(this.container,"open"),this.opened=!1,this.emit("selectr.close")},dismiss:function(a){var b=a.target;this.container.contains(b)||!this.opened&&!this.container.classList.contains("notice")||this.close()},notify:function(a){this.close(),this.container.classList.add("notice"),this.notice.textContent=a},setValue:function(a){var b=this,c=[].slice.call(b.values).indexOf(a);if(!(c<0)){if(b.elem.multiple)b.selectedVals.indexOf(a)<0&&b.createTag(b.opts[c]);else{b.txt.innerHTML=b.customOption?b.options.renderOption(b.opts[c]):b.opts[c].textContent;let a=b.optsOptions.getElementsByClassName("selected")[0];a&&h(a,"selected")}b.selectedVals.push(a),g(b.list[c],"selected"),g(b.container,"has-selected"),b.opts[c].selected=!0,b.emit("selectr.select")}},getValue:function(){return this.elem.multiple?this.selectedVals:this.selectedVal},removeValue:function(a){if(this.tags.length){var b=this,c=[].slice.call(this.values).indexOf(a);if(!(c<0)){var e=this.list[c],f=this.opts[c];if(this.elem.multiple){var g;d(this.tags,function(b,c){c.getAttribute("data-value")===a&&(g=c)}),g&&b.removeTag(g)}else this.txt.innerHTML=this.customOption?this.options.renderOption(f):f.textContent,null!==this.elem.selectedIndex&&h(this.list[this.elem.selectedIndex],"selected");f.selected=!1,h(e,"selected"),this.emit("selectr.select")}}},setDimensions:function(){var a=this.options.width||this.elemRect.width;if("auto"===this.options.width?a="100%":a+="px",this.opened){this.elemRect=this.elem.getBoundingClientRect();var b=this.elemRect.top+this.elemRect.height+230,c=window.innerHeight;this.close(),b>c?g(this.container,"inverted"):h(this.container,"inverted")}this.container.style.cssText+="width: "+a+"; "},emit:function(a){this.elem.dispatchEvent(new Event(a))},enable:function(){this.disabled=!1,h(this.container,"disabled")},disable:function(){this.disabled=!0,g(this.container,"disabled")},destroy:function(){if(this.initialised){var a=this,b=a.container.parentNode;b.insertBefore(a.elem,a.container),b.removeChild(a.container),h(a.elem,"hidden-input"),!a.elem.multiple&&a.options.emptyOption&&a.elem.removeChild(a.elem.options[0]),a.container=null,a.selected=null,a.txt=null,a.optsContainer=null,a.optsOptions=null,a.input=null,a.clear=null,a.inputContainer=null,window.removeEventListener("resize",a.resize),document.removeEventListener("click",a.handleDismiss),document.removeEventListener("keydown",a.handleNavigate),a.initialised=!1}}},k});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc