20e99b0d39608adcb08aa9aa327f0ce4c8d446ee
[sdc.git] /
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, PropertiesGroup, InputsGroup, ServiceInstanceObject, InterfaceModel, InputBEModel} from 'app/models';
20 import {ComponentGenericResponse} from "app/ng2/services/responses/component-generic-response";
21 import {ServiceServiceNg2} from "app/ng2/services/component-services/service.service";
22
23 interface IServiceConsumptionViewModelScope extends ICompositionViewModelScope {
24     service: Service;
25     instancesMappedList: Array<ServiceInstanceObject>;
26     componentInstancesProperties: PropertiesGroup;
27     componentInstancesInputs: InputsGroup;
28     componentInstancesInterfaces: Map<string, Array<InterfaceModel>>;
29     componentInputs: Array<InputBEModel>;
30 }
31
32
33 export class ServiceConsumptionViewModel {
34
35     static '$inject' = [
36         '$scope',
37         'ServiceServiceNg2'
38     ];
39
40     constructor(private $scope:IServiceConsumptionViewModelScope, private ServiceServiceNg2:ServiceServiceNg2) {
41         this.$scope.service = <Service>this.$scope.currentComponent;
42         this.initInstances();
43         this.initScope();
44     }
45
46     private initInstances = ():void => {
47         this.ServiceServiceNg2.getServiceConsumptionData(this.$scope.service).subscribe((genericResponse:ComponentGenericResponse) => {
48             this.$scope.componentInstancesProperties = genericResponse.componentInstancesProperties;
49             this.$scope.componentInstancesInputs = genericResponse.componentInstancesInputs;
50             this.$scope.componentInstancesInterfaces = genericResponse.componentInstancesInterfaces;
51             this.$scope.componentInputs = genericResponse.inputs;
52             this.updateInstanceAttributes();
53         });
54     }
55
56     private updateInstanceAttributes = ():void => {
57         if (this.$scope.isComponentInstanceSelected() && this.$scope.componentInstancesProperties) {
58             this.$scope.instancesMappedList = this.$scope.service.componentInstances.map(coInstance => new ServiceInstanceObject({
59                 id: coInstance.uniqueId,
60                 name: coInstance.name,
61                 properties: this.$scope.componentInstancesProperties[coInstance.uniqueId] || [],
62                 inputs: this.$scope.componentInstancesInputs[coInstance.uniqueId] || [],
63                 interfaces: this.$scope.componentInstancesInterfaces[coInstance.uniqueId] || []
64             }));
65         }
66     }
67
68     private initScope = ():void => {
69         this.$scope.$watch('currentComponent.selectedInstance', ():void => {
70             this.updateInstanceAttributes();
71         });
72
73         this.$scope.registerCreateInstanceEvent(() => {
74             this.initInstances();
75         });
76
77         this.$scope.$on('$destroy', this.$scope.unregisterCreateInstanceEvent);
78     }
79 }