Skip to content

Commit c049692

Browse files
author
Niels Dequeker
committed
Merge pull request angular-ui-tree#582 from angular-ui-tree/#478_out_of_depth_calculation
angular-ui-tree#478 out of depth calculation
2 parents 62c1a1d + a718d4d commit c049692

File tree

3 files changed

+79
-67
lines changed

3 files changed

+79
-67
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "angular-ui-tree",
33
"version": "2.9.0",
44
"description": "An AngularJS UI component that can sort nested lists, provides drag & drop support and doesn't depend on jQuery",
5+
"license": "MIT",
56
"repository": {
67
"type": "git",
78
"url": "https://github.com/angular-ui-tree/angular-ui-tree"

source/controllers/nodeCtrl.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -112,31 +112,32 @@
112112
return 1;
113113
};
114114

115-
var subDepth = 0;
116-
function countSubDepth(scope) {
117-
var i, childNodes,
118-
count = 0,
119-
nodes = scope.childNodes();
120-
121-
for (i = 0; i < nodes.length; i++) {
122-
childNodes = nodes[i].$childNodesScope;
123-
124-
if (childNodes && childNodes.childNodesCount() > 0) {
125-
count = 1;
126-
countSubDepth(childNodes);
127-
}
115+
/**
116+
* Returns the depth of the deepest subtree under this node
117+
* @param scope a TreeNodesController scope object
118+
* @returns Depth of all nodes *beneath* this node. If scope belongs to a leaf node, the
119+
* result is 0 (it has no subtree).
120+
*/
121+
function countSubTreeDepth(scope) {
122+
var thisLevelDepth = 0,
123+
childNodes = scope.childNodes(),
124+
childNode,
125+
childDepth,
126+
i;
127+
if (!childNodes || childNodes.length === 0) {
128+
return 0;
128129
}
129-
subDepth += count;
130+
for (i = childNodes.length - 1; i >= 0 ; i--) {
131+
childNode = childNodes[i],
132+
childDepth = 1 + countSubTreeDepth(childNode);
133+
thisLevelDepth = Math.max(thisLevelDepth, childDepth);
134+
}
135+
return thisLevelDepth;
130136
}
131137

132138
$scope.maxSubDepth = function () {
133-
subDepth = 0;
134-
if ($scope.$childNodesScope) {
135-
countSubDepth($scope.$childNodesScope);
136-
}
137-
return subDepth;
139+
return $scope.$childNodesScope ? countSubTreeDepth($scope.$childNodesScope) : 0;
138140
};
139-
140141
}
141142
]);
142143
})();

source/controllers/nodeCtrl.spec.js

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -46,129 +46,129 @@ describe('treeCtrl', function () {
4646

4747
scope.list = [
4848
{
49-
'id': 1,
50-
'title': 'item1',
49+
'id': 0,
50+
'title': 'item0',
5151
'items': []
5252
},
5353
{
54-
'id': 2,
55-
'title': 'item2',
54+
'id': 1,
55+
'title': 'item1',
5656
'items': [
5757
{
58-
'id': 21,
59-
'title': 'item2.1',
58+
'id': 10,
59+
'title': 'item1.0',
6060
'items': [
6161
{
62-
'id': 211,
63-
'title': 'item2.1.1',
62+
'id': 100,
63+
'title': 'item1.0.0',
6464
'items': []
6565
},
6666
{
67-
'id': 212,
68-
'title': 'item2.1.2',
67+
'id': 101,
68+
'title': 'item1.0.1',
6969
'items': []
7070
},
7171
{
72-
'id': 213,
73-
'title': 'item2.1.3',
72+
'id': 102,
73+
'title': 'item1.0.2',
7474
'items': []
7575
}
7676
]
7777
},
7878
{
79-
'id': 22,
80-
'title': 'item2.2',
79+
'id': 11,
80+
'title': 'item1.1',
8181
'items': []
8282
},
8383
{
84-
'id': 23,
85-
'title': 'item2.3',
84+
'id': 12,
85+
'title': 'item1.2',
8686
'items': [
8787
{
88-
'id': 231,
89-
'title': 'item2.3.1',
88+
'id': 120,
89+
'title': 'item1.2.0',
9090
'items': []
9191
},
9292
{
93-
'id': 232,
94-
'title': 'item2.3.2',
93+
'id': 121,
94+
'title': 'item1.2.1',
9595
'items': []
9696
},
9797
{
98-
'id': 233,
99-
'title': 'item2.3.3',
98+
'id': 122,
99+
'title': 'item1.2.2',
100100
'items': []
101101
}
102102
]
103103
},
104104
{
105-
'id': 24,
106-
'title': 'item2.4',
105+
'id': 13,
106+
'title': 'item1.3',
107107
'items': [
108108
{
109-
'id': 241,
110-
'title': 'item2.4.1',
109+
'id': 130,
110+
'title': 'item1.3.0',
111111
'items': []
112112
},
113113
{
114-
'id': 242,
115-
'title': 'item2.4.2',
114+
'id': 131,
115+
'title': 'item1.3.1',
116116
'items': []
117117
},
118118
{
119-
'id': 243,
119+
'id': 132,
120120
'title': 'item2.4.3',
121121
'items': []
122122
},
123123
{
124124
'id': 244,
125-
'title': 'item2.4.4',
125+
'title': 'item1.3.3',
126126
'items': []
127127
}
128128
]
129129
}
130130
]
131131
},
132132
{
133-
'id': 3,
134-
'title': 'item3',
133+
'id': 2,
134+
'title': 'item2',
135135
'items': [
136136
{
137-
'id': 31,
138-
'title': 'item3.1',
137+
'id': 20,
138+
'title': 'item2.0',
139139
'items': []
140140
},
141141
{
142-
'id': 32,
143-
'title': 'item3.2',
142+
'id': 21,
143+
'title': 'item2.1',
144144
'items': []
145145
},
146146
{
147-
'id': 33,
148-
'title': 'item3.3',
147+
'id': 22,
148+
'title': 'item2.2',
149149
'items': [
150150
{
151-
'id': 331,
152-
'title': 'item3.3.1',
151+
'id': 220,
152+
'title': 'item2.2.0',
153153
'items': []
154154
},
155155
{
156-
'id': 332,
157-
'title': 'item3.3.2',
156+
'id': 221,
157+
'title': 'item2.2.1',
158158
'items': []
159159
},
160160
{
161-
'id': 333,
162-
'title': 'item3.3.3',
161+
'id': 222,
162+
'title': 'item2.2.2',
163163
'items': []
164164
}
165165
]
166166
}
167167
]
168168
},
169169
{
170-
'id': 4,
171-
'title': 'item4',
170+
'id': 3,
171+
'title': 'item3',
172172
'items': []
173173
}
174174
];
@@ -185,8 +185,18 @@ describe('treeCtrl', function () {
185185
tree = createTree();
186186
localScope = angular.element(tree.children('ol').first()).scope();
187187

188-
expect(localScope.childNodes()[1].maxSubDepth()).toEqual(1);
188+
expect(localScope.childNodes()[1].maxSubDepth()).toEqual(2);
189+
190+
});
189191

192+
it('should calculate the depth of any subtree', function () {
193+
var tree, localScope;
194+
tree = createTree();
195+
localScope = angular.element(tree.children('ol').first()).scope();
196+
expect(localScope.childNodes()[0].maxSubDepth()).toEqual(0); // item0
197+
expect(localScope.childNodes()[1].maxSubDepth()).toEqual(2); // item1
198+
expect(localScope.childNodes()[1].childNodes()[0].maxSubDepth()).toEqual(1); // item1.0
199+
expect(localScope.childNodes()[1].childNodes()[0].childNodes()[0].maxSubDepth()).toEqual(0); // item1.0.0
190200
});
191201
});
192202

0 commit comments

Comments
 (0)