From 2094a9d70bf9a99df59182218296692d72a99cf8 Mon Sep 17 00:00:00 2001 From: Johnson Chou Date: Sat, 15 Apr 2017 00:56:29 +0800 Subject: [PATCH 1/4] fix(sortable): fix incorrect helper returned from getSortingHelper(). The helper element is not always in the last position while created from a function and append it to another DOM. E.g. the following will mess up the DOM position after canceled update. ``` var sortableOption = { helper: function (evt, ui) { return ui.clone().appendTo('body'); } }; ``` --- src/sortable.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/sortable.js b/src/sortable.js index 7c799d1..f0eeda4 100644 --- a/src/sortable.js +++ b/src/sortable.js @@ -21,6 +21,7 @@ angular.module('ui.sortable', []) }, link: function(scope, element, attrs, ngModel) { var savedNodes; + var helper; function combineCallbacks(first, second){ var firstIsFunc = typeof first === 'function'; @@ -175,8 +176,7 @@ angular.module('ui.sortable', []) if (hasSortingHelper(element, ui) && element.sortable( 'option', 'appendTo' ) === 'parent') { // The .ui-sortable-helper element (that's the default class name) - // is placed last. - result = savedNodes.last(); + result = helper; } return result; } @@ -293,6 +293,7 @@ angular.module('ui.sortable', []) // This is inside activate (instead of start) in order to save // both lists when dragging between connected lists. savedNodes = element.contents(); + helper = ui.helper; // If this list has a placeholder (the connected lists won't), // don't inlcude it in saved nodes. @@ -394,9 +395,10 @@ angular.module('ui.sortable', []) } } - // It's now safe to clear the savedNodes + // It's now safe to clear the savedNodes and helper // since stop is the last callback. savedNodes = null; + helper = null; }; callbacks.receive = function(e, ui) { From aeafeed1c211bf3ae89b4d3e6e4cd028a4c870b7 Mon Sep 17 00:00:00 2001 From: Thodoris Greasidis Date: Sat, 15 Apr 2017 20:43:18 +0300 Subject: [PATCH 2/4] fix(sortable): fix lint error --- src/sortable.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sortable.js b/src/sortable.js index f0eeda4..563e981 100644 --- a/src/sortable.js +++ b/src/sortable.js @@ -171,7 +171,7 @@ angular.module('ui.sortable', []) return helperOption === 'clone' || (typeof helperOption === 'function' && ui.item.sortable.isCustomHelperUsed()); } - function getSortingHelper (element, ui, savedNodes) { + function getSortingHelper (element, ui/*, savedNodes*/) { var result = null; if (hasSortingHelper(element, ui) && element.sortable( 'option', 'appendTo' ) === 'parent') { From 4b58d3fa272cc93b9e533bd220fe95e1bc0a8df5 Mon Sep 17 00:00:00 2001 From: Thodoris Greasidis Date: Sat, 15 Apr 2017 20:46:21 +0300 Subject: [PATCH 3/4] test(e2e.callbacks.spec): add test for cloned helper appended to body --- test/sortable.e2e.callbacks.spec.js | 59 +++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/test/sortable.e2e.callbacks.spec.js b/test/sortable.e2e.callbacks.spec.js index 9057388..f96c1e8 100644 --- a/test/sortable.e2e.callbacks.spec.js +++ b/test/sortable.e2e.callbacks.spec.js @@ -515,6 +515,65 @@ describe('uiSortable', function() { }); }); + it('should cancel sorting of node "Two" when then helper is appended to the `body`', function() { + inject(function($compile, $rootScope) { + var element; + element = $compile(''.concat( + '
    ', + beforeLiElement, + '
  • {{ item }}
  • ', + afterLiElement + + '
'))($rootScope); + $rootScope.$apply(function() { + $rootScope.opts = { + helper: function (e, item) { + return item.clone().appendTo('body'); + }, + update: function(e, ui) { + if (ui.item.sortable.model === 'Two') { + ui.item.sortable.cancel(); + } + } + }; + $rootScope.items = ['One', 'Two', 'Three']; + }); + + host.append(element); + + var li = element.find('[ng-repeat]:eq(1)'); + var dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight(); + li.simulate('drag', { dy: dy }); + expect($rootScope.items).toEqual(['One', 'Two', 'Three']); + expect($rootScope.items).toEqual(listContent(element)); + // try again + li = element.find('[ng-repeat]:eq(1)'); + dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight(); + li.simulate('drag', { dy: dy }); + expect($rootScope.items).toEqual(['One', 'Two', 'Three']); + expect($rootScope.items).toEqual(listContent(element)); + // try again + li = element.find('[ng-repeat]:eq(1)'); + dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight(); + li.simulate('drag', { dy: dy }); + expect($rootScope.items).toEqual(['One', 'Two', 'Three']); + expect($rootScope.items).toEqual(listContent(element)); + + li = element.find('[ng-repeat]:eq(0)'); + dy = (2 + EXTRA_DY_PERCENTAGE) * li.outerHeight(); + li.simulate('drag', { dy: dy }); + expect($rootScope.items).toEqual(['Two', 'Three', 'One']); + expect($rootScope.items).toEqual(listContent(element)); + + li = element.find('[ng-repeat]:eq(2)'); + dy = -(2 + EXTRA_DY_PERCENTAGE) * li.outerHeight(); + li.simulate('drag', { dy: dy }); + expect($rootScope.items).toEqual(['One', 'Two', 'Three']); + expect($rootScope.items).toEqual(listContent(element)); + + $(element).remove(); + }); + }); + it('should properly reset a deleted callback option', function() { inject(function($compile, $rootScope) { var element, logsElement; From b550451b5463cf7a2783814e6883759c7e7ec1d5 Mon Sep 17 00:00:00 2001 From: Thodoris Greasidis Date: Sat, 15 Apr 2017 20:46:48 +0300 Subject: [PATCH 4/4] chore: increase version to 0.14.4 --- bower.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bower.json b/bower.json index 291eb20..c14987f 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "angular-ui-sortable", - "version": "0.14.3", + "version": "0.14.4", "description": "This directive allows you to jQueryUI Sortable.", "author": "https://github.com/angular-ui/ui-sortable/graphs/contributors", "license": "MIT", diff --git a/package.json b/package.json index 313270e..801e3d8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "angular-ui-sortable", - "version": "0.14.3", + "version": "0.14.4", "description": "This directive allows you to jQueryUI Sortable.", "author": "https://github.com/angular-ui/ui-sortable/graphs/contributors", "license": "MIT",