Files

2 lines
8.9 KiB
JavaScript

function $({statePath:s,isCollapsed:l,collapseKey:u}){return{isCollapsed:u?Alpine.$persist(l).as(u):l,collapseFromEvent(m){m.detail===s&&(this.isCollapsed=!0)},expandFromEvent(m){m.detail===s&&(this.isCollapsed=!1)},expand(){this.isCollapsed=!1},toggleCollapsed(){this.isCollapsed=!this.isCollapsed},get itemClasses(){return{"fi-collapsed":this.isCollapsed}}}}function y({schemaComponent:s,moveUpAction:l,moveDownAction:u,reorderAction:m,message:S,hasCustomReorderAction:x}){let A=e=>JSON.stringify({name:e?.name,arguments:e?.arguments,context:e?.context});return{pending:null,frame:null,destroyed:!1,listeners:[],removeCommitHook:null,init(){let e=(t,o,a,p=!0)=>{t.addEventListener(o,a,p),this.listeners.push(()=>t.removeEventListener(o,a,p))};e(document,"pointerdown",t=>{this.pending?.control?.contains(t.target)||this.relinquishFocus()}),e(document,"keydown",t=>{t.key==="Tab"&&this.relinquishFocus()}),e(document,"focusin",t=>{this.pending&&t.target!==this.pending.control&&this.relinquishFocus()}),e(this.$root,"click",t=>{let o=t.target.closest("[data-reorder-direction]");if(!(!o||!this.owns(o))){if(this.pending){t.preventDefault(),t.stopImmediatePropagation();return}!o.dataset.reorderAction?.startsWith("mountAction(")||o.getAttribute("wire:click")!==o.dataset.reorderAction||o.getAttributeNames().some(a=>a==="wire:confirm"||a.startsWith("wire:confirm."))||o.disabled||this.begin(o.closest("[x-sortable-item]").getAttribute("x-sortable-item"),o.dataset.reorderDirection,o)}}),e(this.$root,"start",t=>{t.target===this.$refs.reorderItems&&this.begin(t.item.getAttribute("x-sortable-item"),"drag")}),e(this.$root,"end",t=>{if(t.target===this.$refs.reorderItems){if(t.oldDraggableIndex===t.newDraggableIndex){this.finish(),t.stopImmediatePropagation();return}this.setSortingDisabled(!0)}}),this.removeCommitHook=Livewire.hook("commit",({component:t,commit:o,succeed:a,fail:p})=>{let r=this.pending;if(t.id!==this.$wire.$id)return;let n=[];for(let i=this.$root;i&&!(i.hasAttribute("wire:id")&&i.getAttribute("wire:id")!==t.id);i=i.parentElement){let h=i.getAttribute("wire:partial");if(h&&n.push(h),i===t.el)break}let g=i=>i.html||n.some(h=>i.partials?.[h]);if(a(({effects:i})=>{this.pending===r&&(r&&(!r.synchronizing||!g(i))||this.schedule(()=>{this.destroyed||!this.$root.isConnected||(this.updateHandles(),r?.synchronizing&&this.isCurrent(r)&&this.finish())}))}),!r||r.synchronizing)return;let c=t.canonical.mountedActions??[],d=i=>i?.context?.schemaComponent===s&&i.name===r.action&&(r.direction==="drag"||String(i.arguments?.item)===r.item),v=c.some(d)&&!d(c.at(-1));if(!o.calls.some(i=>i.method==="mountAction"?d({name:i.params[0],arguments:i.params[1],context:i.params[2]}):["callMountedAction","unmountAction"].includes(i.method)&&c.some(d)))return;let C=o.calls.some(i=>i.method==="unmountAction");a(({effects:i})=>{if(!this.isCurrent(r))return;let h=t.canonical.mountedActions??[],k=h.some(d);for(let[f,w]of r.reopenedModals??[])A(h[f])!==w&&r.reopenedModals.delete(f);if(k){r.focus=!1,r.modal=!0,this.setSortingDisabled(!0);let f=h.length-1;h.length<c.length&&r.reopenedModals?.has(f)&&this.schedule(()=>{if(!this.isCurrent(r))return;let w=t.canonical.mountedActions??[];w.length===f+1&&A(w[f])===r.reopenedModals.get(f)&&this.$dispatch("open-modal",{id:`fi-${t.id}-action-${f}`})});return}if(!g(i)||v){this.resynchronize(r);return}this.schedule(()=>this.reconcile(r))}),p(()=>{this.isCurrent(r)&&(r.focus=!1,r.modal&&C&&(r.reopenedModals??(r.reopenedModals=new Map),r.reopenedModals.set(c.length-1,A(c.at(-1))),this.$dispatch("open-modal",{id:`fi-${t.id}-action-${c.length-1}`})),!r.modal&&(r.direction==="drag"?this.resynchronize(r):this.finish()))})}),this.updateHandles()},owns(e){return e.closest("[data-reorder-owner]")===this.$root},order(){return JSON.parse(this.$root.dataset.reorderItems)},reorder(e){e.target===this.$refs.reorderItems&&this.$wire.mountAction(m,{items:e.target.sortable.toArray()},{schemaComponent:s})},begin(e,t,o=null){this.pending={item:e,direction:t,control:o,focus:!o?.hasAttribute("data-reorder-hidden")&&document.activeElement===o,before:this.order(),action:t==="drag"?m:t==="up"?l:u},t!=="drag"&&this.setSortingDisabled(!0)},relinquishFocus(){this.pending&&(this.pending.focus=!1)},isCurrent(e){return!this.destroyed&&this.$root.isConnected&&this.pending===e},schedule(e){cancelAnimationFrame(this.frame),this.frame=requestAnimationFrame(e)},resynchronize(e){this.setSortingDisabled(!0),e.synchronizing=!0,this.$wire.forceRender().catch(()=>{})},reconcile(e){if(!this.isCurrent(e))return;this.updateHandles();let t=this.order(),o=e.before.filter(n=>t.includes(n)),a=t.filter(n=>e.before.includes(n)),p=t.includes(e.item)&&o.indexOf(e.item)!==a.indexOf(e.item),r=[...this.$root.querySelectorAll("[data-reorder-description]")].find(n=>this.owns(n)&&n.dataset.reorderDescription===e.item);if(p&&r&&(this.$refs.reorderStatus.textContent=S.replace(/:label|:position|:count/g,n=>({":label":r.dataset.reorderLabel,":position":String(t.indexOf(e.item)+1),":count":String(t.length)})[n]),e.focus&&!e.modal&&[document.body,e.control].includes(document.activeElement))){let n=[...this.$root.querySelectorAll("[x-sortable-item]")].find(d=>this.owns(d)&&d.parentElement===this.$refs.reorderItems&&d.getAttribute("x-sortable-item")===e.item),g=[...n?.querySelectorAll("[data-reorder-direction]")??[]].filter(d=>this.owns(d)&&!d.hasAttribute("data-reorder-hidden")&&!d.disabled&&d.getAttribute("aria-disabled")!=="true"&&this.isElementVisible(d)),c=g.find(d=>d.dataset.reorderDirection===e.direction)??g[0];c?c.focus({preventScroll:!0}):n&&(n.tabIndex=-1,!n.hasAttribute("aria-label")&&!n.hasAttribute("aria-labelledby")&&n.setAttribute("aria-labelledby",r.id),n.focus({preventScroll:!0}))}!t.includes(e.item)&&e.focus&&!e.modal&&document.activeElement===document.body&&(this.$root.tabIndex=-1,this.$root.focus({preventScroll:!0})),this.finish()},setSortingDisabled(e){this.$refs.reorderItems?.sortable?.option("disabled",e)},isElementVisible(e){let t=window.getComputedStyle(e);return t.display!=="none"&&t.visibility!=="hidden"&&e.getClientRects().length>0},updateHandles(){if(!x)for(let e of this.$refs.reorderItems?.querySelectorAll("[x-sortable-handle]")??[]){if(!this.owns(e)||e.closest("[x-sortable]")!==this.$refs.reorderItems)continue;[...e.closest("[x-sortable-item]").querySelectorAll("[data-reorder-direction]")].some(a=>this.owns(a)&&!a.disabled&&a.getAttribute("aria-disabled")!=="true"&&this.isElementVisible(a))?(e.tabIndex=-1,e.setAttribute("aria-hidden","true")):(e.removeAttribute("tabindex"),e.removeAttribute("aria-hidden"))}},finish(){this.pending=null,this.setSortingDisabled(!1)},destroy(){this.destroyed=!0,this.pending=null,cancelAnimationFrame(this.frame),this.removeCommitHook?.(),this.listeners.forEach(e=>e())}}}function b(s){return s.normalize("NFD").replace(/[\u0300-\u036f]/g,"").toLowerCase()}function L({statePath:s,...l}){return{...y(l),blockPicker:E,collapseAll(){this.$dispatch("builder-collapse",s)},expandAll(){this.$dispatch("builder-expand",s)},editItem(u){this.$wire.mountAction("edit",{item:u},{schemaComponent:l.schemaComponent})},item(u){return $({statePath:s,...u})}}}function E(){return{search:"",blockLabels:[],observer:null,dropdownPanel:null,dropdownOpenedListener:null,dropdownEscapeListener:null,init(){let s=()=>{this.blockLabels=Array.from(this.$root.querySelectorAll("[data-block-label]"),l=>l.dataset.blockLabel)};s(),this.observer=new MutationObserver(s),this.observer.observe(this.$root,{childList:!0,subtree:!0,attributes:!0,attributeFilter:["data-block-label"]}),this.setUpDropdownAutofocus(),this.dropdownEscapeListener=l=>{this.handleEscape(l),l.stopPropagation()},this.$root.addEventListener("dropdown-escape",this.dropdownEscapeListener)},setUpDropdownAutofocus(){this.dropdownPanel=this.$root.closest(".fi-dropdown-panel"),this.dropdownOpenedListener=()=>this.autofocusSearch(),this.dropdownPanel?.addEventListener("dropdown-opened",this.dropdownOpenedListener);let s=this.dropdownPanel?.closest(".fi-dropdown")?.querySelector(":scope > .fi-dropdown-trigger button, :scope > .fi-dropdown-trigger a, :scope > .fi-dropdown-trigger [tabindex]");this.dropdownPanel?.style.display!=="block"||document.activeElement!==s||this.autofocusSearch()},autofocusSearch(){!this.$root.isConnected||this.dropdownPanel?.style.display!=="block"||(this.clearSearch(),this.$refs.searchInput?.focus({preventScroll:!0}))},clearSearch(){this.search="",this.$refs.searchInput&&(this.$refs.searchInput.value="")},handleEscape(s){!this.search&&!this.$refs.searchInput?.value||(s.preventDefault(),this.clearSearch())},isBlockVisible(s){return this.search?b(s.dataset.blockLabel).includes(b(this.search)):!0},isGroupVisible(s){return this.blockLabels.length>0&&Array.from(s.querySelectorAll("[data-block-label]")).some(l=>this.isBlockVisible(l))},get hasNoSearchResults(){return this.search?!this.blockLabels.some(s=>b(s).includes(b(this.search))):!1},destroy(){this.observer?.disconnect(),this.dropdownPanel?.removeEventListener("dropdown-opened",this.dropdownOpenedListener),this.$root.removeEventListener("dropdown-escape",this.dropdownEscapeListener)}}}export{L as default};