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

Commit e802204

Browse files
committed
Merge pull request #128 from thgreasi/master
Sorting no longer triggers $destroy for inner directives.
2 parents 484a2dc + 8625b71 commit e802204

File tree

6 files changed

+144
-7
lines changed

6 files changed

+144
-7
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.12.0",
3+
"version": "0.12.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.12.0",
3+
"version": "0.12.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: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,9 @@ angular.module('ui.sortable', [])
115115
// the start and stop of repeat sections and sortable doesn't
116116
// respect their order (even if we cancel, the order of the
117117
// comments are still messed up).
118-
savedNodes.detach();
119118
if (element.sortable('option','helper') === 'clone') {
120-
// first detach all the savedNodes and then restore all of them
121-
// except .ui-sortable-helper element (which is placed last).
122-
// That way it will be garbage collected.
119+
// restore all the savedNodes except .ui-sortable-helper element
120+
// (which is placed last). That way it will be garbage collected.
123121
savedNodes = savedNodes.not(savedNodes.last());
124122
}
125123
savedNodes.appendTo(element);
@@ -153,7 +151,7 @@ angular.module('ui.sortable', [])
153151
// if the item was not moved, then restore the elements
154152
// so that the ngRepeat's comment are correct.
155153
if((!('dropindex' in ui.item.sortable) || ui.item.sortable.isCanceled()) && element.sortable('option','helper') !== 'clone') {
156-
savedNodes.detach().appendTo(element);
154+
savedNodes.appendTo(element);
157155
}
158156
}
159157
};

test/karma.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports = function(config) {
2222
'bower_components/angular-mocks/angular-mocks.js',
2323
'src/sortable.js',
2424
'test/sortable.test-helper.js',
25+
'test/sortable.test-directives.js',
2526
'test/*.spec.js'
2627
],
2728

test/sortable.e2e.directives.spec.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
'use strict';
2+
3+
describe('uiSortable', function() {
4+
5+
// Ensure the sortable angular module is loaded
6+
beforeEach(module('ui.sortable'));
7+
beforeEach(module('ui.sortable.testHelper'));
8+
beforeEach(module('ui.sortable.testDirectives'));
9+
10+
var EXTRA_DY_PERCENTAGE, listContent, listInnerContent;
11+
12+
beforeEach(inject(function (sortableTestHelper) {
13+
EXTRA_DY_PERCENTAGE = sortableTestHelper.EXTRA_DY_PERCENTAGE;
14+
listContent = sortableTestHelper.listContent;
15+
listInnerContent = sortableTestHelper.listInnerContent;
16+
}));
17+
18+
describe('Inner directives related', function() {
19+
20+
var host;
21+
22+
beforeEach(inject(function() {
23+
host = $('<div id="test-host"></div>');
24+
$('body').append(host);
25+
}));
26+
27+
afterEach(function() {
28+
host.remove();
29+
host = null;
30+
});
31+
32+
it('should work when inner directives are used', function() {
33+
inject(function($compile, $rootScope) {
34+
var element;
35+
element = $compile(''.concat(
36+
'<ul ui-sortable="opts" ng-model="items">',
37+
'<li ng-repeat="item in items" id="s-{{$index}}" class="sortable-item">',
38+
'<ui-sortable-simple-test-directive ng-model="item"></ui-sortable-simple-test-directive>',
39+
'</li>',
40+
'</ul>'))($rootScope);
41+
42+
$rootScope.$apply(function() {
43+
$rootScope.opts = { };
44+
$rootScope.items = ['One', 'Two', 'Three'];
45+
});
46+
47+
host.append(element);
48+
49+
var li = element.find('> :eq(1)');
50+
var dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
51+
li.simulate('drag', { dy: dy });
52+
expect($rootScope.items).toEqual(['One', 'Three', 'Two']);
53+
expect($rootScope.items).toEqual(listInnerContent(element));
54+
55+
li = element.find('> :eq(1)');
56+
dy = -(1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
57+
li.simulate('drag', { dy: dy });
58+
expect($rootScope.items).toEqual(['Three', 'One', 'Two']);
59+
expect($rootScope.items).toEqual(listInnerContent(element));
60+
61+
$(element).remove();
62+
});
63+
});
64+
65+
it('should not $destroy directives after sorting.', function() {
66+
inject(function($compile, $rootScope) {
67+
var element;
68+
element = $compile(''.concat(
69+
'<ul ui-sortable="opts" ng-model="items">',
70+
'<li ng-repeat="item in items" id="s-{{$index}}" class="sortable-item">',
71+
'<ui-sortable-destroyable-test-directive ng-model="item"></ui-sortable-destroyable-test-directive>',
72+
'</li>',
73+
'</ul>'))($rootScope);
74+
75+
$rootScope.$apply(function() {
76+
$rootScope.opts = { };
77+
$rootScope.items = ['One', 'Two', 'Three'];
78+
});
79+
80+
host.append(element);
81+
82+
var li = element.find('> :eq(1)');
83+
var dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
84+
li.simulate('drag', { dy: dy });
85+
expect($rootScope.items).toEqual(['One', 'Three', 'Two']);
86+
expect($rootScope.items).toEqual(listInnerContent(element));
87+
88+
li = element.find('> :eq(1)');
89+
dy = -(1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
90+
li.simulate('drag', { dy: dy });
91+
expect($rootScope.items).toEqual(['Three', 'One', 'Two']);
92+
expect($rootScope.items).toEqual(listInnerContent(element));
93+
94+
$(element).remove();
95+
});
96+
});
97+
98+
});
99+
100+
});

test/sortable.test-directives.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
angular.module('ui.sortable.testDirectives', [])
4+
.directive('uiSortableSimpleTestDirective',
5+
function() {
6+
return {
7+
restrict: 'AE',
8+
scope: true,
9+
require: '?ngModel',
10+
template: '<div>Directive: <span class="itemContent" ng-bind="text"></span> !!!</div>',
11+
link: function(scope, element, attrs, ngModel) {
12+
scope.$watch(attrs.ngModel, function(value) {
13+
scope.text = value;
14+
});
15+
}
16+
};
17+
}
18+
)
19+
.directive('uiSortableDestroyableTestDirective',
20+
function() {
21+
return {
22+
restrict: 'AE',
23+
scope: true,
24+
require: '?ngModel',
25+
template: '<div>$destroy(able) Directive: <span class="itemContent" ng-bind="text"></span> !!!</div>',
26+
link: function(scope, element, attrs, ngModel) {
27+
scope.$watch(attrs.ngModel, function(value) {
28+
scope.text = value;
29+
});
30+
31+
element.bind('$destroy', function() {
32+
element.html('');
33+
});
34+
}
35+
};
36+
}
37+
);
38+

0 commit comments

Comments
 (0)