forked from think2011/collision-checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDragify.min.js
1 lines (1 loc) · 2.21 KB
/
Dragify.min.js
1
(function(e,t){if(typeof define==="function"&&define.amd){define([],t)}else if(typeof exports==="object"){module.exports=t()}else{e.Dragify=t()}})(this,function(){var e=function(){this.events={}};e.prototype={construct:e,on:function(e,t){this._getEventInfo(e).push(t);return this},trigger:function(e){var t=this._getEventInfo(e);var n=Array.prototype.slice.call(arguments,1);t.forEach(function(e){e.apply(e,n)});return this},_getEventInfo:function(e){if(!this.events[e])this.events[e]=[];return this.events[e]},remove:function(e,t){var n=this._getEventInfo(e);if(!t){this.events[e]=[]}else{n.splice(n.indexOf(t),1)}return this}};function t(t){this.$elem=t;this.watcher=new e;this.init()}t.prototype={constructor:t,init:function(){var e=this;e.$elem.addEventListener(n[0],function(t){var o=e._getEventInfo(t);var i=e.$elem.offsetParent;var r=o.clientX-e.$elem.offsetLeft;var s=o.clientY-e.$elem.offsetTop;var c=i.offsetLeft||0;var a=i.offsetTop||0;var f=document.documentElement.clientWidth;var u=document.documentElement.clientHeight;var m=e.$elem.offsetWidth;var l=e.$elem.offsetHeight;var v=getComputedStyle(e.$elem).zIndex;e.$elem.classList.add("drag-start");e.watcher.trigger("start",e.$elem);document.addEventListener(n[1],h);function h(t){var n=e._getEventInfo(t);var o=n.clientX-r;var i=n.clientY-s;if(o+c<0)o=o-(o+c);if(i+a<0)i=i-(i+a);if(o+c+m>f)o=f-(c+m);if(i+a+l>u)i=u-(a+l);e.$elem.style.position="absolute";e.$elem.style.left=o+"px";e.$elem.style.top=i+"px";e.$elem.style.zIndex=19911125;e.$elem.classList.add("drag-move");e.watcher.trigger("move",e.$elem)}document.addEventListener(n[2],d);function d(t){document.removeEventListener(n[1],h);document.removeEventListener(n[2],d);e.$elem.style.zIndex=v;e.$elem.classList.remove("drag-start");e.$elem.classList.remove("drag-move");e.watcher.trigger("end",e.$elem)}})},on:function(){this.watcher.on.apply(this.watcher,arguments);return this.watcher},_getEventInfo:function(e){return t.isTouch()?e.targetTouches[0]:e}};t.isTouch=function(e){return"ontouchstart"in window||window.DocumentTouch&&document instanceof window.DocumentTouch||navigator.maxTouchPoints>0||window.navigator.msMaxTouchPoints>0};var n=t.isTouch()?["touchstart","touchmove","touchend"]:["mousedown","mousemove","mouseup"];return t});