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