7526970d3c2147ab3a70e14370caf83b9ea0634a
[vid.git] / vid-webpack-master / src / app / drawingBoard / service-planning / drawing-board-tree / drawing-board-tree.component.ts
1 import {AfterViewInit, Component, EventEmitter, Input, OnInit, Output, ViewChild,} from '@angular/core';
2 import {ContextMenuComponent, ContextMenuService} from 'ngx-contextmenu';
3 import {Constants} from '../../../shared/utils/constants';
4 import {IDType, ITreeNode} from "angular-tree-component/dist/defs/api";
5 import {TreeComponent, TreeModel, TreeNode} from "angular-tree-component";
6 import {DialogService} from "ng2-bootstrap-modal";
7 import {ActivatedRoute} from "@angular/router";
8 import {NgRedux} from "@angular-redux/store";
9 import {AppState} from "../../../shared/store/reducers";
10 import {IframeService} from "../../../shared/utils/iframe.service";
11 import {DuplicateService} from '../duplicate/duplicate.service';
12 import {DrawingBoardTreeService, TreeNodeContextMenuModel} from "./drawing-board-tree.service";
13 import {NetworkPopupService} from "../../../shared/components/genericFormPopup/genericFormServices/network/network.popup.service";
14 import {VfModulePopupService} from "../../../shared/components/genericFormPopup/genericFormServices/vfModule/vfModule.popup.service";
15 import {VnfPopupService} from "../../../shared/components/genericFormPopup/genericFormServices/vnf/vnf.popup.service";
16 import {HighlightPipe} from "../../../shared/pipes/highlight/highlight-filter.pipe";
17 import {VnfGroupPopupService} from "../../../shared/components/genericFormPopup/genericFormServices/vnfGroup/vnfGroup.popup.service";
18 import {ObjectToInstanceTreeService} from "../objectsToTree/objectToInstanceTree/objectToInstanceTree.service";
19 import {SharedTreeService} from "../objectsToTree/shared.tree.service";
20 import {Subject} from "rxjs/Subject";
21 import {changeServiceIsDirty} from "../../../shared/storeUtil/utils/service/service.actions";
22 import * as _ from 'lodash';
23 import {ErrorMsgService} from "../../../shared/components/error-msg/error-msg.service";
24 import {DragAndDropService} from "./dragAndDrop/dragAndDrop.service";
25 import {FeatureFlagsService, Features} from "../../../shared/services/featureFlag/feature-flags.service";
26 import {ComponentInfoService} from "../component-info/component-info.service";
27 import {ComponentInfoModel} from "../component-info/component-info-model";
28 import {ObjectToModelTreeService} from "../objectsToTree/objectToModelTree/objectToModelTree.service";
29 import {DrawingBoardModes} from "../drawing-board.modes";
30 import {ServiceInstanceActions} from "../../../shared/models/serviceInstanceActions";
31 import {ModalService} from "../../../shared/components/customModal/services/modal.service";
32
33 @Component({
34   selector: 'drawing-board-tree',
35   templateUrl: './drawing-board-tree.html',
36   styleUrls: ['./drawing-board-tree.scss'],
37   providers: [HighlightPipe]
38 })
39
40 export class DrawingBoardTreeComponent implements OnInit, AfterViewInit {
41   _store: NgRedux<AppState>;
42   duplicateService: DuplicateService;
43   drawingBoardTreeService: DrawingBoardTreeService;
44   objectToModelTreeService : ObjectToModelTreeService;
45   objectToInstanceTreeService : ObjectToInstanceTreeService;
46   errorMsgService: ErrorMsgService;
47   isFilterEnabled: boolean = false;
48   filterValue: string = '';
49   contextMenuOptions: TreeNodeContextMenuModel[];
50
51   @Input() pageMode : DrawingBoardModes;
52   static triggerDeleteActionService: Subject<string> = new Subject<string>();
53   static triggerUndoDeleteActionService: Subject<string> = new Subject<string>();
54   @ViewChild(ContextMenuComponent, {static: false}) public contextMenu: ContextMenuComponent;
55
56   constructor(private _contextMenuService: ContextMenuService,
57               private _iframeService: IframeService,
58               private dialogService: DialogService,
59               private store: NgRedux<AppState>,
60               private route: ActivatedRoute,
61               private _duplicateService: DuplicateService,
62               private modalService: ModalService,
63               private _drawingBoardTreeService: DrawingBoardTreeService,
64               private _networkPopupService: NetworkPopupService,
65               private _vfModulePopuopService: VfModulePopupService,
66               private _vnfPopupService: VnfPopupService,
67               private _vnfGroupPopupService: VnfGroupPopupService,
68               private _errorMsgService: ErrorMsgService,
69               private _highlightPipe: HighlightPipe,
70               private _objectToInstanceTreeService: ObjectToInstanceTreeService,
71               private _sharedTreeService: SharedTreeService,
72               private _dragAndDropService : DragAndDropService,
73               private _objectToModelTreeService : ObjectToModelTreeService,
74               private _componentInfoService: ComponentInfoService) {
75
76     this.errorMsgService = _errorMsgService;
77     this.duplicateService = _duplicateService;
78     this.drawingBoardTreeService = _drawingBoardTreeService;
79     this.contextMenuOptions = _drawingBoardTreeService.generateContextMenuOptions();
80     this.objectToModelTreeService = _objectToModelTreeService;
81     this.objectToInstanceTreeService = _objectToInstanceTreeService;
82     DrawingBoardTreeComponent.triggerDeleteActionService.subscribe((serviceModelId) => {
83       this._sharedTreeService.shouldShowDeleteInstanceWithChildrenModal(this.nodes, serviceModelId, (node, serviceModelId)=>{
84         this.drawingBoardTreeService.deleteActionService(this.nodes, serviceModelId);
85         this.store.dispatch(changeServiceIsDirty(this.nodes, serviceModelId));
86       });
87     });
88
89     DrawingBoardTreeService.triggerCheckIsDirty.subscribe((serviceModelId)=>{
90       this.store.dispatch(changeServiceIsDirty(this.nodes, serviceModelId));
91     })
92
93     DrawingBoardTreeComponent.triggerUndoDeleteActionService.subscribe((serviceModelId) => {
94       this.drawingBoardTreeService.undoDeleteActionService(this.nodes, serviceModelId);
95       this.store.dispatch(changeServiceIsDirty(this.nodes, serviceModelId));
96     });
97
98     this._store = store;
99     this.route
100       .queryParams
101       .subscribe(params => {
102         this.serviceModelId = params['serviceModelId'];
103       });
104   }
105
106   getNodeId(node: ITreeNode): string {
107     return (node.data.parentType !== "" ? (node.data.parentType + "_") : "") + node.data.typeName;
108   }
109
110   updateNodes(updateData: { nodes: any, filterValue: string }): void {
111     this.nodes = updateData.nodes;
112     this.filterValue = updateData.filterValue;
113   }
114
115   isLinkedInstance = (node) : boolean => {
116     return !_.isNil(node) && node.parentType === "VRF" || node.parentType === "VnfGroup";
117   };
118
119   @Output()
120   highlightNode: EventEmitter<number> = new EventEmitter<number>();
121
122   @ViewChild('tree', {static: false}) tree: TreeComponent;
123   missingDataTooltip: string = Constants.Error.MISSING_VNF_DETAILS;
124   currentNode: ITreeNode = null;
125   flags: any;
126   nodes = [];
127   serviceModelId: string;
128   options = {
129     allowDrag: this._dragAndDropService.checkFeatureFlag(Features.FLAG_1911_INSTANTIATION_ORDER_IN_ASYNC_ALACARTE),
130     actionMapping: {
131       mouse: {
132         drop: (tree:TreeModel, node:TreeNode, $event:any, {from, to}) => {
133           this._dragAndDropService.drop(this.store, this.serviceModelId, this.nodes, {from, to});
134         },
135         drag:(tree: TreeModel, node:TreeNode, $event:any) => {
136           this._dragAndDropService.drag(this.store,this.serviceModelId, node);
137         }
138       }
139     },
140     nodeHeight: 45,
141     dropSlotHeight: 1
142   };
143   parentElementClassName = 'content';
144
145   ngOnInit(): void {
146     this.store.subscribe(() => {
147       this.updateTree();
148     });
149     this.updateTree()
150   }
151
152   getNodeName(node: ITreeNode, filter: string) {
153       return this._highlightPipe.transform(node.data.name, filter ? filter : '');
154   }
155
156   updateTree() {
157     const serviceInstance = this.store.getState().service.serviceInstance[this.serviceModelId];
158     this.nodes = this._objectToInstanceTreeService.convertServiceInstanceToTreeData(serviceInstance, this.store.getState().service.serviceHierarchy[this.serviceModelId]).filter((item) => item !== null);
159     console.log('right nodes', this.nodes);
160
161   }
162
163
164   ngAfterViewInit(): void {
165     this.tree.treeModel.expandAll();
166   }
167
168   public onContextMenu($event: MouseEvent, node: ITreeNode): void {
169     this.flags = this.store.getState().global.flags;
170
171     this.currentNode = node;
172     node.focus();
173     node.setActiveAndVisible(false);
174     this.selectNode(node);
175     setTimeout(() => {
176       this._contextMenuService.show.next({
177         contextMenu: this.contextMenu,
178         event: <any>$event,
179         item: node,
180       });
181       $event.preventDefault();
182       $event.stopPropagation();
183     }, 250);
184
185   }
186
187
188   executeMenuAction(methodName: string): void {
189     if (!_.isNil(this.currentNode.data.menuActions) && !_.isNil(this.currentNode.data.menuActions[methodName])) {
190       this.currentNode.data.menuActions[methodName]['method'](this.currentNode, this.serviceModelId);
191       this.store.dispatch(changeServiceIsDirty(this.nodes, this.serviceModelId));
192     }
193   }
194
195   isEnabled(node: ITreeNode, serviceModelId: string, methodName: string): boolean {
196     if (!_.isNil(this.currentNode) && !_.isNil(this.currentNode.data.menuActions) && !_.isNil(this.currentNode.data.menuActions[methodName])) {
197       return this.currentNode.data.menuActions[methodName]['enable'](this.currentNode, this.serviceModelId);
198     }
199     return false;
200   }
201
202   isVisible(node: ITreeNode, methodName: string): boolean {
203     if (!_.isNil(this.currentNode) && !_.isNil(this.currentNode.data.menuActions) && !_.isNil(this.currentNode.data.menuActions[methodName])) {
204       return this.currentNode.data.menuActions[methodName]['visible'](this.currentNode, this.serviceModelId);
205     }
206     return false;
207   }
208
209
210
211   isUpgraded(node: ITreeNode): boolean {
212     return this.isLabeledAsAction(node, "Upgrade");
213   }
214
215   isDeleted(node: ITreeNode): boolean {
216     return this.isLabeledAsAction(node, "Delete");
217   }
218
219   isPaused(node: ITreeNode): boolean {
220     let isPaused = node.data.pauseInstantiation;
221     return !_.isNil(isPaused);
222   }
223
224   private isLabeledAsAction(node: ITreeNode, action) {
225     let nodeAction = node.data.action.split('_').pop();
226     if (!_.isNil(nodeAction)) {
227       return nodeAction === action;
228     }
229     return false;
230   }
231
232   public selectNode(node: ITreeNode): void {
233     node.expand();
234     this._sharedTreeService.setSelectedVNF(node);
235     this.highlightNode.emit(node.data.modelUniqueId);
236     if (FeatureFlagsService.getFlagState(Features.FLAG_1906_COMPONENT_INFO, this.store)) {
237       const serviceHierarchy = this._store.getState().service.serviceHierarchy[this.serviceModelId];
238
239       const instanceModel = this._sharedTreeService.modelByIdentifiers(
240         serviceHierarchy, node.data.modelTypeName,
241         this._sharedTreeService.modelUniqueNameOrId(node.data), node.data.modelName
242       );
243
244       const model = node.data.getModel(instanceModel);
245       const modelInfoItems = node.data.getInfo(model, node.data);
246       const componentInfoModel: ComponentInfoModel = this._sharedTreeService.addGeneralInfoItems(modelInfoItems, node.data.componentInfoType, model, node.data);
247       ComponentInfoService.triggerComponentInfoChange.next(componentInfoModel);
248     }
249   }
250
251   expandParentByNodeId(id: IDType): void {
252     this.tree.treeModel.getNodeById(id).parent.expand();
253   }
254
255   getcontextMenuOptionLabel(contextMenuOption: TreeNodeContextMenuModel): string{
256     let optionLabel = contextMenuOption.label;
257     if(contextMenuOption.label === ServiceInstanceActions.Upgrade) {
258       return optionLabel.concat(" to V" + this._store.getState().service.serviceInstance[this.serviceModelId].latestAvailableVersion);
259     }
260     return optionLabel;
261   }
262
263   isAddPositionFlagTrue() {
264     return this._sharedTreeService.isAddPositionFlagTrue();
265   }
266 }
267
268