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 | |
| 24 | getSubtree(node, all: boolean) { |
| 25 | this.sessionsService.rpcGetSubtree(this.activeSession.key, all, node['path']).subscribe(result => { |
| 26 | if (result['success']) { |
| 27 | node['children'] = result['data']['children']; |
| 28 | } |
| 29 | }); |
| 30 | } |
| 31 | |
| 32 | getType(object) { |
| 33 | let result = 'data'; |
| 34 | if (typeof object == 'object') { |
| 35 | if (object instanceof Array) { |
| 36 | result = 'array'; |
| 37 | } else { |
| 38 | result = 'object'; |
| 39 | } |
| 40 | } |
| 41 | return result; |
| 42 | } |
| 43 | |
| 44 | expandable(node): boolean { |
| 45 | if (node['info']['type'] == 1 || /* container */ |
| 46 | node['info']['type'] == 16) { /* list */ |
| 47 | return true; |
| 48 | } |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | hasHiddenChild(node, clean=false): boolean { |
| 53 | if (!clean && 'hasHiddenChild' in node) { |
| 54 | return node['hasHiddenChild']; |
| 55 | } |
| 56 | node['hasHiddenChild'] = false; |
| 57 | if (!node['children']) { |
| 58 | node['hasHiddenChild'] = true; |
| 59 | } else { |
| 60 | for (let child of node['children']) { |
| 61 | if (!('children' in child) || this.hasHiddenChild(child, clean)) { |
| 62 | node['hasHiddenChild'] = true; |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | return node['hasHiddenChild']; |
| 68 | } |
| 69 | |
| 70 | inheritIndentation(node) { |
| 71 | let newIndent; |
| 72 | if (node['last']) { |
| 73 | newIndent = [true]; |
| 74 | } else { |
| 75 | newIndent = [false]; |
| 76 | } |
| 77 | |
| 78 | if (!this.indentation) { |
| 79 | return newIndent; |
| 80 | } else { |
| 81 | return this.indentation.concat(newIndent); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | collapse(node) { |
| 86 | node['children'] = null; |
| 87 | this.activeSession.dataVisibility = 'mixed'; |
| 88 | for (let iter of this.activeSession.data) { |
| 89 | this.hasHiddenChild(iter, true); |
| 90 | } |
| 91 | } |
| 92 | } |