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

slim-select

Package Overview
Dependencies
Maintainers
1
Versions
111
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

slim-select - npm Package Compare versions

Comparing version 0.6.0 to 0.7.0

2

dist/index.js

@@ -1,1 +0,1 @@

!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports["slim-select"]=e():t["slim-select"]=e()}(this,function(){return function(t){function e(n){if(i[n])return i[n].exports;var s=i[n]={i:n,l:!1,exports:{}};return t[n].call(s.exports,s,s.exports,e),s.l=!0,s.exports}var i={};return e.m=t,e.c=i,e.i=function(t){return t},e.d=function(t,i,n){e.o(t,i)||Object.defineProperty(t,i,{configurable:!1,enumerable:!0,get:n})},e.n=function(t){var i=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(i,"a",i),i},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="/",e(e.s=7)}([function(t,e,i){"use strict";function n(t,e){function i(t,e){return e&&t&&t.classList&&t.classList.contains(e)?t:null}function n(t,e){return t&&t!==document?i(t,e)?t:n(t.parentNode,e):null}return i(t,e)||n(t,e)}function s(t,e){var i=t.scrollTop+t.offsetTop,n=i+t.clientHeight,s=e.offsetTop,a=s+e.clientHeight;s<i?t.scrollTop-=i-s:a>n&&(t.scrollTop+=a-n)}function a(t,e,i){if(!Array.isArray(t))return t[e]===i;for(var n=0;n<t.length;n++)if(t[n]&&t[n][e]&&t[n][e]===i)return!0;return!1}e.a=n,e.b=s,e.c=a},function(t,e){},function(t,e,i){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=i(3),s=i(0),a=i(6),o=i(5),r=i(4),l=function(){function t(t){var e=this;this.beforeOnChange=null,this.onChange=null,this.validate(t);var l=document.querySelector(t.select);this.config=new n.a({select:l,showSearch:t.showSearch,placeholder:t.placeholder}),this.select=new a.a({select:l,main:this}),this.data=new o.a({main:this}),this.slim=new r.a({main:this}),this.select.element.parentNode.insertBefore(this.slim.container,this.select.element.nextSibling),this.render(),document.addEventListener("click",function(t){i.i(s.a)(t.target,e.config.id)||e.close()}),t.beforeOnChange&&(this.beforeOnChange=t.beforeOnChange),t.onChange&&(this.onChange=t.onChange)}return t.prototype.validate=function(t){var e=document.querySelector(t.select);if(!e)throw new Error("Could not find select element");if("SELECT"!==e.tagName)throw new Error("Element isnt of type select")},t.prototype.set=function(t,e,i){void 0===e&&(e="value"),void 0===i&&(i=!0),this.config.isMultiple&&!Array.isArray(t)?this.data.addToSelected(t,e):this.data.setSelected(t,e),this.render(),this.select.setValue(),i&&this.close()},t.prototype.setData=function(t){i.i(o.b)(t);var e=JSON.parse(JSON.stringify(t));this.select.create(e),this.data.parseSelectData(),this.data.setSelectedFromSelect(),this.render()},t.prototype.open=function(){this.slim.search.input.focus(),this.data.contentOpen||(this.data.contentOpen=!0,this.config.isMultiple?this.slim.multiSelected.container.classList.add(this.config.open):(this.slim.singleSelected.container.classList.add(this.config.open),this.slim.singleSelected.arrowIcon.arrow.classList.remove("arrow-up"),this.slim.singleSelected.arrowIcon.arrow.classList.add("arrow-down")),this.slim.content.classList.add(this.config.open))},t.prototype.close=function(){this.data.contentOpen&&(this.data.contentOpen=!1,this.slim.search.input.blur(),this.config.isMultiple?this.slim.multiSelected.container.classList.remove(this.config.open):(this.slim.singleSelected.container.classList.remove(this.config.open),this.slim.singleSelected.arrowIcon.arrow.classList.add("arrow-up"),this.slim.singleSelected.arrowIcon.arrow.classList.remove("arrow-down")),this.slim.content.classList.remove(this.config.open),this.search(""))},t.prototype.search=function(t){this.data.searchValue!==t&&(this.slim.search.input.value=t,this.data.search(t),this.render())},t.prototype.render=function(){this.config.isMultiple?this.slim.values():this.slim.placeholder(),this.slim.options()},t.prototype.destroy=function(){this.select.element.removeAttribute("style"),this.slim.container.parentElement.removeChild(this.slim.container)},t}();e.default=l},function(t,e,i){"use strict";var n=function(){function t(t){this.id="",this.isMultiple=!1,this.showSearch=!0,this.placeholder="Select Value",this.main="ss-main",this.singleSelected="ss-single-selected",this.arrow="ss-arrow",this.multiSelected="ss-multi-selected",this.add="ss-add",this.plus="ss-plus",this.values="ss-values",this.value="ss-value",this.valueText="ss-value-text",this.valueDelete="ss-value-delete",this.content="ss-content",this.open="ss-open",this.search="ss-search",this.list="ss-list",this.optgroup="ss-optgroup",this.optgroupLabel="ss-optgroup-label",this.option="ss-option",this.highlighted="ss-highlighted",this.disabled="ss-disabled",this.hide="ss-hide",this.id="ss-"+Math.floor(1e5*Math.random()),this.isMultiple=t.select.multiple,this.showSearch=!1!==t.showSearch,t.placeholder&&(this.placeholder=t.placeholder)}return t}();e.a=n},function(t,e,i){"use strict";var n=i(0),s=function(){function t(t){this.main=t.main,this.container=this.containerDiv(),this.content=this.contentDiv(),this.search=this.searchDiv(),this.list=this.listDiv(),this.options(),this.main.config.isMultiple?(this.multiSelected=this.multiSelectedDiv(),this.container.appendChild(this.multiSelected.container),this.container.appendChild(this.content),this.content.appendChild(this.search.container),this.content.appendChild(this.list)):(this.singleSelected=this.singleSelectedDiv(),this.container.appendChild(this.singleSelected.container),this.container.appendChild(this.content),this.content.appendChild(this.search.container),this.content.appendChild(this.list))}return t.prototype.containerDiv=function(){var t=document.createElement("div");return t.classList.add(this.main.config.id),t.classList.add(this.main.config.main),t},t.prototype.singleSelectedDiv=function(){var t=this,e=document.createElement("div");e.classList.add(this.main.config.singleSelected);var i=document.createElement("span");i.classList.add("placeholder"),e.appendChild(i);var n=document.createElement("span");n.classList.add(this.main.config.arrow);var s=document.createElement("span");return s.classList.add("arrow-up"),n.appendChild(s),e.appendChild(n),e.onclick=function(){t.main.data.contentOpen?t.main.close():t.main.open()},{container:e,placeholder:i,arrowIcon:{container:n,arrow:s}}},t.prototype.placeholder=function(){var t=this.main.data.getSelected();if(t&&t.placeholder){var e=document.createElement("span");e.classList.add(this.main.config.disabled),e.innerHTML=this.main.config.placeholder,this.singleSelected.placeholder.innerHTML=e.outerHTML}else this.singleSelected.placeholder.innerHTML=t?t.innerHTML||t.text:""},t.prototype.multiSelectedDiv=function(){var t=this,e=document.createElement("div");e.classList.add(this.main.config.multiSelected);var i=document.createElement("div");i.classList.add(this.main.config.values),e.appendChild(i);var n=document.createElement("div");n.classList.add(this.main.config.add);var s=document.createElement("span");return s.classList.add(this.main.config.plus),s.innerHTML="+",n.appendChild(s),e.appendChild(n),e.onclick=function(e){e.target.classList.contains(t.main.config.valueDelete)||t.main.open()},{container:e,values:i,add:n}},t.prototype.values=function(){if(this.main.config.isMultiple){for(var t=this.multiSelected.values.childNodes,e=this.main.data.getSelected(),i=void 0,n=0;n<e.length;n++){i=!1;for(var s=0;s<t.length;s++){var a=t[s];e[n].id===a.dataset.id&&(i=!0)}i||this.multiSelected.values.appendChild(this.valueDiv(e[n]))}for(var s=0;s<t.length;s++){i=!0;for(var a=t[s],n=0;n<e.length;n++)String(e[n].id)===String(a.dataset.id)&&(i=!1);if(i){var o=t[s];o.classList.add("ss-out"),this.multiSelected.values.removeChild(o)}}if(0===e.length){var r=document.createElement("span");r.classList.add(this.main.config.disabled),r.innerHTML=this.main.config.placeholder,this.multiSelected.values.innerHTML=r.outerHTML}}},t.prototype.valueDiv=function(t){var e=this,i=document.createElement("div");i.classList.add(this.main.config.value),i.dataset.id=t.id;var n=document.createElement("span");n.classList.add(this.main.config.valueText),n.innerHTML=t.text,i.appendChild(n);var s=document.createElement("span");return s.classList.add(this.main.config.valueDelete),s.innerHTML="x",s.onclick=function(i){e.main.data.removeFromSelected(t.id,"id"),e.main.render(),e.main.select.setValue(),i.preventDefault()},i.appendChild(s),i},t.prototype.contentDiv=function(){var t=document.createElement("div");return t.classList.add(this.main.config.content),t},t.prototype.searchDiv=function(){var t=this,e=document.createElement("div");e.classList.add(this.main.config.search),this.main.config.showSearch||(e.style.display="none");var i=document.createElement("input");return i.type="search",i.placeholder="Search",i.tabIndex=0,i.onclick=function(e){setTimeout(function(){""===e.target.value&&t.main.search("")},10)},i.onkeydown=function(e){"ArrowUp"===e.key?(t.highlightUp(),e.preventDefault()):"ArrowDown"===e.key?(t.highlightDown(),e.preventDefault()):"Tab"===e.key&&t.main.close()},i.onkeyup=function(e){var i=e.target;if("Enter"===e.key){var n=t.list.querySelector("."+t.main.config.highlighted);n&&n.click()}else"ArrowUp"===e.key||"ArrowDown"===e.key||("Escape"===e.key?t.main.close():t.main.search(i.value));e.preventDefault()},i.onfocus=function(){t.main.open()},e.appendChild(i),{container:e,input:i}},t.prototype.highlightUp=function(){var t=this.list.querySelector("."+this.main.config.highlighted),e=null;if(t)e=t.previousSibling;else{var s=this.list.querySelectorAll("."+this.main.config.option+":not(."+this.main.config.disabled+")");e=s[s.length-1]}if(e&&e.classList.contains(this.main.config.optgroupLabel)&&(e=null),null===e){var a=t.parentNode;a.classList.contains(this.main.config.optgroup)&&a.previousSibling&&(e=a.previousSibling.childNodes[a.previousSibling.childNodes.length-1])}e&&(t&&t.classList.remove(this.main.config.highlighted),e.classList.add(this.main.config.highlighted),i.i(n.b)(this.list,e))},t.prototype.highlightDown=function(){var t=this.list.querySelector("."+this.main.config.highlighted),e=t?t.nextSibling:this.list.querySelector("."+this.main.config.option+":not(."+this.main.config.disabled+")");if(null===e){var s=t.parentNode;if(s.classList.contains(this.main.config.optgroup)&&s.nextSibling){e=s.nextSibling.querySelector("."+this.main.config.option+":not(."+this.main.config.disabled+")")}}e&&(t&&t.classList.remove(this.main.config.highlighted),e.classList.add(this.main.config.highlighted),i.i(n.b)(this.list,e))},t.prototype.listDiv=function(){var t=document.createElement("div");return t.classList.add(this.main.config.list),t.onmousewheel=function(e){var i=t.scrollTop,n=t.scrollHeight,s=t.offsetHeight,a="DOMMouseScroll"==e.type?-40*e.detail:e.wheelDelta,o=a>0,r=function(){return e.stopPropagation(),e.preventDefault(),e.returnValue=!1,!1};return!o&&-a>n-s-i?(t.scrollTop=n,r()):o&&a>i?(t.scrollTop=0,r()):void 0},t},t.prototype.options=function(){var t=this.main.data.filtered||this.main.data.data;this.list.innerHTML="";for(var e=0;e<t.length;e++)if(t[e].hasOwnProperty("label")){var i=t[e],n=document.createElement("div");n.classList.add(this.main.config.optgroup);var s=document.createElement("div");s.classList.add(this.main.config.optgroupLabel),s.innerHTML=i.label,n.appendChild(s);for(var a=i.options,o=0;o<a.length;o++)n.appendChild(this.option(a[o]));this.list.appendChild(n)}else this.list.appendChild(this.option(t[e]))},t.prototype.option=function(t){var e=this;if(t.placeholder){var s=document.createElement("div");return s.classList.add(this.main.config.option),s.classList.add(this.main.config.hide),s}var a=document.createElement("div");a.classList.add(this.main.config.option);var o=this.main.data.getSelected();return a.dataset.id=t.id,a.innerHTML=t.innerHTML,a.onclick=function(t){if(e.main.beforeOnChange){var i=void 0,n=JSON.parse(JSON.stringify(e.main.data.getObjectFromData(t.target.dataset.id)));n.selected=!0,e.main.config.isMultiple?(i=JSON.parse(JSON.stringify(o)),i.push(n)):i=JSON.parse(JSON.stringify(n));!1!==e.main.beforeOnChange(i)&&e.main.set(t.target.dataset.id,"id")}else e.main.set(t.target.dataset.id,"id")},(t.disabled||o&&i.i(n.c)(o,"id",t.id))&&(a.onclick=null,a.classList.add(this.main.config.disabled)),a},t}();e.a=s},function(t,e,i){"use strict";function n(t){if(!t)throw new Error("Data must be an array of objects");for(var e=0;e<t.length;e++)if(t[e].hasOwnProperty("label")){if(t[e].hasOwnProperty("options"))for(var i=t[e],n=i.options,e=0;e<n.length;e++)s(n[e])}else{var a=t[e];s(a)}}function s(t){if(!t.text)throw new Error("Data object option must have at least a text")}e.b=n;var a=function(){function t(t){this.contentOpen=!1,this.main=t.main,this.searchValue="",this.data=[],this.filtered=null,this.parseSelectData(),this.setSelectedFromSelect()}return t.prototype.parseSelectData=function(){this.data=[];for(var t=this.main.select.element,e=t.childNodes,i=0;i<e.length;i++)if("OPTGROUP"===e[i].nodeName){for(var n=e[i],s={label:n.label,options:[]},a=e[i].childNodes,o=0;o<a.length;o++)"OPTION"===a[o].nodeName&&s.options.push(this.pullOptionData(a[o]));this.data.push(s)}else"OPTION"===e[i].nodeName&&this.data.push(this.pullOptionData(e[i]))},t.prototype.pullOptionData=function(t){return{id:!!t.dataset&&t.dataset.id||String(Math.floor(1e8*Math.random())),value:t.value,text:t.text,innerHTML:t.innerHTML,selected:t.selected,disabled:t.disabled,placeholder:t.dataset.placeholder||null,data:t.dataset}},t.prototype.setSelectedFromSelect=function(){var t=this.main.select.element.options;if(this.main.config.isMultiple){for(var e=[],i=0;i<t.length;i++){var n=t[i];n.selected&&e.push(this.getObjectFromData(n.value,"value").id)}this.setSelected(e,"id")}else if(-1!==t.selectedIndex){var n=t[t.selectedIndex],s=n.value;this.setSelected(s,"value")}},t.prototype.setSelected=function(t,e){void 0===e&&(e="id");for(var i=0;i<this.data.length;i++)if(this.data[i].hasOwnProperty("label")){if(this.data[i].hasOwnProperty("options"))for(var n=this.data[i].options,s=0;s<n.length;s++)n[s].selected=this.shouldBeSelected(n[s],t,e)}else this.data[i].selected=this.shouldBeSelected(this.data[i],t,e);this.onDataChange()},t.prototype.shouldBeSelected=function(t,e,i){if(void 0===i&&(i="id"),Array.isArray(e)){for(var n=0;n<e.length;n++)if(String(t[i])===String(e[n]))return!0}else if(String(t[i])===String(e))return!0;return!1},t.prototype.getSelected=function(){for(var t=null,e=[],i=0;i<this.data.length;i++)if(this.data[i].hasOwnProperty("label")){if(this.data[i].hasOwnProperty("options"))for(var n=this.data[i].options,s=0;s<n.length;s++)n[s].selected&&(this.main.config.isMultiple||(t=n[s]),e.push(n[s]))}else this.data[i].selected&&(this.main.config.isMultiple||(t=this.data[i]),e.push(this.data[i]));return this.main.config.isMultiple?e:t},t.prototype.addToSelected=function(t,e){if(void 0===e&&(e="id"),this.main.config.isMultiple){for(var i=[],n=this.getSelected(),s=0;s<n.length;s++)i.push(n[s][e]);i.push(t),this.setSelected(i,e)}},t.prototype.removeFromSelected=function(t,e){if(void 0===e&&(e="id"),this.main.config.isMultiple){for(var i=[],n=this.getSelected(),s=0;s<n.length;s++)String(n[s][e])!==String(t)&&i.push(n[s][e]);this.setSelected(i,e)}},t.prototype.onDataChange=function(){this.main.onChange&&this.main.onChange(JSON.parse(JSON.stringify(this.getSelected())))},t.prototype.getObjectFromData=function(t,e){void 0===e&&(e="id");for(var i=0;i<this.data.length;i++){if(e in this.data[i]&&String(this.data[i][e])===String(t))return this.data[i];if(this.data[i].hasOwnProperty("options"))for(var n=this.data[i],s=0;s<n.options.length;s++)if(String(n.options[s][e])===String(t))return n.options[s]}return null},t.prototype.search=function(t){if(this.searchValue=t,""===t.trim())return void(this.filtered=null);var e=this.data.slice(0);t=t.trim().toLowerCase();var i=e.map(function(e){if(e.hasOwnProperty("options")){var i=e,n=i.options.filter(function(e){return-1!==e.text.toLowerCase().indexOf(t)});if(0!==n.length){var s=Object.assign({},i);return s.options=n,s}}if(e.hasOwnProperty("text")){if(-1!==e.text.toLowerCase().indexOf(t))return e}return null});this.filtered=i.filter(function(t){return t})},t}();e.a=a},function(t,e,i){"use strict";var n=function(){function t(t){this.element=t.select,this.main=t.main,this.addAttributes(),this.addEventListeners(),this.mutationObserver=this.addMutationObserver()}return t.prototype.setValue=function(){if(this.main.data.getSelected())if(this.main.config.isMultiple)for(var t=this.main.data.getSelected(),e=this.element.options,i=0;i<e.length;i++){var n=e[i];n.selected=!1;for(var s=0;s<t.length;s++)t[s].value===n.value&&(n.selected=!0)}else{var t=this.main.data.getSelected();this.element.value=t?t.value:""}},t.prototype.addAttributes=function(){this.element.tabIndex=-1,this.element.style.display="none"},t.prototype.addEventListeners=function(){var t=this;this.element.addEventListener("change",function(e){t.main.data.setSelectedFromSelect(),t.main.render()})},t.prototype.addMutationObserver=function(){var t=this,e=new MutationObserver(function(e){t.main.data.parseSelectData(),t.main.data.setSelectedFromSelect(),t.main.render()});return e.observe(this.element,{attributes:!0,childList:!0,characterData:!0}),e},t.prototype.create=function(t){this.element.innerHTML="";for(var e=0;e<t.length;e++)if(t[e].hasOwnProperty("options")){var i=t[e],n=document.createElement("optgroup");n.label=i.label;for(var s=0;s<i.options.length;s++)n.appendChild(this.createOption(i.options[s]));this.element.appendChild(n)}else this.element.appendChild(this.createOption(t[e]))},t.prototype.createOption=function(t){if(t.placeholder&&""!==t.placeholder)return null;var e=document.createElement("option");return e.value=t.value||t.text,e.innerHTML=t.innerHTML||t.text,t.selected&&(e.selected=t.selected),t.disabled&&(e.disabled=!0),t.data&&"object"==typeof t.data&&Object.keys(t.data).forEach(function(i){e.setAttribute("data-"+i,t.data[i])}),e},t}();e.a=n},function(t,e,i){i(2),t.exports=i(1)}])});
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports["slim-select"]=e():t["slim-select"]=e()}(this,function(){return function(t){function e(n){if(i[n])return i[n].exports;var s=i[n]={i:n,l:!1,exports:{}};return t[n].call(s.exports,s,s.exports,e),s.l=!0,s.exports}var i={};return e.m=t,e.c=i,e.i=function(t){return t},e.d=function(t,i,n){e.o(t,i)||Object.defineProperty(t,i,{configurable:!1,enumerable:!0,get:n})},e.n=function(t){var i=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(i,"a",i),i},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="/",e(e.s=7)}([function(t,e,i){"use strict";function n(t,e){function i(t,e){return e&&t&&t.classList&&t.classList.contains(e)?t:null}function n(t,e){return t&&t!==document?i(t,e)?t:n(t.parentNode,e):null}return i(t,e)||n(t,e)}function s(t,e){var i=t.scrollTop+t.offsetTop,n=i+t.clientHeight,s=e.offsetTop,a=s+e.clientHeight;s<i?t.scrollTop-=i-s:a>n&&(t.scrollTop+=a-n)}function a(t,e,i){if(!Array.isArray(t))return t[e]===i;for(var n=0;n<t.length;n++)if(t[n]&&t[n][e]&&t[n][e]===i)return!0;return!1}e.a=n,e.b=s,e.c=a},function(t,e){},function(t,e,i){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=i(3),s=i(0),a=i(6),o=i(5),r=i(4),l=function(){function t(t){var e=this;this.beforeOnChange=null,this.onChange=null,this.validate(t);var l=document.querySelector(t.select);this.config=new n.a({select:l,showSearch:t.showSearch,searchText:t.searchText,placeholderText:t.placeholder}),this.select=new a.a({select:l,main:this}),this.data=new o.a({main:this}),this.slim=new r.a({main:this}),this.select.element.parentNode.insertBefore(this.slim.container,this.select.element.nextSibling),this.render(),document.addEventListener("click",function(t){i.i(s.a)(t.target,e.config.id)||e.close()}),t.beforeOnChange&&(this.beforeOnChange=t.beforeOnChange),t.onChange&&(this.onChange=t.onChange)}return t.prototype.validate=function(t){var e=document.querySelector(t.select);if(!e)throw new Error("Could not find select element");if("SELECT"!==e.tagName)throw new Error("Element isnt of type select")},t.prototype.set=function(t,e,i){void 0===e&&(e="value"),void 0===i&&(i=!0),this.config.isMultiple&&!Array.isArray(t)?this.data.addToSelected(t,e):this.data.setSelected(t,e),this.render(),this.select.setValue(),i&&this.close()},t.prototype.setData=function(t){i.i(o.b)(t);var e=JSON.parse(JSON.stringify(t));this.select.create(e),this.data.parseSelectData(),this.data.setSelectedFromSelect(),this.render()},t.prototype.open=function(){this.slim.search.input.focus(),this.data.contentOpen||(this.data.contentOpen=!0,this.config.isMultiple?this.slim.multiSelected.container.classList.add(this.config.open):(this.slim.singleSelected.container.classList.add(this.config.open),this.slim.singleSelected.arrowIcon.arrow.classList.remove("arrow-up"),this.slim.singleSelected.arrowIcon.arrow.classList.add("arrow-down")),this.slim.content.classList.add(this.config.open))},t.prototype.close=function(){this.data.contentOpen&&(this.data.contentOpen=!1,this.slim.search.input.blur(),this.config.isMultiple?this.slim.multiSelected.container.classList.remove(this.config.open):(this.slim.singleSelected.container.classList.remove(this.config.open),this.slim.singleSelected.arrowIcon.arrow.classList.add("arrow-up"),this.slim.singleSelected.arrowIcon.arrow.classList.remove("arrow-down")),this.slim.content.classList.remove(this.config.open),this.search(""))},t.prototype.search=function(t){this.data.searchValue!==t&&(this.slim.search.input.value=t,this.data.search(t),this.render())},t.prototype.render=function(){this.config.isMultiple?this.slim.values():this.slim.placeholder(),this.slim.options()},t.prototype.destroy=function(){this.select.element.removeAttribute("style"),this.slim.container.parentElement.removeChild(this.slim.container)},t}();e.default=l},function(t,e,i){"use strict";var n=function(){function t(t){this.id="",this.isMultiple=!1,this.showSearch=!0,this.searchText="No Results",this.placeholderText="Select Value",this.main="ss-main",this.singleSelected="ss-single-selected",this.arrow="ss-arrow",this.multiSelected="ss-multi-selected",this.add="ss-add",this.plus="ss-plus",this.values="ss-values",this.value="ss-value",this.valueText="ss-value-text",this.valueDelete="ss-value-delete",this.content="ss-content",this.open="ss-open",this.search="ss-search",this.list="ss-list",this.optgroup="ss-optgroup",this.optgroupLabel="ss-optgroup-label",this.option="ss-option",this.highlighted="ss-highlighted",this.disabled="ss-disabled",this.hide="ss-hide",this.id="ss-"+Math.floor(1e5*Math.random()),this.style=t.select.style.cssText,this.isMultiple=t.select.multiple,this.showSearch=!1!==t.showSearch,t.searchText&&(this.searchText=t.searchText),t.placeholderText&&(this.placeholderText=t.placeholderText)}return t}();e.a=n},function(t,e,i){"use strict";var n=i(0),s=function(){function t(t){this.main=t.main,this.container=this.containerDiv(),this.content=this.contentDiv(),this.search=this.searchDiv(),this.list=this.listDiv(),this.options(),this.main.config.isMultiple?(this.multiSelected=this.multiSelectedDiv(),this.container.appendChild(this.multiSelected.container),this.container.appendChild(this.content),this.content.appendChild(this.search.container),this.content.appendChild(this.list)):(this.singleSelected=this.singleSelectedDiv(),this.container.appendChild(this.singleSelected.container),this.container.appendChild(this.content),this.content.appendChild(this.search.container),this.content.appendChild(this.list))}return t.prototype.containerDiv=function(){var t=document.createElement("div");return t.classList.add(this.main.config.id),t.classList.add(this.main.config.main),t.style.cssText=this.main.config.style,t},t.prototype.singleSelectedDiv=function(){var t=this,e=document.createElement("div");e.classList.add(this.main.config.singleSelected);var i=document.createElement("span");i.classList.add("placeholder"),e.appendChild(i);var n=document.createElement("span");n.classList.add(this.main.config.arrow);var s=document.createElement("span");return s.classList.add("arrow-up"),n.appendChild(s),e.appendChild(n),e.onclick=function(){t.main.data.contentOpen?t.main.close():t.main.open()},{container:e,placeholder:i,arrowIcon:{container:n,arrow:s}}},t.prototype.placeholder=function(){var t=this.main.data.getSelected();if(t&&t.placeholder){var e=document.createElement("span");e.classList.add(this.main.config.disabled),e.innerHTML=this.main.config.placeholderText,this.singleSelected.placeholder.innerHTML=e.outerHTML}else this.singleSelected.placeholder.innerHTML=t?t.innerHTML||t.text:""},t.prototype.multiSelectedDiv=function(){var t=this,e=document.createElement("div");e.classList.add(this.main.config.multiSelected);var i=document.createElement("div");i.classList.add(this.main.config.values),e.appendChild(i);var n=document.createElement("div");n.classList.add(this.main.config.add);var s=document.createElement("span");return s.classList.add(this.main.config.plus),s.innerHTML="+",n.appendChild(s),e.appendChild(n),e.onclick=function(e){e.target.classList.contains(t.main.config.valueDelete)||t.main.open()},{container:e,values:i,add:n}},t.prototype.values=function(){if(this.main.config.isMultiple){for(var t=this.multiSelected.values.childNodes,e=this.main.data.getSelected(),i=void 0,n=0;n<e.length;n++){i=!1;for(var s=0;s<t.length;s++){var a=t[s];e[n].id===a.dataset.id&&(i=!0)}i||this.multiSelected.values.appendChild(this.valueDiv(e[n]))}for(var s=0;s<t.length;s++){i=!0;for(var a=t[s],n=0;n<e.length;n++)String(e[n].id)===String(a.dataset.id)&&(i=!1);if(i){var o=t[s];o.classList.add("ss-out"),this.multiSelected.values.removeChild(o)}}if(0===e.length){var r=document.createElement("span");r.classList.add(this.main.config.disabled),r.innerHTML=this.main.config.placeholderText,this.multiSelected.values.innerHTML=r.outerHTML}}},t.prototype.valueDiv=function(t){var e=this,i=document.createElement("div");i.classList.add(this.main.config.value),i.dataset.id=t.id;var n=document.createElement("span");n.classList.add(this.main.config.valueText),n.innerHTML=t.text,i.appendChild(n);var s=document.createElement("span");return s.classList.add(this.main.config.valueDelete),s.innerHTML="x",s.onclick=function(i){e.main.data.removeFromSelected(t.id,"id"),e.main.render(),e.main.select.setValue(),i.preventDefault()},i.appendChild(s),i},t.prototype.contentDiv=function(){var t=document.createElement("div");return t.classList.add(this.main.config.content),t},t.prototype.searchDiv=function(){var t=this,e=document.createElement("div");e.classList.add(this.main.config.search),this.main.config.showSearch||(e.style.display="none");var i=document.createElement("input");return i.type="search",i.placeholder="Search",i.tabIndex=0,i.onclick=function(e){setTimeout(function(){""===e.target.value&&t.main.search("")},10)},i.onkeydown=function(e){"ArrowUp"===e.key?(t.highlightUp(),e.preventDefault()):"ArrowDown"===e.key?(t.highlightDown(),e.preventDefault()):"Tab"===e.key&&t.main.close()},i.onkeyup=function(e){var i=e.target;if("Enter"===e.key){var n=t.list.querySelector("."+t.main.config.highlighted);n&&n.click()}else"ArrowUp"===e.key||"ArrowDown"===e.key||("Escape"===e.key?t.main.close():t.main.search(i.value));e.preventDefault()},i.onfocus=function(){t.main.open()},e.appendChild(i),{container:e,input:i}},t.prototype.highlightUp=function(){var t=this.list.querySelector("."+this.main.config.highlighted),e=null;if(t)e=t.previousSibling;else{var s=this.list.querySelectorAll("."+this.main.config.option+":not(."+this.main.config.disabled+")");e=s[s.length-1]}if(e&&e.classList.contains(this.main.config.optgroupLabel)&&(e=null),null===e){var a=t.parentNode;a.classList.contains(this.main.config.optgroup)&&a.previousSibling&&(e=a.previousSibling.childNodes[a.previousSibling.childNodes.length-1])}e&&(t&&t.classList.remove(this.main.config.highlighted),e.classList.add(this.main.config.highlighted),i.i(n.b)(this.list,e))},t.prototype.highlightDown=function(){var t=this.list.querySelector("."+this.main.config.highlighted),e=t?t.nextSibling:this.list.querySelector("."+this.main.config.option+":not(."+this.main.config.disabled+")");if(null===e){var s=t.parentNode;if(s.classList.contains(this.main.config.optgroup)&&s.nextSibling){e=s.nextSibling.querySelector("."+this.main.config.option+":not(."+this.main.config.disabled+")")}}e&&(t&&t.classList.remove(this.main.config.highlighted),e.classList.add(this.main.config.highlighted),i.i(n.b)(this.list,e))},t.prototype.listDiv=function(){var t=document.createElement("div");return t.classList.add(this.main.config.list),t.onmousewheel=function(e){var i=t.scrollTop,n=t.scrollHeight,s=t.offsetHeight,a="DOMMouseScroll"==e.type?-40*e.detail:e.wheelDelta,o=a>0,r=function(){return e.stopPropagation(),e.preventDefault(),e.returnValue=!1,!1};return!o&&-a>n-s-i?(t.scrollTop=n,r()):o&&a>i?(t.scrollTop=0,r()):void 0},t},t.prototype.options=function(){var t=this.main.data.filtered||this.main.data.data;if(this.list.innerHTML="",0===t.length){var e=document.createElement("div");return e.classList.add(this.main.config.option),e.classList.add(this.main.config.disabled),e.innerHTML=this.main.config.searchText,void this.list.appendChild(e)}for(var i=0;i<t.length;i++)if(t[i].hasOwnProperty("label")){var n=t[i],s=document.createElement("div");s.classList.add(this.main.config.optgroup);var a=document.createElement("div");a.classList.add(this.main.config.optgroupLabel),a.innerHTML=n.label,s.appendChild(a);for(var o=n.options,r=0;r<o.length;r++)s.appendChild(this.option(o[r]));this.list.appendChild(s)}else this.list.appendChild(this.option(t[i]))},t.prototype.option=function(t){var e=this;if(t.placeholder){var s=document.createElement("div");return s.classList.add(this.main.config.option),s.classList.add(this.main.config.hide),s}var a=document.createElement("div");a.classList.add(this.main.config.option);var o=this.main.data.getSelected();return a.dataset.id=t.id,a.innerHTML=t.innerHTML,a.onclick=function(t){if(e.main.beforeOnChange){var i=void 0,n=JSON.parse(JSON.stringify(e.main.data.getObjectFromData(t.target.dataset.id)));n.selected=!0,e.main.config.isMultiple?(i=JSON.parse(JSON.stringify(o)),i.push(n)):i=JSON.parse(JSON.stringify(n));!1!==e.main.beforeOnChange(i)&&e.main.set(t.target.dataset.id,"id")}else e.main.set(t.target.dataset.id,"id")},(t.disabled||o&&i.i(n.c)(o,"id",t.id))&&(a.onclick=null,a.classList.add(this.main.config.disabled)),a},t}();e.a=s},function(t,e,i){"use strict";function n(t){if(!t)throw new Error("Data must be an array of objects");for(var e=0;e<t.length;e++)if(t[e].hasOwnProperty("label")){if(t[e].hasOwnProperty("options"))for(var i=t[e],n=i.options,e=0;e<n.length;e++)s(n[e])}else{var a=t[e];s(a)}}function s(t){if(!t.text)throw new Error("Data object option must have at least a text")}e.b=n;var a=function(){function t(t){this.contentOpen=!1,this.main=t.main,this.searchValue="",this.data=[],this.filtered=null,this.parseSelectData(),this.setSelectedFromSelect()}return t.prototype.parseSelectData=function(){this.data=[];for(var t=this.main.select.element,e=t.childNodes,i=0;i<e.length;i++)if("OPTGROUP"===e[i].nodeName){for(var n=e[i],s={label:n.label,options:[]},a=e[i].childNodes,o=0;o<a.length;o++)"OPTION"===a[o].nodeName&&s.options.push(this.pullOptionData(a[o]));this.data.push(s)}else"OPTION"===e[i].nodeName&&this.data.push(this.pullOptionData(e[i]))},t.prototype.pullOptionData=function(t){return{id:!!t.dataset&&t.dataset.id||String(Math.floor(1e8*Math.random())),value:t.value,text:t.text,innerHTML:t.innerHTML,selected:t.selected,disabled:t.disabled,placeholder:t.dataset.placeholder||null,data:t.dataset}},t.prototype.setSelectedFromSelect=function(){var t=this.main.select.element.options;if(this.main.config.isMultiple){for(var e=[],i=0;i<t.length;i++){var n=t[i];n.selected&&e.push(this.getObjectFromData(n.value,"value").id)}this.setSelected(e,"id")}else if(-1!==t.selectedIndex){var n=t[t.selectedIndex],s=n.value;this.setSelected(s,"value")}},t.prototype.setSelected=function(t,e){void 0===e&&(e="id");for(var i=0;i<this.data.length;i++)if(this.data[i].hasOwnProperty("label")){if(this.data[i].hasOwnProperty("options"))for(var n=this.data[i].options,s=0;s<n.length;s++)n[s].selected=this.shouldBeSelected(n[s],t,e)}else this.data[i].selected=this.shouldBeSelected(this.data[i],t,e);this.onDataChange()},t.prototype.shouldBeSelected=function(t,e,i){if(void 0===i&&(i="id"),Array.isArray(e)){for(var n=0;n<e.length;n++)if(String(t[i])===String(e[n]))return!0}else if(String(t[i])===String(e))return!0;return!1},t.prototype.getSelected=function(){for(var t=null,e=[],i=0;i<this.data.length;i++)if(this.data[i].hasOwnProperty("label")){if(this.data[i].hasOwnProperty("options"))for(var n=this.data[i].options,s=0;s<n.length;s++)n[s].selected&&(this.main.config.isMultiple||(t=n[s]),e.push(n[s]))}else this.data[i].selected&&(this.main.config.isMultiple||(t=this.data[i]),e.push(this.data[i]));return this.main.config.isMultiple?e:t},t.prototype.addToSelected=function(t,e){if(void 0===e&&(e="id"),this.main.config.isMultiple){for(var i=[],n=this.getSelected(),s=0;s<n.length;s++)i.push(n[s][e]);i.push(t),this.setSelected(i,e)}},t.prototype.removeFromSelected=function(t,e){if(void 0===e&&(e="id"),this.main.config.isMultiple){for(var i=[],n=this.getSelected(),s=0;s<n.length;s++)String(n[s][e])!==String(t)&&i.push(n[s][e]);this.setSelected(i,e)}},t.prototype.onDataChange=function(){this.main.onChange&&this.main.onChange(JSON.parse(JSON.stringify(this.getSelected())))},t.prototype.getObjectFromData=function(t,e){void 0===e&&(e="id");for(var i=0;i<this.data.length;i++){if(e in this.data[i]&&String(this.data[i][e])===String(t))return this.data[i];if(this.data[i].hasOwnProperty("options"))for(var n=this.data[i],s=0;s<n.options.length;s++)if(String(n.options[s][e])===String(t))return n.options[s]}return null},t.prototype.search=function(t){if(this.searchValue=t,""===t.trim())return void(this.filtered=null);var e=this.data.slice(0);t=t.trim().toLowerCase();var i=e.map(function(e){if(e.hasOwnProperty("options")){var i=e,n=i.options.filter(function(e){return-1!==e.text.toLowerCase().indexOf(t)});if(0!==n.length){var s=Object.assign({},i);return s.options=n,s}}if(e.hasOwnProperty("text")){if(-1!==e.text.toLowerCase().indexOf(t))return e}return null});this.filtered=i.filter(function(t){return t})},t}();e.a=a},function(t,e,i){"use strict";var n=function(){function t(t){this.element=t.select,this.main=t.main,this.addAttributes(),this.addEventListeners(),this.mutationObserver=this.addMutationObserver()}return t.prototype.setValue=function(){if(this.main.data.getSelected())if(this.main.config.isMultiple)for(var t=this.main.data.getSelected(),e=this.element.options,i=0;i<e.length;i++){var n=e[i];n.selected=!1;for(var s=0;s<t.length;s++)t[s].value===n.value&&(n.selected=!0)}else{var t=this.main.data.getSelected();this.element.value=t?t.value:""}},t.prototype.addAttributes=function(){this.element.tabIndex=-1,this.element.style.display="none"},t.prototype.addEventListeners=function(){var t=this;this.element.addEventListener("change",function(e){t.main.data.setSelectedFromSelect(),t.main.render()})},t.prototype.addMutationObserver=function(){var t=this,e=new MutationObserver(function(e){t.main.data.parseSelectData(),t.main.data.setSelectedFromSelect(),t.main.render()});return e.observe(this.element,{attributes:!0,childList:!0,characterData:!0}),e},t.prototype.create=function(t){this.element.innerHTML="";for(var e=0;e<t.length;e++)if(t[e].hasOwnProperty("options")){var i=t[e],n=document.createElement("optgroup");n.label=i.label;for(var s=0;s<i.options.length;s++)n.appendChild(this.createOption(i.options[s]));this.element.appendChild(n)}else this.element.appendChild(this.createOption(t[e]))},t.prototype.createOption=function(t){if(t.placeholder&&""!==t.placeholder)return null;var e=document.createElement("option");return e.value=t.value||t.text,e.innerHTML=t.innerHTML||t.text,t.selected&&(e.selected=t.selected),t.disabled&&(e.disabled=!0),t.data&&"object"==typeof t.data&&Object.keys(t.data).forEach(function(i){e.setAttribute("data-"+i,t.data[i])}),e},t}();e.a=n},function(t,e,i){i(2),t.exports=i(1)}])});
{
"name": "slim-select",
"description": "Slim vanilla javascript select dropdown",
"version": "0.6.0",
"version": "0.7.0",
"author": "Brian Voelker <brian@webiswhatido.com> (http://webiswhatido.com)",

@@ -6,0 +6,0 @@ "homepage": "https://slimselectjs.com",

@@ -11,6 +11,7 @@ # Slim Select

- No Dependencies
- 15kb
- 18kb - 8kb gzip
- Single Select
- Multi Select
- Search Capability
- Placeholder
- Search
- Light Css

@@ -17,0 +18,0 @@ - Clean animations

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

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