forked from sunpietro/dragster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dragster.min.js
13 lines (13 loc) · 7.57 KB
/
dragster.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
/*@preserve
* Dragster - drag'n'drop library v1.3.6
* https://github.com/sunpietro/dragster
*
* Copyright 2015-2016 Piotr Nalepa
* http://blog.piotrnalepa.pl
*
* Released under the MIT license
* https://github.com/sunpietro/dragster/blob/master/LICENSE
*
* Date: 2016-12-12T19:00Z
*/
!function(e,t){"use strict";e.Dragster=function(n){var r,o,a,s,l,i,d,c,g,u,m,p,f,h,v,E,L,y,C,w,b,D,B,T,H,N,A,S,M="dragster-",R="is-dragging",P="is-drag-over",O=M+"draggable",I=M+"drag-region",Y=M+"drop-placeholder",q=M+"temp",x=q+"-container",F=M+"is-hidden",X=M+"replacable",k=M+"region--drag-only",J="touchstart",W="touchmove",j="touchend",z="mousedown",G="mousemove",K="mouseup",Q="top",U="bottom",V="px",Z="div",$=!1,_=!0,ee=null,te=function(){},ne={elementSelector:".dragster-block",regionSelector:".dragster-region",dragHandleCssClass:$,dragOnlyRegionCssClass:k,replaceElements:$,updateRegionsHeight:_,minimumRegionHeight:60,onBeforeDragStart:te,onAfterDragStart:te,onBeforeDragMove:te,onAfterDragMove:te,onBeforeDragEnd:te,onAfterDragEnd:te,onAfterDragDrop:te,scrollWindowOnDrag:$,dragOnlyRegionsEnabled:$,cloneElements:$,wrapDraggableElements:_},re="data-placeholder-position",oe={top:$,bottom:$},ae={drag:{node:ee},drop:{node:ee},shadow:{node:ee,top:0,left:0},placeholder:{node:ee,position:ee},dropped:ee,clonedFrom:ee,clonedTo:ee},se={},le=e.innerHeight;for(r in n)n.hasOwnProperty(r)&&(ne[r]=n[r]);return D=function(){return[].slice.call(t.querySelectorAll(ne.elementSelector))},B=function(e){return ne.wrapDraggableElements===$?(console.warn("You have disabled the default behavior of wrapping the draggable elements. If you want Dragster.js to work properly you still will have to do this manually.\n\nMore info: https://github.com/sunpietro/dragster/blob/master/README.md#user-content-wrapdraggableelements---boolean"),$):void e.forEach(function(e){var t=v(),n=e.parentNode;return n.classList.contains(O)?$:(n.insertBefore(t,e),n.removeChild(e),void t.appendChild(e))})},c=D(),o=[].slice.call(t.querySelectorAll(ne.regionSelector)),ne.replaceElements&&(i=t.createElement(Z),i.classList.add(F),i.classList.add(x),t.body.appendChild(i)),a=function(e,t){var n=e.parentNode;if(n&&(!e.classList||!e.classList.contains(I)||e.classList.contains(k)))return t(e)?e:t(n)?n:a(n,t)},C=function(e){var n=[].slice.call(t.getElementsByClassName(e));n.forEach(function(e){e.parentNode.removeChild(e)})},w=function(e,n){n&&(o.forEach(function(e){e.removeEventListener(n,g.mousemove)}),t.body.removeEventListener(n,g.mousemove)),e&&e.classList.remove(R),[].slice.call(t.getElementsByClassName(O)).forEach(function(e){e.firstChild||e.parentNode.removeChild(e)}),C(Y),C(q),T()},b=function(){[].slice.call(t.getElementsByClassName(X)).forEach(function(e){e.classList.remove(X)})},v=function(){var e=t.createElement(Z);return e.classList.add(O),e},L=function(){var e=t.createElement(Z);return e.classList.add(Y),e},E=function(){var e=t.createElement(Z);return e.classList.add(q),e.classList.add(F),e.style.position="fixed",t.body.appendChild(e),e},f=function(e,t){if(e&&e.parentNode){var n=ne.wrapDraggableElements===$?e:e.nextSibling;e.parentNode.insertBefore(t,n)}},h=function(e,t){e&&e.parentNode&&e.parentNode.insertBefore(t,e)},m=function(e){return e.classList&&e.classList.contains(O)},u=function(e){return e.classList&&e.classList.contains(Y)},p=function(e){return e.classList&&e.classList.contains(ne.dragOnlyRegionCssClass)},T=function(){if(ne.updateRegionsHeight){var n=[].slice.call(t.getElementsByClassName(I));n.forEach(function(t){var n=[].slice.call(t.querySelectorAll(ne.elementSelector)),r=ne.minimumRegionHeight;n.length&&(n.forEach(function(t){var n=e.getComputedStyle(t);r+=t.offsetHeight+parseInt(n.marginTop,10)+parseInt(n.marginBottom,10)}),t.style.height=r+V)})}},g={mousedown:function(e){if(ne.dragHandleCssClass&&("string"!=typeof ne.dragHandleCssClass||!e.target.classList.contains(ne.dragHandleCssClass)))return!1;var n,r;return e.dragster=se,ne.onBeforeDragStart(e)===$||3===e.which?$:(e.preventDefault(),r=e.type===J?W:G,o.forEach(function(e){e.addEventListener(r,g.mousemove)}),t.body.addEventListener(r,g.mousemove),(d=a(e.target,m))?(n=d.getBoundingClientRect(),s=E(),s.innerHTML=d.innerHTML,s.style.width=n.width+V,s.style.height=n.height+V,l=s.getBoundingClientRect(),d.classList.add(R),se=JSON.parse(JSON.stringify(ae)),se.drag.node=d,se.shadow.node=s,e.dragster=se,void ne.onAfterDragStart(e)):$)},mousemove:function(e){if(e.dragster=se,ne.onBeforeDragMove(e)===$)return $;e.preventDefault();var n=e.changedTouches?e.changedTouches[0]:e,r=n.view?n.view.pageXOffset:0,o=n.view?n.view.pageYOffset:0,i=n.clientY+o,c=n.clientX+r,g=t.elementFromPoint(n.clientX,n.clientY),u=a(g,m),f=n.clientY,h=c-l.width/2,v=!(!u||!a(u,p)),E=g.classList.contains(I),L=g.classList.contains(k),C=g.classList.contains(Y),w=g.getElementsByClassName(O).length>0,b=g.getElementsByClassName(Y).length>0;clearTimeout(y),s.style.top=f+V,s.style.left=h+V,s.classList.remove(F),se.shadow.top=f,se.shadow.left=h,u&&u!==d&&!v?(S.removePlaceholders(),S.addPlaceholderOnTarget(u,i,o)):!E||L||w||b?E&&!L&&w&&!b?(S.removePlaceholders(),S.addPlaceholderInRegionBelowTargets(g)):E||C||S.removePlaceholders():(S.removePlaceholders(),S.addPlaceholderInRegion(g)),ne.scrollWindowOnDrag&&H(e),T(),ne.onAfterDragMove(e)},mouseup:function(e){if(e.dragster=se,ne.onBeforeDragEnd(e)===$)return $;var n,r=ne.replaceElements?X:Y,o=t.getElementsByClassName(r)[0],s=!(!d||!a(d,p)),l=e.type===J?W:G,i=ne.cloneElements&&s;return y=setTimeout(function(){w(d,l)},200),b(),d&&o?(n=a(o,m),n=n||o,d!==n&&(ne.replaceElements||i?ne.replaceElements&&!i?(e.dragster=A.replaceElements(e.dragster,n),ne.onAfterDragDrop(e)):!ne.replaceElements&&i&&(e.dragster=A.cloneElements(e.dragster,o,n),ne.onAfterDragDrop(e)):(e.dragster=A.moveElement(e.dragster,o,n),ne.onAfterDragDrop(e)),n.classList.remove(P)),w(d,l),void ne.onAfterDragEnd(e)):(w(d,l),$)}},S={addPlaceholderOnTarget:function(e,t,n){var r=e.getBoundingClientRect(),o=L(),a=r.height/2;b(),ne.replaceElements?e.classList.add(X):t-n-r.top<a&&!oe.top?(C(Y),o.setAttribute(re,Q),h(e.firstChild,o),se.placeholder.position=Q):r.bottom-(t-n)<a&&!oe.bottom&&(C(Y),o.setAttribute(re,U),e.appendChild(o),se.placeholder.position=U),se.placeholder.node=o,se.drop.node=e},addPlaceholderInRegion:function(e){var t=L();e.appendChild(t),se.placeholder.position=U,se.placeholder.node=t,se.drop.node=e},addPlaceholderInRegionBelowTargets:function(e){var t=e.getElementsByClassName(O),n=t[t.length-1],r=L();r.setAttribute(re,U),C(Y),n.appendChild(r),se.placeholder.position=U,se.placeholder.node=r,se.drop.node=n},removePlaceholders:function(){ne.replaceElements?b():C(Y)}},A={moveElement:function(e,t,n){var r=ne.wrapDraggableElements===$?d:v(),o=t.getAttribute(re);return o===Q?h(n,r):ne.wrapDraggableElements===$?f(r,n):f(n,r),d.firstChild&&ne.wrapDraggableElements===_&&r.appendChild(d.firstChild),e.dropped=r,e},replaceElements:function(e,n){var r=t.getElementsByClassName(x)[0];return r.innerHTML=d.innerHTML,d.innerHTML=n.innerHTML,n.innerHTML=r.innerHTML,r.innerHTML="",e.dropped=r,e},cloneElements:function(e,t,n){var r=d.cloneNode(!0),o=t.getAttribute(re);return o===Q?h(n,r):f(n,r),w(r),e.clonedFrom=d,e.clonedTo=r,e}},H=function(t){var n=t.changedTouches?t.changedTouches[0]:t,r=60;le-n.clientY<r?e.scrollBy(0,10):n.clientY<r&&e.scrollBy(0,-10)},N=function(){le=e.innerHeight},B(c),t.body.addEventListener(K,g.mouseup,$),t.body.addEventListener(j,g.mouseup,$),o.forEach(function(e){e.classList.add(I),e.addEventListener(z,g.mousedown,$),e.addEventListener(K,g.mouseup,$),e.addEventListener(J,g.mousedown,$),e.addEventListener(j,g.mouseup,$)}),e.addEventListener("resize",N,!1),{update:function(){B(D()),T(),N()}}}}(window,window.document);