Files

2 lines
6.8 KiB
JavaScript

function $({statePath:l,isCollapsed:c,collapseKey:f}){return{isCollapsed:f?Alpine.$persist(c).as(f):c,collapseFromEvent(m){m.detail===l&&(this.isCollapsed=!0)},expandFromEvent(m){m.detail===l&&(this.isCollapsed=!1)},expand(){this.isCollapsed=!1},toggleCollapsed(){this.isCollapsed=!this.isCollapsed},get itemClasses(){return{"fi-collapsed":this.isCollapsed}}}}function x({schemaComponent:l,moveUpAction:c,moveDownAction:f,reorderAction:m,message:y,hasCustomReorderAction:w}){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,s,d,g=!0)=>{t.addEventListener(s,d,g),this.listeners.push(()=>t.removeEventListener(s,d,g))};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 s=t.target.closest("[data-reorder-direction]");if(!(!s||!this.owns(s))){if(this.pending){t.preventDefault(),t.stopImmediatePropagation();return}!s.dataset.reorderAction?.startsWith("mountAction(")||s.getAttribute("wire:click")!==s.dataset.reorderAction||s.getAttributeNames().some(d=>d==="wire:confirm"||d.startsWith("wire:confirm."))||s.disabled||this.begin(s.closest("[x-sortable-item]").getAttribute("x-sortable-item"),s.dataset.reorderDirection,s)}}),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:s,succeed:d,fail:g})=>{let i=this.pending;if(t.id!==this.$wire.$id)return;let n=[];for(let r=this.$root;r&&!(r.hasAttribute("wire:id")&&r.getAttribute("wire:id")!==t.id);r=r.parentElement){let u=r.getAttribute("wire:partial");if(u&&n.push(u),r===t.el)break}let b=r=>r.html||n.some(u=>r.partials?.[u]);if(d(({effects:r})=>{this.pending===i&&(i&&(!i.synchronizing||!b(r))||this.schedule(()=>{this.destroyed||!this.$root.isConnected||(this.updateHandles(),i?.synchronizing&&this.isCurrent(i)&&this.finish())}))}),!i||i.synchronizing)return;let a=t.canonical.mountedActions??[],o=r=>r?.context?.schemaComponent===l&&r.name===i.action&&(i.direction==="drag"||String(r.arguments?.item)===i.item),C=a.some(o)&&!o(a.at(-1));if(!s.calls.some(r=>r.method==="mountAction"?o({name:r.params[0],arguments:r.params[1],context:r.params[2]}):["callMountedAction","unmountAction"].includes(r.method)&&a.some(o)))return;let S=s.calls.some(r=>r.method==="unmountAction");d(({effects:r})=>{if(!this.isCurrent(i))return;let u=t.canonical.mountedActions??[],I=u.some(o);for(let[h,p]of i.reopenedModals??[])A(u[h])!==p&&i.reopenedModals.delete(h);if(I){i.focus=!1,i.modal=!0,this.setSortingDisabled(!0);let h=u.length-1;u.length<a.length&&i.reopenedModals?.has(h)&&this.schedule(()=>{if(!this.isCurrent(i))return;let p=t.canonical.mountedActions??[];p.length===h+1&&A(p[h])===i.reopenedModals.get(h)&&this.$dispatch("open-modal",{id:`fi-${t.id}-action-${h}`})});return}if(!b(r)||C){this.resynchronize(i);return}this.schedule(()=>this.reconcile(i))}),g(()=>{this.isCurrent(i)&&(i.focus=!1,i.modal&&S&&(i.reopenedModals??(i.reopenedModals=new Map),i.reopenedModals.set(a.length-1,A(a.at(-1))),this.$dispatch("open-modal",{id:`fi-${t.id}-action-${a.length-1}`})),!i.modal&&(i.direction==="drag"?this.resynchronize(i):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:l})},begin(e,t,s=null){this.pending={item:e,direction:t,control:s,focus:!s?.hasAttribute("data-reorder-hidden")&&document.activeElement===s,before:this.order(),action:t==="drag"?m:t==="up"?c:f},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(),s=e.before.filter(n=>t.includes(n)),d=t.filter(n=>e.before.includes(n)),g=t.includes(e.item)&&s.indexOf(e.item)!==d.indexOf(e.item),i=[...this.$root.querySelectorAll("[data-reorder-description]")].find(n=>this.owns(n)&&n.dataset.reorderDescription===e.item);if(g&&i&&(this.$refs.reorderStatus.textContent=y.replace(/:label|:position|:count/g,n=>({":label":i.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(o=>this.owns(o)&&o.parentElement===this.$refs.reorderItems&&o.getAttribute("x-sortable-item")===e.item),b=[...n?.querySelectorAll("[data-reorder-direction]")??[]].filter(o=>this.owns(o)&&!o.hasAttribute("data-reorder-hidden")&&!o.disabled&&o.getAttribute("aria-disabled")!=="true"&&this.isElementVisible(o)),a=b.find(o=>o.dataset.reorderDirection===e.direction)??b[0];a?a.focus({preventScroll:!0}):n&&(n.tabIndex=-1,!n.hasAttribute("aria-label")&&!n.hasAttribute("aria-labelledby")&&n.setAttribute("aria-labelledby",i.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(!w)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(d=>this.owns(d)&&!d.disabled&&d.getAttribute("aria-disabled")!=="true"&&this.isElementVisible(d))?(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 v({statePath:l,...c}){return{...x(c),collapseAll(){this.$dispatch("repeater-collapse",l)},expandAll(){this.$dispatch("repeater-expand",l)},item(f){return $({statePath:l,...f})}}}export{v as default};