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

Commit 10e5866

Browse files
committed
feat(sortable): add workaround for nested sortings not triggering update
1 parent e29492b commit 10e5866

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/sortable.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,14 @@ angular.module('ui.sortable', [])
174174
};
175175

176176
callbacks.remove = function(e, ui) {
177+
// Workaround for a problem observed in nested connected lists.
178+
// There should be an 'update' event before 'remove' when moving
179+
// elements. If the event did not fire, cancel sorting.
180+
if (!('dropindex' in ui.item.sortable)) {
181+
element.sortable('cancel');
182+
ui.item.sortable.cancel();
183+
}
184+
177185
// Remove the item from this list's model and copy data into item,
178186
// so the next list can retrive it
179187
if (!ui.item.sortable.isCanceled()) {

test/sortable.e2e.multi.spec.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -349,14 +349,14 @@ describe('uiSortable', function() {
349349

350350
it('should update model when sorting between nested sortables', function() {
351351
inject(function($compile, $rootScope) {
352-
var elementTree;
352+
var elementTree, li1, li2, dy;
353353

354354
elementTree = $compile(''.concat(
355355
'<ul ui-sortable="sortableOptions" ng-model="items" class="apps-container outterList" style="float: left;margin-left: 10px;padding-bottom: 10px;">',
356356
'<li ng-repeat="item in items track by $index">',
357357
'<div>',
358358
'<span class="itemContent lvl1ItemContent">{{item.text}}</span>',
359-
'<ul ui-sortable="sortableOptions" ng-model="item.items" class="apps-container innerList" style="float: left;margin-left: 10px;padding-bottom: 10px;">',
359+
'<ul ui-sortable="sortableOptions" ng-model="item.items" class="apps-container innerList" style="margin-left: 10px;padding-bottom: 10px;">',
360360
'<li ng-repeat="i in item.items track by $index">',
361361
'<span class="itemContent lvl2ItemContent">{{i.text}}</span>',
362362
'</li>',
@@ -375,8 +375,8 @@ describe('uiSortable', function() {
375375
{
376376
text: 'Item 2',
377377
items: [
378-
{ text: 'Item 2.1' },
379-
{ text: 'Item 2.2' }
378+
{ text: 'Item 2.1', items: [] },
379+
{ text: 'Item 2.2', items: [] }
380380
]
381381
}
382382
];
@@ -388,8 +388,10 @@ describe('uiSortable', function() {
388388

389389
host.append(elementTree);
390390

391-
var li = elementTree.find('.innerList:last').find(':last');
392-
li.simulate('drag', { dx: -200, moves: 30 });
391+
li1 = elementTree.find('.innerList:last').find(':last');
392+
// this should drag the item out of the list and
393+
// the item should return back to its original position
394+
li1.simulate('drag', { dx: -200, moves: 30 });
393395
expect($rootScope.items.map(function(x){ return x.text; }))
394396
.toEqual(['Item 1', 'Item 2']);
395397
expect($rootScope.items.map(function(x){ return x.text; }))
@@ -403,6 +405,27 @@ describe('uiSortable', function() {
403405
expect($rootScope.items[1].items.map(function(x){ return x.text; }))
404406
.toEqual(listInnerContent(elementTree.find('.innerList:eq(1)'), '.lvl2ItemContent'));
405407

408+
li1 = elementTree.find('.innerList:last').find(':last');
409+
li2 = elementTree.find('> li:last');
410+
dy = EXTRA_DY_PERCENTAGE * li1.outerHeight() + (li2.position().top - li1.position().top);
411+
li1.simulate('drag', { dy: dy });
412+
expect($rootScope.items.map(function(x){ return x.text; }))
413+
.toEqual(['Item 1', 'Item 2.2', 'Item 2']);
414+
expect($rootScope.items.map(function(x){ return x.text; }))
415+
.toEqual(listInnerContent(elementTree, '.lvl1ItemContent'));
416+
expect($rootScope.items[0].items.map(function(x){ return x.text; }))
417+
.toEqual([]);
418+
expect($rootScope.items[0].items.map(function(x){ return x.text; }))
419+
.toEqual(listInnerContent(elementTree.find('.innerList:eq(0)'), '.lvl2ItemContent'));
420+
expect($rootScope.items[1].items.map(function(x){ return x.text; }))
421+
.toEqual([]);
422+
expect($rootScope.items[1].items.map(function(x){ return x.text; }))
423+
.toEqual(listInnerContent(elementTree.find('.innerList:eq(1)'), '.lvl2ItemContent'));
424+
expect($rootScope.items[2].items.map(function(x){ return x.text; }))
425+
.toEqual(['Item 2.1']);
426+
expect($rootScope.items[2].items.map(function(x){ return x.text; }))
427+
.toEqual(listInnerContent(elementTree.find('.innerList:eq(2)'), '.lvl2ItemContent'));
428+
406429
$(elementTree).remove();
407430
});
408431
});

0 commit comments

Comments
 (0)