Merge "PombaReqest and ServiceInstance improvements"
[vid.git] / vid-webpack-master / src / app / drawingBoard / drawing-board-tree / drawing-board-tree.component.ts
1 import {AfterViewInit, Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
2 import { ContextMenuService } from 'ngx-contextmenu';
3 import { Constants } from '../../shared/utils/constants';
4 import {ServicePlanningService} from "../../services/service-planning.service";
5 import {ITreeNode} from "angular-tree-component/dist/defs/api";
6 import {ITreeOptions, TreeComponent} from "angular-tree-component";
7 import {VnfPopupComponent} from "../../components/vnf-popup/vnf-popup.components";
8 import {DialogService} from "ng2-bootstrap-modal";
9 import {ActivatedRoute} from "@angular/router";
10 import {NgRedux} from "@angular-redux/store";
11 import {AppState} from "../../store/reducers";
12 import { MessageBoxData, ModalSize, ModalType } from '../../shared/components/messageBox/messageBox.data';
13 import { MessageBoxService } from '../../shared/components/messageBox/messageBox.service';
14 import { deleteVnfInstance, deleteVfModuleInstance } from '../../service.actions';
15 import { isNullOrUndefined } from 'util';
16 import {IframeService} from "../../shared/utils/iframe.service";
17
18
19 @Component({
20   selector: 'drawing-board-tree',
21   templateUrl: './drawing-board-tree.html',
22   styleUrls : ['./drawing-board-tree.scss']
23 })
24
25
26 export class DrawingBoardTreeComponent implements OnInit, AfterViewInit {
27   constructor(private _contextMenuService: ContextMenuService,
28               private _servicePlanningService: ServicePlanningService,
29               private _iframeService : IframeService,
30               private dialogService: DialogService,
31               private store: NgRedux<AppState>,
32               private route: ActivatedRoute) {
33     this.route
34       .queryParams
35       .subscribe(params => {
36         this.serviceModelId = params['serviceModelId'];
37       });
38   }
39
40   @Output()
41   highlightNode : EventEmitter<number> = new EventEmitter<number>();
42
43   @ViewChild('tree') tree: TreeComponent;
44   missingDataTooltip: string = Constants.Error.MISSING_VNF_DETAILS;
45   currentNode: ITreeNode = null; //
46   nodes = [];
47   serviceModelId: string;
48   options: ITreeOptions = {
49     nodeHeight: 45,
50     dropSlotHeight: 1
51   };
52   parentElementClassName = 'content';
53
54   ngOnInit(): void {
55     this.store.subscribe(() => {this.updateTree()});
56     this.updateTree()
57   }
58
59   updateTree() {
60     const serviceInstance = this.store.getState().service.serviceInstance[this.serviceModelId];
61     this.nodes = this._servicePlanningService.convertServiceInstanceToTreeData(serviceInstance, this.serviceModelId);
62   }
63
64   ngAfterViewInit():void {
65     // Expand drawing tree on init.
66     this.tree.treeModel.expandAll();
67   }
68
69   public onContextMenu($event: MouseEvent, node: ITreeNode): void {
70     this.currentNode = node;
71     node.focus();
72     node.setActiveAndVisible(false);
73     this.selectNode(node);
74     this._contextMenuService.show.next({
75       event: <any>$event,
76       item: node,
77     });
78     $event.preventDefault();
79     $event.stopPropagation();
80   }
81
82   public editItem(node: ITreeNode): void {
83     node = this.currentNode;
84     this._iframeService.addClassOpenModal(this.parentElementClassName);
85     this.dialogService.addDialog(VnfPopupComponent, {
86       serviceModelId: this.serviceModelId,
87       modelName: node.data.modelName,
88       modelType: node.data.type,
89       parentModelName: node.parent.data.modelName,
90       isNewVfModule : false
91     })
92   }
93
94   public deleteItem(node: ITreeNode): void {
95     if(this.currentNode.data.type === 'VF'){
96       if(!isNullOrUndefined(this.currentNode.data.children) && this.currentNode.data.children.length === 0){
97         this.store.dispatch(deleteVnfInstance(this.currentNode.data.modelName, this.serviceModelId));
98       }else {
99         let messageBoxData : MessageBoxData = new MessageBoxData(
100           "Remove VNF",  // modal title
101           "You are about to remove this VNF and all its children from this service. Are you sure you want to remove it?",
102
103           ModalType.alert,
104           ModalSize.medium,
105           [
106             {text:"Remove VNF", size:"large",  callback: this.removeVnf.bind(this), closeModal:true},
107             {text:"Don’t Remove", size:"medium", closeModal:true}
108           ]);
109
110         MessageBoxService.openModal.next(messageBoxData);
111       }
112     }else {
113         this.store.dispatch(deleteVfModuleInstance(this.currentNode.data.modelName, this.serviceModelId, node.parent.data.modelName));
114     }
115   }
116
117   removeVnf() {
118     this.store.dispatch(deleteVnfInstance(this.currentNode.data.modelName,  this.serviceModelId));
119   }
120
121   public selectNode(node: ITreeNode): void {
122     node.expand();
123     this.highlightNode.emit(node.data.modelId);
124   }
125
126   isDataMissing(node: ITreeNode) {
127     //todo: currently not showing the alert icon. will be implemented in upcoming story.
128     return false;
129   }
130
131 }
132
133