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.5 to 1.0.6-b

2

bower.json
{
"name": "mobius1-selectr",
"version": "1.0.5",
"version": "1.0.6",
"ignore": [

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

{
"name": "mobius1-selectr",
"version": "1.0.5",
"version": "1.0.6b",
"description": "A lightweight dependency-free javascript select box replacement.",

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

@@ -19,2 +19,10 @@ # Selectr

### v1.0.5
* Pagination option added for large datasets when using options.data
* Fixed bug that caused a problem with deselecting programmatically set value (needed to be clicked twice to deselect)
* Various bug fixes
### v1.0.4

@@ -21,0 +29,0 @@

/*!
* Selectr 1.0.5
* Selectr 1.0.6
* http://mobiuswebdesign.co.uk/plugins/selectr

@@ -127,3 +127,2 @@ *

// Plugin prototype

@@ -308,3 +307,3 @@ Plugin.prototype = {

var placeholder = this.options.placeholder || this.elem.getAttribute('placeholder') || 'Choose...';
this.placeElem = _newElement('div', { class: 'selectr-placeholder', html: placeholder});
this.placeElem = _newElement('div', { class: 'selectr-placeholder', html: placeholder });
_append(this.selected, this.placeElem);

@@ -316,9 +315,11 @@

if ( this.options.emptyOption && !this.elem.multiple && !this.selectedVal ) {
this.emptyOpt = true;
this.txt.innerHTML = null;
this.elem.value = '';
this.elem.options[0].selected = false;
_removeClass(this.list[0], 'selected');
_removeClass(this.container, 'has-selected');
if ( !this.ajaxOpts && this.options.emptyOption && !this.elem.multiple ) {
if ( !this.elem.options[0].hasAttribute('selected') && this.selectedVal === this.elem.options[0].value && this.options.selectedValue != this.elem.options[0].value && this.options.selectedIndex != 0 ) {
this.emptyOpt = true;
this.txt.innerHTML = null;
this.elem.value = '';
this.elem.options[0].selected = false;
_removeClass(this.list[0], 'selected');
_removeClass(this.container, 'has-selected');
}
}

@@ -360,2 +361,3 @@

this.selectedIndex = index;
this.selectedVal = option.value;
}

@@ -449,3 +451,3 @@ }

var _this = this, keyCode = event.keyCode, keyCodes = [13, 38, 33, 34, 40];
var _this = this, keyCode = event.keyCode, keyCodes = [13, 38, 40];

@@ -462,10 +464,2 @@ // Filter out the keys we don't want

return void _this.selectOption(event);
case 33:
dir = "up";
if ( _this.activeIdx > 0 ) _this.activeIdx -= 5;
break;
case 34:
dir = "down";
if ( _this.activeIdx < list.length - 1 ) _this.activeIdx += 5;
break;
case 38:

@@ -1042,4 +1036,4 @@ dir = "up", _this.activeIdx > 0 && _this.activeIdx--;

value = value.toString();
index = [].slice.call(_this.values).indexOf(value);
value = value.toString();

@@ -1091,37 +1085,2 @@ if ( index < 0 ) return;

removeValue: function(value)
{
if ( !this.tags.length ) return;
var _this = this, index = [].slice.call(this.values).indexOf(value);
if ( index < 0 ) return;
var selected = this.list[index], option = this.opts[index];
if ( this.elem.multiple ) {
var selectedTag;
forEach(this.tags, function(i, tag) {
if ( tag.getAttribute('data-value') === value ) {
selectedTag = tag;
}
});
if ( selectedTag ) {
_this.removeTag(selectedTag);
}
} else {
this.txt.innerHTML = this.customOption ? this.options.renderOption(option) : option.textContent;
if ( this.elem.selectedIndex !== null ) {
_removeClass(this.list[this.elem.selectedIndex], 'selected');
}
}
option.selected = false;
_removeClass(selected, 'selected');
this.emit('selectr.select');
},
setDimensions: function()

@@ -1133,2 +1092,4 @@ {

w = '100%';
} else if ( this.options.width.includes('%') ) {
w = this.options.width;
} else {

@@ -1135,0 +1096,0 @@ w += 'px';

/*!
* Selectr 1.0.4
* Selectr 1.0.6
* 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,pagination:0};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();this.pageIndex=this.options.pagination;var d=this.options.pagination?this.options.data.slice(0,this.options.pagination):this.options.data;_forEach(d,function(a,d){let e=c("option",{value:d.value});e.textContent=d.text,b.appendChild(e)}),this.elem.appendChild(b)}else 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)})):null!==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.options.pagination&&b>=a.options.pagination||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.options.emptyOption||this.elem.multiple||this.selectedVal||(this.emptyOpt=!0,this.txt.innerHTML=null,this.elem.value="",this.elem.options[0].selected=!1,h(this.list[0],"selected"),h(this.container,"has-selected")),this.elem.parentNode.insertBefore(this.container,this.elem),i(this.container,this.elem),this.setDimensions(),this.attachEventListeners()},buildOption:function(a,b,d){if("OPTION"===b.nodeName&&b.value){var e=this.customOption?this.options.renderOption(b):b.textContent.trim(),f=c("li",{class:"selectr-option",html:e});if(b.hasAttribute("selected")&&(b.selected=!0),i(d?d:this.optsFrag,f),b.selected&&(g(f,"selected"),this.elem.multiple?(this.createTag(b),this.selectedVals.push(b.value)):(this.txt.innerHTML=e,this.selectedIndex=a)),b.disabled)return void g(f,"disabled");this.opts.push(b),this.values.push(b.value),this.list.push(f)}},attachEventListeners:function(){var a=this;this.requiresPagination=this.options.data&&this.options.data.length>this.options.pagination,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),this.requiresPagination&&(this.paginateItems=e(function(){a.paginate()},50),j(this.optsOptions,"scroll",this.paginateItems))},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.toggleOptions(),f(b,"selectr-option")&&this.selectOption(a),f(b,"selectr-tag-remove")&&this.removeTags(a),a.preventDefault()},navigate:function(a){a=a||window.event;var b=this,c=a.keyCode,d=[13,38,33,34,40];if(b.opened&&!(d.indexOf(c)<0)){a.preventDefault();var f,e=this.searching?this.searchList:this.list;switch(c){case 13:return void b.selectOption(a);case 33:f="up",b.activeIdx>0&&(b.activeIdx-=5);break;case 34:f="down",b.activeIdx<e.length-1&&(b.activeIdx+=5);break;case 38:f="up",b.activeIdx>0&&b.activeIdx--;break;case 40:f="down",b.activeIdx<e.length-1&&b.activeIdx++}var i=e[b.activeIdx],j=i.getBoundingClientRect(),k=b.optsOptions.scrollTop,l=window.scrollY||window.pageYOffset,m=b.optsRect.top+l;if("up"===f){var n=m,o=j.top+l,p=k+(o-n);0===b.activeIdx?b.optsOptions.scrollTop=0:o-n<0&&(b.optsOptions.scrollTop=p)}else{var n=m+b.optsRect.height,q=j.top+l+j.height,p=k+q-n;0===b.activeIdx?b.optsOptions.scrollTop=0:q>n&&(b.optsOptions.scrollTop=p),console.log(b.activeIdx),b.requiresPagination&&b.paginate()}h(b.optsOptions.getElementsByClassName("active")[0],"active"),g(e[b.activeIdx],"active")}},paginate:function(){let a=this,b=this.optsOptions,d=b.scrollTop,e=b.scrollHeight,f=b.offsetHeight,g=d>=e-f;if(g&&a.pageIndex<a.options.data.length){var h=document.createDocumentFragment(),i=document.createDocumentFragment(),j=a.options.data.slice(a.pageIndex,a.options.pagination+a.pageIndex);_forEach(j,function(b,d){let e=c("option",{value:d.value,text:d.text});h.appendChild(e),a.buildOption(b,e,i)}),a.elem.appendChild(h),b.appendChild(i),a.pageIndex+=a.options.pagination,a.emit("selectr.paginate")}},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||this.emptyOpt){this.emptyOpt=!1;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"),b.activeIdx=f,j=!0}else b.txt.innerHTML="",i.selected=!1,h(c,"selected"),b.selectedVal=null,b.selectedIndex=null,b.elem.value=null,b.emit("selectr.deselect");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");this.disabled||(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 c,b=this;if(Array.isArray(a))return void(b.elem.multiple&&_forEach(a,function(a,d){d=d.toString(),c=[].slice.call(b.values).indexOf(d),c>-1&&!b.hasSelectedValue(d)&&(b.createTag(b.opts[c]),b.updateValues(c,d,!0))}));if(c=[].slice.call(b.values).indexOf(a),a=a.toString(),!(c<0))if(b.elem.multiple)b.hasSelectedValue(a)||(b.createTag(b.opts[c]),b.updateValues(c,a,!0));else{b.txt.innerHTML=b.customOption?b.options.renderOption(b.opts[c]):b.opts[c].textContent;let d=b.optsOptions.getElementsByClassName("selected")[0];d&&h(d,"selected"),b.updateValues(c,a)}},updateValues:function(a,b,c){c?this.selectedVals.push(b):this.selectedVal=b,g(this.list[a],"selected"),g(this.container,"has-selected"),this.opts[a].selected=!0},hasSelectedValue:function(a){return this.selectedVals.indexOf(a)>-1},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.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,pagination:0};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();this.pageIndex=this.options.pagination;var d=this.options.pagination?this.options.data.slice(0,this.options.pagination):this.options.data;_forEach(d,function(a,d){let e=c("option",{value:d.value});e.textContent=d.text,b.appendChild(e)}),this.elem.appendChild(b)}else 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)})):null!==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.options.pagination&&b>=a.options.pagination||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.options.emptyOption&&!this.elem.multiple&&this.selectedVal===this.elem.options[0].value&&this.options.selectedValue!=this.elem.options[0].value&&0!=this.options.selectedIndex&&!this.elem.options[0].hasAttribute('selected')&&(this.emptyOpt=!0,this.txt.innerHTML=null,this.elem.value="",this.elem.options[0].selected=!1,h(this.list[0],"selected"),h(this.container,"has-selected")),this.elem.parentNode.insertBefore(this.container,this.elem),i(this.container,this.elem),this.setDimensions(),this.attachEventListeners()},buildOption:function(a,b,d){if("OPTION"===b.nodeName&&b.value){var e=this.customOption?this.options.renderOption(b):b.textContent.trim(),f=c("li",{class:"selectr-option",html:e});if(b.hasAttribute("selected")&&(b.selected=!0),i(d?d:this.optsFrag,f),b.selected&&(g(f,"selected"),this.elem.multiple?(this.createTag(b),this.selectedVals.push(b.value)):(this.txt.innerHTML=e,this.selectedIndex=a,this.selectedVal=b.value)),b.disabled)return void g(f,"disabled");this.opts.push(b),this.values.push(b.value),this.list.push(f)}},attachEventListeners:function(){var a=this;this.requiresPagination=this.options.data&&this.options.data.length>this.options.pagination,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),this.requiresPagination&&(this.paginateItems=e(function(){a.paginate()},50),j(this.optsOptions,"scroll",this.paginateItems))},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.toggleOptions(),f(b,"selectr-option")&&this.selectOption(a),f(b,"selectr-tag-remove")&&this.removeTags(a),a.preventDefault()},navigate:function(a){a=a||window.event;var b=this,c=a.keyCode,d=[13,38,40];if(b.opened&&!(d.indexOf(c)<0)){a.preventDefault();var f,e=this.searching?this.searchList:this.list;switch(c){case 13:return void b.selectOption(a);case 38:f="up",b.activeIdx>0&&b.activeIdx--;break;case 40:f="down",b.activeIdx<e.length-1&&b.activeIdx++}var i=e[b.activeIdx],j=i.getBoundingClientRect(),k=b.optsOptions.scrollTop,l=window.scrollY||window.pageYOffset,m=b.optsRect.top+l;if("up"===f){var n=m,o=j.top+l,p=k+(o-n);0===b.activeIdx?b.optsOptions.scrollTop=0:o-n<0&&(b.optsOptions.scrollTop=p)}else{var n=m+b.optsRect.height,q=j.top+l+j.height,p=k+q-n;0===b.activeIdx?b.optsOptions.scrollTop=0:q>n&&(b.optsOptions.scrollTop=p),console.log(b.activeIdx),b.requiresPagination&&b.paginate()}h(b.optsOptions.getElementsByClassName("active")[0],"active"),g(e[b.activeIdx],"active")}},paginate:function(){let a=this,b=this.optsOptions,d=b.scrollTop,e=b.scrollHeight,f=b.offsetHeight,g=d>=e-f;if(g&&a.pageIndex<a.options.data.length){var h=document.createDocumentFragment(),i=document.createDocumentFragment(),j=a.options.data.slice(a.pageIndex,a.options.pagination+a.pageIndex);_forEach(j,function(b,d){let e=c("option",{value:d.value,text:d.text});h.appendChild(e),a.buildOption(b,e,i)}),a.elem.appendChild(h),b.appendChild(i),a.pageIndex+=a.options.pagination,a.emit("selectr.paginate")}},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||this.emptyOpt){this.emptyOpt=!1;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"),b.activeIdx=f,j=!0}else b.txt.innerHTML="",i.selected=!1,h(c,"selected"),b.selectedVal=null,b.selectedIndex=null,b.elem.value=null,b.emit("selectr.deselect");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");this.disabled||(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 c,b=this;if(Array.isArray(a))return void(b.elem.multiple&&_forEach(a,function(a,d){d=d.toString(),c=[].slice.call(b.values).indexOf(d),c>-1&&!b.hasSelectedValue(d)&&(b.createTag(b.opts[c]),b.updateValues(c,d,!0))}));if(a=a.toString(),c=[].slice.call(b.values).indexOf(a),!(c<0))if(b.elem.multiple)b.hasSelectedValue(a)||(b.createTag(b.opts[c]),b.updateValues(c,a,!0));else{b.txt.innerHTML=b.customOption?b.options.renderOption(b.opts[c]):b.opts[c].textContent;let d=b.optsOptions.getElementsByClassName("selected")[0];d&&h(d,"selected"),b.updateValues(c,a)}},updateValues:function(a,b,c){c?this.selectedVals.push(b):this.selectedVal=b,g(this.list[a],"selected"),g(this.container,"has-selected"),this.opts[a].selected=!0},hasSelectedValue:function(a){return this.selectedVals.indexOf(a)>-1},getValue:function(){return this.elem.multiple?this.selectedVals:this.selectedVal},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.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