123456 |
- /*
- * Copyright (c) 2016 Bernardo Castilho
- *
- * Licensed under the MIT License (MIT)
- */
- var DragDropTouch;!function(t){"use strict";var e=(Object.defineProperty(i.prototype,"dropEffect",{get:function(){return this._dropEffect},set:function(t){this._dropEffect=t},enumerable:!0,configurable:!0}),Object.defineProperty(i.prototype,"effectAllowed",{get:function(){return this._effectAllowed},set:function(t){this._effectAllowed=t},enumerable:!0,configurable:!0}),Object.defineProperty(i.prototype,"types",{get:function(){return Object.keys(this._data)},enumerable:!0,configurable:!0}),i.prototype.clearData=function(t){null!=t?delete this._data[t]:this._data=null},i.prototype.getData=function(t){return this._data[t]||""},i.prototype.setData=function(t,e){this._data[t]=e},i.prototype.setDragImage=function(t,e,i){var s=o._instance;s._imgCustom=t,s._imgOffset={x:e,y:i}},i);function i(){this._dropEffect="move",this._effectAllowed="all",this._data={}}t.DataTransfer=e;var o=(a.getInstance=function(){return a._instance},a.prototype._touchstart=function(t){var e=this;if(this._shouldHandle(t)){if(Date.now()-this._lastClick<a._DBLCLICK&&this._dispatchEvent(t,"dblclick",t.target))return t.preventDefault(),void this._reset();this._reset();var i=this._closestDraggable(t.target);i&&(this._dispatchEvent(t,"mousemove",t.target)||this._dispatchEvent(t,"mousedown",t.target)||(this._dragSource=i,this._ptDown=this._getPoint(t),(this._lastTouch=t).preventDefault(),setTimeout(function(){e._dragSource==i&&null==e._img&&e._dispatchEvent(t,"contextmenu",i)&&e._reset()},a._CTXMENU),a._ISPRESSHOLDMODE&&(this._pressHoldInterval=setTimeout(function(){e._isDragEnabled=!0,e._touchmove(t)},a._PRESSHOLDAWAIT))))}},a.prototype._touchmove=function(t){if(this._shouldCancelPressHoldMove(t))this._reset();else if(this._shouldHandleMove(t)||this._shouldHandlePressHoldMove(t)){var e=this._getTarget(t);if(this._dispatchEvent(t,"mousemove",e))return void(this._lastTouch=t).preventDefault();this._dragSource&&!this._img&&this._shouldStartDragging(t)&&(this._dispatchEvent(t,"dragstart",this._dragSource),this._createImage(t),this._dispatchEvent(t,"dragenter",e)),this._img&&((this._lastTouch=t).preventDefault(),e!=this._lastTarget&&(this._dispatchEvent(this._lastTouch,"dragleave",this._lastTarget),this._dispatchEvent(t,"dragenter",e),this._lastTarget=e),this._moveImage(t),this._isDropZone=this._dispatchEvent(t,"dragover",e))}},a.prototype._touchend=function(t){if(this._shouldHandle(t)){if(this._dispatchEvent(this._lastTouch,"mouseup",t.target))return void t.preventDefault();this._img||(this._dragSource=null,this._dispatchEvent(this._lastTouch,"click",t.target),this._lastClick=Date.now()),this._destroyImage(),this._dragSource&&(t.type.indexOf("cancel")<0&&this._isDropZone&&this._dispatchEvent(this._lastTouch,"drop",this._lastTarget),this._dispatchEvent(this._lastTouch,"dragend",this._dragSource),this._reset())}},a.prototype._shouldHandle=function(t){return t&&!t.defaultPrevented&&t.touches&&t.touches.length<2},a.prototype._shouldHandleMove=function(t){return!a._ISPRESSHOLDMODE&&this._shouldHandle(t)},a.prototype._shouldHandlePressHoldMove=function(t){return a._ISPRESSHOLDMODE&&this._isDragEnabled&&t&&t.touches&&t.touches.length},a.prototype._shouldCancelPressHoldMove=function(t){return a._ISPRESSHOLDMODE&&!this._isDragEnabled&&this._getDelta(t)>a._PRESSHOLDMARGIN},a.prototype._shouldStartDragging=function(t){var e=this._getDelta(t);return a._THRESHOLD<e||a._ISPRESSHOLDMODE&&a._PRESSHOLDTHRESHOLD<=e},a.prototype._reset=function(){this._destroyImage(),this._dragSource=null,this._lastTouch=null,this._lastTarget=null,this._ptDown=null,this._isDragEnabled=!1,this._isDropZone=!1,this._dataTransfer=new e,clearInterval(this._pressHoldInterval)},a.prototype._getPoint=function(t,e){return t&&t.touches&&(t=t.touches[0]),{x:e?t.pageX:t.clientX,y:e?t.pageY:t.clientY}},a.prototype._getDelta=function(t){if(a._ISPRESSHOLDMODE&&!this._ptDown)return 0;var e=this._getPoint(t);return Math.abs(e.x-this._ptDown.x)+Math.abs(e.y-this._ptDown.y)},a.prototype._getTarget=function(t){for(var e=this._getPoint(t),i=document.elementFromPoint(e.x,e.y);i&&"none"==getComputedStyle(i).pointerEvents;)i=i.parentElement;return i},a.prototype._createImage=function(t){this._img&&this._destroyImage();var e,i,s=this._imgCustom||this._dragSource;this._img=s.cloneNode(!0),this._copyStyle(s,this._img),this._img.style.top=this._img.style.left="-9999px",this._imgCustom||(e=s.getBoundingClientRect(),i=this._getPoint(t),this._imgOffset={x:i.x-e.left,y:i.y-e.top},this._img.style.opacity=a._OPACITY.toString()),this._moveImage(t),document.body.appendChild(this._img)},a.prototype._destroyImage=function(){this._img&&this._img.parentElement&&this._img.parentElement.removeChild(this._img),this._img=null,this._imgCustom=null},a.prototype._moveImage=function(i){var s=this;requestAnimationFrame(function(){var t,e;s._img&&(t=s._getPoint(i,!0),(e=s._img.style).position="absolute",e.pointerEvents="none",e.zIndex="999999",e.left=Math.round(t.x-s._imgOffset.x)+"px",e.top=Math.round(t.y-s._imgOffset.y)+"px")})},a.prototype._copyProps=function(t,e,i){for(var s=0;s<i.length;s++){var o=i[s];t[o]=e[o]}},a.prototype._copyStyle=function(t,e){var i,s;a._rmvAtts.forEach(function(t){e.removeAttribute(t)}),t instanceof HTMLCanvasElement&&(i=t,(s=e).width=i.width,s.height=i.height,s.getContext("2d").drawImage(i,0,0));for(var o=getComputedStyle(t),n=0;n<o.length;n++){var r=o[n];r.indexOf("transition")<0&&(e.style[r]=o[r])}for(e.style.pointerEvents="none",n=0;n<t.children.length;n++)this._copyStyle(t.children[n],e.children[n])},a.prototype._dispatchEvent=function(t,e,i){if(t&&i){var s=document.createEvent("Event"),o=t.touches?t.touches[0]:t;return s.initEvent(e,!0,!0),s.button=0,s.which=s.buttons=1,this._copyProps(s,t,a._kbdProps),this._copyProps(s,o,a._ptProps),s.dataTransfer=this._dataTransfer,i.dispatchEvent(s),s.defaultPrevented}return!1},a.prototype._closestDraggable=function(t){for(;t;t=t.parentElement)if(t.hasAttribute("draggable")&&t.draggable)return t;return null},a);function a(){if(this._lastClick=0,a._instance)throw"DragDropTouch instance already created.";var t,e,i,s,o,n=!1;document.addEventListener("test",function(){},{get passive(){return n=!0}}),"ontouchstart"in document&&(t=document,e=this._touchstart.bind(this),i=this._touchmove.bind(this),s=this._touchend.bind(this),o=!!n&&{passive:!1,capture:!1},t.addEventListener("touchstart",e,o),t.addEventListener("touchmove",i,o),t.addEventListener("touchend",s),t.addEventListener("touchcancel",s))}o._instance=new o,o._THRESHOLD=5,o._OPACITY=.5,o._DBLCLICK=500,o._CTXMENU=900,o._ISPRESSHOLDMODE=!1,o._PRESSHOLDAWAIT=400,o._PRESSHOLDMARGIN=25,o._PRESSHOLDTHRESHOLD=0,o._rmvAtts="id,class,style,draggable".split(","),o._kbdProps="altKey,ctrlKey,metaKey,shiftKey".split(","),o._ptProps="pageX,pageY,clientX,clientY,screenX,screenY,offsetX,offsetY".split(","),t.DragDropTouch=o}(DragDropTouch=DragDropTouch||{});
|