From a0e3a60995a42c986e0a8ea3ba4a3c93871f6c9c Mon Sep 17 00:00:00 2001 From: Dan Dascalescu Date: Sun, 7 Dec 2014 22:22:03 -0800 Subject: [PATCH] Expose old & new element index on drag&drop Fix #148 --- README.md | 38 ++++++++++++++++++++++++-------------- Sortable.js | 45 +++++++++++++++++++++++++++++++++------------ Sortable.min.js | 4 ++-- bower.json | 2 +- component.json | 2 +- package.json | 2 +- 6 files changed, 62 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 7ce3fb5..9c27680 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ You can use any element for the list and its elements, not just `ul`/`li`. Here ### Options ```js var sortable = new Sortable(el, { - group: "name", // or { name: "..", pull: [true, false, clone], put: [true, false, array] } + group: "name", // or { name: "...", pull: [true, false, clone], put: [true, false, array] } sort: true, // sorting inside list disabled: false, // Disables the sortable if set to true. store: null, // @see Store @@ -52,32 +52,42 @@ var sortable = new Sortable(el, { dataTransfer.setData('Text', dragEl.textContent); }, - onStart: function (/*Event*/evt) { /* dragging started*/ }, - onEnd: function (/*Event*/evt) { /* dragging ended */ }, + // dragging started + onStart: function (/**Event*/evt) { + evt.oldIndex; // element index within parent + }, + + // dragging ended + onEnd: function (/**Event*/evt) { + evt.oldIndex; // element's old index within parent + evt.newIndex; // element's new index within parent + }, // Element is dropped into the list from another list - onAdd: function (/*Event*/evt){ - var itemEl = evt.item; // dragged HTMLElement - itemEl.from; // previous list + onAdd: function (/**Event*/evt) { + var itemEl = evt.item; // dragged HTMLElement + itemEl.from; // previous list + // + indexes from onEnd }, // Changed sorting within list - onUpdate: function (/*Event*/evt){ - var itemEl = evt.item; // dragged HTMLElement + onUpdate: function (/**Event*/evt) { + var itemEl = evt.item; // dragged HTMLElement + // + indexes from onEnd }, // Called by any change to the list (add / update / remove) - onSort: function (/*Event*/evt){ - var itemEl = evt.item; // dragged HTMLElement + onSort: function (/**Event*/evt) { + // same properties as onUpdate }, // Element is removed from the list into another list - onRemove: function (/*Event*/evt){ - var itemEl = evt.item; // dragged HTMLElement + onRemove: function (/**Event*/evt) { + // same properties as onUpdate }, - onFilter: function (/*Event*/evt){ - var itemEl = evt.item; // HTMLElement receiving the `mousedown|tapstart` event. + onFilter: function (/**Event*/evt) { + var itemEl = evt.item; // HTMLElement receiving the `mousedown|tapstart` event. } }); ``` diff --git a/Sortable.js b/Sortable.js index 694ea1f..f4f30d4 100644 --- a/Sortable.js +++ b/Sortable.js @@ -25,6 +25,7 @@ "use strict"; var dragEl, + startIndex, ghostEl, cloneEl, rootEl, @@ -47,12 +48,14 @@ _silent = false, - _dispatchEvent = function (rootEl, name, targetEl, fromEl) { + _dispatchEvent = function (rootEl, name, targetEl, fromEl, startIndex, newIndex) { var evt = document.createEvent('Event'); evt.initEvent(name, true, true); evt.item = targetEl || rootEl; evt.from = fromEl || rootEl; + if (startIndex !== undefined) evt.oldIndex = startIndex; + if (newIndex !== undefined) evt.newIndex = newIndex; rootEl.dispatchEvent(evt); }, @@ -163,6 +166,8 @@ el = this.el, filter = options.filter; + // get the index of the dragged element within its parent + startIndex = _index(target); if (evt.type === 'mousedown' && evt.button !== 0 || options.disabled) { return; // only left button or enabled @@ -245,7 +250,7 @@ } - _dispatchEvent(dragEl, 'start'); + _dispatchEvent(dragEl, 'start', undefined, undefined, startIndex); if (activeGroup.pull == 'clone') { @@ -502,28 +507,31 @@ ghostEl && ghostEl.parentNode.removeChild(ghostEl); if (dragEl) { + // get the index of the dragged element within its parent + var newIndex = _index(dragEl); _disableDraggable(dragEl); _toggleClass(dragEl, this.options.ghostClass, false); if (!rootEl.contains(dragEl)) { - _dispatchEvent(dragEl, 'sort', dragEl, dragEl.parentNode); - _dispatchEvent(rootEl, 'sort', dragEl); + // drag from one list and drop into another + _dispatchEvent(dragEl, 'sort', dragEl, dragEl.parentNode, startIndex, newIndex); + _dispatchEvent(rootEl, 'sort', dragEl, undefined, startIndex, newIndex); // Add event - _dispatchEvent(dragEl, 'add', dragEl, rootEl); + _dispatchEvent(dragEl, 'add', dragEl, rootEl, startIndex, newIndex); // Remove event - _dispatchEvent(rootEl, 'remove', dragEl); + _dispatchEvent(rootEl, 'remove', dragEl, undefined, startIndex, newIndex); } else if (dragEl.nextSibling !== nextEl) { - // Update event - _dispatchEvent(dragEl, 'update'); - _dispatchEvent(dragEl, 'sort'); + // drag & drop within the same list + _dispatchEvent(dragEl, 'update', undefined, undefined, startIndex, newIndex); + _dispatchEvent(dragEl, 'sort', undefined, undefined, startIndex, newIndex); cloneEl && cloneEl.parentNode.removeChild(cloneEl); } - _dispatchEvent(rootEl, 'end'); + _dispatchEvent(rootEl, 'end', undefined, undefined, startIndex, newIndex); } // Set NULL @@ -793,6 +801,18 @@ return sum.toString(36); } + /** + * Returns the index of an element within its parent + * @param el + * @returns {HTMLElement} + */ + function _index(/**HTMLElement*/el) { + var index = 0; + while ((el = el.previousElementSibling)) { + index++; + } + return index; + } // Export utils Sortable.utils = { @@ -803,11 +823,12 @@ bind: _bind, closest: _closest, toggleClass: _toggleClass, - dispatchEvent: _dispatchEvent + dispatchEvent: _dispatchEvent, + index: _index }; - Sortable.version = '0.7.1'; + Sortable.version = '0.7.2'; /** diff --git a/Sortable.min.js b/Sortable.min.js index 599a03d..824d55d 100644 --- a/Sortable.min.js +++ b/Sortable.min.js @@ -1,2 +1,2 @@ -/*! Sortable 0.7.1 - 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,c){this.el=a,this.options=c=c||{};var d={group:Math.random(),sort:!0,store:null,handle:null,draggable:a.children[0]&&a.children[0].nodeName||(/[uo]l/i.test(a.nodeName)?"li":"*"),ghostClass:"sortable-ghost",ignore:"a, img",filter:null,animation:0,setData:function(a,b){a.setData("Text",b.textContent)}};for(var f in d)!(f in c)&&(c[f]=d[f]);c.group.name||(c.group={name:c.group}),["pull","put"].forEach(function(a){a in c.group||(c.group[a]=!0)}),E.forEach(function(d){c[d]=b(this,c[d]||F),e(a,d.substr(2).toLowerCase(),c[d])},this),a[x]=c.group.name;for(var g in this)"_"===g.charAt(0)&&(this[g]=b(this,this[g]));e(a,"mousedown",this._onTapStart),e(a,"touchstart",this._onTapStart),B&&e(a,"selectstart",this._onTapStart),e(a,"dragover",this._onDragOver),e(a,"dragenter",this._onDragOver),H.push(this._onDragOver),c.store&&this.sort(c.store.get(this))}function b(a,b){var c=G.call(arguments,2);return b.bind?b.bind.apply(b,[a].concat(c)):function(){return b.apply(a,c.concat(G.call(arguments)))}}function c(a,b,c){if("*"===b)return a;if(a){c=c||z,b=b.split(".");var d=b.shift().toUpperCase(),e=new RegExp("\\s("+b.join("|")+")\\s","g");do if(!(""!==d&&a.nodeName!=d||b.length&&((" "+a.className+" ").match(e)||[]).length!=b.length))return a;while(a!==c&&(a=a.parentNode))}return null}function d(a){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(/\s+/g," ").replace(" "+b+" ","");a.className=d+(c?" "+b:"")}}function h(a,b,c){var d=a&&a.style;if(d){if(void 0===c)return z.defaultView&&z.defaultView.getComputedStyle?c=z.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){a.draggable=!1}function k(){C=!1}function l(a,b){var c=a.lastElementChild,d=c.getBoundingClientRect();return b.clientY-(d.top+d.height)>5&&c}function m(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)}var n,o,p,q,r,s,t,u,v,w,x="Sortable"+(new Date).getTime(),y=window,z=y.document,A=y.parseInt,B=!!z.createElement("div").dragDrop,C=!1,D=function(a,b,c,d){var e=z.createEvent("Event");e.initEvent(b,!0,!0),e.item=c||a,e.from=d||a,a.dispatchEvent(e)},E="onAdd onUpdate onRemove onStart onEnd onFilter onSort".split(" "),F=function(){},G=[].slice,H=[];return a.prototype={constructor:a,_applyEffects:function(){g(n,this.options.ghostClass,!0)},_onTapStart:function(a){var b=a.touches&&a.touches[0],f=(b||a).target,g=this.options,k=this.el,l=g.filter;if("mousedown"!==a.type||0===a.button){if("function"==typeof l){if(l.call(this,f,this))return D(k,"filter",f),void 0}else if(l&&(l=l.split(",").filter(function(a){return c(f,a.trim(),k)}),l.length))return D(k,"filter",f),void 0;if(g.handle&&(f=c(f,g.handle,k)),f=c(f,g.draggable,k),f&&"selectstart"==a.type&&"A"!=f.tagName&&"IMG"!=f.tagName&&f.dragDrop(),f&&!n&&f.parentNode===k){v=a,q=this.el,n=f,r=n.nextSibling,u=this.options.group,n.draggable=!0,g.ignore.split(",").forEach(function(a){i(f,a.trim(),j)}),b&&(v={target:f,clientX:b.clientX,clientY:b.clientY},this._onDragStart(v,!0),a.preventDefault()),e(z,"mouseup",this._onDrop),e(z,"touchend",this._onDrop),e(z,"touchcancel",this._onDrop),e(this.el,"dragstart",this._onDragStart),e(this.el,"dragend",this._onDrop),e(z,"dragover",d);try{z.selection?z.selection.empty():window.getSelection().removeAllRanges()}catch(m){}D(n,"start"),"clone"==u.pull&&(p=n.cloneNode(!0),h(p,"display","none"),q.insertBefore(p,n))}}},_emulateDragOver:function(){if(w){h(o,"display","none");var a=z.elementFromPoint(w.clientX,w.clientY),b=a,c=this.options.group.name,d=H.length;if(b)do{if(b[x]===c){for(;d--;)H[d]({clientX:w.clientX,clientY:w.clientY,target:a,rootEl:b});break}a=b}while(b=b.parentNode);h(o,"display","")}},_onTouchMove:function(a){if(v){var b=a.touches[0],c=b.clientX-v.clientX,d=b.clientY-v.clientY,e="translate3d("+c+"px,"+d+"px,0)";w=b,h(o,"webkitTransform",e),h(o,"mozTransform",e),h(o,"msTransform",e),h(o,"transform",e),a.preventDefault()}},_onDragStart:function(a,b){var c=a.dataTransfer,d=this.options;if(this._offUpEvents(),b){var f,g=n.getBoundingClientRect(),i=h(n);o=n.cloneNode(!0),h(o,"top",g.top-A(i.marginTop,10)),h(o,"left",g.left-A(i.marginLeft,10)),h(o,"width",g.width),h(o,"height",g.height),h(o,"opacity","0.8"),h(o,"position","fixed"),h(o,"zIndex","100000"),q.appendChild(o),f=o.getBoundingClientRect(),h(o,"width",2*g.width-f.width),h(o,"height",2*g.height-f.height),e(z,"touchmove",this._onTouchMove),e(z,"touchend",this._onDrop),e(z,"touchcancel",this._onDrop),this._loopId=setInterval(this._emulateDragOver,150)}else c.effectAllowed="move",d.setData&&d.setData.call(this,c,n),e(z,"drop",this._onDrop);setTimeout(this._applyEffects)},_onDragOver:function(a){var b,d,e,f=this.el,g=this.options,i=g.group,j=i.put,m=u===i,v=g.sort;if(!C&&(m?v||(e=!q.contains(n)):u.pull&&j&&(u.name===i.name||j.indexOf&&~j.indexOf(u.name)))&&(void 0===a.rootEl||a.rootEl===this.el)){if(b=c(a.target,g.draggable,f),d=n.getBoundingClientRect(),p&&p.state!==m&&(h(p,"display",m?"none":""),!m&&p.state&&q.insertBefore(p,n),p.state=m),e)return p||r?q.insertBefore(n,p||r):v||q.appendChild(n),void 0;if(0===f.children.length||f.children[0]===o||f===a.target&&(b=l(f,a))){if(b){if(b.animated)return;y=b.getBoundingClientRect()}f.appendChild(n),this._animate(d,n),b&&this._animate(y,b)}else if(b&&!b.animated&&b!==n&&void 0!==b.parentNode[x]){s!==b&&(s=b,t=h(b));var w,y=b.getBoundingClientRect(),z=y.right-y.left,A=y.bottom-y.top,B=/left|right|inline/.test(t.cssFloat+t.display),D=b.offsetWidth>n.offsetWidth,E=b.offsetHeight>n.offsetHeight,F=(B?(a.clientX-y.left)/z:(a.clientY-y.top)/A)>.5,G=b.nextElementSibling;C=!0,setTimeout(k,30),w=B?b.previousElementSibling===n&&!D||F&&D:G!==n&&!E||F&&E,w&&!G?f.appendChild(n):b.parentNode.insertBefore(n,w?G:b),this._animate(d,n),this._animate(y,b)}}},_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",""),b.animated=!1},c)}},_offUpEvents:function(){f(z,"mouseup",this._onDrop),f(z,"touchmove",this._onTouchMove),f(z,"touchend",this._onDrop),f(z,"touchcancel",this._onDrop)},_onDrop:function(a){clearInterval(this._loopId),f(z,"drop",this._onDrop),f(z,"dragover",d),f(this.el,"dragend",this._onDrop),f(this.el,"dragstart",this._onDragStart),f(this.el,"selectstart",this._onTapStart),this._offUpEvents(),a&&(a.preventDefault(),a.stopPropagation(),o&&o.parentNode.removeChild(o),n&&(j(n),g(n,this.options.ghostClass,!1),q.contains(n)?n.nextSibling!==r&&(D(n,"update"),D(n,"sort"),p&&p.parentNode.removeChild(p)):(D(n,"sort"),D(q,"sort"),D(n,"add",n,q),D(q,"remove",n)),D(q,"end")),q=n=o=r=p=v=w=s=t=u=null,this.options.store&&this.options.store.set(this))},toArray:function(){for(var a,b=[],d=this.el.children,e=0,f=d.length;f>e;e++)a=d[e],c(a,this.options.draggable,this.el)&&b.push(a.getAttribute("data-id")||m(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]))})},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 0)},destroy:function(){var a=this.el,b=this.options;E.forEach(function(c){f(a,c.substr(2).toLowerCase(),b[c])}),f(a,"mousedown",this._onTapStart),f(a,"touchstart",this._onTapStart),f(a,"selectstart",this._onTapStart),f(a,"dragover",this._onDragOver),f(a,"dragenter",this._onDragOver),Array.prototype.forEach.call(a.querySelectorAll("[draggable]"),function(a){a.removeAttribute("draggable")}),H.splice(H.indexOf(this._onDragOver),1),this._onDrop(),this.el=null}},a.utils={on:e,off:f,css:h,find:i,bind:b,closest:c,toggleClass:g,dispatchEvent:D},a.version="0.7.1",a.create=function(b,c){return new a(b,c)},a}); \ No newline at end of file +/*! Sortable 0.7.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,c){this.el=a,this.options=c=c||{};var d={group:Math.random(),sort:!0,disabled:!1,store:null,handle:null,draggable:a.children[0]&&a.children[0].nodeName||(/[uo]l/i.test(a.nodeName)?"li":"*"),ghostClass:"sortable-ghost",ignore:"a, img",filter:null,animation:0,setData:function(a,b){a.setData("Text",b.textContent)}};for(var f in d)!(f in c)&&(c[f]=d[f]);c.group.name||(c.group={name:c.group}),["pull","put"].forEach(function(a){a in c.group||(c.group[a]=!0)}),G.forEach(function(d){c[d]=b(this,c[d]||H),e(a,d.substr(2).toLowerCase(),c[d])},this),a[z]=c.group.name;for(var g in this)"_"===g.charAt(0)&&(this[g]=b(this,this[g]));e(a,"mousedown",this._onTapStart),e(a,"touchstart",this._onTapStart),D&&e(a,"selectstart",this._onTapStart),e(a,"dragover",this._onDragOver),e(a,"dragenter",this._onDragOver),J.push(this._onDragOver),c.store&&this.sort(c.store.get(this))}function b(a,b){var c=I.call(arguments,2);return b.bind?b.bind.apply(b,[a].concat(c)):function(){return b.apply(a,c.concat(I.call(arguments)))}}function c(a,b,c){if("*"===b)return a;if(a){c=c||B,b=b.split(".");var d=b.shift().toUpperCase(),e=new RegExp("\\s("+b.join("|")+")\\s","g");do if(!(""!==d&&a.nodeName!=d||b.length&&((" "+a.className+" ").match(e)||[]).length!=b.length))return a;while(a!==c&&(a=a.parentNode))}return null}function d(a){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(/\s+/g," ").replace(" "+b+" ","");a.className=d+(c?" "+b:"")}}function h(a,b,c){var d=a&&a.style;if(d){if(void 0===c)return B.defaultView&&B.defaultView.getComputedStyle?c=B.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){a.draggable=!1}function k(){E=!1}function l(a,b){var c=a.lastElementChild,d=c.getBoundingClientRect();return b.clientY-(d.top+d.height)>5&&c}function m(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 n(a){for(var b=0;a=a.previousElementSibling;)b++;return b}var o,p,q,r,s,t,u,v,w,x,y,z="Sortable"+(new Date).getTime(),A=window,B=A.document,C=A.parseInt,D=!!B.createElement("div").dragDrop,E=!1,F=function(a,b,c,d,e,f){var g=B.createEvent("Event");g.initEvent(b,!0,!0),g.item=c||a,g.from=d||a,void 0!==e&&(g.oldIndex=e),void 0!==f&&(g.newIndex=f),a.dispatchEvent(g)},G="onAdd onUpdate onRemove onStart onEnd onFilter onSort".split(" "),H=function(){},I=[].slice,J=[];return a.prototype={constructor:a,_applyEffects:function(){g(o,this.options.ghostClass,!0)},_onTapStart:function(a){var b=a.touches&&a.touches[0],f=(b||a).target,g=this.options,k=this.el,l=g.filter;if(p=n(f),!("mousedown"===a.type&&0!==a.button||g.disabled)){if("function"==typeof l){if(l.call(this,f,this))return void F(k,"filter",f)}else if(l&&(l=l.split(",").filter(function(a){return c(f,a.trim(),k)}),l.length))return void F(k,"filter",f);if(g.handle&&(f=c(f,g.handle,k)),f=c(f,g.draggable,k),f&&"selectstart"==a.type&&"A"!=f.tagName&&"IMG"!=f.tagName&&f.dragDrop(),f&&!o&&f.parentNode===k){x=a,s=this.el,o=f,t=o.nextSibling,w=this.options.group,o.draggable=!0,g.ignore.split(",").forEach(function(a){i(f,a.trim(),j)}),b&&(x={target:f,clientX:b.clientX,clientY:b.clientY},this._onDragStart(x,!0),a.preventDefault()),e(B,"mouseup",this._onDrop),e(B,"touchend",this._onDrop),e(B,"touchcancel",this._onDrop),e(this.el,"dragstart",this._onDragStart),e(this.el,"dragend",this._onDrop),e(B,"dragover",d);try{B.selection?B.selection.empty():window.getSelection().removeAllRanges()}catch(m){}F(o,"start",void 0,void 0,p),"clone"==w.pull&&(r=o.cloneNode(!0),h(r,"display","none"),s.insertBefore(r,o))}}},_emulateDragOver:function(){if(y){h(q,"display","none");var a=B.elementFromPoint(y.clientX,y.clientY),b=a,c=this.options.group.name,d=J.length;if(b)do{if(b[z]===c){for(;d--;)J[d]({clientX:y.clientX,clientY:y.clientY,target:a,rootEl:b});break}a=b}while(b=b.parentNode);h(q,"display","")}},_onTouchMove:function(a){if(x){var b=a.touches[0],c=b.clientX-x.clientX,d=b.clientY-x.clientY,e="translate3d("+c+"px,"+d+"px,0)";y=b,h(q,"webkitTransform",e),h(q,"mozTransform",e),h(q,"msTransform",e),h(q,"transform",e),a.preventDefault()}},_onDragStart:function(a,b){var c=a.dataTransfer,d=this.options;if(this._offUpEvents(),b){var f,g=o.getBoundingClientRect(),i=h(o);q=o.cloneNode(!0),h(q,"top",g.top-C(i.marginTop,10)),h(q,"left",g.left-C(i.marginLeft,10)),h(q,"width",g.width),h(q,"height",g.height),h(q,"opacity","0.8"),h(q,"position","fixed"),h(q,"zIndex","100000"),s.appendChild(q),f=q.getBoundingClientRect(),h(q,"width",2*g.width-f.width),h(q,"height",2*g.height-f.height),e(B,"touchmove",this._onTouchMove),e(B,"touchend",this._onDrop),e(B,"touchcancel",this._onDrop),this._loopId=setInterval(this._emulateDragOver,150)}else c.effectAllowed="move",d.setData&&d.setData.call(this,c,o),e(B,"drop",this._onDrop);setTimeout(this._applyEffects)},_onDragOver:function(a){var b,d,e,f=this.el,g=this.options,i=g.group,j=i.put,m=w===i,n=g.sort;if(!E&&(m?n||(e=!s.contains(o)):w.pull&&j&&(w.name===i.name||j.indexOf&&~j.indexOf(w.name)))&&(void 0===a.rootEl||a.rootEl===this.el)){if(b=c(a.target,g.draggable,f),d=o.getBoundingClientRect(),r&&r.state!==m&&(h(r,"display",m?"none":""),!m&&r.state&&s.insertBefore(r,o),r.state=m),e)return void(r||t?s.insertBefore(o,r||t):n||s.appendChild(o));if(0===f.children.length||f.children[0]===q||f===a.target&&(b=l(f,a))){if(b){if(b.animated)return;x=b.getBoundingClientRect()}f.appendChild(o),this._animate(d,o),b&&this._animate(x,b)}else if(b&&!b.animated&&b!==o&&void 0!==b.parentNode[z]){u!==b&&(u=b,v=h(b));var p,x=b.getBoundingClientRect(),y=x.right-x.left,A=x.bottom-x.top,B=/left|right|inline/.test(v.cssFloat+v.display),C=b.offsetWidth>o.offsetWidth,D=b.offsetHeight>o.offsetHeight,F=(B?(a.clientX-x.left)/y:(a.clientY-x.top)/A)>.5,G=b.nextElementSibling;E=!0,setTimeout(k,30),p=B?b.previousElementSibling===o&&!C||F&&C:G!==o&&!D||F&&D,p&&!G?f.appendChild(o):b.parentNode.insertBefore(o,p?G:b),this._animate(d,o),this._animate(x,b)}}},_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",""),b.animated=!1},c)}},_offUpEvents:function(){f(B,"mouseup",this._onDrop),f(B,"touchmove",this._onTouchMove),f(B,"touchend",this._onDrop),f(B,"touchcancel",this._onDrop)},_onDrop:function(a){if(clearInterval(this._loopId),f(B,"drop",this._onDrop),f(B,"dragover",d),f(this.el,"dragend",this._onDrop),f(this.el,"dragstart",this._onDragStart),f(this.el,"selectstart",this._onTapStart),this._offUpEvents(),a){if(a.preventDefault(),a.stopPropagation(),q&&q.parentNode.removeChild(q),o){var b=n(o);j(o),g(o,this.options.ghostClass,!1),s.contains(o)?o.nextSibling!==t&&(F(o,"update",void 0,void 0,p,b),F(o,"sort",void 0,void 0,p,b),r&&r.parentNode.removeChild(r)):(F(o,"sort",o,o.parentNode,p,b),F(s,"sort",o,void 0,p,b),F(o,"add",o,s,p,b),F(s,"remove",o,void 0,p,b)),F(s,"end",void 0,void 0,p,b)}s=o=q=t=r=x=y=u=v=w=null,this.options.store&&this.options.store.set(this)}},toArray:function(){for(var a,b=[],d=this.el.children,e=0,f=d.length;f>e;e++)a=d[e],c(a,this.options.draggable,this.el)&&b.push(a.getAttribute("data-id")||m(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]))})},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]:void(c[a]=b)},destroy:function(){var a=this.el,b=this.options;G.forEach(function(c){f(a,c.substr(2).toLowerCase(),b[c])}),f(a,"mousedown",this._onTapStart),f(a,"touchstart",this._onTapStart),f(a,"selectstart",this._onTapStart),f(a,"dragover",this._onDragOver),f(a,"dragenter",this._onDragOver),Array.prototype.forEach.call(a.querySelectorAll("[draggable]"),function(a){a.removeAttribute("draggable")}),J.splice(J.indexOf(this._onDragOver),1),this._onDrop(),this.el=null}},a.utils={on:e,off:f,css:h,find:i,bind:b,closest:c,toggleClass:g,dispatchEvent:F,index:n},a.version="0.7.2",a.create=function(b,c){return new a(b,c)},a}); \ No newline at end of file diff --git a/bower.json b/bower.json index 7a804c4..a986404 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "Sortable", "main": "Sortable.js", - "version": "0.7.1", + "version": "0.7.2", "homepage": "http://rubaxa.github.io/Sortable/", "authors": [ "RubaXa " diff --git a/component.json b/component.json index 2b839cd..f9a9214 100644 --- a/component.json +++ b/component.json @@ -1,7 +1,7 @@ { "name": "Sortable", "main": "Sortable.js", - "version": "0.7.1", + "version": "0.7.2", "homepage": "http://rubaxa.github.io/Sortable/", "repo": "RubaXa/Sortable", "authors": [ diff --git a/package.json b/package.json index 0e56104..3f5bb55 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "sortablejs", "exportName": "Sortable", - "version": "0.7.1", + "version": "0.7.2", "devDependencies": { "grunt": "*", "grunt-version": "*",