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

Commit af8a674

Browse files
Jeremy Mickelsondouglasduteil
Jeremy Mickelson
authored andcommitted
Added support for drag and drop between lists (idea taken from filipkis' comment). #48
1 parent a09c41d commit af8a674

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

src/sortable.js

+32-7
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ angular.module('ui.sortable', [])
5858
};
5959

6060
callbacks.update = function(e, ui) {
61-
// Fetch saved and current position of dropped element
62-
var end, start;
63-
start = ui.item.sortable.index;
64-
end = ui.item.index();
61+
// Save current drop position but only if this is not a second
62+
// update that happens when moving between lists because then
63+
// the value will be overwritten with the old value
64+
if(!ui.item.sortable.relocate){
65+
ui.item.sortable.dropindex = ui.item.index();
66+
}
6567

6668
// Cancel the sort (let ng-repeat do the sort for us)
6769
element.sortable('cancel');
@@ -73,9 +75,32 @@ angular.module('ui.sortable', [])
7375
// comments are still messed up).
7476
savedNodes.detach().appendTo(element);
7577

76-
// Reorder array and apply change to scope
77-
scope.$apply(function() {
78-
ngModel.$modelValue.splice(end, 0, ngModel.$modelValue.splice(start, 1)[0]);
78+
// If relocate is true (an item was dropped in from another list)
79+
// then we add the new item to this list otherwise we move the
80+
// item to it's new location
81+
scope.$apply(function () {
82+
if(ui.item.sortable.relocate) {
83+
ngModel.$modelValue.splice(ui.item.sortable.dropindex, 0,
84+
ui.item.sortable.moved);
85+
} else {
86+
ngModel.$modelValue.splice(
87+
ui.item.sortable.dropindex, 0,
88+
ngModel.$modelValue.splice(ui.item.sortable.index, 1)[0]);
89+
}
90+
});
91+
};
92+
93+
callbacks.receive = function(e, ui) {
94+
// An item was dropped here from another list, set a flag
95+
ui.item.sortable.relocate = true;
96+
};
97+
98+
callbacks.remove = function(e, ui) {
99+
// Remove the item from this list's model and copy data into item,
100+
// so the next list can retrive it
101+
scope.$apply(function () {
102+
ui.item.sortable.moved = ngModel.$modelValue.splice(
103+
ui.item.sortable.index, 1)[0];
79104
});
80105
};
81106

0 commit comments

Comments
 (0)