Skip to content

Commit d171504

Browse files
author
Georgii Rychko
committed
feat(node menu): implement 'Add node' menu item
So basically this commit provides implementation of adding new node to the exitsting tree. Though there is left lots of work to do.
1 parent aa0e651 commit d171504

3 files changed

Lines changed: 52 additions & 44 deletions

File tree

components/editable-node.directive.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import {Ng2TreeService} from './ng2-tree.service';
77
export class EditableNodeDirective implements OnInit {
88
@Input()
99
nodeValue: string;
10-
10+
1111
@Output()
1212
valueChanged: EventEmitter<any> = new EventEmitter(false);
1313

1414
private element: any;
1515
private treeService: Ng2TreeService;
16-
16+
1717
constructor(elementRef: ElementRef, treeService: Ng2TreeService) {
1818
this.element = elementRef;
1919
this.treeService = treeService;

components/ng2-tree.component.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {EditableNodeDirective} from './editable-node.directive.ts';
1616
<div class="node-menu" *ngIf="isMenuVisible">
1717
<ul class="node-menu-content">
1818
<li (click)="rename($event, tree)">Rename node</li>
19-
<li>Add node</li>
19+
<li (click)="addNode($event, tree)">Add node</li>
2020
<li (click)="remove($event, tree)">Remove node</li>
2121
</ul>
2222
</div>
@@ -88,6 +88,16 @@ export class Ng2Tree implements OnInit {
8888
}
8989
}
9090

91+
92+
private addNode($event: any, node: any) {
93+
if ($event.which === 1) {
94+
if (!node.children || !node.children.push) {
95+
node.children = [];
96+
}
97+
node.children.push({value: '', status: 'new'});
98+
}
99+
}
100+
91101
private rename($event: any, node: any) {
92102
if ($event.which === 1) {
93103
this.edit = true;
@@ -112,36 +122,46 @@ export class Ng2Tree implements OnInit {
112122
if (!this.previousEvent) {
113123
this.previousEvent = $event.type;
114124
}
115-
125+
116126
if (this.previousEvent === 'keyup' && $event.type === 'blur') {
117127
this.previousEvent = $event.type;
118128
return;
119129
}
120-
130+
131+
if (!$event.value) {
132+
return this.treeService.emitRemoveEvent({node});
133+
}
134+
121135
this.previousEvent = $event.type;
122136
node.value = $event.value;
123137
this.edit = false;
124138
}
125139

126140
ngOnInit(): void {
141+
if (!this.tree) return;
142+
143+
if (this.tree.status === 'new') {
144+
this.edit = true;
145+
}
146+
127147
this.treeService.menuEventStream()
128148
.subscribe(menuEvent => {
129149
if (menuEvent.sender !== this && menuEvent.action === 'close') {
130150
this.isMenuVisible = false;
131151
}
132-
})
133-
152+
});
153+
134154
this.treeService.removeNodeEventStream()
135155
.subscribe(removeEvent => {
136156
if (!this.tree || !this.tree.children) return;
137157
for (let i = 0; i < this.tree.children.length; i++) {
138158
const child = this.tree.children[i];
139159
if (child === removeEvent.node) {
140160
delete this.tree.children[i];
141-
161+
142162
break;
143163
}
144164
}
145-
})
165+
});
146166
}
147167
}

components/ng2-tree.service.ts

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,34 @@ import {Observable} from 'rxjs/Observable';
33

44
@Injectable()
55
export class Ng2TreeService {
6-
private menuEvents$:EventEmitter<any> = new EventEmitter();
7-
private cancelEvents$:EventEmitter<any> = new EventEmitter();
8-
private removeEvents$:EventEmitter<any> = new EventEmitter();
9-
public root: any;
10-
11-
constructor() {
12-
window.addEventListener('keyup', (event: any) => {
6+
private menuEvents$: EventEmitter<any> = new EventEmitter();
7+
private removeEvents$: EventEmitter<any> = new EventEmitter();
8+
9+
constructor() {
10+
window.addEventListener('keyup', (event: any) => {
1311
if (event.keyCode === 27) {
1412
this.emitMenuEvent({sender: null, action: 'close'});
1513
}
1614
});
17-
18-
15+
1916
window.addEventListener('click', (event: any) => {
20-
this.emitMenuEvent({sender: null, action: 'close'});
17+
this.emitMenuEvent({sender: null, action: 'close'});
2118
});
22-
}
23-
24-
menuEventStream(): Observable<any> {
25-
return this.menuEvents$;
26-
}
27-
28-
emitMenuEvent(event: any): void {
29-
this.menuEvents$.emit(event);
30-
}
31-
32-
33-
removeNodeEventStream(): Observable<any> {
34-
return this.removeEvents$;
35-
}
36-
37-
emitRemoveEvent(event: any): void {
38-
this.removeEvents$.emit(event);
39-
}
19+
}
20+
21+
menuEventStream(): Observable<any> {
22+
return this.menuEvents$;
23+
}
24+
25+
emitMenuEvent(event: any): void {
26+
this.menuEvents$.emit(event);
27+
}
28+
29+
removeNodeEventStream(): Observable<any> {
30+
return this.removeEvents$;
31+
}
4032

41-
cancelEventStream(): Observable<any> {
42-
return this.cancelEvents$;
43-
}
44-
45-
emitCancelEvent(event: any): void {
46-
this.cancelEvents$.emit(event);
47-
}
33+
emitRemoveEvent(event: any): void {
34+
this.removeEvents$.emit(event);
35+
}
4836
}

0 commit comments

Comments
 (0)