mirror of https://github.com/RubaXa/Sortable.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
3 lines
14 KiB
3 lines
14 KiB
/*! Sortable 1.5.0-rc1 - MIT | git://github.com/rubaxa/Sortable.git */ |
|
|
|
!function(t){"use strict";"function"==typeof define&&define.amd?define(t):"undefined"!=typeof module&&void 0!==module.exports?module.exports=t():window.Sortable=t()}(function(){"use strict";function t(t,e){if(!t||!t.nodeType||1!==t.nodeType)throw"Sortable: `el` must be HTMLElement, and not "+{}.toString.call(t);this.el=t,this.options=e=_({},e),t[W]=this;var n={group:Math.random(),sort:!0,disabled:!1,store:null,handle:null,scroll:!0,scrollSensitivity:30,scrollSpeed:10,draggable:/[uo]l/i.test(t.nodeName)?"li":">*",ghostClass:"sortable-ghost",chosenClass:"sortable-chosen",dragClass:"sortable-drag",ignore:"a, img",filter:null,preventOnFilter:!0,animation:0,setData:function(t,e){t.setData("Text",e.textContent)},dropBubble:!1,dragoverBubble:!1,dataIdAttr:"data-id",delay:0,forceFallback:!1,fallbackClass:"sortable-fallback",fallbackOnBody:!1,fallbackTolerance:0,fallbackOffset:{x:0,y:0}};for(var i in n)!(i in e)&&(e[i]=n[i]);it(e);for(var o in this)"_"===o.charAt(0)&&"function"==typeof this[o]&&(this[o]=this[o].bind(this));this.nativeDraggable=!e.forceFallback&&Z,r(t,"mousedown",this._onTapStart),r(t,"touchstart",this._onTapStart),r(t,"pointerdown",this._onTapStart),this.nativeDraggable&&(r(t,"dragover",this),r(t,"dragenter",this)),et.push(this._onDragOver),e.store&&this.sort(e.store.get(this))}function e(t,e){"clone"!==t.lastPullMode&&(e=!0),C&&C.state!==e&&(s(C,"display",e?"none":""),e||C.state&&(t.options.group.revertClone?(S.insertBefore(C,E),t._animate(y,C)):S.insertBefore(C,y)),C.state=e)}function n(t,e,n){if(t){n=n||V;do{if(">*"===e&&t.parentNode===n||m(t,e))return t}while(t=i(t))}return null}function i(t){var e=t.host;return e&&e.nodeType?e:t.parentNode}function o(t){t.dataTransfer&&(t.dataTransfer.dropEffect="move"),t.preventDefault()}function r(t,e,n){t.addEventListener(e,n,Q)}function a(t,e,n){t.removeEventListener(e,n,Q)}function l(t,e,n){if(t)if(t.classList)t.classList[n?"add":"remove"](e);else{var i=(" "+t.className+" ").replace(H," ").replace(" "+e+" "," ");t.className=(i+(n?" "+e:"")).replace(H," ")}}function s(t,e,n){var i=t&&t.style;if(i){if(void 0===n)return V.defaultView&&V.defaultView.getComputedStyle?n=V.defaultView.getComputedStyle(t,""):t.currentStyle&&(n=t.currentStyle),void 0===e?n:n[e];e in i||(e="-webkit-"+e),i[e]=n+("string"==typeof n?"":"px")}}function c(t,e,n){if(t){var i=t.getElementsByTagName(e),o=0,r=i.length;if(n)for(;o<r;o++)n(i[o],o);return i}return[]}function d(t,e,n,i,o,r,a){t=t||e[W];var l=V.createEvent("Event"),s=t.options,c="on"+n.charAt(0).toUpperCase()+n.substr(1);l.initEvent(n,!0,!0),l.to=e,l.from=o||e,l.item=i||e,l.clone=C,l.oldIndex=r,l.newIndex=a,e.dispatchEvent(l),s[c]&&s[c].call(t,l)}function h(t,e,n,i,o,r,a){var l,s,c=t[W],d=c.options.onMove;return(l=V.createEvent("Event")).initEvent("move",!0,!0),l.to=e,l.from=t,l.dragged=n,l.draggedRect=i,l.related=o||e,l.relatedRect=r||e.getBoundingClientRect(),t.dispatchEvent(l),d&&(s=d.call(c,l,a)),s}function u(t){t.draggable=!1}function f(){K=!1}function p(t,e){var n=t.lastElementChild.getBoundingClientRect();return e.clientY-(n.top+n.height)>5||e.clientX-(n.left+n.width)>5}function g(t){for(var e=t.tagName+t.className+t.src+t.href+t.textContent,n=e.length,i=0;n--;)i+=e.charCodeAt(n);return i.toString(36)}function v(t,e){var n=0;if(!t||!t.parentNode)return-1;for(;t&&(t=t.previousElementSibling);)"TEMPLATE"===t.nodeName.toUpperCase()||">*"!==e&&!m(t,e)||n++;return n}function m(t,e){if(t){var n=(e=e.split(".")).shift().toUpperCase(),i=new RegExp("\\s("+e.join("|")+")(?=\\s)","g");return!(""!==n&&t.nodeName.toUpperCase()!=n||e.length&&((" "+t.className+" ").match(i)||[]).length!=e.length)}return!1}function b(t,e){var n,i;return function(){void 0===n&&(n=arguments,i=this,setTimeout(function(){1===n.length?t.call(i,n[0]):t.apply(i,n),n=void 0},e))}}function _(t,e){if(t&&e)for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n]);return t}function D(t){return z?z(t).clone(!0)[0]:G&&G.dom?G.dom(t).cloneNode(!0):t.cloneNode(!0)}if("undefined"==typeof window||!window.document)return function(){throw new Error("Sortable.js requires a window with a document")};var y,w,T,C,S,E,x,N,B,k,Y,X,O,A,M,R,I,P,F,L,U={},H=/\s+/g,W="Sortable"+(new Date).getTime(),j=window,V=j.document,q=j.parseInt,z=j.jQuery||j.Zepto,G=j.Polymer,Q={capture:!1,passive:!1},Z=!!("draggable"in V.createElement("div")),J=function(t){return!navigator.userAgent.match(/Trident.*rv[ :]?11\./)&&(t=V.createElement("x"),t.style.cssText="pointer-events:auto","auto"===t.style.pointerEvents)}(),K=!1,$=Math.abs,tt=Math.min,et=[],nt=b(function(t,e,n){if(n&&e.scroll){var i,o,r,a,l,s,c=n[W],d=e.scrollSensitivity,h=e.scrollSpeed,u=t.clientX,f=t.clientY,p=window.innerWidth,g=window.innerHeight;if(B!==n&&(N=e.scroll,B=n,k=e.scrollFn,!0===N)){N=n;do{if(N.offsetWidth<N.scrollWidth||N.offsetHeight<N.scrollHeight)break}while(N=N.parentNode)}N&&(i=N,o=N.getBoundingClientRect(),r=($(o.right-u)<=d)-($(o.left-u)<=d),a=($(o.bottom-f)<=d)-($(o.top-f)<=d)),r||a||(a=(g-f<=d)-(f<=d),((r=(p-u<=d)-(u<=d))||a)&&(i=j)),U.vx===r&&U.vy===a&&U.el===i||(U.el=i,U.vx=r,U.vy=a,clearInterval(U.pid),i&&(U.pid=setInterval(function(){if(s=a?a*h:0,l=r?r*h:0,"function"==typeof k)return k.call(c,l,s,t);i===j?j.scrollTo(j.pageXOffset+l,j.pageYOffset+s):(i.scrollTop+=s,i.scrollLeft+=l)},24)))}},30),it=function(t){function e(t,e){return void 0!==t&&!0!==t||(t=n.name),"function"==typeof t?t:function(n,i){var o=i.options.group.name;return e?t:t&&(t.join?t.indexOf(o)>-1:o==t)}}var n={},i=t.group;i&&"object"==typeof i||(i={name:i}),n.name=i.name,n.checkPull=e(i.pull,!0),n.checkPut=e(i.put),n.revertClone=i.revertClone,t.group=n};return t.prototype={constructor:t,_onTapStart:function(t){var e,i=this,o=this.el,r=this.options,a=r.preventOnFilter,l=t.type,s=t.touches&&t.touches[0],c=(s||t).target,h=t.target.shadowRoot&&t.path[0]||c,u=r.filter;if(!y&&!("mousedown"===l&&0!==t.button||r.disabled)&&(c=n(c,r.draggable,o))&&x!==c){if(e=v(c,r.draggable),"function"==typeof u){if(u.call(this,t,c,this))return d(i,h,"filter",c,o,e),void(a&&t.preventDefault())}else if(u&&(u=u.split(",").some(function(t){if(t=n(h,t.trim(),o))return d(i,t,"filter",c,o,e),!0})))return void(a&&t.preventDefault());r.handle&&!n(h,r.handle,o)||this._prepareDragStart(t,s,c,e)}},_prepareDragStart:function(t,e,n,i){var o,a=this,s=a.el,h=a.options,f=s.ownerDocument;n&&!y&&n.parentNode===s&&(P=t,S=s,w=(y=n).parentNode,E=y.nextSibling,x=n,R=h.group,A=i,this._lastX=(e||t).clientX,this._lastY=(e||t).clientY,y.style["will-change"]="transform",o=function(){a._disableDelayedDrag(),y.draggable=a.nativeDraggable,l(y,h.chosenClass,!0),a._triggerDragStart(t,e),d(a,S,"choose",y,S,A)},h.ignore.split(",").forEach(function(t){c(y,t.trim(),u)}),r(f,"mouseup",a._onDrop),r(f,"touchend",a._onDrop),r(f,"touchcancel",a._onDrop),r(f,"pointercancel",a._onDrop),h.delay?(r(f,"mouseup",a._disableDelayedDrag),r(f,"touchend",a._disableDelayedDrag),r(f,"touchcancel",a._disableDelayedDrag),r(f,"mousemove",a._disableDelayedDrag),r(f,"touchmove",a._disableDelayedDrag),r(f,"pointermove",a._disableDelayedDrag),a._dragStartTimer=setTimeout(o,h.delay)):o(),h.forceFallback&&t.preventDefault())},_disableDelayedDrag:function(){var t=this.el.ownerDocument;clearTimeout(this._dragStartTimer),a(t,"mouseup",this._disableDelayedDrag),a(t,"touchend",this._disableDelayedDrag),a(t,"touchcancel",this._disableDelayedDrag),a(t,"mousemove",this._disableDelayedDrag),a(t,"touchmove",this._disableDelayedDrag),a(t,"pointermove",this._disableDelayedDrag)},_triggerDragStart:function(t,e){(e=e||("touch"==t.pointerType?t:null))?(P={target:y,clientX:e.clientX,clientY:e.clientY},this._onDragStart(P,"touch")):this.nativeDraggable?(r(y,"dragend",this),r(S,"dragstart",this._onDragStart)):this._onDragStart(P,!0);try{V.selection?setTimeout(function(){V.selection.empty()}):window.getSelection().removeAllRanges()}catch(t){}},_dragStarted:function(){if(S&&y){var e=this.options;l(y,e.ghostClass,!0),l(y,e.dragClass,!1),t.active=this,d(this,S,"start",y,S,A)}else this._nulling()},_emulateDragOver:function(){if(F){if(this._lastX===F.clientX&&this._lastY===F.clientY)return;this._lastX=F.clientX,this._lastY=F.clientY,J||s(T,"display","none");var t=V.elementFromPoint(F.clientX,F.clientY),e=t,n=et.length;if(e)do{if(e[W]){for(;n--;)et[n]({clientX:F.clientX,clientY:F.clientY,target:t,rootEl:e});break}t=e}while(e=e.parentNode);J||s(T,"display","")}},_onTouchMove:function(e){if(P){var n=this.options,i=n.fallbackTolerance,o=n.fallbackOffset,r=e.touches?e.touches[0]:e,a=r.clientX-P.clientX+o.x,l=r.clientY-P.clientY+o.y,c=e.touches?"translate3d("+a+"px,"+l+"px,0)":"translate("+a+"px,"+l+"px)";if(!t.active){if(i&&tt($(r.clientX-this._lastX),$(r.clientY-this._lastY))<i)return;this._dragStarted()}this._appendGhost(),L=!0,F=r,s(T,"webkitTransform",c),s(T,"mozTransform",c),s(T,"msTransform",c),s(T,"transform",c),e.preventDefault()}},_appendGhost:function(){if(!T){var t,e=y.getBoundingClientRect(),n=s(y),i=this.options;l(T=y.cloneNode(!0),i.ghostClass,!1),l(T,i.fallbackClass,!0),l(T,i.dragClass,!0),s(T,"top",e.top-q(n.marginTop,10)),s(T,"left",e.left-q(n.marginLeft,10)),s(T,"width",e.width),s(T,"height",e.height),s(T,"opacity","0.8"),s(T,"position","fixed"),s(T,"zIndex","100000"),s(T,"pointerEvents","none"),i.fallbackOnBody&&V.body.appendChild(T)||S.appendChild(T),t=T.getBoundingClientRect(),s(T,"width",2*e.width-t.width),s(T,"height",2*e.height-t.height)}},_onDragStart:function(t,e){var n=t.dataTransfer,i=this.options;this._offUpEvents(),R.checkPull(this,this,y,t)&&((C=D(y)).draggable=!1,C.style["will-change"]="",s(C,"display","none"),l(C,this.options.chosenClass,!1),S.insertBefore(C,y),d(this,S,"clone",y)),l(y,i.dragClass,!0),e?("touch"===e?(r(V,"touchmove",this._onTouchMove),r(V,"touchend",this._onDrop),r(V,"touchcancel",this._onDrop),r(V,"pointermove",this._onTouchMove),r(V,"pointerup",this._onDrop)):(r(V,"mousemove",this._onTouchMove),r(V,"mouseup",this._onDrop)),this._loopId=setInterval(this._emulateDragOver,50)):(n&&(n.effectAllowed="move",i.setData&&i.setData.call(this,n,y)),r(V,"drop",this),setTimeout(this._dragStarted,0))},_onDragOver:function(i){var o,r,a,l,c=this.el,d=this.options,u=d.group,g=t.active,v=R===u,m=d.sort;if(void 0!==i.preventDefault&&(i.preventDefault(),!d.dragoverBubble&&i.stopPropagation()),L=!0,g&&!d.disabled&&(v?m||(l=!S.contains(y)):I===this||(g.lastPullMode=R.checkPull(this,g,y,i))&&u.checkPut(this,g,y,i))&&(void 0===i.rootEl||i.rootEl===this.el)){if(nt(i,d,this.el),K)return;if(o=n(i.target,d.draggable,c),r=y.getBoundingClientRect(),I=this,l)return e(g,!0),w=S,void(C||E?S.insertBefore(y,C||E):m||S.appendChild(y));if(0===c.children.length||c.children[0]===T||c===i.target&&p(c,i)){if(0!==c.children.length&&c.children[0]!==T&&c===i.target&&(o=c.lastElementChild),o){if(o.animated)return;a=o.getBoundingClientRect()}e(g,v),!1!==h(S,c,y,r,o,a,i)&&(y.contains(c)||(c.appendChild(y),w=c),this._animate(r,y),o&&this._animate(a,o))}else if(o&&!o.animated&&o!==y&&void 0!==o.parentNode[W]){Y!==o&&(Y=o,X=s(o),O=s(o.parentNode));var b,_=(a=o.getBoundingClientRect()).right-a.left,D=a.bottom-a.top,x=/left|right|inline/.test(X.cssFloat+X.display)||"flex"==O.display&&0===O["flex-direction"].indexOf("row"),N=o.offsetWidth>y.offsetWidth,B=o.offsetHeight>y.offsetHeight,k=(x?(i.clientX-a.left)/_:(i.clientY-a.top)/D)>.5,A=o.nextElementSibling,M=h(S,c,y,r,o,a,i);if(!1!==M){if(K=!0,setTimeout(f,30),e(g,v),1===M||-1===M)b=1===M;else if(x){var P=y.offsetTop,F=o.offsetTop;b=P===F?o.previousElementSibling===y&&!N||k&&N:o.previousElementSibling===y||y.previousElementSibling===o?(i.clientY-a.top)/D>.5:F>P}else b=A!==y&&!B||k&&B;y.contains(c)||(b&&!A?c.appendChild(y):o.parentNode.insertBefore(y,b?A:o)),w=y.parentNode,this._animate(r,y),this._animate(a,o)}}}},_animate:function(t,e){var n=this.options.animation;if(n){var i=e.getBoundingClientRect();1===t.nodeType&&(t=t.getBoundingClientRect()),s(e,"transition","none"),s(e,"transform","translate3d("+(t.left-i.left)+"px,"+(t.top-i.top)+"px,0)"),e.offsetWidth,s(e,"transition","all "+n+"ms"),s(e,"transform","translate3d(0,0,0)"),clearTimeout(e.animated),e.animated=setTimeout(function(){s(e,"transition",""),s(e,"transform",""),e.animated=!1},n)}},_offUpEvents:function(){var t=this.el.ownerDocument;a(V,"touchmove",this._onTouchMove),a(V,"pointermove",this._onTouchMove),a(t,"mouseup",this._onDrop),a(t,"touchend",this._onDrop),a(t,"pointerup",this._onDrop),a(t,"touchcancel",this._onDrop)},_onDrop:function(e){var n=this.el,i=this.options;clearInterval(this._loopId),clearInterval(U.pid),clearTimeout(this._dragStartTimer),a(V,"mousemove",this._onTouchMove),this.nativeDraggable&&(a(V,"drop",this),a(n,"dragstart",this._onDragStart)),this._offUpEvents(),e&&(L&&(e.preventDefault(),!i.dropBubble&&e.stopPropagation()),T&&T.parentNode.removeChild(T),y&&(this.nativeDraggable&&a(y,"dragend",this),u(y),y.style["will-change"]="",l(y,this.options.ghostClass,!1),l(y,this.options.chosenClass,!1),d(this,S,"unchoose",y,S,A),S!==w?(M=v(y,i.draggable))>=0&&(d(null,w,"add",y,S,A,M),d(this,S,"remove",y,S,A,M),d(null,w,"sort",y,S,A,M),d(this,S,"sort",y,S,A,M)):(C&&C.parentNode.removeChild(C),y.nextSibling!==E&&(M=v(y,i.draggable))>=0&&(d(this,S,"update",y,S,A,M),d(this,S,"sort",y,S,A,M))),t.active&&(null!=M&&-1!==M||(M=A),d(this,S,"end",y,S,A,M),this.save()))),this._nulling()},_nulling:function(){S=y=w=T=E=C=x=N=B=P=F=L=M=Y=X=I=R=t.active=null},handleEvent:function(t){var e=t.type;"dragover"===e||"dragenter"===e?y&&(this._onDragOver(t),o(t)):"drop"!==e&&"dragend"!==e||this._onDrop(t)},toArray:function(){for(var t,e=[],i=this.el.children,o=0,r=i.length,a=this.options;o<r;o++)n(t=i[o],a.draggable,this.el)&&e.push(t.getAttribute(a.dataIdAttr)||g(t));return e},sort:function(t){var e={},i=this.el;this.toArray().forEach(function(t,o){var r=i.children[o];n(r,this.options.draggable,i)&&(e[t]=r)},this),t.forEach(function(t){e[t]&&(i.removeChild(e[t]),i.appendChild(e[t]))})},save:function(){var t=this.options.store;t&&t.set(this)},closest:function(t,e){return n(t,e||this.options.draggable,this.el)},option:function(t,e){var n=this.options;if(void 0===e)return n[t];n[t]=e,"group"===t&&it(n)},destroy:function(){var t=this.el;t[W]=null,a(t,"mousedown",this._onTapStart),a(t,"touchstart",this._onTapStart),a(t,"pointerdown",this._onTapStart),this.nativeDraggable&&(a(t,"dragover",this),a(t,"dragenter",this)),Array.prototype.forEach.call(t.querySelectorAll("[draggable]"),function(t){t.removeAttribute("draggable")}),et.splice(et.indexOf(this._onDragOver),1),this._onDrop(),this.el=t=null}},t.utils={on:r,off:a,css:s,find:c,is:function(t,e){return!!n(t,e,t)},extend:_,throttle:b,closest:n,toggleClass:l,clone:D,index:v},t.create=function(e,n){return new t(e,n)},t.version="1.5.0-rc1",t}); |