Thanks to visit codestin.com
Credit goes to github.com

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

Commit 409f6bc

Browse files
committed
Merge pull request #89 from thgreasi/angular1.2
Restoring ngRepeat's elements (and comments) when no sorting occured.
2 parents d9e9e18 + 816fd56 commit 409f6bc

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

src/sortable.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ angular.module('ui.sortable', [])
139139
ui.item.sortable.dropindex, 0,
140140
ngModel.$modelValue.splice(ui.item.sortable.index, 1)[0]);
141141
});
142+
} else {
143+
// if the item was not moved, then restore the elements
144+
// so that the ngRepeat's comment are correct.
145+
if(!('dropindex' in ui.item.sortable) || ui.item.sortable.isCanceled()) {
146+
savedNodes.detach().appendTo(element);
147+
}
142148
}
143149
};
144150

test/sortable.spec.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ describe('uiSortable', function() {
1010
return list.children().map(function(){ return this.innerHTML; }).toArray();
1111
}
1212
return [];
13+
},
14+
toEqualListInnerContent: function (list) {
15+
if (list && list.length) {
16+
return list.children().map(function(){ return $(this).find('.itemContent').html(); }).toArray();
17+
}
18+
return [];
1319
}
1420
});
1521
});
@@ -230,6 +236,116 @@ describe('uiSortable', function() {
230236
});
231237
});
232238

239+
it('should work when "handle" option is used', function() {
240+
inject(function($compile, $rootScope) {
241+
var element, logsElement;
242+
element = $compile('<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}"><span class="handle">H</span> <span class="itemContent">{{ item }}</span></li></ul>')($rootScope);
243+
$rootScope.$apply(function() {
244+
$rootScope.opts = {
245+
handle: '.handle'
246+
};
247+
$rootScope.items = ["One", "Two", "Three"];
248+
});
249+
250+
host.append(element);
251+
252+
var li = element.find('li:eq(1)');
253+
var dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
254+
li.find('.handle').simulate('drag', { dy: dy });
255+
expect($rootScope.items).toEqual(["One", "Three", "Two"]);
256+
expect($rootScope.items).toEqualListInnerContent(element);
257+
258+
li = element.find('li:eq(1)');
259+
dy = -(1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
260+
li.find('.handle').simulate('drag', { dy: dy });
261+
expect($rootScope.items).toEqual(["Three", "One", "Two"]);
262+
expect($rootScope.items).toEqualListInnerContent(element);
263+
264+
$(element).remove();
265+
});
266+
});
267+
268+
it('should properly remove elements after a sorting', function() {
269+
inject(function($compile, $rootScope) {
270+
var element, logsElement;
271+
element = $compile('<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}"><span class="handle">H</span> <span class="itemContent">{{ item }}</span> <button type="button" class="removeButton" ng-click="remove(item, $index)">X</button></li></ul>')($rootScope);
272+
$rootScope.$apply(function() {
273+
$rootScope.opts = {
274+
handle: '.handle'
275+
};
276+
$rootScope.items = ["One", "Two", "Three"];
277+
278+
$rootScope.remove = function (item, itemIndex) {
279+
$rootScope.items.splice(itemIndex, 1);
280+
};
281+
});
282+
283+
host.append(element);
284+
285+
var li = element.find('li:eq(1)');
286+
var dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
287+
li.find('.handle').simulate('drag', { dy: dy });
288+
expect($rootScope.items).toEqual(["One", "Three", "Two"]);
289+
expect($rootScope.items).toEqualListInnerContent(element);
290+
291+
li = element.find('li:eq(1)');
292+
li.find('.removeButton').click();
293+
expect($rootScope.items).toEqual(["One", "Two"]);
294+
expect($rootScope.items).toEqualListInnerContent(element);
295+
296+
li = element.find('li:eq(0)');
297+
dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
298+
li.find('.handle').simulate('drag', { dy: dy });
299+
expect($rootScope.items).toEqual(["Two", "One"]);
300+
expect($rootScope.items).toEqualListInnerContent(element);
301+
302+
li = element.find('li:eq(0)');
303+
li.find('.removeButton').click();
304+
expect($rootScope.items).toEqual(["One"]);
305+
expect($rootScope.items).toEqualListInnerContent(element);
306+
307+
$(element).remove();
308+
});
309+
});
310+
311+
it('should properly remove elements after a drag is reverted', function() {
312+
inject(function($compile, $rootScope) {
313+
var element, logsElement;
314+
element = $compile('<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}"><span class="handle">H</span> <span class="itemContent">{{ item }}</span> <button type="button" class="removeButton" ng-click="remove(item, $index)">X</button></li></ul>')($rootScope);
315+
$rootScope.$apply(function() {
316+
$rootScope.opts = {
317+
handle: '.handle'
318+
};
319+
$rootScope.items = ["One", "Two", "Three"];
320+
321+
$rootScope.remove = function (item, itemIndex) {
322+
$rootScope.items.splice(itemIndex, 1);
323+
};
324+
});
325+
326+
host.append(element);
327+
328+
var li = element.find('li:eq(0)');
329+
var dy = (2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
330+
li.find('.handle').simulate('dragAndRevert', { dy: dy });
331+
expect($rootScope.items).toEqual(["One", "Two", "Three"]);
332+
expect($rootScope.items).toEqualListInnerContent(element);
333+
334+
li = element.find('li:eq(0)');
335+
li.find('.removeButton').click();
336+
expect($rootScope.items).toEqual(["Two", "Three"]);
337+
expect($rootScope.items).toEqualListInnerContent(element);
338+
339+
li = element.find('li:eq(0)');
340+
dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
341+
li.find('.handle').simulate('drag', { dy: dy });
342+
expect($rootScope.items).toEqual(["Three", "Two"]);
343+
expect($rootScope.items).toEqualListInnerContent(element);
344+
345+
$(element).remove();
346+
});
347+
});
348+
233349
});
234350

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

0 commit comments

Comments
 (0)