-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
dragster.min.js
13 lines (13 loc) · 8.92 KB
/
dragster.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
/*@preserve
* Dragster - drag'n'drop library v1.6.2
* https://github.com/sunpietro/dragster
*
* Copyright 2015-2017 Piotr Nalepa
* http://blog.piotrnalepa.pl
*
* Released under the MIT license
* https://github.com/sunpietro/dragster/blob/master/LICENSE
*
* Date: 2017-06-28T22:30Z
*/
!function(e,t,n){"use strict";"function"==typeof define&&define.amd?define(t,n):"object"==typeof exports?exports=module.exports=n():e[t]=n()}(this,"Dragster",function(){"use strict";return function(e={}){function t(){}var n,a,p,f,h,v,o,s,E,L,r,d,l,i,c,w,m,u,g,y,C,D,b,B,T,H,M,N,P,R,S,I="dragster-",O="is-dragging",A=I+"draggable",Y=I+"drag-region",x=I+"drop-placeholder",X=I+"temp",j=X+"-container",k=I+"is-hidden",q=I+"replacable",F="touchstart",U="touchmove",z="touchend",J="mousedown",W="mousemove",G="mouseup",K="top",Q="bottom",V="px",Z="div",$=!1,_=null,ee={elementSelector:".dragster-block",regionSelector:".dragster-region",dragHandleCssClass:$,dragOnlyRegionCssClass:I+"region--drag-only",replaceElements:$,updateRegionsHeight:!0,minimumRegionHeight:60,onBeforeDragStart:t,onAfterDragStart:t,onBeforeDragMove:t,onAfterDragMove:t,onBeforeDragEnd:t,onAfterDragEnd:t,onAfterDragDrop:t,scrollWindowOnDrag:$,dragOnlyRegionsEnabled:$,cloneElements:$,wrapDraggableElements:!0,shadowElementUnderMouse:$},te=$,ne=$,oe={drag:{node:_},drop:{node:_},shadow:{node:_,top:0,left:0},placeholder:{node:_,position:_},dropped:_,clonedFrom:_,clonedTo:_},re={},ae=window.innerHeight,se=Math.floor(65536*(1+Math.random())).toString(16);for(n in e)Object.prototype.hasOwnProperty.call(e,"key")&&(ee[n]=e[n]);return ee={...ee,...e},C=function(){return[].slice.call(document.querySelectorAll(ee.regionSelector))},D=function(e){if(ee.wrapDraggableElements===$)return 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"),$;e.forEach(function(e){var t=l(),n=e.parentNode;if(n.classList.contains(A))return $;n.insertBefore(t,e),n.removeChild(e),t.appendChild(e)})},o=(y=function(){return[].slice.call(document.querySelectorAll(ee.elementSelector))})(),a=C(),ee.replaceElements&&((_=document.createElement(Z)).classList.add(k),_.classList.add(j),document.body.appendChild(_)),p=function(e,t){var n=e.parentNode;if(n&&(!e.classList||!e.classList.contains(Y)||e.classList.contains(ee.dragOnlyRegionCssClass)))return t(e)?e:t(n)?n:p(n,t)},m=function(e){[].slice.call(document.getElementsByClassName(e)).forEach(function(e){e.dataset.dragsterId===se&&e.parentNode.removeChild(e)})},u=function(e,t){t&&(a.forEach(function(e){e.removeEventListener(t,s.mousemove)}),document.body.removeEventListener(t,s.mousemove)),e&&e.classList.remove(O),[].slice.call(document.getElementsByClassName(A)).forEach(function(e){e.firstChild||e.parentNode.removeChild(e)}),m(x),m(X),b()},g=function(){[].slice.call(document.getElementsByClassName(q)).forEach(function(e){e.classList.remove(q)})},l=function(){var e=document.createElement(Z);return e.classList.add(A),e.dataset.dragsterId=se,e},c=function(){var e=document.createElement(Z);return e.classList.add(x),e.dataset.dragsterId=se,e},i=function(){var e=document.createElement(Z);return e.classList.add(X),e.classList.add(k),e.style.position="fixed",e.dataset.dragsterId=se,document.body.appendChild(e),e},r=function(e,t){var n;e&&e.parentNode&&(n=ee.wrapDraggableElements===$?e:e.nextSibling,e.parentNode.insertBefore(t,n))},d=function(e,t){e&&e.parentNode&&e.parentNode.insertBefore(t,e)},E=function(e){return e.classList&&e.classList.contains(A)&&e.dataset.dragsterId===se},L=function(e){return e.classList&&e.classList.contains(ee.dragOnlyRegionCssClass)},b=function(){ee.updateRegionsHeight&&[].slice.call(document.getElementsByClassName(Y)).forEach(function(e){var t=[].slice.call(e.querySelectorAll(ee.elementSelector)),n=ee.minimumRegionHeight;t.length&&(t.forEach(function(e){var t=window.getComputedStyle(e);n+=e.offsetHeight+parseInt(t.marginTop,10)+parseInt(t.marginBottom,10)}),e.style.height=n+V)})},H=function(e,t){u(v,e),u(v,t)},s={mousedown:function(e){if(ee.dragHandleCssClass&&("string"!=typeof ee.dragHandleCssClass||!e.target.classList.contains(ee.dragHandleCssClass)))return $;var t,n,o=e.type===F,r=e.changedTouches?e.changedTouches[0]:e;return re=JSON.parse(JSON.stringify(oe)),e.dragster=re,ee.onBeforeDragStart(e)===$||3===e.which?$:(e.preventDefault(),(v=p(e.target,E))?(t=o?U:W,n=o?z:G,a.forEach(function(e){e.addEventListener(t,s.mousemove,$),e.addEventListener(n,s.mouseup,$)}),document.body.addEventListener(t,s.mousemove,$),document.body.addEventListener(n,s.mouseup,$),o=v.getBoundingClientRect(),P=o.left-r.clientX,R=o.top-r.clientY,(f=i()).innerHTML=v.innerHTML,f.style.width=o.width+V,f.style.height=o.height+V,f.dataset.dragsterId=se,h=f.getBoundingClientRect(),v.classList.add(O),re.drag.node=v,re.shadow.node=f,e.dragster=re,void ee.onAfterDragStart(e)):$)},mousemove:function(e){if(e.dragster=re,ee.onBeforeDragMove(e)===$||!h)return $;e.preventDefault();var t=e.changedTouches?e.changedTouches[0]:e,n=t.view?t.view.pageXOffset:0,o=t.view?t.view.pageYOffset:0,r=t.clientY+o,a=t.clientX+n,s=document.elementFromPoint(t.clientX,t.clientY),d=p(s,E),l=ee.shadowElementUnderMouse?t.clientY+R:t.clientY,i=ee.shadowElementUnderMouse?a+P:a-h.width/2,c=re.drag.node&&re.drag.node.dataset,m=!(!d||!p(d,L)),u=s.dataset.dragsterId===se,g=s.classList.contains(Y)&&u,n=s.classList.contains(ee.dragOnlyRegionCssClass)&&u,t=s.classList.contains(x),a=0<s.getElementsByClassName(A).length,u=0<s.getElementsByClassName(x).length;clearTimeout(w),f.style.top=l+V,f.style.left=i+V,f.classList.remove(k),re.shadow.top=l,re.shadow.left=i,c||g||t?d&&d!==v&&!m?(N.removePlaceholders(),N.addPlaceholderOnTarget(d,r,o)):!g||n||a||u?g&&!n&&a&&!u&&(N.removePlaceholders(),N.addPlaceholderInRegionBelowTargets(s)):(N.removePlaceholders(),N.addPlaceholderInRegion(s)):N.removePlaceholders(),ee.scrollWindowOnDrag&&B(e),b(),ee.onAfterDragMove(e)},mouseup:function(e){e.dragster=re;var t,n,o=e.type===F,r=o?U:W,a=o?z:G;return ee.onBeforeDragEnd(e)===$?(H(r,a),$):(n=ee.replaceElements?q:x,t=document.getElementsByClassName(n)[0],o=!(!v||!p(v,L)),n=ee.cloneElements&&o,w=setTimeout(H,200),g(),v&&t?(o=p(t,E),v!==(o=o||t)&&(ee.replaceElements||n?ee.replaceElements&&!n?(e.dragster=M.replaceElements(e.dragster,o),ee.onAfterDragDrop(e)):!ee.replaceElements&&n&&(e.dragster=M.cloneElements(e.dragster,t,o),ee.onAfterDragDrop(e)):(e.dragster=M.moveElement(e.dragster,t,o),ee.onAfterDragDrop(e)),o.classList.remove("is-drag-over")),H(r,a),void ee.onAfterDragEnd(e)):(H(r,a),$))}},N={addPlaceholderOnTarget:function(e,t,n){var o=e.getBoundingClientRect(),r=c(),a=o.height/2;g(),ee.replaceElements?e.classList.add(q):t-n-o.top<a&&!te?(m(x),r.dataset.placeholderPosition=K,d(e.firstChild,r),re.placeholder.position=K):o.bottom-(t-n)<a&&!ne&&(m(x),r.dataset.placeholderPosition=Q,e.appendChild(r),re.placeholder.position=Q),re.placeholder.node=r,re.drop.node=e},addPlaceholderInRegion:function(e){var t=c();e.appendChild(t),re.placeholder.position=Q,re.placeholder.node=t,re.drop.node=e},addPlaceholderInRegionBelowTargets:function(e){var t=[].slice.call(e.getElementsByClassName(A)).filter(function(e){return e.dataset.dragsterId===se}),e=t[t.length-1],t=c();t.dataset.placeholderPosition=Q,m(x),e.appendChild(t),re.placeholder.position=Q,re.placeholder.node=t,re.drop.node=e},removePlaceholders:function(){ee.replaceElements?g():m(x)}},M={moveElement:function(e,t,n){var o=ee.wrapDraggableElements===$?v:l();return t.dataset.placeholderPosition===K?d(n,o):ee.wrapDraggableElements===$?r(o,n):r(n,o),v.firstChild&&!0===ee.wrapDraggableElements&&o.appendChild(v.firstChild),e.dropped=o,e},replaceElements:function(e,t){var n=document.getElementsByClassName(j)[0];return n.innerHTML=v.innerHTML,v.innerHTML=t.innerHTML,t.innerHTML=n.innerHTML,n.innerHTML="",e.dropped=n,e},cloneElements:function(e,t,n){var o=v.cloneNode(!0);return(t.dataset.placeholderPosition===K?d:r)(n,o),u(o),e.clonedFrom=v,e.clonedTo=o,e}},B=function(e){e=e.changedTouches?e.changedTouches[0]:e;ae-e.clientY<60?window.scrollBy(0,10):e.clientY<60&&window.scrollBy(0,-10)},T=function(){ae=window.innerHeight},D(o),(S=function(){a.forEach(function(e){e.classList.add(Y),e.dataset.dragsterId=se,e.addEventListener(J,s.mousedown,$),e.addEventListener(F,s.mousedown,$)})})(),window.addEventListener("resize",T,!1),{update:function(){o=y(),D(o),b(),T()},updateRegions:function(){a=C(),S()},destroy:function(){a.forEach(function(e){e.classList.remove(Y),e.removeEventListener(J,s.mousedown,$),e.removeEventListener(W,s.mousemove,$),e.removeEventListener(G,s.mouseup,$),e.removeEventListener(F,s.mousedown,$),e.removeEventListener(U,s.mousemove,$),e.removeEventListener(z,s.mouseup,$)}),document.body.removeEventListener(W,s.mousemove,$),document.body.removeEventListener(U,s.mousemove,$),document.body.removeEventListener(G,s.mouseup,$),document.body.removeEventListener(z,s.mouseup,$),window.removeEventListener("resize",T,!1)}}}});