multi-select-webcomponent.min.js 6.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* @honatas/multi-select-webcomponent 1.2.0 edited */
  2. function htmlEntities(str) {
  3. return String(str)
  4. .replace(/&/g, '&')
  5. .replace(/"/g, '"')
  6. .replace(/'/g, ''')
  7. .replace(/`/g, '`')
  8. .replace(/</g, '&lt;')
  9. .replace(/>/g, '&gt;')
  10. .replace(/\|/g, '&#124;');
  11. }
  12. function is_mobile(){if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)){return true}else{return false}}
  13. !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).MultiselectWebcomponent=e()}(this,(function(){"use strict";class t extends HTMLElement{constructor(){var t,e;super(),this.options=[],this.is_modile=is_mobile(),this.optgroup=this.getAttribute("optgroup")||false, /* multi-select parameter 'optgroup="true"' for use disabled as optgroup '<option value="" disabled>Локації ---</option>' */
  14. this.searchbox=document.createElement("input"),this.dropdown=document.createElement("div"),this.selected=document.createElement("div"),
  15. this.querySelectorAll("option").forEach((t=>this.options.push(t.cloneNode(!0)))),this.setValuesOnConstructor( htmlEntities( this.getAttribute("value") ) ),this.searchbox.type="text",
  16. this.searchbox.className=`msw-searchbox ${this.getAttribute("searchbox")||""}`,this.searchbox.addEventListener("keyup",(t=>this.onSearchboxKeyup(t))),
  17. this.selected.className=`msw-selected ${this.getAttribute("selected")||""}`,
  18. this.dropdown.className=`msw-dropdown ${this.getAttribute("dropdown")||""}`,this.dropdown.addEventListener("click",(()=>this.onDropdownClick())),this.innerHTML="",
  19. this.appendChild(this.selected),null===(t=this.parentNode)||void 0===t||t.insertBefore(this.dropdown,this.nextSibling),
  20. this.addEventListener("click",(()=>this.onMultiselectClick())),
  21. null===(e=this.parentElement)||void 0===e||e.addEventListener("mouseleave",(()=>this.dropdown.style.display="none")),
  22. this.searchbox.placeholder=this.getAttribute("placeholder")||"";
  23. const s=this.getAttribute("disabled");s&&"false"!==s&&(this.searchbox.disabled=!0),this.build()}
  24. set value(t){for(const t of this.options)t.selected=!1;if(t&&0!=t.length){for(const e of this.options)t.includes(e.value)&&(e.selected=!0);this.build()}}
  25. setValuesOnConstructor(t){if(!t)return;const e=t.split(",");for(const t of this.options)e.includes(t.value)&&(t.selected=!0)}
  26. get value(){const t=[];for(const e of this.options)e.selected&&t.push( htmlEntities(e.value) );return t}
  27. set disabled(t){this.setAttribute("disabled",t?"true":"false"),this.searchbox.disabled=t,this.build()}
  28. get disabled(){const t=this.getAttribute("disabled");return!(!t||"false"===t)}
  29. set placeholder(t){this.setAttribute("placeholder",t),this.searchbox.placeholder=t}
  30. get placeholder(){return this.getAttribute("placeholder")}
  31. clear(){this.options=[],this.build()}
  32. build(){
  33. this.selected.innerHTML="",this.dropdown.innerHTML="";
  34. if(!this.optgroup){/* without optgroups */ for(const t of this.options)t.selected?this.selected.appendChild(this.buildSelectedItem(t)):this.dropdown.appendChild(this.buildDropdownItem(t));
  35. }else{ /* with optgroups */
  36. for(let j=0;j<this.options.length;j++){let t = this.options[j];if(t.selected){this.selected.appendChild(this.buildSelectedItem(t))}else{
  37. let is_next=!!this.options[j+1],skip=true;
  38. if(t.disabled){if(is_next){
  39. for(let k=j+1;k<this.options.length;k++){if(this.options[k].selected)continue;if(!this.options[k].disabled){skip=false;break}break} /* break when disabled/not selected and not disabled */
  40. }else{/* disabled and no next */}}else skip=false;
  41. if(!skip)/* not disabled or not skip */ this.dropdown.appendChild(this.buildDropdownItem(t));
  42. }}}
  43. this.selected.appendChild(this.searchbox),this.dispatchEvent(new Event("change"));
  44. if(!this.dropdown.childElementCount){this.searchbox.style.display="none"}else{this.searchbox.style.display="block"}}
  45. buildSelectedItem(t){const e=document.createElement("div");return e.className=`msw-selecteditem ${this.getAttribute("selecteditem")||""}`,e.title="видалити",e.innerText=t.textContent,e.dataset.value=htmlEntities(t.value),this.disabled||e.addEventListener("click",(t=>this.onSelectedClick(t))),e}
  46. buildDropdownItem(t){var classd;classd=(t.disabled?" msw-disabled":"");const e=document.createElement("div");return e.className=`msw-dropdownitem${classd} ${this.getAttribute("dropdownitem")||this.getAttribute("item")||""}`,e.innerText=t.textContent,e.dataset.value=htmlEntities(t.value),t.disabled||e.addEventListener("click",(t=>this.onItemClick(t))),e}
  47. findOptionByValue(t){for(const e of this.options)if(t===e.value)return e}
  48. chooseOption(t){t&&(t.selected=!0),this.searchbox.value="",this.build()}
  49. onItemClick(t){this.chooseOption(this.findOptionByValue(t.currentTarget.dataset.value));if(this.is_modile)this.dropdown.style.display="none"}
  50. onSelectedClick(t){const e=this.findOptionByValue(t.currentTarget.dataset.value);e&&(e.selected=!1),this.build()}
  51. onClearClick(t){this.options.forEach((t=>t.selected=!1)),this.build(),this.searchbox.value="",this.searchbox.focus(),t.stopPropagation()}
  52. onSelectAllClick(t){this.options.forEach((t=>t.selected=!0)),this.build(),this.dropdown.style.display="none",this.searchbox.value="",this.searchbox.focus(),t.stopPropagation()}
  53. onMultiselectClick(){!0!==this.disabled&&(this.dropdown.style.display="block",this.searchbox.focus())}
  54. onDropdownClick(){this.searchbox.focus()}
  55. onSearchboxKeyup(t){if(("Enter"===t.key||"NumpadEnter"===t.key)&&""!==this.searchbox.value&&(this.dropdown.firstChild)){
  56. return this.chooseOption(this.findOptionByValue(this.dropdown.firstChild.dataset.value)),void this.searchbox.focus();}
  57. if(this.dropdown.innerHTML="",""===this.searchbox.value)
  58. for(const t of this.options)t.selected||this.dropdown.appendChild(this.buildDropdownItem(t));
  59. else{ for(const t of this.options){
  60. !t.selected&&!t.disabled&&t.textContent&&t.textContent.toLocaleUpperCase().indexOf(this.searchbox.value.toLocaleUpperCase())>=0&&this.dropdown.appendChild(this.buildDropdownItem(t));}}
  61. this.dropdown.style.display="block"}}return t}));