| import {Component, Input, OnInit, ChangeDetectorRef} from '@angular/core'; |
| |
| import {Session} from './session'; |
| import {SessionsService} from './sessions.service'; |
| |
| @Component({ |
| selector: 'tree-view', |
| templateUrl: './tree.component.html', |
| styleUrls: ['./tree.component.scss'] |
| }) |
| |
| export class TreeView implements OnInit { |
| @Input() treeData; |
| @Input() indentation; |
| c = 1; i = 1; |
| activeSession: Session; |
| constructor(private sessionsService: SessionsService, private changeDetector: ChangeDetectorRef) {} |
| |
| ngOnInit(): void { |
| this.activeSession = this.sessionsService.getActiveSession(); |
| } |
| |
| inheritIndentation(node) { |
| let newIndent; |
| if (node['last']) { |
| newIndent = [true]; |
| } else { |
| newIndent = [false]; |
| } |
| |
| if (!this.indentation) { |
| return newIndent; |
| } else { |
| return this.indentation.concat(newIndent); |
| } |
| } |
| |
| startEditing(node, target) { |
| let parent = target.parentElement; |
| |
| node['edit'] = true; |
| this.changeDetector.detectChanges(); |
| |
| parent.nextElementSibling.lastElementChild.focus(); |
| } |
| |
| checkValue(node, target) { |
| let confirm = target.previousElementSibling; |
| this.sessionsService.checkValue(this.activeSession.key, node['path'], target.value).subscribe(result => { |
| if (result['success']) { |
| target.classList.remove("invalid"); |
| confirm.style.visibility = "visible"; |
| } else { |
| target.classList.add("invalid"); |
| confirm.style.visibility = "hidden"; |
| } |
| }); |
| } |
| |
| changeValue(node, target) { |
| let input; |
| if (target.classList.contains('value')) { |
| if (target.classList.contains('invalid')) { |
| return; |
| } |
| input = target; |
| } else { |
| input = target.nextElementSibling; |
| } |
| |
| if (!this.activeSession.modifications) { |
| this.activeSession.modifications = {}; |
| } |
| if (!(node['path'] in this.activeSession.modifications)) { |
| /* new record */ |
| if (node['value'] == input['value']) { |
| /* no change to the original value */ |
| return; |
| } |
| this.activeSession.modifications[node['path']] = {}; |
| this.activeSession.modifications[node['path']]['type'] = 'change'; |
| this.activeSession.modifications[node['path']]['original'] = node['value']; |
| this.activeSession.modifications[node['path']]['value'] = input.value; |
| node['dirty'] = true; |
| } else if (this.activeSession.modifications[node['path']]['type'] == 'change' && |
| this.activeSession.modifications[node['path']]['original'] == input['value']) { |
| /* change to the original value, remove the change record */ |
| delete this.activeSession.modifications[node['path']]; |
| node['dirty'] = false; |
| |
| if (!Object.keys(this.activeSession.modifications).length) { |
| delete this.activeSession.modifications; |
| } |
| } else { |
| /* another change of existing change record */ |
| this.activeSession.modifications[node['path']]['value'] = input.value; |
| node['dirty'] = true; |
| } |
| console.log('Modifications list: ' + this.activeSession.modifications); |
| |
| node['value'] = input.value; |
| node['edit'] = false; |
| this.sessionsService.storeData(); |
| } |
| |
| expandable(node): boolean { |
| if (node['info']['type'] == 1 || /* container */ |
| node['info']['type'] == 16) { /* list */ |
| return true; |
| } |
| return false; |
| } |
| |
| hasHiddenChild(node, clean=false): boolean { |
| if (!clean && 'hasHiddenChild' in node) { |
| return node['hasHiddenChild']; |
| } |
| node['hasHiddenChild'] = false; |
| if (!this.expandable(node)) { |
| /* terminal node (leaf or leaf-list) */ |
| return node['hasHiddenChild']; |
| } else if (!('children' in node)) { |
| /* internal node without children */ |
| node['hasHiddenChild'] = true; |
| } else { |
| /* go recursively */ |
| for (let child of node['children']) { |
| if (this.hasHiddenChild(child, clean)) { |
| node['hasHiddenChild'] = true; |
| break; |
| } |
| } |
| } |
| return node['hasHiddenChild']; |
| } |
| |
| collapse(node) { |
| delete node['children']; |
| this.activeSession.dataVisibility = 'mixed'; |
| for (let iter of this.activeSession.data) { |
| this.hasHiddenChild(iter, true); |
| } |
| this.sessionsService.storeData(); |
| } |
| |
| expand(node, all: boolean) { |
| this.sessionsService.rpcGetSubtree(this.activeSession.key, all, node['path']).subscribe(result => { |
| if (result['success']) { |
| node['children'] = result['data']['children']; |
| for (let iter of this.activeSession.data) { |
| this.hasHiddenChild(iter, true); |
| } |
| this.sessionsService.storeData(); |
| } |
| }); |
| } |
| } |