blob: f57a88cc468abbd25ac165afe9717eaa13a1af90 [file] [log] [blame]
Radek Krejcia1339602017-11-02 13:52:38 +01001import {Component, Input, OnInit} from '@angular/core';
2
3import {Session} from './session';
4import {SessionsService} from './sessions.service';
5
6@Component({
7 selector: 'tree-view',
8 templateUrl: './tree.component.html',
9 styleUrls: ['../netopeer.css', './tree.component.css']
10})
11
12export class TreeView implements OnInit {
13 @Input() treeData;
14 @Input() indentation;
15 c = 1; i = 1;
16 activeSession: Session;
17 objectKeys = Object.keys;
18 constructor(private sessionsService: SessionsService) {}
19
20 ngOnInit(): void {
21 this.activeSession = this.sessionsService.getActiveSession(this.sessionsService.activeSession);
22 }
23
Radek Krejci77f77202017-11-03 15:33:50 +010024 inheritIndentation(node) {
25 let newIndent;
26 if (node['last']) {
27 newIndent = [true];
28 } else {
29 newIndent = [false];
Radek Krejcia1339602017-11-02 13:52:38 +010030 }
Radek Krejci77f77202017-11-03 15:33:50 +010031
32 if (!this.indentation) {
33 return newIndent;
34 } else {
35 return this.indentation.concat(newIndent);
36 }
Radek Krejcia1339602017-11-02 13:52:38 +010037 }
38
39 expandable(node): boolean {
40 if (node['info']['type'] == 1 || /* container */
41 node['info']['type'] == 16) { /* list */
42 return true;
43 }
44 return false;
45 }
46
47 hasHiddenChild(node, clean=false): boolean {
48 if (!clean && 'hasHiddenChild' in node) {
49 return node['hasHiddenChild'];
50 }
51 node['hasHiddenChild'] = false;
Radek Krejci0d4c8632017-11-03 13:54:21 +010052 if (!this.expandable(node)) {
53 /* terminal node (leaf or leaf-list) */
54 return node['hasHiddenChild'];
55 } else if (!('children' in node)) {
56 /* internal node without children */
Radek Krejcia1339602017-11-02 13:52:38 +010057 node['hasHiddenChild'] = true;
58 } else {
Radek Krejci0d4c8632017-11-03 13:54:21 +010059 /* go recursively */
Radek Krejcia1339602017-11-02 13:52:38 +010060 for (let child of node['children']) {
Radek Krejci0d4c8632017-11-03 13:54:21 +010061 if (this.hasHiddenChild(child, clean)) {
Radek Krejcia1339602017-11-02 13:52:38 +010062 node['hasHiddenChild'] = true;
63 break;
64 }
65 }
66 }
67 return node['hasHiddenChild'];
68 }
69
Radek Krejcia1339602017-11-02 13:52:38 +010070 collapse(node) {
Radek Krejci0d4c8632017-11-03 13:54:21 +010071 delete node['children'];
Radek Krejcia1339602017-11-02 13:52:38 +010072 this.activeSession.dataVisibility = 'mixed';
73 for (let iter of this.activeSession.data) {
74 this.hasHiddenChild(iter, true);
75 }
Radek Krejci77f77202017-11-03 15:33:50 +010076 this.sessionsService.storeData();
77 }
78
79 expand(node, all: boolean) {
80 this.sessionsService.rpcGetSubtree(this.activeSession.key, all, node['path']).subscribe(result => {
81 if (result['success']) {
82 node['children'] = result['data']['children'];
83 for (let iter of this.activeSession.data) {
84 this.hasHiddenChild(iter, true);
85 }
86 this.sessionsService.storeData();
87 }
88 });
Radek Krejcia1339602017-11-02 13:52:38 +010089 }
90}