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

Commit

Permalink
Merge pull request #515 from thgreasi/joujiahe-master
Browse files Browse the repository at this point in the history
fix(sortable): fix incorrect helper returned from getSortingHelper()
  • Loading branch information
thgreasi authored Apr 15, 2017
2 parents 18cc295 + a9d796f commit cfcb636
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
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

0 comments on commit cfcb636

Please sign in to comment.