Browse Source

Adds swapItem property to end event when swapping

Fixes bug with swapping animation when items are next to each other
Fixes bug when dragged item is the last sibling
pull/1361/head
Ken Cation 6 years ago
parent
commit
4abd04a089
  1. 49
      Sortable.js
  2. 2
      Sortable.min.js

49
Sortable.js

@ -966,12 +966,10 @@
_toggleClass(lastEl, options.swapClass, false); _toggleClass(lastEl, options.swapClass, false);
if (dragEl !== lastEl) { if (dragEl !== lastEl) {
var anchor = dragEl.nextSibling;
var dragRect = _getBoundingClientRect(dragEl); var dragRect = _getBoundingClientRect(dragEl);
var lastRect = _getBoundingClientRect(lastEl); var lastRect = _getBoundingClientRect(lastEl);
_insertBefore(null, dragEl, lastEl) _swapNodes(dragEl, lastEl);
_insertBefore(null, lastEl, anchor);
this._animate(dragRect, dragEl); this._animate(dragRect, dragEl);
this._animate(lastRect, lastEl); this._animate(lastRect, lastEl);
@ -1040,7 +1038,14 @@
newIndex = oldIndex; newIndex = oldIndex;
} }
_dispatchEvent(this, rootEl, 'end', dragEl, parentEl, rootEl, oldIndex, newIndex, evt); if (options.swap && lastEl)
{
_dispatchEvent(this, rootEl, 'end', dragEl, parentEl, rootEl, oldIndex, newIndex, evt, {swapItem:lastEl});
}
else
{
_dispatchEvent(this, rootEl, 'end', dragEl, parentEl, rootEl, oldIndex, newIndex, evt);
}
// Save sorting // Save sorting
this.save(); this.save();
@ -1349,7 +1354,7 @@
function _dispatchEvent(sortable, rootEl, name, targetEl, toEl, fromEl, startIndex, newIndex, originalEvt) { function _dispatchEvent(sortable, rootEl, name, targetEl, toEl, fromEl, startIndex, newIndex, originalEvt, eventOptions) {
sortable = (sortable || rootEl[expando]); sortable = (sortable || rootEl[expando]);
var evt = document.createEvent('Event'), var evt = document.createEvent('Event'),
@ -1368,6 +1373,14 @@
evt.originalEvent = originalEvt; evt.originalEvent = originalEvt;
if (eventOptions)
{
for (var option in eventOptions)
{
evt[option] = eventOptions[option];
}
}
rootEl.dispatchEvent(evt); rootEl.dispatchEvent(evt);
if (options[onName]) { if (options[onName]) {
@ -1561,6 +1574,32 @@
parent.insertBefore(el, ref); parent.insertBefore(el, ref);
} }
function _swapNodes(n1, n2) {
var p1 = n1.parentNode;
var p2 = n2.parentNode;
var i1, i2;
if ( !p1 || !p2 || p1.isEqualNode(n2) || p2.isEqualNode(n1) ) return;
for (var i = 0; i < p1.children.length; i++) {
if (p1.children[i].isEqualNode(n1)) {
i1 = i;
}
}
for (var i = 0; i < p2.children.length; i++) {
if (p2.children[i].isEqualNode(n2)) {
i2 = i;
}
}
if ( p1.isEqualNode(p2) && i1 < i2 ) {
i2++;
}
p1.insertBefore(n2, p1.children[i1]);
p2.insertBefore(n1, p2.children[i2]);
}
// Fixed #973: // Fixed #973:
_on(document, 'touchmove', function (evt) { _on(document, 'touchmove', function (evt) {
if (Sortable.active) { if (Sortable.active) {

2
Sortable.min.js vendored

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save