Skip to content

Commit 0d0ed9d

Browse files
simplify the fields in the node class and make the up and down movement of nodes between siblings
1 parent e917ac2 commit 0d0ed9d

File tree

8 files changed

+164
-21
lines changed

8 files changed

+164
-21
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ div.text {
1818
background: lightgoldenrodyellow;
1919
}
2020

21+
div.description {
22+
display: inline-block;
23+
line-height: 15px;
24+
font-family: arial;
25+
}
26+
2127
div.text:hover {
2228
border: 1px solid darkgoldenrod;
2329
color:black;

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,21 @@
1212
<img *ngIf="hasLeaf() && node.expand" src="./assets/collapse_icon.png" (click)="clickExpandCollapse()"/>
1313
<img *ngIf="hasLeaf() && !node.expand" src="./assets/expand_icon.png" (click)="clickExpandCollapse()"/>
1414
<img *ngIf="!hasLeaf()" src="./assets/line_sheet.png"/>
15-
</span>
15+
</span>
1616

17-
<div class="text"><b>{{ node.name }}</b> el seu pare es: {{ getParentId() }}</div>
17+
<div class="text" title="His parent is: {{ getParentName() }} with id: {{ getParentId() }}">
18+
<b>{{ node.name }}</b>
19+
</div>
20+
<img *ngIf="!node.firstNode" src="./assets/up.png" (click)="upNode()"/>
21+
<img *ngIf="!node.lastNode" src="./assets/down.png" (click)="downNode()"/>
22+
<div class="description">
23+
{{ node.description }}
24+
</div>
1825
</div>
1926
<div *ngIf="hasLeaf()">
2027
<div *ngIf="node.expand">
21-
<div *ngFor="let child of node.childs; let lastNodeChild = last">
22-
<app-node-tree [node]="child" [parent]="node" [lastNode]="lastNodeChild"></app-node-tree>
28+
<div *ngFor="let child of node.childs; let firstNodeChild = first; let lastNodeChild = last">
29+
<app-node-tree [node]="child" [parent]="node" [firstNode]="firstNodeChild" [lastNode]="lastNodeChild"></app-node-tree>
2330
</div>
2431
</div>
2532
</div>

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

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export class NodeTreeComponent implements OnInit {
1313

1414
@Input() parent: Node;
1515

16+
@Input() firstNode;
17+
1618
@Input() lastNode;
1719

1820
constructor() { }
@@ -37,7 +39,16 @@ export class NodeTreeComponent implements OnInit {
3739
}
3840
}
3941

42+
getParentName(): string {
43+
if(this.node.ancestors != null && this.node.ancestors.length > 0) {
44+
return this.node.ancestors[this.node.ancestors.length-1].name;
45+
} else {
46+
return "root";
47+
}
48+
}
49+
4050
loadNode() {
51+
this.node.firstNode = this.firstNode;
4152
this.node.lastNode = this.lastNode;
4253
console.log("el node " + this.node.name + " te lastNode " + this.node.lastNode);
4354
if(this.node.ancestors == null) {
@@ -57,4 +68,99 @@ export class NodeTreeComponent implements OnInit {
5768
console.log('el node ' + this.node.name + " te " + this.node.ancestors.length + " parents.");
5869
}
5970

71+
upNode() {
72+
console.log('upNode');
73+
74+
let parentTree: Node;
75+
76+
if(this.node.ancestors != null && this.node.ancestors.length > 0) {
77+
parentTree = this.node.ancestors[this.node.ancestors.length-1];
78+
}
79+
80+
if(parentTree != null) {
81+
const nodeIndex = parentTree.childs.indexOf(this.node);
82+
const siblingIndex = nodeIndex -1;
83+
const siblingNode = parentTree.childs[siblingIndex];
84+
85+
console.log('nodeIndex:' + nodeIndex);
86+
console.log('siblingIndex:' + siblingIndex);
87+
console.log('this.node.ancestors.length:' + this.node.ancestors.length);
88+
89+
let firstNode = false;
90+
let lastNode = false;
91+
if(nodeIndex === 0) {
92+
firstNode = true;
93+
}
94+
if(nodeIndex === parentTree.childs.length-1) {
95+
lastNode = true;
96+
}
97+
98+
let firstNodeSibling = false;
99+
let lastNodeSibling = false;
100+
if(siblingIndex === 0) {
101+
firstNodeSibling = true;
102+
}
103+
if(siblingIndex === parentTree.childs.length-1) {
104+
lastNodeSibling = true;
105+
}
106+
107+
this.node.lastNode = lastNodeSibling;
108+
this.node.firstNode = firstNodeSibling;
109+
siblingNode.lastNode = lastNode;
110+
siblingNode.firstNode = firstNode;
111+
112+
parentTree.childs[siblingIndex] = this.node;
113+
parentTree.childs[nodeIndex] = siblingNode;
114+
115+
}
116+
117+
}
118+
119+
downNode() {
120+
console.log('downNode');
121+
122+
let parentTree: Node;
123+
124+
if(this.node.ancestors != null && this.node.ancestors.length > 0) {
125+
parentTree = this.node.ancestors[this.node.ancestors.length-1];
126+
}
127+
128+
if(parentTree != null) {
129+
const nodeIndex = parentTree.childs.indexOf(this.node);
130+
const siblingIndex = nodeIndex +1;
131+
const siblingNode = parentTree.childs[siblingIndex];
132+
133+
console.log('nodeIndex:' + nodeIndex);
134+
console.log('siblingIndex:' + siblingIndex);
135+
console.log('parentTree.childs.length:' + parentTree.childs.length);
136+
137+
let firstNode = false;
138+
let lastNode = false;
139+
if(nodeIndex === 0) {
140+
firstNode = true;
141+
}
142+
if(nodeIndex === parentTree.childs.length-1) {
143+
lastNode = true;
144+
}
145+
146+
let firstNodeSibling = false;
147+
let lastNodeSibling = false;
148+
if(siblingIndex === 0) {
149+
firstNodeSibling = true;
150+
}
151+
if(siblingIndex === parentTree.childs.length-1) {
152+
lastNodeSibling = true;
153+
}
154+
155+
this.node.lastNode = lastNodeSibling;
156+
this.node.firstNode = firstNodeSibling;
157+
siblingNode.lastNode = lastNode;
158+
siblingNode.firstNode = firstNode;
159+
160+
parentTree.childs[siblingIndex] = this.node;
161+
parentTree.childs[nodeIndex] = siblingNode;
162+
163+
}
164+
}
165+
60166
}

src/app/node.class.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
export class Node {
22
id: number;
33
name: string;
4+
description: string;
45

56
childs: Node[];
67

78
expand: boolean = false;
89

10+
firstNode: boolean;
911
lastNode: boolean;
1012

1113
ancestors: Node[];

src/app/tree/tree.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="parent">
2-
<div *ngFor="let node of tree | async; let lastNode = last">
3-
<app-node-tree [node]="node" [parent]="" [lastNode]="lastNode"></app-node-tree>
2+
<div *ngFor="let node of tree | async; let firstNode = first; let lastNode = last">
3+
<app-node-tree [node]="node" [parent]="" [firstNode]="firstNode" [lastNode]="lastNode"></app-node-tree>
44
</div>
55
</div>

src/assets/down.png

216 Bytes
Loading

src/assets/up.png

210 Bytes
Loading

src/data/tree.json

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,107 +2,129 @@
22
{
33
"id": 1,
44
"name": "Node 1",
5+
"description": "Desc Node 1",
56
"childs":
67
[
78
{
89
"id": 11,
9-
"name": "Node 1.1"
10+
"name": "Node 1.1",
11+
"description": "Desc Node 1.1"
1012
},
1113
{
1214
"id": 12,
1315
"name": "Node 1.2",
16+
"description": "Desc Node 1.2",
1417
"childs":
1518
[
1619
{
1720
"id": 121,
18-
"name": "Node 1.2.1"
21+
"name": "Node 1.2.1",
22+
"description": "Desc Node 1.2.1"
1923
}
2024
]
2125
},
2226
{
2327
"id": 13,
24-
"name": "Node 1.3"
28+
"name": "Node 1.3",
29+
"description": "Desc Node 1.3"
2530
}
2631
]
2732
},
2833
{
2934
"id": 2,
3035
"name": "Node 2",
36+
"description": "Desc Node 2",
3137
"childs":
3238
[
3339
{
3440
"id": 21,
35-
"name": "Node 2.1"
41+
"name": "Node 2.1",
42+
"description": "Desc Node 2.1"
3643
},
3744
{
3845
"id": 22,
3946
"name": "Node 2.2",
47+
"description": "Desc Node 2.2",
4048
"childs":
4149
[
4250
{
4351
"id": 221,
44-
"name": "Node 2.2.1"
52+
"name": "Node 2.2.1",
53+
"description": "Desc Node 2.2.1"
4554
}
4655
]
4756
},
4857
{
4958
"id": 23,
50-
"name": "Node 2.3"
59+
"name": "Node 2.3",
60+
"description": "Desc Node 2.3"
5161
},
5262
{
5363
"id": 24,
54-
"name": "Node 2.4"
64+
"name": "Node 2.4",
65+
"description": "Desc Node 2.4"
5566
},
5667
{
5768
"id": 25,
58-
"name": "Node 2.5"
69+
"name": "Node 2.5",
70+
"description": "Desc Node 2.5"
5971
}
6072
]
6173
},
6274
{
6375
"id": 3,
6476
"name": "Node 3",
77+
"description": "Desc Node 3",
6578
"childs":
6679
[
6780
{
6881
"id": 31,
69-
"name": "Node 3.1"
82+
"name": "Node 3.1",
83+
"description": "Desc Node 3.1"
7084
},
7185
{
7286
"id": 32,
7387
"name": "Node 3.2",
88+
"description": "Desc Node 3.2",
7489
"childs":
7590
[
7691
{
7792
"id": 321,
78-
"name": "Node 3.2.1"
93+
"name": "Node 3.2.1",
94+
"description": "Desc Node 3.2.1"
7995
}
8096
]
8197
},
8298
{
8399
"id": 33,
84-
"name": "Node 3.3"
100+
"name": "Node 3.3",
101+
"description": "Desc Node 3.3"
85102
},
86103
{
87104
"id": 34,
88105
"name": "Node 3.4",
106+
"description": "Desc Node 3.4",
89107
"childs":
90108
[
91109
{
92110
"id": 341,
93-
"name": "Node 3.4.1"
111+
"name": "Node 3.4.1",
112+
"description": "Desc Node 3.4.1"
94113
},
95114
{
96115
"id": 342,
97-
"name": "Node 3.4.2"
116+
"name": "Node 3.4.2",
117+
"description": "Desc Node 3.4.2"
98118
},
99119
{
100120
"id": 343,
101-
"name": "Node 3.4.3"
121+
"name": "Node 3.4.3",
122+
"description": "Desc Node 3.4.3"
102123
},
103124
{
104125
"id": 344,
105-
"name": "Node 3.4.4"
126+
"name": "Node 3.4.4",
127+
"description": "Desc Node 3.4.4"
106128
}
107129
]
108130
}

0 commit comments

Comments
 (0)