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.4.1 to 0.5.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}),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.placeholder="",this.isMultiple=!1,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.id="ss-"+Math.floor(1e5*Math.random()),this.isMultiple=t.select.multiple}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");this.main.data.selected;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.selected;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.selected,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)}}}},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);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,s=document.createElement("div");s.classList.add(this.main.config.option);var a=this.main.data.selected;return a&&a.id===t.id&&s.classList.add(this.main.config.highlighted),s.dataset.id=t.id,s.innerHTML=t.innerHTML,s.onclick=function(t){if(e.main.beforeOnChange){var i=void 0,n=e.main.data.getObjectFromData(t.target.dataset.id);if(e.main.config.isMultiple){i=[];for(var s=e.main.data.selected,a=0;a<s.length;a++)i.push(s[a].value);i.push(n.value)}else i=n.value;!1!==e.main.beforeOnChange(i)&&e.main.set(t.target.dataset.id,"id")}else e.main.set(t.target.dataset.id,"id")},(t.disabled||this.main.data.selected&&i.i(n.c)(this.main.data.selected,"id",t.id))&&(s.onclick=null,s.classList.add(this.main.config.disabled)),s},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.selected=this.main.config.isMultiple?[]: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,disabled:t.disabled,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){if(void 0===e&&(e="id"),Array.isArray(t)){this.selected=[];for(var i=0;i<t.length;i++)this.selected.push(this.getObjectFromData(t[i],e))}else this.selected=this.getObjectFromData(t,e);this.onDataChange()},t.prototype.addToSelected=function(t,e){if(void 0===e&&(e="id"),this.main.config.isMultiple){this.selected.push(this.getObjectFromData(t,e)),this.onDataChange()}},t.prototype.removeFromSelected=function(t,e){if(void 0===e&&(e="id"),this.main.config.isMultiple){for(var i=this.selected,n=0;n<i.length;n++)String(i[n][e])===String(t)&&i.splice(n,1);this.onDataChange()}},t.prototype.onDataChange=function(){if(this.main.onChange){var t=void 0;if(this.main.config.isMultiple){t=[];for(var e=0;e<this.selected.length;e++)t.push(this.selected[e].value)}else t=this.selected.value;this.main.onChange(t)}},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.selected)if(this.main.config.isMultiple)for(var t=this.main.data.selected,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.selected;this.element.value=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){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,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)}])});
{
"name": "slim-select",
"description": "Slim vanilla javascript select dropdown",
"version": "0.4.1",
"version": "0.5.0",
"author": "Brian Voelker <brian@webiswhatido.com> (http://webiswhatido.com)",

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

interface constructor {
select: HTMLSelectElement
showSearch: boolean
placeholder: string
}

@@ -7,4 +9,5 @@

id: string = ''
placeholder: string = ''
isMultiple: boolean = false
showSearch: boolean = true
placeholder: string = 'Select Value'

@@ -31,2 +34,3 @@ // Classes

readonly disabled: string = 'ss-disabled'
readonly hide: string = 'ss-hide'

@@ -37,3 +41,5 @@ constructor (info: constructor) {

this.isMultiple = info.select.multiple
this.showSearch = (info.showSearch === false ? false : true)
if (info.placeholder) {this.placeholder = info.placeholder}
}
}

@@ -76,3 +76,2 @@ import SlimSelect from './index'

placeholder.classList.add('placeholder')
let singleSelect = <option>this.main.data.selected
container.appendChild(placeholder)

@@ -103,4 +102,13 @@

placeholder (): void {
let selected = <option>this.main.data.selected
this.singleSelected.placeholder.innerHTML = (selected ? selected.innerHTML || selected.text: '')
let selected = <option>this.main.data.getSelected()
// Placeholder display
if (selected && selected.placeholder) {
let placeholder = document.createElement('span')
placeholder.classList.add(this.main.config.disabled)
placeholder.innerHTML = this.main.config.placeholder
this.singleSelected.placeholder.innerHTML = placeholder.outerHTML
} else {
this.singleSelected.placeholder.innerHTML = (selected ? selected.innerHTML || selected.text: '')
}
}

@@ -145,6 +153,6 @@

let currentNodes = this.multiSelected.values.childNodes
let selected = <option[]>this.main.data.selected
let exists
let selected = <option[]>this.main.data.getSelected()
// Add values that dont currently exist
let exists
for (var s = 0; s < selected.length; s++) {

@@ -181,2 +189,9 @@ exists = false

// If there are no values set placeholder
if (selected.length === 0) {
let placeholder = document.createElement('span')
placeholder.classList.add(this.main.config.disabled)
placeholder.innerHTML = this.main.config.placeholder
this.multiSelected.values.innerHTML = placeholder.outerHTML
}
}

@@ -219,2 +234,3 @@ }

container.classList.add(this.main.config.search)
if (!this.main.config.showSearch) {container.style.display = 'none'}

@@ -387,9 +403,14 @@ var input = document.createElement('input')

option (data): HTMLDivElement {
// Add hidden placeholder
if (data.placeholder) {
let placeholder = document.createElement('div')
placeholder.classList.add(this.main.config.option)
placeholder.classList.add(this.main.config.hide)
return placeholder
}
var option = document.createElement('div')
option.classList.add(this.main.config.option)
let singleSelect = <option>this.main.data.selected
if (singleSelect && singleSelect.id === data.id) {
option.classList.add(this.main.config.highlighted)
}
let selected = <option>this.main.data.getSelected()

@@ -401,13 +422,10 @@ option.dataset.id = data.id

let value
let objectInfo = this.main.data.getObjectFromData((<HTMLDivElement>e.target).dataset.id)
let objectInfo = JSON.parse(JSON.stringify(this.main.data.getObjectFromData((<HTMLDivElement>e.target).dataset.id)))
objectInfo.selected = true
if (this.main.config.isMultiple) {
value = []
let selected = <option[]>this.main.data.selected
for (var i = 0; i < selected.length; i++) {
value.push(selected[i].value)
}
value.push(objectInfo.value)
value = JSON.parse(JSON.stringify(selected))
value.push(objectInfo)
} else {
value = objectInfo.value
value = JSON.parse(JSON.stringify(objectInfo))
}

@@ -424,3 +442,3 @@

if (data.disabled || (this.main.data.selected && isValueInArrayOfObjects(this.main.data.selected, 'id', data.id))) {
if (data.disabled || (selected && isValueInArrayOfObjects(selected, 'id', data.id))) {
option.onclick = null

@@ -427,0 +445,0 @@ option.classList.add(this.main.config.disabled)

@@ -19,3 +19,5 @@ import SlimSelect from './index'

innerHTML: string
selected: boolean
disabled: boolean
placeholder: string
data: object

@@ -30,3 +32,2 @@ }

filtered: dataObject[]
selected: option | option[]
contentOpen: boolean = false

@@ -38,3 +39,2 @@ constructor (info: constructor) {

this.filtered = null
this.selected = (this.main.config.isMultiple ? [] : null)

@@ -78,3 +78,5 @@ this.parseSelectData()

innerHTML: option.innerHTML,
selected: option.selected,
disabled: option.disabled,
placeholder: option.dataset.placeholder || null,
data: option.dataset

@@ -108,21 +110,84 @@ }

setSelected (value: string | string[], type = 'id'): void {
// Loop through data and set selected values
for (var i = 0; i < this.data.length; i++) {
// Deal with optgroups
if (this.data[i].hasOwnProperty('label')) {
if (this.data[i].hasOwnProperty('options')) {
let options = (<optgroup>this.data[i]).options
for (var o = 0; o < options.length; o++) {
options[o].selected = this.shouldBeSelected(options[o], value, type)
}
}
} else {
(<option>this.data[i]).selected = this.shouldBeSelected((<option>this.data[i]), value, type)
}
}
this.onDataChange()
}
// Determines whether or not passed in option should be selected based upon possible values
shouldBeSelected (option: option, value: string | string [], type = 'id'): boolean {
if (Array.isArray(value)) {
this.selected = []
for (var i = 0; i < value.length; i++) {
this.selected.push(this.getObjectFromData(value[i], type))
if (String(option[type]) === String(value[i])) {
return true
}
}
} else {
this.selected = this.getObjectFromData(value, type)
if (String(option[type]) === String(value)) {
return true
}
}
this.onDataChange()
return false
}
// If select type is multiple
// From data get option | option[] of selected values
// If single select return last selected value
getSelected (): option | option[] {
var value = null
var values = []
for (var i = 0; i < this.data.length; i++) {
// Deal with optgroups
if (this.data[i].hasOwnProperty('label')) {
if (this.data[i].hasOwnProperty('options')) {
let options = (<optgroup>this.data[i]).options
for (var o = 0; o < options.length; o++) {
if (options[o].selected) {
// If single return option
if (!this.main.config.isMultiple) {value = options[o]}
// Else push to multiple array
values.push(options[o])
}
}
}
} else {
// Push options to array
if ((<option>this.data[i]).selected) {
// If single return option
if (!this.main.config.isMultiple) {value = this.data[i]}
// Else push to multiple array
values.push(this.data[i])
}
}
}
// Either return array or object or null
return (this.main.config.isMultiple ? values : value)
}
// If select type is multiple append value and set selected
addToSelected (value: string, type = 'id') {
if (this.main.config.isMultiple) {
let selected = <option[]>this.selected
selected.push(this.getObjectFromData(value, type))
let values = []
let selected = <option[]>this.getSelected()
for (var i = 0; i < selected.length; i++) {
values.push(selected[i][type])
}
values.push(value)
this.onDataChange()
this.setSelected(values, type)
}

@@ -134,10 +199,11 @@ }

if (this.main.config.isMultiple) {
let selected = <option[]>this.selected
let values = []
let selected = <option[]>this.getSelected()
for (var i = 0; i < selected.length; i++) {
if (String(selected[i][type]) === String(value)) {
selected.splice(i, 1)
if (String(selected[i][type]) !== String(value)) {
values.push(selected[i][type])
}
}
this.onDataChange()
this.setSelected(values, type)
}

@@ -149,13 +215,3 @@ }

if (this.main.onChange) {
let value
if (this.main.config.isMultiple) {
value = []
for (var i = 0; i < (<option[]>this.selected).length; i++) {
value.push((<option>this.selected[i]).value)
}
} else {
value = (<option>this.selected).value
}
this.main.onChange(value)
this.main.onChange(JSON.parse(JSON.stringify(this.getSelected())))
}

@@ -162,0 +218,0 @@ }

@@ -9,2 +9,4 @@ import Config from './config'

select: string
showSearch: boolean
placeholder: string

@@ -28,3 +30,5 @@ // Events

this.config = new Config({
select: selectElement
select: selectElement,
showSearch: info.showSearch,
placeholder: info.placeholder
})

@@ -124,2 +128,3 @@

// Take in string value and search current options
search (value: string): void {

@@ -126,0 +131,0 @@ // Only filter data and rerender if value has changed

@@ -23,8 +23,8 @@ import SlimSelect from './index'

setValue (): void {
if (!this.main.data.selected) {return}
if (!this.main.data.getSelected()) {return}
if (this.main.config.isMultiple) {
let selected = <option[]>this.main.data.selected
// If multiple loop through options and set selected
let selected = <option[]>this.main.data.getSelected()
let options = this.element.options
let selectedValues = []
for (var o = 0; o < options.length; o++) {

@@ -40,4 +40,5 @@ let option = <HTMLOptionElement>options[o]

} else {
let selected = <option>this.main.data.selected
this.element.value = selected.value
// If single select simply set value
let selected = <option>this.main.data.getSelected()
this.element.value = (selected ? selected.value : '')
}

@@ -96,2 +97,4 @@ }

createOption (info): HTMLOptionElement {
if (info.placeholder && info.placeholder !== '') {return null}
var option = document.createElement('option')

@@ -98,0 +101,0 @@ option.value = info.value || info.text

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

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 not supported yet

Sorry, the diff of this file is not supported yet

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