From efe4319964c6efbc77dab074715d10a0c3726032 Mon Sep 17 00:00:00 2001 From: Taha Shashtari Date: Fri, 20 Dec 2024 15:40:46 +0200 Subject: [PATCH] v1.0.3 --- dist/swapy.js | 8 +++++--- dist/swapy.min.js | 4 ++-- package.json | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/dist/swapy.js b/dist/swapy.js index 1129895..e095bf4 100644 --- a/dist/swapy.js +++ b/dist/swapy.js @@ -140,14 +140,15 @@ function mt(t, e, n, o) { return requestAnimationFrame(a), i; } const Tt = { - startDelay: 0 + startDelay: 0, + targetEl: null }; function Mt(t, e) { const n = { ...Tt, ...e }; let o = t.el(), l = !1, i = null, d = null, u = null, a = null, h = 0, s = 0, c = 0, f = 0, y = 0, v = 0, g = 0, T = 0, r = 0, A = 0, E = null, p; o.addEventListener("pointerdown", w), document.body.addEventListener("pointerup", x), document.body.addEventListener("pointermove", X), document.body.addEventListener("touchmove", M, { passive: !1 }); function w(m) { - if (l || !m.isPrimary) return; + if (n.targetEl && m.target !== n.targetEl || l || !m.isPrimary) return; n.startDelay > 0 ? (u == null || u({ el: m.target }), p = setTimeout(() => { B(); }, n.startDelay)) : B(); @@ -822,7 +823,8 @@ function Pt(t, e) { }, y = () => { }; const { onDrag: v, onDrop: g, onHold: T, onRelease: r } = n.usePlugin(Mt, { - startDelay: e.config().dragOnHold ? 400 : 0 + startDelay: e.config().dragOnHold ? 400 : 0, + targetEl: P() }), A = R(0, 0), E = R(0, 0), p = R(0, 0), w = R(0, 0); let I = null, x = null; T((S) => { diff --git a/dist/swapy.min.js b/dist/swapy.min.js index 764ecc5..7e38529 100644 --- a/dist/swapy.min.js +++ b/dist/swapy.min.js @@ -1,5 +1,5 @@ -(function(P,j){typeof exports=="object"&&typeof module<"u"?j(exports):typeof define=="function"&&define.amd?define(["exports"],j):(P=typeof globalThis<"u"?globalThis:P||self,j(P.Swapy={}))})(this,function(P){"use strict";function j(t){return typeof t=="object"&&t!==null&&"x"in t&&"y"in t&&"unit"in t&&typeof t.unit=="string"&&typeof t.x=="object"&&typeof t.y=="object"&&"topLeft"in t.x&&"topRight"in t.x&&"bottomRight"in t.x&&"bottomLeft"in t.x&&"topLeft"in t.y&&"topRight"in t.y&&"bottomRight"in t.y&&"bottomLeft"in t.y}function lt(t){var h;const e=t.match(/(\d+(?:\.\d+)?)(px|%)/g);if(!e)return{x:{topLeft:0,topRight:0,bottomRight:0,bottomLeft:0},y:{topLeft:0,topRight:0,bottomRight:0,bottomLeft:0},unit:"px"};const n=e.map(a=>{const[c,f,m]=a.match(/(\d+(?:\.\d+)?)(px|%)/)??[];return{value:parseFloat(f),unit:m}}),o=((h=n[0])==null?void 0:h.unit)||"px";if(n.some(a=>a.unit!==o))throw new Error("Inconsistent units in border-radius string.");const[l,r,d,u]=n.map(a=>a.value),s={topLeft:l??0,topRight:r??l??0,bottomRight:d??l??0,bottomLeft:u??r??l??0};return{x:{...s},y:{...s},unit:o}}function it({x:t,y:e,unit:n},o,l){if(n==="px"){const r={topLeft:t.topLeft/o,topRight:t.topRight/o,bottomLeft:t.bottomLeft/o,bottomRight:t.bottomRight/o},d={topLeft:e.topLeft/l,topRight:e.topRight/l,bottomLeft:e.bottomLeft/l,bottomRight:e.bottomRight/l};return{x:r,y:d,unit:"px"}}else if(n==="%")return{x:t,y:e,unit:"%"};return{x:t,y:e,unit:n}}function Z(t){return` +(function(N,j){typeof exports=="object"&&typeof module<"u"?j(exports):typeof define=="function"&&define.amd?define(["exports"],j):(N=typeof globalThis<"u"?globalThis:N||self,j(N.Swapy={}))})(this,function(N){"use strict";function j(t){return typeof t=="object"&&t!==null&&"x"in t&&"y"in t&&"unit"in t&&typeof t.unit=="string"&&typeof t.x=="object"&&typeof t.y=="object"&&"topLeft"in t.x&&"topRight"in t.x&&"bottomRight"in t.x&&"bottomLeft"in t.x&&"topLeft"in t.y&&"topRight"in t.y&&"bottomRight"in t.y&&"bottomLeft"in t.y}function lt(t){var h;const e=t.match(/(\d+(?:\.\d+)?)(px|%)/g);if(!e)return{x:{topLeft:0,topRight:0,bottomRight:0,bottomLeft:0},y:{topLeft:0,topRight:0,bottomRight:0,bottomLeft:0},unit:"px"};const n=e.map(s=>{const[c,f,m]=s.match(/(\d+(?:\.\d+)?)(px|%)/)??[];return{value:parseFloat(f),unit:m}}),o=((h=n[0])==null?void 0:h.unit)||"px";if(n.some(s=>s.unit!==o))throw new Error("Inconsistent units in border-radius string.");const[l,i,d,u]=n.map(s=>s.value),a={topLeft:l??0,topRight:i??l??0,bottomRight:d??l??0,bottomLeft:u??i??l??0};return{x:{...a},y:{...a},unit:o}}function rt({x:t,y:e,unit:n},o,l){if(n==="px"){const i={topLeft:t.topLeft/o,topRight:t.topRight/o,bottomLeft:t.bottomLeft/o,bottomRight:t.bottomRight/o},d={topLeft:e.topLeft/l,topRight:e.topRight/l,bottomLeft:e.bottomLeft/l,bottomRight:e.bottomRight/l};return{x:i,y:d,unit:"px"}}else if(n==="%")return{x:t,y:e,unit:"%"};return{x:t,y:e,unit:n}}function Z(t){return` ${t.x.topLeft}${t.unit} ${t.x.topRight}${t.unit} ${t.x.bottomRight}${t.unit} ${t.x.bottomLeft}${t.unit} / ${t.y.topLeft}${t.unit} ${t.y.topRight}${t.unit} ${t.y.bottomRight}${t.unit} ${t.y.bottomLeft}${t.unit} - `}function rt(t){return t.x.topLeft===0&&t.x.topRight===0&&t.x.bottomRight===0&&t.x.bottomLeft===0&&t.y.topLeft===0&&t.y.topRight===0&&t.y.bottomRight===0&&t.y.bottomLeft===0}function st(t){return typeof t=="object"&&"x"in t&&"y"in t}function R(t,e){return{x:t,y:e}}function xt(t,e){return R(t.x+e.x,t.y+e.y)}function vt(t,e){return R(t.x-e.x,t.y-e.y)}function At(t,e){return R(t.x*e,t.y*e)}function L(t,e,n){return t+(e-t)*n}function It(t,e,n){return xt(t,At(vt(e,t),n))}function at(t,e,n){return{x:{topLeft:L(t.x.topLeft,e.x.topLeft,n),topRight:L(t.x.topRight,e.x.topRight,n),bottomRight:L(t.x.bottomRight,e.x.bottomRight,n),bottomLeft:L(t.x.bottomLeft,e.x.bottomLeft,n)},y:{topLeft:L(t.y.topLeft,e.y.topLeft,n),topRight:L(t.y.topRight,e.y.topRight,n),bottomRight:L(t.y.bottomRight,e.y.bottomRight,n),bottomLeft:L(t.y.bottomLeft,e.y.bottomLeft,n)},unit:t.unit}}function Et(t,e,n){return W((n-t)/(e-t),0,1)}function K(t,e,n,o,l){return L(n,o,Et(t,e,l))}function W(t,e,n){return Math.min(Math.max(t,e),n)}const Tt={duration:350,easing:t=>t};function ct(t,e,n,o){let l=!1;const r=()=>{l=!0},d={...Tt,...o};let u;function s(h){u===void 0&&(u=h);const a=h-u,c=W(a/d.duration,0,1),f=Object.keys(t),m=Object.keys(e);if(!f.every(g=>m.includes(g))){console.error("animate Error: `from` keys are different than `to`");return}const v={};f.forEach(g=>{typeof t[g]=="number"&&typeof e[g]=="number"?v[g]=L(t[g],e[g],d.easing(c)):j(t[g])&&j(e[g])?v[g]=at(t[g],e[g],d.easing(c)):st(t[g])&&st(e[g])&&(v[g]=It(t[g],e[g],d.easing(c)))}),n(v,c>=1,c),c<1&&!l&&requestAnimationFrame(s)}return requestAnimationFrame(s),r}const Xt={startDelay:0};function Mt(t,e){const n={...Xt,...e};let o=t.el(),l=!1,r=null,d=null,u=null,s=null,h=0,a=0,c=0,f=0,m=0,v=0,g=0,X=0,i=0,A=0,E=null,p;o.addEventListener("pointerdown",w),document.body.addEventListener("pointerup",x),document.body.addEventListener("pointermove",T),document.body.addEventListener("touchmove",M,{passive:!1});function w(y){if(l||!y.isPrimary)return;n.startDelay>0?(u==null||u({el:y.target}),p=setTimeout(()=>{B()},n.startDelay)):B();function B(){E=y.target;const C=t.boundingRect(),V=t.layoutRect();m=V.x,v=V.y,c=C.x-m,f=C.y-v,h=y.clientX-c,a=y.clientY-f,g=y.clientX,X=y.clientY,i=(y.clientX-C.x)/C.width,A=(y.clientY-C.y)/C.height,l=!0,T(y)}}function I(){const y=t.layoutRect();h-=m-y.x,a-=v-y.y,m=y.x,v=y.y}function x(y){if(!l){p&&(clearTimeout(p),p=null,s==null||s({el:y.target}));return}if(!y.isPrimary)return;l=!1;const B=y.clientX-g,C=y.clientY-X;d==null||d({x:c,y:f,pointerX:y.clientX,pointerY:y.clientY,width:B,height:C,relativeX:i,relativeY:A,el:E}),E=null}function T(y){if(!l){p&&(clearTimeout(p),p=null,s==null||s({el:y.target}));return}if(!y.isPrimary)return;const B=y.clientX-g,C=y.clientY-X,V=c=y.clientX-h,nt=f=y.clientY-a;r==null||r({width:B,height:C,x:V,y:nt,pointerX:y.clientX,pointerY:y.clientY,relativeX:i,relativeY:A,el:E})}function M(y){if(!l)return!0;y.preventDefault()}function b(y){r=y}function H(y){d=y}function _(y){u=y}function N(y){s=y}function $(){o.removeEventListener("pointerdown",w),o=t.el(),o.addEventListener("pointerdown",w)}function q(){t.el().removeEventListener("pointerdown",w),document.body.removeEventListener("pointerup",x),document.body.removeEventListener("pointermove",T),document.body.removeEventListener("touchmove",M),r=null,d=null,u=null,s=null}return{onDrag:b,onDrop:H,onHold:_,onRelease:N,onElementUpdate:$,destroy:q,readjust:I}}function bt(t){return 1+2.70158*Math.pow(t-1,3)+1.70158*Math.pow(t-1,2)}function Dt(t){return 1-Math.pow(1-t,3)}function G(t){return{x:t.x,y:t.y,width:t.width,height:t.height}}function Lt(t){const e=t.getBoundingClientRect();let n=0,o=0,l=t.parentElement;for(;l;){const d=getComputedStyle(l).transform;if(d&&d!=="none"){const u=d.match(/matrix.*\((.+)\)/);if(u){const s=u[1].split(", ").map(Number);n+=s[4]||0,o+=s[5]||0}}l=l.parentElement}return{y:e.top-o,x:e.left-n,width:e.width,height:e.height}}function Q(t){let e=t,n=0,o=0;for(;e;)n+=e.offsetTop,o+=e.offsetLeft,e=e.offsetParent;return{x:o,y:n,width:t.offsetWidth,height:t.offsetHeight}}function ft(t,e){return t.x>=e.x&&t.x<=e.x+e.width&&t.y>=e.y&&t.y<=e.y+e.height}function Ct(t){let e=t,n=0,o=0;for(;e;){const l=r=>{const d=getComputedStyle(r);return/(auto|scroll)/.test(d.overflow+d.overflowY+d.overflowX)};if(e===document.body){o+=window.scrollX,n+=window.scrollY;break}l(e)&&(o+=e.scrollLeft,n+=e.scrollTop),e=e.parentElement}return{x:o,y:n}}function tt(t){let e="unread",n,o,l,r,d,u,s,h,a,c,f;function m(){n=t.currentTransform(),o=Lt(t.el()),l=Ct(t.el()),f=ut(t.el()).map(({parent:A,children:E})=>({parent:{el:A,initialRect:G(A.getBoundingClientRect())},children:E.filter(p=>p instanceof HTMLElement).map(p=>{const w=p;return w.originalBorderRadius||(w.originalBorderRadius=getComputedStyle(p).borderRadius),{el:p,borderRadius:lt(w.originalBorderRadius),initialRect:G(p.getBoundingClientRect())}})})),e="readInitial"}function v(){if(e!=="readInitial")throw new Error("FlipView: Cannot read final values before reading initial values");a=t.layoutRect(),u=o.width/a.width,s=o.height/a.height,r=o.x-a.x-n.dragX+l.x,d=o.y-a.y-n.dragY+l.y,h=it(t.borderRadius(),u,s);const i=ut(t.el());f=f.map(({parent:E,children:p},w)=>{const I=i[w].parent;return{parent:{...E,el:I,finalRect:Q(I)},children:p.map((x,T)=>{const M=i[w].children[T];let b=Q(M);return M.hasAttribute("data-swapy-text")&&(b={...b,width:x.initialRect.width,height:x.initialRect.height}),{...x,el:M,finalRect:b}})}});const A={translateX:r,translateY:d,scaleX:u,scaleY:s};t.el().style.transformOrigin="0 0",t.el().style.borderRadius=Z(h),t.setTransform(A),c=[],f.forEach(({parent:E,children:p})=>{const w=p.map(({el:I,initialRect:x,finalRect:T,borderRadius:M})=>Yt(I,x,T,M,E.initialRect,E.finalRect));c.push(...w)}),e="readFinal"}function g(){if(e!=="readFinal")throw new Error("FlipView: Cannot get transition values before reading");return{from:{width:o.width,height:o.height,translate:R(r,d),scale:R(u,s),borderRadius:h},to:{width:a.width,height:a.height,translate:R(0,0),scale:R(1,1),borderRadius:t.borderRadius()}}}function X(){if(e!=="readFinal")throw new Error("FlipView: Cannot get children transition values before reading");return c}return{readInitial:m,readFinalAndReverse:v,transitionValues:g,childrenTransitionData:X}}function Yt(t,e,n,o,l,r){t.style.transformOrigin="0 0";const d=l.width/r.width,u=l.height/r.height,s=e.width/n.width,h=e.height/n.height,a=it(o,s,h),c=e.x-l.x,f=n.x-r.x,m=e.y-l.y,v=n.y-r.y,g=(c-f*d)/d,X=(m-v*u)/u;return t.style.transform=`translate(${g}px, ${X}px) scale(${s/d}, ${h/u})`,t.style.borderRadius=Z(a),{el:t,fromTranslate:R(g,X),fromScale:R(s,h),fromBorderRadius:a,toBorderRadius:o,parentScale:{x:d,y:u}}}function ut(t){const e=[];function n(o){const l=Array.from(o.children).filter(r=>r instanceof HTMLElement);l.length>0&&(e.push({parent:o,children:l}),l.forEach(r=>n(r)))}return n(t),e}function dt(t){const e=[];let n=t,o={dragX:0,dragY:0,translateX:0,translateY:0,scaleX:1,scaleY:1};const l=lt(window.getComputedStyle(n).borderRadius),r={el:()=>n,setTransform:d,clearTransform:u,currentTransform:()=>o,borderRadius:()=>l,layoutRect:()=>Q(n),boundingRect:()=>G(n.getBoundingClientRect()),usePlugin:h,destroy:a,updateElement:c};function d(f){o={...o,...f},s()}function u(){o={dragX:0,dragY:0,translateX:0,translateY:0,scaleX:1,scaleY:1},s()}function s(){const{dragX:f,dragY:m,translateX:v,translateY:g,scaleX:X,scaleY:i}=o;f===0&&m===0&&v===0&&g===0&&X===1&&i===1?n.style.transform="":n.style.transform=`translate(${f+v}px, ${m+g}px) scale(${X}, ${i})`}function h(f,m){const v=f(r,m);return e.push(v),v}function a(){e.forEach(f=>f.destroy())}function c(f){if(!f)return;const m=n.hasAttribute("data-swapy-dragging"),v=n.style.cssText;n=f,m&&n.setAttribute("data-swapy-dragging",""),n.style.cssText=v,e.forEach(g=>g.onElementUpdate())}return r}function Rt(t,e,n){return n.map(o=>({slotId:o.slot,itemId:o.item,item:o.item===""?null:t.find(l=>o.item===l[e])}))}function Ht(t,e){return t.map(n=>({item:n[e],slot:n[e]}))}function $t(t,e,n,o,l,r=!1){const d=e.filter(h=>!o.some(a=>a.item===h[n])).map(h=>({slot:h[n],item:h[n]}));let u;r?u=o.map(h=>e.some(a=>a[n]===h.item)?h:{slot:h.slot,item:""}):u=o.filter(h=>e.some(a=>a[n]===h.item)||!h.item);const s=[...u,...d];l(s),(d.length>0||u.length!==o.length)&&requestAnimationFrame(()=>{t==null||t.update()})}const Bt=Object.freeze(Object.defineProperty({__proto__:null,dynamicSwapy:$t,initSlotItemMap:Ht,toSlottedItems:Rt},Symbol.toStringTag,{value:"Module"})),Ot={animation:"dynamic",enabled:!0,swapMode:"hover",dragOnHold:!1,autoScrollOnDrag:!1,dragAxis:"both",manualSwap:!1};function gt(t){switch(t){case"dynamic":return{easing:Dt,duration:300};case"spring":return{easing:bt,duration:350};case"none":return{easing:e=>e,duration:1}}}function _t(t,e){const n={...Ot,...e},o=Pt({slots:[],items:[],config:n});let l=[],r=[];d();function d(){if(!jt(t))throw new Error("Cannot create a Swapy instance because your HTML structure is invalid. Fix all above errors and then try!");l=Array.from(t.querySelectorAll("[data-swapy-slot]")).map(i=>Nt(i,o)),o.setSlots(l),r=Array.from(t.querySelectorAll("[data-swapy-item]")).map(i=>qt(i,o)),o.setItems(r),o.syncSlotItemMap(),r.forEach(i=>{i.onDrag(({pointerX:A,pointerY:E})=>{s();let p=!1;l.forEach(w=>{const I=w.rect();ft({x:A,y:E},I)&&(p=!0,w.isHighlighted()||w.highlight())}),!p&&o.config().swapMode==="drop"&&i.slot().highlight(),n.swapMode==="hover"&&u(i,{pointerX:A,pointerY:E})}),i.onDrop(({pointerX:A,pointerY:E})=>{h(),n.swapMode==="drop"&&u(i,{pointerX:A,pointerY:E})}),i.onHold(()=>{s()}),i.onRelease(()=>{h()})})}function u(i,{pointerX:A,pointerY:E}){l.forEach(p=>{const w=p.rect();if(ft({x:A,y:E},w)){if(i.id()===p.itemId())return;o.config().swapMode==="hover"&&i.setContinuousDrag(!0);const I=i.slot(),x=p.item();if(!o.eventHandlers().onBeforeSwap({fromSlot:I.id(),toSlot:p.id(),draggingItem:i.id(),swapWithItem:(x==null?void 0:x.id())||""}))return;if(o.config().manualSwap){const T=structuredClone(o.slotItemMap());o.swapItems(i,p);const M=o.slotItemMap(),b=tt(i.view());b.readInitial();const H=x?tt(x.view()):null;H==null||H.readInitial();let _=0,N=0;const $=J(i.view().el());$ instanceof Window?(_=$.scrollY,N=$.scrollX):(_=$.scrollTop,N=$.scrollLeft),o.eventHandlers().onSwap({oldSlotItemMap:T,newSlotItemMap:M,fromSlot:I.id(),toSlot:p.id(),draggingItem:i.id(),swappedWithItem:(x==null?void 0:x.id())||""}),requestAnimationFrame(()=>{const q=t.querySelectorAll("[data-swapy-item]");o.items().forEach(y=>{const B=Array.from(q).find(C=>C.dataset.swapyItem===y.id());y.view().updateElement(B)}),o.syncSlotItemMap(),b.readFinalAndReverse(),H==null||H.readFinalAndReverse(),et(i,b),x&&H&&et(x,H),$.scrollTo({left:N,top:_})})}else{let T=0,M=0;const b=J(i.view().el());b instanceof Window?(T=b.scrollY,M=b.scrollX):(T=b.scrollTop,M=b.scrollLeft),ht(i,p,!0),x&&ht(x,I),b.scrollTo({left:M,top:T});const H=o.slotItemMap();o.syncSlotItemMap();const _=o.slotItemMap();o.eventHandlers().onSwap({oldSlotItemMap:H,newSlotItemMap:_,fromSlot:I.id(),toSlot:p.id(),draggingItem:i.id(),swappedWithItem:(x==null?void 0:x.id())||""})}}})}function s(){t.querySelectorAll("img").forEach(i=>{i.style.pointerEvents="none"}),t.style.userSelect="none",t.style.webkitUserSelect="none"}function h(){t.querySelectorAll("img").forEach(i=>{i.style.pointerEvents=""}),t.style.userSelect="",t.style.webkitUserSelect=""}function a(i){o.config().enabled=i}function c(i){o.eventHandlers().onSwapStart=i}function f(i){o.eventHandlers().onSwap=i}function m(i){o.eventHandlers().onSwapEnd=i}function v(i){o.eventHandlers().onBeforeSwap=i}function g(){X(),requestAnimationFrame(()=>{d()})}function X(){r.forEach(i=>i.destroy()),l.forEach(i=>i.destroy()),o.destroy(),r=[],l=[]}return{enable:a,slotItemMap:()=>o.slotItemMap(),onSwapStart:c,onSwap:f,onSwapEnd:m,onBeforeSwap:v,update:g,destroy:X}}function Pt({slots:t,items:e,config:n}){const o={slots:t,items:e,config:n,slotItemMap:{asObject:{},asMap:new Map,asArray:[]},zIndexCount:1,eventHandlers:{onSwapStart:()=>{},onSwap:()=>{},onSwapEnd:()=>{},onBeforeSwap:()=>!0},scrollOffsetWhileDragging:{x:0,y:0},scrollHandler:null};let l={...o};const r=c=>{var f;(f=l.scrollHandler)==null||f.call(l,c)};window.addEventListener("scroll",r);function d(c){return l.slots.find(f=>f.id()===c)}function u(c){return l.items.find(f=>f.id()===c)}function s(){const c={},f=new Map,m=[];l.slots.forEach(v=>{var i;const g=v.id(),X=((i=v.item())==null?void 0:i.id())||"";c[g]=X,f.set(g,X),m.push({slot:g,item:X})}),l.slotItemMap={asObject:c,asMap:f,asArray:m}}function h(c,f){var p;const m=l.slotItemMap,v=c.id(),g=((p=f.item())==null?void 0:p.id())||"",X=f.id(),i=c.slot().id();m.asObject[X]=v,m.asObject[i]=g,m.asMap.set(X,v),m.asMap.set(i,g);const A=m.asArray.findIndex(w=>w.slot===X),E=m.asArray.findIndex(w=>w.slot===i);m.asArray[A].item=v,m.asArray[E].item=g}function a(){window.removeEventListener("scroll",r),l={...o}}return{slots:()=>l.slots,items:()=>l.items,config:()=>n,setItems:c=>l.items=c,setSlots:c=>l.slots=c,slotById:d,itemById:u,zIndex:(c=!1)=>c?++l.zIndexCount:l.zIndexCount,resetZIndex:()=>{l.zIndexCount=1},eventHandlers:()=>l.eventHandlers,syncSlotItemMap:s,slotItemMap:()=>l.slotItemMap,onScroll:c=>{l.scrollHandler=c},swapItems:h,destroy:a}}function Nt(t,e){const n=dt(t);function o(){return n.el().dataset.swapySlot}function l(){const a=n.el().children[0];return(a==null?void 0:a.dataset.swapyItem)||null}function r(){return G(n.el().getBoundingClientRect())}function d(){const a=n.el().children[0];if(a)return e.itemById(a.dataset.swapyItem)}function u(){e.slots().forEach(a=>{a.view().el().removeAttribute("data-swapy-highlighted")})}function s(){u(),n.el().setAttribute("data-swapy-highlighted","")}function h(){}return{id:o,view:()=>n,itemId:l,rect:r,item:d,highlight:s,unhighlightAllSlots:u,isHighlighted:()=>n.el().hasAttribute("data-swapy-highlighted"),destroy:h}}function qt(t,e){const n=dt(t),o={};let l=null,r=null,d=!1,u=!0,s;const h=Ft();let a=()=>{},c=()=>{},f=()=>{},m=()=>{};const{onDrag:v,onDrop:g,onHold:X,onRelease:i}=n.usePlugin(Mt,{startDelay:e.config().dragOnHold?400:0}),A=R(0,0),E=R(0,0),p=R(0,0),w=R(0,0);let I=null,x=null;X(S=>{e.config().enabled&&(q()&&!$(S.el)||C()&&B(S.el)||f==null||f(S))}),i(S=>{e.config().enabled&&(q()&&!$(S.el)||C()&&B(S.el)||m==null||m(S))});function T(S){var F;V(),ot().highlight(),(F=o.drop)==null||F.call(o);const Y=e.slots().map(O=>O.view().boundingRect());e.slots().forEach((O,z)=>{const U=Y[z];O.view().el().style.width=`${U.width}px`,O.view().el().style.maxWidth=`${U.width}px`,O.view().el().style.flexShrink="0",O.view().el().style.height=`${U.height}px`});const D=e.slotItemMap();e.eventHandlers().onSwapStart({draggingItem:mt(),fromSlot:wt(),slotItemMap:D}),r=D,n.el().style.position="relative",n.el().style.zIndex=`${e.zIndex(!0)}`,I=J(S.el),e.config().autoScrollOnDrag&&(l=kt(I,e.config().dragAxis),l.updatePointer({x:S.pointerX,y:S.pointerY})),A.x=window.scrollX,A.y=window.scrollY,p.x=0,p.y=0,I instanceof HTMLElement&&(E.x=I.scrollLeft,E.y=I.scrollTop,x=()=>{w.x=I.scrollLeft-E.x,w.y=I.scrollTop-E.y,n.setTransform({dragX:((s==null?void 0:s.width)||0)+p.x+w.x,dragY:((s==null?void 0:s.height)||0)+p.y+w.y})},I.addEventListener("scroll",x)),e.onScroll(()=>{p.x=window.scrollX-A.x,p.y=window.scrollY-A.y;const O=w.x||0,z=w.y||0;n.setTransform({dragX:((s==null?void 0:s.width)||0)+p.x+O,dragY:((s==null?void 0:s.height)||0)+p.y+z})})}v(S=>{var Y;if(e.config().enabled){if(!d){if(q()&&!$(S.el)||C()&&B(S.el))return;T(S)}d=!0,l&&l.updatePointer({x:S.pointerX,y:S.pointerY}),s=S,(Y=o.drop)==null||Y.call(o),h(()=>{n.el().style.position="relative";const D=S.width+p.x+w.x,F=S.height+p.y+w.y;e.config().dragAxis==="y"?n.setTransform({dragY:F}):e.config().dragAxis==="x"?n.setTransform({dragX:D}):n.setTransform({dragX:D,dragY:F}),a==null||a(S)})}}),g(S=>{if(!d)return;nt(),d=!1,u=!1,s=null,I&&(I.removeEventListener("scroll",x),x=null),I=null,w.x=0,w.y=0,p.x=0,p.y=0,l&&(l.destroy(),l=null),ot().unhighlightAllSlots(),c==null||c(S),e.eventHandlers().onSwapEnd({slotItemMap:e.slotItemMap(),hasChanged:r!=null&&r.asMap?!Wt(r==null?void 0:r.asMap,e.slotItemMap().asMap):!1}),r=null,e.onScroll(null),e.slots().forEach(D=>{D.view().el().style.width="",D.view().el().style.maxWidth="",D.view().el().style.flexShrink="",D.view().el().style.height=""}),e.config().manualSwap&&e.config().swapMode==="drop"?requestAnimationFrame(Y):Y();function Y(){const D=n.currentTransform(),F=D.dragX+D.translateX,O=D.dragY+D.translateY;o.drop=ct({translate:R(F,O)},{translate:R(0,0)},({translate:z},U)=>{U?d||(n.clearTransform(),n.el().style.transformOrigin=""):n.setTransform({dragX:0,dragY:0,translateX:z.x,translateY:z.y}),U&&(e.items().forEach(St=>{St.isDragging()||(St.view().el().style.zIndex="")}),e.resetZIndex(),n.el().style.position="",u=!0)},gt(e.config().animation))}});function M(S){a=S}function b(S){c=S}function H(S){f=S}function _(S){m=S}function N(){return n.el().querySelector("[data-swapy-handle]")}function $(S){const Y=N();return Y?Y===S||Y.contains(S):!1}function q(){return N()!==null}function y(){return Array.from(n.el().querySelectorAll("[data-swapy-no-drag]"))}function B(S){const Y=y();return!Y||Y.length===0?!1:Y.includes(S)||Y.some(D=>D.contains(S))}function C(){return y().length>0}function V(){n.el().setAttribute("data-swapy-dragging","")}function nt(){n.el().removeAttribute("data-swapy-dragging")}function Vt(){a=null,c=null,f=null,m=null,s=null,r=null,l&&(l.destroy(),l=null),I&&x&&I.removeEventListener("scroll",x),n.destroy()}function mt(){return n.el().dataset.swapyItem}function ot(){return e.slotById(n.el().parentElement.dataset.swapySlot)}function wt(){return n.el().parentElement.dataset.swapySlot}return{id:mt,view:()=>n,slot:ot,slotId:wt,onDrag:M,onDrop:b,onHold:H,onRelease:_,destroy:Vt,isDragging:()=>d,cancelAnimation:()=>o,dragEvent:()=>s,store:()=>e,continuousDrag:()=>u,setContinuousDrag:S=>u=S}}function ht(t,e,n=!1){if(n){const l=e.item();l&&(e.view().el().style.position="relative",l.view().el().style.position="absolute")}else{const l=t.slot();l.view().el().style.position="",t.view().el().style.position=""}if(!t)return;const o=tt(t.view());o.readInitial(),e.view().el().appendChild(t.view().el()),o.readFinalAndReverse(),et(t,o)}function Ft(){let t=!1;return e=>{t||(t=!0,requestAnimationFrame(()=>{e(),t=!1}))}}function et(t,e){var u,s,h,a;(s=(u=t.cancelAnimation()).moveToSlot)==null||s.call(u),(a=(h=t.cancelAnimation()).drop)==null||a.call(h);const n=gt(t.store().config().animation),o=e.transitionValues();let l=t.view().currentTransform(),r=0,d=!1;t.cancelAnimation().moveToSlot=ct({translate:o.from.translate,scale:o.from.scale,borderRadius:o.from.borderRadius},{translate:o.to.translate,scale:o.to.scale,borderRadius:o.to.borderRadius},({translate:c,scale:f,borderRadius:m},v,g)=>{if(t.isDragging()){r!==0&&(d=!0);const i=t.dragEvent().relativeX,A=t.dragEvent().relativeY;t.continuousDrag()?t.view().setTransform({translateX:L(l.translateX,l.translateX+(o.from.width-o.to.width)*i,n.easing(g-r)),translateY:L(l.translateY,l.translateY+(o.from.height-o.to.height)*A,n.easing(g-r)),scaleX:f.x,scaleY:f.y}):t.view().setTransform({scaleX:f.x,scaleY:f.y})}else l=t.view().currentTransform(),r=g,d?t.view().setTransform({scaleX:f.x,scaleY:f.y}):t.view().setTransform({dragX:0,dragY:0,translateX:c.x,translateY:c.y,scaleX:f.x,scaleY:f.y});const X=e.childrenTransitionData();X.forEach(({el:i,fromTranslate:A,fromScale:E,fromBorderRadius:p,toBorderRadius:w,parentScale:I})=>{const x=L(I.x,1,n.easing(g)),T=L(I.y,1,n.easing(g));i.style.transform=`translate(${A.x+(0-A.x/x)*n.easing(g)}px, ${A.y+(0-A.y/T)*n.easing(g)}px) scale(${L(E.x/x,1/x,n.easing(g))}, ${L(E.y/T,1/T,n.easing(g))})`,rt(p)||(i.style.borderRadius=Z(at(p,w,n.easing(g))))}),rt(m)||(t.view().el().style.borderRadius=Z(m)),v&&(t.isDragging()||(t.view().el().style.transformOrigin="",t.view().clearTransform()),t.view().el().style.borderRadius="",X.forEach(({el:i})=>{i.style.transform="",i.style.transformOrigin="",i.style.borderRadius=""}))},n)}function k(...t){console.error("Swapy Error:",...t)}function jt(t){const e=t;let n=!0;const o=e.querySelectorAll("[data-swapy-slot]");e||(k("container passed to createSwapy() is undefined or null"),n=!1),o.forEach(u=>{const s=u,h=s.dataset.swapySlot,a=s.children,c=a[0];(!h||h.length===0)&&(k(s,"does not contain a slotId using data-swapy-slot"),n=!1),a.length>1&&(k("slot:",`"${h}"`,"cannot contain more than one element"),n=!1),c&&(!c.dataset.swapyItem||c.dataset.swapyItem.length===0)&&(k("slot",`"${h}"`,"does not contain an element with an item id using data-swapy-item"),n=!1)});const l=Array.from(o).map(u=>u.dataset.swapySlot),r=e.querySelectorAll("[data-swapy-item]"),d=Array.from(r).map(u=>u.dataset.swapyItem);if(pt(l)){const u=yt(l);k("your container has duplicate slot ids",`(${u.join(", ")})`),n=!1}if(pt(d)){const u=yt(d);k("your container has duplicate item ids",`(${u.join(", ")})`),n=!1}return n}function pt(t){return new Set(t).size!==t.length}function yt(t){const e=new Set,n=new Set;for(const o of t)e.has(o)?n.add(o):e.add(o);return Array.from(n)}function Wt(t,e){if(t.size!==e.size)return!1;for(const[n,o]of t)if(e.get(n)!==o)return!1;return!0}function J(t){let e=t;for(;e;){const n=window.getComputedStyle(e),o=n.overflowY,l=n.overflowX;if((o==="auto"||o==="scroll")&&e.scrollHeight>e.clientHeight||(l==="auto"||l==="scroll")&&e.scrollWidth>e.clientWidth)return e;e=e.parentElement}return window}function kt(t,e){let l=!1,r,d=0,u=0,s=0,h=0,a=0,c=0,f=null;t instanceof HTMLElement?(r=G(t.getBoundingClientRect()),d=t.scrollHeight-r.height,u=t.scrollWidth-r.width):(r={x:0,y:0,width:window.innerWidth,height:window.innerHeight},d=document.documentElement.scrollHeight-window.innerHeight,u=document.documentElement.scrollWidth-window.innerWidth);function m(){t instanceof HTMLElement?(s=t.scrollTop,h=t.scrollLeft):(s=window.scrollY,h=window.scrollX)}function v(i){l=!1;const A=r.y,E=r.y+r.height,p=r.x,w=r.x+r.width,I=Math.abs(A-i.y)=-100){const M=W(T,-100,0);a=-K(-100,0,0,5,M),l=!0}}else{const T=E-i.y;if(T<=100){const M=W(T,0,100);a=K(100,0,0,5,M),l=!0}}if(e!=="y")if(x){const T=p-i.x;if(T>=-100){const M=W(T,-100,0);c=-K(-100,0,0,5,M),l=!0}}else{const T=w-i.x;if(T<=100){const M=W(T,0,100);c=K(100,0,0,5,M),l=!0}}l&&(f&&cancelAnimationFrame(f),g())}function g(){m(),e!=="x"&&(a=s+a>=d?0:a),e!=="y"&&(c=h+c>=u?0:c),t.scrollBy({top:a,left:c}),l&&(f=requestAnimationFrame(g))}function X(){l=!1}return{updatePointer:v,destroy:X}}P.createSwapy=_t,P.getClosestScrollableContainer=J,P.utils=Bt,Object.defineProperty(P,Symbol.toStringTag,{value:"Module"})}); + `}function it(t){return t.x.topLeft===0&&t.x.topRight===0&&t.x.bottomRight===0&&t.x.bottomLeft===0&&t.y.topLeft===0&&t.y.topRight===0&&t.y.bottomRight===0&&t.y.bottomLeft===0}function at(t){return typeof t=="object"&&"x"in t&&"y"in t}function R(t,e){return{x:t,y:e}}function xt(t,e){return R(t.x+e.x,t.y+e.y)}function vt(t,e){return R(t.x-e.x,t.y-e.y)}function At(t,e){return R(t.x*e,t.y*e)}function L(t,e,n){return t+(e-t)*n}function It(t,e,n){return xt(t,At(vt(e,t),n))}function st(t,e,n){return{x:{topLeft:L(t.x.topLeft,e.x.topLeft,n),topRight:L(t.x.topRight,e.x.topRight,n),bottomRight:L(t.x.bottomRight,e.x.bottomRight,n),bottomLeft:L(t.x.bottomLeft,e.x.bottomLeft,n)},y:{topLeft:L(t.y.topLeft,e.y.topLeft,n),topRight:L(t.y.topRight,e.y.topRight,n),bottomRight:L(t.y.bottomRight,e.y.bottomRight,n),bottomLeft:L(t.y.bottomLeft,e.y.bottomLeft,n)},unit:t.unit}}function Et(t,e,n){return W((n-t)/(e-t),0,1)}function K(t,e,n,o,l){return L(n,o,Et(t,e,l))}function W(t,e,n){return Math.min(Math.max(t,e),n)}const Tt={duration:350,easing:t=>t};function ct(t,e,n,o){let l=!1;const i=()=>{l=!0},d={...Tt,...o};let u;function a(h){u===void 0&&(u=h);const s=h-u,c=W(s/d.duration,0,1),f=Object.keys(t),m=Object.keys(e);if(!f.every(g=>m.includes(g))){console.error("animate Error: `from` keys are different than `to`");return}const v={};f.forEach(g=>{typeof t[g]=="number"&&typeof e[g]=="number"?v[g]=L(t[g],e[g],d.easing(c)):j(t[g])&&j(e[g])?v[g]=st(t[g],e[g],d.easing(c)):at(t[g])&&at(e[g])&&(v[g]=It(t[g],e[g],d.easing(c)))}),n(v,c>=1,c),c<1&&!l&&requestAnimationFrame(a)}return requestAnimationFrame(a),i}const Xt={startDelay:0,targetEl:null};function Mt(t,e){const n={...Xt,...e};let o=t.el(),l=!1,i=null,d=null,u=null,a=null,h=0,s=0,c=0,f=0,m=0,v=0,g=0,X=0,r=0,A=0,E=null,p;o.addEventListener("pointerdown",w),document.body.addEventListener("pointerup",x),document.body.addEventListener("pointermove",T),document.body.addEventListener("touchmove",M,{passive:!1});function w(y){if(n.targetEl&&y.target!==n.targetEl||l||!y.isPrimary)return;n.startDelay>0?(u==null||u({el:y.target}),p=setTimeout(()=>{B()},n.startDelay)):B();function B(){E=y.target;const C=t.boundingRect(),V=t.layoutRect();m=V.x,v=V.y,c=C.x-m,f=C.y-v,h=y.clientX-c,s=y.clientY-f,g=y.clientX,X=y.clientY,r=(y.clientX-C.x)/C.width,A=(y.clientY-C.y)/C.height,l=!0,T(y)}}function I(){const y=t.layoutRect();h-=m-y.x,s-=v-y.y,m=y.x,v=y.y}function x(y){if(!l){p&&(clearTimeout(p),p=null,a==null||a({el:y.target}));return}if(!y.isPrimary)return;l=!1;const B=y.clientX-g,C=y.clientY-X;d==null||d({x:c,y:f,pointerX:y.clientX,pointerY:y.clientY,width:B,height:C,relativeX:r,relativeY:A,el:E}),E=null}function T(y){if(!l){p&&(clearTimeout(p),p=null,a==null||a({el:y.target}));return}if(!y.isPrimary)return;const B=y.clientX-g,C=y.clientY-X,V=c=y.clientX-h,nt=f=y.clientY-s;i==null||i({width:B,height:C,x:V,y:nt,pointerX:y.clientX,pointerY:y.clientY,relativeX:r,relativeY:A,el:E})}function M(y){if(!l)return!0;y.preventDefault()}function b(y){i=y}function H(y){d=y}function _(y){u=y}function P(y){a=y}function $(){o.removeEventListener("pointerdown",w),o=t.el(),o.addEventListener("pointerdown",w)}function q(){t.el().removeEventListener("pointerdown",w),document.body.removeEventListener("pointerup",x),document.body.removeEventListener("pointermove",T),document.body.removeEventListener("touchmove",M),i=null,d=null,u=null,a=null}return{onDrag:b,onDrop:H,onHold:_,onRelease:P,onElementUpdate:$,destroy:q,readjust:I}}function bt(t){return 1+2.70158*Math.pow(t-1,3)+1.70158*Math.pow(t-1,2)}function Dt(t){return 1-Math.pow(1-t,3)}function G(t){return{x:t.x,y:t.y,width:t.width,height:t.height}}function Lt(t){const e=t.getBoundingClientRect();let n=0,o=0,l=t.parentElement;for(;l;){const d=getComputedStyle(l).transform;if(d&&d!=="none"){const u=d.match(/matrix.*\((.+)\)/);if(u){const a=u[1].split(", ").map(Number);n+=a[4]||0,o+=a[5]||0}}l=l.parentElement}return{y:e.top-o,x:e.left-n,width:e.width,height:e.height}}function Q(t){let e=t,n=0,o=0;for(;e;)n+=e.offsetTop,o+=e.offsetLeft,e=e.offsetParent;return{x:o,y:n,width:t.offsetWidth,height:t.offsetHeight}}function ft(t,e){return t.x>=e.x&&t.x<=e.x+e.width&&t.y>=e.y&&t.y<=e.y+e.height}function Ct(t){let e=t,n=0,o=0;for(;e;){const l=i=>{const d=getComputedStyle(i);return/(auto|scroll)/.test(d.overflow+d.overflowY+d.overflowX)};if(e===document.body){o+=window.scrollX,n+=window.scrollY;break}l(e)&&(o+=e.scrollLeft,n+=e.scrollTop),e=e.parentElement}return{x:o,y:n}}function tt(t){let e="unread",n,o,l,i,d,u,a,h,s,c,f;function m(){n=t.currentTransform(),o=Lt(t.el()),l=Ct(t.el()),f=ut(t.el()).map(({parent:A,children:E})=>({parent:{el:A,initialRect:G(A.getBoundingClientRect())},children:E.filter(p=>p instanceof HTMLElement).map(p=>{const w=p;return w.originalBorderRadius||(w.originalBorderRadius=getComputedStyle(p).borderRadius),{el:p,borderRadius:lt(w.originalBorderRadius),initialRect:G(p.getBoundingClientRect())}})})),e="readInitial"}function v(){if(e!=="readInitial")throw new Error("FlipView: Cannot read final values before reading initial values");s=t.layoutRect(),u=o.width/s.width,a=o.height/s.height,i=o.x-s.x-n.dragX+l.x,d=o.y-s.y-n.dragY+l.y,h=rt(t.borderRadius(),u,a);const r=ut(t.el());f=f.map(({parent:E,children:p},w)=>{const I=r[w].parent;return{parent:{...E,el:I,finalRect:Q(I)},children:p.map((x,T)=>{const M=r[w].children[T];let b=Q(M);return M.hasAttribute("data-swapy-text")&&(b={...b,width:x.initialRect.width,height:x.initialRect.height}),{...x,el:M,finalRect:b}})}});const A={translateX:i,translateY:d,scaleX:u,scaleY:a};t.el().style.transformOrigin="0 0",t.el().style.borderRadius=Z(h),t.setTransform(A),c=[],f.forEach(({parent:E,children:p})=>{const w=p.map(({el:I,initialRect:x,finalRect:T,borderRadius:M})=>Yt(I,x,T,M,E.initialRect,E.finalRect));c.push(...w)}),e="readFinal"}function g(){if(e!=="readFinal")throw new Error("FlipView: Cannot get transition values before reading");return{from:{width:o.width,height:o.height,translate:R(i,d),scale:R(u,a),borderRadius:h},to:{width:s.width,height:s.height,translate:R(0,0),scale:R(1,1),borderRadius:t.borderRadius()}}}function X(){if(e!=="readFinal")throw new Error("FlipView: Cannot get children transition values before reading");return c}return{readInitial:m,readFinalAndReverse:v,transitionValues:g,childrenTransitionData:X}}function Yt(t,e,n,o,l,i){t.style.transformOrigin="0 0";const d=l.width/i.width,u=l.height/i.height,a=e.width/n.width,h=e.height/n.height,s=rt(o,a,h),c=e.x-l.x,f=n.x-i.x,m=e.y-l.y,v=n.y-i.y,g=(c-f*d)/d,X=(m-v*u)/u;return t.style.transform=`translate(${g}px, ${X}px) scale(${a/d}, ${h/u})`,t.style.borderRadius=Z(s),{el:t,fromTranslate:R(g,X),fromScale:R(a,h),fromBorderRadius:s,toBorderRadius:o,parentScale:{x:d,y:u}}}function ut(t){const e=[];function n(o){const l=Array.from(o.children).filter(i=>i instanceof HTMLElement);l.length>0&&(e.push({parent:o,children:l}),l.forEach(i=>n(i)))}return n(t),e}function dt(t){const e=[];let n=t,o={dragX:0,dragY:0,translateX:0,translateY:0,scaleX:1,scaleY:1};const l=lt(window.getComputedStyle(n).borderRadius),i={el:()=>n,setTransform:d,clearTransform:u,currentTransform:()=>o,borderRadius:()=>l,layoutRect:()=>Q(n),boundingRect:()=>G(n.getBoundingClientRect()),usePlugin:h,destroy:s,updateElement:c};function d(f){o={...o,...f},a()}function u(){o={dragX:0,dragY:0,translateX:0,translateY:0,scaleX:1,scaleY:1},a()}function a(){const{dragX:f,dragY:m,translateX:v,translateY:g,scaleX:X,scaleY:r}=o;f===0&&m===0&&v===0&&g===0&&X===1&&r===1?n.style.transform="":n.style.transform=`translate(${f+v}px, ${m+g}px) scale(${X}, ${r})`}function h(f,m){const v=f(i,m);return e.push(v),v}function s(){e.forEach(f=>f.destroy())}function c(f){if(!f)return;const m=n.hasAttribute("data-swapy-dragging"),v=n.style.cssText;n=f,m&&n.setAttribute("data-swapy-dragging",""),n.style.cssText=v,e.forEach(g=>g.onElementUpdate())}return i}function Rt(t,e,n){return n.map(o=>({slotId:o.slot,itemId:o.item,item:o.item===""?null:t.find(l=>o.item===l[e])}))}function Ht(t,e){return t.map(n=>({item:n[e],slot:n[e]}))}function $t(t,e,n,o,l,i=!1){const d=e.filter(h=>!o.some(s=>s.item===h[n])).map(h=>({slot:h[n],item:h[n]}));let u;i?u=o.map(h=>e.some(s=>s[n]===h.item)?h:{slot:h.slot,item:""}):u=o.filter(h=>e.some(s=>s[n]===h.item)||!h.item);const a=[...u,...d];l(a),(d.length>0||u.length!==o.length)&&requestAnimationFrame(()=>{t==null||t.update()})}const Bt=Object.freeze(Object.defineProperty({__proto__:null,dynamicSwapy:$t,initSlotItemMap:Ht,toSlottedItems:Rt},Symbol.toStringTag,{value:"Module"})),Ot={animation:"dynamic",enabled:!0,swapMode:"hover",dragOnHold:!1,autoScrollOnDrag:!1,dragAxis:"both",manualSwap:!1};function gt(t){switch(t){case"dynamic":return{easing:Dt,duration:300};case"spring":return{easing:bt,duration:350};case"none":return{easing:e=>e,duration:1}}}function _t(t,e){const n={...Ot,...e},o=Pt({slots:[],items:[],config:n});let l=[],i=[];d();function d(){if(!jt(t))throw new Error("Cannot create a Swapy instance because your HTML structure is invalid. Fix all above errors and then try!");l=Array.from(t.querySelectorAll("[data-swapy-slot]")).map(r=>Nt(r,o)),o.setSlots(l),i=Array.from(t.querySelectorAll("[data-swapy-item]")).map(r=>qt(r,o)),o.setItems(i),o.syncSlotItemMap(),i.forEach(r=>{r.onDrag(({pointerX:A,pointerY:E})=>{a();let p=!1;l.forEach(w=>{const I=w.rect();ft({x:A,y:E},I)&&(p=!0,w.isHighlighted()||w.highlight())}),!p&&o.config().swapMode==="drop"&&r.slot().highlight(),n.swapMode==="hover"&&u(r,{pointerX:A,pointerY:E})}),r.onDrop(({pointerX:A,pointerY:E})=>{h(),n.swapMode==="drop"&&u(r,{pointerX:A,pointerY:E})}),r.onHold(()=>{a()}),r.onRelease(()=>{h()})})}function u(r,{pointerX:A,pointerY:E}){l.forEach(p=>{const w=p.rect();if(ft({x:A,y:E},w)){if(r.id()===p.itemId())return;o.config().swapMode==="hover"&&r.setContinuousDrag(!0);const I=r.slot(),x=p.item();if(!o.eventHandlers().onBeforeSwap({fromSlot:I.id(),toSlot:p.id(),draggingItem:r.id(),swapWithItem:(x==null?void 0:x.id())||""}))return;if(o.config().manualSwap){const T=structuredClone(o.slotItemMap());o.swapItems(r,p);const M=o.slotItemMap(),b=tt(r.view());b.readInitial();const H=x?tt(x.view()):null;H==null||H.readInitial();let _=0,P=0;const $=J(r.view().el());$ instanceof Window?(_=$.scrollY,P=$.scrollX):(_=$.scrollTop,P=$.scrollLeft),o.eventHandlers().onSwap({oldSlotItemMap:T,newSlotItemMap:M,fromSlot:I.id(),toSlot:p.id(),draggingItem:r.id(),swappedWithItem:(x==null?void 0:x.id())||""}),requestAnimationFrame(()=>{const q=t.querySelectorAll("[data-swapy-item]");o.items().forEach(y=>{const B=Array.from(q).find(C=>C.dataset.swapyItem===y.id());y.view().updateElement(B)}),o.syncSlotItemMap(),b.readFinalAndReverse(),H==null||H.readFinalAndReverse(),et(r,b),x&&H&&et(x,H),$.scrollTo({left:P,top:_})})}else{let T=0,M=0;const b=J(r.view().el());b instanceof Window?(T=b.scrollY,M=b.scrollX):(T=b.scrollTop,M=b.scrollLeft),ht(r,p,!0),x&&ht(x,I),b.scrollTo({left:M,top:T});const H=o.slotItemMap();o.syncSlotItemMap();const _=o.slotItemMap();o.eventHandlers().onSwap({oldSlotItemMap:H,newSlotItemMap:_,fromSlot:I.id(),toSlot:p.id(),draggingItem:r.id(),swappedWithItem:(x==null?void 0:x.id())||""})}}})}function a(){t.querySelectorAll("img").forEach(r=>{r.style.pointerEvents="none"}),t.style.userSelect="none",t.style.webkitUserSelect="none"}function h(){t.querySelectorAll("img").forEach(r=>{r.style.pointerEvents=""}),t.style.userSelect="",t.style.webkitUserSelect=""}function s(r){o.config().enabled=r}function c(r){o.eventHandlers().onSwapStart=r}function f(r){o.eventHandlers().onSwap=r}function m(r){o.eventHandlers().onSwapEnd=r}function v(r){o.eventHandlers().onBeforeSwap=r}function g(){X(),requestAnimationFrame(()=>{d()})}function X(){i.forEach(r=>r.destroy()),l.forEach(r=>r.destroy()),o.destroy(),i=[],l=[]}return{enable:s,slotItemMap:()=>o.slotItemMap(),onSwapStart:c,onSwap:f,onSwapEnd:m,onBeforeSwap:v,update:g,destroy:X}}function Pt({slots:t,items:e,config:n}){const o={slots:t,items:e,config:n,slotItemMap:{asObject:{},asMap:new Map,asArray:[]},zIndexCount:1,eventHandlers:{onSwapStart:()=>{},onSwap:()=>{},onSwapEnd:()=>{},onBeforeSwap:()=>!0},scrollOffsetWhileDragging:{x:0,y:0},scrollHandler:null};let l={...o};const i=c=>{var f;(f=l.scrollHandler)==null||f.call(l,c)};window.addEventListener("scroll",i);function d(c){return l.slots.find(f=>f.id()===c)}function u(c){return l.items.find(f=>f.id()===c)}function a(){const c={},f=new Map,m=[];l.slots.forEach(v=>{var r;const g=v.id(),X=((r=v.item())==null?void 0:r.id())||"";c[g]=X,f.set(g,X),m.push({slot:g,item:X})}),l.slotItemMap={asObject:c,asMap:f,asArray:m}}function h(c,f){var p;const m=l.slotItemMap,v=c.id(),g=((p=f.item())==null?void 0:p.id())||"",X=f.id(),r=c.slot().id();m.asObject[X]=v,m.asObject[r]=g,m.asMap.set(X,v),m.asMap.set(r,g);const A=m.asArray.findIndex(w=>w.slot===X),E=m.asArray.findIndex(w=>w.slot===r);m.asArray[A].item=v,m.asArray[E].item=g}function s(){window.removeEventListener("scroll",i),l={...o}}return{slots:()=>l.slots,items:()=>l.items,config:()=>n,setItems:c=>l.items=c,setSlots:c=>l.slots=c,slotById:d,itemById:u,zIndex:(c=!1)=>c?++l.zIndexCount:l.zIndexCount,resetZIndex:()=>{l.zIndexCount=1},eventHandlers:()=>l.eventHandlers,syncSlotItemMap:a,slotItemMap:()=>l.slotItemMap,onScroll:c=>{l.scrollHandler=c},swapItems:h,destroy:s}}function Nt(t,e){const n=dt(t);function o(){return n.el().dataset.swapySlot}function l(){const s=n.el().children[0];return(s==null?void 0:s.dataset.swapyItem)||null}function i(){return G(n.el().getBoundingClientRect())}function d(){const s=n.el().children[0];if(s)return e.itemById(s.dataset.swapyItem)}function u(){e.slots().forEach(s=>{s.view().el().removeAttribute("data-swapy-highlighted")})}function a(){u(),n.el().setAttribute("data-swapy-highlighted","")}function h(){}return{id:o,view:()=>n,itemId:l,rect:i,item:d,highlight:a,unhighlightAllSlots:u,isHighlighted:()=>n.el().hasAttribute("data-swapy-highlighted"),destroy:h}}function qt(t,e){const n=dt(t),o={};let l=null,i=null,d=!1,u=!0,a;const h=Ft();let s=()=>{},c=()=>{},f=()=>{},m=()=>{};const{onDrag:v,onDrop:g,onHold:X,onRelease:r}=n.usePlugin(Mt,{startDelay:e.config().dragOnHold?400:0,targetEl:P()}),A=R(0,0),E=R(0,0),p=R(0,0),w=R(0,0);let I=null,x=null;X(S=>{e.config().enabled&&(q()&&!$(S.el)||C()&&B(S.el)||f==null||f(S))}),r(S=>{e.config().enabled&&(q()&&!$(S.el)||C()&&B(S.el)||m==null||m(S))});function T(S){var F;V(),ot().highlight(),(F=o.drop)==null||F.call(o);const Y=e.slots().map(O=>O.view().boundingRect());e.slots().forEach((O,z)=>{const U=Y[z];O.view().el().style.width=`${U.width}px`,O.view().el().style.maxWidth=`${U.width}px`,O.view().el().style.flexShrink="0",O.view().el().style.height=`${U.height}px`});const D=e.slotItemMap();e.eventHandlers().onSwapStart({draggingItem:mt(),fromSlot:wt(),slotItemMap:D}),i=D,n.el().style.position="relative",n.el().style.zIndex=`${e.zIndex(!0)}`,I=J(S.el),e.config().autoScrollOnDrag&&(l=kt(I,e.config().dragAxis),l.updatePointer({x:S.pointerX,y:S.pointerY})),A.x=window.scrollX,A.y=window.scrollY,p.x=0,p.y=0,I instanceof HTMLElement&&(E.x=I.scrollLeft,E.y=I.scrollTop,x=()=>{w.x=I.scrollLeft-E.x,w.y=I.scrollTop-E.y,n.setTransform({dragX:((a==null?void 0:a.width)||0)+p.x+w.x,dragY:((a==null?void 0:a.height)||0)+p.y+w.y})},I.addEventListener("scroll",x)),e.onScroll(()=>{p.x=window.scrollX-A.x,p.y=window.scrollY-A.y;const O=w.x||0,z=w.y||0;n.setTransform({dragX:((a==null?void 0:a.width)||0)+p.x+O,dragY:((a==null?void 0:a.height)||0)+p.y+z})})}v(S=>{var Y;if(e.config().enabled){if(!d){if(q()&&!$(S.el)||C()&&B(S.el))return;T(S)}d=!0,l&&l.updatePointer({x:S.pointerX,y:S.pointerY}),a=S,(Y=o.drop)==null||Y.call(o),h(()=>{n.el().style.position="relative";const D=S.width+p.x+w.x,F=S.height+p.y+w.y;e.config().dragAxis==="y"?n.setTransform({dragY:F}):e.config().dragAxis==="x"?n.setTransform({dragX:D}):n.setTransform({dragX:D,dragY:F}),s==null||s(S)})}}),g(S=>{if(!d)return;nt(),d=!1,u=!1,a=null,I&&(I.removeEventListener("scroll",x),x=null),I=null,w.x=0,w.y=0,p.x=0,p.y=0,l&&(l.destroy(),l=null),ot().unhighlightAllSlots(),c==null||c(S),e.eventHandlers().onSwapEnd({slotItemMap:e.slotItemMap(),hasChanged:i!=null&&i.asMap?!Wt(i==null?void 0:i.asMap,e.slotItemMap().asMap):!1}),i=null,e.onScroll(null),e.slots().forEach(D=>{D.view().el().style.width="",D.view().el().style.maxWidth="",D.view().el().style.flexShrink="",D.view().el().style.height=""}),e.config().manualSwap&&e.config().swapMode==="drop"?requestAnimationFrame(Y):Y();function Y(){const D=n.currentTransform(),F=D.dragX+D.translateX,O=D.dragY+D.translateY;o.drop=ct({translate:R(F,O)},{translate:R(0,0)},({translate:z},U)=>{U?d||(n.clearTransform(),n.el().style.transformOrigin=""):n.setTransform({dragX:0,dragY:0,translateX:z.x,translateY:z.y}),U&&(e.items().forEach(St=>{St.isDragging()||(St.view().el().style.zIndex="")}),e.resetZIndex(),n.el().style.position="",u=!0)},gt(e.config().animation))}});function M(S){s=S}function b(S){c=S}function H(S){f=S}function _(S){m=S}function P(){return n.el().querySelector("[data-swapy-handle]")}function $(S){const Y=P();return Y?Y===S||Y.contains(S):!1}function q(){return P()!==null}function y(){return Array.from(n.el().querySelectorAll("[data-swapy-no-drag]"))}function B(S){const Y=y();return!Y||Y.length===0?!1:Y.includes(S)||Y.some(D=>D.contains(S))}function C(){return y().length>0}function V(){n.el().setAttribute("data-swapy-dragging","")}function nt(){n.el().removeAttribute("data-swapy-dragging")}function Vt(){s=null,c=null,f=null,m=null,a=null,i=null,l&&(l.destroy(),l=null),I&&x&&I.removeEventListener("scroll",x),n.destroy()}function mt(){return n.el().dataset.swapyItem}function ot(){return e.slotById(n.el().parentElement.dataset.swapySlot)}function wt(){return n.el().parentElement.dataset.swapySlot}return{id:mt,view:()=>n,slot:ot,slotId:wt,onDrag:M,onDrop:b,onHold:H,onRelease:_,destroy:Vt,isDragging:()=>d,cancelAnimation:()=>o,dragEvent:()=>a,store:()=>e,continuousDrag:()=>u,setContinuousDrag:S=>u=S}}function ht(t,e,n=!1){if(n){const l=e.item();l&&(e.view().el().style.position="relative",l.view().el().style.position="absolute")}else{const l=t.slot();l.view().el().style.position="",t.view().el().style.position=""}if(!t)return;const o=tt(t.view());o.readInitial(),e.view().el().appendChild(t.view().el()),o.readFinalAndReverse(),et(t,o)}function Ft(){let t=!1;return e=>{t||(t=!0,requestAnimationFrame(()=>{e(),t=!1}))}}function et(t,e){var u,a,h,s;(a=(u=t.cancelAnimation()).moveToSlot)==null||a.call(u),(s=(h=t.cancelAnimation()).drop)==null||s.call(h);const n=gt(t.store().config().animation),o=e.transitionValues();let l=t.view().currentTransform(),i=0,d=!1;t.cancelAnimation().moveToSlot=ct({translate:o.from.translate,scale:o.from.scale,borderRadius:o.from.borderRadius},{translate:o.to.translate,scale:o.to.scale,borderRadius:o.to.borderRadius},({translate:c,scale:f,borderRadius:m},v,g)=>{if(t.isDragging()){i!==0&&(d=!0);const r=t.dragEvent().relativeX,A=t.dragEvent().relativeY;t.continuousDrag()?t.view().setTransform({translateX:L(l.translateX,l.translateX+(o.from.width-o.to.width)*r,n.easing(g-i)),translateY:L(l.translateY,l.translateY+(o.from.height-o.to.height)*A,n.easing(g-i)),scaleX:f.x,scaleY:f.y}):t.view().setTransform({scaleX:f.x,scaleY:f.y})}else l=t.view().currentTransform(),i=g,d?t.view().setTransform({scaleX:f.x,scaleY:f.y}):t.view().setTransform({dragX:0,dragY:0,translateX:c.x,translateY:c.y,scaleX:f.x,scaleY:f.y});const X=e.childrenTransitionData();X.forEach(({el:r,fromTranslate:A,fromScale:E,fromBorderRadius:p,toBorderRadius:w,parentScale:I})=>{const x=L(I.x,1,n.easing(g)),T=L(I.y,1,n.easing(g));r.style.transform=`translate(${A.x+(0-A.x/x)*n.easing(g)}px, ${A.y+(0-A.y/T)*n.easing(g)}px) scale(${L(E.x/x,1/x,n.easing(g))}, ${L(E.y/T,1/T,n.easing(g))})`,it(p)||(r.style.borderRadius=Z(st(p,w,n.easing(g))))}),it(m)||(t.view().el().style.borderRadius=Z(m)),v&&(t.isDragging()||(t.view().el().style.transformOrigin="",t.view().clearTransform()),t.view().el().style.borderRadius="",X.forEach(({el:r})=>{r.style.transform="",r.style.transformOrigin="",r.style.borderRadius=""}))},n)}function k(...t){console.error("Swapy Error:",...t)}function jt(t){const e=t;let n=!0;const o=e.querySelectorAll("[data-swapy-slot]");e||(k("container passed to createSwapy() is undefined or null"),n=!1),o.forEach(u=>{const a=u,h=a.dataset.swapySlot,s=a.children,c=s[0];(!h||h.length===0)&&(k(a,"does not contain a slotId using data-swapy-slot"),n=!1),s.length>1&&(k("slot:",`"${h}"`,"cannot contain more than one element"),n=!1),c&&(!c.dataset.swapyItem||c.dataset.swapyItem.length===0)&&(k("slot",`"${h}"`,"does not contain an element with an item id using data-swapy-item"),n=!1)});const l=Array.from(o).map(u=>u.dataset.swapySlot),i=e.querySelectorAll("[data-swapy-item]"),d=Array.from(i).map(u=>u.dataset.swapyItem);if(pt(l)){const u=yt(l);k("your container has duplicate slot ids",`(${u.join(", ")})`),n=!1}if(pt(d)){const u=yt(d);k("your container has duplicate item ids",`(${u.join(", ")})`),n=!1}return n}function pt(t){return new Set(t).size!==t.length}function yt(t){const e=new Set,n=new Set;for(const o of t)e.has(o)?n.add(o):e.add(o);return Array.from(n)}function Wt(t,e){if(t.size!==e.size)return!1;for(const[n,o]of t)if(e.get(n)!==o)return!1;return!0}function J(t){let e=t;for(;e;){const n=window.getComputedStyle(e),o=n.overflowY,l=n.overflowX;if((o==="auto"||o==="scroll")&&e.scrollHeight>e.clientHeight||(l==="auto"||l==="scroll")&&e.scrollWidth>e.clientWidth)return e;e=e.parentElement}return window}function kt(t,e){let l=!1,i,d=0,u=0,a=0,h=0,s=0,c=0,f=null;t instanceof HTMLElement?(i=G(t.getBoundingClientRect()),d=t.scrollHeight-i.height,u=t.scrollWidth-i.width):(i={x:0,y:0,width:window.innerWidth,height:window.innerHeight},d=document.documentElement.scrollHeight-window.innerHeight,u=document.documentElement.scrollWidth-window.innerWidth);function m(){t instanceof HTMLElement?(a=t.scrollTop,h=t.scrollLeft):(a=window.scrollY,h=window.scrollX)}function v(r){l=!1;const A=i.y,E=i.y+i.height,p=i.x,w=i.x+i.width,I=Math.abs(A-r.y)=-100){const M=W(T,-100,0);s=-K(-100,0,0,5,M),l=!0}}else{const T=E-r.y;if(T<=100){const M=W(T,0,100);s=K(100,0,0,5,M),l=!0}}if(e!=="y")if(x){const T=p-r.x;if(T>=-100){const M=W(T,-100,0);c=-K(-100,0,0,5,M),l=!0}}else{const T=w-r.x;if(T<=100){const M=W(T,0,100);c=K(100,0,0,5,M),l=!0}}l&&(f&&cancelAnimationFrame(f),g())}function g(){m(),e!=="x"&&(s=a+s>=d?0:s),e!=="y"&&(c=h+c>=u?0:c),t.scrollBy({top:s,left:c}),l&&(f=requestAnimationFrame(g))}function X(){l=!1}return{updatePointer:v,destroy:X}}N.createSwapy=_t,N.getClosestScrollableContainer=J,N.utils=Bt,Object.defineProperty(N,Symbol.toStringTag,{value:"Module"})}); diff --git a/package.json b/package.json index 88c95aa..bdf697a 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "swapy", "author": "Taha Shashtari (taha@tahazsh.com)", "private": false, - "version": "1.0.2", + "version": "1.0.3", "type": "module", "main": "./dist/swapy.min.js", "module": "./dist/swapy.js",