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

fix(sortable): fix incorrect helper returned from getSortingHelper() #515

Merged
merged 4 commits into from
Apr 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-ui-sortable",
"version": "0.17.0",
"version": "0.17.1",
"description": "This directive allows you to jQueryUI Sortable.",
"author": "https://github.com/angular-ui/ui-sortable/graphs/contributors",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-ui-sortable",
"version": "0.17.0",
"version": "0.17.1",
"description": "This directive allows you to jQueryUI Sortable.",
"author": "https://github.com/angular-ui/ui-sortable/graphs/contributors",
"license": "MIT",
Expand Down
10 changes: 6 additions & 4 deletions src/sortable.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ angular.module('ui.sortable', [])
},
link: function(scope, element, attrs, ngModel) {
var savedNodes;
var helper;

function combineCallbacks(first, second){
var firstIsFunc = typeof first === 'function';
Expand Down Expand Up @@ -185,13 +186,12 @@ 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') {
// The .ui-sortable-helper element (that's the default class name)
// is placed last.
result = savedNodes.last();
result = helper;
}
return result;
}
Expand Down Expand Up @@ -331,6 +331,7 @@ angular.module('ui.sortable', [])
// This is inside activate (instead of start) in order to save
// both lists when dragging between connected lists.
savedNodes = savedNodesOrigin.contents();
helper = ui.helper;

// If this list has a placeholder (the connected lists won't),
// don't inlcude it in saved nodes.
Expand Down Expand Up @@ -428,9 +429,10 @@ angular.module('ui.sortable', [])
savedNodes.appendTo(elementContext.savedNodesOrigin);
}

// 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) {
Expand Down
59 changes: 59 additions & 0 deletions test/sortable.e2e.callbacks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,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(
'<ul ui-sortable="opts" ng-model="items">',
beforeLiElement,
'<li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li>',
afterLiElement +
'</ul>'))($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 cancel sorting of node "Two" and "helper: function" that returns a list element is used', function() {
inject(function($compile, $rootScope) {
var element;
Expand Down