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