forked from kbjr/DragDrop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drag-drop.min.js
12 lines (12 loc) · 6.31 KB
/
drag-drop.min.js
1
2
3
4
5
6
7
8
9
10
11
12
/**
* DragDrop.js
*
* A JavaScript micro-framework for adding drag-and-drop functionality
* to elements for advanced UI development.
*
* @author James Brumond
* @version 0.3.0
* @copyright Copyright 2011 James Brumond
* @license Dual licensed under MIT and GPL
*//*jshint browser: true, bitwise: false, camelcase: false, eqnull: true, latedef: false,
plusplus: false, jquery: true, shadow: true, smarttabs: true, loopfunc: true */(function(){function s(a){var b=0,c=0;if(a.targetTouches)b=a.targetTouches[0].pageX,c=a.targetTouches[0].pageY;else if(a.pageX||a.pageY)b=a.pageX,c=a.pageY;else if(a.clientX||a.clientY)b=a.clientX+document.body.scrollLeft+document.documentElement.scrollLeft,c=a.clientY+document.body.scrollTop+document.documentElement.scrollTop;return{x:b,y:c}}var a="ontouchstart"in window,b="drag",c=function(){var d={},k=a?{start:"touchstart",move:"touchmove",end:"touchend"}:{start:"mousedown",move:"mousemove",end:"mouseup"},o=[],s=function(a,b){for(var c=0,d=o.length;c<d;c++)if(o[c]&&o[c].element===a&&o[c].anchor===b)return!0;return!1},t=function(a,b,c){o[a._id]&&o[a._id].events[b]&&c(o[a._id].events[b])},u=function(a,b){b=b||{},b.element=a,b.anchor=b.anchor||a,b.boundingBox=b.boundingBox||null,b.releaseAnchors=b.releaseAnchors||[],b.releaseAnchors.unshift(document);return b},v=1,w=function(){this._id=v++};w.prototype.unbind=function(){return c.unbind(this)},w.prototype.bindEvent=function(a,b){return c.bindEvent(this,a,b)},w.prototype.unbindEvent=function(a,b){return c.unbindEvent(this,a,b)},w.prototype.invokeEvent=function(a,b){return c.invokeEvent(this,a,b)},w.prototype.setBoundingBox=function(a){o[this._id].boundingBox=a},d.bind=function(d,e){e=u(d,e);if(!n(e.element))throw new Error("Must give an element to drag");if(g(e.element,"position")==="static")throw new Error("Cannot drag-drop an element with position:static");if(!s(e.element,e.anchor)){var t=new w,v={element:e.element,anchor:e.anchor,releaseAnchors:e.releaseAnchors,dragging:!1,event:null,shouldUnbind:!1,boundingBox:e.boundingBox,events:{beforedrag:p(e.beforedrag),dragstart:p(e.dragstart),dragend:p(e.dragend),drag:p(e.drag),unbind:p(e.unbind)}};v.event=r.bind(v.anchor,k.start,function(d){function z(a){return function(d){for(var e=0,f=t.length;e<f;e++)r.unbind(t[e]);v.dragging=!1,l(v.element,b),v.shouldUnbind&&c.unbind(v.element,v.anchor),v.events.dragend.call(v.element,new q("dragend",d,v,{releaseAnchor:a}));return j(d)}}if(window.event&&d.button===1||d.button===0||a){j(d),v.events.beforedrag.call(v.element,new q("beforedrag",d,v)),v.dragging=!0,m(v.element,b);var e=f(v.element,"left"),n=f(v.element,"top"),o=v.element.offsetLeft,p=v.element.offsetTop,s=i(v.element),t=[],u=a?v.anchor:document;t.push(r.bind(u,k.move,function(a){var b=a.clientX-d.clientX,c=a.clientY-d.clientY,f=v.element.offsetWidth,i=v.element.offsetHeight,k=e+b,l=n+c;if(v.boundingBox){var m=v.boundingBox,r,t,u,w;if(m==="offsetParent"){var x=v.element.offsetParent;g(v.element,"position")==="relative"?(r=-o,u=-p):r=u=0,t=x.clientWidth+r,w=x.clientHeight+u}else if(m==="windowSize"){var y=h();g(v.element,"position")==="relative"?(r=-s.x,u=-s.y):r=u=0,t=y.x+r,w=y.y+u}else r=m.x.min,t=m.x.max,u=m.y.min,w=m.y.max;k=Math.max(r,Math.min(t-f,k)),l=Math.max(u,Math.min(w-i,l))}v.element.style.left=k+"px",v.element.style.top=l+"px",v.events.drag.call(v.element,new q("drag",a,v));return j(a)}));for(var w=0,x=v.releaseAnchors.length;w<x;w++){var y=v.releaseAnchors[w];t.push(r.bind(y,k.end,z(y)))}document.body.focus(),t.push(r.bind(document,"selectstart",!1)),t.push(r.bind(v.anchor,"dragstart",!1)),v.events.dragstart.call(v.element,new q("dragstart",d,v));return!1}}),o[t._id]=v;return t}},d.unbind=function(a){if(a instanceof w){var b=a._id;o[b]&&(o[b].dragging?o[b].shouldUnbind=!0:(r.unbind(o[b].event),o[b]=null),binding.events.unbind.call(binding.element,new q("unbind",e,binding)))}},d.bindEvent=function(a,b,c){t(a,b,function(a){a.push(c)})},d.unbindEvent=function(a,b,c){t(a,b,function(a){a.remove(c)})},d.invokeEvent=function(a,b,c){t(a,b,function(d){d.call(o[a._id].element,new q(b,c,a))})};return d}(),d=function(a,b,c){var d=a.slice((c||b)+1||a.length);a.length=b<0?a.length+b:b;return a.push.apply(a,d)},f=function(a,b){var c=parseFloat(g(a,b));return isNaN(c)?0:c},g=function(a,b){if(a.currentStyle)return a.currentStyle[b];if(window.getComputedStyle)return document.defaultView.getComputedStyle(a,null).getPropertyValue(b);if(a.style)return a.style[b]},h=function(){return{x:window.innerWidth||document.documentElement.clientWidth||body().clientWidth,y:window.innerHeight||document.documentElement.clientHeight||body().clientHeight}},i=function(a){var b=0,c=0;if(a.offsetParent)do b+=a.offsetLeft,c+=a.offsetTop;while(a=a.offsetParent);return{x:b,y:c}},j=function(a){a.preventDefault&&a.preventDefault(),a.stopPropagation&&a.stopPropagation(),a.returnValue=!1;return!1},k={},l=function(a,b){k[b]||(k[b]=new RegExp("(^|\\s)+"+b+"(\\s|$)+")),a.className=a.className.replace(k[b]," ")},m=function(a,b){l(a,b),a.className+=" "+b},n=function(a){return!!a&&typeof a=="object"},o=function(a){var b;a.target?b=a.target:a.srcElement&&(b=a.srcElement),b.nodeType===3&&(b=b.parentNode);return b},p=function(a){var b=[],c=function(){var a;for(var c=0,d=b.length;c<d;c++)a=b[c].apply(this,arguments);return a};c.push=function(){b.push.apply(b,arguments)},c.remove=function(){var a=Array.prototype.slice.call(arguments),c=[];OUTER:for(var d=0,e=b.length;d<e;d++){for(var f=0,g=a.length;f<g;f++)if(b[d]===a[f])continue OUTER;c.push(b[d])}b=c},typeof a=="function"&&b.push(a);return c},q=function(b,c,d,e){this.type=b,this.originalEvent=c,this.altKey=c.altKey||!1,this.ctrlKey=c.ctrlKey||!1,this.shiftKey=c.shiftKey||!1,this.timestamp=c.timestamp||+(new Date),this.pos=s(c),this.binding=d,this.target=o(c);if(e)for(var f in e)e.hasOwnProperty(f)&&(this[f]=e[f])},r=function(){var a=function(){return document.addEventListener?function(a,b,c){a.addEventListener(b,c,!1)}:document.attachEvent?function(a,b,c){a.attachEvent("on"+b,c)}:function(){}}(),b=function(){return document.removeEventListener?function(a,b,c){a.removeEventListener(b,c,!1)}:document.detachEvent?function(a,b,c){a.detachEvent("on"+b,c)}:function(){}}();return{bind:function(c,d,e){var f=e===!1?function(a){return j(a)}:e;e=function(a){return f.call(c,a||window.event)},a(c,d,e);var g=function(){b(c,d,e)};g.unbind=function(){g()};return g},unbind:function(a){a()}}}();window.DragDrop=c})()