You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3 lines
11 KiB
JavaScript

/*! mobile-drag-drop 3.0.0-rc.0 | Copyright (c) 2022 Tim Ruffles | MIT License */
!function(t,i){"object"==typeof exports&&"undefined"!=typeof module?i(exports):"function"==typeof define&&define.amd?define(["exports"],i):i(t.MobileDragDrop=t.MobileDragDrop||{})}(this,function(t){"use strict";var c="dnd-poly-",s=["none","copy","copyLink","copyMove","link","linkMove","move","all"],f=["none","copy","move","link"];function i(){var t=!1;try{var i=Object.defineProperty({},"passive",{get:function(){t=!0}});window.addEventListener("test",null,i)}catch(t){}return t}var e=i();function l(t){return t&&t.tagName}function h(t,i,s){void 0===s&&(s=!0),document.addEventListener(t,i,!!e&&{passive:s})}function n(t,i){document.removeEventListener(t,i)}function u(t,i,s,n){void 0===n&&(n=!1);var h=e?{passive:!0,capture:n}:n;return t.addEventListener(i,s,h),{off:function(){t.removeEventListener(i,s,h)}}}function o(t){return 0===t.length?0:t.reduce(function(t,i){return i+t},0)/t.length}function r(t,i){for(var s=0;s<t.changedTouches.length;s++)if(t.changedTouches[s].identifier===i)return!0;return!1}function a(t,i,s){for(var n=[],h=[],e=0;e<i.touches.length;e++){var r=i.touches[e];n.push(r[t+"X"]),h.push(r[t+"Y"])}s.x=o(n),s.y=o(h)}var d=["","-webkit-"];function v(t,i,s,n,h){var e=i.x,i=i.y;n&&(e+=n.x,i+=n.y),(h=void 0===h||h)&&(e-=parseInt(t.offsetWidth,10)/2,i-=parseInt(t.offsetHeight,10)/2);for(var r="translate3d("+e+"px,"+i+"px, 0)",o=0;o<d.length;o++){var u=d[o]+"transform";t.style[u]=r+" "+s[o]}}var p=(Object.defineProperty(g.prototype,"dropEffect",{get:function(){return this.t},set:function(t){0!==this.i.mode&&-1<s.indexOf(t)&&(this.t=t)},enumerable:!1,configurable:!0}),Object.defineProperty(g.prototype,"types",{get:function(){if(0!==this.i.mode)return Object.freeze(this.i.types)},enumerable:!1,configurable:!0}),Object.defineProperty(g.prototype,"effectAllowed",{get:function(){return this.i.effectAllowed},set:function(t){2===this.i.mode&&-1<s.indexOf(t)&&(this.i.effectAllowed=t)},enumerable:!1,configurable:!0}),g.prototype.setData=function(t,i){if(2===this.i.mode){if(-1<t.indexOf(" "))throw new Error("illegal arg: type contains space");this.i.data[t]=i,-1===this.i.types.indexOf(t)&&this.i.types.push(t)}},g.prototype.getData=function(t){if(1===this.i.mode||2===this.i.mode)return this.i.data[t]||""},g.prototype.clearData=function(t){2===this.i.mode&&(t&&this.i.data[t]?(delete this.i.data[t],-1<(t=this.i.types.indexOf(t))&&this.i.types.splice(t,1)):(this.i.data={},this.i.types=[]))},g.prototype.setDragImage=function(t,i,s){2===this.i.mode&&this.h(t,i,s)},g);function g(t,i){this.i=t,this.h=i,this.t=f[0]}function m(t,i){return t?t===s[0]?f[0]:0===t.indexOf(s[1])||t===s[7]?f[1]:0===t.indexOf(s[4])?f[3]:t===s[6]?f[2]:f[1]:3===i.nodeType&&"A"===i.tagName?f[3]:f[1]}function y(u,t,a,i,s,n,c){void 0===c&&(c=null);s=function(t,i,s,n,h,e,r){void 0===c&&(r=null);var o=a.changedTouches[0],n=new Event(u,{bubbles:!0,cancelable:n});n.dataTransfer=e,n.relatedTarget=r,n.screenX=o.screenX,n.screenY=o.screenY,n.clientX=o.clientX,n.clientY=o.clientY,n.pageX=o.pageX,n.pageY=o.pageY;t=t.getBoundingClientRect();return n.offsetX=n.clientX-t.left,n.offsetY=n.clientY-t.top,n}(t,0,0,n=void 0===n||n,document.defaultView,s,c),s=!t.dispatchEvent(s);return i.mode=0,s}function b(t,i){if(!t||t===s[7])return i;if(i===f[1]){if(0===t.indexOf(f[1]))return f[1]}else if(i===f[3]){if(0===t.indexOf(f[3])||-1<t.indexOf("Link"))return f[3]}else if(i===f[2]&&(0===t.indexOf(f[2])||-1<t.indexOf("Move")))return f[2];return f[0]}var w=(x.prototype.o=function(){var n=this;this.u=1,this.l=f[0],this.v={data:{},effectAllowed:void 0,mode:3,types:[]},this.p={x:null,y:null},this.g={x:null,y:null};var h=this.m;if(this.I=new p(this.v,function(t,i,s){h=t,"number"!=typeof i&&"number"!=typeof s||(n.j={x:i||0,y:s||0})}),this.v.mode=2,this.I.dropEffect=f[0],y("dragstart",this.m,this.k,this.v,this.I))return this.u=3,this.C(),!1;a("page",this.k,this.g);var i,t,s=this.S.dragImageSetup(h);return this.A=(i=s,d.map(function(t){t=i.style[t+"transform"];return t&&"none"!==t?t.replace(/translate\(\D*\d+[^,]*,\D*\d+[^,]*\)\s*/g,""):""})),s.style.position="absolute",s.style.left="0px",s.style.top="0px",s.style.zIndex="999999",s.classList.add("dnd-poly-drag-image"),s.classList.add("dnd-poly-icon"),this.O=s,this.j||(this.S.dragImageOffset?this.j={x:this.S.dragImageOffset.x,y:this.S.dragImageOffset.y}:this.S.dragImageCenterOnTouch?(t=getComputedStyle(h),this.j={x:0-parseInt(t.marginLeft,10),y:0-parseInt(t.marginTop,10)}):(s=h.getBoundingClientRect(),t=getComputedStyle(h),this.j={x:s.left-this.M.clientX-parseInt(t.marginLeft,10)+s.width/2,y:s.top-this.M.clientY-parseInt(t.marginTop,10)+s.height/2})),v(this.O,this.g,this.A,this.j,this.S.dragImageCenterOnTouch),document.body.appendChild(this.O),this.D=window.setInterval(function(){n.F||(n.F=!0,n.N(),n.F=!1)},this.S.iterationInterval),!0},x.prototype.C=function(){this.D&&(clearInterval(this.D),this.D=null),n("touchmove",this.P),n("touchend",this.T),n("touchcancel",this.T),this.O&&(this.O.parentNode.removeChild(this.O),this.O=null),this.L(this.S,this.k,this.u)},x.prototype._=function(t){var s=this;if(!1!==r(t,this.M.identifier)){if(this.k=t,0===this.u){var i=void 0;if(this.S.dragStartConditionOverride)try{i=this.S.dragStartConditionOverride(t)}catch(t){i=!1}else i=1===t.touches.length;return i?void(!0===this.o()&&(this.H.preventDefault(),t.preventDefault())):void this.C()}if(t.preventDefault(),a("client",t,this.p),a("page",t,this.g),this.S.dragImageTranslateOverride)try{var n=!1;if(this.S.dragImageTranslateOverride(t,{x:this.p.x,y:this.p.y},this.V,function(t,i){s.O&&(n=!0,s.p.x+=t,s.p.y+=i,s.g.x+=t,s.g.y+=i,v(s.O,s.g,s.A,s.j,s.S.dragImageCenterOnTouch))}),n)return}catch(t){}v(this.O,this.g,this.A,this.j,this.S.dragImageCenterOnTouch)}},x.prototype.X=function(t){if(!1!==r(t,this.M.identifier)){if(this.S.dragImageTranslateOverride)try{this.S.dragImageTranslateOverride(void 0,void 0,void 0,function(){})}catch(t){}0!==this.u?(t.preventDefault(),this.u="touchcancel"===t.type?3:2):this.C()}},x.prototype.N=function(){var t=this,i=this.l;this.v.mode=3,this.I.dropEffect=f[0];var s,n,h,e,r,o=y("drag",this.m,this.k,this.v,this.I);if(o&&(this.l=f[0]),o||2===this.u||3===this.u)return this.Y(this.u)?(e=this.m,s=this.O,n=this.A,a=function(){t.q()},void("hidden"!==(r=getComputedStyle(e)).visibility&&"none"!==r.display?(s.classList.add("dnd-poly-snapback"),h=getComputedStyle(s),o=parseFloat(h.transitionDuration),isNaN(o)||0===o?a():((e={x:(u=e.getBoundingClientRect()).left,y:u.top}).x+=document.body.scrollLeft||document.documentElement.scrollLeft,e.y+=document.body.scrollTop||document.documentElement.scrollTop,e.x-=parseInt(r.marginLeft,10),e.y-=parseInt(r.marginTop,10),u=parseFloat(h.transitionDelay),u=Math.round(1e3*(o+u)),v(s,e,n,void 0,!1),setTimeout(a,u))):a())):void this.q();var u=this.S.elementFromPoint(this.p.x,this.p.y),a=this.B;u!==this.V&&u!==this.B&&(this.V=u,null!==this.B&&(this.v.mode=3,this.I.dropEffect=f[0],y("dragexit",this.B,this.k,this.v,this.I,!1)),null===this.V?this.B=this.V:(this.v.mode=3,this.I.dropEffect=m(this.v.effectAllowed,this.m),y("dragenter",this.V,this.k,this.v,this.I)?(this.B=this.V,this.l=b(this.I.effectAllowed,this.I.dropEffect)):this.V!==document.body&&(this.B=document.body))),a!==this.B&&l(a)&&(this.v.mode=3,this.I.dropEffect=f[0],y("dragleave",a,this.k,this.v,this.I,!1,this.B)),l(this.B)&&(this.v.mode=3,this.I.dropEffect=m(this.v.effectAllowed,this.m),!1===y("dragover",this.B,this.k,this.v,this.I)?this.l=f[0]:this.l=b(this.I.effectAllowed,this.I.dropEffect)),i!==this.l&&this.O.classList.remove(c+i);i=c+this.l;this.O.classList.add(i)},x.prototype.Y=function(t){t=this.l===f[0]||null===this.B||3===t;return t?l(this.B)&&(this.v.mode=3,this.I.dropEffect=f[0],y("dragleave",this.B,this.k,this.v,this.I,!1)):l(this.B)&&(this.v.mode=1,this.I.dropEffect=this.l,!0===y("drop",this.B,this.k,this.v,this.I)?this.l=this.I.dropEffect:this.l=f[0]),t},x.prototype.q=function(){this.v.mode=3,this.I.dropEffect=this.l,y("dragend",this.m,this.k,this.v,this.I,!1),this.u=2,this.C()},x);function x(t,i,s,n){this.H=t,this.S=i,this.m=s,this.L=n,this.u=0,this.V=null,this.B=null,this.k=t,this.M=t.changedTouches[0],this.P=this._.bind(this),this.T=this.X.bind(this),h("touchmove",this.P,!1),h("touchend",this.T,!1),h("touchcancel",this.T,!1)}var I,j={iterationInterval:150,tryFindDraggableTarget:function(t){for(var i=0,s=t.composedPath();i<s.length;i++){var n=s[i];do{if(!1!==n.draggable){if(!0===n.draggable)return n;if(n.getAttribute&&"true"===n.getAttribute("draggable"))return n}}while((n=n.parentNode)&&n!==document.body)}},dragImageSetup:function(t){var i=t.cloneNode(!0);return function t(i,s){if(1===i.nodeType){for(var n,h,e=getComputedStyle(i),r=0;r<e.length;r++){var o=e[r];s.style.setProperty(o,e.getPropertyValue(o),e.getPropertyPriority(o))}s.style.pointerEvents="none",s.removeAttribute("id"),s.removeAttribute("class"),s.removeAttribute("draggable"),"CANVAS"===s.nodeName&&(n=s,h=(h=i).getContext("2d").getImageData(0,0,h.width,h.height),n.getContext("2d").putImageData(h,0,0))}if(i.hasChildNodes())for(r=0;r<i.childNodes.length;r++)t(i.childNodes[r],s.childNodes[r]);!function t(i){if(i instanceof HTMLElement&&(i.style.pointerEvents="none"),i.children&&i.children.length)for(var s=0;s<i.children.length;s++)t(i.children[s]);if(i.shadowRoot&&i.shadowRoot.children.length)for(s=0;s<i.shadowRoot.children.length;s++)t(i.shadowRoot.children[s])}(s)}(t,i),i},elementFromPoint:function(t,i){var s=document.elementFromPoint(t,i);if(s){for(;s.shadowRoot;){var n=s.shadowRoot.elementFromPoint(t,i);if(null===n||n===s)break;s=n}return s}}};function k(i){if(!I){var t=j.tryFindDraggableTarget(i);if(t)try{I=new w(i,j,t,S)}catch(t){throw S(j,i,3),t}}}function C(t){function i(t){h.off(),e.off(),r.off(),o.off(),s&&s.dispatchEvent(new CustomEvent("dnd-poly-dragstart-cancel",{bubbles:!0,cancelable:!0})),clearTimeout(n)}var s=t.target;s&&s.dispatchEvent(new CustomEvent("dnd-poly-dragstart-pending",{bubbles:!0,cancelable:!0}));var n=window.setTimeout(function(){h.off(),e.off(),r.off(),o.off(),k(t)},j.holdToDrag),h=u(s,"touchend",i),e=u(s,"touchcancel",i),r=u(s,"touchmove",i),o=u(window,"scroll",i,!0)}function S(t,i,s){if(0===s&&t.defaultActionOverride)try{t.defaultActionOverride(i),i.defaultPrevented}catch(t){}I=null}t.polyfill=function(i){if(i&&Object.keys(i).forEach(function(t){j[t]=i[t]}),!j.forceApply){t=(t=!!window.chrome||/chrome/i.test(navigator.userAgent),{dragEvents:"ondragstart"in document.documentElement,draggable:"draggable"in document.documentElement,userAgentSupportingNativeDnD:!(/iPad|iPhone|iPod|Android/.test(navigator.userAgent)||t&&"ontouchstart"in document.documentElement)});if(t.userAgentSupportingNativeDnD&&t.draggable&&t.dragEvents)return!1}var t;return j.holdToDrag?h("touchstart",C,!1):h("touchstart",k,!1),!0},t.supportsPassiveEventListener=i,Object.defineProperty(t,"G",{value:!0})});
//# sourceMappingURL=index.min.js.map