2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
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";
30 export interface ICompositionViewModelScope extends IWorkspaceViewModelScope {
32 currentComponent:Component;
33 selectedComponent:Component;
36 sharingService:SharingService;
40 isLoadingRightPanel:boolean;
41 onComponentInstanceVersionChange(component:Component);
42 isComponentInstanceSelected():boolean;
43 updateSelectedComponent():void
45 deleteSelectedComponentInstance():void;
46 onBackgroundClick():void;
47 setSelectedInstance(componentInstance:ComponentInstance):void;
50 cacheComponentsInstancesFullData:Component;
53 export class CompositionViewModel {
62 'Sdc.Services.SharingService',
64 'Sdc.Services.CacheService',
66 'ChangeLifecycleStateHandler',
67 'LeftPaletteLoaderService',
69 'EventListenerService',
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) {
89 this.$scope.setValidState(true);
92 this.$scope.updateSelectedMenuItem();
93 this.registerGraphEvents(this.$scope);
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);
108 this.eventListenerService.notifyObservers(GRAPH_EVENTS.ON_COMPOSITION_GRAPH_DATA_LOADED);
110 this.eventListenerService.unRegisterObserver(GRAPH_EVENTS.ON_COMPOSITION_GRAPH_DATA_LOADED);
114 private cacheComponentsInstancesFullData:Array<Component>;
116 private initComponent = ():void => {
118 this.$scope.currentComponent = this.$scope.component;
119 this.$scope.selectedComponent = this.$scope.currentComponent;
120 this.updateUuidMap();
121 this.$scope.isViewOnly = this.$scope.isViewMode();
123 private registerGraphEvents = (scope:ICompositionViewModelScope):void => {
125 this.eventListenerService.registerObserverCallback(GRAPH_EVENTS.ON_NODE_SELECTED, scope.setSelectedInstance);
126 this.eventListenerService.registerObserverCallback(GRAPH_EVENTS.ON_GRAPH_BACKGROUND_CLICKED, scope.onBackgroundClick);
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);
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;
143 private updateUuidMap = ():void => {
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.
149 this.sharingService.addUuidValue(this.$scope.currentComponent.uniqueId, this.$scope.currentComponent.uuid);
152 private initScope = ():void => {
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();
162 this.cacheComponentsInstancesFullData = new Array<Component>();
164 this.$scope.isComponentInstanceSelected = ():boolean => {
165 return this.$scope.currentComponent && this.$scope.currentComponent.selectedInstance != undefined && this.$scope.currentComponent.selectedInstance != null;
168 this.$scope.updateSelectedComponent = ():void => {
169 if (this.$scope.currentComponent.selectedInstance) {
171 let componentParent = _.find(this.cacheComponentsInstancesFullData, (component) => {
172 return component.uniqueId === this.$scope.currentComponent.selectedInstance.componentUid;
174 if (componentParent) {
175 this.$scope.selectedComponent = componentParent;
179 let onSuccess = (component:Component) => {
180 this.$scope.isLoadingRightPanel = false;
181 this.$scope.selectedComponent = component;
182 this.cacheComponentsInstancesFullData.push(component);
184 let onError = (component:Component) => {
185 console.log("Error updating selected component");
186 this.$scope.isLoadingRightPanel = false;
188 this.ComponentFactory.getComponentFromServer(this.$scope.currentComponent.selectedInstance.originType, this.$scope.currentComponent.selectedInstance.componentUid).then(onSuccess, onError);
190 console.log("Error updating selected component", e);
191 this.$scope.isLoadingRightPanel = false;
197 this.$scope.selectedComponent = this.$scope.currentComponent;
201 this.$scope.setSelectedInstance = (selectedComponent:ComponentInstance):void => {
203 this.$log.debug('composition-view-model::onNodeSelected:: with id: ' + selectedComponent.uniqueId);
204 this.$scope.currentComponent.setSelectedInstance(selectedComponent);
205 this.$scope.updateSelectedComponent();
207 if (this.$state.current.name === 'workspace.composition.api') {
208 this.$state.go('workspace.composition.details');
212 this.$scope.onBackgroundClick = ():void => {
213 this.$scope.currentComponent.selectedInstance = null;
214 this.$scope.selectedComponent = this.$scope.currentComponent;
216 if (this.$state.current.name === 'workspace.composition.api') {
217 this.$state.go('workspace.composition.details');
220 if(this.$scope.selectedComponent.isService() && this.$state.current.name === 'workspace.composition.relations'){
221 this.$state.go('workspace.composition.api');
225 this.$scope.openUpdateModal = ():void => {
226 this.openUpdateComponentInstanceNameModal();
229 this.$scope.deleteSelectedComponentInstance = ():void => {
230 let state = "deleteInstance";
231 let onOk = ():void => {
232 this.removeSelectedComponentInstance();
233 //this.$scope.graphApi.deleteSelectedNodes();
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);
240 this.$scope.onComponentInstanceVersionChange = (component:Component):void => {
241 this.$scope.currentComponent = component;
242 this.$scope.setComponent(this.$scope.currentComponent);
243 this.$scope.updateSelectedComponent();
247 this.eventListenerService.registerObserverCallback(EVENTS.ON_CHECKOUT, this.$scope.reload);