Skip to content

Commit a6d7490

Browse files
David Martínez RosDavid Martínez Ros
authored andcommitted
solution some bugs with the movement of a node with childs to the parent
1 parent a354ca8 commit a6d7490

File tree

8 files changed

+58
-51
lines changed

8 files changed

+58
-51
lines changed

run_ng_prod.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ng build --base-href http://davidmartinezros.com/Angular2/component-tree-project/ --prod --env=prod
1+
ng build --prod --env=prod

src/app/load/load.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div>
22
<ul>
33
<li>Load the file <input type="file" (change)="onChange($event)"/></li>
4-
<li>Example json file <a href="../../data/tree.json" target="_blank">download here</a></li>
4+
<li>Example json file <a href="./assets/data/tree.json" target="_blank">download here</a></li>
55
</ul>
66
</div>
77
<app-tree [(tree)]="tree"></app-tree>

src/app/load/load.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class LoadComponent implements OnInit {
2929
}
3030

3131
loadFile() {
32-
this.tree = this.service.getTree('./data/tree.json');
32+
this.tree = this.service.getTree('./assets/data/tree.json');
3333
}
3434

3535
onChange(event) {
@@ -42,7 +42,7 @@ export class LoadComponent implements OnInit {
4242
if(file != null) {
4343
url = URL.createObjectURL(file);
4444
} else {
45-
url = './data/tree.json';
45+
url = './assets/data/tree.json';
4646
}
4747
console.log('url:' + url);
4848
if(url != null) {

src/app/node-tree/node-tree.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
</div>
2727
<div *ngIf="hasLeaf()">
2828
<div *ngIf="node.expand">
29-
<div *ngFor="let child of node.childs; let firstNodeChild = first; let lastNodeChild = last">
29+
<div *ngFor="let child of node.children; let firstNodeChild = first; let lastNodeChild = last">
3030
<app-node-tree [node]="child" [parent]="node" [firstNode]="firstNodeChild" [lastNode]="lastNodeChild"></app-node-tree>
3131
</div>
3232
</div>

src/app/node-tree/node-tree.component.ts

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class NodeTreeComponent implements OnInit {
2828
}
2929

3030
hasLeaf(): boolean {
31-
return ((this.node.childs != null) && (this.node.childs.length > 0));
31+
return ((this.node.children != null) && (this.node.children.length > 0));
3232
}
3333

3434
hasParent(): boolean {
@@ -55,6 +55,9 @@ export class NodeTreeComponent implements OnInit {
5555
this.node.firstNode = this.firstNode;
5656
this.node.lastNode = this.lastNode;
5757
console.log("el node " + this.node.name + " te lastNode " + this.node.lastNode);
58+
if(this.node.children == null) {
59+
this.node.children = [];
60+
}
5861
if(this.node.ancestors == null) {
5962
this.node.ancestors = [];
6063
}
@@ -82,16 +85,16 @@ export class NodeTreeComponent implements OnInit {
8285
}
8386

8487
if(parentTree != null) {
85-
const nodeIndex = parentTree.childs.indexOf(this.node);
88+
const nodeIndex = parentTree.children.indexOf(this.node);
8689
const siblingIndex = nodeIndex -1;
87-
const siblingNode = parentTree.childs[siblingIndex];
90+
const siblingNode = parentTree.children[siblingIndex];
8891

8992
console.log('nodeIndex:' + nodeIndex);
9093
console.log('siblingIndex:' + siblingIndex);
9194
console.log('this.node.ancestors.length:' + this.node.ancestors.length);
9295

93-
parentTree.childs[siblingIndex] = this.node;
94-
parentTree.childs[nodeIndex] = siblingNode;
96+
parentTree.children[siblingIndex] = this.node;
97+
parentTree.children[nodeIndex] = siblingNode;
9598

9699
this.reloadFirstAndLastNode(parentTree, parentTree.firstNode, parentTree.lastNode);
97100
}
@@ -107,16 +110,16 @@ export class NodeTreeComponent implements OnInit {
107110
}
108111

109112
if(parentTree != null) {
110-
const nodeIndex = parentTree.childs.indexOf(this.node);
113+
const nodeIndex = parentTree.children.indexOf(this.node);
111114
const siblingIndex = nodeIndex +1;
112-
const siblingNode = parentTree.childs[siblingIndex];
115+
const siblingNode = parentTree.children[siblingIndex];
113116

114117
console.log('nodeIndex:' + nodeIndex);
115118
console.log('siblingIndex:' + siblingIndex);
116-
console.log('parentTree.childs.length:' + parentTree.childs.length);
119+
console.log('parentTree.children.length:' + parentTree.children.length);
117120

118-
parentTree.childs[siblingIndex] = this.node;
119-
parentTree.childs[nodeIndex] = siblingNode;
121+
parentTree.children[siblingIndex] = this.node;
122+
parentTree.children[nodeIndex] = siblingNode;
120123

121124
this.reloadFirstAndLastNode(parentTree, parentTree.firstNode, parentTree.lastNode);
122125
}
@@ -138,24 +141,24 @@ export class NodeTreeComponent implements OnInit {
138141

139142
if(parentTree != null && grandParentTree != null) {
140143

141-
let ancestorsSecondLevel: Node[] = grandParentTree.childs;
142-
let childsSecondLevel: Node[] = [];
144+
let ancestorsSecondLevel: Node[] = grandParentTree.children;
145+
let childrenSecondLevel: Node[] = [];
143146
for(let ancestor of ancestorsSecondLevel) {
144-
childsSecondLevel.push(ancestor);
147+
childrenSecondLevel.push(ancestor);
145148
if(ancestor === parentTree) {
146-
childsSecondLevel.push(this.node);
149+
childrenSecondLevel.push(this.node);
147150
}
148151
}
149-
grandParentTree.childs = childsSecondLevel;
152+
grandParentTree.children = childrenSecondLevel;
150153

151-
let ancestorsFirstLevel: Node[] = parentTree.childs;
152-
let childsFirstLevel: Node[] = [];
154+
let ancestorsFirstLevel: Node[] = parentTree.children;
155+
let childrenFirstLevel: Node[] = [];
153156
for(let ancestor of ancestorsFirstLevel) {
154157
if(!(ancestor === this.node)) {
155-
childsFirstLevel.push(ancestor);
158+
childrenFirstLevel.push(ancestor);
156159
}
157160
}
158-
parentTree.childs = childsFirstLevel;
161+
parentTree.children = childrenFirstLevel;
159162

160163
let ancestors: Node[] = [];
161164
for(let ancestor of this.node.ancestors) {
@@ -165,6 +168,8 @@ export class NodeTreeComponent implements OnInit {
165168
}
166169
this.node.ancestors = ancestors;
167170

171+
this.removeParentMovedInChildren(this.node, parentTree);
172+
168173
this.reloadFirstAndLastNode(grandParentTree, grandParentTree.firstNode, grandParentTree.lastNode);
169174
}
170175
}
@@ -173,12 +178,27 @@ export class NodeTreeComponent implements OnInit {
173178
let i = 0;
174179
node.firstNode = firstNode;
175180
node.lastNode = lastNode;
176-
if(node.childs != null && node.childs.length > 0) {
177-
for(let child of node.childs) {
178-
this.reloadFirstAndLastNode(child, i === 0, i === node.childs.length-1);
181+
if(node.children != null && node.children.length > 0) {
182+
for(let child of node.children) {
183+
this.reloadFirstAndLastNode(child, i === 0, i === node.children.length-1);
179184
i++;
180185
}
181186
}
182187
}
183188

189+
removeParentMovedInChildren(node: Node, parent: Node) {
190+
for(let child of node.children) {
191+
let ancestors: Node[] = [];
192+
for(let ancestorChild of child.ancestors) {
193+
if(!(ancestorChild === parent)) {
194+
ancestors.push(ancestorChild);
195+
}
196+
}
197+
child.ancestors = ancestors;
198+
if(child.children.length > 0) {
199+
this.removeParentMovedInChildren(child, parent);
200+
}
201+
}
202+
}
203+
184204
}

src/app/node.class.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,13 @@ export class Node {
33
name: string;
44
description: string;
55

6-
childs: Node[];
6+
children: Node[] = [];
77

88
expand: boolean = false;
99

1010
firstNode: boolean;
1111
lastNode: boolean;
1212

13-
ancestors: Node[];
14-
15-
/*removeParentInChilds(parent: Node) {
16-
let ans: Node[] = [];
17-
for(let a of this.ancestors) {
18-
if(!(a === parent)) {
19-
ans.push(a);
20-
}
21-
}
22-
this.ancestors = ans;
23-
for(let child of this.childs) {
24-
child.removeParentInChilds(parent);
25-
}
26-
}*/
13+
ancestors: Node[] = [];
2714

2815
}

src/data/tree.json renamed to src/assets/data/tree.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
"id": 0,
44
"name": "root",
55
"description": "Desc Root",
6-
"childs":
6+
"children":
77
[
88
{
99
"id": 1,
1010
"name": "Node 1",
1111
"description": "Desc Node 1",
12-
"childs":
12+
"children":
1313
[
1414
{
1515
"id": 11,
@@ -20,7 +20,7 @@
2020
"id": 12,
2121
"name": "Node 1.2",
2222
"description": "Desc Node 1.2",
23-
"childs":
23+
"children":
2424
[
2525
{
2626
"id": 121,
@@ -40,7 +40,7 @@
4040
"id": 2,
4141
"name": "Node 2",
4242
"description": "Desc Node 2",
43-
"childs":
43+
"children":
4444
[
4545
{
4646
"id": 21,
@@ -51,7 +51,7 @@
5151
"id": 22,
5252
"name": "Node 2.2",
5353
"description": "Desc Node 2.2",
54-
"childs":
54+
"children":
5555
[
5656
{
5757
"id": 221,
@@ -81,7 +81,7 @@
8181
"id": 3,
8282
"name": "Node 3",
8383
"description": "Desc Node 3",
84-
"childs":
84+
"children":
8585
[
8686
{
8787
"id": 31,
@@ -92,7 +92,7 @@
9292
"id": 32,
9393
"name": "Node 3.2",
9494
"description": "Desc Node 3.2",
95-
"childs":
95+
"children":
9696
[
9797
{
9898
"id": 321,
@@ -110,7 +110,7 @@
110110
"id": 34,
111111
"name": "Node 3.4",
112112
"description": "Desc Node 3.4",
113-
"childs":
113+
"children":
114114
[
115115
{
116116
"id": 341,

src/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta charset="utf-8">
55
<title>ComponentTreeProject</title>
6-
<base href="/">
6+
<base href="">
77

88
<meta name="viewport" content="width=device-width, initial-scale=1">
99
<link rel="icon" type="image/x-icon" href="favicon.ico">

0 commit comments

Comments
 (0)