Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

Commit 6cd1c07

Browse files
committed
fix(sortable): properly restore DOM after cancel()
Resolves #491
1 parent 49b4f9e commit 6cd1c07

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

Diff for: src/sortable.js

+16-15
Original file line numberDiff line numberDiff line change
@@ -369,29 +369,30 @@ angular.module('ui.sortable', [])
369369
// If the received flag hasn't be set on the item, this is a
370370
// normal sort, if dropindex is set, the item was moved, so move
371371
// the items in the list.
372-
if(!ui.item.sortable.received &&
373-
('dropindex' in ui.item.sortable) &&
374-
!ui.item.sortable.isCanceled()) {
372+
var wasMoved = ('dropindex' in ui.item.sortable) &&
373+
!ui.item.sortable.isCanceled();
374+
375+
if (wasMoved && !ui.item.sortable.received) {
375376

376377
scope.$apply(function () {
377378
ngModel.$modelValue.splice(
378379
ui.item.sortable.dropindex, 0,
379380
ngModel.$modelValue.splice(ui.item.sortable.index, 1)[0]);
380381
});
381-
} else {
382-
// if the item was not moved, then restore the elements
382+
} else if (!wasMoved &&
383+
!angular.equals(element.contents().toArray(), savedNodes.toArray())) {
384+
// if the item was not moved
385+
// and the DOM element order has changed,
386+
// then restore the elements
383387
// so that the ngRepeat's comment are correct.
384-
if ((!('dropindex' in ui.item.sortable) || ui.item.sortable.isCanceled()) &&
385-
!angular.equals(element.contents(), savedNodes)) {
386-
387-
var sortingHelper = getSortingHelper(element, ui, savedNodes);
388-
if (sortingHelper && sortingHelper.length) {
389-
// Restore all the savedNodes except from the sorting helper element.
390-
// That way it will be garbage collected.
391-
savedNodes = savedNodes.not(sortingHelper);
392-
}
393-
savedNodes.appendTo(element);
388+
389+
var sortingHelper = getSortingHelper(element, ui, savedNodes);
390+
if (sortingHelper && sortingHelper.length) {
391+
// Restore all the savedNodes except from the sorting helper element.
392+
// That way it will be garbage collected.
393+
savedNodes = savedNodes.not(sortingHelper);
394394
}
395+
savedNodes.appendTo(element);
395396
}
396397

397398
// It's now safe to clear the savedNodes

0 commit comments

Comments
 (0)