CSIT Fix for SDC-2585
[sdc.git] / catalog-ui / src / app / view-models / workspace / tabs / composition / tabs / service-dependencies / service-dependencies-view-model.ts
1 /*!
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16
17
18 import {ICompositionViewModelScope} from "../../composition-view-model";
19 import {Service, ComponentInstance, PropertiesGroup, ServiceInstanceObject, PropertyBEModel} from 'app/models';
20 import {ComponentServiceNg2} from "app/ng2/services/component-services/component.service";
21 import {ConstraintObject} from "app/ng2/components/logic/service-dependencies/service-dependencies.component";
22 import {ComponentGenericResponse} from 'app/ng2/services/responses/component-generic-response';
23 import {DEPENDENCY_EVENTS} from "app/utils/constants";
24 import {EventListenerService} from 'app/services';
25
26 interface IServiceDependenciesViewModelScope extends ICompositionViewModelScope {
27     service: Service;
28     selectedInstanceSiblings: Array<ServiceInstanceObject>;
29     componentInstancesConstraints: Array<any>;
30     selectedInstanceConstraints: Array<ConstraintObject>;
31     selectedInstanceProperties: Array<PropertyBEModel>;
32     updateSelectedInstanceConstraints(constraintsList:Array<ConstraintObject>): void;
33     loadConstraints(): void;
34     componentInstanceProperties: PropertiesGroup;
35     notifyDependencyEventsObserver: Function;
36 }
37
38
39
40 export class ServiceDependenciesViewModel {
41
42     static '$inject' = [
43         '$scope',
44         'ComponentServiceNg2',
45         'EventListenerService'
46     ];
47
48     constructor(private $scope:IServiceDependenciesViewModelScope, private ComponentServiceNg2:ComponentServiceNg2, private eventListenerService: EventListenerService) {
49         this.$scope.service = <Service>this.$scope.currentComponent;
50         this.$scope.notifyDependencyEventsObserver = this.notifyDependencyEventsObserver;
51         this.initInstancesWithProperties();
52         this.loadConstraints();
53
54         this.initScope();
55     }
56
57     private initInstancesWithProperties = ():void => {
58         this.ComponentServiceNg2.getComponentInstanceProperties(this.$scope.currentComponent).subscribe((genericResponse:ComponentGenericResponse) => {
59             this.$scope.componentInstanceProperties = genericResponse.componentInstancesProperties;
60             this.updateInstanceAttributes();
61         });
62     }
63
64     private updateInstanceAttributes = ():void => {
65         if (this.$scope.isComponentInstanceSelected() && this.$scope.componentInstanceProperties) {
66             let instancesMappedList = this.$scope.service.componentInstances.map(coInstance => new ServiceInstanceObject({
67                 id: coInstance.uniqueId,
68                 name: coInstance.name,
69                 properties: this.$scope.componentInstanceProperties[coInstance.uniqueId] || []
70             }));
71             this.$scope.selectedInstanceProperties = this.$scope.componentInstanceProperties[this.$scope.currentComponent.selectedInstance.uniqueId];
72             this.$scope.selectedInstanceSiblings = instancesMappedList.filter(coInstance => coInstance.id !== this.$scope.currentComponent.selectedInstance.uniqueId);
73         }
74     }
75
76     private initScope = ():void => {
77         this.$scope.$watch('currentComponent.selectedInstance', (newInstance:ComponentInstance):void => {
78             if (angular.isDefined(newInstance) && this.$scope.componentInstancesConstraints) {
79                 this.updateInstanceAttributes();
80                 this.$scope.selectedInstanceConstraints = this.$scope.componentInstancesConstraints[this.$scope.currentComponent.selectedInstance.uniqueId] ?
81                     this.$scope.componentInstancesConstraints[this.$scope.currentComponent.selectedInstance.uniqueId].properties :
82                     [];
83             }
84         });
85         this.$scope.$watch('componentInstancesConstraints', (constraints: Array<any>):void => {
86             if (angular.isDefined(constraints)) {
87                 if(this.$scope.isComponentInstanceSelected()) {
88                     this.$scope.selectedInstanceConstraints = this.$scope.componentInstancesConstraints[this.$scope.currentComponent.selectedInstance.uniqueId] ?
89                         this.$scope.componentInstancesConstraints[this.$scope.currentComponent.selectedInstance.uniqueId].properties || [] :
90                         [];
91                 }
92             }
93         });
94
95         this.$scope.updateSelectedInstanceConstraints = (constraintsList:Array<ConstraintObject>):void => {
96             this.$scope.componentInstancesConstraints[this.$scope.currentComponent.selectedInstance.uniqueId].properties = constraintsList;
97             this.$scope.selectedInstanceConstraints = this.$scope.componentInstancesConstraints[this.$scope.currentComponent.selectedInstance.uniqueId].properties;
98         }
99
100         this.$scope.loadConstraints = ():void => {
101             this.loadConstraints();
102         }
103
104         this.$scope.registerCreateInstanceEvent(() => {
105             this.initInstancesWithProperties();
106         });
107
108         this.$scope.registerChangeComponentInstanceNameEvent((updatedComponentInstance) => {
109             this.$scope.currentComponent.selectedInstance = updatedComponentInstance;
110         });
111
112         this.$scope.$on('$destroy', this.$scope.unregisterCreateInstanceEvent);
113         this.$scope.$on('$destroy', this.$scope.unregisterChangeComponentInstanceNameEvent);
114     }
115
116     private loadConstraints = ():void => {
117         this.ComponentServiceNg2.getServiceFilterConstraints(this.$scope.service).subscribe((response) => {
118             this.$scope.componentInstancesConstraints = response.nodeFilterData;
119         });
120     }
121
122     public notifyDependencyEventsObserver = (isChecked: boolean):void => {
123         this.eventListenerService.notifyObservers(DEPENDENCY_EVENTS.ON_DEPENDENCY_CHANGE, isChecked);
124     }
125 }