This repository has been archived by the owner on Sep 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
drag.min.js
6 lines (6 loc) · 3.67 KB
/
drag.min.js
1
2
3
4
5
6
/*!
* drag.js - copyright Jake Luer 2011
* https://github.com/jakeluer/drag.js
* MIT License
*/
!function(a,b){"undefined"==typeof bean&&(bean=require("bean"));var c=bean,d=getComputedStyle||currentStyle,e="ontouchstart"in b.documentElement?!0:!1;drag=function(a){return new Drag(drag.select(a))},drag.select=function(a){return"string"==typeof a?document.getElementById(a)||document.querySelectorAll(a)[0]:a},drag.value=function(a,b,c){if(!c)return parseFloat(d(a).getPropertyValue(b));a.style[b]=c},drag.evs=function(){return e?{start:"touchstart",move:"touchmove",end:"touchend"}:{start:"mousedown",move:"mousemove",end:"mouseup"}}(),Drag=function g(a){return this instanceof g?(this.el=a,this._axis="both",this._start=[],this._dragging=[],this._end=[],this.pos={},this):new g(a)},Drag.prototype.current=function(a){return drag.value(this.el,a)},Drag.prototype.axis=function(a){if(a=="x"||a=="y"||a=="both")this._axis=a;return this},Drag.prototype.container=function(a){return this._container=drag.select(a),this},Drag.prototype.handle=function(a){return this._handle=drag.select(a),this},Drag.prototype.start=function(a){return a&&"function"==typeof a&&this._start.push(a),this},Drag.prototype.dragging=function(a){return a&&"function"==typeof a&&this._dragging.push(a),this},Drag.prototype.end=function(a){return a&&"function"==typeof a&&this._end.push(a),this},Drag.prototype.getPos=function(a){return this.pos.x=this.current("left"),this.pos.y=this.current("top"),a&&"function"==typeof a&&a.apply(this),this},Drag.prototype.bind=function(){var a=this;return this.unbind(),this._eventHandler=function(d){var f=a.current("left"),g=a.current("top"),h=function(b){var c,h,i,j;e?d.touches.length==1&&(c=b.touches[0].clientX-d.touches[0].clientX,h=b.touches[0].clientY-d.touches[0].clientY):(c=b.clientX-d.clientX,h=b.clientY-d.clientY),i=f+c,j=g+h,a.pos.dX=i-a.current("left"),a.pos.dY=j-a.current("top");if(a._container){var k=a._container;i<0?i=0:i>=0&&(maxX=drag.value(k,"padding-left")+drag.value(k,"width")+drag.value(k,"padding-right")-(drag.value(a.el,"margin-left")+drag.value(a.el,"border-left-width")+drag.value(a.el,"padding-left")+drag.value(a.el,"width")+drag.value(a.el,"padding-right")+drag.value(a.el,"border-right-width")+drag.value(a.el,"margin-right")),i>maxX&&(i=maxX)),j<0?j=0:j>=0&&(maxY=drag.value(k,"padding-top")+drag.value(k,"height")+drag.value(k,"padding-bottom")-(drag.value(a.el,"margin-top")+drag.value(a.el,"border-top-width")+drag.value(a.el,"padding-top")+drag.value(a.el,"height")+drag.value(a.el,"padding-bottom")+drag.value(a.el,"border-bottom-width")+drag.value(a.el,"margin-bottom")),j>maxY&&(j=maxY))}a._axis=="x"?drag.value(a.el,"left",i+"px"):a._axis=="y"?drag.value(a.el,"top",j+"px"):(drag.value(a.el,"left",i+"px"),drag.value(a.el,"top",j+"px")),a.getPos();for(var l in a._dragging)a._dragging[l].apply(a);b.preventDefault(),b.stopPropagation()},i=function(){c.remove(b,drag.evs.move,h),c.remove(b,drag.evs.end,j),c.remove(b,"selectstart",k),c.remove(a._handle,"dragstart",k)},j=function(b){for(var c in a._end)a._end[c].apply(a);i()},k=function(a){a.preventDefault(),a.stopPropagation()};for(var l in a._start)a._start[l].apply(a);b.body.focus(),c.add(b,"selectstart",k),c.add(a._handle,"dragstart",k),c.add(b,drag.evs.move,h),c.add(b,drag.evs.end,j)},this.getPos(),this._handle||(this._handle=this.el),c.add(this._handle,drag.evs.start,this._eventHandler),this},Drag.prototype.unbind=function(){return this._eventHandler?(c.remove(this.el,drag.evs.start,this._eventHandler),c.remove(this._handle,drag.evs.start,this._eventHandler),this):this};var f=a.drag;drag.noConflict=function(){return a.drag=f,this},typeof module!="undefined"&&module.exports&&(module.exports=drag),a.drag=drag}(this,document)