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

Commit 62bc914

Browse files
committed
Merge pull request #104 from thgreasi/angular1.2
Initial support for helper: 'clone' option.
2 parents c0b9d17 + b3fb1ba commit 62bc914

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

src/sortable.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,14 @@ angular.module('ui.sortable', [])
114114
// the start and stop of repeat sections and sortable doesn't
115115
// respect their order (even if we cancel, the order of the
116116
// comments are still messed up).
117-
savedNodes.detach().appendTo(element);
117+
savedNodes.detach();
118+
if (element.sortable('option','helper') === 'clone') {
119+
// first detach all the savedNodes and then restore all of them
120+
// except .ui-sortable-helper element (which is placed last).
121+
// That way it will be garbage collected.
122+
savedNodes = savedNodes.not(savedNodes.last());
123+
}
124+
savedNodes.appendTo(element);
118125

119126
// If received is true (an item was dropped in from another list)
120127
// then we add the new item to this list otherwise wait until the
@@ -144,7 +151,7 @@ angular.module('ui.sortable', [])
144151
} else {
145152
// if the item was not moved, then restore the elements
146153
// so that the ngRepeat's comment are correct.
147-
if(!('dropindex' in ui.item.sortable) || ui.item.sortable.isCanceled()) {
154+
if((!('dropindex' in ui.item.sortable) || ui.item.sortable.isCanceled()) && element.sortable('option','helper') !== 'clone') {
148155
savedNodes.detach().appendTo(element);
149156
}
150157
}

test/sortable.spec.js

+70
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,76 @@ describe('uiSortable', function() {
345345
});
346346
});
347347

348+
it('should work when "helper: clone" option is used', function() {
349+
inject(function($compile, $rootScope) {
350+
var element;
351+
element = $compile('<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}" class="sortable-item">{{ item }}</li></ul>')($rootScope);
352+
$rootScope.$apply(function() {
353+
$rootScope.opts = {
354+
helper: 'clone',
355+
};
356+
$rootScope.items = ['One', 'Two', 'Three'];
357+
});
358+
359+
host.append(element);
360+
361+
var li = element.find(':eq(1)');
362+
var dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
363+
li.simulate('drag', { dy: dy });
364+
expect($rootScope.items).toEqual(['One', 'Three', 'Two']);
365+
expect($rootScope.items).toEqual(listContent(element));
366+
367+
li = element.find(':eq(1)');
368+
dy = -(1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
369+
li.simulate('drag', { dy: dy });
370+
expect($rootScope.items).toEqual(['Three', 'One', 'Two']);
371+
expect($rootScope.items).toEqual(listContent(element));
372+
373+
$(element).remove();
374+
});
375+
});
376+
377+
it('should work when "helper: clone" option is used and a drag is reverted', function() {
378+
inject(function($compile, $rootScope) {
379+
var element;
380+
element = $compile('<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}" class="sortable-item">{{ item }}</li></ul>')($rootScope);
381+
$rootScope.$apply(function() {
382+
$rootScope.opts = {
383+
helper: 'clone',
384+
};
385+
$rootScope.items = ['One', 'Two', 'Three'];
386+
});
387+
388+
host.append(element);
389+
390+
var li = element.find(':eq(0)');
391+
var dy = (2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
392+
li.simulate('dragAndRevert', { dy: dy });
393+
expect($rootScope.items).toEqual(['One', 'Two', 'Three']);
394+
expect($rootScope.items).toEqual(listContent(element));
395+
396+
li = element.find(':eq(0)');
397+
dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
398+
li.simulate('drag', { dy: dy });
399+
expect($rootScope.items).toEqual(['Two', 'One', 'Three']);
400+
expect($rootScope.items).toEqual(listContent(element));
401+
402+
li = element.find(':eq(1)');
403+
dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
404+
li.simulate('drag', { dy: dy });
405+
expect($rootScope.items).toEqual(['Two', 'Three', 'One']);
406+
expect($rootScope.items).toEqual(listContent(element));
407+
408+
li = element.find(':eq(1)');
409+
dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
410+
li.simulate('drag', { dy: dy });
411+
expect($rootScope.items).toEqual(['Two', 'One', 'Three']);
412+
expect($rootScope.items).toEqual(listContent(element));
413+
414+
$(element).remove();
415+
});
416+
});
417+
348418
});
349419

350420
describe('Multiple sortables related', function() {

0 commit comments

Comments
 (0)