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

Commit abc3f93

Browse files
committed
Progress on cancelling sort using separate lists #30.
1 parent 8c84fed commit abc3f93

File tree

2 files changed

+67
-9
lines changed

2 files changed

+67
-9
lines changed

src/sortable.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ angular.module('ui.sortable', [])
108108
// then we add the new item to this list otherwise wait until the
109109
// stop event where we will know if it was a sort or item was
110110
// moved here from another list
111-
if(ui.item.sortable.received) {
111+
if(ui.item.sortable.received && !ui.item.sortable.isCanceled()) {
112112
scope.$apply(function () {
113113
ngModel.$modelValue.splice(ui.item.sortable.dropindex, 0,
114114
ui.item.sortable.moved);
@@ -138,10 +138,12 @@ angular.module('ui.sortable', [])
138138
callbacks.remove = function(e, ui) {
139139
// Remove the item from this list's model and copy data into item,
140140
// so the next list can retrive it
141-
scope.$apply(function () {
142-
ui.item.sortable.moved = ngModel.$modelValue.splice(
143-
ui.item.sortable.index, 1)[0];
144-
});
141+
if (!ui.item.sortable.isCanceled()) {
142+
scope.$apply(function () {
143+
ui.item.sortable.moved = ngModel.$modelValue.splice(
144+
ui.item.sortable.index, 1)[0];
145+
});
146+
}
145147
};
146148

147149
scope.$watch(attrs.uiSortable, function(newVal, oldVal) {

test/sortable.spec.js

+60-4
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,11 @@ describe('uiSortable', function() {
9898
li.simulate('drag', { dy: dy });
9999
expect($rootScope.items.map(function(x){ return x.text; })).toEqual(["One", "Three", "Four", "Two"]);
100100

101-
// fails on angular 1.2
102101
li = element.find(':eq(2)');
103102
dy = -(2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
104103
li.simulate('drag', { dy: dy });
105104
expect($rootScope.items.map(function(x){ return x.text; })).toEqual(["Four", "One", "Three", "Two"]);
106105

107-
// fails on angular 1.2
108106
li = element.find(':eq(3)');
109107
dy = -(2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
110108
li.simulate('drag', { dy: dy });
@@ -129,15 +127,13 @@ describe('uiSortable', function() {
129127

130128
host.append(elementTop).append(elementBottom);
131129

132-
// fails on angular 1.2
133130
var li1 = elementTop.find(':eq(0)');
134131
var li2 = elementBottom.find(':eq(0)');
135132
var dy = EXTRA_DY_PERCENTAGE * li1.outerHeight() + (li2.position().top - li1.position().top);
136133
li1.simulate('drag', { dy: dy });
137134
expect($rootScope.itemsTop).toEqual(["Top Two", "Top Three"]);
138135
expect($rootScope.itemsBottom).toEqual(["Bottom One", "Top One", "Bottom Two", "Bottom Three"]);
139136

140-
// fails on angular 1.2
141137
li1 = elementBottom.find(':eq(1)');
142138
li2 = elementTop.find(':eq(1)');
143139
dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
@@ -194,10 +190,70 @@ describe('uiSortable', function() {
194190
li.simulate('drag', { dy: dy });
195191
expect($rootScope.items).toEqual(["Two", "Three", "One"]);
196192

193+
li = element.find(':eq(2)');
194+
dy = -(2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
195+
li.simulate('drag', { dy: dy });
196+
expect($rootScope.items).toEqual(["One", "Two", "Three"]);
197+
197198
$(element).remove();
198199
});
199200
});
200201

202+
it('should cancel sorting of nodes that contain "Two"', function() {
203+
inject(function($compile, $rootScope) {
204+
var elementTop, elementBottom;
205+
elementTop = $compile('<ul ui-sortable="opts" class="cross-sortable" ng-model="itemsTop"><li ng-repeat="item in itemsTop" id="s-top-{{$index}}">{{ item }}</li></ul>')($rootScope);
206+
elementBottom = $compile('<ul ui-sortable="opts" class="cross-sortable" ng-model="itemsBottom"><li ng-repeat="item in itemsBottom" id="s-bottom-{{$index}}">{{ item }}</li></ul>')($rootScope);
207+
$rootScope.$apply(function() {
208+
$rootScope.itemsTop = ["Top One", "Top Two", "Top Three"];
209+
$rootScope.itemsBottom = ["Bottom One", "Bottom Two", "Bottom Three"];
210+
$rootScope.opts = {
211+
connectWith: ".cross-sortable",
212+
update: function(e, ui) {
213+
if (ui.item.scope() &&
214+
(typeof ui.item.scope().item === 'string') &&
215+
ui.item.scope().item.indexOf("Two") >= 0) {
216+
ui.item.sortable.cancel();
217+
}
218+
}
219+
};
220+
});
221+
222+
host.append(elementTop).append(elementBottom);
223+
224+
var li1 = elementTop.find(':eq(1)');
225+
var li2 = elementBottom.find(':eq(0)');
226+
var dy = EXTRA_DY_PERCENTAGE * li1.outerHeight() + (li2.position().top - li1.position().top);
227+
li1.simulate('drag', { dy: dy });
228+
expect($rootScope.itemsTop).toEqual(["Top One", "Top Two", "Top Three"]);
229+
expect($rootScope.itemsBottom).toEqual(["Bottom One", "Bottom Two", "Bottom Three"]);
230+
231+
li1 = elementBottom.find(':eq(1)');
232+
li2 = elementTop.find(':eq(1)');
233+
dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
234+
li1.simulate('drag', { dy: dy });
235+
expect($rootScope.itemsTop).toEqual(["Top One", "Top Two", "Top Three"]);
236+
expect($rootScope.itemsBottom).toEqual(["Bottom One", "Bottom Two", "Bottom Three"]);
237+
238+
li1 = elementTop.find(':eq(0)');
239+
li2 = elementBottom.find(':eq(0)');
240+
dy = EXTRA_DY_PERCENTAGE * li1.outerHeight() + (li2.position().top - li1.position().top);
241+
li1.simulate('drag', { dy: dy });
242+
expect($rootScope.itemsTop).toEqual(["Top Two", "Top Three"]);
243+
expect($rootScope.itemsBottom).toEqual(["Bottom One", "Top One", "Bottom Two", "Bottom Three"]);
244+
245+
li1 = elementBottom.find(':eq(1)');
246+
li2 = elementTop.find(':eq(1)');
247+
dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
248+
li1.simulate('drag', { dy: dy });
249+
expect($rootScope.itemsTop).toEqual(["Top Two", "Top One", "Top Three"]);
250+
expect($rootScope.itemsBottom).toEqual(["Bottom One", "Bottom Two", "Bottom Three"]);
251+
252+
$(elementTop).remove();
253+
$(elementBottom).remove();
254+
});
255+
});
256+
201257
it('should update model from update() callback', function() {
202258
inject(function($compile, $rootScope) {
203259
var element, logsElement;

0 commit comments

Comments
 (0)