Skip to content

Commit

Permalink
When drage, Swap Elements instead of moving them
Browse files Browse the repository at this point in the history
This fixes this problem: SortableJS#869
  • Loading branch information
Horray authored Jun 24, 2016
1 parent 6c93d48 commit bd91176
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions Sortable.js
Original file line number Diff line number Diff line change
Expand Up @@ -683,11 +683,31 @@
}

if (!dragEl.contains(el)) {
if (after && !nextSibling) {
el.appendChild(dragEl);
} else {
target.parentNode.insertBefore(dragEl, after ? nextSibling : target);
}
swapElements(dragEl, target);

function swapElements( /** DOM */ obj1, /** DOM */ obj2, /** DOM */ obj1Parent, /** DOM */ obj2Parent) {
// save the location of obj2
var parent1 = obj1Parent || obj1.parentNode;
var parent2 = obj2Parent || obj2.parentNode;
var next2 = obj2.nextSibling;
// special case for obj1 is the next sibling of obj2
if (next2 === obj1) {
// just put obj1 before obj2
parent2.insertBefore(obj1, obj2);
} else {
// insert obj2 right before obj1
parent1.insertBefore(obj2, obj1);

// now insert obj1 where obj2 was
if (next2) {
// if there was an element after obj2, then insert obj1 right before that
parent2.insertBefore(obj1, next2);
} else {
// otherwise, just append as last child
parent2.appendChild(obj1);
}
}
}
}

parentEl = dragEl.parentNode; // actualization
Expand Down

0 comments on commit bd91176

Please sign in to comment.