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

Commit cfcb636

Browse files
authored
Merge pull request #515 from thgreasi/joujiahe-master
fix(sortable): fix incorrect helper returned from getSortingHelper()
2 parents 18cc295 + a9d796f commit cfcb636

File tree

4 files changed

+67
-6
lines changed

4 files changed

+67
-6
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-ui-sortable",
3-
"version": "0.17.0",
3+
"version": "0.17.1",
44
"description": "This directive allows you to jQueryUI Sortable.",
55
"author": "https://github.com/angular-ui/ui-sortable/graphs/contributors",
66
"license": "MIT",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-ui-sortable",
3-
"version": "0.17.0",
3+
"version": "0.17.1",
44
"description": "This directive allows you to jQueryUI Sortable.",
55
"author": "https://github.com/angular-ui/ui-sortable/graphs/contributors",
66
"license": "MIT",

src/sortable.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ angular.module('ui.sortable', [])
3636
},
3737
link: function(scope, element, attrs, ngModel) {
3838
var savedNodes;
39+
var helper;
3940

4041
function combineCallbacks(first, second){
4142
var firstIsFunc = typeof first === 'function';
@@ -185,13 +186,12 @@ angular.module('ui.sortable', [])
185186
return helperOption === 'clone' || (typeof helperOption === 'function' && ui.item.sortable.isCustomHelperUsed());
186187
}
187188

188-
function getSortingHelper (element, ui, savedNodes) {
189+
function getSortingHelper (element, ui/*, savedNodes*/) {
189190
var result = null;
190191
if (hasSortingHelper(element, ui) &&
191192
element.sortable( 'option', 'appendTo' ) === 'parent') {
192193
// The .ui-sortable-helper element (that's the default class name)
193-
// is placed last.
194-
result = savedNodes.last();
194+
result = helper;
195195
}
196196
return result;
197197
}
@@ -331,6 +331,7 @@ angular.module('ui.sortable', [])
331331
// This is inside activate (instead of start) in order to save
332332
// both lists when dragging between connected lists.
333333
savedNodes = savedNodesOrigin.contents();
334+
helper = ui.helper;
334335

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

431-
// It's now safe to clear the savedNodes
432+
// It's now safe to clear the savedNodes and helper
432433
// since stop is the last callback.
433434
savedNodes = null;
435+
helper = null;
434436
};
435437

436438
callbacks.receive = function(e, ui) {

test/sortable.e2e.callbacks.spec.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,65 @@ describe('uiSortable', function() {
156156
});
157157
});
158158

159+
it('should cancel sorting of node "Two" when then helper is appended to the `body`', function() {
160+
inject(function($compile, $rootScope) {
161+
var element;
162+
element = $compile(''.concat(
163+
'<ul ui-sortable="opts" ng-model="items">',
164+
beforeLiElement,
165+
'<li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li>',
166+
afterLiElement +
167+
'</ul>'))($rootScope);
168+
$rootScope.$apply(function() {
169+
$rootScope.opts = {
170+
helper: function (e, item) {
171+
return item.clone().appendTo('body');
172+
},
173+
update: function(e, ui) {
174+
if (ui.item.sortable.model === 'Two') {
175+
ui.item.sortable.cancel();
176+
}
177+
}
178+
};
179+
$rootScope.items = ['One', 'Two', 'Three'];
180+
});
181+
182+
host.append(element);
183+
184+
var li = element.find('[ng-repeat]:eq(1)');
185+
var dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
186+
li.simulate('drag', { dy: dy });
187+
expect($rootScope.items).toEqual(['One', 'Two', 'Three']);
188+
expect($rootScope.items).toEqual(listContent(element));
189+
// try again
190+
li = element.find('[ng-repeat]:eq(1)');
191+
dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
192+
li.simulate('drag', { dy: dy });
193+
expect($rootScope.items).toEqual(['One', 'Two', 'Three']);
194+
expect($rootScope.items).toEqual(listContent(element));
195+
// try again
196+
li = element.find('[ng-repeat]:eq(1)');
197+
dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
198+
li.simulate('drag', { dy: dy });
199+
expect($rootScope.items).toEqual(['One', 'Two', 'Three']);
200+
expect($rootScope.items).toEqual(listContent(element));
201+
202+
li = element.find('[ng-repeat]:eq(0)');
203+
dy = (2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
204+
li.simulate('drag', { dy: dy });
205+
expect($rootScope.items).toEqual(['Two', 'Three', 'One']);
206+
expect($rootScope.items).toEqual(listContent(element));
207+
208+
li = element.find('[ng-repeat]:eq(2)');
209+
dy = -(2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
210+
li.simulate('drag', { dy: dy });
211+
expect($rootScope.items).toEqual(['One', 'Two', 'Three']);
212+
expect($rootScope.items).toEqual(listContent(element));
213+
214+
$(element).remove();
215+
});
216+
});
217+
159218
it('should cancel sorting of node "Two" and "helper: function" that returns a list element is used', function() {
160219
inject(function($compile, $rootScope) {
161220
var element;

0 commit comments

Comments
 (0)