skylark-utils
Version:
An Elegant HTML5 JavaScript Library.
10 lines (9 loc) • 6.04 kB
JavaScript
/**
* skylark-utils - An Elegant HTML5 JavaScript Library.
* @author Hudaokeji Co.,Ltd
* @version v0.9.3
* @link www.skylarkjs.org
* @license MIT
*/
define([],function(){var t=function(){function t(){this._dropEffect="move",this._effectAllowed="all",this._data={}}return Object.defineProperty(t.prototype,"dropEffect",{get:function(){return this._dropEffect},set:function(t){this._dropEffect=t},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"effectAllowed",{get:function(){return this._effectAllowed},set:function(t){this._effectAllowed=t},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"types",{get:function(){return Object.keys(this._data)},enumerable:!0,configurable:!0}),t.prototype.clearData=function(t){null!=t?delete this._data[t]:this._data=null},t.prototype.getData=function(t){return this._data[t]||""},t.prototype.setData=function(t,e){this._data[t]=e},t.prototype.setDragImage=function(t,i,n){var s=e._instance;s._imgCustom=t,s._imgOffset={x:i,y:n}},t}(),e=function(){function e(){if(this._lastClick=0,e._instance)throw"DragDropTouch instance already created.";var t=!1;if(document.addEventListener("test",null,{get passive(){return t=!0,!0}}),"ontouchstart"in document){var i=document,n=this._touchstart.bind(this),s=this._touchmove.bind(this),o=this._touchend.bind(this),r=!!t&&{passive:!1,capture:!1};i.addEventListener("touchstart",n,r),i.addEventListener("touchmove",s,r),i.addEventListener("touchend",o),i.addEventListener("touchcancel",o)}}return e.getInstance=function(){return e._instance},e.prototype._touchstart=function(t){var i=this;if(this._shouldHandle(t)){if(Date.now()-this._lastClick<e._DBLCLICK&&this._dispatchEvent(t,"dblclick",t.target))return t.preventDefault(),void this._reset();this._reset();var n=this._closestDraggable(t.target);n&&(this._dispatchEvent(t,"mousemove",t.target)||this._dispatchEvent(t,"mousedown",t.target)||(this._dragSource=n,this._ptDown=this._getPoint(t),this._lastTouch=t,t.preventDefault(),setTimeout(function(){i._dragSource==n&&null==i._img&&i._dispatchEvent(t,"contextmenu",n)&&i._reset()},e._CTXMENU)))}},e.prototype._touchmove=function(t){if(this._shouldHandle(t)){var i=this._getTarget(t);if(this._dispatchEvent(t,"mousemove",i))return this._lastTouch=t,void t.preventDefault();if(this._dragSource&&!this._img){var n=this._getDelta(t);n>e._THRESHOLD&&(this._dispatchEvent(t,"dragstart",this._dragSource),this._createImage(t),this._dispatchEvent(t,"dragenter",i))}this._img&&(this._lastTouch=t,t.preventDefault(),i!=this._lastTarget&&(this._dispatchEvent(this._lastTouch,"dragleave",this._lastTarget),this._dispatchEvent(t,"dragenter",i),this._lastTarget=i),this._moveImage(t),this._dispatchEvent(t,"dragover",i))}},e.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._dispatchEvent(this._lastTouch,"drop",this._lastTarget),this._dispatchEvent(this._lastTouch,"dragend",this._dragSource),this._reset())}},e.prototype._shouldHandle=function(t){return t&&!t.defaultPrevented&&t.touches&&t.touches.length<2},e.prototype._reset=function(){this._destroyImage(),this._dragSource=null,this._lastTouch=null,this._lastTarget=null,this._ptDown=null,this._dataTransfer=new t},e.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}},e.prototype._getDelta=function(t){var e=this._getPoint(t);return Math.abs(e.x-this._ptDown.x)+Math.abs(e.y-this._ptDown.y)},e.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},e.prototype._createImage=function(t){this._img&&this._destroyImage();var i=this._imgCustom||this._dragSource;if(this._img=i.cloneNode(!0),this._copyStyle(i,this._img),this._img.style.top=this._img.style.left="-9999px",!this._imgCustom){var n=i.getBoundingClientRect(),s=this._getPoint(t);this._imgOffset={x:s.x-n.left,y:s.y-n.top},this._img.style.opacity=e._OPACITY.toString()}this._moveImage(t),document.body.appendChild(this._img)},e.prototype._destroyImage=function(){this._img&&this._img.parentElement&&this._img.parentElement.removeChild(this._img),this._img=null,this._imgCustom=null},e.prototype._moveImage=function(t){var e=this;this._img&&requestAnimationFrame(function(){var i=e._getPoint(t,!0),n=e._img.style;n.position="absolute",n.pointerEvents="none",n.zIndex="999999",n.left=Math.round(i.x-e._imgOffset.x)+"px",n.top=Math.round(i.y-e._imgOffset.y)+"px"})},e.prototype._copyProps=function(t,e,i){for(var n=0;n<i.length;n++){var s=i[n];t[s]=e[s]}},e.prototype._copyStyle=function(t,i){if(e._rmvAtts.forEach(function(t){i.removeAttribute(t)}),t instanceof HTMLCanvasElement){var n=t,s=i;s.width=n.width,s.height=n.height,s.getContext("2d").drawImage(n,0,0)}for(var o=getComputedStyle(t),r=0;r<o.length;r++){var a=o[r];a.indexOf("transition")<0&&(i.style[a]=o[a])}i.style.pointerEvents="none";for(var r=0;r<t.children.length;r++)this._copyStyle(t.children[r],i.children[r])},e.prototype._dispatchEvent=function(t,i,n){if(t&&n){var s=document.createEvent("Event"),o=t.touches?t.touches[0]:t;return s.initEvent(i,!0,!0),s.button=0,s.which=s.buttons=1,this._copyProps(s,t,e._kbdProps),this._copyProps(s,o,e._ptProps),s.dataTransfer=this._dataTransfer,n.dispatchEvent(s),s.defaultPrevented}return!1},e.prototype._closestDraggable=function(t){for(;t;t=t.parentElement)if(t.hasAttribute("draggable")&&t.draggable)return t;return null},e}();return e._instance=new e,e._THRESHOLD=5,e._OPACITY=.5,e._DBLCLICK=500,e._CTXMENU=900,e._rmvAtts="id,class,style,draggable".split(","),e._kbdProps="altKey,ctrlKey,metaKey,shiftKey".split(","),e._ptProps="pageX,pageY,clientX,clientY,screenX,screenY".split(","),e});
//# sourceMappingURL=sourcemaps/touchx.js.map