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, 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";
28 export interface ICompositionViewModelScope extends IWorkspaceViewModelScope {
30 currentComponent:Component;
31 selectedComponent:Component;
34 sharingService:SharingService;
38 isLoadingRightPanel:boolean;
39 onComponentInstanceVersionChange(component:Component);
40 isComponentInstanceSelected():boolean;
41 updateSelectedComponent():void
43 deleteSelectedComponentInstance():void;
44 onBackgroundClick():void;
45 setSelectedInstance(componentInstance:ComponentInstance):void;
48 cacheComponentsInstancesFullData:Component;
51 export class CompositionViewModel {
60 'Sdc.Services.SharingService',
62 'Sdc.Services.CacheService',
64 'ChangeLifecycleStateHandler',
65 'LeftPaletteLoaderService',
67 'EventListenerService',
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) {
87 this.$scope.setValidState(true);
90 this.$scope.updateSelectedMenuItem();
91 this.registerGraphEvents(this.$scope);
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);
106 this.eventListenerService.notifyObservers(GRAPH_EVENTS.ON_COMPOSITION_GRAPH_DATA_LOADED);
108 this.eventListenerService.unRegisterObserver(GRAPH_EVENTS.ON_COMPOSITION_GRAPH_DATA_LOADED);
112 private cacheComponentsInstancesFullData:Array<Component>;
114 private initComponent = ():void => {
116 this.$scope.currentComponent = this.$scope.component;
117 this.$scope.selectedComponent = this.$scope.currentComponent;
118 this.updateUuidMap();
119 this.$scope.isViewOnly = this.$scope.isViewMode();
121 private registerGraphEvents = (scope:ICompositionViewModelScope):void => {
123 this.eventListenerService.registerObserverCallback(GRAPH_EVENTS.ON_NODE_SELECTED, scope.setSelectedInstance);
124 this.eventListenerService.registerObserverCallback(GRAPH_EVENTS.ON_GRAPH_BACKGROUND_CLICKED, scope.onBackgroundClick);
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);
135 private removeSelectedComponentInstance = ():void => {
136 this.eventListenerService.notifyObservers(GRAPH_EVENTS.ON_DELETE_MULTIPLE_COMPONENTS);
139 private updateUuidMap = ():void => {
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.
145 this.sharingService.addUuidValue(this.$scope.currentComponent.uniqueId, this.$scope.currentComponent.uuid);
148 private initScope = ():void => {
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();
158 this.cacheComponentsInstancesFullData = new Array<Component>();
160 this.$scope.isComponentInstanceSelected = ():boolean => {
161 return this.$scope.currentComponent && this.$scope.currentComponent.selectedInstance != undefined && this.$scope.currentComponent.selectedInstance != null;
164 this.$scope.updateSelectedComponent = ():void => {
165 if (this.$scope.currentComponent.selectedInstance) {
167 let componentParent = _.find(this.cacheComponentsInstancesFullData, (component) => {
168 return component.uniqueId === this.$scope.currentComponent.selectedInstance.componentUid;
170 if (componentParent) {
171 this.$scope.selectedComponent = componentParent;
175 let onSuccess = (component:Component) => {
176 this.$scope.isLoadingRightPanel = false;
177 this.$scope.selectedComponent = component;
178 this.cacheComponentsInstancesFullData.push(component);
180 let onError = (component:Component) => {
181 console.log("Error updating selected component");
182 this.$scope.isLoadingRightPanel = false;
184 this.ComponentFactory.getComponentFromServer(this.$scope.currentComponent.selectedInstance.originType, this.$scope.currentComponent.selectedInstance.componentUid).then(onSuccess, onError);
186 console.log("Error updating selected component", e);
187 this.$scope.isLoadingRightPanel = false;
193 this.$scope.selectedComponent = this.$scope.currentComponent;
197 this.$scope.setSelectedInstance = (selectedComponent:ComponentInstance):void => {
199 this.$log.debug('composition-view-model::onNodeSelected:: with id: ' + selectedComponent.uniqueId);
200 this.$scope.currentComponent.setSelectedInstance(selectedComponent);
201 this.$scope.updateSelectedComponent();
203 if (this.$state.current.name === 'workspace.composition.api') {
204 this.$state.go('workspace.composition.details');
206 if (this.$state.current.name === 'workspace.composition.relations' && this.$scope.currentComponent.isProduct()) {
207 this.$state.go('workspace.composition.details');
211 this.$scope.onBackgroundClick = ():void => {
212 this.$scope.currentComponent.selectedInstance = null;
213 this.$scope.selectedComponent = this.$scope.currentComponent;
215 if (this.$state.current.name === 'workspace.composition.api') {
216 this.$state.go('workspace.composition.details');
219 if(this.$scope.selectedComponent.isService() && this.$state.current.name === 'workspace.composition.relations'){
220 this.$state.go('workspace.composition.api');
224 this.$scope.openUpdateModal = ():void => {
225 this.openUpdateComponentInstanceNameModal();
228 this.$scope.deleteSelectedComponentInstance = ():void => {
229 let state = "deleteInstance";
230 let onOk = ():void => {
231 this.removeSelectedComponentInstance();
232 //this.$scope.graphApi.deleteSelectedNodes();
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);
239 this.$scope.onComponentInstanceVersionChange = (component:Product):void => {
240 this.$scope.currentComponent = component;
241 this.$scope.setComponent(this.$scope.currentComponent);
242 this.$scope.updateSelectedComponent();