[SDC] rebase 1710 code
[sdc.git] / catalog-ui / src / app / view-models / workspace / tabs / composition / composition-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 'use strict';
21 import {Component, ComponentInstance, IAppMenu} from "app/models";
22 import {SharingService, CacheService, EventListenerService, LeftPaletteLoaderService} from "app/services";
23 import {ModalsHandler, GRAPH_EVENTS, ComponentFactory, ChangeLifecycleStateHandler, MenuHandler, EVENTS} from "app/utils";
24 import {IWorkspaceViewModelScope} from "../../workspace-view-model";
25 import {ComponentServiceNg2} from "app/ng2/services/component-services/component.service";
26 import {ComponentGenericResponse} from "app/ng2/services/responses/component-generic-response";
27 import {Resource} from "app/models/components/resource";
28 import {ResourceType} from "../../../../utils/constants";
29
30 export interface ICompositionViewModelScope extends IWorkspaceViewModelScope {
31
32     currentComponent:Component;
33     selectedComponent:Component;
34     isLoading:boolean;
35     graphApi:any;
36     sharingService:SharingService;
37     sdcMenu:IAppMenu;
38     version:string;
39     isViewOnly:boolean;
40     isLoadingRightPanel:boolean;
41     onComponentInstanceVersionChange(component:Component);
42     isComponentInstanceSelected():boolean;
43     updateSelectedComponent():void
44     openUpdateModal();
45     deleteSelectedComponentInstance():void;
46     onBackgroundClick():void;
47     setSelectedInstance(componentInstance:ComponentInstance):void;
48     printScreen():void;
49
50     cacheComponentsInstancesFullData:Component;
51 }
52
53 export class CompositionViewModel {
54
55     static '$inject' = [
56         '$scope',
57         '$log',
58         'sdcMenu',
59         'MenuHandler',
60         '$uibModal',
61         '$state',
62         'Sdc.Services.SharingService',
63         '$filter',
64         'Sdc.Services.CacheService',
65         'ComponentFactory',
66         'ChangeLifecycleStateHandler',
67         'LeftPaletteLoaderService',
68         'ModalsHandler',
69         'EventListenerService',
70         'ComponentServiceNg2'
71     ];
72
73     constructor(private $scope:ICompositionViewModelScope,
74                 private $log:ng.ILogService,
75                 private sdcMenu:IAppMenu,
76                 private MenuHandler:MenuHandler,
77                 private $uibModal:ng.ui.bootstrap.IModalService,
78                 private $state:ng.ui.IStateService,
79                 private sharingService:SharingService,
80                 private $filter:ng.IFilterService,
81                 private cacheService:CacheService,
82                 private ComponentFactory:ComponentFactory,
83                 private ChangeLifecycleStateHandler:ChangeLifecycleStateHandler,
84                 private LeftPaletteLoaderService:LeftPaletteLoaderService,
85                 private ModalsHandler:ModalsHandler,
86                 private eventListenerService:EventListenerService,
87                 private ComponentServiceNg2: ComponentServiceNg2) {
88
89         this.$scope.setValidState(true);
90         this.initScope();
91         this.initGraphData();
92         this.$scope.updateSelectedMenuItem();
93         this.registerGraphEvents(this.$scope);
94     }
95
96
97     private initGraphData = ():void => {
98         if(!this.$scope.component.componentInstances || !this.$scope.component.componentInstancesRelations ) {
99             this.$scope.isLoading = true;
100             this.ComponentServiceNg2.getComponentInstancesAndRelation(this.$scope.component).subscribe((response:ComponentGenericResponse) => {
101                 this.$scope.component.componentInstances = response.componentInstances;
102                 this.$scope.component.componentInstancesRelations = response.componentInstancesRelations;
103                 this.$scope.isLoading = false;
104                 this.initComponent();
105                 this.eventListenerService.notifyObservers(GRAPH_EVENTS.ON_COMPOSITION_GRAPH_DATA_LOADED);
106             });
107         } else {
108             this.eventListenerService.notifyObservers(GRAPH_EVENTS.ON_COMPOSITION_GRAPH_DATA_LOADED);
109         }
110         this.eventListenerService.unRegisterObserver(GRAPH_EVENTS.ON_COMPOSITION_GRAPH_DATA_LOADED);
111     };
112
113
114     private cacheComponentsInstancesFullData:Array<Component>;
115
116     private initComponent = ():void => {
117
118         this.$scope.currentComponent = this.$scope.component;
119         this.$scope.selectedComponent = this.$scope.currentComponent;
120         this.updateUuidMap();
121         this.$scope.isViewOnly = this.$scope.isViewMode();
122     };
123     private registerGraphEvents = (scope:ICompositionViewModelScope):void => {
124
125         this.eventListenerService.registerObserverCallback(GRAPH_EVENTS.ON_NODE_SELECTED, scope.setSelectedInstance);
126         this.eventListenerService.registerObserverCallback(GRAPH_EVENTS.ON_GRAPH_BACKGROUND_CLICKED, scope.onBackgroundClick);
127
128     };
129
130     private openUpdateComponentInstanceNameModal = ():void => {
131         this.ModalsHandler.openUpdateComponentInstanceNameModal(this.$scope.currentComponent).then(()=> {
132             this.eventListenerService.notifyObservers(GRAPH_EVENTS.ON_COMPONENT_INSTANCE_NAME_CHANGED, this.$scope.currentComponent.selectedInstance);
133
134         });
135     };
136
137     private removeSelectedComponentInstance = ():void => {
138         this.eventListenerService.notifyObservers(GRAPH_EVENTS.ON_DELETE_MULTIPLE_COMPONENTS);
139         this.$scope.currentComponent.selectedInstance = null;
140         this.$scope.selectedComponent = this.$scope.currentComponent;
141     };
142
143     private updateUuidMap = ():void => {
144         /**
145          * In case user press F5, the page is refreshed and this.sharingService.currentEntity will be undefined,
146          * but after loadService or loadResource this.sharingService.currentEntity will be defined.
147          * Need to update the uuidMap with the new resource or service.
148          */
149         this.sharingService.addUuidValue(this.$scope.currentComponent.uniqueId, this.$scope.currentComponent.uuid);
150     };
151
152     private initScope = ():void => {
153
154         this.$scope.sharingService = this.sharingService;
155         this.$scope.sdcMenu = this.sdcMenu;
156         this.$scope.isLoading = false;
157         this.$scope.isLoadingRightPanel = false;
158         this.$scope.graphApi = {};
159         this.$scope.version = this.cacheService.get('version');
160         this.initComponent();
161
162         this.cacheComponentsInstancesFullData = new Array<Component>();
163
164         this.$scope.isComponentInstanceSelected = ():boolean => {
165             return this.$scope.currentComponent && this.$scope.currentComponent.selectedInstance != undefined && this.$scope.currentComponent.selectedInstance != null;
166         };
167
168         this.$scope.updateSelectedComponent = ():void => {
169             if (this.$scope.currentComponent.selectedInstance) {
170
171                 let componentParent = _.find(this.cacheComponentsInstancesFullData, (component) => {
172                     return component.uniqueId === this.$scope.currentComponent.selectedInstance.componentUid;
173                 });
174                 if (componentParent) {
175                     this.$scope.selectedComponent = componentParent;
176                 }
177                 else {
178                     try {
179                         let onSuccess = (component:Component) => {
180                             this.$scope.isLoadingRightPanel = false;
181                             this.$scope.selectedComponent = component;
182                             this.cacheComponentsInstancesFullData.push(component);
183                         };
184                         let onError = (component:Component) => {
185                             console.log("Error updating selected component");
186                             this.$scope.isLoadingRightPanel = false;
187                         };
188                         this.ComponentFactory.getComponentFromServer(this.$scope.currentComponent.selectedInstance.originType, this.$scope.currentComponent.selectedInstance.componentUid).then(onSuccess, onError);
189                     } catch (e) {
190                         console.log("Error updating selected component", e);
191                         this.$scope.isLoadingRightPanel = false;
192                     }
193                 }
194             }
195             else {
196
197                 this.$scope.selectedComponent = this.$scope.currentComponent;
198             }
199         };
200
201         this.$scope.setSelectedInstance = (selectedComponent:ComponentInstance):void => {
202
203             this.$log.debug('composition-view-model::onNodeSelected:: with id: ' + selectedComponent.uniqueId);
204             this.$scope.currentComponent.setSelectedInstance(selectedComponent);
205             this.$scope.updateSelectedComponent();
206
207             if (this.$state.current.name === 'workspace.composition.api') {
208                 this.$state.go('workspace.composition.details');
209             }
210         };
211
212         this.$scope.onBackgroundClick = ():void => {
213             this.$scope.currentComponent.selectedInstance = null;
214             this.$scope.selectedComponent = this.$scope.currentComponent;
215
216             if (this.$state.current.name === 'workspace.composition.api') {
217                 this.$state.go('workspace.composition.details');
218             }
219
220             if(this.$scope.selectedComponent.isService() && this.$state.current.name === 'workspace.composition.relations'){
221                 this.$state.go('workspace.composition.api');
222             }
223         };
224
225         this.$scope.openUpdateModal = ():void => {
226             this.openUpdateComponentInstanceNameModal();
227         };
228
229         this.$scope.deleteSelectedComponentInstance = ():void => {
230             let state = "deleteInstance";
231             let onOk = ():void => {
232                 this.removeSelectedComponentInstance();
233                 //this.$scope.graphApi.deleteSelectedNodes();
234             };
235             let title:string = this.$scope.sdcMenu.alertMessages[state].title;
236             let message:string = this.$scope.sdcMenu.alertMessages[state].message.format([this.$scope.currentComponent.selectedInstance.name]);
237             this.ModalsHandler.openAlertModal(title, message).then(onOk);
238         };
239
240         this.$scope.onComponentInstanceVersionChange = (component:Component):void => {
241             this.$scope.currentComponent = component;
242             this.$scope.setComponent(this.$scope.currentComponent);
243             this.$scope.updateSelectedComponent();
244         };
245         
246
247         this.eventListenerService.registerObserverCallback(EVENTS.ON_CHECKOUT, this.$scope.reload);
248
249     }
250 }