diff --git a/Sortable.js b/Sortable.js index 6a025ea..6567142 100644 --- a/Sortable.js +++ b/Sortable.js @@ -4,7 +4,6 @@ * @license MIT */ - (function (factory) { "use strict"; @@ -24,10 +23,10 @@ })(function () { "use strict"; - if (typeof window == "undefined" || typeof window.document == "undefined") { - return function() { - throw new Error( "Sortable.js requires a window with a document" ); - } + if (typeof window == "undefined" || !window.document) { + return function SortableError() { + throw new Error("Sortable.js requires a window with a document"); + }; } var dragEl, diff --git a/Sortable.min.js b/Sortable.min.js index 7e8099b..5124456 100644 --- a/Sortable.min.js +++ b/Sortable.min.js @@ -1,2 +1,2 @@ /*! Sortable 1.4.2 - MIT | git://github.com/rubaxa/Sortable.git */ -!function(a){"use strict";"function"==typeof define&&define.amd?define(a):"undefined"!=typeof module&&"undefined"!=typeof module.exports?module.exports=a():"undefined"!=typeof Package?Sortable=a():window.Sortable=a()}(function(){"use strict";function a(a,b){if(!a||!a.nodeType||1!==a.nodeType)throw"Sortable: `el` must be HTMLElement, and not "+{}.toString.call(a);this.el=a,this.options=b=s({},b),a[M]=this;var c={group:Math.random(),sort:!0,disabled:!1,store:null,handle:null,scroll:!0,scrollSensitivity:30,scrollSpeed:10,draggable:/[uo]l/i.test(a.nodeName)?"li":">*",ghostClass:"sortable-ghost",chosenClass:"sortable-chosen",ignore:"a, img",filter:null,animation:0,setData:function(a,b){a.setData("Text",b.textContent)},dropBubble:!1,dragoverBubble:!1,dataIdAttr:"data-id",delay:0,forceFallback:!1,fallbackClass:"sortable-fallback",fallbackOnBody:!1,fallbackTolerance:0};for(var d in c)!(d in b)&&(b[d]=c[d]);X(b);for(var f in this)"_"===f.charAt(0)&&(this[f]=this[f].bind(this));this.nativeDraggable=b.forceFallback?!1:Q,e(a,"mousedown",this._onTapStart),e(a,"touchstart",this._onTapStart),this.nativeDraggable&&(e(a,"dragover",this),e(a,"dragenter",this)),V.push(this._onDragOver),b.store&&this.sort(b.store.get(this))}function b(a){w&&w.state!==a&&(h(w,"display",a?"none":""),!a&&w.state&&x.insertBefore(w,t),w.state=a)}function c(a,b,c){if(a){c=c||O;do if(">*"===b&&a.parentNode===c||q(a,b))return a;while(a!==c&&(a=a.parentNode))}return null}function d(a){a.dataTransfer&&(a.dataTransfer.dropEffect="move"),a.preventDefault()}function e(a,b,c){a.addEventListener(b,c,!1)}function f(a,b,c){a.removeEventListener(b,c,!1)}function g(a,b,c){if(a)if(a.classList)a.classList[c?"add":"remove"](b);else{var d=(" "+a.className+" ").replace(L," ").replace(" "+b+" "," ");a.className=(d+(c?" "+b:"")).replace(L," ")}}function h(a,b,c){var d=a&&a.style;if(d){if(void 0===c)return O.defaultView&&O.defaultView.getComputedStyle?c=O.defaultView.getComputedStyle(a,""):a.currentStyle&&(c=a.currentStyle),void 0===b?c:c[b];b in d||(b="-webkit-"+b),d[b]=c+("string"==typeof c?"":"px")}}function i(a,b,c){if(a){var d=a.getElementsByTagName(b),e=0,f=d.length;if(c)for(;f>e;e++)c(d[e],e);return d}return[]}function j(a,b,c,d,e,f,g){var h=O.createEvent("Event"),i=(a||b[M]).options,j="on"+c.charAt(0).toUpperCase()+c.substr(1);h.initEvent(c,!0,!0),h.to=b,h.from=e||b,h.item=d||b,h.clone=w,h.oldIndex=f,h.newIndex=g,b.dispatchEvent(h),i[j]&&i[j].call(a,h)}function k(a,b,c,d,e,f){var g,h,i=a[M],j=i.options.onMove;return g=O.createEvent("Event"),g.initEvent("move",!0,!0),g.to=b,g.from=a,g.dragged=c,g.draggedRect=d,g.related=e||b,g.relatedRect=f||b.getBoundingClientRect(),a.dispatchEvent(g),j&&(h=j.call(i,g)),h}function l(a){a.draggable=!1}function m(){S=!1}function n(a,b){var c=a.lastElementChild,d=c.getBoundingClientRect();return(b.clientY-(d.top+d.height)>5||b.clientX-(d.right+d.width)>5)&&c}function o(a){for(var b=a.tagName+a.className+a.src+a.href+a.textContent,c=b.length,d=0;c--;)d+=b.charCodeAt(c);return d.toString(36)}function p(a,b){var c=0;if(!a||!a.parentNode)return-1;for(;a&&(a=a.previousElementSibling);)"TEMPLATE"!==a.nodeName.toUpperCase()&&q(a,b)&&c++;return c}function q(a,b){if(a){b=b.split(".");var c=b.shift().toUpperCase(),d=new RegExp("\\s("+b.join("|")+")(?=\\s)","g");return!(""!==c&&a.nodeName.toUpperCase()!=c||b.length&&((" "+a.className+" ").match(d)||[]).length!=b.length)}return!1}function r(a,b){var c,d;return function(){void 0===c&&(c=arguments,d=this,setTimeout(function(){1===c.length?a.call(d,c[0]):a.apply(d,c),c=void 0},b))}}function s(a,b){if(a&&b)for(var c in b)b.hasOwnProperty(c)&&(a[c]=b[c]);return a}var t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K={},L=/\s+/g,M="Sortable"+(new Date).getTime(),N=window,O=N.document,P=N.parseInt,Q=!!("draggable"in O.createElement("div")),R=function(a){return a=O.createElement("x"),a.style.cssText="pointer-events:auto","auto"===a.style.pointerEvents}(),S=!1,T=Math.abs,U=Math.min,V=([].slice,[]),W=r(function(a,b,c){if(c&&b.scroll){var d,e,f,g,h=b.scrollSensitivity,i=b.scrollSpeed,j=a.clientX,k=a.clientY,l=window.innerWidth,m=window.innerHeight;if(A!==c&&(z=b.scroll,A=c,z===!0)){z=c;do if(z.offsetWidth=l-j)-(h>=j),g=(h>=m-k)-(h>=k),(f||g)&&(d=N)),(K.vx!==f||K.vy!==g||K.el!==d)&&(K.el=d,K.vx=f,K.vy=g,clearInterval(K.pid),d&&(K.pid=setInterval(function(){d===N?N.scrollTo(N.pageXOffset+f*i,N.pageYOffset+g*i):(g&&(d.scrollTop+=g*i),f&&(d.scrollLeft+=f*i))},24)))}},30),X=function(a){var b=a.group;b&&"object"==typeof b||(b=a.group={name:b}),["pull","put"].forEach(function(a){a in b||(b[a]=!0)}),a.groups=" "+b.name+(b.put.join?" "+b.put.join(" "):"")+" "};return a.prototype={constructor:a,_onTapStart:function(a){var b=this,d=this.el,e=this.options,f=a.type,g=a.touches&&a.touches[0],h=(g||a).target,i=h,k=e.filter;if(!t&&!("mousedown"===f&&0!==a.button||e.disabled)&&(h=c(h,e.draggable,d),h&&(!e.handle||c(i,e.handle,d)))){if(E=p(h,e.draggable),"function"==typeof k){if(k.call(this,a,h,this))return j(b,i,"filter",h,d,E),void a.preventDefault()}else if(k&&(k=k.split(",").some(function(a){return a=c(i,a.trim(),d),a?(j(b,a,"filter",h,d,E),!0):void 0})))return void a.preventDefault();this._prepareDragStart(a,g,h)}},_prepareDragStart:function(a,b,c){var d,f=this,h=f.el,k=f.options,m=h.ownerDocument;c&&!t&&c.parentNode===h&&(H=a,x=h,t=c,u=t.parentNode,y=t.nextSibling,G=k.group,this._lastX=(b||a).clientX,this._lastY=(b||a).clientY,d=function(){f._disableDelayedDrag(),t.draggable=!0,g(t,f.options.chosenClass,!0),f._triggerDragStart(b),j(f,x,"choose",t,x,E)},k.ignore.split(",").forEach(function(a){i(t,a.trim(),l)}),e(m,"mouseup",f._onDrop),e(m,"touchend",f._onDrop),e(m,"touchcancel",f._onDrop),k.delay?(e(m,"mouseup",f._disableDelayedDrag),e(m,"touchend",f._disableDelayedDrag),e(m,"touchcancel",f._disableDelayedDrag),e(m,"mousemove",f._disableDelayedDrag),e(m,"touchmove",f._disableDelayedDrag),f._dragStartTimer=setTimeout(d,k.delay)):d())},_disableDelayedDrag:function(){var a=this.el.ownerDocument;clearTimeout(this._dragStartTimer),f(a,"mouseup",this._disableDelayedDrag),f(a,"touchend",this._disableDelayedDrag),f(a,"touchcancel",this._disableDelayedDrag),f(a,"mousemove",this._disableDelayedDrag),f(a,"touchmove",this._disableDelayedDrag)},_triggerDragStart:function(a){a?(H={target:t,clientX:a.clientX,clientY:a.clientY},this._onDragStart(H,"touch")):this.nativeDraggable?(e(t,"dragend",this),e(x,"dragstart",this._onDragStart)):this._onDragStart(H,!0);try{O.selection?O.selection.empty():window.getSelection().removeAllRanges()}catch(b){}},_dragStarted:function(){x&&t&&(g(t,this.options.ghostClass,!0),a.active=this,j(this,x,"start",t,x,E))},_emulateDragOver:function(){if(I){if(this._lastX===I.clientX&&this._lastY===I.clientY)return;this._lastX=I.clientX,this._lastY=I.clientY,R||h(v,"display","none");var a=O.elementFromPoint(I.clientX,I.clientY),b=a,c=" "+this.options.group.name,d=V.length;if(b)do{if(b[M]&&b[M].options.groups.indexOf(c)>-1){for(;d--;)V[d]({clientX:I.clientX,clientY:I.clientY,target:a,rootEl:b});break}a=b}while(b=b.parentNode);R||h(v,"display","")}},_onTouchMove:function(b){if(H){var c=this.options,d=c.fallbackTolerance,e=b.touches?b.touches[0]:b,f=e.clientX-H.clientX,g=e.clientY-H.clientY,i=b.touches?"translate3d("+f+"px,"+g+"px,0)":"translate("+f+"px,"+g+"px)";if(!a.active){if(d&&U(T(e.clientX-this._lastX),T(e.clientY-this._lastY))t.offsetWidth,F=d.offsetHeight>t.offsetHeight,H=(A?(a.clientX-r.left)/s:(a.clientY-r.top)/z)>.5,I=d.nextElementSibling,K=k(x,g,t,e,d,r);if(K!==!1){if(S=!0,setTimeout(m,30),b(o),1===K||-1===K)q=1===K;else if(A){var L=t.offsetTop,N=d.offsetTop;q=L===N?d.previousElementSibling===t&&!E||H&&E:d.previousElementSibling===t||t.previousElementSibling===d?(a.clientY-r.top)/z>.5:N>L}else q=I!==t&&!F||H&&F;t.contains(g)||(q&&!I?g.appendChild(t):d.parentNode.insertBefore(t,q?I:d)),u=t.parentNode,this._animate(e,t),this._animate(r,d)}}}},_animate:function(a,b){var c=this.options.animation;if(c){var d=b.getBoundingClientRect();h(b,"transition","none"),h(b,"transform","translate3d("+(a.left-d.left)+"px,"+(a.top-d.top)+"px,0)"),b.offsetWidth,h(b,"transition","all "+c+"ms"),h(b,"transform","translate3d(0,0,0)"),clearTimeout(b.animated),b.animated=setTimeout(function(){h(b,"transition",""),h(b,"transform",""),b.animated=!1},c)}},_offUpEvents:function(){var a=this.el.ownerDocument;f(O,"touchmove",this._onTouchMove),f(a,"mouseup",this._onDrop),f(a,"touchend",this._onDrop),f(a,"touchcancel",this._onDrop)},_onDrop:function(b){var c=this.el,d=this.options;clearInterval(this._loopId),clearInterval(K.pid),clearTimeout(this._dragStartTimer),f(O,"mousemove",this._onTouchMove),this.nativeDraggable&&(f(O,"drop",this),f(c,"dragstart",this._onDragStart)),this._offUpEvents(),b&&(J&&(b.preventDefault(),!d.dropBubble&&b.stopPropagation()),v&&v.parentNode.removeChild(v),t&&(this.nativeDraggable&&f(t,"dragend",this),l(t),g(t,this.options.ghostClass,!1),g(t,this.options.chosenClass,!1),x!==u?(F=p(t,d.draggable),F>=0&&(j(null,u,"sort",t,x,E,F),j(this,x,"sort",t,x,E,F),j(null,u,"add",t,x,E,F),j(this,x,"remove",t,x,E,F))):(w&&w.parentNode.removeChild(w),t.nextSibling!==y&&(F=p(t,d.draggable),F>=0&&(j(this,x,"update",t,x,E,F),j(this,x,"sort",t,x,E,F)))),a.active&&((null===F||-1===F)&&(F=E),j(this,x,"end",t,x,E,F),this.save())),x=t=u=v=y=w=z=A=H=I=J=F=B=C=G=a.active=null)},handleEvent:function(a){var b=a.type;"dragover"===b||"dragenter"===b?t&&(this._onDragOver(a),d(a)):("drop"===b||"dragend"===b)&&this._onDrop(a)},toArray:function(){for(var a,b=[],d=this.el.children,e=0,f=d.length,g=this.options;f>e;e++)a=d[e],c(a,g.draggable,this.el)&&b.push(a.getAttribute(g.dataIdAttr)||o(a));return b},sort:function(a){var b={},d=this.el;this.toArray().forEach(function(a,e){var f=d.children[e];c(f,this.options.draggable,d)&&(b[a]=f)},this),a.forEach(function(a){b[a]&&(d.removeChild(b[a]),d.appendChild(b[a]))})},save:function(){var a=this.options.store;a&&a.set(this)},closest:function(a,b){return c(a,b||this.options.draggable,this.el)},option:function(a,b){var c=this.options;return void 0===b?c[a]:(c[a]=b,void("group"===a&&X(c)))},destroy:function(){var a=this.el;a[M]=null,f(a,"mousedown",this._onTapStart),f(a,"touchstart",this._onTapStart),this.nativeDraggable&&(f(a,"dragover",this),f(a,"dragenter",this)),Array.prototype.forEach.call(a.querySelectorAll("[draggable]"),function(a){a.removeAttribute("draggable")}),V.splice(V.indexOf(this._onDragOver),1),this._onDrop(),this.el=a=null}},a.utils={on:e,off:f,css:h,find:i,is:function(a,b){return!!c(a,b,a)},extend:s,throttle:r,closest:c,toggleClass:g,index:p},a.create=function(b,c){return new a(b,c)},a.version="1.4.2",a}); \ No newline at end of file +!function(a){"use strict";"function"==typeof define&&define.amd?define(a):"undefined"!=typeof module&&"undefined"!=typeof module.exports?module.exports=a():"undefined"!=typeof Package?Sortable=a():window.Sortable=a()}(function(){"use strict";function a(a,b){if(!a||!a.nodeType||1!==a.nodeType)throw"Sortable: `el` must be HTMLElement, and not "+{}.toString.call(a);this.el=a,this.options=b=s({},b),a[O]=this;var c={group:Math.random(),sort:!0,disabled:!1,store:null,handle:null,scroll:!0,scrollSensitivity:30,scrollSpeed:10,draggable:/[uo]l/i.test(a.nodeName)?"li":">*",ghostClass:"sortable-ghost",chosenClass:"sortable-chosen",ignore:"a, img",filter:null,animation:0,setData:function(a,b){a.setData("Text",b.textContent)},dropBubble:!1,dragoverBubble:!1,dataIdAttr:"data-id",delay:0,forceFallback:!1,fallbackClass:"sortable-fallback",fallbackOnBody:!1,fallbackTolerance:0};for(var d in c)!(d in b)&&(b[d]=c[d]);_(b);for(var f in this)"_"===f.charAt(0)&&(this[f]=this[f].bind(this));this.nativeDraggable=b.forceFallback?!1:U,e(a,"mousedown",this._onTapStart),e(a,"touchstart",this._onTapStart),this.nativeDraggable&&(e(a,"dragover",this),e(a,"dragenter",this)),Z.push(this._onDragOver),b.store&&this.sort(b.store.get(this))}function b(a){x&&x.state!==a&&(h(x,"display",a?"none":""),!a&&x.state&&y.insertBefore(x,u),x.state=a)}function c(a,b,c){if(a){c=c||Q;do if(">*"===b&&a.parentNode===c||q(a,b))return a;while(a!==c&&(a=a.parentNode))}return null}function d(a){a.dataTransfer&&(a.dataTransfer.dropEffect="move"),a.preventDefault()}function e(a,b,c){a.addEventListener(b,c,!1)}function f(a,b,c){a.removeEventListener(b,c,!1)}function g(a,b,c){if(a)if(a.classList)a.classList[c?"add":"remove"](b);else{var d=(" "+a.className+" ").replace(N," ").replace(" "+b+" "," ");a.className=(d+(c?" "+b:"")).replace(N," ")}}function h(a,b,c){var d=a&&a.style;if(d){if(void 0===c)return Q.defaultView&&Q.defaultView.getComputedStyle?c=Q.defaultView.getComputedStyle(a,""):a.currentStyle&&(c=a.currentStyle),void 0===b?c:c[b];b in d||(b="-webkit-"+b),d[b]=c+("string"==typeof c?"":"px")}}function i(a,b,c){if(a){var d=a.getElementsByTagName(b),e=0,f=d.length;if(c)for(;f>e;e++)c(d[e],e);return d}return[]}function j(a,b,c,d,e,f,g){a=a||b[O];var h=Q.createEvent("Event"),i=a.options,j="on"+c.charAt(0).toUpperCase()+c.substr(1);h.initEvent(c,!0,!0),h.to=b,h.from=e||b,h.item=d||b,h.clone=x,h.oldIndex=f,h.newIndex=g,b.dispatchEvent(h),i[j]&&i[j].call(a,h)}function k(a,b,c,d,e,f){var g,h,i=a[O],j=i.options.onMove;return g=Q.createEvent("Event"),g.initEvent("move",!0,!0),g.to=b,g.from=a,g.dragged=c,g.draggedRect=d,g.related=e||b,g.relatedRect=f||b.getBoundingClientRect(),a.dispatchEvent(g),j&&(h=j.call(i,g)),h}function l(a){a.draggable=!1}function m(){W=!1}function n(a,b){var c=a.lastElementChild,d=c.getBoundingClientRect();return(X(b.clientY-(d.top+d.height))>5||X(b.clientX-(d.right+d.width))>5)&&c}function o(a){for(var b=a.tagName+a.className+a.src+a.href+a.textContent,c=b.length,d=0;c--;)d+=b.charCodeAt(c);return d.toString(36)}function p(a,b){var c=0;if(!a||!a.parentNode)return-1;for(;a&&(a=a.previousElementSibling);)"TEMPLATE"===a.nodeName.toUpperCase()||">*"!==b&&!q(a,b)||c++;return c}function q(a,b){if(a){b=b.split(".");var c=b.shift().toUpperCase(),d=new RegExp("\\s("+b.join("|")+")(?=\\s)","g");return!(""!==c&&a.nodeName.toUpperCase()!=c||b.length&&((" "+a.className+" ").match(d)||[]).length!=b.length)}return!1}function r(a,b){var c,d;return function(){void 0===c&&(c=arguments,d=this,setTimeout(function(){1===c.length?a.call(d,c[0]):a.apply(d,c),c=void 0},b))}}function s(a,b){if(a&&b)for(var c in b)b.hasOwnProperty(c)&&(a[c]=b[c]);return a}function t(a){return S?S(a).clone(!0)[0]:T&&T.dom?T.dom(a).cloneNode(!0):a.cloneNode(!0)}if("undefined"==typeof window||!window.document)return function(){throw new Error("Sortable.js requires a window with a document")};var u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M={},N=/\s+/g,O="Sortable"+(new Date).getTime(),P=window,Q=P.document,R=P.parseInt,S=P.jQuery||P.Zepto,T=P.Polymer,U=!!("draggable"in Q.createElement("div")),V=function(a){return a=Q.createElement("x"),a.style.cssText="pointer-events:auto","auto"===a.style.pointerEvents}(),W=!1,X=Math.abs,Y=Math.min,Z=([].slice,[]),$=r(function(a,b,c){if(c&&b.scroll){var d,e,f,g,h=b.scrollSensitivity,i=b.scrollSpeed,j=a.clientX,k=a.clientY,l=window.innerWidth,m=window.innerHeight;if(B!==c&&(A=b.scroll,B=c,A===!0)){A=c;do if(A.offsetWidth=l-j)-(h>=j),g=(h>=m-k)-(h>=k),(f||g)&&(d=P)),(M.vx!==f||M.vy!==g||M.el!==d)&&(M.el=d,M.vx=f,M.vy=g,clearInterval(M.pid),d&&(M.pid=setInterval(function(){d===P?P.scrollTo(P.pageXOffset+f*i,P.pageYOffset+g*i):(g&&(d.scrollTop+=g*i),f&&(d.scrollLeft+=f*i))},24)))}},30),_=function(a){function b(a,b){return void 0===a&&(a=c.name),"function"==typeof a?a:function(c,d){var e=d.options.group.name;return b?a:a&&(a.join?a.indexOf(e)>-1:e==a)}}var c={},d=a.group;d&&"object"==typeof d||(d={name:d}),c.name=d.name,c.checkPull=b(d.pull,!0),c.checkPut=b(d.put),a.group=c};return a.prototype={constructor:a,_onTapStart:function(a){var b,d=this,e=this.el,f=this.options,g=a.type,h=a.touches&&a.touches[0],i=(h||a).target,k=i,l=f.filter;if(!u&&!("mousedown"===g&&0!==a.button||f.disabled)&&(i=c(i,f.draggable,e),i&&(!f.handle||c(k,f.handle,e)))){if(b=p(i,f.draggable),"function"==typeof l){if(l.call(this,a,i,this))return j(d,k,"filter",i,e,b),void a.preventDefault()}else if(l&&(l=l.split(",").some(function(a){return a=c(k,a.trim(),e),a?(j(d,a,"filter",i,e,b),!0):void 0})))return void a.preventDefault();this._prepareDragStart(a,h,i,b)}},_prepareDragStart:function(a,b,c,d){var f,h=this,k=h.el,m=h.options,n=k.ownerDocument;c&&!u&&c.parentNode===k&&(J=a,y=k,u=c,v=u.parentNode,z=u.nextSibling,H=m.group,F=d,this._lastX=(b||a).clientX,this._lastY=(b||a).clientY,f=function(){h._disableDelayedDrag(),u.draggable=h.nativeDraggable,g(u,h.options.chosenClass,!0),h._triggerDragStart(b),j(h,y,"choose",u,y,F)},m.ignore.split(",").forEach(function(a){i(u,a.trim(),l)}),e(n,"mouseup",h._onDrop),e(n,"touchend",h._onDrop),e(n,"touchcancel",h._onDrop),m.delay?(e(n,"mouseup",h._disableDelayedDrag),e(n,"touchend",h._disableDelayedDrag),e(n,"touchcancel",h._disableDelayedDrag),e(n,"mousemove",h._disableDelayedDrag),e(n,"touchmove",h._disableDelayedDrag),h._dragStartTimer=setTimeout(f,m.delay)):f())},_disableDelayedDrag:function(){var a=this.el.ownerDocument;clearTimeout(this._dragStartTimer),f(a,"mouseup",this._disableDelayedDrag),f(a,"touchend",this._disableDelayedDrag),f(a,"touchcancel",this._disableDelayedDrag),f(a,"mousemove",this._disableDelayedDrag),f(a,"touchmove",this._disableDelayedDrag)},_triggerDragStart:function(a){a?(J={target:u,clientX:a.clientX,clientY:a.clientY},this._onDragStart(J,"touch")):this.nativeDraggable?(e(u,"dragend",this),e(y,"dragstart",this._onDragStart)):this._onDragStart(J,!0);try{Q.selection?Q.selection.empty():window.getSelection().removeAllRanges()}catch(b){}},_dragStarted:function(){y&&u&&(g(u,this.options.ghostClass,!0),a.active=this,j(this,y,"start",u,y,F))},_emulateDragOver:function(){if(K){if(this._lastX===K.clientX&&this._lastY===K.clientY)return;this._lastX=K.clientX,this._lastY=K.clientY,V||h(w,"display","none");var a=Q.elementFromPoint(K.clientX,K.clientY),b=a,c=(" "+this.options.group.name,Z.length);if(b)do{if(b[O]){for(;c--;)Z[c]({clientX:K.clientX,clientY:K.clientY,target:a,rootEl:b});break}a=b}while(b=b.parentNode);V||h(w,"display","")}},_onTouchMove:function(b){if(J){var c=this.options,d=c.fallbackTolerance,e=b.touches?b.touches[0]:b,f=e.clientX-J.clientX,g=e.clientY-J.clientY,i=b.touches?"translate3d("+f+"px,"+g+"px,0)":"translate("+f+"px,"+g+"px)";if(!a.active){if(d&&Y(X(e.clientX-this._lastX),X(e.clientY-this._lastY))u.offsetWidth,G=e.offsetHeight>u.offsetHeight,J=(B?(d.clientX-s.left)/t:(d.clientY-s.top)/A)>.5,K=e.nextElementSibling,M=k(y,i,u,f,e,s);if(M!==!1){if(W=!0,setTimeout(m,30),b(p),1===M||-1===M)r=1===M;else if(B){var N=u.offsetTop,P=e.offsetTop;r=N===P?e.previousElementSibling===u&&!F||J&&F:e.previousElementSibling===u||u.previousElementSibling===e?(d.clientY-s.top)/A>.5:P>N}else r=K!==u&&!G||J&&G;u.contains(i)||(r&&!K?i.appendChild(u):e.parentNode.insertBefore(u,r?K:e)),v=u.parentNode,this._animate(f,u),this._animate(s,e)}}}},_animate:function(a,b){var c=this.options.animation;if(c){var d=b.getBoundingClientRect();h(b,"transition","none"),h(b,"transform","translate3d("+(a.left-d.left)+"px,"+(a.top-d.top)+"px,0)"),b.offsetWidth,h(b,"transition","all "+c+"ms"),h(b,"transform","translate3d(0,0,0)"),clearTimeout(b.animated),b.animated=setTimeout(function(){h(b,"transition",""),h(b,"transform",""),b.animated=!1},c)}},_offUpEvents:function(){var a=this.el.ownerDocument;f(Q,"touchmove",this._onTouchMove),f(a,"mouseup",this._onDrop),f(a,"touchend",this._onDrop),f(a,"touchcancel",this._onDrop)},_onDrop:function(b){var c=this.el,d=this.options;clearInterval(this._loopId),clearInterval(M.pid),clearTimeout(this._dragStartTimer),f(Q,"mousemove",this._onTouchMove),this.nativeDraggable&&(f(Q,"drop",this),f(c,"dragstart",this._onDragStart)),this._offUpEvents(),b&&(L&&(b.preventDefault(),!d.dropBubble&&b.stopPropagation()),w&&w.parentNode.removeChild(w),u&&(this.nativeDraggable&&f(u,"dragend",this),l(u),g(u,this.options.ghostClass,!1),g(u,this.options.chosenClass,!1),y!==v?(G=p(u,d.draggable),G>=0&&(j(null,v,"sort",u,y,F,G),j(this,y,"sort",u,y,F,G),j(null,v,"add",u,y,F,G),j(this,y,"remove",u,y,F,G))):(x&&x.parentNode.removeChild(x),u.nextSibling!==z&&(G=p(u,d.draggable),G>=0&&(j(this,y,"update",u,y,F,G),j(this,y,"sort",u,y,F,G)))),a.active&&((null===G||-1===G)&&(G=F),j(this,y,"end",u,y,F,G),this.save()))),this._nulling()},_nulling:function(){y=u=v=w=z=x=A=B=J=K=L=G=C=D=I=H=a.active=null},handleEvent:function(a){var b=a.type;"dragover"===b||"dragenter"===b?u&&(this._onDragOver(a),d(a)):("drop"===b||"dragend"===b)&&this._onDrop(a)},toArray:function(){for(var a,b=[],d=this.el.children,e=0,f=d.length,g=this.options;f>e;e++)a=d[e],c(a,g.draggable,this.el)&&b.push(a.getAttribute(g.dataIdAttr)||o(a));return b},sort:function(a){var b={},d=this.el;this.toArray().forEach(function(a,e){var f=d.children[e];c(f,this.options.draggable,d)&&(b[a]=f)},this),a.forEach(function(a){b[a]&&(d.removeChild(b[a]),d.appendChild(b[a]))})},save:function(){var a=this.options.store;a&&a.set(this)},closest:function(a,b){return c(a,b||this.options.draggable,this.el)},option:function(a,b){var c=this.options;return void 0===b?c[a]:(c[a]=b,void("group"===a&&_(c)))},destroy:function(){var a=this.el;a[O]=null,f(a,"mousedown",this._onTapStart),f(a,"touchstart",this._onTapStart),this.nativeDraggable&&(f(a,"dragover",this),f(a,"dragenter",this)),Array.prototype.forEach.call(a.querySelectorAll("[draggable]"),function(a){a.removeAttribute("draggable")}),Z.splice(Z.indexOf(this._onDragOver),1),this._onDrop(),this.el=a=null}},a.utils={on:e,off:f,css:h,find:i,is:function(a,b){return!!c(a,b,a)},extend:s,throttle:r,closest:c,toggleClass:g,clone:t,index:p},a.create=function(b,c){return new a(b,c)},a.version="1.4.2",a}); \ No newline at end of file diff --git a/knockout-sortable.js b/knockout-sortable.js index b2a0daf..2f98896 100644 --- a/knockout-sortable.js +++ b/knockout-sortable.js @@ -1,55 +1,61 @@ +/*global ko*/ + (function (factory) { - "use strict"; - //get ko ref via global or require - var koRef; - if (typeof ko !== 'undefined') { - //global ref already defined - koRef = ko; - } - else if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') { - //commonjs / node.js - koRef = require('knockout'); - } - //get sortable ref via global or require - var sortableRef; - if (typeof Sortable !== 'undefined') { - //global ref already defined - sortableRef = Sortable; - } - else if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') { - //commonjs / node.js - sortableRef = require('sortablejs'); - } - //use references if we found them - if (koRef !== undefined && sortableRef !== undefined) { - factory(koRef, sortableRef); - } - //if both references aren't found yet, get via AMD if available - else if (typeof define === 'function' && define.amd){ - //we may have a reference to only 1, or none - if (koRef !== undefined && sortableRef === undefined) { - define(['./Sortable'], function(amdSortableRef){ factory(koRef, amdSortableRef); }); - } - else if (koRef === undefined && sortableRef !== undefined) { - define(['knockout'], function(amdKnockout){ factory(amdKnockout, sortableRef); }); - } - else if (koRef === undefined && sortableRef === undefined) { - define(['knockout', './Sortable'], factory); - } - } - //no more routes to get references - else { - //report specific error - if (koRef !== undefined && sortableRef === undefined) { - throw new Error('knockout-sortable could not get reference to Sortable'); - } - else if (koRef === undefined && sortableRef !== undefined) { - throw new Error('knockout-sortable could not get reference to Knockout'); - } - else if (koRef === undefined && sortableRef === undefined) { - throw new Error('knockout-sortable could not get reference to Knockout or Sortable'); - } - } + "use strict"; + //get ko ref via global or require + var koRef; + if (typeof ko !== 'undefined') { + //global ref already defined + koRef = ko; + } + else if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') { + //commonjs / node.js + koRef = require('knockout'); + } + //get sortable ref via global or require + var sortableRef; + if (typeof Sortable !== 'undefined') { + //global ref already defined + sortableRef = Sortable; + } + else if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') { + //commonjs / node.js + sortableRef = require('sortablejs'); + } + //use references if we found them + if (koRef !== undefined && sortableRef !== undefined) { + factory(koRef, sortableRef); + } + //if both references aren't found yet, get via AMD if available + else if (typeof define === 'function' && define.amd) { + //we may have a reference to only 1, or none + if (koRef !== undefined && sortableRef === undefined) { + define(['./Sortable'], function (amdSortableRef) { + factory(koRef, amdSortableRef); + }); + } + else if (koRef === undefined && sortableRef !== undefined) { + define(['knockout'], function (amdKnockout) { + factory(amdKnockout, sortableRef); + }); + } + else if (koRef === undefined && sortableRef === undefined) { + define(['knockout', './Sortable'], factory); + } + } + //no more routes to get references + else { + //report specific error + if (koRef !== undefined && sortableRef === undefined) { + throw new Error('knockout-sortable could not get reference to Sortable'); + } + else if (koRef === undefined && sortableRef !== undefined) { + throw new Error('knockout-sortable could not get reference to Knockout'); + } + else if (koRef === undefined && sortableRef === undefined) { + throw new Error('knockout-sortable could not get reference to Knockout or Sortable'); + } + } })(function (ko, Sortable) { "use strict"; @@ -127,7 +133,7 @@ originalIndex = fromArray.indexOf(itemVM), newIndex = e.newIndex; - // We have to find out the actual desired index of the to array, + // We have to find out the actual desired index of the to array, // as this might be a computed array. We could otherwise potentially // drop an item above the 3rd visible item, but the 2nd visible item // has an actual index of 5.