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

Commit 8c84fed

Browse files
committed
Provide a way to cancel a sorting.
1 parent 121728b commit 8c84fed

File tree

3 files changed

+42
-33
lines changed

3 files changed

+42
-33
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Inside the `update` callback, you can check the item that is dragged and cancel
6161
$scope.sortableOptions = {
6262
update: function(e, ui) {
6363
if (ui.item.scope().item == "can't be moved") {
64-
ui.item.parent().sortable('cancel');
64+
ui.item.sortable.cancel();
6565
}
6666
}
6767
};

src/sortable.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,16 @@ angular.module('ui.sortable', [])
4848

4949
callbacks.start = function(e, ui) {
5050
// Save the starting position of dragged item
51-
ui.item.sortable = { index: ui.item.index() };
51+
ui.item.sortable = {
52+
index: ui.item.index(),
53+
cancel: function () {
54+
ui.item.sortable._isCanceled = true;
55+
},
56+
isCanceled: function () {
57+
return ui.item.sortable._isCanceled;
58+
},
59+
_isCanceled: false
60+
};
5261
};
5362

5463
callbacks.activate = function(e, ui) {
@@ -111,7 +120,7 @@ angular.module('ui.sortable', [])
111120
// If the received flag hasn't be set on the item, this is a
112121
// normal sort, if dropindex is set, the item was moved, so move
113122
// the items in the list.
114-
if(!ui.item.sortable.received && ('dropindex' in ui.item.sortable)) {
123+
if(!ui.item.sortable.received && ('dropindex' in ui.item.sortable) && !ui.item.sortable.isCanceled()) {
115124
scope.$apply(function () {
116125
ngModel.$modelValue.splice(
117126
ui.item.sortable.dropindex, 0,

test/sortable.spec.js

+30-30
Original file line numberDiff line numberDiff line change
@@ -167,36 +167,36 @@ describe('uiSortable', function() {
167167
host = null;
168168
});
169169

170-
// it('should cancel sorting of node "Two"', function() {
171-
// inject(function($compile, $rootScope) {
172-
// var element;
173-
// element = $compile('<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>')($rootScope);
174-
// $rootScope.$apply(function() {
175-
// $rootScope.opts = {
176-
// update: function(e, ui) {
177-
// if (ui.item.scope().item === "Two") {
178-
// ui.item.parent().sortable('cancel');
179-
// }
180-
// }
181-
// };
182-
// $rootScope.items = ["One", "Two", "Three"];
183-
// });
184-
185-
// host.append(element);
186-
187-
// var li = element.find(':eq(1)');
188-
// var dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
189-
// li.simulate('drag', { dy: dy });
190-
// expect($rootScope.items).toEqual(["One", "Two", "Three"]);
191-
192-
// li = element.find(':eq(0)');
193-
// dy = (2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
194-
// li.simulate('drag', { dy: dy });
195-
// expect($rootScope.items).toEqual(["Two", "Three", "One"]);
196-
197-
// $(element).remove();
198-
// });
199-
// });
170+
it('should cancel sorting of node "Two"', function() {
171+
inject(function($compile, $rootScope) {
172+
var element;
173+
element = $compile('<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>')($rootScope);
174+
$rootScope.$apply(function() {
175+
$rootScope.opts = {
176+
update: function(e, ui) {
177+
if (ui.item.scope().item === "Two") {
178+
ui.item.sortable.cancel();
179+
}
180+
}
181+
};
182+
$rootScope.items = ["One", "Two", "Three"];
183+
});
184+
185+
host.append(element);
186+
187+
var li = element.find(':eq(1)');
188+
var dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
189+
li.simulate('drag', { dy: dy });
190+
expect($rootScope.items).toEqual(["One", "Two", "Three"]);
191+
192+
li = element.find(':eq(0)');
193+
dy = (2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
194+
li.simulate('drag', { dy: dy });
195+
expect($rootScope.items).toEqual(["Two", "Three", "One"]);
196+
197+
$(element).remove();
198+
});
199+
});
200200

201201
it('should update model from update() callback', function() {
202202
inject(function($compile, $rootScope) {

0 commit comments

Comments
 (0)