From 89bb2f8e70510fd76958ad132142cef7ca6afc0d Mon Sep 17 00:00:00 2001 From: Valentin Tsanev Date: Tue, 25 Mar 2025 10:48:36 +0200 Subject: [PATCH 01/13] Fix session state set, add prepend and multiple options --- resources/dist/filament-select-tree.js | 2 +- resources/js/index.js | 6 +++++- resources/views/select-tree.blade.php | 4 ++-- src/SelectTree.php | 26 +++++++++++++++++++++++--- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/resources/dist/filament-select-tree.js b/resources/dist/filament-select-tree.js index c19316c..8028255 100644 --- a/resources/dist/filament-select-tree.js +++ b/resources/dist/filament-select-tree.js @@ -1 +1 @@ -var di=Object.defineProperty,ui=(t,e,s)=>e in t?di(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s,c=(t,e,s)=>(ui(t,typeof e!="symbol"?e+"":e,s),s),He=(t,e,s)=>{if(!e.has(t))throw TypeError("Cannot "+s)},l=(t,e,s)=>(He(t,e,"read from private field"),s?s.call(t):e.get(t)),h=(t,e,s)=>{if(e.has(t))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(t):e.set(t,s)},m=(t,e,s,i)=>(He(t,e,"write to private field"),i?i.call(t,s):e.set(t,s),s),a=(t,e,s)=>(He(t,e,"access private method"),s),Ve={arrowUp:'',arrowDown:'',arrowRight:'',attention:'',clear:'',cross:'',check:'',partialCheck:''},O=(t,e)=>{if(e.innerHTML="",typeof t=="string")e.innerHTML=t;else{let s=t.cloneNode(!0);e.appendChild(s)}},Ge=t=>{let e=t?{...t}:{};return Object.keys(Ve).forEach(s=>{e[s]||(e[s]=Ve[s])}),e},pi=t=>t.reduce((e,{name:s},i)=>(e+=s,il(this,L).focus(),0)}blur(){this.isOpened&&a(this,A,H).call(this),this.clearSearch(),l(this,L).blur()}updateValue(e){this.value=e,a(this,G,F).call(this),a(this,M,Y).call(this)}removeItem(e){this.value=this.value.filter(s=>s.id!==e),a(this,Q,qt).call(this),a(this,G,F).call(this),a(this,M,Y).call(this)}clear(){this.value=[],a(this,Q,qt).call(this),a(this,G,F).call(this),this.clearSearch()}openClose(){a(this,A,H).call(this)}clearSearch(){this.searchText="",this.searchCallback(""),a(this,M,Y).call(this)}};N=new WeakMap,L=new WeakMap,D=new WeakMap,S=new WeakMap,St=new WeakSet,Hs=function(){a(this,G,F).call(this),a(this,M,Y).call(this),a(this,Lt,Me).call(this)},G=new WeakSet,F=function(){if(l(this,N).innerHTML="",this.showTags){l(this,N).append(...a(this,Tt,$e).call(this));let t=pi(this.value);this.nameChangeCallback(t)}else{let t=a(this,Bt,Ue).call(this);l(this,N).appendChild(t),this.nameChangeCallback(t.innerText)}l(this,N).appendChild(l(this,L))},Lt=new WeakSet,Me=function(){let t=[];l(this,D).innerHTML="",this.clearable&&t.push(a(this,Mt,Ye).call(this)),this.isAlwaysOpened||t.push(a(this,Dt,Ke).call(this,this.isOpened)),t.length&&l(this,D).append(...t)},yt=new WeakSet,Ie=function(){if(!this.isAlwaysOpened&&l(this,S)){let t=this.isOpened?this.iconElements.arrowUp:this.iconElements.arrowDown;O(t,l(this,S))}},M=new WeakSet,Y=function(){var t;(t=this.value)!=null&&t.length?(l(this,L).removeAttribute("placeholder"),this.srcElement.classList.remove("treeselect-input--value-not-selected")):(l(this,L).setAttribute("placeholder",this.placeholder),this.srcElement.classList.add("treeselect-input--value-not-selected")),this.searchable?this.srcElement.classList.remove("treeselect-input--unsearchable"):this.srcElement.classList.add("treeselect-input--unsearchable"),this.isSingleSelect?this.srcElement.classList.add("treeselect-input--is-single-select"):this.srcElement.classList.remove("treeselect-input--is-single-select"),l(this,L).value=this.searchText},A=new WeakSet,H=function(){this.isOpened=!this.isOpened,a(this,yt,Ie).call(this),this.isOpened?this.openCallback():this.closeCallback()},xt=new WeakSet,Ps=function(t,e,s){let i=document.createElement("div");return i.classList.add("treeselect-input"),i.setAttribute("tabindex","-1"),i.addEventListener("mousedown",n=>a(this,_t,De).call(this,n)),i.addEventListener("focus",()=>this.focusCallback(),!0),i.addEventListener("blur",()=>this.blurCallback(),!0),t.appendChild(e),i.append(t,s),i},_t=new WeakSet,De=function(t){t.stopPropagation(),this.isOpened||a(this,A,H).call(this),this.focus()},Wt=new WeakSet,Vs=function(){let t=document.createElement("div");return t.classList.add("treeselect-input__tags"),t},Tt=new WeakSet,$e=function(){return this.value.map(t=>{let e=document.createElement("div");e.classList.add("treeselect-input__tags-element"),e.setAttribute("tabindex","-1"),e.setAttribute("tag-id",t.id.toString()),e.setAttribute("title",t.name);let s=a(this,Nt,je).call(this,t.name),i=a(this,At,Re).call(this);return e.addEventListener("mousedown",n=>a(this,Ot,qe).call(this,n,t.id)),e.append(s,i),e})},Ot=new WeakSet,qe=function(t,e){t.preventDefault(),t.stopPropagation(),this.removeItem(e),this.focus()},Nt=new WeakSet,je=function(t){let e=document.createElement("span");return e.classList.add("treeselect-input__tags-name"),e.textContent=t,e},At=new WeakSet,Re=function(){let t=document.createElement("span");return t.classList.add("treeselect-input__tags-cross"),O(this.iconElements.cross,t),t},Bt=new WeakSet,Ue=function(){let t=document.createElement("span");if(t.classList.add("treeselect-input__tags-count"),!this.value.length)return t.textContent="",t.setAttribute("title",""),t;let e=this.value.length===1?this.value[0].name:`${this.value.length} ${this.tagsCountText}`;return t.textContent=e,t.setAttribute("title",e),t},Ht=new WeakSet,Gs=function(){let t=document.createElement("input");return t.classList.add("treeselect-input__edit"),this.id&&t.setAttribute("id",this.id),(!this.searchable||this.disabled)&&t.setAttribute("readonly","readonly"),this.disabled&&t.setAttribute("tabindex","-1"),this.ariaLabel.length&&t.setAttribute("aria-label",this.ariaLabel),t.addEventListener("keydown",e=>a(this,Pt,ze).call(this,e)),t.addEventListener("input",e=>a(this,Vt,Fe).call(this,e,t)),t},Pt=new WeakSet,ze=function(t){t.stopPropagation();let e=t.key;e==="Backspace"&&!this.searchText.length&&this.value.length&&!this.showTags&&this.clear(),e==="Backspace"&&!this.searchText.length&&this.value.length&&this.removeItem(this.value[this.value.length-1].id),t.code==="Space"&&(!this.searchText||!this.searchable)&&a(this,A,H).call(this),(e==="Enter"||e==="ArrowDown"||e==="ArrowUp")&&t.preventDefault(),this.keydownCallback(t),e!=="Tab"&&this.focus()},Vt=new WeakSet,Fe=function(t,e){t.stopPropagation();let s=this.searchText,i=e.value.trim();if(s.length===0&&i.length===0){e.value="";return}if(this.searchable){let n=t.target.value;this.searchCallback(n),this.isOpened||a(this,A,H).call(this)}else e.value="";this.searchText=e.value},Gt=new WeakSet,Ms=function(){let t=document.createElement("div");return t.classList.add("treeselect-input__operators"),t},Mt=new WeakSet,Ye=function(){let t=document.createElement("span");return t.classList.add("treeselect-input__clear"),t.setAttribute("tabindex","-1"),O(this.iconElements.clear,t),t.addEventListener("mousedown",e=>a(this,It,Je).call(this,e)),t},It=new WeakSet,Je=function(t){t.preventDefault(),t.stopPropagation(),(this.searchText.length||this.value.length)&&this.clear(),this.focus()},Dt=new WeakSet,Ke=function(t){m(this,S,document.createElement("span")),l(this,S).classList.add("treeselect-input__arrow");let e=t?this.iconElements.arrowUp:this.iconElements.arrowDown;return O(e,l(this,S)),l(this,S).addEventListener("mousedown",s=>a(this,$t,Xe).call(this,s)),l(this,S)},$t=new WeakSet,Xe=function(t){t.stopPropagation(),t.preventDefault(),this.focus(),a(this,A,H).call(this)},Q=new WeakSet,qt=function(){this.inputCallback(this.value)};var Is=(t,e,s,i)=>{vi(e);let n=e.filter(o=>!o.disabled&&t.some(r=>r===o.id));if(s&&n.length){n[0].checked=!0;return}n.forEach(o=>{o.checked=!0;let r=Pe(o,e,i);o.checked=r})},Pe=({id:t,checked:e},s,i)=>{let n=s.find(r=>r.id===t);if(!n)return!1;if(i)return n.checked=n.disabled?!1:!!e,n.checked;let o=Ds(!!e,n,s);return $s(n,s),o},Ds=(t,e,s)=>{if(!e.isGroup)return e.checked=e.disabled?!1:!!t,e.isPartialChecked=!1,e.checked;let i=s.filter(n=>n.childOf===e.id);return!t||e.disabled||e.isPartialChecked?(e.checked=!1,e.isPartialChecked=!1,Rt(e,i,s),e.checked):qs(i,s)?js(i)?(e.checked=!1,e.isPartialChecked=!1,e.disabled=!0,e.checked):(e.checked=!1,e.isPartialChecked=!0,i.forEach(n=>{Ds(t,n,s)}),e.checked):(e.checked=!0,e.isPartialChecked=!1,Rt(e,i,s),e.checked)},$s=(t,e)=>{let s=e.find(i=>i.id===t.childOf);s&&(mi(s,e),$s(s,e))},mi=(t,e)=>{let s=kt(t,e);if(js(s)){t.checked=!1,t.isPartialChecked=!1,t.disabled=!0;return}if(ki(s)){t.checked=!0,t.isPartialChecked=!1;return}if(wi(s)){t.checked=!1,t.isPartialChecked=!0;return}t.checked=!1,t.isPartialChecked=!1},Rt=({checked:t,disabled:e},s,i)=>{s.forEach(n=>{n.disabled=!!e||!!n.disabled,n.checked=!!t&&!n.disabled,n.isPartialChecked=!1;let o=kt(n,i);Rt({checked:t,disabled:e},o,i)})},qs=(t,e)=>t.some(s=>s.disabled)?!0:t.some(s=>{if(s.isGroup){let i=kt(s,e);return qs(i,e)}return!1}),js=t=>t.every(e=>!!e.disabled),ki=t=>t.every(e=>!!e.checked),wi=t=>t.some(e=>!!e.checked||!!e.isPartialChecked),vi=t=>{t.forEach(e=>{e.checked=!1,e.isPartialChecked=!1})},fi=(t,e,s)=>{let i={level:0,groupId:""},n=Rs(t,e,i.groupId,i.level);return gi(n,s)},Rs=(t,e,s,i)=>t.reduce((n,o)=>{var r;let u=!!((r=o.children)!=null&&r.length),v=i>=e&&u,k=i>e;if(n.push({id:o.value,name:o.name,childOf:s,isGroup:u,checked:!1,isPartialChecked:!1,level:i,isClosed:v,hidden:k,disabled:o.disabled??!1}),u){let w=Rs(o.children,e,o.value,i+1);n.push(...w)}return n},[]),kt=({id:t},e)=>e.filter(s=>s.childOf===t),bi=t=>{let{ungroupedNodes:e,allGroupedNodes:s,allNodes:i}=t.reduce((o,r)=>(r.checked&&(o.allNodes.push(r),r.isGroup?o.allGroupedNodes.push(r):o.ungroupedNodes.push(r)),o),{ungroupedNodes:[],allGroupedNodes:[],allNodes:[]}),n=i.filter(o=>!s.some(({id:r})=>r===o.childOf));return{ungroupedNodes:e,groupedNodes:n,allNodes:i}},gi=(t,e)=>(t.filter(s=>!!s.disabled).forEach(({id:s})=>Pe({id:s,checked:!1},t,e)),t),wt=(t,{id:e,isClosed:s})=>{kt({id:e},t).forEach(i=>{i.hidden=s??!1,i.isGroup&&!i.isClosed&&wt(t,{id:i.id,isClosed:s})})},Ci=t=>{t.filter(e=>e.isGroup&&!e.disabled&&(e.checked||e.isPartialChecked)).forEach(e=>{e.isClosed=!1,wt(t,e)})},Ei=(t,e)=>{let s=Si(t,e);t.forEach(i=>{s.some(({id:n})=>n===i.id)?(i.isGroup&&(i.isClosed=!1,wt(t,i)),i.hidden=!1):i.hidden=!0})},Si=(t,e)=>t.reduce((s,i)=>{if(i.name.toLowerCase().includes(e.toLowerCase())){if(s.push(i),i.isGroup){let n=Us(i.id,t);s.push(...n)}if(i.childOf){let n=zs(i.childOf,t);s.push(...n)}}return s},[]),Us=(t,e)=>e.reduce((s,i)=>(i.childOf===t&&(s.push(i),i.isGroup&&s.push(...Us(i.id,e))),s),[]),zs=(t,e)=>e.reduce((s,i)=>(i.id===t&&(s.push(i),i.childOf&&s.push(...zs(i.childOf,e))),s),[]),Li=t=>{let{duplications:e}=t.reduce((s,i)=>(s.allItems.some(n=>n.toString()===i.id.toString())&&s.duplications.push(i.id),s.allItems.push(i.id),s),{duplications:[],allItems:[]});e.length&&console.error(`Validation: You have duplicated values: ${e.join(", ")}! You should use unique values.`)},yi=(t,e,s,i,n,o,r,u,v,k)=>{Is(t,e,n,v),u&&r&&Ci(e),ot(e,s,i,o,k)},ot=(t,e,s,i,n)=>{t.forEach(o=>{let r=e.querySelector(`[input-id="${o.id}"]`),u=W(r);r.checked=o.checked,xi(o,u,i),_i(o,u),Wi(o,u),Ti(o,u,s),Oi(o,u),Ai(o,u,t,n),Ni(o,r,s)}),Bi(t,e)},xi=(t,e,s)=>{t.checked?e.classList.add("treeselect-list__item--checked"):e.classList.remove("treeselect-list__item--checked"),Array.isArray(s)&&s[0]===t.id&&!t.disabled?e.classList.add("treeselect-list__item--single-selected"):e.classList.remove("treeselect-list__item--single-selected")},_i=(t,e)=>{t.isPartialChecked?e.classList.add("treeselect-list__item--partial-checked"):e.classList.remove("treeselect-list__item--partial-checked")},Wi=(t,e)=>{t.disabled?e.classList.add("treeselect-list__item--disabled"):e.classList.remove("treeselect-list__item--disabled")},Ti=(t,e,s)=>{if(t.isGroup){let i=e.querySelector(".treeselect-list__item-icon"),n=t.isClosed?s.arrowRight:s.arrowDown;O(n,i),t.isClosed?e.classList.add("treeselect-list__item--closed"):e.classList.remove("treeselect-list__item--closed")}},Oi=(t,e)=>{t.hidden?e.classList.add("treeselect-list__item--hidden"):e.classList.remove("treeselect-list__item--hidden")},Ni=(t,e,s)=>{let i=e.parentNode.querySelector(".treeselect-list__item-checkbox-icon");t.checked?O(s.check,i):t.isPartialChecked?O(s.partialCheck,i):i.innerHTML=""},Ai=(t,e,s,i)=>{let n=t.level===0,o=20,r=5;if(n){let u=s.some(w=>w.isGroup&&w.level===t.level),v=!t.isGroup&&u?`${o}px`:`${r}px`,k=t.isGroup?"0":v;i?e.style.paddingRight=k:e.style.paddingLeft=k}else{let u=t.isGroup?`${t.level*o}px`:`${t.level*o+o}px`;i?e.style.paddingRight=u:e.style.paddingLeft=u}e.setAttribute("level",t.level.toString()),e.setAttribute("group",t.isGroup.toString())},Bi=(t,e)=>{let s=t.some(n=>!n.hidden),i=e.querySelector(".treeselect-list__empty");s?i.classList.add("treeselect-list__empty--hidden"):i.classList.remove("treeselect-list__empty--hidden")},W=t=>t.parentNode.parentNode,Qe=(t,e)=>e.find(s=>s.id.toString()===t),Hi=t=>W(t).querySelector(".treeselect-list__item-icon"),Pi=(t,e)=>{e&&Object.keys(e).forEach(s=>{let i=e[s];typeof i=="string"&&t.setAttribute(s,i)})},I,P,T,Z,Ut,Fs,zt,Ys,Ft,Js,Yt,Ze,Jt,ts,Kt,es,tt,vt,Xt,ss,Qt,is,Zt,ls,et,ft,te,ns,ee,as,se,hs,ie,cs,le,os,ne,rs,ae,ds,he,us,ce,ps,oe,ms,re,ks,st,bt,rt,de,ue,ws,pe=class{constructor({options:e,value:s,openLevel:i,listSlotHtmlComponent:n,emptyText:o,isSingleSelect:r,iconElements:u,showCount:v,disabledBranchNode:k,expandSelected:w,isIndependentNodes:f,rtl:b,inputCallback:g,arrowClickCallback:y,mouseupCallback:C}){h(this,Ut),h(this,zt),h(this,Ft),h(this,Yt),h(this,Jt),h(this,Kt),h(this,tt),h(this,Xt),h(this,Qt),h(this,Zt),h(this,et),h(this,te),h(this,ee),h(this,se),h(this,ie),h(this,le),h(this,ne),h(this,ae),h(this,he),h(this,ce),h(this,oe),h(this,re),h(this,st),h(this,rt),h(this,ue),c(this,"options"),c(this,"value"),c(this,"openLevel"),c(this,"listSlotHtmlComponent"),c(this,"emptyText"),c(this,"isSingleSelect"),c(this,"showCount"),c(this,"disabledBranchNode"),c(this,"expandSelected"),c(this,"isIndependentNodes"),c(this,"rtl"),c(this,"iconElements"),c(this,"searchText"),c(this,"flattedOptions"),c(this,"flattedOptionsBeforeSearch"),c(this,"selectedNodes"),c(this,"srcElement"),c(this,"inputCallback"),c(this,"arrowClickCallback"),c(this,"mouseupCallback"),h(this,I,null),h(this,P,!0),h(this,T,[]),h(this,Z,!0),this.options=e,this.value=s,this.openLevel=i??0,this.listSlotHtmlComponent=n??null,this.emptyText=o??"No results found...",this.isSingleSelect=r??!1,this.showCount=v??!1,this.disabledBranchNode=k??!1,this.expandSelected=w??!1,this.isIndependentNodes=f??!1,this.rtl=b??!1,this.iconElements=u,this.searchText="",this.flattedOptions=fi(this.options,this.openLevel,this.isIndependentNodes),this.flattedOptionsBeforeSearch=this.flattedOptions,this.selectedNodes={nodes:[],groupedNodes:[],allNodes:[]},this.srcElement=a(this,Ft,Js).call(this),this.inputCallback=g,this.arrowClickCallback=y,this.mouseupCallback=C,Li(this.flattedOptions)}updateValue(e){this.value=e,m(this,T,this.isSingleSelect?this.value:[]),yi(e,this.flattedOptions,this.srcElement,this.iconElements,this.isSingleSelect,l(this,T),this.expandSelected,l(this,Z),this.isIndependentNodes,this.rtl),m(this,Z,!1),a(this,rt,de).call(this)}updateSearchValue(e){if(e===this.searchText)return;let s=this.searchText===""&&e!=="";this.searchText=e,s&&(this.flattedOptionsBeforeSearch=JSON.parse(JSON.stringify(this.flattedOptions))),this.searchText===""&&(this.flattedOptions=this.flattedOptionsBeforeSearch.map(i=>{let n=this.flattedOptions.find(o=>o.id===i.id);return n.isClosed=i.isClosed,n.hidden=i.hidden,n}),this.flattedOptionsBeforeSearch=[]),this.searchText&&Ei(this.flattedOptions,e),ot(this.flattedOptions,this.srcElement,this.iconElements,l(this,T),this.rtl),this.focusFirstListElement()}callKeyAction(e){m(this,P,!1);let s=this.srcElement.querySelector(".treeselect-list__item--focused");if(s?.classList.contains("treeselect-list__item--hidden"))return;let i=e.key;i==="Enter"&&s&&s.dispatchEvent(new Event("mousedown")),(i==="ArrowLeft"||i==="ArrowRight")&&a(this,Ut,Fs).call(this,s,e),(i==="ArrowDown"||i==="ArrowUp")&&a(this,zt,Ys).call(this,s,i)}focusFirstListElement(){let e="treeselect-list__item--focused",s=this.srcElement.querySelector(`.${e}`),i=Array.from(this.srcElement.querySelectorAll(".treeselect-list__item-checkbox")).filter(n=>window.getComputedStyle(W(n)).display!=="none");i.length&&(s&&s.classList.remove(e),W(i[0]).classList.add(e))}isLastFocusedElementExist(){return!!l(this,I)}};I=new WeakMap,P=new WeakMap,T=new WeakMap,Z=new WeakMap,Ut=new WeakSet,Fs=function(t,e){if(!t)return;let s=e.key,i=t.querySelector(".treeselect-list__item-checkbox").getAttribute("input-id"),n=Qe(i,this.flattedOptions),o=t.querySelector(".treeselect-list__item-icon");s==="ArrowLeft"&&!n.isClosed&&n.isGroup&&(o.dispatchEvent(new Event("mousedown")),e.preventDefault()),s==="ArrowRight"&&n.isClosed&&n.isGroup&&(o.dispatchEvent(new Event("mousedown")),e.preventDefault())},zt=new WeakSet,Ys=function(t,e){var s;let i=Array.from(this.srcElement.querySelectorAll(".treeselect-list__item-checkbox")).filter(n=>window.getComputedStyle(W(n)).display!=="none");if(i.length)if(!t)W(i[0]).classList.add("treeselect-list__item--focused");else{let n=i.findIndex(g=>W(g).classList.contains("treeselect-list__item--focused"));W(i[n]).classList.remove("treeselect-list__item--focused");let o=e==="ArrowDown"?n+1:n-1,r=e==="ArrowDown"?0:i.length-1,u=i[o]??i[r],v=!i[o],k=W(u);k.classList.add("treeselect-list__item--focused");let w=this.srcElement.getBoundingClientRect(),f=k.getBoundingClientRect();if(v&&e==="ArrowDown"){this.srcElement.scroll(0,0);return}if(v&&e==="ArrowUp"){this.srcElement.scroll(0,this.srcElement.scrollHeight);return}let b=((s=this.listSlotHtmlComponent)==null?void 0:s.clientHeight)??0;if(w.y+w.heightf.y){this.srcElement.scroll(0,this.srcElement.scrollTop-f.height);return}}},Ft=new WeakSet,Js=function(){let t=a(this,Yt,Ze).call(this),e=a(this,tt,vt).call(this,this.options);t.append(...e);let s=a(this,Qt,is).call(this);t.append(s);let i=a(this,Xt,ss).call(this);return i&&t.append(i),t},Yt=new WeakSet,Ze=function(){let t=document.createElement("div");return t.classList.add("treeselect-list"),this.isSingleSelect&&t.classList.add("treeselect-list--single-select"),this.disabledBranchNode&&t.classList.add("treeselect-list--disabled-branch-node"),t.addEventListener("mouseout",e=>a(this,Jt,ts).call(this,e)),t.addEventListener("mousemove",()=>a(this,Kt,es).call(this)),t.addEventListener("mouseup",()=>this.mouseupCallback(),!0),t},Jt=new WeakSet,ts=function(t){t.stopPropagation(),l(this,I)&&l(this,P)&&l(this,I).classList.add("treeselect-list__item--focused")},Kt=new WeakSet,es=function(){m(this,P,!0)},tt=new WeakSet,vt=function(t){return t.reduce((e,s)=>{var i;if((i=s.children)!=null&&i.length){let o=a(this,Zt,ls).call(this,s),r=a(this,tt,vt).call(this,s.children);return o.append(...r),e.push(o),e}let n=a(this,et,ft).call(this,s,!1);return e.push(n),e},[])},Xt=new WeakSet,ss=function(){if(!this.listSlotHtmlComponent)return null;let t=document.createElement("div");return t.classList.add("treeselect-list__slot"),t.appendChild(this.listSlotHtmlComponent),t},Qt=new WeakSet,is=function(){let t=document.createElement("div");t.classList.add("treeselect-list__empty"),t.setAttribute("title",this.emptyText);let e=document.createElement("span");e.classList.add("treeselect-list__empty-icon"),O(this.iconElements.attention,e);let s=document.createElement("span");return s.classList.add("treeselect-list__empty-text"),s.textContent=this.emptyText,t.append(e,s),t},Zt=new WeakSet,ls=function(t){let e=document.createElement("div");e.setAttribute("group-container-id",t.value.toString()),e.classList.add("treeselect-list__group-container");let s=a(this,et,ft).call(this,t,!0);return e.appendChild(s),e},et=new WeakSet,ft=function(t,e){let s=a(this,te,ns).call(this,t);if(e){let o=a(this,le,os).call(this);s.appendChild(o),s.classList.add("treeselect-list__item--group")}let i=a(this,ae,ds).call(this,t),n=a(this,he,us).call(this,t,e);return s.append(i,n),s},te=new WeakSet,ns=function(t){let e=document.createElement("div");return Pi(e,t.htmlAttr),e.setAttribute("tabindex","-1"),e.setAttribute("title",t.name),e.classList.add("treeselect-list__item"),e.addEventListener("mouseover",()=>a(this,ee,as).call(this,e),!0),e.addEventListener("mouseout",()=>a(this,se,hs).call(this,e),!0),e.addEventListener("mousedown",s=>a(this,ie,cs).call(this,s,t)),e},ee=new WeakSet,as=function(t){l(this,P)&&a(this,st,bt).call(this,!0,t)},se=new WeakSet,hs=function(t){l(this,P)&&(a(this,st,bt).call(this,!1,t),m(this,I,t))},ie=new WeakSet,cs=function(t,e){var s;if(t.preventDefault(),t.stopPropagation(),(s=this.flattedOptions.find(n=>n.id===e.value))==null?void 0:s.disabled)return;let i=t.target.querySelector(".treeselect-list__item-checkbox");i.checked=!i.checked,a(this,oe,ms).call(this,i,e)},le=new WeakSet,os=function(){let t=document.createElement("span");return t.setAttribute("tabindex","-1"),t.classList.add("treeselect-list__item-icon"),O(this.iconElements.arrowDown,t),t.addEventListener("mousedown",e=>a(this,ne,rs).call(this,e)),t},ne=new WeakSet,rs=function(t){t.preventDefault(),t.stopPropagation(),a(this,re,ks).call(this,t)},ae=new WeakSet,ds=function(t){let e=document.createElement("div");e.classList.add("treeselect-list__item-checkbox-container");let s=document.createElement("span");s.classList.add("treeselect-list__item-checkbox-icon"),s.innerHTML="";let i=document.createElement("input");return i.setAttribute("tabindex","-1"),i.setAttribute("type","checkbox"),i.setAttribute("input-id",t.value.toString()),i.classList.add("treeselect-list__item-checkbox"),e.append(s,i),e},he=new WeakSet,us=function(t,e){let s=document.createElement("label");if(s.textContent=t.name,s.classList.add("treeselect-list__item-label"),e&&this.showCount){let i=a(this,ce,ps).call(this,t);s.appendChild(i)}return s},ce=new WeakSet,ps=function(t){let e=document.createElement("span"),s=this.flattedOptions.filter(i=>i.childOf===t.value);return e.textContent=`(${s.length})`,e.classList.add("treeselect-list__item-label-counter"),e},oe=new WeakSet,ms=function(t,e){let s=this.flattedOptions.find(i=>i.id===e.value);if(s){if(s!=null&&s.isGroup&&this.disabledBranchNode){let i=Hi(t);i?.dispatchEvent(new Event("mousedown"));return}if(this.isSingleSelect){let[i]=l(this,T);if(s.id===i)return;m(this,T,[s.id]),Is([s.id],this.flattedOptions,this.isSingleSelect,this.isIndependentNodes)}else{s.checked=t.checked;let i=Pe(s,this.flattedOptions,this.isIndependentNodes);t.checked=i}ot(this.flattedOptions,this.srcElement,this.iconElements,l(this,T),this.rtl),a(this,ue,ws).call(this)}},re=new WeakSet,ks=function(t){var e,s;let i=(s=(e=t.target)==null?void 0:e.parentNode)==null?void 0:s.querySelector("[input-id]"),n=i?.getAttribute("input-id")??null,o=Qe(n,this.flattedOptions);o&&(o.isClosed=!o.isClosed,wt(this.flattedOptions,o),ot(this.flattedOptions,this.srcElement,this.iconElements,l(this,T),this.rtl),this.arrowClickCallback(o.id,o.isClosed))},st=new WeakSet,bt=function(t,e){let s="treeselect-list__item--focused";if(t){let i=Array.from(this.srcElement.querySelectorAll(`.${s}`));i.length&&i.forEach(n=>n.classList.remove(s)),e.classList.add(s)}else e.classList.remove(s)},rt=new WeakSet,de=function(){let{ungroupedNodes:t,groupedNodes:e,allNodes:s}=bi(this.flattedOptions);this.selectedNodes={nodes:t,groupedNodes:e,allNodes:s}},ue=new WeakSet,ws=function(){a(this,rt,de).call(this),this.inputCallback(this.selectedNodes),this.value=this.selectedNodes.nodes.map(t=>t.id)};var vs=({parentHtmlContainer:t,staticList:e,appendToBody:s,isSingleSelect:i,value:n,direction:o})=>{t||console.error("Validation: parentHtmlContainer prop is required!"),e&&s&&console.error("Validation: You should set staticList to false if you use appendToBody!"),i&&Array.isArray(n)&&console.error("Validation: if you use isSingleSelect prop, you should pass a single value!"),!i&&!Array.isArray(n)&&console.error("Validation: you should pass an array as a value!"),o&&o!=="auto"&&o!=="bottom"&&o!=="top"&&console.error("Validation: you should pass (auto | top | bottom | undefined) as a value for the direction prop!")},X=t=>t.map(e=>e.id),Vi=t=>t?Array.isArray(t)?t:[t]:[],Gi=(t,e)=>{if(e){let[s]=t;return s??null}return t},d,p,R,it,U,x,_,E,B,lt,me,nt,gt,ke,fs,we,bs,ve,gs,fe,Cs,be,Es,ge,Ss,dt,Ce,Ee,Ls,Se,ys,Le,xs,ye,_s,ut,xe,_e,Ks,J,pt,at,Ct,z,ht,We,Ws,ct,Et,Te,Ts,Oe,Os,Ne,Ns,Ae,As,Be,Bs,mt=class{constructor({parentHtmlContainer:e,value:s,options:i,openLevel:n,appendToBody:o,alwaysOpen:r,showTags:u,tagsCountText:v,clearable:k,searchable:w,placeholder:f,grouped:b,isGroupedValue:g,listSlotHtmlComponent:y,disabled:C,emptyText:$,staticList:V,id:q,ariaLabel:j,isSingleSelect:K,showCount:Xs,disabledBranchNode:Qs,direction:Zs,expandSelected:ti,saveScrollPosition:ei,isIndependentNodes:si,rtl:ii,iconElements:li,inputCallback:ni,openCallback:ai,closeCallback:hi,nameChangeCallback:ci,searchCallback:oi,openCloseGroupCallback:ri}){h(this,lt),h(this,nt),h(this,ke),h(this,we),h(this,ve),h(this,fe),h(this,be),h(this,ge),h(this,dt),h(this,Ee),h(this,Se),h(this,Le),h(this,ye),h(this,ut),h(this,_e),h(this,J),h(this,at),h(this,z),h(this,We),h(this,ct),h(this,Te),h(this,Oe),h(this,Ne),h(this,Ae),h(this,Be),c(this,"parentHtmlContainer"),c(this,"value"),c(this,"options"),c(this,"openLevel"),c(this,"appendToBody"),c(this,"alwaysOpen"),c(this,"showTags"),c(this,"tagsCountText"),c(this,"clearable"),c(this,"searchable"),c(this,"placeholder"),c(this,"grouped"),c(this,"isGroupedValue"),c(this,"listSlotHtmlComponent"),c(this,"disabled"),c(this,"emptyText"),c(this,"staticList"),c(this,"id"),c(this,"ariaLabel"),c(this,"isSingleSelect"),c(this,"showCount"),c(this,"disabledBranchNode"),c(this,"direction"),c(this,"expandSelected"),c(this,"saveScrollPosition"),c(this,"isIndependentNodes"),c(this,"rtl"),c(this,"iconElements"),c(this,"inputCallback"),c(this,"openCallback"),c(this,"closeCallback"),c(this,"nameChangeCallback"),c(this,"searchCallback"),c(this,"openCloseGroupCallback"),c(this,"ungroupedValue"),c(this,"groupedValue"),c(this,"allValue"),c(this,"isListOpened"),c(this,"selectedName"),c(this,"srcElement"),h(this,d,null),h(this,p,null),h(this,R,null),h(this,it,0),h(this,U,0),h(this,x,null),h(this,_,null),h(this,E,null),h(this,B,null),vs({parentHtmlContainer:e,value:s,staticList:V,appendToBody:o,isSingleSelect:K}),this.parentHtmlContainer=e,this.value=[],this.options=i??[],this.openLevel=n??0,this.appendToBody=o??!1,this.alwaysOpen=!!(r&&!C),this.showTags=u??!0,this.tagsCountText=v??"elements selected",this.clearable=k??!0,this.searchable=w??!0,this.placeholder=f??"Search...",this.grouped=b??!0,this.isGroupedValue=g??!1,this.listSlotHtmlComponent=y??null,this.disabled=C??!1,this.emptyText=$??"No results found...",this.staticList=!!(V&&!this.appendToBody),this.id=q??"",this.ariaLabel=j??"",this.isSingleSelect=K??!1,this.showCount=Xs??!1,this.disabledBranchNode=Qs??!1,this.direction=Zs??"auto",this.expandSelected=ti??!1,this.saveScrollPosition=ei??!0,this.isIndependentNodes=si??!1,this.rtl=ii??!1,this.iconElements=Ge(li),this.inputCallback=ni,this.openCallback=ai,this.closeCallback=hi,this.nameChangeCallback=ci,this.searchCallback=oi,this.openCloseGroupCallback=ri,this.ungroupedValue=[],this.groupedValue=[],this.allValue=[],this.isListOpened=!1,this.selectedName="",this.srcElement=null,a(this,lt,me).call(this,s)}mount(){vs({parentHtmlContainer:this.parentHtmlContainer,value:this.value,staticList:this.staticList,appendToBody:this.appendToBody,isSingleSelect:this.isSingleSelect}),this.iconElements=Ge(this.iconElements),a(this,lt,me).call(this,this.value)}updateValue(e){let s=Vi(e),i=l(this,d);i&&(i.updateValue(s),a(this,dt,Ce).call(this,i?.selectedNodes))}destroy(){this.srcElement&&(a(this,ut,xe).call(this),this.srcElement.innerHTML="",this.srcElement=null,a(this,z,ht).call(this,!0))}focus(){l(this,p)&&l(this,p).focus()}toggleOpenClose(){l(this,p)&&(l(this,p).openClose(),l(this,p).focus())}scrollWindowHandler(){this.updateListPosition()}focusWindowHandler(e){var s,i,n;(s=this.srcElement)!=null&&s.contains(e.target)||(i=l(this,d))!=null&&i.srcElement.contains(e.target)||((n=l(this,p))==null||n.blur(),a(this,z,ht).call(this,!1),a(this,J,pt).call(this,!1))}blurWindowHandler(){var e;(e=l(this,p))==null||e.blur(),a(this,z,ht).call(this,!1),a(this,J,pt).call(this,!1)}updateListPosition(){var e;let s=this.srcElement,i=(e=l(this,d))==null?void 0:e.srcElement;if(!s||!i)return;let{height:n}=i.getBoundingClientRect(),{x:o,y:r,height:u,width:v}=s.getBoundingClientRect(),k=window.innerHeight,w=r,f=k-r-u,b=w>f&&w>=n&&fa(this,Ee,Ls).call(this,i),arrowClickCallback:(i,n)=>a(this,Se,ys).call(this,i,n),mouseupCallback:()=>{var i;return(i=l(this,p))==null?void 0:i.focus()}}),s=new jt({value:[],showTags:this.showTags,tagsCountText:this.tagsCountText,clearable:this.clearable,isAlwaysOpened:this.alwaysOpen,searchable:this.searchable,placeholder:this.placeholder,disabled:this.disabled,isSingleSelect:this.isSingleSelect,id:this.id,ariaLabel:this.ariaLabel,iconElements:this.iconElements,inputCallback:i=>a(this,we,bs).call(this,i),searchCallback:i=>a(this,fe,Cs).call(this,i),openCallback:()=>a(this,ye,_s).call(this),closeCallback:()=>a(this,ut,xe).call(this),keydownCallback:i=>a(this,ve,gs).call(this,i),focusCallback:()=>a(this,be,Es).call(this),blurCallback:()=>a(this,ge,Ss).call(this),nameChangeCallback:i=>a(this,Le,xs).call(this,i)});return this.appendToBody&&m(this,R,new ResizeObserver(()=>this.updateListPosition())),t.append(s.srcElement),{container:t,list:e,input:s}},we=new WeakSet,bs=function(t){var e,s;let i=X(t);(e=l(this,d))==null||e.updateValue(i);let n=((s=l(this,d))==null?void 0:s.selectedNodes)??{};a(this,nt,gt).call(this,n),a(this,ct,Et).call(this)},ve=new WeakSet,gs=function(t){var e;this.isListOpened&&((e=l(this,d))==null||e.callKeyAction(t))},fe=new WeakSet,Cs=function(t){l(this,U)&&clearTimeout(l(this,U)),m(this,U,window.setTimeout(()=>{var e;(e=l(this,d))==null||e.updateSearchValue(t),this.updateListPosition()},350)),a(this,Ae,As).call(this,t)},be=new WeakSet,Es=function(){a(this,J,pt).call(this,!0),l(this,E)&&l(this,E)&&l(this,B)&&(document.addEventListener("mousedown",l(this,E),!0),document.addEventListener("focus",l(this,E),!0),window.addEventListener("blur",l(this,B)))},ge=new WeakSet,Ss=function(){setTimeout(()=>{var t,e;let s=(t=l(this,p))==null?void 0:t.srcElement.contains(document.activeElement),i=(e=l(this,d))==null?void 0:e.srcElement.contains(document.activeElement);!s&&!i&&this.blurWindowHandler()},1)},dt=new WeakSet,Ce=function(t){var e;if(!t)return;let s=[];this.isIndependentNodes||this.isSingleSelect?s=t.allNodes:this.grouped?s=t.groupedNodes:s=t.nodes,(e=l(this,p))==null||e.updateValue(s),a(this,nt,gt).call(this,t)},Ee=new WeakSet,Ls=function(t){var e,s,i;a(this,dt,Ce).call(this,t),this.isSingleSelect&&!this.alwaysOpen&&((e=l(this,p))==null||e.openClose(),(s=l(this,p))==null||s.clearSearch()),(i=l(this,p))==null||i.focus(),a(this,ct,Et).call(this)},Se=new WeakSet,ys=function(t,e){var s;(s=l(this,p))==null||s.focus(),this.updateListPosition(),a(this,Be,Bs).call(this,t,e)},Le=new WeakSet,xs=function(t){this.selectedName!==t&&(this.selectedName=t,a(this,Te,Ts).call(this))},ye=new WeakSet,_s=function(){var t;this.isListOpened=!0,l(this,x)&&l(this,_)&&(window.addEventListener("scroll",l(this,x),!0),window.addEventListener("resize",l(this,_))),!(!l(this,d)||!this.srcElement)&&(this.appendToBody?(document.body.appendChild(l(this,d).srcElement),(t=l(this,R))==null||t.observe(this.srcElement)):this.srcElement.appendChild(l(this,d).srcElement),this.updateListPosition(),a(this,at,Ct).call(this,!0),a(this,We,Ws).call(this),a(this,Oe,Os).call(this))},ut=new WeakSet,xe=function(){var t;this.alwaysOpen||(this.isListOpened=!1,l(this,x)&&l(this,_)&&(window.removeEventListener("scroll",l(this,x),!0),window.removeEventListener("resize",l(this,_))),!l(this,d)||!this.srcElement)||!(this.appendToBody?document.body.contains(l(this,d).srcElement):this.srcElement.contains(l(this,d).srcElement))||(m(this,it,l(this,d).srcElement.scrollTop),this.appendToBody?(document.body.removeChild(l(this,d).srcElement),(t=l(this,R))==null||t.disconnect()):this.srcElement.removeChild(l(this,d).srcElement),a(this,at,Ct).call(this,!1),a(this,Ne,Ns).call(this))},_e=new WeakSet,Ks=function(t,e){if(!l(this,d)||!l(this,p))return;let s=e?"treeselect-list--top-to-body":"treeselect-list--top",i=e?"treeselect-list--bottom-to-body":"treeselect-list--bottom";t?(l(this,d).srcElement.classList.add(s),l(this,d).srcElement.classList.remove(i),l(this,p).srcElement.classList.add("treeselect-input--top"),l(this,p).srcElement.classList.remove("treeselect-input--bottom")):(l(this,d).srcElement.classList.remove(s),l(this,d).srcElement.classList.add(i),l(this,p).srcElement.classList.remove("treeselect-input--top"),l(this,p).srcElement.classList.add("treeselect-input--bottom"))},J=new WeakSet,pt=function(t){!l(this,p)||!l(this,d)||(t?(l(this,p).srcElement.classList.add("treeselect-input--focused"),l(this,d).srcElement.classList.add("treeselect-list--focused")):(l(this,p).srcElement.classList.remove("treeselect-input--focused"),l(this,d).srcElement.classList.remove("treeselect-list--focused")))},at=new WeakSet,Ct=function(t){var e,s,i,n;t?(e=l(this,p))==null||e.srcElement.classList.add("treeselect-input--opened"):(s=l(this,p))==null||s.srcElement.classList.remove("treeselect-input--opened"),this.staticList?(i=l(this,d))==null||i.srcElement.classList.add("treeselect-list--static"):(n=l(this,d))==null||n.srcElement.classList.remove("treeselect-list--static")},z=new WeakSet,ht=function(t){!l(this,x)||!l(this,_)||!l(this,E)||!l(this,B)||((!this.alwaysOpen||t)&&(window.removeEventListener("scroll",l(this,x),!0),window.removeEventListener("resize",l(this,_))),document.removeEventListener("mousedown",l(this,E),!0),document.removeEventListener("focus",l(this,E),!0),window.removeEventListener("blur",l(this,B)))},We=new WeakSet,Ws=function(){var t,e,s;let i=(t=l(this,d))==null?void 0:t.isLastFocusedElementExist();this.saveScrollPosition&&i?(e=l(this,d))==null||e.srcElement.scroll(0,l(this,it)):(s=l(this,d))==null||s.focusFirstListElement()},ct=new WeakSet,Et=function(){var t;(t=this.srcElement)==null||t.dispatchEvent(new CustomEvent("input",{detail:this.value})),this.inputCallback&&this.inputCallback(this.value)},Te=new WeakSet,Ts=function(){var t;(t=this.srcElement)==null||t.dispatchEvent(new CustomEvent("name-change",{detail:this.selectedName})),this.nameChangeCallback&&this.nameChangeCallback(this.selectedName)},Oe=new WeakSet,Os=function(){var t;this.alwaysOpen||((t=this.srcElement)==null||t.dispatchEvent(new CustomEvent("open",{detail:this.value})),this.openCallback&&this.openCallback(this.value))},Ne=new WeakSet,Ns=function(){var t;this.alwaysOpen||((t=this.srcElement)==null||t.dispatchEvent(new CustomEvent("close",{detail:this.value})),this.closeCallback&&this.closeCallback(this.value))},Ae=new WeakSet,As=function(t){var e;let s=t?.trim()??"";(e=this.srcElement)==null||e.dispatchEvent(new CustomEvent("search",{detail:s})),this.searchCallback&&this.searchCallback(s)},Be=new WeakSet,Bs=function(t,e){var s;(s=this.srcElement)==null||s.dispatchEvent(new CustomEvent("open-close-group",{detail:{groupId:t,isClosed:e}})),this.openCloseGroupCallback&&this.openCloseGroupCallback(t,e)};function Mi({state:t,name:e,options:s,searchable:i,showCount:n,placeholder:o,rtl:r,disabledBranchNode:u=!0,disabled:v=!1,isSingleSelect:k=!0,showTags:w=!0,clearable:f=!0,isIndependentNodes:b=!0,alwaysOpen:g=!1,emptyText:y,expandSelected:C=!0,grouped:$=!0,openLevel:V=0,direction:q="auto"}){return{state:t,tree:null,init(){this.tree=new mt({id:`tree-${e}-id`,ariaLabel:`tree-${e}-label`,parentHtmlContainer:this.$refs.tree,value:this.state,options:s,searchable:i,showCount:n,placeholder:o,disabledBranchNode:u,disabled:v,isSingleSelect:k,showTags:w,clearable:f,isIndependentNodes:b,alwaysOpen:g,emptyText:y,expandSelected:C,grouped:$,openLevel:V,direction:q,rtl:r}),this.tree.srcElement.addEventListener("input",j=>{this.state=j.detail})}}}export{Mi as default}; +var di=Object.defineProperty,ui=(t,e,s)=>e in t?di(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s,c=(t,e,s)=>(ui(t,typeof e!="symbol"?e+"":e,s),s),He=(t,e,s)=>{if(!e.has(t))throw TypeError("Cannot "+s)},l=(t,e,s)=>(He(t,e,"read from private field"),s?s.call(t):e.get(t)),h=(t,e,s)=>{if(e.has(t))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(t):e.set(t,s)},m=(t,e,s,i)=>(He(t,e,"write to private field"),i?i.call(t,s):e.set(t,s),s),a=(t,e,s)=>(He(t,e,"access private method"),s),Ve={arrowUp:'',arrowDown:'',arrowRight:'',attention:'',clear:'',cross:'',check:'',partialCheck:''},O=(t,e)=>{if(e.innerHTML="",typeof t=="string")e.innerHTML=t;else{let s=t.cloneNode(!0);e.appendChild(s)}},Ge=t=>{let e=t?{...t}:{};return Object.keys(Ve).forEach(s=>{e[s]||(e[s]=Ve[s])}),e},pi=t=>t.reduce((e,{name:s},i)=>(e+=s,il(this,L).focus(),0)}blur(){this.isOpened&&a(this,A,H).call(this),this.clearSearch(),l(this,L).blur()}updateValue(e){this.value=e,a(this,G,F).call(this),a(this,M,Y).call(this)}removeItem(e){this.value=this.value.filter(s=>s.id!==e),a(this,Q,qt).call(this),a(this,G,F).call(this),a(this,M,Y).call(this)}clear(){this.value=[],a(this,Q,qt).call(this),a(this,G,F).call(this),this.clearSearch()}openClose(){a(this,A,H).call(this)}clearSearch(){this.searchText="",this.searchCallback(""),a(this,M,Y).call(this)}};N=new WeakMap,L=new WeakMap,D=new WeakMap,S=new WeakMap,St=new WeakSet,Hs=function(){a(this,G,F).call(this),a(this,M,Y).call(this),a(this,Lt,Me).call(this)},G=new WeakSet,F=function(){if(l(this,N).innerHTML="",this.showTags){l(this,N).append(...a(this,Tt,$e).call(this));let t=pi(this.value);this.nameChangeCallback(t)}else{let t=a(this,Bt,Ue).call(this);l(this,N).appendChild(t),this.nameChangeCallback(t.innerText)}l(this,N).appendChild(l(this,L))},Lt=new WeakSet,Me=function(){let t=[];l(this,D).innerHTML="",this.clearable&&t.push(a(this,Mt,Ye).call(this)),this.isAlwaysOpened||t.push(a(this,Dt,Ke).call(this,this.isOpened)),t.length&&l(this,D).append(...t)},yt=new WeakSet,Ie=function(){if(!this.isAlwaysOpened&&l(this,S)){let t=this.isOpened?this.iconElements.arrowUp:this.iconElements.arrowDown;O(t,l(this,S))}},M=new WeakSet,Y=function(){var t;(t=this.value)!=null&&t.length?(l(this,L).removeAttribute("placeholder"),this.srcElement.classList.remove("treeselect-input--value-not-selected")):(l(this,L).setAttribute("placeholder",this.placeholder),this.srcElement.classList.add("treeselect-input--value-not-selected")),this.searchable?this.srcElement.classList.remove("treeselect-input--unsearchable"):this.srcElement.classList.add("treeselect-input--unsearchable"),this.isSingleSelect?this.srcElement.classList.add("treeselect-input--is-single-select"):this.srcElement.classList.remove("treeselect-input--is-single-select"),l(this,L).value=this.searchText},A=new WeakSet,H=function(){this.isOpened=!this.isOpened,a(this,yt,Ie).call(this),this.isOpened?this.openCallback():this.closeCallback()},xt=new WeakSet,Ps=function(t,e,s){let i=document.createElement("div");return i.classList.add("treeselect-input"),i.setAttribute("tabindex","-1"),i.addEventListener("mousedown",n=>a(this,_t,De).call(this,n)),i.addEventListener("focus",()=>this.focusCallback(),!0),i.addEventListener("blur",()=>this.blurCallback(),!0),t.appendChild(e),i.append(t,s),i},_t=new WeakSet,De=function(t){t.stopPropagation(),this.isOpened||a(this,A,H).call(this),this.focus()},Wt=new WeakSet,Vs=function(){let t=document.createElement("div");return t.classList.add("treeselect-input__tags"),t},Tt=new WeakSet,$e=function(){return this.value.map(t=>{let e=document.createElement("div");e.classList.add("treeselect-input__tags-element"),e.setAttribute("tabindex","-1"),e.setAttribute("tag-id",t.id.toString()),e.setAttribute("title",t.name);let s=a(this,Nt,je).call(this,t.name),i=a(this,At,Re).call(this);return e.addEventListener("mousedown",n=>a(this,Ot,qe).call(this,n,t.id)),e.append(s,i),e})},Ot=new WeakSet,qe=function(t,e){t.preventDefault(),t.stopPropagation(),this.removeItem(e),this.focus()},Nt=new WeakSet,je=function(t){let e=document.createElement("span");return e.classList.add("treeselect-input__tags-name"),e.textContent=t,e},At=new WeakSet,Re=function(){let t=document.createElement("span");return t.classList.add("treeselect-input__tags-cross"),O(this.iconElements.cross,t),t},Bt=new WeakSet,Ue=function(){let t=document.createElement("span");if(t.classList.add("treeselect-input__tags-count"),!this.value.length)return t.textContent="",t.setAttribute("title",""),t;let e=this.value.length===1?this.value[0].name:`${this.value.length} ${this.tagsCountText}`;return t.textContent=e,t.setAttribute("title",e),t},Ht=new WeakSet,Gs=function(){let t=document.createElement("input");return t.classList.add("treeselect-input__edit"),this.id&&t.setAttribute("id",this.id),(!this.searchable||this.disabled)&&t.setAttribute("readonly","readonly"),this.disabled&&t.setAttribute("tabindex","-1"),this.ariaLabel.length&&t.setAttribute("aria-label",this.ariaLabel),t.addEventListener("keydown",e=>a(this,Pt,ze).call(this,e)),t.addEventListener("input",e=>a(this,Vt,Fe).call(this,e,t)),t},Pt=new WeakSet,ze=function(t){t.stopPropagation();let e=t.key;e==="Backspace"&&!this.searchText.length&&this.value.length&&!this.showTags&&this.clear(),e==="Backspace"&&!this.searchText.length&&this.value.length&&this.removeItem(this.value[this.value.length-1].id),t.code==="Space"&&(!this.searchText||!this.searchable)&&a(this,A,H).call(this),(e==="Enter"||e==="ArrowDown"||e==="ArrowUp")&&t.preventDefault(),this.keydownCallback(t),e!=="Tab"&&this.focus()},Vt=new WeakSet,Fe=function(t,e){t.stopPropagation();let s=this.searchText,i=e.value.trim();if(s.length===0&&i.length===0){e.value="";return}if(this.searchable){let n=t.target.value;this.searchCallback(n),this.isOpened||a(this,A,H).call(this)}else e.value="";this.searchText=e.value},Gt=new WeakSet,Ms=function(){let t=document.createElement("div");return t.classList.add("treeselect-input__operators"),t},Mt=new WeakSet,Ye=function(){let t=document.createElement("span");return t.classList.add("treeselect-input__clear"),t.setAttribute("tabindex","-1"),O(this.iconElements.clear,t),t.addEventListener("mousedown",e=>a(this,It,Je).call(this,e)),t},It=new WeakSet,Je=function(t){t.preventDefault(),t.stopPropagation(),(this.searchText.length||this.value.length)&&this.clear(),this.focus()},Dt=new WeakSet,Ke=function(t){m(this,S,document.createElement("span")),l(this,S).classList.add("treeselect-input__arrow");let e=t?this.iconElements.arrowUp:this.iconElements.arrowDown;return O(e,l(this,S)),l(this,S).addEventListener("mousedown",s=>a(this,$t,Xe).call(this,s)),l(this,S)},$t=new WeakSet,Xe=function(t){t.stopPropagation(),t.preventDefault(),this.focus(),a(this,A,H).call(this)},Q=new WeakSet,qt=function(){this.inputCallback(this.value)};var Is=(t,e,s,i)=>{vi(e);let n=e.filter(o=>!o.disabled&&t.some(r=>r===o.id));if(s&&n.length){n[0].checked=!0;return}n.forEach(o=>{o.checked=!0;let r=Pe(o,e,i);o.checked=r})},Pe=({id:t,checked:e},s,i)=>{let n=s.find(r=>r.id===t);if(!n)return!1;if(i)return n.checked=n.disabled?!1:!!e,n.checked;let o=Ds(!!e,n,s);return $s(n,s),o},Ds=(t,e,s)=>{if(!e.isGroup)return e.checked=e.disabled?!1:!!t,e.isPartialChecked=!1,e.checked;let i=s.filter(n=>n.childOf===e.id);return!t||e.disabled||e.isPartialChecked?(e.checked=!1,e.isPartialChecked=!1,Rt(e,i,s),e.checked):qs(i,s)?js(i)?(e.checked=!1,e.isPartialChecked=!1,e.disabled=!0,e.checked):(e.checked=!1,e.isPartialChecked=!0,i.forEach(n=>{Ds(t,n,s)}),e.checked):(e.checked=!0,e.isPartialChecked=!1,Rt(e,i,s),e.checked)},$s=(t,e)=>{let s=e.find(i=>i.id===t.childOf);s&&(mi(s,e),$s(s,e))},mi=(t,e)=>{let s=kt(t,e);if(js(s)){t.checked=!1,t.isPartialChecked=!1,t.disabled=!0;return}if(ki(s)){t.checked=!0,t.isPartialChecked=!1;return}if(wi(s)){t.checked=!1,t.isPartialChecked=!0;return}t.checked=!1,t.isPartialChecked=!1},Rt=({checked:t,disabled:e},s,i)=>{s.forEach(n=>{n.disabled=!!e||!!n.disabled,n.checked=!!t&&!n.disabled,n.isPartialChecked=!1;let o=kt(n,i);Rt({checked:t,disabled:e},o,i)})},qs=(t,e)=>t.some(s=>s.disabled)?!0:t.some(s=>{if(s.isGroup){let i=kt(s,e);return qs(i,e)}return!1}),js=t=>t.every(e=>!!e.disabled),ki=t=>t.every(e=>!!e.checked),wi=t=>t.some(e=>!!e.checked||!!e.isPartialChecked),vi=t=>{t.forEach(e=>{e.checked=!1,e.isPartialChecked=!1})},fi=(t,e,s)=>{let i={level:0,groupId:""},n=Rs(t,e,i.groupId,i.level);return gi(n,s)},Rs=(t,e,s,i)=>t.reduce((n,o)=>{var r;let u=!!((r=o.children)!=null&&r.length),v=i>=e&&u,k=i>e;if(n.push({id:o.value,name:o.name,childOf:s,isGroup:u,checked:!1,isPartialChecked:!1,level:i,isClosed:v,hidden:k,disabled:o.disabled??!1}),u){let w=Rs(o.children,e,o.value,i+1);n.push(...w)}return n},[]),kt=({id:t},e)=>e.filter(s=>s.childOf===t),bi=t=>{let{ungroupedNodes:e,allGroupedNodes:s,allNodes:i}=t.reduce((o,r)=>(r.checked&&(o.allNodes.push(r),r.isGroup?o.allGroupedNodes.push(r):o.ungroupedNodes.push(r)),o),{ungroupedNodes:[],allGroupedNodes:[],allNodes:[]}),n=i.filter(o=>!s.some(({id:r})=>r===o.childOf));return{ungroupedNodes:e,groupedNodes:n,allNodes:i}},gi=(t,e)=>(t.filter(s=>!!s.disabled).forEach(({id:s})=>Pe({id:s,checked:!1},t,e)),t),wt=(t,{id:e,isClosed:s})=>{kt({id:e},t).forEach(i=>{i.hidden=s??!1,i.isGroup&&!i.isClosed&&wt(t,{id:i.id,isClosed:s})})},Ci=t=>{t.filter(e=>e.isGroup&&!e.disabled&&(e.checked||e.isPartialChecked)).forEach(e=>{e.isClosed=!1,wt(t,e)})},Ei=(t,e)=>{let s=Si(t,e);t.forEach(i=>{s.some(({id:n})=>n===i.id)?(i.isGroup&&(i.isClosed=!1,wt(t,i)),i.hidden=!1):i.hidden=!0})},Si=(t,e)=>t.reduce((s,i)=>{if(i.name.toLowerCase().includes(e.toLowerCase())){if(s.push(i),i.isGroup){let n=Us(i.id,t);s.push(...n)}if(i.childOf){let n=zs(i.childOf,t);s.push(...n)}}return s},[]),Us=(t,e)=>e.reduce((s,i)=>(i.childOf===t&&(s.push(i),i.isGroup&&s.push(...Us(i.id,e))),s),[]),zs=(t,e)=>e.reduce((s,i)=>(i.id===t&&(s.push(i),i.childOf&&s.push(...zs(i.childOf,e))),s),[]),Li=t=>{let{duplications:e}=t.reduce((s,i)=>(s.allItems.some(n=>n.toString()===i.id.toString())&&s.duplications.push(i.id),s.allItems.push(i.id),s),{duplications:[],allItems:[]});e.length&&console.error(`Validation: You have duplicated values: ${e.join(", ")}! You should use unique values.`)},yi=(t,e,s,i,n,o,r,u,v,k)=>{Is(t,e,n,v),u&&r&&Ci(e),ot(e,s,i,o,k)},ot=(t,e,s,i,n)=>{t.forEach(o=>{let r=e.querySelector(`[input-id="${o.id}"]`),u=W(r);r.checked=o.checked,xi(o,u,i),_i(o,u),Wi(o,u),Ti(o,u,s),Oi(o,u),Ai(o,u,t,n),Ni(o,r,s)}),Bi(t,e)},xi=(t,e,s)=>{t.checked?e.classList.add("treeselect-list__item--checked"):e.classList.remove("treeselect-list__item--checked"),Array.isArray(s)&&s[0]===t.id&&!t.disabled?e.classList.add("treeselect-list__item--single-selected"):e.classList.remove("treeselect-list__item--single-selected")},_i=(t,e)=>{t.isPartialChecked?e.classList.add("treeselect-list__item--partial-checked"):e.classList.remove("treeselect-list__item--partial-checked")},Wi=(t,e)=>{t.disabled?e.classList.add("treeselect-list__item--disabled"):e.classList.remove("treeselect-list__item--disabled")},Ti=(t,e,s)=>{if(t.isGroup){let i=e.querySelector(".treeselect-list__item-icon"),n=t.isClosed?s.arrowRight:s.arrowDown;O(n,i),t.isClosed?e.classList.add("treeselect-list__item--closed"):e.classList.remove("treeselect-list__item--closed")}},Oi=(t,e)=>{t.hidden?e.classList.add("treeselect-list__item--hidden"):e.classList.remove("treeselect-list__item--hidden")},Ni=(t,e,s)=>{let i=e.parentNode.querySelector(".treeselect-list__item-checkbox-icon");t.checked?O(s.check,i):t.isPartialChecked?O(s.partialCheck,i):i.innerHTML=""},Ai=(t,e,s,i)=>{let n=t.level===0,o=20,r=5;if(n){let u=s.some(w=>w.isGroup&&w.level===t.level),v=!t.isGroup&&u?`${o}px`:`${r}px`,k=t.isGroup?"0":v;i?e.style.paddingRight=k:e.style.paddingLeft=k}else{let u=t.isGroup?`${t.level*o}px`:`${t.level*o+o}px`;i?e.style.paddingRight=u:e.style.paddingLeft=u}e.setAttribute("level",t.level.toString()),e.setAttribute("group",t.isGroup.toString())},Bi=(t,e)=>{let s=t.some(n=>!n.hidden),i=e.querySelector(".treeselect-list__empty");s?i.classList.add("treeselect-list__empty--hidden"):i.classList.remove("treeselect-list__empty--hidden")},W=t=>t.parentNode.parentNode,Qe=(t,e)=>e.find(s=>s.id.toString()===t),Hi=t=>W(t).querySelector(".treeselect-list__item-icon"),Pi=(t,e)=>{e&&Object.keys(e).forEach(s=>{let i=e[s];typeof i=="string"&&t.setAttribute(s,i)})},I,P,T,Z,Ut,Fs,zt,Ys,Ft,Js,Yt,Ze,Jt,ts,Kt,es,tt,vt,Xt,ss,Qt,is,Zt,ls,et,ft,te,ns,ee,as,se,hs,ie,cs,le,os,ne,rs,ae,ds,he,us,ce,ps,oe,ms,re,ks,st,bt,rt,de,ue,ws,pe=class{constructor({options:e,value:s,openLevel:i,listSlotHtmlComponent:n,emptyText:o,isSingleSelect:r,iconElements:u,showCount:v,disabledBranchNode:k,expandSelected:w,isIndependentNodes:f,rtl:b,inputCallback:g,arrowClickCallback:y,mouseupCallback:C}){h(this,Ut),h(this,zt),h(this,Ft),h(this,Yt),h(this,Jt),h(this,Kt),h(this,tt),h(this,Xt),h(this,Qt),h(this,Zt),h(this,et),h(this,te),h(this,ee),h(this,se),h(this,ie),h(this,le),h(this,ne),h(this,ae),h(this,he),h(this,ce),h(this,oe),h(this,re),h(this,st),h(this,rt),h(this,ue),c(this,"options"),c(this,"value"),c(this,"openLevel"),c(this,"listSlotHtmlComponent"),c(this,"emptyText"),c(this,"isSingleSelect"),c(this,"showCount"),c(this,"disabledBranchNode"),c(this,"expandSelected"),c(this,"isIndependentNodes"),c(this,"rtl"),c(this,"iconElements"),c(this,"searchText"),c(this,"flattedOptions"),c(this,"flattedOptionsBeforeSearch"),c(this,"selectedNodes"),c(this,"srcElement"),c(this,"inputCallback"),c(this,"arrowClickCallback"),c(this,"mouseupCallback"),h(this,I,null),h(this,P,!0),h(this,T,[]),h(this,Z,!0),this.options=e,this.value=s,this.openLevel=i??0,this.listSlotHtmlComponent=n??null,this.emptyText=o??"No results found...",this.isSingleSelect=r??!1,this.showCount=v??!1,this.disabledBranchNode=k??!1,this.expandSelected=w??!1,this.isIndependentNodes=f??!1,this.rtl=b??!1,this.iconElements=u,this.searchText="",this.flattedOptions=fi(this.options,this.openLevel,this.isIndependentNodes),this.flattedOptionsBeforeSearch=this.flattedOptions,this.selectedNodes={nodes:[],groupedNodes:[],allNodes:[]},this.srcElement=a(this,Ft,Js).call(this),this.inputCallback=g,this.arrowClickCallback=y,this.mouseupCallback=C,Li(this.flattedOptions)}updateValue(e){this.value=e,m(this,T,this.isSingleSelect?this.value:[]),yi(e,this.flattedOptions,this.srcElement,this.iconElements,this.isSingleSelect,l(this,T),this.expandSelected,l(this,Z),this.isIndependentNodes,this.rtl),m(this,Z,!1),a(this,rt,de).call(this)}updateSearchValue(e){if(e===this.searchText)return;let s=this.searchText===""&&e!=="";this.searchText=e,s&&(this.flattedOptionsBeforeSearch=JSON.parse(JSON.stringify(this.flattedOptions))),this.searchText===""&&(this.flattedOptions=this.flattedOptionsBeforeSearch.map(i=>{let n=this.flattedOptions.find(o=>o.id===i.id);return n.isClosed=i.isClosed,n.hidden=i.hidden,n}),this.flattedOptionsBeforeSearch=[]),this.searchText&&Ei(this.flattedOptions,e),ot(this.flattedOptions,this.srcElement,this.iconElements,l(this,T),this.rtl),this.focusFirstListElement()}callKeyAction(e){m(this,P,!1);let s=this.srcElement.querySelector(".treeselect-list__item--focused");if(s?.classList.contains("treeselect-list__item--hidden"))return;let i=e.key;i==="Enter"&&s&&s.dispatchEvent(new Event("mousedown")),(i==="ArrowLeft"||i==="ArrowRight")&&a(this,Ut,Fs).call(this,s,e),(i==="ArrowDown"||i==="ArrowUp")&&a(this,zt,Ys).call(this,s,i)}focusFirstListElement(){let e="treeselect-list__item--focused",s=this.srcElement.querySelector(`.${e}`),i=Array.from(this.srcElement.querySelectorAll(".treeselect-list__item-checkbox")).filter(n=>window.getComputedStyle(W(n)).display!=="none");i.length&&(s&&s.classList.remove(e),W(i[0]).classList.add(e))}isLastFocusedElementExist(){return!!l(this,I)}};I=new WeakMap,P=new WeakMap,T=new WeakMap,Z=new WeakMap,Ut=new WeakSet,Fs=function(t,e){if(!t)return;let s=e.key,i=t.querySelector(".treeselect-list__item-checkbox").getAttribute("input-id"),n=Qe(i,this.flattedOptions),o=t.querySelector(".treeselect-list__item-icon");s==="ArrowLeft"&&!n.isClosed&&n.isGroup&&(o.dispatchEvent(new Event("mousedown")),e.preventDefault()),s==="ArrowRight"&&n.isClosed&&n.isGroup&&(o.dispatchEvent(new Event("mousedown")),e.preventDefault())},zt=new WeakSet,Ys=function(t,e){var s;let i=Array.from(this.srcElement.querySelectorAll(".treeselect-list__item-checkbox")).filter(n=>window.getComputedStyle(W(n)).display!=="none");if(i.length)if(!t)W(i[0]).classList.add("treeselect-list__item--focused");else{let n=i.findIndex(g=>W(g).classList.contains("treeselect-list__item--focused"));W(i[n]).classList.remove("treeselect-list__item--focused");let o=e==="ArrowDown"?n+1:n-1,r=e==="ArrowDown"?0:i.length-1,u=i[o]??i[r],v=!i[o],k=W(u);k.classList.add("treeselect-list__item--focused");let w=this.srcElement.getBoundingClientRect(),f=k.getBoundingClientRect();if(v&&e==="ArrowDown"){this.srcElement.scroll(0,0);return}if(v&&e==="ArrowUp"){this.srcElement.scroll(0,this.srcElement.scrollHeight);return}let b=((s=this.listSlotHtmlComponent)==null?void 0:s.clientHeight)??0;if(w.y+w.heightf.y){this.srcElement.scroll(0,this.srcElement.scrollTop-f.height);return}}},Ft=new WeakSet,Js=function(){let t=a(this,Yt,Ze).call(this),e=a(this,tt,vt).call(this,this.options);t.append(...e);let s=a(this,Qt,is).call(this);t.append(s);let i=a(this,Xt,ss).call(this);return i&&t.append(i),t},Yt=new WeakSet,Ze=function(){let t=document.createElement("div");return t.classList.add("treeselect-list"),this.isSingleSelect&&t.classList.add("treeselect-list--single-select"),this.disabledBranchNode&&t.classList.add("treeselect-list--disabled-branch-node"),t.addEventListener("mouseout",e=>a(this,Jt,ts).call(this,e)),t.addEventListener("mousemove",()=>a(this,Kt,es).call(this)),t.addEventListener("mouseup",()=>this.mouseupCallback(),!0),t},Jt=new WeakSet,ts=function(t){t.stopPropagation(),l(this,I)&&l(this,P)&&l(this,I).classList.add("treeselect-list__item--focused")},Kt=new WeakSet,es=function(){m(this,P,!0)},tt=new WeakSet,vt=function(t){return t.reduce((e,s)=>{var i;if((i=s.children)!=null&&i.length){let o=a(this,Zt,ls).call(this,s),r=a(this,tt,vt).call(this,s.children);return o.append(...r),e.push(o),e}let n=a(this,et,ft).call(this,s,!1);return e.push(n),e},[])},Xt=new WeakSet,ss=function(){if(!this.listSlotHtmlComponent)return null;let t=document.createElement("div");return t.classList.add("treeselect-list__slot"),t.appendChild(this.listSlotHtmlComponent),t},Qt=new WeakSet,is=function(){let t=document.createElement("div");t.classList.add("treeselect-list__empty"),t.setAttribute("title",this.emptyText);let e=document.createElement("span");e.classList.add("treeselect-list__empty-icon"),O(this.iconElements.attention,e);let s=document.createElement("span");return s.classList.add("treeselect-list__empty-text"),s.textContent=this.emptyText,t.append(e,s),t},Zt=new WeakSet,ls=function(t){let e=document.createElement("div");e.setAttribute("group-container-id",t.value.toString()),e.classList.add("treeselect-list__group-container");let s=a(this,et,ft).call(this,t,!0);return e.appendChild(s),e},et=new WeakSet,ft=function(t,e){let s=a(this,te,ns).call(this,t);if(e){let o=a(this,le,os).call(this);s.appendChild(o),s.classList.add("treeselect-list__item--group")}let i=a(this,ae,ds).call(this,t),n=a(this,he,us).call(this,t,e);return s.append(i,n),s},te=new WeakSet,ns=function(t){let e=document.createElement("div");return Pi(e,t.htmlAttr),e.setAttribute("tabindex","-1"),e.setAttribute("title",t.name),e.classList.add("treeselect-list__item"),e.addEventListener("mouseover",()=>a(this,ee,as).call(this,e),!0),e.addEventListener("mouseout",()=>a(this,se,hs).call(this,e),!0),e.addEventListener("mousedown",s=>a(this,ie,cs).call(this,s,t)),e},ee=new WeakSet,as=function(t){l(this,P)&&a(this,st,bt).call(this,!0,t)},se=new WeakSet,hs=function(t){l(this,P)&&(a(this,st,bt).call(this,!1,t),m(this,I,t))},ie=new WeakSet,cs=function(t,e){var s;if(t.preventDefault(),t.stopPropagation(),(s=this.flattedOptions.find(n=>n.id===e.value))==null?void 0:s.disabled)return;let i=t.target.querySelector(".treeselect-list__item-checkbox");i.checked=!i.checked,a(this,oe,ms).call(this,i,e)},le=new WeakSet,os=function(){let t=document.createElement("span");return t.setAttribute("tabindex","-1"),t.classList.add("treeselect-list__item-icon"),O(this.iconElements.arrowDown,t),t.addEventListener("mousedown",e=>a(this,ne,rs).call(this,e)),t},ne=new WeakSet,rs=function(t){t.preventDefault(),t.stopPropagation(),a(this,re,ks).call(this,t)},ae=new WeakSet,ds=function(t){let e=document.createElement("div");e.classList.add("treeselect-list__item-checkbox-container");let s=document.createElement("span");s.classList.add("treeselect-list__item-checkbox-icon"),s.innerHTML="";let i=document.createElement("input");return i.setAttribute("tabindex","-1"),i.setAttribute("type","checkbox"),i.setAttribute("input-id",t.value.toString()),i.classList.add("treeselect-list__item-checkbox"),e.append(s,i),e},he=new WeakSet,us=function(t,e){let s=document.createElement("label");if(s.textContent=t.name,s.classList.add("treeselect-list__item-label"),e&&this.showCount){let i=a(this,ce,ps).call(this,t);s.appendChild(i)}return s},ce=new WeakSet,ps=function(t){let e=document.createElement("span"),s=this.flattedOptions.filter(i=>i.childOf===t.value);return e.textContent=`(${s.length})`,e.classList.add("treeselect-list__item-label-counter"),e},oe=new WeakSet,ms=function(t,e){let s=this.flattedOptions.find(i=>i.id===e.value);if(s){if(s!=null&&s.isGroup&&this.disabledBranchNode){let i=Hi(t);i?.dispatchEvent(new Event("mousedown"));return}if(this.isSingleSelect){let[i]=l(this,T);if(s.id===i)return;m(this,T,[s.id]),Is([s.id],this.flattedOptions,this.isSingleSelect,this.isIndependentNodes)}else{s.checked=t.checked;let i=Pe(s,this.flattedOptions,this.isIndependentNodes);t.checked=i}ot(this.flattedOptions,this.srcElement,this.iconElements,l(this,T),this.rtl),a(this,ue,ws).call(this)}},re=new WeakSet,ks=function(t){var e,s;let i=(s=(e=t.target)==null?void 0:e.parentNode)==null?void 0:s.querySelector("[input-id]"),n=i?.getAttribute("input-id")??null,o=Qe(n,this.flattedOptions);o&&(o.isClosed=!o.isClosed,wt(this.flattedOptions,o),ot(this.flattedOptions,this.srcElement,this.iconElements,l(this,T),this.rtl),this.arrowClickCallback(o.id,o.isClosed))},st=new WeakSet,bt=function(t,e){let s="treeselect-list__item--focused";if(t){let i=Array.from(this.srcElement.querySelectorAll(`.${s}`));i.length&&i.forEach(n=>n.classList.remove(s)),e.classList.add(s)}else e.classList.remove(s)},rt=new WeakSet,de=function(){let{ungroupedNodes:t,groupedNodes:e,allNodes:s}=bi(this.flattedOptions);this.selectedNodes={nodes:t,groupedNodes:e,allNodes:s}},ue=new WeakSet,ws=function(){a(this,rt,de).call(this),this.inputCallback(this.selectedNodes),this.value=this.selectedNodes.nodes.map(t=>t.id)};var vs=({parentHtmlContainer:t,staticList:e,appendToBody:s,isSingleSelect:i,value:n,direction:o})=>{t||console.error("Validation: parentHtmlContainer prop is required!"),e&&s&&console.error("Validation: You should set staticList to false if you use appendToBody!"),i&&Array.isArray(n)&&console.error("Validation: if you use isSingleSelect prop, you should pass a single value!"),!i&&!Array.isArray(n)&&console.error("Validation: you should pass an array as a value!"),o&&o!=="auto"&&o!=="bottom"&&o!=="top"&&console.error("Validation: you should pass (auto | top | bottom | undefined) as a value for the direction prop!")},X=t=>t.map(e=>e.id),Vi=t=>t?Array.isArray(t)?t:[t]:[],Gi=(t,e)=>{if(e){let[s]=t;return s??null}return t},d,p,R,it,U,x,_,E,B,lt,me,nt,gt,ke,fs,we,bs,ve,gs,fe,Cs,be,Es,ge,Ss,dt,Ce,Ee,Ls,Se,ys,Le,xs,ye,_s,ut,xe,_e,Ks,J,pt,at,Ct,z,ht,We,Ws,ct,Et,Te,Ts,Oe,Os,Ne,Ns,Ae,As,Be,Bs,mt=class{constructor({parentHtmlContainer:e,value:s,options:i,openLevel:n,appendToBody:o,alwaysOpen:r,showTags:u,tagsCountText:v,clearable:k,searchable:w,placeholder:f,grouped:b,isGroupedValue:g,listSlotHtmlComponent:y,disabled:C,emptyText:$,staticList:V,id:q,ariaLabel:j,isSingleSelect:K,showCount:Xs,disabledBranchNode:Qs,direction:Zs,expandSelected:ti,saveScrollPosition:ei,isIndependentNodes:si,rtl:ii,iconElements:li,inputCallback:ni,openCallback:ai,closeCallback:hi,nameChangeCallback:ci,searchCallback:oi,openCloseGroupCallback:ri}){h(this,lt),h(this,nt),h(this,ke),h(this,we),h(this,ve),h(this,fe),h(this,be),h(this,ge),h(this,dt),h(this,Ee),h(this,Se),h(this,Le),h(this,ye),h(this,ut),h(this,_e),h(this,J),h(this,at),h(this,z),h(this,We),h(this,ct),h(this,Te),h(this,Oe),h(this,Ne),h(this,Ae),h(this,Be),c(this,"parentHtmlContainer"),c(this,"value"),c(this,"options"),c(this,"openLevel"),c(this,"appendToBody"),c(this,"alwaysOpen"),c(this,"showTags"),c(this,"tagsCountText"),c(this,"clearable"),c(this,"searchable"),c(this,"placeholder"),c(this,"grouped"),c(this,"isGroupedValue"),c(this,"listSlotHtmlComponent"),c(this,"disabled"),c(this,"emptyText"),c(this,"staticList"),c(this,"id"),c(this,"ariaLabel"),c(this,"isSingleSelect"),c(this,"showCount"),c(this,"disabledBranchNode"),c(this,"direction"),c(this,"expandSelected"),c(this,"saveScrollPosition"),c(this,"isIndependentNodes"),c(this,"rtl"),c(this,"iconElements"),c(this,"inputCallback"),c(this,"openCallback"),c(this,"closeCallback"),c(this,"nameChangeCallback"),c(this,"searchCallback"),c(this,"openCloseGroupCallback"),c(this,"ungroupedValue"),c(this,"groupedValue"),c(this,"allValue"),c(this,"isListOpened"),c(this,"selectedName"),c(this,"srcElement"),h(this,d,null),h(this,p,null),h(this,R,null),h(this,it,0),h(this,U,0),h(this,x,null),h(this,_,null),h(this,E,null),h(this,B,null),vs({parentHtmlContainer:e,value:s,staticList:V,appendToBody:o,isSingleSelect:K}),this.parentHtmlContainer=e,this.value=[],this.options=i??[],this.openLevel=n??0,this.appendToBody=o??!1,this.alwaysOpen=!!(r&&!C),this.showTags=u??!0,this.tagsCountText=v??"elements selected",this.clearable=k??!0,this.searchable=w??!0,this.placeholder=f??"Search...",this.grouped=b??!0,this.isGroupedValue=g??!1,this.listSlotHtmlComponent=y??null,this.disabled=C??!1,this.emptyText=$??"No results found...",this.staticList=!!(V&&!this.appendToBody),this.id=q??"",this.ariaLabel=j??"",this.isSingleSelect=K??!1,this.showCount=Xs??!1,this.disabledBranchNode=Qs??!1,this.direction=Zs??"auto",this.expandSelected=ti??!1,this.saveScrollPosition=ei??!0,this.isIndependentNodes=si??!1,this.rtl=ii??!1,this.iconElements=Ge(li),this.inputCallback=ni,this.openCallback=ai,this.closeCallback=hi,this.nameChangeCallback=ci,this.searchCallback=oi,this.openCloseGroupCallback=ri,this.ungroupedValue=[],this.groupedValue=[],this.allValue=[],this.isListOpened=!1,this.selectedName="",this.srcElement=null,a(this,lt,me).call(this,s)}mount(){vs({parentHtmlContainer:this.parentHtmlContainer,value:this.value,staticList:this.staticList,appendToBody:this.appendToBody,isSingleSelect:this.isSingleSelect}),this.iconElements=Ge(this.iconElements),a(this,lt,me).call(this,this.value)}updateValue(e){let s=Vi(e),i=l(this,d);i&&(i.updateValue(s),a(this,dt,Ce).call(this,i?.selectedNodes))}destroy(){this.srcElement&&(a(this,ut,xe).call(this),this.srcElement.innerHTML="",this.srcElement=null,a(this,z,ht).call(this,!0))}focus(){l(this,p)&&l(this,p).focus()}toggleOpenClose(){l(this,p)&&(l(this,p).openClose(),l(this,p).focus())}scrollWindowHandler(){this.updateListPosition()}focusWindowHandler(e){var s,i,n;(s=this.srcElement)!=null&&s.contains(e.target)||(i=l(this,d))!=null&&i.srcElement.contains(e.target)||((n=l(this,p))==null||n.blur(),a(this,z,ht).call(this,!1),a(this,J,pt).call(this,!1))}blurWindowHandler(){var e;(e=l(this,p))==null||e.blur(),a(this,z,ht).call(this,!1),a(this,J,pt).call(this,!1)}updateListPosition(){var e;let s=this.srcElement,i=(e=l(this,d))==null?void 0:e.srcElement;if(!s||!i)return;let{height:n}=i.getBoundingClientRect(),{x:o,y:r,height:u,width:v}=s.getBoundingClientRect(),k=window.innerHeight,w=r,f=k-r-u,b=w>f&&w>=n&&fa(this,Ee,Ls).call(this,i),arrowClickCallback:(i,n)=>a(this,Se,ys).call(this,i,n),mouseupCallback:()=>{var i;return(i=l(this,p))==null?void 0:i.focus()}}),s=new jt({value:[],showTags:this.showTags,tagsCountText:this.tagsCountText,clearable:this.clearable,isAlwaysOpened:this.alwaysOpen,searchable:this.searchable,placeholder:this.placeholder,disabled:this.disabled,isSingleSelect:this.isSingleSelect,id:this.id,ariaLabel:this.ariaLabel,iconElements:this.iconElements,inputCallback:i=>a(this,we,bs).call(this,i),searchCallback:i=>a(this,fe,Cs).call(this,i),openCallback:()=>a(this,ye,_s).call(this),closeCallback:()=>a(this,ut,xe).call(this),keydownCallback:i=>a(this,ve,gs).call(this,i),focusCallback:()=>a(this,be,Es).call(this),blurCallback:()=>a(this,ge,Ss).call(this),nameChangeCallback:i=>a(this,Le,xs).call(this,i)});return this.appendToBody&&m(this,R,new ResizeObserver(()=>this.updateListPosition())),t.append(s.srcElement),{container:t,list:e,input:s}},we=new WeakSet,bs=function(t){var e,s;let i=X(t);(e=l(this,d))==null||e.updateValue(i);let n=((s=l(this,d))==null?void 0:s.selectedNodes)??{};a(this,nt,gt).call(this,n),a(this,ct,Et).call(this)},ve=new WeakSet,gs=function(t){var e;this.isListOpened&&((e=l(this,d))==null||e.callKeyAction(t))},fe=new WeakSet,Cs=function(t){l(this,U)&&clearTimeout(l(this,U)),m(this,U,window.setTimeout(()=>{var e;(e=l(this,d))==null||e.updateSearchValue(t),this.updateListPosition()},350)),a(this,Ae,As).call(this,t)},be=new WeakSet,Es=function(){a(this,J,pt).call(this,!0),l(this,E)&&l(this,E)&&l(this,B)&&(document.addEventListener("mousedown",l(this,E),!0),document.addEventListener("focus",l(this,E),!0),window.addEventListener("blur",l(this,B)))},ge=new WeakSet,Ss=function(){setTimeout(()=>{var t,e;let s=(t=l(this,p))==null?void 0:t.srcElement.contains(document.activeElement),i=(e=l(this,d))==null?void 0:e.srcElement.contains(document.activeElement);!s&&!i&&this.blurWindowHandler()},1)},dt=new WeakSet,Ce=function(t){var e;if(!t)return;let s=[];this.isIndependentNodes||this.isSingleSelect?s=t.allNodes:this.grouped?s=t.groupedNodes:s=t.nodes,(e=l(this,p))==null||e.updateValue(s),a(this,nt,gt).call(this,t)},Ee=new WeakSet,Ls=function(t){var e,s,i;a(this,dt,Ce).call(this,t),this.isSingleSelect&&!this.alwaysOpen&&((e=l(this,p))==null||e.openClose(),(s=l(this,p))==null||s.clearSearch()),(i=l(this,p))==null||i.focus(),a(this,ct,Et).call(this)},Se=new WeakSet,ys=function(t,e){var s;(s=l(this,p))==null||s.focus(),this.updateListPosition(),a(this,Be,Bs).call(this,t,e)},Le=new WeakSet,xs=function(t){this.selectedName!==t&&(this.selectedName=t,a(this,Te,Ts).call(this))},ye=new WeakSet,_s=function(){var t;this.isListOpened=!0,l(this,x)&&l(this,_)&&(window.addEventListener("scroll",l(this,x),!0),window.addEventListener("resize",l(this,_))),!(!l(this,d)||!this.srcElement)&&(this.appendToBody?(document.body.appendChild(l(this,d).srcElement),(t=l(this,R))==null||t.observe(this.srcElement)):this.srcElement.appendChild(l(this,d).srcElement),this.updateListPosition(),a(this,at,Ct).call(this,!0),a(this,We,Ws).call(this),a(this,Oe,Os).call(this))},ut=new WeakSet,xe=function(){var t;this.alwaysOpen||(this.isListOpened=!1,l(this,x)&&l(this,_)&&(window.removeEventListener("scroll",l(this,x),!0),window.removeEventListener("resize",l(this,_))),!l(this,d)||!this.srcElement)||!(this.appendToBody?document.body.contains(l(this,d).srcElement):this.srcElement.contains(l(this,d).srcElement))||(m(this,it,l(this,d).srcElement.scrollTop),this.appendToBody?(document.body.removeChild(l(this,d).srcElement),(t=l(this,R))==null||t.disconnect()):this.srcElement.removeChild(l(this,d).srcElement),a(this,at,Ct).call(this,!1),a(this,Ne,Ns).call(this))},_e=new WeakSet,Ks=function(t,e){if(!l(this,d)||!l(this,p))return;let s=e?"treeselect-list--top-to-body":"treeselect-list--top",i=e?"treeselect-list--bottom-to-body":"treeselect-list--bottom";t?(l(this,d).srcElement.classList.add(s),l(this,d).srcElement.classList.remove(i),l(this,p).srcElement.classList.add("treeselect-input--top"),l(this,p).srcElement.classList.remove("treeselect-input--bottom")):(l(this,d).srcElement.classList.remove(s),l(this,d).srcElement.classList.add(i),l(this,p).srcElement.classList.remove("treeselect-input--top"),l(this,p).srcElement.classList.add("treeselect-input--bottom"))},J=new WeakSet,pt=function(t){!l(this,p)||!l(this,d)||(t?(l(this,p).srcElement.classList.add("treeselect-input--focused"),l(this,d).srcElement.classList.add("treeselect-list--focused")):(l(this,p).srcElement.classList.remove("treeselect-input--focused"),l(this,d).srcElement.classList.remove("treeselect-list--focused")))},at=new WeakSet,Ct=function(t){var e,s,i,n;t?(e=l(this,p))==null||e.srcElement.classList.add("treeselect-input--opened"):(s=l(this,p))==null||s.srcElement.classList.remove("treeselect-input--opened"),this.staticList?(i=l(this,d))==null||i.srcElement.classList.add("treeselect-list--static"):(n=l(this,d))==null||n.srcElement.classList.remove("treeselect-list--static")},z=new WeakSet,ht=function(t){!l(this,x)||!l(this,_)||!l(this,E)||!l(this,B)||((!this.alwaysOpen||t)&&(window.removeEventListener("scroll",l(this,x),!0),window.removeEventListener("resize",l(this,_))),document.removeEventListener("mousedown",l(this,E),!0),document.removeEventListener("focus",l(this,E),!0),window.removeEventListener("blur",l(this,B)))},We=new WeakSet,Ws=function(){var t,e,s;let i=(t=l(this,d))==null?void 0:t.isLastFocusedElementExist();this.saveScrollPosition&&i?(e=l(this,d))==null||e.srcElement.scroll(0,l(this,it)):(s=l(this,d))==null||s.focusFirstListElement()},ct=new WeakSet,Et=function(){var t;(t=this.srcElement)==null||t.dispatchEvent(new CustomEvent("input",{detail:this.value})),this.inputCallback&&this.inputCallback(this.value)},Te=new WeakSet,Ts=function(){var t;(t=this.srcElement)==null||t.dispatchEvent(new CustomEvent("name-change",{detail:this.selectedName})),this.nameChangeCallback&&this.nameChangeCallback(this.selectedName)},Oe=new WeakSet,Os=function(){var t;this.alwaysOpen||((t=this.srcElement)==null||t.dispatchEvent(new CustomEvent("open",{detail:this.value})),this.openCallback&&this.openCallback(this.value))},Ne=new WeakSet,Ns=function(){var t;this.alwaysOpen||((t=this.srcElement)==null||t.dispatchEvent(new CustomEvent("close",{detail:this.value})),this.closeCallback&&this.closeCallback(this.value))},Ae=new WeakSet,As=function(t){var e;let s=t?.trim()??"";(e=this.srcElement)==null||e.dispatchEvent(new CustomEvent("search",{detail:s})),this.searchCallback&&this.searchCallback(s)},Be=new WeakSet,Bs=function(t,e){var s;(s=this.srcElement)==null||s.dispatchEvent(new CustomEvent("open-close-group",{detail:{groupId:t,isClosed:e}})),this.openCloseGroupCallback&&this.openCloseGroupCallback(t,e)};function Mi({state:t,name:e,options:s,searchable:i,showCount:n,placeholder:o,rtl:r,disabledBranchNode:u=!0,disabled:v=!1,isSingleSelect:k=!0,showTags:w=!0,clearable:f=!0,isIndependentNodes:b=!0,alwaysOpen:g=!1,emptyText:y,expandSelected:C=!0,grouped:$=!0,openLevel:V=0,direction:q="auto"}){return{state:t,tree:null,init(){typeof this.state=="string"&&(this.state=Number(this.state)),this.tree=new mt({id:`tree-${e}-id`,ariaLabel:`tree-${e}-label`,parentHtmlContainer:this.$refs.tree,value:this.state,options:s,searchable:i,showCount:n,placeholder:o,disabledBranchNode:u,disabled:v,isSingleSelect:k,showTags:w,clearable:f,isIndependentNodes:b,alwaysOpen:g,emptyText:y,expandSelected:C,grouped:$,openLevel:V,direction:q,rtl:r}),this.tree.srcElement.addEventListener("input",j=>{this.state=j.detail})}}}export{Mi as default}; diff --git a/resources/js/index.js b/resources/js/index.js index 4fb9c41..de42df9 100644 --- a/resources/js/index.js +++ b/resources/js/index.js @@ -26,8 +26,12 @@ export default function selectTree({ /** @type Treeselect */ tree: null, - init() { + + if (typeof this.state === 'string') { + this.state = Number(this.state); + } + this.tree = new Treeselect({ id: `tree-${name}-id`, ariaLabel: `tree-${name}-label`, diff --git a/resources/views/select-tree.blade.php b/resources/views/select-tree.blade.php index f963279..e3f63a3 100644 --- a/resources/views/select-tree.blade.php +++ b/resources/views/select-tree.blade.php @@ -20,8 +20,8 @@ @else ax-load @endif - ax-load-css="{{ FilamentAsset::getStyleHref('filament-select-tree-styles', package: 'codewithdennis/filament-select-tree') }}" - ax-load-src="{{ FilamentAsset::getAlpineComponentSrc('filament-select-tree', package: 'codewithdennis/filament-select-tree') }}" + ax-load-css="{{ FilamentAsset::getStyleHref('filament-select-tree-styles', package: 'codewithdennis/filament-select-tree') }}?v={{ time() }}" + ax-load-src="{{ FilamentAsset::getAlpineComponentSrc('filament-select-tree', package: 'codewithdennis/filament-select-tree') }}?v={{ time() }}" x-data="selectTree({ name: @js($getName()), state: $wire.{{ $applyStateBindingModifiers("\$entangle('{$getStatePath()}')") }}, diff --git a/src/SelectTree.php b/src/SelectTree.php index 57b13dd..48a3357 100644 --- a/src/SelectTree.php +++ b/src/SelectTree.php @@ -84,6 +84,11 @@ class SelectTree extends Field implements HasAffixActions protected Collection|array|null $results = null; + protected Closure|bool|null $multiple = null; + + protected Closure|array|null $prepend = null; + + protected function setUp(): void { // Load the state from relationships using a callback function. @@ -143,7 +148,7 @@ protected function setUp(): void ]); } - private function buildTree(): Collection + protected function buildTree(): Collection { // Start with two separate query builders $nullParentQuery = $this->getRelationship()->getRelated()->query()->where($this->getParentAttribute(), $this->getParentNullValue()); @@ -288,6 +293,19 @@ public function parentNullValue(int|string|null $parentNullValue = null): static return $this; } + public function multiple(Closure|bool $multiple = true): static + { + $this->multiple =$this->evaluate($multiple); + return $this; + } + + public function prepend(Closure|array|null $prepend = null): static + { + $this->prepend = $this->evaluate($prepend); + return $this; + } + + public function getRelationship(): BelongsToMany|BelongsTo { return $this->getModelInstance()->{$this->evaluate($this->relationship)}(); @@ -394,7 +412,7 @@ public function storeResults(bool $storeResults = true): static public function getTree(): Collection|array { - return $this->evaluate($this->buildTree()); + return $this->evaluate($this->buildTree()->when($this->prepend, fn(Collection $tree) => $tree->prepend($this->prepend))); } public function getResults(): Collection|array|null @@ -434,7 +452,9 @@ public function getWithCount(): bool public function getMultiple(): bool { - return $this->evaluate($this->getRelationship() instanceof BelongsToMany); + return $this->evaluate( + is_null($this->multiple) ? $this->getRelationship() instanceof BelongsToMany : $this->multiple + ); } public function getClearable(): bool From 684958c9562408fe7bf774b68ca9cb38a8c3f64e Mon Sep 17 00:00:00 2001 From: Valentin Tsanev Date: Tue, 25 Mar 2025 10:57:04 +0200 Subject: [PATCH 02/13] Formatting --- src/SelectTree.php | 505 +++++++++++++++++++++++---------------------- 1 file changed, 259 insertions(+), 246 deletions(-) diff --git a/src/SelectTree.php b/src/SelectTree.php index 48a3357..5d7cddb 100644 --- a/src/SelectTree.php +++ b/src/SelectTree.php @@ -88,71 +88,146 @@ class SelectTree extends Field implements HasAffixActions protected Closure|array|null $prepend = null; + public function relationship( + string $relationship, + string $titleAttribute, + string $parentAttribute, + ?Closure $modifyQueryUsing = null, + ?Closure $modifyChildQueryUsing = null + ): self { + $this->relationship = $relationship; + $this->titleAttribute = $titleAttribute; + $this->parentAttribute = $parentAttribute; + $this->modifyQueryUsing = $modifyQueryUsing; + $this->modifyChildQueryUsing = $modifyChildQueryUsing; - protected function setUp(): void + return $this; + } + + public function withCount(bool $withCount = true): static { - // Load the state from relationships using a callback function. - $this->loadStateFromRelationshipsUsing(static function (self $component): void { - // Get the current relationship associated with the component. - $relationship = $component->getRelationship(); + $this->withCount = $withCount; - // Check if the relationship is a BelongsToMany relationship. - if ($relationship instanceof BelongsToMany) { - // Retrieve related model instances and extract their IDs into an array. - $state = $relationship->getResults() - ->pluck($relationship->getRelatedKeyName()) - ->toArray(); + return $this; + } - // Set the component's state with the extracted IDs. - $component->state($state); - } - }); + public function direction(string $direction): static + { + $this->direction = $direction; - // Save relationships using a callback function. - $this->saveRelationshipsUsing(static function (self $component, $state) { - // Check if the component's relationship is a BelongsToMany relationship. - if ($component->getRelationship() instanceof BelongsToMany) { - // Wrap the state in a collection and convert it to an array if it's not set. - $state = Arr::wrap($state ?? []); + return $this; + } - $pivotData = $component->getPivotData(); + public function parentNullValue(int|string|null $parentNullValue = null): static + { + $this->parentNullValue = $parentNullValue; - // Sync the relationship with the provided state (IDs). - if ($pivotData === []) { - $component->getRelationship()->sync($state ?? []); + return $this; + } - return; - } + public function multiple(Closure|bool $multiple = true): static + { + $this->multiple = $multiple; + return $this; + } - // Sync the relationship with the provided state (IDs) plus pivot data. - $component->getRelationship()->syncWithPivotValues($state ?? [], $pivotData); - } - }); + public function clearable(bool $clearable = true): static + { + $this->clearable = $clearable; - $this->createOptionUsing(static function (SelectTree $component, array $data, Form $form) { - $record = $component->getRelationship()->getRelated(); - $record->fill($data); - $record->save(); + return $this; + } - $form->model($record)->saveRelationships(); + public function grouped(bool $grouped = true): static + { + $this->grouped = $grouped; - return $component->getCustomKey($record); - }); + return $this; + } - $this->dehydrated(fn (SelectTree $component): bool => ! $component->getRelationship() instanceof BelongsToMany); + public function defaultOpenLevel(Closure|int $defaultOpenLevel = 0): static + { + $this->defaultOpenLevel = $defaultOpenLevel; - $this->placeholder(static fn (SelectTree $component): ?string => $component->isDisabled() ? null : __('filament-forms::components.select.placeholder')); + return $this; + } - $this->suffixActions([ - static fn (SelectTree $component): ?Action => $component->getCreateOptionAction(), - ]); + public function expandSelected(bool $expandSelected = true): static + { + $this->expandSelected = $expandSelected; + + return $this; + } + + public function emptyLabel(string $emptyLabel): static + { + $this->noSearchResultsMessage($emptyLabel); + + return $this; + } + + public function independent(bool $independent = true): static + { + $this->independent = $independent; + + return $this; + } + + public function withKey(string $customKey): static + { + $this->customKey = $customKey; + + return $this; + } + + public function disabledOptions(Closure|array $disabledOptions): static + { + $this->disabledOptions = $disabledOptions; + + return $this; + } + + public function hiddenOptions(Closure|array $hiddenOptions): static + { + $this->hiddenOptions = $hiddenOptions; + + return $this; + } + + public function alwaysOpen(bool $alwaysOpen = true): static + { + $this->alwaysOpen = $alwaysOpen; + + return $this; + } + + public function enableBranchNode(bool $enableBranchNode = true): static + { + $this->enableBranchNode = $enableBranchNode; + + return $this; + } + + public function storeResults(bool $storeResults = true): static + { + $this->storeResults = $storeResults; + + return $this; + } + + public function getTree(): Collection|array + { + return $this->evaluate($this->buildTree()->when($this->prepend, + fn(Collection $tree) => $tree->prepend($this->evaluate($this->prepend)))); } protected function buildTree(): Collection { // Start with two separate query builders - $nullParentQuery = $this->getRelationship()->getRelated()->query()->where($this->getParentAttribute(), $this->getParentNullValue()); - $nonNullParentQuery = $this->getRelationship()->getRelated()->query()->whereNot($this->getParentAttribute(), $this->getParentNullValue()); + $nullParentQuery = $this->getRelationship()->getRelated()->query()->where($this->getParentAttribute(), + $this->getParentNullValue()); + $nonNullParentQuery = $this->getRelationship()->getRelated()->query()->whereNot($this->getParentAttribute(), + $this->getParentNullValue()); // If we're not at the root level and a modification callback is provided, apply it to null query if ($this->modifyQueryUsing) { @@ -183,6 +258,23 @@ protected function buildTree(): Collection return $this->buildTreeFromResults($combinedResults); } + public function getParentAttribute(): string + { + return $this->evaluate($this->parentAttribute); + } + + public function getParentNullValue(): null|int|string + { + return $this->evaluate($this->parentNullValue); + } + + public function withTrashed(bool $withTrashed = true): static + { + $this->withTrashed = $withTrashed; + + return $this; + } + private function buildTreeFromResults($results, $parent = null): Collection { // Assign the parent's null value to the $parent variable if it's not null @@ -199,7 +291,7 @@ private function buildTreeFromResults($results, $parent = null): Collection // Group results by their parent IDs foreach ($results as $result) { $parentId = $result->{$this->getParentAttribute()}; - if (! isset($resultMap[$parentId])) { + if (!isset($resultMap[$parentId])) { $resultMap[$parentId] = []; } $resultMap[$parentId][] = $result; @@ -222,6 +314,16 @@ private function buildTreeFromResults($results, $parent = null): Collection return $tree; } + public function getDisabledOptions(): array + { + return $this->evaluate($this->disabledOptions); + } + + public function getHiddenOptions(): array + { + return $this->evaluate($this->hiddenOptions); + } + private function buildNode($result, $resultMap, $disabledOptions, $hiddenOptions): array { $key = $this->getCustomKey($result); @@ -254,172 +356,17 @@ private function buildNode($result, $resultMap, $disabledOptions, $hiddenOptions return $node; } - public function relationship(string $relationship, string $titleAttribute, string $parentAttribute, ?Closure $modifyQueryUsing = null, ?Closure $modifyChildQueryUsing = null): self - { - $this->relationship = $relationship; - $this->titleAttribute = $titleAttribute; - $this->parentAttribute = $parentAttribute; - $this->modifyQueryUsing = $modifyQueryUsing; - $this->modifyChildQueryUsing = $modifyChildQueryUsing; - - return $this; - } - - public function withCount(bool $withCount = true): static - { - $this->withCount = $withCount; - - return $this; - } - - public function withTrashed(bool $withTrashed = true): static - { - $this->withTrashed = $withTrashed; - - return $this; - } - - public function direction(string $direction): static - { - $this->direction = $direction; - - return $this; - } - - public function parentNullValue(int|string|null $parentNullValue = null): static - { - $this->parentNullValue = $parentNullValue; - - return $this; - } - - public function multiple(Closure|bool $multiple = true): static - { - $this->multiple =$this->evaluate($multiple); - return $this; - } - - public function prepend(Closure|array|null $prepend = null): static - { - $this->prepend = $this->evaluate($prepend); - return $this; - } - - - public function getRelationship(): BelongsToMany|BelongsTo - { - return $this->getModelInstance()->{$this->evaluate($this->relationship)}(); - } - public function getTitleAttribute(): string { return $this->evaluate($this->titleAttribute); } - public function getParentAttribute(): string - { - return $this->evaluate($this->parentAttribute); - } - - public function getParentNullValue(): null|int|string - { - return $this->evaluate($this->parentNullValue); - } - - public function clearable(bool $clearable = true): static - { - $this->clearable = $clearable; - - return $this; - } - - public function grouped(bool $grouped = true): static - { - $this->grouped = $grouped; - - return $this; - } - - public function defaultOpenLevel(Closure|int $defaultOpenLevel = 0): static - { - $this->defaultOpenLevel = $defaultOpenLevel; - - return $this; - } - - public function expandSelected(bool $expandSelected = true): static - { - $this->expandSelected = $expandSelected; - - return $this; - } - - public function emptyLabel(string $emptyLabel): static - { - $this->noSearchResultsMessage($emptyLabel); - - return $this; - } - - public function independent(bool $independent = true): static - { - $this->independent = $independent; - - return $this; - } - - public function withKey(string $customKey): static - { - $this->customKey = $customKey; - - return $this; - } - - public function disabledOptions(Closure|array $disabledOptions): static - { - $this->disabledOptions = $disabledOptions; - - return $this; - } - - public function hiddenOptions(Closure|array $hiddenOptions): static - { - $this->hiddenOptions = $hiddenOptions; - - return $this; - } - - public function alwaysOpen(bool $alwaysOpen = true): static - { - $this->alwaysOpen = $alwaysOpen; - - return $this; - } - - public function enableBranchNode(bool $enableBranchNode = true): static - { - $this->enableBranchNode = $enableBranchNode; - - return $this; - } - - public function storeResults(bool $storeResults = true): static + public function prepend(Closure|array|null $prepend = null): static { - $this->storeResults = $storeResults; - + $this->prepend = $prepend; return $this; } - public function getTree(): Collection|array - { - return $this->evaluate($this->buildTree()->when($this->prepend, fn(Collection $tree) => $tree->prepend($this->prepend))); - } - - public function getResults(): Collection|array|null - { - return $this->evaluate($this->results); - } - public function getExpandSelected(): bool { return $this->evaluate($this->expandSelected); @@ -440,23 +387,11 @@ public function getIndependent(): bool return $this->evaluate($this->independent); } - public function getCustomKey($record) - { - return is_null($this->customKey) ? $record->getKey() : $record->{$this->customKey}; - } - public function getWithCount(): bool { return $this->evaluate($this->withCount); } - public function getMultiple(): bool - { - return $this->evaluate( - is_null($this->multiple) ? $this->getRelationship() instanceof BelongsToMany : $this->multiple - ); - } - public function getClearable(): bool { return $this->evaluate($this->clearable); @@ -487,46 +422,88 @@ public function getDirection(): string return $this->evaluate($this->direction); } - public function getDisabledOptions(): array + public function createOptionForm(array|Closure|null $schema): static { - return $this->evaluate($this->disabledOptions); - } + $this->createOptionActionForm = $schema; - public function getHiddenOptions(): array - { - return $this->evaluate($this->hiddenOptions); + return $this; } - public function getCreateOptionActionForm(Form $form): array|Form|null + public function createOptionModalHeading(string|Closure|null $heading): static { - return $this->evaluate($this->createOptionActionForm, ['form' => $form]); - } + $this->createOptionModalHeading = $heading; - public function hasCreateOptionActionFormSchema(): bool - { - return (bool) $this->createOptionActionForm; + return $this; } - public function getCreateOptionModalHeading(): ?string + protected function setUp(): void { - return $this->evaluate($this->createOptionModalHeading); - } + // Load the state from relationships using a callback function. + $this->loadStateFromRelationshipsUsing(static function (self $component): void { + // Get the current relationship associated with the component. + $relationship = $component->getRelationship(); - public function createOptionForm(array|Closure|null $schema): static - { - $this->createOptionActionForm = $schema; + // Check if the relationship is a BelongsToMany relationship. + if ($relationship instanceof BelongsToMany) { + // Retrieve related model instances and extract their IDs into an array. + $state = $relationship->getResults() + ->pluck($relationship->getRelatedKeyName()) + ->toArray(); - return $this; + // Set the component's state with the extracted IDs. + $component->state($state); + } + }); + + // Save relationships using a callback function. + $this->saveRelationshipsUsing(static function (self $component, $state) { + // Check if the component's relationship is a BelongsToMany relationship. + if ($component->getRelationship() instanceof BelongsToMany) { + // Wrap the state in a collection and convert it to an array if it's not set. + $state = Arr::wrap($state ?? []); + + $pivotData = $component->getPivotData(); + + // Sync the relationship with the provided state (IDs). + if ($pivotData === []) { + $component->getRelationship()->sync($state ?? []); + + return; + } + + // Sync the relationship with the provided state (IDs) plus pivot data. + $component->getRelationship()->syncWithPivotValues($state ?? [], $pivotData); + } + }); + + $this->createOptionUsing(static function (SelectTree $component, array $data, Form $form) { + $record = $component->getRelationship()->getRelated(); + $record->fill($data); + $record->save(); + + $form->model($record)->saveRelationships(); + + return $component->getCustomKey($record); + }); + + $this->dehydrated(fn(SelectTree $component): bool => !$component->getRelationship() instanceof BelongsToMany); + + $this->placeholder(static fn(SelectTree $component + ): ?string => $component->isDisabled() ? null : __('filament-forms::components.select.placeholder')); + + $this->suffixActions([ + static fn(SelectTree $component): ?Action => $component->getCreateOptionAction(), + ]); } - public function getCreateOptionActionName(): string + public function getRelationship(): BelongsToMany|BelongsTo { - return 'createOption'; + return $this->getModelInstance()->{$this->evaluate($this->relationship)}(); } - public function getCreateOptionUsing(): ?Closure + public function getResults(): Collection|array|null { - return $this->createOptionUsing; + return $this->evaluate($this->results); } public function createOptionUsing(Closure $callback): static @@ -536,13 +513,18 @@ public function createOptionUsing(Closure $callback): static return $this; } + public function getCustomKey($record) + { + return is_null($this->customKey) ? $record->getKey() : $record->{$this->customKey}; + } + public function getCreateOptionAction(): ?Action { if ($this->isDisabled()) { return null; } - if (! $this->hasCreateOptionActionFormSchema()) { + if (!$this->hasCreateOptionActionFormSchema()) { return null; } @@ -552,8 +534,14 @@ public function getCreateOptionAction(): ?Action $component->getRelationship() ? $component->getRelationship()->getModel()::class : null, )); }) - ->action(static function (Action $action, array $arguments, SelectTree $component, array $data, ComponentContainer $form) { - if (! $component->getCreateOptionUsing()) { + ->action(static function ( + Action $action, + array $arguments, + SelectTree $component, + array $data, + ComponentContainer $form + ) { + if (!$component->getCreateOptionUsing()) { throw new Exception("Select field [{$component->getStatePath()}] must have a [createOptionUsing()] closure set."); } @@ -572,7 +560,7 @@ public function getCreateOptionAction(): ?Action $component->state($state); $component->callAfterStateUpdated(); - if (! ($arguments['another'] ?? false)) { + if (!($arguments['another'] ?? false)) { return; } @@ -587,7 +575,7 @@ public function getCreateOptionAction(): ?Action ->iconButton() ->modalHeading($this->getCreateOptionModalHeading() ?? __('filament-forms::components.select.actions.create_option.modal.heading')) ->modalSubmitActionLabel(__('filament-forms::components.select.actions.create_option.modal.actions.create.label')) - ->extraModalFooterActions(fn (Action $action, SelectTree $component): array => $component->getMultiple() ? [ + ->extraModalFooterActions(fn(Action $action, SelectTree $component): array => $component->getMultiple() ? [ $action->makeModalSubmitAction('createAnother', arguments: ['another' => true]) ->label(__('filament-forms::components.select.actions.create_option.modal.actions.create_another.label')), ] : []); @@ -607,10 +595,35 @@ public function getCreateOptionAction(): ?Action return $action; } - public function createOptionModalHeading(string|Closure|null $heading): static + public function hasCreateOptionActionFormSchema(): bool { - $this->createOptionModalHeading = $heading; + return (bool) $this->createOptionActionForm; + } - return $this; + public function getCreateOptionActionName(): string + { + return 'createOption'; + } + + public function getCreateOptionActionForm(Form $form): array|Form|null + { + return $this->evaluate($this->createOptionActionForm, ['form' => $form]); + } + + public function getCreateOptionUsing(): ?Closure + { + return $this->createOptionUsing; + } + + public function getMultiple(): bool + { + return $this->evaluate( + is_null($this->multiple) ? $this->getRelationship() instanceof BelongsToMany : $this->evaluate($this->multiple) + ); + } + + public function getCreateOptionModalHeading(): ?string + { + return $this->evaluate($this->createOptionModalHeading); } } From 970b35d956505e54f830d7ab0be9879a77c4c776 Mon Sep 17 00:00:00 2001 From: Valentin Tsanev Date: Tue, 25 Mar 2025 11:12:02 +0200 Subject: [PATCH 03/13] remove version timestamp --- resources/views/select-tree.blade.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/resources/views/select-tree.blade.php b/resources/views/select-tree.blade.php index e3f63a3..f963279 100644 --- a/resources/views/select-tree.blade.php +++ b/resources/views/select-tree.blade.php @@ -20,8 +20,8 @@ @else ax-load @endif - ax-load-css="{{ FilamentAsset::getStyleHref('filament-select-tree-styles', package: 'codewithdennis/filament-select-tree') }}?v={{ time() }}" - ax-load-src="{{ FilamentAsset::getAlpineComponentSrc('filament-select-tree', package: 'codewithdennis/filament-select-tree') }}?v={{ time() }}" + ax-load-css="{{ FilamentAsset::getStyleHref('filament-select-tree-styles', package: 'codewithdennis/filament-select-tree') }}" + ax-load-src="{{ FilamentAsset::getAlpineComponentSrc('filament-select-tree', package: 'codewithdennis/filament-select-tree') }}" x-data="selectTree({ name: @js($getName()), state: $wire.{{ $applyStateBindingModifiers("\$entangle('{$getStatePath()}')") }}, From 76b64ff09e928a0b214ccf4737b2972b63e1f4e2 Mon Sep 17 00:00:00 2001 From: Valentin Tsanev Date: Tue, 25 Mar 2025 11:26:13 +0200 Subject: [PATCH 04/13] fix formatting --- src/SelectTree.php | 505 ++++++++++++++++++++++----------------------- 1 file changed, 246 insertions(+), 259 deletions(-) diff --git a/src/SelectTree.php b/src/SelectTree.php index 5d7cddb..806e0ab 100644 --- a/src/SelectTree.php +++ b/src/SelectTree.php @@ -88,146 +88,71 @@ class SelectTree extends Field implements HasAffixActions protected Closure|array|null $prepend = null; - public function relationship( - string $relationship, - string $titleAttribute, - string $parentAttribute, - ?Closure $modifyQueryUsing = null, - ?Closure $modifyChildQueryUsing = null - ): self { - $this->relationship = $relationship; - $this->titleAttribute = $titleAttribute; - $this->parentAttribute = $parentAttribute; - $this->modifyQueryUsing = $modifyQueryUsing; - $this->modifyChildQueryUsing = $modifyChildQueryUsing; - - return $this; - } - - public function withCount(bool $withCount = true): static - { - $this->withCount = $withCount; - - return $this; - } - public function direction(string $direction): static - { - $this->direction = $direction; - - return $this; - } - - public function parentNullValue(int|string|null $parentNullValue = null): static - { - $this->parentNullValue = $parentNullValue; - - return $this; - } - - public function multiple(Closure|bool $multiple = true): static - { - $this->multiple = $multiple; - return $this; - } - - public function clearable(bool $clearable = true): static - { - $this->clearable = $clearable; - - return $this; - } - - public function grouped(bool $grouped = true): static - { - $this->grouped = $grouped; - - return $this; - } - - public function defaultOpenLevel(Closure|int $defaultOpenLevel = 0): static - { - $this->defaultOpenLevel = $defaultOpenLevel; - - return $this; - } - - public function expandSelected(bool $expandSelected = true): static - { - $this->expandSelected = $expandSelected; - - return $this; - } - - public function emptyLabel(string $emptyLabel): static - { - $this->noSearchResultsMessage($emptyLabel); - - return $this; - } - - public function independent(bool $independent = true): static + protected function setUp(): void { - $this->independent = $independent; - - return $this; - } + // Load the state from relationships using a callback function. + $this->loadStateFromRelationshipsUsing(static function (self $component): void { + // Get the current relationship associated with the component. + $relationship = $component->getRelationship(); - public function withKey(string $customKey): static - { - $this->customKey = $customKey; + // Check if the relationship is a BelongsToMany relationship. + if ($relationship instanceof BelongsToMany) { + // Retrieve related model instances and extract their IDs into an array. + $state = $relationship->getResults() + ->pluck($relationship->getRelatedKeyName()) + ->toArray(); - return $this; - } + // Set the component's state with the extracted IDs. + $component->state($state); + } + }); - public function disabledOptions(Closure|array $disabledOptions): static - { - $this->disabledOptions = $disabledOptions; + // Save relationships using a callback function. + $this->saveRelationshipsUsing(static function (self $component, $state) { + // Check if the component's relationship is a BelongsToMany relationship. + if ($component->getRelationship() instanceof BelongsToMany) { + // Wrap the state in a collection and convert it to an array if it's not set. + $state = Arr::wrap($state ?? []); - return $this; - } + $pivotData = $component->getPivotData(); - public function hiddenOptions(Closure|array $hiddenOptions): static - { - $this->hiddenOptions = $hiddenOptions; + // Sync the relationship with the provided state (IDs). + if ($pivotData === []) { + $component->getRelationship()->sync($state ?? []); - return $this; - } + return; + } - public function alwaysOpen(bool $alwaysOpen = true): static - { - $this->alwaysOpen = $alwaysOpen; + // Sync the relationship with the provided state (IDs) plus pivot data. + $component->getRelationship()->syncWithPivotValues($state ?? [], $pivotData); + } + }); - return $this; - } + $this->createOptionUsing(static function (SelectTree $component, array $data, Form $form) { + $record = $component->getRelationship()->getRelated(); + $record->fill($data); + $record->save(); - public function enableBranchNode(bool $enableBranchNode = true): static - { - $this->enableBranchNode = $enableBranchNode; + $form->model($record)->saveRelationships(); - return $this; - } + return $component->getCustomKey($record); + }); - public function storeResults(bool $storeResults = true): static - { - $this->storeResults = $storeResults; + $this->dehydrated(fn (SelectTree $component): bool => ! $component->getRelationship() instanceof BelongsToMany); - return $this; - } + $this->placeholder(static fn (SelectTree $component): ?string => $component->isDisabled() ? null : __('filament-forms::components.select.placeholder')); - public function getTree(): Collection|array - { - return $this->evaluate($this->buildTree()->when($this->prepend, - fn(Collection $tree) => $tree->prepend($this->evaluate($this->prepend)))); + $this->suffixActions([ + static fn (SelectTree $component): ?Action => $component->getCreateOptionAction(), + ]); } protected function buildTree(): Collection { // Start with two separate query builders - $nullParentQuery = $this->getRelationship()->getRelated()->query()->where($this->getParentAttribute(), - $this->getParentNullValue()); - $nonNullParentQuery = $this->getRelationship()->getRelated()->query()->whereNot($this->getParentAttribute(), - $this->getParentNullValue()); + $nullParentQuery = $this->getRelationship()->getRelated()->query()->where($this->getParentAttribute(), $this->getParentNullValue()); + $nonNullParentQuery = $this->getRelationship()->getRelated()->query()->whereNot($this->getParentAttribute(), $this->getParentNullValue()); // If we're not at the root level and a modification callback is provided, apply it to null query if ($this->modifyQueryUsing) { @@ -258,23 +183,6 @@ protected function buildTree(): Collection return $this->buildTreeFromResults($combinedResults); } - public function getParentAttribute(): string - { - return $this->evaluate($this->parentAttribute); - } - - public function getParentNullValue(): null|int|string - { - return $this->evaluate($this->parentNullValue); - } - - public function withTrashed(bool $withTrashed = true): static - { - $this->withTrashed = $withTrashed; - - return $this; - } - private function buildTreeFromResults($results, $parent = null): Collection { // Assign the parent's null value to the $parent variable if it's not null @@ -291,7 +199,7 @@ private function buildTreeFromResults($results, $parent = null): Collection // Group results by their parent IDs foreach ($results as $result) { $parentId = $result->{$this->getParentAttribute()}; - if (!isset($resultMap[$parentId])) { + if (! isset($resultMap[$parentId])) { $resultMap[$parentId] = []; } $resultMap[$parentId][] = $result; @@ -314,16 +222,6 @@ private function buildTreeFromResults($results, $parent = null): Collection return $tree; } - public function getDisabledOptions(): array - { - return $this->evaluate($this->disabledOptions); - } - - public function getHiddenOptions(): array - { - return $this->evaluate($this->hiddenOptions); - } - private function buildNode($result, $resultMap, $disabledOptions, $hiddenOptions): array { $key = $this->getCustomKey($result); @@ -356,9 +254,49 @@ private function buildNode($result, $resultMap, $disabledOptions, $hiddenOptions return $node; } - public function getTitleAttribute(): string + public function relationship(string $relationship, string $titleAttribute, string $parentAttribute, ?Closure $modifyQueryUsing = null, ?Closure $modifyChildQueryUsing = null): self { - return $this->evaluate($this->titleAttribute); + $this->relationship = $relationship; + $this->titleAttribute = $titleAttribute; + $this->parentAttribute = $parentAttribute; + $this->modifyQueryUsing = $modifyQueryUsing; + $this->modifyChildQueryUsing = $modifyChildQueryUsing; + + return $this; + } + + public function withCount(bool $withCount = true): static + { + $this->withCount = $withCount; + + return $this; + } + + public function withTrashed(bool $withTrashed = true): static + { + $this->withTrashed = $withTrashed; + + return $this; + } + + public function direction(string $direction): static + { + $this->direction = $direction; + + return $this; + } + + public function parentNullValue(int|string|null $parentNullValue = null): static + { + $this->parentNullValue = $parentNullValue; + + return $this; + } + + public function multiple(Closure|bool $multiple = true): static + { + $this->multiple = $multiple; + return $this; } public function prepend(Closure|array|null $prepend = null): static @@ -367,6 +305,121 @@ public function prepend(Closure|array|null $prepend = null): static return $this; } + public function getRelationship(): BelongsToMany|BelongsTo + { + return $this->getModelInstance()->{$this->evaluate($this->relationship)}(); + } + + public function getTitleAttribute(): string + { + return $this->evaluate($this->titleAttribute); + } + + public function getParentAttribute(): string + { + return $this->evaluate($this->parentAttribute); + } + + public function getParentNullValue(): null|int|string + { + return $this->evaluate($this->parentNullValue); + } + + public function clearable(bool $clearable = true): static + { + $this->clearable = $clearable; + + return $this; + } + + public function grouped(bool $grouped = true): static + { + $this->grouped = $grouped; + + return $this; + } + + public function defaultOpenLevel(Closure|int $defaultOpenLevel = 0): static + { + $this->defaultOpenLevel = $defaultOpenLevel; + + return $this; + } + + public function expandSelected(bool $expandSelected = true): static + { + $this->expandSelected = $expandSelected; + + return $this; + } + + public function emptyLabel(string $emptyLabel): static + { + $this->noSearchResultsMessage($emptyLabel); + + return $this; + } + + public function independent(bool $independent = true): static + { + $this->independent = $independent; + + return $this; + } + + public function withKey(string $customKey): static + { + $this->customKey = $customKey; + + return $this; + } + + public function disabledOptions(Closure|array $disabledOptions): static + { + $this->disabledOptions = $disabledOptions; + + return $this; + } + + public function hiddenOptions(Closure|array $hiddenOptions): static + { + $this->hiddenOptions = $hiddenOptions; + + return $this; + } + + public function alwaysOpen(bool $alwaysOpen = true): static + { + $this->alwaysOpen = $alwaysOpen; + + return $this; + } + + public function enableBranchNode(bool $enableBranchNode = true): static + { + $this->enableBranchNode = $enableBranchNode; + + return $this; + } + + public function storeResults(bool $storeResults = true): static + { + $this->storeResults = $storeResults; + + return $this; + } + + public function getTree(): Collection|array + { + return $this->evaluate($this->buildTree()->when($this->prepend, + fn(Collection $tree) => $tree->prepend($this->evaluate($this->prepend)))); + } + + public function getResults(): Collection|array|null + { + return $this->evaluate($this->results); + } + public function getExpandSelected(): bool { return $this->evaluate($this->expandSelected); @@ -387,11 +440,23 @@ public function getIndependent(): bool return $this->evaluate($this->independent); } + public function getCustomKey($record) + { + return is_null($this->customKey) ? $record->getKey() : $record->{$this->customKey}; + } + public function getWithCount(): bool { return $this->evaluate($this->withCount); } + public function getMultiple(): bool + { + return $this->evaluate( + is_null($this->multiple) ? $this->getRelationship() instanceof BelongsToMany : $this->evaluate($this->multiple) + ); + } + public function getClearable(): bool { return $this->evaluate($this->clearable); @@ -422,88 +487,46 @@ public function getDirection(): string return $this->evaluate($this->direction); } - public function createOptionForm(array|Closure|null $schema): static + public function getDisabledOptions(): array { - $this->createOptionActionForm = $schema; - - return $this; + return $this->evaluate($this->disabledOptions); } - public function createOptionModalHeading(string|Closure|null $heading): static + public function getHiddenOptions(): array { - $this->createOptionModalHeading = $heading; - - return $this; + return $this->evaluate($this->hiddenOptions); } - protected function setUp(): void + public function getCreateOptionActionForm(Form $form): array|Form|null { - // Load the state from relationships using a callback function. - $this->loadStateFromRelationshipsUsing(static function (self $component): void { - // Get the current relationship associated with the component. - $relationship = $component->getRelationship(); - - // Check if the relationship is a BelongsToMany relationship. - if ($relationship instanceof BelongsToMany) { - // Retrieve related model instances and extract their IDs into an array. - $state = $relationship->getResults() - ->pluck($relationship->getRelatedKeyName()) - ->toArray(); - - // Set the component's state with the extracted IDs. - $component->state($state); - } - }); - - // Save relationships using a callback function. - $this->saveRelationshipsUsing(static function (self $component, $state) { - // Check if the component's relationship is a BelongsToMany relationship. - if ($component->getRelationship() instanceof BelongsToMany) { - // Wrap the state in a collection and convert it to an array if it's not set. - $state = Arr::wrap($state ?? []); - - $pivotData = $component->getPivotData(); - - // Sync the relationship with the provided state (IDs). - if ($pivotData === []) { - $component->getRelationship()->sync($state ?? []); - - return; - } - - // Sync the relationship with the provided state (IDs) plus pivot data. - $component->getRelationship()->syncWithPivotValues($state ?? [], $pivotData); - } - }); - - $this->createOptionUsing(static function (SelectTree $component, array $data, Form $form) { - $record = $component->getRelationship()->getRelated(); - $record->fill($data); - $record->save(); - - $form->model($record)->saveRelationships(); + return $this->evaluate($this->createOptionActionForm, ['form' => $form]); + } - return $component->getCustomKey($record); - }); + public function hasCreateOptionActionFormSchema(): bool + { + return (bool) $this->createOptionActionForm; + } - $this->dehydrated(fn(SelectTree $component): bool => !$component->getRelationship() instanceof BelongsToMany); + public function getCreateOptionModalHeading(): ?string + { + return $this->evaluate($this->createOptionModalHeading); + } - $this->placeholder(static fn(SelectTree $component - ): ?string => $component->isDisabled() ? null : __('filament-forms::components.select.placeholder')); + public function createOptionForm(array|Closure|null $schema): static + { + $this->createOptionActionForm = $schema; - $this->suffixActions([ - static fn(SelectTree $component): ?Action => $component->getCreateOptionAction(), - ]); + return $this; } - public function getRelationship(): BelongsToMany|BelongsTo + public function getCreateOptionActionName(): string { - return $this->getModelInstance()->{$this->evaluate($this->relationship)}(); + return 'createOption'; } - public function getResults(): Collection|array|null + public function getCreateOptionUsing(): ?Closure { - return $this->evaluate($this->results); + return $this->createOptionUsing; } public function createOptionUsing(Closure $callback): static @@ -513,18 +536,13 @@ public function createOptionUsing(Closure $callback): static return $this; } - public function getCustomKey($record) - { - return is_null($this->customKey) ? $record->getKey() : $record->{$this->customKey}; - } - public function getCreateOptionAction(): ?Action { if ($this->isDisabled()) { return null; } - if (!$this->hasCreateOptionActionFormSchema()) { + if (! $this->hasCreateOptionActionFormSchema()) { return null; } @@ -534,14 +552,8 @@ public function getCreateOptionAction(): ?Action $component->getRelationship() ? $component->getRelationship()->getModel()::class : null, )); }) - ->action(static function ( - Action $action, - array $arguments, - SelectTree $component, - array $data, - ComponentContainer $form - ) { - if (!$component->getCreateOptionUsing()) { + ->action(static function (Action $action, array $arguments, SelectTree $component, array $data, ComponentContainer $form) { + if (! $component->getCreateOptionUsing()) { throw new Exception("Select field [{$component->getStatePath()}] must have a [createOptionUsing()] closure set."); } @@ -560,7 +572,7 @@ public function getCreateOptionAction(): ?Action $component->state($state); $component->callAfterStateUpdated(); - if (!($arguments['another'] ?? false)) { + if (! ($arguments['another'] ?? false)) { return; } @@ -575,7 +587,7 @@ public function getCreateOptionAction(): ?Action ->iconButton() ->modalHeading($this->getCreateOptionModalHeading() ?? __('filament-forms::components.select.actions.create_option.modal.heading')) ->modalSubmitActionLabel(__('filament-forms::components.select.actions.create_option.modal.actions.create.label')) - ->extraModalFooterActions(fn(Action $action, SelectTree $component): array => $component->getMultiple() ? [ + ->extraModalFooterActions(fn (Action $action, SelectTree $component): array => $component->getMultiple() ? [ $action->makeModalSubmitAction('createAnother', arguments: ['another' => true]) ->label(__('filament-forms::components.select.actions.create_option.modal.actions.create_another.label')), ] : []); @@ -595,35 +607,10 @@ public function getCreateOptionAction(): ?Action return $action; } - public function hasCreateOptionActionFormSchema(): bool - { - return (bool) $this->createOptionActionForm; - } - - public function getCreateOptionActionName(): string - { - return 'createOption'; - } - - public function getCreateOptionActionForm(Form $form): array|Form|null - { - return $this->evaluate($this->createOptionActionForm, ['form' => $form]); - } - - public function getCreateOptionUsing(): ?Closure - { - return $this->createOptionUsing; - } - - public function getMultiple(): bool + public function createOptionModalHeading(string|Closure|null $heading): static { - return $this->evaluate( - is_null($this->multiple) ? $this->getRelationship() instanceof BelongsToMany : $this->evaluate($this->multiple) - ); - } + $this->createOptionModalHeading = $heading; - public function getCreateOptionModalHeading(): ?string - { - return $this->evaluate($this->createOptionModalHeading); + return $this; } } From 06c799af78e2f7887e9c3892565805ec0fe0201f Mon Sep 17 00:00:00 2001 From: Valentin Tsanev Date: Tue, 25 Mar 2025 13:35:31 +0200 Subject: [PATCH 05/13] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 78c53f8..186595c 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "codewithdennis/filament-select-tree", + "name": "lotestudio/filament-select-tree", "description": "The multi-level select field enables you to make single selections from a predefined list of options that are organized into multiple levels or depths.", "keywords": [ "CodeWithDennis", From b3a6d08f4fb8a9270c98da11b4220de472e178d7 Mon Sep 17 00:00:00 2001 From: Valentin Tsanev Date: Wed, 26 Mar 2025 12:17:20 +0200 Subject: [PATCH 06/13] Add features doc to readme --- README.md | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/README.md b/README.md index 068d2fb..9664785 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,49 @@ Now you can access the results in `disabledOptions` or `hiddenOptions` }) ``` +By default, multiple or single selection in the tree is determined by the type of the relationship. Single for `BelongsTo` multiple for `BelongsToMany`. If you want explicitly set the type of selection use: + +```PHP +->multiple(false) //or true, Closure that returns boolean +``` + +If you need to prepend an item to the tree menu use `prepend`. Method accept an array or closure. Useful when tree-select is used as a filter (see example below). + +```php +use Filament\Tables\Filters\Filter; +use Illuminate\Database\Eloquent\Builder; +use CodeWithDennis\FilamentSelectTree\SelectTree; +``` + +```php +->filters([ + Filter::make('tree') + ->form([ + SelectTree::make('category') + ->relationship('categories', 'name', 'parent_id') + ->multiple(false) + ->prepend([ + 'name'=>'Uncategorized Products', //required + 'value'=>-1, //required + 'parent' => null // optional + 'disabled' => false // optional + 'hidden' => false // optional + 'children'=>[] //optional + ]) + ]) + ->query(function (Builder $query, array $data) { + $categories= [(int) $data['category']]; + return $query->when($data['category'], function ($query, $categories) { + if($data['category']===-1){ + return $query->whereDoesntHave('categories'); + } + + return $query->whereHas('categories', fn($query) => $query->whereIn('id', $categories)); + }); + }) +]) +``` + ## Filters Use the tree in your table filters. Here's an example to show you how. From e515f15fe2385fa1189a66e5047cdcac0de7dda5 Mon Sep 17 00:00:00 2001 From: Valentin Tsanev Date: Wed, 26 Mar 2025 12:49:21 +0200 Subject: [PATCH 07/13] Edit Readme --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9664785..f057eca 100644 --- a/README.md +++ b/README.md @@ -170,13 +170,12 @@ Now you can access the results in `disabledOptions` or `hiddenOptions` }) ``` -By default, multiple or single selection in the tree is determined by the type of the relationship. Single for `BelongsTo` multiple for `BelongsToMany`. If you want explicitly set the type of selection use: +By default, the type of selection in the tree (single or multiple) is determined by the relationship type: `BelongsTo` for single selection and `BelongsToMany` for multiple selection. If you want to explicitly set the selection type, use: ```PHP ->multiple(false) //or true, Closure that returns boolean ``` - -If you need to prepend an item to the tree menu use `prepend`. Method accept an array or closure. Useful when tree-select is used as a filter (see example below). +If you need to prepend an item to the tree menu, use the `prepend` method. This method accepts an array or a closure. It is useful when the tree-select is used as a filter (see example below). ```php use Filament\Tables\Filters\Filter; @@ -190,6 +189,7 @@ use CodeWithDennis\FilamentSelectTree\SelectTree; ->form([ SelectTree::make('category') ->relationship('categories', 'name', 'parent_id') + ->enableBranchNode() ->multiple(false) ->prepend([ 'name'=>'Uncategorized Products', //required From fa0654a02336c19592e9a2222d85a15ea08706b4 Mon Sep 17 00:00:00 2001 From: Valentin Tsanev Date: Wed, 26 Mar 2025 13:23:20 +0200 Subject: [PATCH 08/13] remove convert to number --- resources/dist/filament-select-tree.js | 2 +- resources/js/index.js | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/resources/dist/filament-select-tree.js b/resources/dist/filament-select-tree.js index 8028255..c19316c 100644 --- a/resources/dist/filament-select-tree.js +++ b/resources/dist/filament-select-tree.js @@ -1 +1 @@ -var di=Object.defineProperty,ui=(t,e,s)=>e in t?di(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s,c=(t,e,s)=>(ui(t,typeof e!="symbol"?e+"":e,s),s),He=(t,e,s)=>{if(!e.has(t))throw TypeError("Cannot "+s)},l=(t,e,s)=>(He(t,e,"read from private field"),s?s.call(t):e.get(t)),h=(t,e,s)=>{if(e.has(t))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(t):e.set(t,s)},m=(t,e,s,i)=>(He(t,e,"write to private field"),i?i.call(t,s):e.set(t,s),s),a=(t,e,s)=>(He(t,e,"access private method"),s),Ve={arrowUp:'',arrowDown:'',arrowRight:'',attention:'',clear:'',cross:'',check:'',partialCheck:''},O=(t,e)=>{if(e.innerHTML="",typeof t=="string")e.innerHTML=t;else{let s=t.cloneNode(!0);e.appendChild(s)}},Ge=t=>{let e=t?{...t}:{};return Object.keys(Ve).forEach(s=>{e[s]||(e[s]=Ve[s])}),e},pi=t=>t.reduce((e,{name:s},i)=>(e+=s,il(this,L).focus(),0)}blur(){this.isOpened&&a(this,A,H).call(this),this.clearSearch(),l(this,L).blur()}updateValue(e){this.value=e,a(this,G,F).call(this),a(this,M,Y).call(this)}removeItem(e){this.value=this.value.filter(s=>s.id!==e),a(this,Q,qt).call(this),a(this,G,F).call(this),a(this,M,Y).call(this)}clear(){this.value=[],a(this,Q,qt).call(this),a(this,G,F).call(this),this.clearSearch()}openClose(){a(this,A,H).call(this)}clearSearch(){this.searchText="",this.searchCallback(""),a(this,M,Y).call(this)}};N=new WeakMap,L=new WeakMap,D=new WeakMap,S=new WeakMap,St=new WeakSet,Hs=function(){a(this,G,F).call(this),a(this,M,Y).call(this),a(this,Lt,Me).call(this)},G=new WeakSet,F=function(){if(l(this,N).innerHTML="",this.showTags){l(this,N).append(...a(this,Tt,$e).call(this));let t=pi(this.value);this.nameChangeCallback(t)}else{let t=a(this,Bt,Ue).call(this);l(this,N).appendChild(t),this.nameChangeCallback(t.innerText)}l(this,N).appendChild(l(this,L))},Lt=new WeakSet,Me=function(){let t=[];l(this,D).innerHTML="",this.clearable&&t.push(a(this,Mt,Ye).call(this)),this.isAlwaysOpened||t.push(a(this,Dt,Ke).call(this,this.isOpened)),t.length&&l(this,D).append(...t)},yt=new WeakSet,Ie=function(){if(!this.isAlwaysOpened&&l(this,S)){let t=this.isOpened?this.iconElements.arrowUp:this.iconElements.arrowDown;O(t,l(this,S))}},M=new WeakSet,Y=function(){var t;(t=this.value)!=null&&t.length?(l(this,L).removeAttribute("placeholder"),this.srcElement.classList.remove("treeselect-input--value-not-selected")):(l(this,L).setAttribute("placeholder",this.placeholder),this.srcElement.classList.add("treeselect-input--value-not-selected")),this.searchable?this.srcElement.classList.remove("treeselect-input--unsearchable"):this.srcElement.classList.add("treeselect-input--unsearchable"),this.isSingleSelect?this.srcElement.classList.add("treeselect-input--is-single-select"):this.srcElement.classList.remove("treeselect-input--is-single-select"),l(this,L).value=this.searchText},A=new WeakSet,H=function(){this.isOpened=!this.isOpened,a(this,yt,Ie).call(this),this.isOpened?this.openCallback():this.closeCallback()},xt=new WeakSet,Ps=function(t,e,s){let i=document.createElement("div");return i.classList.add("treeselect-input"),i.setAttribute("tabindex","-1"),i.addEventListener("mousedown",n=>a(this,_t,De).call(this,n)),i.addEventListener("focus",()=>this.focusCallback(),!0),i.addEventListener("blur",()=>this.blurCallback(),!0),t.appendChild(e),i.append(t,s),i},_t=new WeakSet,De=function(t){t.stopPropagation(),this.isOpened||a(this,A,H).call(this),this.focus()},Wt=new WeakSet,Vs=function(){let t=document.createElement("div");return t.classList.add("treeselect-input__tags"),t},Tt=new WeakSet,$e=function(){return this.value.map(t=>{let e=document.createElement("div");e.classList.add("treeselect-input__tags-element"),e.setAttribute("tabindex","-1"),e.setAttribute("tag-id",t.id.toString()),e.setAttribute("title",t.name);let s=a(this,Nt,je).call(this,t.name),i=a(this,At,Re).call(this);return e.addEventListener("mousedown",n=>a(this,Ot,qe).call(this,n,t.id)),e.append(s,i),e})},Ot=new WeakSet,qe=function(t,e){t.preventDefault(),t.stopPropagation(),this.removeItem(e),this.focus()},Nt=new WeakSet,je=function(t){let e=document.createElement("span");return e.classList.add("treeselect-input__tags-name"),e.textContent=t,e},At=new WeakSet,Re=function(){let t=document.createElement("span");return t.classList.add("treeselect-input__tags-cross"),O(this.iconElements.cross,t),t},Bt=new WeakSet,Ue=function(){let t=document.createElement("span");if(t.classList.add("treeselect-input__tags-count"),!this.value.length)return t.textContent="",t.setAttribute("title",""),t;let e=this.value.length===1?this.value[0].name:`${this.value.length} ${this.tagsCountText}`;return t.textContent=e,t.setAttribute("title",e),t},Ht=new WeakSet,Gs=function(){let t=document.createElement("input");return t.classList.add("treeselect-input__edit"),this.id&&t.setAttribute("id",this.id),(!this.searchable||this.disabled)&&t.setAttribute("readonly","readonly"),this.disabled&&t.setAttribute("tabindex","-1"),this.ariaLabel.length&&t.setAttribute("aria-label",this.ariaLabel),t.addEventListener("keydown",e=>a(this,Pt,ze).call(this,e)),t.addEventListener("input",e=>a(this,Vt,Fe).call(this,e,t)),t},Pt=new WeakSet,ze=function(t){t.stopPropagation();let e=t.key;e==="Backspace"&&!this.searchText.length&&this.value.length&&!this.showTags&&this.clear(),e==="Backspace"&&!this.searchText.length&&this.value.length&&this.removeItem(this.value[this.value.length-1].id),t.code==="Space"&&(!this.searchText||!this.searchable)&&a(this,A,H).call(this),(e==="Enter"||e==="ArrowDown"||e==="ArrowUp")&&t.preventDefault(),this.keydownCallback(t),e!=="Tab"&&this.focus()},Vt=new WeakSet,Fe=function(t,e){t.stopPropagation();let s=this.searchText,i=e.value.trim();if(s.length===0&&i.length===0){e.value="";return}if(this.searchable){let n=t.target.value;this.searchCallback(n),this.isOpened||a(this,A,H).call(this)}else e.value="";this.searchText=e.value},Gt=new WeakSet,Ms=function(){let t=document.createElement("div");return t.classList.add("treeselect-input__operators"),t},Mt=new WeakSet,Ye=function(){let t=document.createElement("span");return t.classList.add("treeselect-input__clear"),t.setAttribute("tabindex","-1"),O(this.iconElements.clear,t),t.addEventListener("mousedown",e=>a(this,It,Je).call(this,e)),t},It=new WeakSet,Je=function(t){t.preventDefault(),t.stopPropagation(),(this.searchText.length||this.value.length)&&this.clear(),this.focus()},Dt=new WeakSet,Ke=function(t){m(this,S,document.createElement("span")),l(this,S).classList.add("treeselect-input__arrow");let e=t?this.iconElements.arrowUp:this.iconElements.arrowDown;return O(e,l(this,S)),l(this,S).addEventListener("mousedown",s=>a(this,$t,Xe).call(this,s)),l(this,S)},$t=new WeakSet,Xe=function(t){t.stopPropagation(),t.preventDefault(),this.focus(),a(this,A,H).call(this)},Q=new WeakSet,qt=function(){this.inputCallback(this.value)};var Is=(t,e,s,i)=>{vi(e);let n=e.filter(o=>!o.disabled&&t.some(r=>r===o.id));if(s&&n.length){n[0].checked=!0;return}n.forEach(o=>{o.checked=!0;let r=Pe(o,e,i);o.checked=r})},Pe=({id:t,checked:e},s,i)=>{let n=s.find(r=>r.id===t);if(!n)return!1;if(i)return n.checked=n.disabled?!1:!!e,n.checked;let o=Ds(!!e,n,s);return $s(n,s),o},Ds=(t,e,s)=>{if(!e.isGroup)return e.checked=e.disabled?!1:!!t,e.isPartialChecked=!1,e.checked;let i=s.filter(n=>n.childOf===e.id);return!t||e.disabled||e.isPartialChecked?(e.checked=!1,e.isPartialChecked=!1,Rt(e,i,s),e.checked):qs(i,s)?js(i)?(e.checked=!1,e.isPartialChecked=!1,e.disabled=!0,e.checked):(e.checked=!1,e.isPartialChecked=!0,i.forEach(n=>{Ds(t,n,s)}),e.checked):(e.checked=!0,e.isPartialChecked=!1,Rt(e,i,s),e.checked)},$s=(t,e)=>{let s=e.find(i=>i.id===t.childOf);s&&(mi(s,e),$s(s,e))},mi=(t,e)=>{let s=kt(t,e);if(js(s)){t.checked=!1,t.isPartialChecked=!1,t.disabled=!0;return}if(ki(s)){t.checked=!0,t.isPartialChecked=!1;return}if(wi(s)){t.checked=!1,t.isPartialChecked=!0;return}t.checked=!1,t.isPartialChecked=!1},Rt=({checked:t,disabled:e},s,i)=>{s.forEach(n=>{n.disabled=!!e||!!n.disabled,n.checked=!!t&&!n.disabled,n.isPartialChecked=!1;let o=kt(n,i);Rt({checked:t,disabled:e},o,i)})},qs=(t,e)=>t.some(s=>s.disabled)?!0:t.some(s=>{if(s.isGroup){let i=kt(s,e);return qs(i,e)}return!1}),js=t=>t.every(e=>!!e.disabled),ki=t=>t.every(e=>!!e.checked),wi=t=>t.some(e=>!!e.checked||!!e.isPartialChecked),vi=t=>{t.forEach(e=>{e.checked=!1,e.isPartialChecked=!1})},fi=(t,e,s)=>{let i={level:0,groupId:""},n=Rs(t,e,i.groupId,i.level);return gi(n,s)},Rs=(t,e,s,i)=>t.reduce((n,o)=>{var r;let u=!!((r=o.children)!=null&&r.length),v=i>=e&&u,k=i>e;if(n.push({id:o.value,name:o.name,childOf:s,isGroup:u,checked:!1,isPartialChecked:!1,level:i,isClosed:v,hidden:k,disabled:o.disabled??!1}),u){let w=Rs(o.children,e,o.value,i+1);n.push(...w)}return n},[]),kt=({id:t},e)=>e.filter(s=>s.childOf===t),bi=t=>{let{ungroupedNodes:e,allGroupedNodes:s,allNodes:i}=t.reduce((o,r)=>(r.checked&&(o.allNodes.push(r),r.isGroup?o.allGroupedNodes.push(r):o.ungroupedNodes.push(r)),o),{ungroupedNodes:[],allGroupedNodes:[],allNodes:[]}),n=i.filter(o=>!s.some(({id:r})=>r===o.childOf));return{ungroupedNodes:e,groupedNodes:n,allNodes:i}},gi=(t,e)=>(t.filter(s=>!!s.disabled).forEach(({id:s})=>Pe({id:s,checked:!1},t,e)),t),wt=(t,{id:e,isClosed:s})=>{kt({id:e},t).forEach(i=>{i.hidden=s??!1,i.isGroup&&!i.isClosed&&wt(t,{id:i.id,isClosed:s})})},Ci=t=>{t.filter(e=>e.isGroup&&!e.disabled&&(e.checked||e.isPartialChecked)).forEach(e=>{e.isClosed=!1,wt(t,e)})},Ei=(t,e)=>{let s=Si(t,e);t.forEach(i=>{s.some(({id:n})=>n===i.id)?(i.isGroup&&(i.isClosed=!1,wt(t,i)),i.hidden=!1):i.hidden=!0})},Si=(t,e)=>t.reduce((s,i)=>{if(i.name.toLowerCase().includes(e.toLowerCase())){if(s.push(i),i.isGroup){let n=Us(i.id,t);s.push(...n)}if(i.childOf){let n=zs(i.childOf,t);s.push(...n)}}return s},[]),Us=(t,e)=>e.reduce((s,i)=>(i.childOf===t&&(s.push(i),i.isGroup&&s.push(...Us(i.id,e))),s),[]),zs=(t,e)=>e.reduce((s,i)=>(i.id===t&&(s.push(i),i.childOf&&s.push(...zs(i.childOf,e))),s),[]),Li=t=>{let{duplications:e}=t.reduce((s,i)=>(s.allItems.some(n=>n.toString()===i.id.toString())&&s.duplications.push(i.id),s.allItems.push(i.id),s),{duplications:[],allItems:[]});e.length&&console.error(`Validation: You have duplicated values: ${e.join(", ")}! You should use unique values.`)},yi=(t,e,s,i,n,o,r,u,v,k)=>{Is(t,e,n,v),u&&r&&Ci(e),ot(e,s,i,o,k)},ot=(t,e,s,i,n)=>{t.forEach(o=>{let r=e.querySelector(`[input-id="${o.id}"]`),u=W(r);r.checked=o.checked,xi(o,u,i),_i(o,u),Wi(o,u),Ti(o,u,s),Oi(o,u),Ai(o,u,t,n),Ni(o,r,s)}),Bi(t,e)},xi=(t,e,s)=>{t.checked?e.classList.add("treeselect-list__item--checked"):e.classList.remove("treeselect-list__item--checked"),Array.isArray(s)&&s[0]===t.id&&!t.disabled?e.classList.add("treeselect-list__item--single-selected"):e.classList.remove("treeselect-list__item--single-selected")},_i=(t,e)=>{t.isPartialChecked?e.classList.add("treeselect-list__item--partial-checked"):e.classList.remove("treeselect-list__item--partial-checked")},Wi=(t,e)=>{t.disabled?e.classList.add("treeselect-list__item--disabled"):e.classList.remove("treeselect-list__item--disabled")},Ti=(t,e,s)=>{if(t.isGroup){let i=e.querySelector(".treeselect-list__item-icon"),n=t.isClosed?s.arrowRight:s.arrowDown;O(n,i),t.isClosed?e.classList.add("treeselect-list__item--closed"):e.classList.remove("treeselect-list__item--closed")}},Oi=(t,e)=>{t.hidden?e.classList.add("treeselect-list__item--hidden"):e.classList.remove("treeselect-list__item--hidden")},Ni=(t,e,s)=>{let i=e.parentNode.querySelector(".treeselect-list__item-checkbox-icon");t.checked?O(s.check,i):t.isPartialChecked?O(s.partialCheck,i):i.innerHTML=""},Ai=(t,e,s,i)=>{let n=t.level===0,o=20,r=5;if(n){let u=s.some(w=>w.isGroup&&w.level===t.level),v=!t.isGroup&&u?`${o}px`:`${r}px`,k=t.isGroup?"0":v;i?e.style.paddingRight=k:e.style.paddingLeft=k}else{let u=t.isGroup?`${t.level*o}px`:`${t.level*o+o}px`;i?e.style.paddingRight=u:e.style.paddingLeft=u}e.setAttribute("level",t.level.toString()),e.setAttribute("group",t.isGroup.toString())},Bi=(t,e)=>{let s=t.some(n=>!n.hidden),i=e.querySelector(".treeselect-list__empty");s?i.classList.add("treeselect-list__empty--hidden"):i.classList.remove("treeselect-list__empty--hidden")},W=t=>t.parentNode.parentNode,Qe=(t,e)=>e.find(s=>s.id.toString()===t),Hi=t=>W(t).querySelector(".treeselect-list__item-icon"),Pi=(t,e)=>{e&&Object.keys(e).forEach(s=>{let i=e[s];typeof i=="string"&&t.setAttribute(s,i)})},I,P,T,Z,Ut,Fs,zt,Ys,Ft,Js,Yt,Ze,Jt,ts,Kt,es,tt,vt,Xt,ss,Qt,is,Zt,ls,et,ft,te,ns,ee,as,se,hs,ie,cs,le,os,ne,rs,ae,ds,he,us,ce,ps,oe,ms,re,ks,st,bt,rt,de,ue,ws,pe=class{constructor({options:e,value:s,openLevel:i,listSlotHtmlComponent:n,emptyText:o,isSingleSelect:r,iconElements:u,showCount:v,disabledBranchNode:k,expandSelected:w,isIndependentNodes:f,rtl:b,inputCallback:g,arrowClickCallback:y,mouseupCallback:C}){h(this,Ut),h(this,zt),h(this,Ft),h(this,Yt),h(this,Jt),h(this,Kt),h(this,tt),h(this,Xt),h(this,Qt),h(this,Zt),h(this,et),h(this,te),h(this,ee),h(this,se),h(this,ie),h(this,le),h(this,ne),h(this,ae),h(this,he),h(this,ce),h(this,oe),h(this,re),h(this,st),h(this,rt),h(this,ue),c(this,"options"),c(this,"value"),c(this,"openLevel"),c(this,"listSlotHtmlComponent"),c(this,"emptyText"),c(this,"isSingleSelect"),c(this,"showCount"),c(this,"disabledBranchNode"),c(this,"expandSelected"),c(this,"isIndependentNodes"),c(this,"rtl"),c(this,"iconElements"),c(this,"searchText"),c(this,"flattedOptions"),c(this,"flattedOptionsBeforeSearch"),c(this,"selectedNodes"),c(this,"srcElement"),c(this,"inputCallback"),c(this,"arrowClickCallback"),c(this,"mouseupCallback"),h(this,I,null),h(this,P,!0),h(this,T,[]),h(this,Z,!0),this.options=e,this.value=s,this.openLevel=i??0,this.listSlotHtmlComponent=n??null,this.emptyText=o??"No results found...",this.isSingleSelect=r??!1,this.showCount=v??!1,this.disabledBranchNode=k??!1,this.expandSelected=w??!1,this.isIndependentNodes=f??!1,this.rtl=b??!1,this.iconElements=u,this.searchText="",this.flattedOptions=fi(this.options,this.openLevel,this.isIndependentNodes),this.flattedOptionsBeforeSearch=this.flattedOptions,this.selectedNodes={nodes:[],groupedNodes:[],allNodes:[]},this.srcElement=a(this,Ft,Js).call(this),this.inputCallback=g,this.arrowClickCallback=y,this.mouseupCallback=C,Li(this.flattedOptions)}updateValue(e){this.value=e,m(this,T,this.isSingleSelect?this.value:[]),yi(e,this.flattedOptions,this.srcElement,this.iconElements,this.isSingleSelect,l(this,T),this.expandSelected,l(this,Z),this.isIndependentNodes,this.rtl),m(this,Z,!1),a(this,rt,de).call(this)}updateSearchValue(e){if(e===this.searchText)return;let s=this.searchText===""&&e!=="";this.searchText=e,s&&(this.flattedOptionsBeforeSearch=JSON.parse(JSON.stringify(this.flattedOptions))),this.searchText===""&&(this.flattedOptions=this.flattedOptionsBeforeSearch.map(i=>{let n=this.flattedOptions.find(o=>o.id===i.id);return n.isClosed=i.isClosed,n.hidden=i.hidden,n}),this.flattedOptionsBeforeSearch=[]),this.searchText&&Ei(this.flattedOptions,e),ot(this.flattedOptions,this.srcElement,this.iconElements,l(this,T),this.rtl),this.focusFirstListElement()}callKeyAction(e){m(this,P,!1);let s=this.srcElement.querySelector(".treeselect-list__item--focused");if(s?.classList.contains("treeselect-list__item--hidden"))return;let i=e.key;i==="Enter"&&s&&s.dispatchEvent(new Event("mousedown")),(i==="ArrowLeft"||i==="ArrowRight")&&a(this,Ut,Fs).call(this,s,e),(i==="ArrowDown"||i==="ArrowUp")&&a(this,zt,Ys).call(this,s,i)}focusFirstListElement(){let e="treeselect-list__item--focused",s=this.srcElement.querySelector(`.${e}`),i=Array.from(this.srcElement.querySelectorAll(".treeselect-list__item-checkbox")).filter(n=>window.getComputedStyle(W(n)).display!=="none");i.length&&(s&&s.classList.remove(e),W(i[0]).classList.add(e))}isLastFocusedElementExist(){return!!l(this,I)}};I=new WeakMap,P=new WeakMap,T=new WeakMap,Z=new WeakMap,Ut=new WeakSet,Fs=function(t,e){if(!t)return;let s=e.key,i=t.querySelector(".treeselect-list__item-checkbox").getAttribute("input-id"),n=Qe(i,this.flattedOptions),o=t.querySelector(".treeselect-list__item-icon");s==="ArrowLeft"&&!n.isClosed&&n.isGroup&&(o.dispatchEvent(new Event("mousedown")),e.preventDefault()),s==="ArrowRight"&&n.isClosed&&n.isGroup&&(o.dispatchEvent(new Event("mousedown")),e.preventDefault())},zt=new WeakSet,Ys=function(t,e){var s;let i=Array.from(this.srcElement.querySelectorAll(".treeselect-list__item-checkbox")).filter(n=>window.getComputedStyle(W(n)).display!=="none");if(i.length)if(!t)W(i[0]).classList.add("treeselect-list__item--focused");else{let n=i.findIndex(g=>W(g).classList.contains("treeselect-list__item--focused"));W(i[n]).classList.remove("treeselect-list__item--focused");let o=e==="ArrowDown"?n+1:n-1,r=e==="ArrowDown"?0:i.length-1,u=i[o]??i[r],v=!i[o],k=W(u);k.classList.add("treeselect-list__item--focused");let w=this.srcElement.getBoundingClientRect(),f=k.getBoundingClientRect();if(v&&e==="ArrowDown"){this.srcElement.scroll(0,0);return}if(v&&e==="ArrowUp"){this.srcElement.scroll(0,this.srcElement.scrollHeight);return}let b=((s=this.listSlotHtmlComponent)==null?void 0:s.clientHeight)??0;if(w.y+w.heightf.y){this.srcElement.scroll(0,this.srcElement.scrollTop-f.height);return}}},Ft=new WeakSet,Js=function(){let t=a(this,Yt,Ze).call(this),e=a(this,tt,vt).call(this,this.options);t.append(...e);let s=a(this,Qt,is).call(this);t.append(s);let i=a(this,Xt,ss).call(this);return i&&t.append(i),t},Yt=new WeakSet,Ze=function(){let t=document.createElement("div");return t.classList.add("treeselect-list"),this.isSingleSelect&&t.classList.add("treeselect-list--single-select"),this.disabledBranchNode&&t.classList.add("treeselect-list--disabled-branch-node"),t.addEventListener("mouseout",e=>a(this,Jt,ts).call(this,e)),t.addEventListener("mousemove",()=>a(this,Kt,es).call(this)),t.addEventListener("mouseup",()=>this.mouseupCallback(),!0),t},Jt=new WeakSet,ts=function(t){t.stopPropagation(),l(this,I)&&l(this,P)&&l(this,I).classList.add("treeselect-list__item--focused")},Kt=new WeakSet,es=function(){m(this,P,!0)},tt=new WeakSet,vt=function(t){return t.reduce((e,s)=>{var i;if((i=s.children)!=null&&i.length){let o=a(this,Zt,ls).call(this,s),r=a(this,tt,vt).call(this,s.children);return o.append(...r),e.push(o),e}let n=a(this,et,ft).call(this,s,!1);return e.push(n),e},[])},Xt=new WeakSet,ss=function(){if(!this.listSlotHtmlComponent)return null;let t=document.createElement("div");return t.classList.add("treeselect-list__slot"),t.appendChild(this.listSlotHtmlComponent),t},Qt=new WeakSet,is=function(){let t=document.createElement("div");t.classList.add("treeselect-list__empty"),t.setAttribute("title",this.emptyText);let e=document.createElement("span");e.classList.add("treeselect-list__empty-icon"),O(this.iconElements.attention,e);let s=document.createElement("span");return s.classList.add("treeselect-list__empty-text"),s.textContent=this.emptyText,t.append(e,s),t},Zt=new WeakSet,ls=function(t){let e=document.createElement("div");e.setAttribute("group-container-id",t.value.toString()),e.classList.add("treeselect-list__group-container");let s=a(this,et,ft).call(this,t,!0);return e.appendChild(s),e},et=new WeakSet,ft=function(t,e){let s=a(this,te,ns).call(this,t);if(e){let o=a(this,le,os).call(this);s.appendChild(o),s.classList.add("treeselect-list__item--group")}let i=a(this,ae,ds).call(this,t),n=a(this,he,us).call(this,t,e);return s.append(i,n),s},te=new WeakSet,ns=function(t){let e=document.createElement("div");return Pi(e,t.htmlAttr),e.setAttribute("tabindex","-1"),e.setAttribute("title",t.name),e.classList.add("treeselect-list__item"),e.addEventListener("mouseover",()=>a(this,ee,as).call(this,e),!0),e.addEventListener("mouseout",()=>a(this,se,hs).call(this,e),!0),e.addEventListener("mousedown",s=>a(this,ie,cs).call(this,s,t)),e},ee=new WeakSet,as=function(t){l(this,P)&&a(this,st,bt).call(this,!0,t)},se=new WeakSet,hs=function(t){l(this,P)&&(a(this,st,bt).call(this,!1,t),m(this,I,t))},ie=new WeakSet,cs=function(t,e){var s;if(t.preventDefault(),t.stopPropagation(),(s=this.flattedOptions.find(n=>n.id===e.value))==null?void 0:s.disabled)return;let i=t.target.querySelector(".treeselect-list__item-checkbox");i.checked=!i.checked,a(this,oe,ms).call(this,i,e)},le=new WeakSet,os=function(){let t=document.createElement("span");return t.setAttribute("tabindex","-1"),t.classList.add("treeselect-list__item-icon"),O(this.iconElements.arrowDown,t),t.addEventListener("mousedown",e=>a(this,ne,rs).call(this,e)),t},ne=new WeakSet,rs=function(t){t.preventDefault(),t.stopPropagation(),a(this,re,ks).call(this,t)},ae=new WeakSet,ds=function(t){let e=document.createElement("div");e.classList.add("treeselect-list__item-checkbox-container");let s=document.createElement("span");s.classList.add("treeselect-list__item-checkbox-icon"),s.innerHTML="";let i=document.createElement("input");return i.setAttribute("tabindex","-1"),i.setAttribute("type","checkbox"),i.setAttribute("input-id",t.value.toString()),i.classList.add("treeselect-list__item-checkbox"),e.append(s,i),e},he=new WeakSet,us=function(t,e){let s=document.createElement("label");if(s.textContent=t.name,s.classList.add("treeselect-list__item-label"),e&&this.showCount){let i=a(this,ce,ps).call(this,t);s.appendChild(i)}return s},ce=new WeakSet,ps=function(t){let e=document.createElement("span"),s=this.flattedOptions.filter(i=>i.childOf===t.value);return e.textContent=`(${s.length})`,e.classList.add("treeselect-list__item-label-counter"),e},oe=new WeakSet,ms=function(t,e){let s=this.flattedOptions.find(i=>i.id===e.value);if(s){if(s!=null&&s.isGroup&&this.disabledBranchNode){let i=Hi(t);i?.dispatchEvent(new Event("mousedown"));return}if(this.isSingleSelect){let[i]=l(this,T);if(s.id===i)return;m(this,T,[s.id]),Is([s.id],this.flattedOptions,this.isSingleSelect,this.isIndependentNodes)}else{s.checked=t.checked;let i=Pe(s,this.flattedOptions,this.isIndependentNodes);t.checked=i}ot(this.flattedOptions,this.srcElement,this.iconElements,l(this,T),this.rtl),a(this,ue,ws).call(this)}},re=new WeakSet,ks=function(t){var e,s;let i=(s=(e=t.target)==null?void 0:e.parentNode)==null?void 0:s.querySelector("[input-id]"),n=i?.getAttribute("input-id")??null,o=Qe(n,this.flattedOptions);o&&(o.isClosed=!o.isClosed,wt(this.flattedOptions,o),ot(this.flattedOptions,this.srcElement,this.iconElements,l(this,T),this.rtl),this.arrowClickCallback(o.id,o.isClosed))},st=new WeakSet,bt=function(t,e){let s="treeselect-list__item--focused";if(t){let i=Array.from(this.srcElement.querySelectorAll(`.${s}`));i.length&&i.forEach(n=>n.classList.remove(s)),e.classList.add(s)}else e.classList.remove(s)},rt=new WeakSet,de=function(){let{ungroupedNodes:t,groupedNodes:e,allNodes:s}=bi(this.flattedOptions);this.selectedNodes={nodes:t,groupedNodes:e,allNodes:s}},ue=new WeakSet,ws=function(){a(this,rt,de).call(this),this.inputCallback(this.selectedNodes),this.value=this.selectedNodes.nodes.map(t=>t.id)};var vs=({parentHtmlContainer:t,staticList:e,appendToBody:s,isSingleSelect:i,value:n,direction:o})=>{t||console.error("Validation: parentHtmlContainer prop is required!"),e&&s&&console.error("Validation: You should set staticList to false if you use appendToBody!"),i&&Array.isArray(n)&&console.error("Validation: if you use isSingleSelect prop, you should pass a single value!"),!i&&!Array.isArray(n)&&console.error("Validation: you should pass an array as a value!"),o&&o!=="auto"&&o!=="bottom"&&o!=="top"&&console.error("Validation: you should pass (auto | top | bottom | undefined) as a value for the direction prop!")},X=t=>t.map(e=>e.id),Vi=t=>t?Array.isArray(t)?t:[t]:[],Gi=(t,e)=>{if(e){let[s]=t;return s??null}return t},d,p,R,it,U,x,_,E,B,lt,me,nt,gt,ke,fs,we,bs,ve,gs,fe,Cs,be,Es,ge,Ss,dt,Ce,Ee,Ls,Se,ys,Le,xs,ye,_s,ut,xe,_e,Ks,J,pt,at,Ct,z,ht,We,Ws,ct,Et,Te,Ts,Oe,Os,Ne,Ns,Ae,As,Be,Bs,mt=class{constructor({parentHtmlContainer:e,value:s,options:i,openLevel:n,appendToBody:o,alwaysOpen:r,showTags:u,tagsCountText:v,clearable:k,searchable:w,placeholder:f,grouped:b,isGroupedValue:g,listSlotHtmlComponent:y,disabled:C,emptyText:$,staticList:V,id:q,ariaLabel:j,isSingleSelect:K,showCount:Xs,disabledBranchNode:Qs,direction:Zs,expandSelected:ti,saveScrollPosition:ei,isIndependentNodes:si,rtl:ii,iconElements:li,inputCallback:ni,openCallback:ai,closeCallback:hi,nameChangeCallback:ci,searchCallback:oi,openCloseGroupCallback:ri}){h(this,lt),h(this,nt),h(this,ke),h(this,we),h(this,ve),h(this,fe),h(this,be),h(this,ge),h(this,dt),h(this,Ee),h(this,Se),h(this,Le),h(this,ye),h(this,ut),h(this,_e),h(this,J),h(this,at),h(this,z),h(this,We),h(this,ct),h(this,Te),h(this,Oe),h(this,Ne),h(this,Ae),h(this,Be),c(this,"parentHtmlContainer"),c(this,"value"),c(this,"options"),c(this,"openLevel"),c(this,"appendToBody"),c(this,"alwaysOpen"),c(this,"showTags"),c(this,"tagsCountText"),c(this,"clearable"),c(this,"searchable"),c(this,"placeholder"),c(this,"grouped"),c(this,"isGroupedValue"),c(this,"listSlotHtmlComponent"),c(this,"disabled"),c(this,"emptyText"),c(this,"staticList"),c(this,"id"),c(this,"ariaLabel"),c(this,"isSingleSelect"),c(this,"showCount"),c(this,"disabledBranchNode"),c(this,"direction"),c(this,"expandSelected"),c(this,"saveScrollPosition"),c(this,"isIndependentNodes"),c(this,"rtl"),c(this,"iconElements"),c(this,"inputCallback"),c(this,"openCallback"),c(this,"closeCallback"),c(this,"nameChangeCallback"),c(this,"searchCallback"),c(this,"openCloseGroupCallback"),c(this,"ungroupedValue"),c(this,"groupedValue"),c(this,"allValue"),c(this,"isListOpened"),c(this,"selectedName"),c(this,"srcElement"),h(this,d,null),h(this,p,null),h(this,R,null),h(this,it,0),h(this,U,0),h(this,x,null),h(this,_,null),h(this,E,null),h(this,B,null),vs({parentHtmlContainer:e,value:s,staticList:V,appendToBody:o,isSingleSelect:K}),this.parentHtmlContainer=e,this.value=[],this.options=i??[],this.openLevel=n??0,this.appendToBody=o??!1,this.alwaysOpen=!!(r&&!C),this.showTags=u??!0,this.tagsCountText=v??"elements selected",this.clearable=k??!0,this.searchable=w??!0,this.placeholder=f??"Search...",this.grouped=b??!0,this.isGroupedValue=g??!1,this.listSlotHtmlComponent=y??null,this.disabled=C??!1,this.emptyText=$??"No results found...",this.staticList=!!(V&&!this.appendToBody),this.id=q??"",this.ariaLabel=j??"",this.isSingleSelect=K??!1,this.showCount=Xs??!1,this.disabledBranchNode=Qs??!1,this.direction=Zs??"auto",this.expandSelected=ti??!1,this.saveScrollPosition=ei??!0,this.isIndependentNodes=si??!1,this.rtl=ii??!1,this.iconElements=Ge(li),this.inputCallback=ni,this.openCallback=ai,this.closeCallback=hi,this.nameChangeCallback=ci,this.searchCallback=oi,this.openCloseGroupCallback=ri,this.ungroupedValue=[],this.groupedValue=[],this.allValue=[],this.isListOpened=!1,this.selectedName="",this.srcElement=null,a(this,lt,me).call(this,s)}mount(){vs({parentHtmlContainer:this.parentHtmlContainer,value:this.value,staticList:this.staticList,appendToBody:this.appendToBody,isSingleSelect:this.isSingleSelect}),this.iconElements=Ge(this.iconElements),a(this,lt,me).call(this,this.value)}updateValue(e){let s=Vi(e),i=l(this,d);i&&(i.updateValue(s),a(this,dt,Ce).call(this,i?.selectedNodes))}destroy(){this.srcElement&&(a(this,ut,xe).call(this),this.srcElement.innerHTML="",this.srcElement=null,a(this,z,ht).call(this,!0))}focus(){l(this,p)&&l(this,p).focus()}toggleOpenClose(){l(this,p)&&(l(this,p).openClose(),l(this,p).focus())}scrollWindowHandler(){this.updateListPosition()}focusWindowHandler(e){var s,i,n;(s=this.srcElement)!=null&&s.contains(e.target)||(i=l(this,d))!=null&&i.srcElement.contains(e.target)||((n=l(this,p))==null||n.blur(),a(this,z,ht).call(this,!1),a(this,J,pt).call(this,!1))}blurWindowHandler(){var e;(e=l(this,p))==null||e.blur(),a(this,z,ht).call(this,!1),a(this,J,pt).call(this,!1)}updateListPosition(){var e;let s=this.srcElement,i=(e=l(this,d))==null?void 0:e.srcElement;if(!s||!i)return;let{height:n}=i.getBoundingClientRect(),{x:o,y:r,height:u,width:v}=s.getBoundingClientRect(),k=window.innerHeight,w=r,f=k-r-u,b=w>f&&w>=n&&fa(this,Ee,Ls).call(this,i),arrowClickCallback:(i,n)=>a(this,Se,ys).call(this,i,n),mouseupCallback:()=>{var i;return(i=l(this,p))==null?void 0:i.focus()}}),s=new jt({value:[],showTags:this.showTags,tagsCountText:this.tagsCountText,clearable:this.clearable,isAlwaysOpened:this.alwaysOpen,searchable:this.searchable,placeholder:this.placeholder,disabled:this.disabled,isSingleSelect:this.isSingleSelect,id:this.id,ariaLabel:this.ariaLabel,iconElements:this.iconElements,inputCallback:i=>a(this,we,bs).call(this,i),searchCallback:i=>a(this,fe,Cs).call(this,i),openCallback:()=>a(this,ye,_s).call(this),closeCallback:()=>a(this,ut,xe).call(this),keydownCallback:i=>a(this,ve,gs).call(this,i),focusCallback:()=>a(this,be,Es).call(this),blurCallback:()=>a(this,ge,Ss).call(this),nameChangeCallback:i=>a(this,Le,xs).call(this,i)});return this.appendToBody&&m(this,R,new ResizeObserver(()=>this.updateListPosition())),t.append(s.srcElement),{container:t,list:e,input:s}},we=new WeakSet,bs=function(t){var e,s;let i=X(t);(e=l(this,d))==null||e.updateValue(i);let n=((s=l(this,d))==null?void 0:s.selectedNodes)??{};a(this,nt,gt).call(this,n),a(this,ct,Et).call(this)},ve=new WeakSet,gs=function(t){var e;this.isListOpened&&((e=l(this,d))==null||e.callKeyAction(t))},fe=new WeakSet,Cs=function(t){l(this,U)&&clearTimeout(l(this,U)),m(this,U,window.setTimeout(()=>{var e;(e=l(this,d))==null||e.updateSearchValue(t),this.updateListPosition()},350)),a(this,Ae,As).call(this,t)},be=new WeakSet,Es=function(){a(this,J,pt).call(this,!0),l(this,E)&&l(this,E)&&l(this,B)&&(document.addEventListener("mousedown",l(this,E),!0),document.addEventListener("focus",l(this,E),!0),window.addEventListener("blur",l(this,B)))},ge=new WeakSet,Ss=function(){setTimeout(()=>{var t,e;let s=(t=l(this,p))==null?void 0:t.srcElement.contains(document.activeElement),i=(e=l(this,d))==null?void 0:e.srcElement.contains(document.activeElement);!s&&!i&&this.blurWindowHandler()},1)},dt=new WeakSet,Ce=function(t){var e;if(!t)return;let s=[];this.isIndependentNodes||this.isSingleSelect?s=t.allNodes:this.grouped?s=t.groupedNodes:s=t.nodes,(e=l(this,p))==null||e.updateValue(s),a(this,nt,gt).call(this,t)},Ee=new WeakSet,Ls=function(t){var e,s,i;a(this,dt,Ce).call(this,t),this.isSingleSelect&&!this.alwaysOpen&&((e=l(this,p))==null||e.openClose(),(s=l(this,p))==null||s.clearSearch()),(i=l(this,p))==null||i.focus(),a(this,ct,Et).call(this)},Se=new WeakSet,ys=function(t,e){var s;(s=l(this,p))==null||s.focus(),this.updateListPosition(),a(this,Be,Bs).call(this,t,e)},Le=new WeakSet,xs=function(t){this.selectedName!==t&&(this.selectedName=t,a(this,Te,Ts).call(this))},ye=new WeakSet,_s=function(){var t;this.isListOpened=!0,l(this,x)&&l(this,_)&&(window.addEventListener("scroll",l(this,x),!0),window.addEventListener("resize",l(this,_))),!(!l(this,d)||!this.srcElement)&&(this.appendToBody?(document.body.appendChild(l(this,d).srcElement),(t=l(this,R))==null||t.observe(this.srcElement)):this.srcElement.appendChild(l(this,d).srcElement),this.updateListPosition(),a(this,at,Ct).call(this,!0),a(this,We,Ws).call(this),a(this,Oe,Os).call(this))},ut=new WeakSet,xe=function(){var t;this.alwaysOpen||(this.isListOpened=!1,l(this,x)&&l(this,_)&&(window.removeEventListener("scroll",l(this,x),!0),window.removeEventListener("resize",l(this,_))),!l(this,d)||!this.srcElement)||!(this.appendToBody?document.body.contains(l(this,d).srcElement):this.srcElement.contains(l(this,d).srcElement))||(m(this,it,l(this,d).srcElement.scrollTop),this.appendToBody?(document.body.removeChild(l(this,d).srcElement),(t=l(this,R))==null||t.disconnect()):this.srcElement.removeChild(l(this,d).srcElement),a(this,at,Ct).call(this,!1),a(this,Ne,Ns).call(this))},_e=new WeakSet,Ks=function(t,e){if(!l(this,d)||!l(this,p))return;let s=e?"treeselect-list--top-to-body":"treeselect-list--top",i=e?"treeselect-list--bottom-to-body":"treeselect-list--bottom";t?(l(this,d).srcElement.classList.add(s),l(this,d).srcElement.classList.remove(i),l(this,p).srcElement.classList.add("treeselect-input--top"),l(this,p).srcElement.classList.remove("treeselect-input--bottom")):(l(this,d).srcElement.classList.remove(s),l(this,d).srcElement.classList.add(i),l(this,p).srcElement.classList.remove("treeselect-input--top"),l(this,p).srcElement.classList.add("treeselect-input--bottom"))},J=new WeakSet,pt=function(t){!l(this,p)||!l(this,d)||(t?(l(this,p).srcElement.classList.add("treeselect-input--focused"),l(this,d).srcElement.classList.add("treeselect-list--focused")):(l(this,p).srcElement.classList.remove("treeselect-input--focused"),l(this,d).srcElement.classList.remove("treeselect-list--focused")))},at=new WeakSet,Ct=function(t){var e,s,i,n;t?(e=l(this,p))==null||e.srcElement.classList.add("treeselect-input--opened"):(s=l(this,p))==null||s.srcElement.classList.remove("treeselect-input--opened"),this.staticList?(i=l(this,d))==null||i.srcElement.classList.add("treeselect-list--static"):(n=l(this,d))==null||n.srcElement.classList.remove("treeselect-list--static")},z=new WeakSet,ht=function(t){!l(this,x)||!l(this,_)||!l(this,E)||!l(this,B)||((!this.alwaysOpen||t)&&(window.removeEventListener("scroll",l(this,x),!0),window.removeEventListener("resize",l(this,_))),document.removeEventListener("mousedown",l(this,E),!0),document.removeEventListener("focus",l(this,E),!0),window.removeEventListener("blur",l(this,B)))},We=new WeakSet,Ws=function(){var t,e,s;let i=(t=l(this,d))==null?void 0:t.isLastFocusedElementExist();this.saveScrollPosition&&i?(e=l(this,d))==null||e.srcElement.scroll(0,l(this,it)):(s=l(this,d))==null||s.focusFirstListElement()},ct=new WeakSet,Et=function(){var t;(t=this.srcElement)==null||t.dispatchEvent(new CustomEvent("input",{detail:this.value})),this.inputCallback&&this.inputCallback(this.value)},Te=new WeakSet,Ts=function(){var t;(t=this.srcElement)==null||t.dispatchEvent(new CustomEvent("name-change",{detail:this.selectedName})),this.nameChangeCallback&&this.nameChangeCallback(this.selectedName)},Oe=new WeakSet,Os=function(){var t;this.alwaysOpen||((t=this.srcElement)==null||t.dispatchEvent(new CustomEvent("open",{detail:this.value})),this.openCallback&&this.openCallback(this.value))},Ne=new WeakSet,Ns=function(){var t;this.alwaysOpen||((t=this.srcElement)==null||t.dispatchEvent(new CustomEvent("close",{detail:this.value})),this.closeCallback&&this.closeCallback(this.value))},Ae=new WeakSet,As=function(t){var e;let s=t?.trim()??"";(e=this.srcElement)==null||e.dispatchEvent(new CustomEvent("search",{detail:s})),this.searchCallback&&this.searchCallback(s)},Be=new WeakSet,Bs=function(t,e){var s;(s=this.srcElement)==null||s.dispatchEvent(new CustomEvent("open-close-group",{detail:{groupId:t,isClosed:e}})),this.openCloseGroupCallback&&this.openCloseGroupCallback(t,e)};function Mi({state:t,name:e,options:s,searchable:i,showCount:n,placeholder:o,rtl:r,disabledBranchNode:u=!0,disabled:v=!1,isSingleSelect:k=!0,showTags:w=!0,clearable:f=!0,isIndependentNodes:b=!0,alwaysOpen:g=!1,emptyText:y,expandSelected:C=!0,grouped:$=!0,openLevel:V=0,direction:q="auto"}){return{state:t,tree:null,init(){typeof this.state=="string"&&(this.state=Number(this.state)),this.tree=new mt({id:`tree-${e}-id`,ariaLabel:`tree-${e}-label`,parentHtmlContainer:this.$refs.tree,value:this.state,options:s,searchable:i,showCount:n,placeholder:o,disabledBranchNode:u,disabled:v,isSingleSelect:k,showTags:w,clearable:f,isIndependentNodes:b,alwaysOpen:g,emptyText:y,expandSelected:C,grouped:$,openLevel:V,direction:q,rtl:r}),this.tree.srcElement.addEventListener("input",j=>{this.state=j.detail})}}}export{Mi as default}; +var di=Object.defineProperty,ui=(t,e,s)=>e in t?di(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s,c=(t,e,s)=>(ui(t,typeof e!="symbol"?e+"":e,s),s),He=(t,e,s)=>{if(!e.has(t))throw TypeError("Cannot "+s)},l=(t,e,s)=>(He(t,e,"read from private field"),s?s.call(t):e.get(t)),h=(t,e,s)=>{if(e.has(t))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(t):e.set(t,s)},m=(t,e,s,i)=>(He(t,e,"write to private field"),i?i.call(t,s):e.set(t,s),s),a=(t,e,s)=>(He(t,e,"access private method"),s),Ve={arrowUp:'',arrowDown:'',arrowRight:'',attention:'',clear:'',cross:'',check:'',partialCheck:''},O=(t,e)=>{if(e.innerHTML="",typeof t=="string")e.innerHTML=t;else{let s=t.cloneNode(!0);e.appendChild(s)}},Ge=t=>{let e=t?{...t}:{};return Object.keys(Ve).forEach(s=>{e[s]||(e[s]=Ve[s])}),e},pi=t=>t.reduce((e,{name:s},i)=>(e+=s,il(this,L).focus(),0)}blur(){this.isOpened&&a(this,A,H).call(this),this.clearSearch(),l(this,L).blur()}updateValue(e){this.value=e,a(this,G,F).call(this),a(this,M,Y).call(this)}removeItem(e){this.value=this.value.filter(s=>s.id!==e),a(this,Q,qt).call(this),a(this,G,F).call(this),a(this,M,Y).call(this)}clear(){this.value=[],a(this,Q,qt).call(this),a(this,G,F).call(this),this.clearSearch()}openClose(){a(this,A,H).call(this)}clearSearch(){this.searchText="",this.searchCallback(""),a(this,M,Y).call(this)}};N=new WeakMap,L=new WeakMap,D=new WeakMap,S=new WeakMap,St=new WeakSet,Hs=function(){a(this,G,F).call(this),a(this,M,Y).call(this),a(this,Lt,Me).call(this)},G=new WeakSet,F=function(){if(l(this,N).innerHTML="",this.showTags){l(this,N).append(...a(this,Tt,$e).call(this));let t=pi(this.value);this.nameChangeCallback(t)}else{let t=a(this,Bt,Ue).call(this);l(this,N).appendChild(t),this.nameChangeCallback(t.innerText)}l(this,N).appendChild(l(this,L))},Lt=new WeakSet,Me=function(){let t=[];l(this,D).innerHTML="",this.clearable&&t.push(a(this,Mt,Ye).call(this)),this.isAlwaysOpened||t.push(a(this,Dt,Ke).call(this,this.isOpened)),t.length&&l(this,D).append(...t)},yt=new WeakSet,Ie=function(){if(!this.isAlwaysOpened&&l(this,S)){let t=this.isOpened?this.iconElements.arrowUp:this.iconElements.arrowDown;O(t,l(this,S))}},M=new WeakSet,Y=function(){var t;(t=this.value)!=null&&t.length?(l(this,L).removeAttribute("placeholder"),this.srcElement.classList.remove("treeselect-input--value-not-selected")):(l(this,L).setAttribute("placeholder",this.placeholder),this.srcElement.classList.add("treeselect-input--value-not-selected")),this.searchable?this.srcElement.classList.remove("treeselect-input--unsearchable"):this.srcElement.classList.add("treeselect-input--unsearchable"),this.isSingleSelect?this.srcElement.classList.add("treeselect-input--is-single-select"):this.srcElement.classList.remove("treeselect-input--is-single-select"),l(this,L).value=this.searchText},A=new WeakSet,H=function(){this.isOpened=!this.isOpened,a(this,yt,Ie).call(this),this.isOpened?this.openCallback():this.closeCallback()},xt=new WeakSet,Ps=function(t,e,s){let i=document.createElement("div");return i.classList.add("treeselect-input"),i.setAttribute("tabindex","-1"),i.addEventListener("mousedown",n=>a(this,_t,De).call(this,n)),i.addEventListener("focus",()=>this.focusCallback(),!0),i.addEventListener("blur",()=>this.blurCallback(),!0),t.appendChild(e),i.append(t,s),i},_t=new WeakSet,De=function(t){t.stopPropagation(),this.isOpened||a(this,A,H).call(this),this.focus()},Wt=new WeakSet,Vs=function(){let t=document.createElement("div");return t.classList.add("treeselect-input__tags"),t},Tt=new WeakSet,$e=function(){return this.value.map(t=>{let e=document.createElement("div");e.classList.add("treeselect-input__tags-element"),e.setAttribute("tabindex","-1"),e.setAttribute("tag-id",t.id.toString()),e.setAttribute("title",t.name);let s=a(this,Nt,je).call(this,t.name),i=a(this,At,Re).call(this);return e.addEventListener("mousedown",n=>a(this,Ot,qe).call(this,n,t.id)),e.append(s,i),e})},Ot=new WeakSet,qe=function(t,e){t.preventDefault(),t.stopPropagation(),this.removeItem(e),this.focus()},Nt=new WeakSet,je=function(t){let e=document.createElement("span");return e.classList.add("treeselect-input__tags-name"),e.textContent=t,e},At=new WeakSet,Re=function(){let t=document.createElement("span");return t.classList.add("treeselect-input__tags-cross"),O(this.iconElements.cross,t),t},Bt=new WeakSet,Ue=function(){let t=document.createElement("span");if(t.classList.add("treeselect-input__tags-count"),!this.value.length)return t.textContent="",t.setAttribute("title",""),t;let e=this.value.length===1?this.value[0].name:`${this.value.length} ${this.tagsCountText}`;return t.textContent=e,t.setAttribute("title",e),t},Ht=new WeakSet,Gs=function(){let t=document.createElement("input");return t.classList.add("treeselect-input__edit"),this.id&&t.setAttribute("id",this.id),(!this.searchable||this.disabled)&&t.setAttribute("readonly","readonly"),this.disabled&&t.setAttribute("tabindex","-1"),this.ariaLabel.length&&t.setAttribute("aria-label",this.ariaLabel),t.addEventListener("keydown",e=>a(this,Pt,ze).call(this,e)),t.addEventListener("input",e=>a(this,Vt,Fe).call(this,e,t)),t},Pt=new WeakSet,ze=function(t){t.stopPropagation();let e=t.key;e==="Backspace"&&!this.searchText.length&&this.value.length&&!this.showTags&&this.clear(),e==="Backspace"&&!this.searchText.length&&this.value.length&&this.removeItem(this.value[this.value.length-1].id),t.code==="Space"&&(!this.searchText||!this.searchable)&&a(this,A,H).call(this),(e==="Enter"||e==="ArrowDown"||e==="ArrowUp")&&t.preventDefault(),this.keydownCallback(t),e!=="Tab"&&this.focus()},Vt=new WeakSet,Fe=function(t,e){t.stopPropagation();let s=this.searchText,i=e.value.trim();if(s.length===0&&i.length===0){e.value="";return}if(this.searchable){let n=t.target.value;this.searchCallback(n),this.isOpened||a(this,A,H).call(this)}else e.value="";this.searchText=e.value},Gt=new WeakSet,Ms=function(){let t=document.createElement("div");return t.classList.add("treeselect-input__operators"),t},Mt=new WeakSet,Ye=function(){let t=document.createElement("span");return t.classList.add("treeselect-input__clear"),t.setAttribute("tabindex","-1"),O(this.iconElements.clear,t),t.addEventListener("mousedown",e=>a(this,It,Je).call(this,e)),t},It=new WeakSet,Je=function(t){t.preventDefault(),t.stopPropagation(),(this.searchText.length||this.value.length)&&this.clear(),this.focus()},Dt=new WeakSet,Ke=function(t){m(this,S,document.createElement("span")),l(this,S).classList.add("treeselect-input__arrow");let e=t?this.iconElements.arrowUp:this.iconElements.arrowDown;return O(e,l(this,S)),l(this,S).addEventListener("mousedown",s=>a(this,$t,Xe).call(this,s)),l(this,S)},$t=new WeakSet,Xe=function(t){t.stopPropagation(),t.preventDefault(),this.focus(),a(this,A,H).call(this)},Q=new WeakSet,qt=function(){this.inputCallback(this.value)};var Is=(t,e,s,i)=>{vi(e);let n=e.filter(o=>!o.disabled&&t.some(r=>r===o.id));if(s&&n.length){n[0].checked=!0;return}n.forEach(o=>{o.checked=!0;let r=Pe(o,e,i);o.checked=r})},Pe=({id:t,checked:e},s,i)=>{let n=s.find(r=>r.id===t);if(!n)return!1;if(i)return n.checked=n.disabled?!1:!!e,n.checked;let o=Ds(!!e,n,s);return $s(n,s),o},Ds=(t,e,s)=>{if(!e.isGroup)return e.checked=e.disabled?!1:!!t,e.isPartialChecked=!1,e.checked;let i=s.filter(n=>n.childOf===e.id);return!t||e.disabled||e.isPartialChecked?(e.checked=!1,e.isPartialChecked=!1,Rt(e,i,s),e.checked):qs(i,s)?js(i)?(e.checked=!1,e.isPartialChecked=!1,e.disabled=!0,e.checked):(e.checked=!1,e.isPartialChecked=!0,i.forEach(n=>{Ds(t,n,s)}),e.checked):(e.checked=!0,e.isPartialChecked=!1,Rt(e,i,s),e.checked)},$s=(t,e)=>{let s=e.find(i=>i.id===t.childOf);s&&(mi(s,e),$s(s,e))},mi=(t,e)=>{let s=kt(t,e);if(js(s)){t.checked=!1,t.isPartialChecked=!1,t.disabled=!0;return}if(ki(s)){t.checked=!0,t.isPartialChecked=!1;return}if(wi(s)){t.checked=!1,t.isPartialChecked=!0;return}t.checked=!1,t.isPartialChecked=!1},Rt=({checked:t,disabled:e},s,i)=>{s.forEach(n=>{n.disabled=!!e||!!n.disabled,n.checked=!!t&&!n.disabled,n.isPartialChecked=!1;let o=kt(n,i);Rt({checked:t,disabled:e},o,i)})},qs=(t,e)=>t.some(s=>s.disabled)?!0:t.some(s=>{if(s.isGroup){let i=kt(s,e);return qs(i,e)}return!1}),js=t=>t.every(e=>!!e.disabled),ki=t=>t.every(e=>!!e.checked),wi=t=>t.some(e=>!!e.checked||!!e.isPartialChecked),vi=t=>{t.forEach(e=>{e.checked=!1,e.isPartialChecked=!1})},fi=(t,e,s)=>{let i={level:0,groupId:""},n=Rs(t,e,i.groupId,i.level);return gi(n,s)},Rs=(t,e,s,i)=>t.reduce((n,o)=>{var r;let u=!!((r=o.children)!=null&&r.length),v=i>=e&&u,k=i>e;if(n.push({id:o.value,name:o.name,childOf:s,isGroup:u,checked:!1,isPartialChecked:!1,level:i,isClosed:v,hidden:k,disabled:o.disabled??!1}),u){let w=Rs(o.children,e,o.value,i+1);n.push(...w)}return n},[]),kt=({id:t},e)=>e.filter(s=>s.childOf===t),bi=t=>{let{ungroupedNodes:e,allGroupedNodes:s,allNodes:i}=t.reduce((o,r)=>(r.checked&&(o.allNodes.push(r),r.isGroup?o.allGroupedNodes.push(r):o.ungroupedNodes.push(r)),o),{ungroupedNodes:[],allGroupedNodes:[],allNodes:[]}),n=i.filter(o=>!s.some(({id:r})=>r===o.childOf));return{ungroupedNodes:e,groupedNodes:n,allNodes:i}},gi=(t,e)=>(t.filter(s=>!!s.disabled).forEach(({id:s})=>Pe({id:s,checked:!1},t,e)),t),wt=(t,{id:e,isClosed:s})=>{kt({id:e},t).forEach(i=>{i.hidden=s??!1,i.isGroup&&!i.isClosed&&wt(t,{id:i.id,isClosed:s})})},Ci=t=>{t.filter(e=>e.isGroup&&!e.disabled&&(e.checked||e.isPartialChecked)).forEach(e=>{e.isClosed=!1,wt(t,e)})},Ei=(t,e)=>{let s=Si(t,e);t.forEach(i=>{s.some(({id:n})=>n===i.id)?(i.isGroup&&(i.isClosed=!1,wt(t,i)),i.hidden=!1):i.hidden=!0})},Si=(t,e)=>t.reduce((s,i)=>{if(i.name.toLowerCase().includes(e.toLowerCase())){if(s.push(i),i.isGroup){let n=Us(i.id,t);s.push(...n)}if(i.childOf){let n=zs(i.childOf,t);s.push(...n)}}return s},[]),Us=(t,e)=>e.reduce((s,i)=>(i.childOf===t&&(s.push(i),i.isGroup&&s.push(...Us(i.id,e))),s),[]),zs=(t,e)=>e.reduce((s,i)=>(i.id===t&&(s.push(i),i.childOf&&s.push(...zs(i.childOf,e))),s),[]),Li=t=>{let{duplications:e}=t.reduce((s,i)=>(s.allItems.some(n=>n.toString()===i.id.toString())&&s.duplications.push(i.id),s.allItems.push(i.id),s),{duplications:[],allItems:[]});e.length&&console.error(`Validation: You have duplicated values: ${e.join(", ")}! You should use unique values.`)},yi=(t,e,s,i,n,o,r,u,v,k)=>{Is(t,e,n,v),u&&r&&Ci(e),ot(e,s,i,o,k)},ot=(t,e,s,i,n)=>{t.forEach(o=>{let r=e.querySelector(`[input-id="${o.id}"]`),u=W(r);r.checked=o.checked,xi(o,u,i),_i(o,u),Wi(o,u),Ti(o,u,s),Oi(o,u),Ai(o,u,t,n),Ni(o,r,s)}),Bi(t,e)},xi=(t,e,s)=>{t.checked?e.classList.add("treeselect-list__item--checked"):e.classList.remove("treeselect-list__item--checked"),Array.isArray(s)&&s[0]===t.id&&!t.disabled?e.classList.add("treeselect-list__item--single-selected"):e.classList.remove("treeselect-list__item--single-selected")},_i=(t,e)=>{t.isPartialChecked?e.classList.add("treeselect-list__item--partial-checked"):e.classList.remove("treeselect-list__item--partial-checked")},Wi=(t,e)=>{t.disabled?e.classList.add("treeselect-list__item--disabled"):e.classList.remove("treeselect-list__item--disabled")},Ti=(t,e,s)=>{if(t.isGroup){let i=e.querySelector(".treeselect-list__item-icon"),n=t.isClosed?s.arrowRight:s.arrowDown;O(n,i),t.isClosed?e.classList.add("treeselect-list__item--closed"):e.classList.remove("treeselect-list__item--closed")}},Oi=(t,e)=>{t.hidden?e.classList.add("treeselect-list__item--hidden"):e.classList.remove("treeselect-list__item--hidden")},Ni=(t,e,s)=>{let i=e.parentNode.querySelector(".treeselect-list__item-checkbox-icon");t.checked?O(s.check,i):t.isPartialChecked?O(s.partialCheck,i):i.innerHTML=""},Ai=(t,e,s,i)=>{let n=t.level===0,o=20,r=5;if(n){let u=s.some(w=>w.isGroup&&w.level===t.level),v=!t.isGroup&&u?`${o}px`:`${r}px`,k=t.isGroup?"0":v;i?e.style.paddingRight=k:e.style.paddingLeft=k}else{let u=t.isGroup?`${t.level*o}px`:`${t.level*o+o}px`;i?e.style.paddingRight=u:e.style.paddingLeft=u}e.setAttribute("level",t.level.toString()),e.setAttribute("group",t.isGroup.toString())},Bi=(t,e)=>{let s=t.some(n=>!n.hidden),i=e.querySelector(".treeselect-list__empty");s?i.classList.add("treeselect-list__empty--hidden"):i.classList.remove("treeselect-list__empty--hidden")},W=t=>t.parentNode.parentNode,Qe=(t,e)=>e.find(s=>s.id.toString()===t),Hi=t=>W(t).querySelector(".treeselect-list__item-icon"),Pi=(t,e)=>{e&&Object.keys(e).forEach(s=>{let i=e[s];typeof i=="string"&&t.setAttribute(s,i)})},I,P,T,Z,Ut,Fs,zt,Ys,Ft,Js,Yt,Ze,Jt,ts,Kt,es,tt,vt,Xt,ss,Qt,is,Zt,ls,et,ft,te,ns,ee,as,se,hs,ie,cs,le,os,ne,rs,ae,ds,he,us,ce,ps,oe,ms,re,ks,st,bt,rt,de,ue,ws,pe=class{constructor({options:e,value:s,openLevel:i,listSlotHtmlComponent:n,emptyText:o,isSingleSelect:r,iconElements:u,showCount:v,disabledBranchNode:k,expandSelected:w,isIndependentNodes:f,rtl:b,inputCallback:g,arrowClickCallback:y,mouseupCallback:C}){h(this,Ut),h(this,zt),h(this,Ft),h(this,Yt),h(this,Jt),h(this,Kt),h(this,tt),h(this,Xt),h(this,Qt),h(this,Zt),h(this,et),h(this,te),h(this,ee),h(this,se),h(this,ie),h(this,le),h(this,ne),h(this,ae),h(this,he),h(this,ce),h(this,oe),h(this,re),h(this,st),h(this,rt),h(this,ue),c(this,"options"),c(this,"value"),c(this,"openLevel"),c(this,"listSlotHtmlComponent"),c(this,"emptyText"),c(this,"isSingleSelect"),c(this,"showCount"),c(this,"disabledBranchNode"),c(this,"expandSelected"),c(this,"isIndependentNodes"),c(this,"rtl"),c(this,"iconElements"),c(this,"searchText"),c(this,"flattedOptions"),c(this,"flattedOptionsBeforeSearch"),c(this,"selectedNodes"),c(this,"srcElement"),c(this,"inputCallback"),c(this,"arrowClickCallback"),c(this,"mouseupCallback"),h(this,I,null),h(this,P,!0),h(this,T,[]),h(this,Z,!0),this.options=e,this.value=s,this.openLevel=i??0,this.listSlotHtmlComponent=n??null,this.emptyText=o??"No results found...",this.isSingleSelect=r??!1,this.showCount=v??!1,this.disabledBranchNode=k??!1,this.expandSelected=w??!1,this.isIndependentNodes=f??!1,this.rtl=b??!1,this.iconElements=u,this.searchText="",this.flattedOptions=fi(this.options,this.openLevel,this.isIndependentNodes),this.flattedOptionsBeforeSearch=this.flattedOptions,this.selectedNodes={nodes:[],groupedNodes:[],allNodes:[]},this.srcElement=a(this,Ft,Js).call(this),this.inputCallback=g,this.arrowClickCallback=y,this.mouseupCallback=C,Li(this.flattedOptions)}updateValue(e){this.value=e,m(this,T,this.isSingleSelect?this.value:[]),yi(e,this.flattedOptions,this.srcElement,this.iconElements,this.isSingleSelect,l(this,T),this.expandSelected,l(this,Z),this.isIndependentNodes,this.rtl),m(this,Z,!1),a(this,rt,de).call(this)}updateSearchValue(e){if(e===this.searchText)return;let s=this.searchText===""&&e!=="";this.searchText=e,s&&(this.flattedOptionsBeforeSearch=JSON.parse(JSON.stringify(this.flattedOptions))),this.searchText===""&&(this.flattedOptions=this.flattedOptionsBeforeSearch.map(i=>{let n=this.flattedOptions.find(o=>o.id===i.id);return n.isClosed=i.isClosed,n.hidden=i.hidden,n}),this.flattedOptionsBeforeSearch=[]),this.searchText&&Ei(this.flattedOptions,e),ot(this.flattedOptions,this.srcElement,this.iconElements,l(this,T),this.rtl),this.focusFirstListElement()}callKeyAction(e){m(this,P,!1);let s=this.srcElement.querySelector(".treeselect-list__item--focused");if(s?.classList.contains("treeselect-list__item--hidden"))return;let i=e.key;i==="Enter"&&s&&s.dispatchEvent(new Event("mousedown")),(i==="ArrowLeft"||i==="ArrowRight")&&a(this,Ut,Fs).call(this,s,e),(i==="ArrowDown"||i==="ArrowUp")&&a(this,zt,Ys).call(this,s,i)}focusFirstListElement(){let e="treeselect-list__item--focused",s=this.srcElement.querySelector(`.${e}`),i=Array.from(this.srcElement.querySelectorAll(".treeselect-list__item-checkbox")).filter(n=>window.getComputedStyle(W(n)).display!=="none");i.length&&(s&&s.classList.remove(e),W(i[0]).classList.add(e))}isLastFocusedElementExist(){return!!l(this,I)}};I=new WeakMap,P=new WeakMap,T=new WeakMap,Z=new WeakMap,Ut=new WeakSet,Fs=function(t,e){if(!t)return;let s=e.key,i=t.querySelector(".treeselect-list__item-checkbox").getAttribute("input-id"),n=Qe(i,this.flattedOptions),o=t.querySelector(".treeselect-list__item-icon");s==="ArrowLeft"&&!n.isClosed&&n.isGroup&&(o.dispatchEvent(new Event("mousedown")),e.preventDefault()),s==="ArrowRight"&&n.isClosed&&n.isGroup&&(o.dispatchEvent(new Event("mousedown")),e.preventDefault())},zt=new WeakSet,Ys=function(t,e){var s;let i=Array.from(this.srcElement.querySelectorAll(".treeselect-list__item-checkbox")).filter(n=>window.getComputedStyle(W(n)).display!=="none");if(i.length)if(!t)W(i[0]).classList.add("treeselect-list__item--focused");else{let n=i.findIndex(g=>W(g).classList.contains("treeselect-list__item--focused"));W(i[n]).classList.remove("treeselect-list__item--focused");let o=e==="ArrowDown"?n+1:n-1,r=e==="ArrowDown"?0:i.length-1,u=i[o]??i[r],v=!i[o],k=W(u);k.classList.add("treeselect-list__item--focused");let w=this.srcElement.getBoundingClientRect(),f=k.getBoundingClientRect();if(v&&e==="ArrowDown"){this.srcElement.scroll(0,0);return}if(v&&e==="ArrowUp"){this.srcElement.scroll(0,this.srcElement.scrollHeight);return}let b=((s=this.listSlotHtmlComponent)==null?void 0:s.clientHeight)??0;if(w.y+w.heightf.y){this.srcElement.scroll(0,this.srcElement.scrollTop-f.height);return}}},Ft=new WeakSet,Js=function(){let t=a(this,Yt,Ze).call(this),e=a(this,tt,vt).call(this,this.options);t.append(...e);let s=a(this,Qt,is).call(this);t.append(s);let i=a(this,Xt,ss).call(this);return i&&t.append(i),t},Yt=new WeakSet,Ze=function(){let t=document.createElement("div");return t.classList.add("treeselect-list"),this.isSingleSelect&&t.classList.add("treeselect-list--single-select"),this.disabledBranchNode&&t.classList.add("treeselect-list--disabled-branch-node"),t.addEventListener("mouseout",e=>a(this,Jt,ts).call(this,e)),t.addEventListener("mousemove",()=>a(this,Kt,es).call(this)),t.addEventListener("mouseup",()=>this.mouseupCallback(),!0),t},Jt=new WeakSet,ts=function(t){t.stopPropagation(),l(this,I)&&l(this,P)&&l(this,I).classList.add("treeselect-list__item--focused")},Kt=new WeakSet,es=function(){m(this,P,!0)},tt=new WeakSet,vt=function(t){return t.reduce((e,s)=>{var i;if((i=s.children)!=null&&i.length){let o=a(this,Zt,ls).call(this,s),r=a(this,tt,vt).call(this,s.children);return o.append(...r),e.push(o),e}let n=a(this,et,ft).call(this,s,!1);return e.push(n),e},[])},Xt=new WeakSet,ss=function(){if(!this.listSlotHtmlComponent)return null;let t=document.createElement("div");return t.classList.add("treeselect-list__slot"),t.appendChild(this.listSlotHtmlComponent),t},Qt=new WeakSet,is=function(){let t=document.createElement("div");t.classList.add("treeselect-list__empty"),t.setAttribute("title",this.emptyText);let e=document.createElement("span");e.classList.add("treeselect-list__empty-icon"),O(this.iconElements.attention,e);let s=document.createElement("span");return s.classList.add("treeselect-list__empty-text"),s.textContent=this.emptyText,t.append(e,s),t},Zt=new WeakSet,ls=function(t){let e=document.createElement("div");e.setAttribute("group-container-id",t.value.toString()),e.classList.add("treeselect-list__group-container");let s=a(this,et,ft).call(this,t,!0);return e.appendChild(s),e},et=new WeakSet,ft=function(t,e){let s=a(this,te,ns).call(this,t);if(e){let o=a(this,le,os).call(this);s.appendChild(o),s.classList.add("treeselect-list__item--group")}let i=a(this,ae,ds).call(this,t),n=a(this,he,us).call(this,t,e);return s.append(i,n),s},te=new WeakSet,ns=function(t){let e=document.createElement("div");return Pi(e,t.htmlAttr),e.setAttribute("tabindex","-1"),e.setAttribute("title",t.name),e.classList.add("treeselect-list__item"),e.addEventListener("mouseover",()=>a(this,ee,as).call(this,e),!0),e.addEventListener("mouseout",()=>a(this,se,hs).call(this,e),!0),e.addEventListener("mousedown",s=>a(this,ie,cs).call(this,s,t)),e},ee=new WeakSet,as=function(t){l(this,P)&&a(this,st,bt).call(this,!0,t)},se=new WeakSet,hs=function(t){l(this,P)&&(a(this,st,bt).call(this,!1,t),m(this,I,t))},ie=new WeakSet,cs=function(t,e){var s;if(t.preventDefault(),t.stopPropagation(),(s=this.flattedOptions.find(n=>n.id===e.value))==null?void 0:s.disabled)return;let i=t.target.querySelector(".treeselect-list__item-checkbox");i.checked=!i.checked,a(this,oe,ms).call(this,i,e)},le=new WeakSet,os=function(){let t=document.createElement("span");return t.setAttribute("tabindex","-1"),t.classList.add("treeselect-list__item-icon"),O(this.iconElements.arrowDown,t),t.addEventListener("mousedown",e=>a(this,ne,rs).call(this,e)),t},ne=new WeakSet,rs=function(t){t.preventDefault(),t.stopPropagation(),a(this,re,ks).call(this,t)},ae=new WeakSet,ds=function(t){let e=document.createElement("div");e.classList.add("treeselect-list__item-checkbox-container");let s=document.createElement("span");s.classList.add("treeselect-list__item-checkbox-icon"),s.innerHTML="";let i=document.createElement("input");return i.setAttribute("tabindex","-1"),i.setAttribute("type","checkbox"),i.setAttribute("input-id",t.value.toString()),i.classList.add("treeselect-list__item-checkbox"),e.append(s,i),e},he=new WeakSet,us=function(t,e){let s=document.createElement("label");if(s.textContent=t.name,s.classList.add("treeselect-list__item-label"),e&&this.showCount){let i=a(this,ce,ps).call(this,t);s.appendChild(i)}return s},ce=new WeakSet,ps=function(t){let e=document.createElement("span"),s=this.flattedOptions.filter(i=>i.childOf===t.value);return e.textContent=`(${s.length})`,e.classList.add("treeselect-list__item-label-counter"),e},oe=new WeakSet,ms=function(t,e){let s=this.flattedOptions.find(i=>i.id===e.value);if(s){if(s!=null&&s.isGroup&&this.disabledBranchNode){let i=Hi(t);i?.dispatchEvent(new Event("mousedown"));return}if(this.isSingleSelect){let[i]=l(this,T);if(s.id===i)return;m(this,T,[s.id]),Is([s.id],this.flattedOptions,this.isSingleSelect,this.isIndependentNodes)}else{s.checked=t.checked;let i=Pe(s,this.flattedOptions,this.isIndependentNodes);t.checked=i}ot(this.flattedOptions,this.srcElement,this.iconElements,l(this,T),this.rtl),a(this,ue,ws).call(this)}},re=new WeakSet,ks=function(t){var e,s;let i=(s=(e=t.target)==null?void 0:e.parentNode)==null?void 0:s.querySelector("[input-id]"),n=i?.getAttribute("input-id")??null,o=Qe(n,this.flattedOptions);o&&(o.isClosed=!o.isClosed,wt(this.flattedOptions,o),ot(this.flattedOptions,this.srcElement,this.iconElements,l(this,T),this.rtl),this.arrowClickCallback(o.id,o.isClosed))},st=new WeakSet,bt=function(t,e){let s="treeselect-list__item--focused";if(t){let i=Array.from(this.srcElement.querySelectorAll(`.${s}`));i.length&&i.forEach(n=>n.classList.remove(s)),e.classList.add(s)}else e.classList.remove(s)},rt=new WeakSet,de=function(){let{ungroupedNodes:t,groupedNodes:e,allNodes:s}=bi(this.flattedOptions);this.selectedNodes={nodes:t,groupedNodes:e,allNodes:s}},ue=new WeakSet,ws=function(){a(this,rt,de).call(this),this.inputCallback(this.selectedNodes),this.value=this.selectedNodes.nodes.map(t=>t.id)};var vs=({parentHtmlContainer:t,staticList:e,appendToBody:s,isSingleSelect:i,value:n,direction:o})=>{t||console.error("Validation: parentHtmlContainer prop is required!"),e&&s&&console.error("Validation: You should set staticList to false if you use appendToBody!"),i&&Array.isArray(n)&&console.error("Validation: if you use isSingleSelect prop, you should pass a single value!"),!i&&!Array.isArray(n)&&console.error("Validation: you should pass an array as a value!"),o&&o!=="auto"&&o!=="bottom"&&o!=="top"&&console.error("Validation: you should pass (auto | top | bottom | undefined) as a value for the direction prop!")},X=t=>t.map(e=>e.id),Vi=t=>t?Array.isArray(t)?t:[t]:[],Gi=(t,e)=>{if(e){let[s]=t;return s??null}return t},d,p,R,it,U,x,_,E,B,lt,me,nt,gt,ke,fs,we,bs,ve,gs,fe,Cs,be,Es,ge,Ss,dt,Ce,Ee,Ls,Se,ys,Le,xs,ye,_s,ut,xe,_e,Ks,J,pt,at,Ct,z,ht,We,Ws,ct,Et,Te,Ts,Oe,Os,Ne,Ns,Ae,As,Be,Bs,mt=class{constructor({parentHtmlContainer:e,value:s,options:i,openLevel:n,appendToBody:o,alwaysOpen:r,showTags:u,tagsCountText:v,clearable:k,searchable:w,placeholder:f,grouped:b,isGroupedValue:g,listSlotHtmlComponent:y,disabled:C,emptyText:$,staticList:V,id:q,ariaLabel:j,isSingleSelect:K,showCount:Xs,disabledBranchNode:Qs,direction:Zs,expandSelected:ti,saveScrollPosition:ei,isIndependentNodes:si,rtl:ii,iconElements:li,inputCallback:ni,openCallback:ai,closeCallback:hi,nameChangeCallback:ci,searchCallback:oi,openCloseGroupCallback:ri}){h(this,lt),h(this,nt),h(this,ke),h(this,we),h(this,ve),h(this,fe),h(this,be),h(this,ge),h(this,dt),h(this,Ee),h(this,Se),h(this,Le),h(this,ye),h(this,ut),h(this,_e),h(this,J),h(this,at),h(this,z),h(this,We),h(this,ct),h(this,Te),h(this,Oe),h(this,Ne),h(this,Ae),h(this,Be),c(this,"parentHtmlContainer"),c(this,"value"),c(this,"options"),c(this,"openLevel"),c(this,"appendToBody"),c(this,"alwaysOpen"),c(this,"showTags"),c(this,"tagsCountText"),c(this,"clearable"),c(this,"searchable"),c(this,"placeholder"),c(this,"grouped"),c(this,"isGroupedValue"),c(this,"listSlotHtmlComponent"),c(this,"disabled"),c(this,"emptyText"),c(this,"staticList"),c(this,"id"),c(this,"ariaLabel"),c(this,"isSingleSelect"),c(this,"showCount"),c(this,"disabledBranchNode"),c(this,"direction"),c(this,"expandSelected"),c(this,"saveScrollPosition"),c(this,"isIndependentNodes"),c(this,"rtl"),c(this,"iconElements"),c(this,"inputCallback"),c(this,"openCallback"),c(this,"closeCallback"),c(this,"nameChangeCallback"),c(this,"searchCallback"),c(this,"openCloseGroupCallback"),c(this,"ungroupedValue"),c(this,"groupedValue"),c(this,"allValue"),c(this,"isListOpened"),c(this,"selectedName"),c(this,"srcElement"),h(this,d,null),h(this,p,null),h(this,R,null),h(this,it,0),h(this,U,0),h(this,x,null),h(this,_,null),h(this,E,null),h(this,B,null),vs({parentHtmlContainer:e,value:s,staticList:V,appendToBody:o,isSingleSelect:K}),this.parentHtmlContainer=e,this.value=[],this.options=i??[],this.openLevel=n??0,this.appendToBody=o??!1,this.alwaysOpen=!!(r&&!C),this.showTags=u??!0,this.tagsCountText=v??"elements selected",this.clearable=k??!0,this.searchable=w??!0,this.placeholder=f??"Search...",this.grouped=b??!0,this.isGroupedValue=g??!1,this.listSlotHtmlComponent=y??null,this.disabled=C??!1,this.emptyText=$??"No results found...",this.staticList=!!(V&&!this.appendToBody),this.id=q??"",this.ariaLabel=j??"",this.isSingleSelect=K??!1,this.showCount=Xs??!1,this.disabledBranchNode=Qs??!1,this.direction=Zs??"auto",this.expandSelected=ti??!1,this.saveScrollPosition=ei??!0,this.isIndependentNodes=si??!1,this.rtl=ii??!1,this.iconElements=Ge(li),this.inputCallback=ni,this.openCallback=ai,this.closeCallback=hi,this.nameChangeCallback=ci,this.searchCallback=oi,this.openCloseGroupCallback=ri,this.ungroupedValue=[],this.groupedValue=[],this.allValue=[],this.isListOpened=!1,this.selectedName="",this.srcElement=null,a(this,lt,me).call(this,s)}mount(){vs({parentHtmlContainer:this.parentHtmlContainer,value:this.value,staticList:this.staticList,appendToBody:this.appendToBody,isSingleSelect:this.isSingleSelect}),this.iconElements=Ge(this.iconElements),a(this,lt,me).call(this,this.value)}updateValue(e){let s=Vi(e),i=l(this,d);i&&(i.updateValue(s),a(this,dt,Ce).call(this,i?.selectedNodes))}destroy(){this.srcElement&&(a(this,ut,xe).call(this),this.srcElement.innerHTML="",this.srcElement=null,a(this,z,ht).call(this,!0))}focus(){l(this,p)&&l(this,p).focus()}toggleOpenClose(){l(this,p)&&(l(this,p).openClose(),l(this,p).focus())}scrollWindowHandler(){this.updateListPosition()}focusWindowHandler(e){var s,i,n;(s=this.srcElement)!=null&&s.contains(e.target)||(i=l(this,d))!=null&&i.srcElement.contains(e.target)||((n=l(this,p))==null||n.blur(),a(this,z,ht).call(this,!1),a(this,J,pt).call(this,!1))}blurWindowHandler(){var e;(e=l(this,p))==null||e.blur(),a(this,z,ht).call(this,!1),a(this,J,pt).call(this,!1)}updateListPosition(){var e;let s=this.srcElement,i=(e=l(this,d))==null?void 0:e.srcElement;if(!s||!i)return;let{height:n}=i.getBoundingClientRect(),{x:o,y:r,height:u,width:v}=s.getBoundingClientRect(),k=window.innerHeight,w=r,f=k-r-u,b=w>f&&w>=n&&fa(this,Ee,Ls).call(this,i),arrowClickCallback:(i,n)=>a(this,Se,ys).call(this,i,n),mouseupCallback:()=>{var i;return(i=l(this,p))==null?void 0:i.focus()}}),s=new jt({value:[],showTags:this.showTags,tagsCountText:this.tagsCountText,clearable:this.clearable,isAlwaysOpened:this.alwaysOpen,searchable:this.searchable,placeholder:this.placeholder,disabled:this.disabled,isSingleSelect:this.isSingleSelect,id:this.id,ariaLabel:this.ariaLabel,iconElements:this.iconElements,inputCallback:i=>a(this,we,bs).call(this,i),searchCallback:i=>a(this,fe,Cs).call(this,i),openCallback:()=>a(this,ye,_s).call(this),closeCallback:()=>a(this,ut,xe).call(this),keydownCallback:i=>a(this,ve,gs).call(this,i),focusCallback:()=>a(this,be,Es).call(this),blurCallback:()=>a(this,ge,Ss).call(this),nameChangeCallback:i=>a(this,Le,xs).call(this,i)});return this.appendToBody&&m(this,R,new ResizeObserver(()=>this.updateListPosition())),t.append(s.srcElement),{container:t,list:e,input:s}},we=new WeakSet,bs=function(t){var e,s;let i=X(t);(e=l(this,d))==null||e.updateValue(i);let n=((s=l(this,d))==null?void 0:s.selectedNodes)??{};a(this,nt,gt).call(this,n),a(this,ct,Et).call(this)},ve=new WeakSet,gs=function(t){var e;this.isListOpened&&((e=l(this,d))==null||e.callKeyAction(t))},fe=new WeakSet,Cs=function(t){l(this,U)&&clearTimeout(l(this,U)),m(this,U,window.setTimeout(()=>{var e;(e=l(this,d))==null||e.updateSearchValue(t),this.updateListPosition()},350)),a(this,Ae,As).call(this,t)},be=new WeakSet,Es=function(){a(this,J,pt).call(this,!0),l(this,E)&&l(this,E)&&l(this,B)&&(document.addEventListener("mousedown",l(this,E),!0),document.addEventListener("focus",l(this,E),!0),window.addEventListener("blur",l(this,B)))},ge=new WeakSet,Ss=function(){setTimeout(()=>{var t,e;let s=(t=l(this,p))==null?void 0:t.srcElement.contains(document.activeElement),i=(e=l(this,d))==null?void 0:e.srcElement.contains(document.activeElement);!s&&!i&&this.blurWindowHandler()},1)},dt=new WeakSet,Ce=function(t){var e;if(!t)return;let s=[];this.isIndependentNodes||this.isSingleSelect?s=t.allNodes:this.grouped?s=t.groupedNodes:s=t.nodes,(e=l(this,p))==null||e.updateValue(s),a(this,nt,gt).call(this,t)},Ee=new WeakSet,Ls=function(t){var e,s,i;a(this,dt,Ce).call(this,t),this.isSingleSelect&&!this.alwaysOpen&&((e=l(this,p))==null||e.openClose(),(s=l(this,p))==null||s.clearSearch()),(i=l(this,p))==null||i.focus(),a(this,ct,Et).call(this)},Se=new WeakSet,ys=function(t,e){var s;(s=l(this,p))==null||s.focus(),this.updateListPosition(),a(this,Be,Bs).call(this,t,e)},Le=new WeakSet,xs=function(t){this.selectedName!==t&&(this.selectedName=t,a(this,Te,Ts).call(this))},ye=new WeakSet,_s=function(){var t;this.isListOpened=!0,l(this,x)&&l(this,_)&&(window.addEventListener("scroll",l(this,x),!0),window.addEventListener("resize",l(this,_))),!(!l(this,d)||!this.srcElement)&&(this.appendToBody?(document.body.appendChild(l(this,d).srcElement),(t=l(this,R))==null||t.observe(this.srcElement)):this.srcElement.appendChild(l(this,d).srcElement),this.updateListPosition(),a(this,at,Ct).call(this,!0),a(this,We,Ws).call(this),a(this,Oe,Os).call(this))},ut=new WeakSet,xe=function(){var t;this.alwaysOpen||(this.isListOpened=!1,l(this,x)&&l(this,_)&&(window.removeEventListener("scroll",l(this,x),!0),window.removeEventListener("resize",l(this,_))),!l(this,d)||!this.srcElement)||!(this.appendToBody?document.body.contains(l(this,d).srcElement):this.srcElement.contains(l(this,d).srcElement))||(m(this,it,l(this,d).srcElement.scrollTop),this.appendToBody?(document.body.removeChild(l(this,d).srcElement),(t=l(this,R))==null||t.disconnect()):this.srcElement.removeChild(l(this,d).srcElement),a(this,at,Ct).call(this,!1),a(this,Ne,Ns).call(this))},_e=new WeakSet,Ks=function(t,e){if(!l(this,d)||!l(this,p))return;let s=e?"treeselect-list--top-to-body":"treeselect-list--top",i=e?"treeselect-list--bottom-to-body":"treeselect-list--bottom";t?(l(this,d).srcElement.classList.add(s),l(this,d).srcElement.classList.remove(i),l(this,p).srcElement.classList.add("treeselect-input--top"),l(this,p).srcElement.classList.remove("treeselect-input--bottom")):(l(this,d).srcElement.classList.remove(s),l(this,d).srcElement.classList.add(i),l(this,p).srcElement.classList.remove("treeselect-input--top"),l(this,p).srcElement.classList.add("treeselect-input--bottom"))},J=new WeakSet,pt=function(t){!l(this,p)||!l(this,d)||(t?(l(this,p).srcElement.classList.add("treeselect-input--focused"),l(this,d).srcElement.classList.add("treeselect-list--focused")):(l(this,p).srcElement.classList.remove("treeselect-input--focused"),l(this,d).srcElement.classList.remove("treeselect-list--focused")))},at=new WeakSet,Ct=function(t){var e,s,i,n;t?(e=l(this,p))==null||e.srcElement.classList.add("treeselect-input--opened"):(s=l(this,p))==null||s.srcElement.classList.remove("treeselect-input--opened"),this.staticList?(i=l(this,d))==null||i.srcElement.classList.add("treeselect-list--static"):(n=l(this,d))==null||n.srcElement.classList.remove("treeselect-list--static")},z=new WeakSet,ht=function(t){!l(this,x)||!l(this,_)||!l(this,E)||!l(this,B)||((!this.alwaysOpen||t)&&(window.removeEventListener("scroll",l(this,x),!0),window.removeEventListener("resize",l(this,_))),document.removeEventListener("mousedown",l(this,E),!0),document.removeEventListener("focus",l(this,E),!0),window.removeEventListener("blur",l(this,B)))},We=new WeakSet,Ws=function(){var t,e,s;let i=(t=l(this,d))==null?void 0:t.isLastFocusedElementExist();this.saveScrollPosition&&i?(e=l(this,d))==null||e.srcElement.scroll(0,l(this,it)):(s=l(this,d))==null||s.focusFirstListElement()},ct=new WeakSet,Et=function(){var t;(t=this.srcElement)==null||t.dispatchEvent(new CustomEvent("input",{detail:this.value})),this.inputCallback&&this.inputCallback(this.value)},Te=new WeakSet,Ts=function(){var t;(t=this.srcElement)==null||t.dispatchEvent(new CustomEvent("name-change",{detail:this.selectedName})),this.nameChangeCallback&&this.nameChangeCallback(this.selectedName)},Oe=new WeakSet,Os=function(){var t;this.alwaysOpen||((t=this.srcElement)==null||t.dispatchEvent(new CustomEvent("open",{detail:this.value})),this.openCallback&&this.openCallback(this.value))},Ne=new WeakSet,Ns=function(){var t;this.alwaysOpen||((t=this.srcElement)==null||t.dispatchEvent(new CustomEvent("close",{detail:this.value})),this.closeCallback&&this.closeCallback(this.value))},Ae=new WeakSet,As=function(t){var e;let s=t?.trim()??"";(e=this.srcElement)==null||e.dispatchEvent(new CustomEvent("search",{detail:s})),this.searchCallback&&this.searchCallback(s)},Be=new WeakSet,Bs=function(t,e){var s;(s=this.srcElement)==null||s.dispatchEvent(new CustomEvent("open-close-group",{detail:{groupId:t,isClosed:e}})),this.openCloseGroupCallback&&this.openCloseGroupCallback(t,e)};function Mi({state:t,name:e,options:s,searchable:i,showCount:n,placeholder:o,rtl:r,disabledBranchNode:u=!0,disabled:v=!1,isSingleSelect:k=!0,showTags:w=!0,clearable:f=!0,isIndependentNodes:b=!0,alwaysOpen:g=!1,emptyText:y,expandSelected:C=!0,grouped:$=!0,openLevel:V=0,direction:q="auto"}){return{state:t,tree:null,init(){this.tree=new mt({id:`tree-${e}-id`,ariaLabel:`tree-${e}-label`,parentHtmlContainer:this.$refs.tree,value:this.state,options:s,searchable:i,showCount:n,placeholder:o,disabledBranchNode:u,disabled:v,isSingleSelect:k,showTags:w,clearable:f,isIndependentNodes:b,alwaysOpen:g,emptyText:y,expandSelected:C,grouped:$,openLevel:V,direction:q,rtl:r}),this.tree.srcElement.addEventListener("input",j=>{this.state=j.detail})}}}export{Mi as default}; diff --git a/resources/js/index.js b/resources/js/index.js index de42df9..2556907 100644 --- a/resources/js/index.js +++ b/resources/js/index.js @@ -28,10 +28,6 @@ export default function selectTree({ tree: null, init() { - if (typeof this.state === 'string') { - this.state = Number(this.state); - } - this.tree = new Treeselect({ id: `tree-${name}-id`, ariaLabel: `tree-${name}-label`, From 7472b453c1dcccc3c2ebbd5131809c7223ae2f86 Mon Sep 17 00:00:00 2001 From: Valentin Tsanev Date: Wed, 26 Mar 2025 13:27:00 +0200 Subject: [PATCH 09/13] fix composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 186595c..78c53f8 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "lotestudio/filament-select-tree", + "name": "codewithdennis/filament-select-tree", "description": "The multi-level select field enables you to make single selections from a predefined list of options that are organized into multiple levels or depths.", "keywords": [ "CodeWithDennis", From e31d1ba5a3df2da2343271ab385e75f83ff273e4 Mon Sep 17 00:00:00 2001 From: Valentin Tsanev Date: Wed, 26 Mar 2025 13:30:07 +0200 Subject: [PATCH 10/13] formatting --- resources/js/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/js/index.js b/resources/js/index.js index 2556907..4fb9c41 100644 --- a/resources/js/index.js +++ b/resources/js/index.js @@ -26,8 +26,8 @@ export default function selectTree({ /** @type Treeselect */ tree: null, - init() { + init() { this.tree = new Treeselect({ id: `tree-${name}-id`, ariaLabel: `tree-${name}-label`, From 32b7d93299318b7e3e94c723028f134d91e88bf0 Mon Sep 17 00:00:00 2001 From: Dennis Elsinga Date: Wed, 26 Mar 2025 15:39:12 +0100 Subject: [PATCH 11/13] Format --- src/SelectTree.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/SelectTree.php b/src/SelectTree.php index 806e0ab..e809057 100644 --- a/src/SelectTree.php +++ b/src/SelectTree.php @@ -88,7 +88,6 @@ class SelectTree extends Field implements HasAffixActions protected Closure|array|null $prepend = null; - protected function setUp(): void { // Load the state from relationships using a callback function. @@ -296,12 +295,14 @@ public function parentNullValue(int|string|null $parentNullValue = null): static public function multiple(Closure|bool $multiple = true): static { $this->multiple = $multiple; + return $this; } public function prepend(Closure|array|null $prepend = null): static { $this->prepend = $prepend; + return $this; } @@ -412,7 +413,7 @@ public function storeResults(bool $storeResults = true): static public function getTree(): Collection|array { return $this->evaluate($this->buildTree()->when($this->prepend, - fn(Collection $tree) => $tree->prepend($this->evaluate($this->prepend)))); + fn (Collection $tree) => $tree->prepend($this->evaluate($this->prepend)))); } public function getResults(): Collection|array|null From cbbad83dc96856021ead12562d789f3d9a1ed006 Mon Sep 17 00:00:00 2001 From: Dennis Elsinga Date: Wed, 26 Mar 2025 15:41:36 +0100 Subject: [PATCH 12/13] Format README --- README.md | 71 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index f057eca..dd4795e 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,8 @@ [![Latest Version on Packagist](https://img.shields.io/packagist/v/codewithdennis/filament-select-tree.svg?style=flat-square)](https://packagist.org/packages/codewithdennis/filament-select-tree) [![Total Downloads](https://img.shields.io/packagist/dt/codewithdennis/filament-select-tree.svg?style=flat-square)](https://packagist.org/packages/codewithdennis/filament-select-tree) -This package adds a dynamic select tree field to your Laravel / Filament application, allowing you to create interactive hierarchical selection dropdowns based on relationships. It's handy for building selection dropdowns with various customization options. +This package adds a dynamic select tree field to your Laravel / Filament application, allowing you to create interactive hierarchical selection dropdowns based on relationships. It's handy for +building selection dropdowns with various customization options. ![thumbnail](https://raw.githubusercontent.com/CodeWithDennis/filament-select-tree/3.x/resources/images/thumbnail.jpg) @@ -23,14 +24,14 @@ php artisan filament:assets Use the tree for a `BelongsToMany` relationship -```PHP +```php SelectTree::make('categories') ->relationship('categories', 'name', 'parent_id') ``` Use the tree for a `BelongsTo` relationship -```PHP +```php SelectTree::make('category_id') ->relationship('category', 'name', 'parent_id') ``` @@ -39,14 +40,14 @@ SelectTree::make('category_id') Customize the parent query -```PHP +```php SelectTree::make('categories') ->relationship(relationship: 'categories', titleAttribute: 'name', parentAttribute: 'parent_id', modifyQueryUsing: fn($query) => $query)); ``` Customize the child query -```PHP +```php SelectTree::make('categories') ->relationship(relationship: 'categories', titleAttribute: 'name', parentAttribute: 'parent_id', modifyChildQueryUsing: fn($query) => $query)); ``` @@ -55,126 +56,128 @@ SelectTree::make('categories') Set a custom placeholder when no items are selected -```PHP +```php ->placeholder(__('Please select a category')) ``` Enable the selection of groups -```PHP +```php ->enableBranchNode() ``` Customize the label when there are zero search results -```PHP +```php ->emptyLabel(__('Oops, no results have been found!')) ``` Display the count of children alongside the group's name -```PHP +```php ->withCount() ``` Keep the dropdown open at all times -```PHP +```php ->alwaysOpen() ``` Set nodes as dependent -```PHP +```php ->independent(false) ``` Expand the tree with selected values (only works if field is dependent) -```PHP +```php ->expandSelected(false) ``` Set the parent's null value to -1, allowing you to use -1 as a sentinel value (default = null) -```PHP +```php ->parentNullValue(-1) ``` All groups will be opened to this level -```PHP +```php ->defaultOpenLevel(2) ``` Specify the list's force direction. Options include: auto (default), top, and bottom. -```PHP +```php ->direction('top') ``` Display individual leaf nodes instead of the main group when all leaf nodes are selected -```PHP +```php ->grouped(false) ``` Hide the clearable icon -```PHP +```php ->clearable(false) ``` Activate the search functionality -```PHP +```php ->searchable(); ``` Disable specific options in the tree -```PHP +```php ->disabledOptions([2, 3, 4]) ``` Hide specific options in the tree -```PHP +```php ->hiddenOptions([2, 3, 4]) ``` Allow soft deleted items to be displayed -```PHP +```php ->withTrashed() ``` Specify a different key for your model. For example: you have id, code and parent_code. Your model uses id as key, but the parent-child relation is established between code and parent_code -```PHP +```php ->withKey('code') ``` Store fetched models for additional functionality -```PHP +```php ->storeResults() ``` Now you can access the results in `disabledOptions` or `hiddenOptions` -```PHP +```php ->disabledOptions(function ($state, SelectTree $component) { $results = $component->getResults(); }) ``` -By default, the type of selection in the tree (single or multiple) is determined by the relationship type: `BelongsTo` for single selection and `BelongsToMany` for multiple selection. If you want to explicitly set the selection type, use: +By default, the type of selection in the tree (single or multiple) is determined by the relationship type: `BelongsTo` for single selection and `BelongsToMany` for multiple selection. If you want to +explicitly set the selection type, use: -```PHP -->multiple(false) //or true, Closure that returns boolean +```php +->multiple(false) ``` + If you need to prepend an item to the tree menu, use the `prepend` method. This method accepts an array or a closure. It is useful when the tree-select is used as a filter (see example below). ```php @@ -192,22 +195,23 @@ use CodeWithDennis\FilamentSelectTree\SelectTree; ->enableBranchNode() ->multiple(false) ->prepend([ - 'name'=>'Uncategorized Products', //required - 'value'=>-1, //required + 'name' => 'Uncategorized Products', + 'value' => -1, 'parent' => null // optional 'disabled' => false // optional 'hidden' => false // optional - 'children'=>[] //optional + 'children' => [] // optional ]) ]) ->query(function (Builder $query, array $data) { $categories= [(int) $data['category']]; - return $query->when($data['category'], function ($query, $categories) { - if($data['category']===-1){ + + return $query->when($data['category'], function (Builder $query, $categories) { + if($data['category'] === -1){ return $query->whereDoesntHave('categories'); } - return $query->whereHas('categories', fn($query) => $query->whereIn('id', $categories)); + return $query->whereHas('categories', fn(Builder $query) => $query->whereIn('id', $categories)); }); }) ]) @@ -248,6 +252,7 @@ use CodeWithDennis\FilamentSelectTree\SelectTree; ``` ## Screenshots + ![example-1](https://raw.githubusercontent.com/CodeWithDennis/filament-select-tree/3.x/resources/images/example-1.jpg) ![example-2](https://raw.githubusercontent.com/CodeWithDennis/filament-select-tree/3.x/resources/images/example-2.jpg) ![example-3](https://raw.githubusercontent.com/CodeWithDennis/filament-select-tree/3.x/resources/images/example-3.jpg) From 5fa9f9714f7d0b4666996f108f49b204b5ce1c5a Mon Sep 17 00:00:00 2001 From: Dennis Elsinga Date: Wed, 26 Mar 2025 16:15:23 +0100 Subject: [PATCH 13/13] Update README.md with new default option for uncategorized records --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index dd4795e..8902756 100644 --- a/README.md +++ b/README.md @@ -195,16 +195,16 @@ use CodeWithDennis\FilamentSelectTree\SelectTree; ->enableBranchNode() ->multiple(false) ->prepend([ - 'name' => 'Uncategorized Products', + 'name' => 'Uncategorized Records', 'value' => -1, - 'parent' => null // optional - 'disabled' => false // optional - 'hidden' => false // optional - 'children' => [] // optional + 'parent' => null, // optional + 'disabled' => false, // optional + 'hidden' => false, // optional + 'children' => [], // optional ]) ]) ->query(function (Builder $query, array $data) { - $categories= [(int) $data['category']]; + $categories = [(int) $data['category']]; return $query->when($data['category'], function (Builder $query, $categories) { if($data['category'] === -1){