Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 1 | import {Component, Input, OnInit} from '@angular/core'; |
| 2 | |
| 3 | import {Session} from './session'; |
| 4 | import {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 | |
| 12 | export 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 Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame^] | 24 | inheritIndentation(node) { |
| 25 | let newIndent; |
| 26 | if (node['last']) { |
| 27 | newIndent = [true]; |
| 28 | } else { |
| 29 | newIndent = [false]; |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 30 | } |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame^] | 31 | |
| 32 | if (!this.indentation) { |
| 33 | return newIndent; |
| 34 | } else { |
| 35 | return this.indentation.concat(newIndent); |
| 36 | } |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 37 | } |
| 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 Krejci | 0d4c863 | 2017-11-03 13:54:21 +0100 | [diff] [blame] | 52 | 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 Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 57 | node['hasHiddenChild'] = true; |
| 58 | } else { |
Radek Krejci | 0d4c863 | 2017-11-03 13:54:21 +0100 | [diff] [blame] | 59 | /* go recursively */ |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 60 | for (let child of node['children']) { |
Radek Krejci | 0d4c863 | 2017-11-03 13:54:21 +0100 | [diff] [blame] | 61 | if (this.hasHiddenChild(child, clean)) { |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 62 | node['hasHiddenChild'] = true; |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | return node['hasHiddenChild']; |
| 68 | } |
| 69 | |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 70 | collapse(node) { |
Radek Krejci | 0d4c863 | 2017-11-03 13:54:21 +0100 | [diff] [blame] | 71 | delete node['children']; |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 72 | this.activeSession.dataVisibility = 'mixed'; |
| 73 | for (let iter of this.activeSession.data) { |
| 74 | this.hasHiddenChild(iter, true); |
| 75 | } |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame^] | 76 | 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 Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 89 | } |
| 90 | } |