UNPKG

@snapdrag/core

Version:

Core logic for Snapdrag drag and drop library

2 lines 4.21 kB
'use strict';var s="data-draggable",p="data-droppable",d="data-droppable-force";var l=new WeakMap,g=class{constructor(t){this.config=t;this.setConfig=t=>{this.config=t;};this.listen=t=>(t.setAttribute(p,"true"),l.set(t,this),()=>{t.removeAttribute(p),l.delete(t);});}get data(){return this.config.data}get disabled(){return !!this.config.disabled}};function b(n){return new g(n)}function f(n,t){return n.addEventListener("pointerdown",t),()=>{n.removeEventListener("pointerdown",t);}}function u(n){return document.addEventListener("pointermove",n),()=>{document.removeEventListener("pointermove",n);}}function E(n){return document.addEventListener("pointerup",n),()=>{document.removeEventListener("pointerup",n);}}function D(n){return document.addEventListener("pointercancel",n),()=>{document.removeEventListener("pointercancel",n);}}var h=class{constructor(t){this.config=t;this.n=null;this.o=false;this.p=null;this.i=new Map;this.l=new Map;this.g=null;this.a=[];this.r=null;this.s=new Set;this.t=new Map;this.T=t=>{try{this.H(t);}catch(r){throw this.e(),r}};this.b=t=>{try{this.A(t);}catch(r){throw this.e(),r}};this.P=t=>{try{this.M(t);}catch(r){throw this.e(),r}};this.L=t=>{try{this.v(t);}catch(r){throw this.e(),r}};this.setConfig=t=>{this.config=t;};this.listen=(t,r=true)=>{let i=(this.config.pointerConfig?.pointerDown??f)(t,this.L);return r&&!t.hasAttribute(s)&&t.setAttribute(s,"true"),()=>{this.e(),i(),r&&t.removeAttribute(s);}};}h(t){let e=document.elementsFromPoint(t.x,t.y)??[],i=new Map;return e.forEach(o=>{let a=o.getAttribute(p);if(!a||a==="false")return;let c=l.get(o);c&&i.set(o,c);}),i}u(t){let r={event:t,dragElement:this.n,dragStartEvent:this.p,data:this.r};return this.config.shouldDrag&&!this.config.shouldDrag(r)?false:(this.config.onDragStart?.(r),this.a.forEach(e=>{e.onDragStart?.(r);}),true)}f(t){let r={event:t,dragElement:this.n,dragStartEvent:this.p,dropTargets:this.t,data:this.r};this.config.onDragMove?.(r),this.a.forEach(e=>{e.onDragMove?.(r);});}c(t){let r=this.d(t);this.t.forEach((i,o)=>{i.config.onDrop?.({...r,dropTarget:i,dropElement:o});});let e={event:t,dragElement:r.dragElement,dragStartEvent:r.dragStartEvent,dropTargets:r.dropTargets,data:this.r};this.config.onDragEnd?.(e),this.a.forEach(i=>{i.onDragEnd?.(e);});}d(t){return {event:t,dragElement:this.n,dragStartEvent:this.p,dropTargets:this.t,sourceType:this.config.kind,sourceData:this.r}}E(t){this.t.forEach((r,e)=>{let i={...t,dropTarget:r,dropElement:e};this.l.has(e)?r.config.onDragMove?.(i):r.config.onDragIn?.(i);});}D(t){this.l.forEach((r,e)=>{this.i.has(e)||(this.t.delete(e),!this.s.delete(e)&&r.config.onDragOut?.({...t,dropTarget:r,dropElement:e}));});}v(t){if(!t.isPrimary)return;let{disabled:r,data:e,pointerConfig:i}=this.config;if(r)return;let a=t.target.closest(`[${s}]`);if(!a||a.getAttribute(s)==="false")return;this.n=a,this.p=t,this.r=typeof e=="function"?e({dragElement:a,dragStartEvent:t}):e;let c=i?.pointerMove??u,v=i?.pointerUp??E,T=i?.pointerCancel??D,P=c(this.T),m=v(this.b),A=T(this.P);this.g=()=>{P(),m(),A();},this.a=this.config.plugins?.slice()??[];}m(t){this.i.forEach((r,e)=>{if(this.s.has(e))return;if(r.disabled||e.closest(`[${d}="false"]`)){this.s.add(e);return}let{accepts:i}=r.config,o=true;if(Array.isArray(i)?o=i.includes(this.config.kind):typeof i=="function"?this.t.has(e)||(o=i({kind:this.config.kind,data:this.r,element:this.n,event:t})):o=i===this.config.kind,!o){this.s.add(e);return}this.t.set(e,r);});}H(t){if(!t.isPrimary)return;if(!this.o){if(!this.u(t))return;this.o=true;}this.i=this.h(t),this.m(t);let r=this.d(t);this.E(r),this.D(r),this.f(t),this.l=this.i;}A(t){t.isPrimary&&(this.o&&this.c(t),this.e());}M(t){if(t.isPrimary){if(this.o){let r=this.d(t);this.D(r),this.c(t);}this.e();}}e(){this.g?.(),this.g=null,this.n=null,this.o=false,this.i.clear(),this.l.clear(),this.r=null,this.s.clear(),this.t.clear(),this.a.forEach(t=>{t.cleanup?.();}),this.a=[];}};function H(n){return new h(n)}exports.DRAGGABLE_ATTRIBUTE=s;exports.DROPPABLE_ATTRIBUTE=p;exports.DROPPABLE_FORCE_ATTRIBUTE=d;exports.Draggable=h;exports.Droppable=g;exports.createDraggable=H;exports.createDroppable=b;//# sourceMappingURL=index.cjs.map //# sourceMappingURL=index.cjs.map