re base code
[sdc.git] / catalog-ui / src / app / view-models / workspace / tabs / deployment / deployment-view-model.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 'use strict';
22 import {IWorkspaceViewModelScope} from "app/view-models/workspace/workspace-view-model";
23 import {ComponentFactory, MenuHandler, ChangeLifecycleStateHandler, ModalsHandler} from "app/utils";
24 import {LeftPaletteLoaderService, CacheService, SharingService} from "app/services";
25 import {Component, IAppMenu, Tab, ComponentInstance} from "app/models";
26 import {GRAPH_EVENTS} from "../../../../utils/constants";
27 import {ComponentGenericResponse} from "../../../../ng2/services/responses/component-generic-response";
28 import {EventListenerService} from "../../../../services/event-listener-service";
29 import {ComponentServiceNg2} from "../../../../ng2/services/component-services/component.service";
30
31 export interface IDeploymentViewModelScope extends IWorkspaceViewModelScope {
32
33     currentComponent:Component;
34     selectedComponent:Component;
35     isLoading:boolean;
36     sharingService:SharingService;
37     sdcMenu:IAppMenu;
38     version:string;
39     isViewOnly:boolean;
40     tabs:Array<Tab>;
41     selectedTab: Tab;
42     isComponentInstanceSelected():boolean;
43     updateSelectedComponent():void
44     openUpdateModal();
45     deleteSelectedComponentInstance():void;
46     onBackgroundClick():void;
47     setSelectedInstance(componentInstance:ComponentInstance):void;
48     printScreen():void;
49
50 }
51
52 export class DeploymentViewModel {
53
54     static '$inject' = [
55         '$scope',
56         '$templateCache',
57         'sdcMenu',
58         'MenuHandler',
59         '$state',
60         'Sdc.Services.SharingService',
61         '$filter',
62         'Sdc.Services.CacheService',
63         'ComponentFactory',
64         'ChangeLifecycleStateHandler',
65         'LeftPaletteLoaderService',
66         'ModalsHandler',
67         'EventListenerService',
68         'ComponentServiceNg2'
69     ];
70
71     constructor(private $scope:IDeploymentViewModelScope,
72                 private $templateCache:ng.ITemplateCacheService,
73                 private sdcMenu:IAppMenu,
74                 private MenuHandler:MenuHandler,
75                 private $state:ng.ui.IStateService,
76                 private sharingService:SharingService,
77                 private $filter:ng.IFilterService,
78                 private cacheService:CacheService,
79                 private ComponentFactory:ComponentFactory,
80                 private ChangeLifecycleStateHandler:ChangeLifecycleStateHandler,
81                 private LeftPaletteLoaderService:LeftPaletteLoaderService,
82                 private ModalsHandler:ModalsHandler,
83                 private eventListenerService: EventListenerService,
84                 private ComponentServiceNg2: ComponentServiceNg2) {
85
86         this.$scope.setValidState(true);
87         this.initScope();
88         this.initGraphData();
89     }
90
91
92     private initComponent = ():void => {
93
94         this.$scope.currentComponent = this.$scope.component;
95         this.$scope.selectedComponent = this.$scope.currentComponent;
96         this.updateUuidMap();
97         this.$scope.isViewOnly = this.$scope.isViewMode();
98     };
99
100
101     private updateUuidMap = ():void => {
102         /**
103          * In case user press F5, the page is refreshed and this.sharingService.currentEntity will be undefined,
104          * but after loadService or loadResource this.sharingService.currentEntity will be defined.
105          * Need to update the uuidMap with the new resource or service.
106          */
107         this.sharingService.addUuidValue(this.$scope.currentComponent.uniqueId, this.$scope.currentComponent.uuid);
108     };
109
110     private initRightTabs = ()=> {
111         if (this.$scope.currentComponent.modules) {
112             this.$templateCache.put("hierarchy-view.html", require('app/view-models/tabs/hierarchy/hierarchy-view.html'));
113             let hierarchyTab = new Tab("hierarchy-view.html", 'Sdc.ViewModels.HierarchyViewModel', 'hierarchy', this.$scope.isViewMode(), this.$scope.currentComponent, 'hierarchy');
114             this.$scope.tabs.push(hierarchyTab)
115         }
116     }
117
118     private initGraphData = ():void => {
119         if(!this.$scope.component.componentInstances || !this.$scope.component.componentInstancesRelations || !this.$scope.component.modules) {
120             this.ComponentServiceNg2.getDeploymentGraphData(this.$scope.component).subscribe((response:ComponentGenericResponse) => {
121                 this.$scope.component.componentInstances = response.componentInstances;
122                 this.$scope.component.componentInstancesRelations = response.componentInstancesRelations;
123                 this.$scope.component.modules = response.modules;
124                 this.$scope.isLoading = false;
125                 this.initComponent();
126                 this.initRightTabs();
127                 this.eventListenerService.notifyObservers(GRAPH_EVENTS.ON_DEPLOYMENT_GRAPH_DATA_LOADED);
128                 this.$scope.selectedTab = this.$scope.tabs[0];
129             });
130         } else {
131             this.$scope.isLoading = false;
132             this.initRightTabs();
133             this.eventListenerService.notifyObservers(GRAPH_EVENTS.ON_DEPLOYMENT_GRAPH_DATA_LOADED);
134
135         }
136     };
137
138     private initScope = ():void => {
139         this.$scope.isLoading = true;
140         this.$scope.sharingService = this.sharingService;
141         this.$scope.sdcMenu = this.sdcMenu;
142         this.$scope.version = this.cacheService.get('version');
143         this.initComponent();
144         this.$scope.tabs = Array<Tab>();
145     }
146 }