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

Commit 27569fa

Browse files
committed
Merge pull request #94 from thgreasi/master
Added check to only add items that have not been cancelled. Closing #81
2 parents 555ad80 + 3770a97 commit 27569fa

File tree

2 files changed

+53
-14
lines changed

2 files changed

+53
-14
lines changed

src/sortable.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@ angular.module('ui.sortable', [])
5959

6060
callbacks.receive = function(e, ui) {
6161
ui.item.sortable.relocate = true;
62-
// added item to array into correct position and set up flag
63-
ngModel.$modelValue.splice(ui.item.index(), 0, ui.item.sortable.moved);
62+
// if the item still exists (it has not been cancelled)
63+
if('moved' in ui.item.sortable) {
64+
// added item to array into correct position and set up flag
65+
ngModel.$modelValue.splice(ui.item.index(), 0, ui.item.sortable.moved);
66+
}
6467
};
6568

6669
callbacks.remove = function(e, ui) {

test/sortable.spec.js

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,42 @@ describe('uiSortable', function() {
400400
});
401401
});
402402

403+
it('should update model when sorting a "falsy" item between sortables', function() {
404+
inject(function($compile, $rootScope) {
405+
var elementTop, elementBottom;
406+
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);
407+
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);
408+
$rootScope.$apply(function() {
409+
$rootScope.itemsTop = [0, 'Top Two', 'Top Three'];
410+
$rootScope.itemsBottom = ['Bottom One', 'Bottom Two', 'Bottom Three'];
411+
$rootScope.opts = { connectWith: '.cross-sortable' };
412+
});
413+
414+
host.append(elementTop).append(elementBottom);
415+
416+
var li1 = elementTop.find(':eq(0)');
417+
var li2 = elementBottom.find(':eq(0)');
418+
var dy = EXTRA_DY_PERCENTAGE * li1.outerHeight() + (li2.position().top - li1.position().top);
419+
li1.simulate('drag', { dy: dy });
420+
expect($rootScope.itemsTop).toEqual(['Top Two', 'Top Three']);
421+
expect($rootScope.itemsBottom).toEqual(['Bottom One', 0, 'Bottom Two', 'Bottom Three']);
422+
expect($rootScope.itemsTop).toEqualListContent(elementTop);
423+
expect($rootScope.itemsBottom).toEqualListContent(elementBottom);
424+
425+
li1 = elementBottom.find(':eq(1)');
426+
li2 = elementTop.find(':eq(1)');
427+
dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
428+
li1.simulate('drag', { dy: dy });
429+
expect($rootScope.itemsTop).toEqual(['Top Two', 0, 'Top Three']);
430+
expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Bottom Two', 'Bottom Three']);
431+
expect($rootScope.itemsTop).toEqualListContent(elementTop);
432+
expect($rootScope.itemsBottom).toEqualListContent(elementBottom);
433+
434+
$(elementTop).remove();
435+
$(elementBottom).remove();
436+
});
437+
});
438+
403439
it('should work when "placeholder" option is used', function() {
404440
inject(function($compile, $rootScope) {
405441
var elementTop, elementBottom;
@@ -540,14 +576,14 @@ describe('uiSortable', function() {
540576
// 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);
541577
// 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);
542578
// $rootScope.$apply(function() {
543-
// $rootScope.itemsTop = ["Top One", "Top Two", "Top Three"];
544-
// $rootScope.itemsBottom = ["Bottom One", "Bottom Two", "Bottom Three"];
579+
// $rootScope.itemsTop = ['Top One', 'Top Two', 'Top Three'];
580+
// $rootScope.itemsBottom = ['Bottom One', 'Bottom Two', 'Bottom Three'];
545581
// $rootScope.opts = {
546-
// connectWith: ".cross-sortable",
582+
// connectWith: '.cross-sortable',
547583
// update: function(e, ui) {
548584
// if (ui.item.scope() &&
549585
// (typeof ui.item.scope().item === 'string') &&
550-
// ui.item.scope().item.indexOf("Two") >= 0) {
586+
// ui.item.scope().item.indexOf('Two') >= 0) {
551587
// ui.item.parent().sortable('cancel');
552588
// }
553589
// }
@@ -560,35 +596,35 @@ describe('uiSortable', function() {
560596
// var li2 = elementBottom.find(':eq(0)');
561597
// var dy = EXTRA_DY_PERCENTAGE * li1.outerHeight() + (li2.position().top - li1.position().top);
562598
// li1.simulate('drag', { dy: dy });
563-
// expect($rootScope.itemsTop).toEqual(["Top One", "Top Two", "Top Three"]);
564-
// expect($rootScope.itemsBottom).toEqual(["Bottom One", "Bottom Two", "Bottom Three"]);
599+
// expect($rootScope.itemsTop).toEqual(['Top One', 'Top Two', 'Top Three']);
600+
// expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Bottom Two', 'Bottom Three']);
565601
// expect($rootScope.itemsTop).toEqualListContent(elementTop);
566602
// expect($rootScope.itemsBottom).toEqualListContent(elementBottom);
567603

568604
// li1 = elementBottom.find(':eq(1)');
569605
// li2 = elementTop.find(':eq(1)');
570606
// dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
571607
// li1.simulate('drag', { dy: dy });
572-
// expect($rootScope.itemsTop).toEqual(["Top One", "Top Two", "Top Three"]);
573-
// expect($rootScope.itemsBottom).toEqual(["Bottom One", "Bottom Two", "Bottom Three"]);
608+
// expect($rootScope.itemsTop).toEqual(['Top One', 'Top Two', 'Top Three']);
609+
// expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Bottom Two', 'Bottom Three']);
574610
// expect($rootScope.itemsTop).toEqualListContent(elementTop);
575611
// expect($rootScope.itemsBottom).toEqualListContent(elementBottom);
576612

577613
// li1 = elementTop.find(':eq(0)');
578614
// li2 = elementBottom.find(':eq(0)');
579615
// dy = EXTRA_DY_PERCENTAGE * li1.outerHeight() + (li2.position().top - li1.position().top);
580616
// li1.simulate('drag', { dy: dy });
581-
// expect($rootScope.itemsTop).toEqual(["Top Two", "Top Three"]);
582-
// expect($rootScope.itemsBottom).toEqual(["Bottom One", "Top One", "Bottom Two", "Bottom Three"]);
617+
// expect($rootScope.itemsTop).toEqual(['Top Two', 'Top Three']);
618+
// expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Top One', 'Bottom Two', 'Bottom Three']);
583619
// expect($rootScope.itemsTop).toEqualListContent(elementTop);
584620
// expect($rootScope.itemsBottom).toEqualListContent(elementBottom);
585621

586622
// li1 = elementBottom.find(':eq(1)');
587623
// li2 = elementTop.find(':eq(1)');
588624
// dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
589625
// li1.simulate('drag', { dy: dy });
590-
// expect($rootScope.itemsTop).toEqual(["Top Two", "Top One", "Top Three"]);
591-
// expect($rootScope.itemsBottom).toEqual(["Bottom One", "Bottom Two", "Bottom Three"]);
626+
// expect($rootScope.itemsTop).toEqual(['Top Two', 'Top One', 'Top Three']);
627+
// expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Bottom Two', 'Bottom Three']);
592628
// expect($rootScope.itemsTop).toEqualListContent(elementTop);
593629
// expect($rootScope.itemsBottom).toEqualListContent(elementBottom);
594630

0 commit comments

Comments
 (0)