whisper.cat/weboasis/todo/scripts/AppDraggable.min.js
2023-10-05 23:28:32 +11:00

1 line
3.6 KiB
JavaScript

window.VT=window.VT||{},VT.AppDraggable=function(a,b){function c(b){if(!a.classList.contains("_nodrag")&&("mousedown"!==b.type||0===b.button)&&!(b.touches&&1<b.touches.length)){b.preventDefault();var c=w(b);z=x=c.clientX||c.pageX,A=y=c.clientY||c.pageY,B=Date.now(),f()}}function d(a){a.preventDefault();var b=w(a);if(z=b.clientX||b.pageX,A=b.clientY||b.pageY,L)return i(),void j();var c=z-x,d=A-y;return Math.abs(c)<I&&Math.abs(d)<I?void 0:a.touches&&50>Date.now()-B?void g():void(L=!0,C={},h(),i(),j(),k())}function e(a){a.preventDefault(),L||(a.target.click(),M=!0),g(),requestAnimationFrame(function(){M=!1,L&&(j(),m())})}function f(){a.addEventListener("touchmove",d),a.addEventListener("touchend",e),window.addEventListener("mousemove",d),window.addEventListener("mouseup",e)}function g(){a.removeEventListener("touchmove",d),a.removeEventListener("touchend",e),window.removeEventListener("mousemove",d),window.removeEventListener("mouseup",e)}function h(){o(a),a.dispatchEvent(new CustomEvent("draggableStart",{detail:n(),bubbles:!0}))}function i(){D.dispatchEvent(new CustomEvent("draggableDrag",{detail:n(),bubbles:!0}))}function j(){if(L){var a=u();a===H||(H&&(H.addEventListener("draggableLeave",q),H.dispatchEvent(new CustomEvent("draggableLeave",{detail:n(),bubbles:!0}))),a&&(a.addEventListener("draggableEnter",p),a.dispatchEvent(new CustomEvent("draggableEnter",{detail:n(),bubbles:!0}))),H=a)}}function k(){L&&(l(),setTimeout(l,50))}function l(){H&&H.dispatchEvent(new CustomEvent("draggableOver",{detail:n(),bubbles:!0})),setTimeout(l,50)}function m(){H?(H.addEventListener("draggableDrop",r),H.dispatchEvent(new CustomEvent("draggableDrop",{detail:n(),bubbles:!0}))):D.dispatchEvent(new CustomEvent("draggableCancel",{detail:n(),bubbles:!0}))}function n(){var b={el:a,data:C,image:D,imageSource:E,originX:x,originY:y,clientX:z,clientY:A,imageX:F,imageY:G,setImage:function(a){o(a),b.image=D}};return b}function o(a){if(E!==a){E=a,t(),D=E.cloneNode(!0),D.style.position="fixed",D.style.left="0",D.style.top="0",D.style.width=E.offsetWidth+"px",D.style.height=E.offsetHeight+"px",D.style.margin="0",D.style.zIndex=9999,D.classList.add("-dragging");var b=a.getBoundingClientRect();F=x-b.left,G=y-b.top,D.addEventListener("draggableDrag",function(a){var b=a.detail.clientX-a.detail.imageX,c=a.detail.clientY-a.detail.imageY;D.style.transition="none",D.style.transform="translate("+b+"px, "+c+"px)"}),D.addEventListener("draggableCancel",s),document.body.appendChild(D)}}function p(a){a.target.removeEventListener(a.type,p),a.target.classList.add("-drop")}function q(a){a.target.removeEventListener(a.type,p),a.target.classList.remove("-drop")}function r(a){a.target.removeEventListener(a.type,r),s()}function s(){H&&H.classList.remove("-drop"),t(),C=null,D=null,E=null,H=null}function t(){D&&D.parentNode&&D.parentNode.removeChild(D)}function u(){var a=[];return document.querySelectorAll(b.dropSelector).forEach(function(b){var c=b.getBoundingClientRect(),d=v(z,A,c);d>K||a.push({el:b,distance2:d})}),a.sort(function(c,a){return 0===c.distance2&&0===a.distance2?c.el.contains(a.el)?-1:a.el.contains(c.el)?1:0:c.distance2-a.distance2}),0<a.length?a[0].el:null}function v(a,b,c){var d=a<c.left?a-c.left:a>c.right?a-c.right:0,e=b<c.top?b-c.top:b>c.bottom?b-c.bottom:0;return d*d+e*e}function w(a){return a.targetTouches&&0<a.targetTouches.length?a.targetTouches[0]:a.changedTouches&&0<a.changedTouches.length?a.changedTouches[0]:a}var x,y,z,A,B,C,D,E,F,G,H,I=b.dragThreshold||5,J=b.dropRange||50,K=J*J,L=!1,M=!1;a.addEventListener("touchstart",c),a.addEventListener("mousedown",c),a.addEventListener("click",function(a){(L||M)&&(a.preventDefault(),a.stopImmediatePropagation())},!0)};