aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/view-models/workspace/tabs/composition/tabs/service-dependencies/service-dependencies-view-model.ts
blob: b634e6021f9d2ffe646b753152aa288dff215ef6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*!
 * Copyright © 2016-2018 European Support Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */


import {ICompositionViewModelScope} from "../../composition-view-model";
import {Service, ComponentInstance, PropertiesGroup, ServiceInstanceObject, PropertyBEModel} from 'app/models';
import {ComponentServiceNg2} from "app/ng2/services/component-services/component.service";
import {ConstraintObject} from "app/ng2/components/logic/service-dependencies/service-dependencies.component";
import {ComponentGenericResponse} from 'app/ng2/services/responses/component-generic-response';
import {DEPENDENCY_EVENTS} from "app/utils/constants";
import {EventListenerService} from 'app/services';

interface IServiceDependenciesViewModelScope extends ICompositionViewModelScope {
    service: Service;
    selectedInstanceSiblings: Array<ServiceInstanceObject>;
    componentInstancesConstraints: Array<any>;
    selectedInstanceConstraints: Array<ConstraintObject>;
    selectedInstanceProperties: Array<PropertyBEModel>;
    updateSelectedInstanceConstraints(constraintsList:Array<ConstraintObject>): void;
    loadConstraints(): void;
    componentInstanceProperties: PropertiesGroup;
    notifyDependencyEventsObserver: Function;
}



export class ServiceDependenciesViewModel {

    static '$inject' = [
        '$scope',
        'ComponentServiceNg2',
        'EventListenerService'
    ];

    constructor(private $scope:IServiceDependenciesViewModelScope, private ComponentServiceNg2:ComponentServiceNg2, private eventListenerService: EventListenerService) {
        this.$scope.service = <Service>this.$scope.currentComponent;
        this.$scope.notifyDependencyEventsObserver = this.notifyDependencyEventsObserver;
        this.initInstancesWithProperties();
        this.loadConstraints();

        this.initScope();
    }

    private initInstancesWithProperties = ():void => {
        this.ComponentServiceNg2.getComponentInstanceProperties(this.$scope.currentComponent).subscribe((genericResponse:ComponentGenericResponse) => {
            this.$scope.componentInstanceProperties = genericResponse.componentInstancesProperties;
            this.updateInstanceAttributes();
        });
    }

    private updateInstanceAttributes = ():void => {
        if (this.$scope.isComponentInstanceSelected() && this.$scope.componentInstanceProperties) {
            let instancesMappedList = this.$scope.service.componentInstances.map(coInstance => new ServiceInstanceObject({
                id: coInstance.uniqueId,
                name: coInstance.name,
                properties: this.$scope.componentInstanceProperties[coInstance.uniqueId] || []
            }));
            this.$scope.selectedInstanceProperties = this.$scope.componentInstanceProperties[this.$scope.currentComponent.selectedInstance.uniqueId];
            this.$scope.selectedInstanceSiblings = instancesMappedList.filter(coInstance => coInstance.id !== this.$scope.currentComponent.selectedInstance.uniqueId);
        }
    }

    private initScope = ():void => {
        this.$scope.$watch('currentComponent.selectedInstance', (newInstance:ComponentInstance):void => {
            if (angular.isDefined(newInstance) && this.$scope.componentInstancesConstraints) {
                this.updateInstanceAttributes();
                this.$scope.selectedInstanceConstraints = this.$scope.componentInstancesConstraints[this.$scope.currentComponent.selectedInstance.uniqueId] ?
                    this.$scope.componentInstancesConstraints[this.$scope.currentComponent.selectedInstance.uniqueId].properties :
                    [];
            }
        });
        this.$scope.$watch('componentInstancesConstraints', (constraints: Array<any>):void => {
            if (angular.isDefined(constraints)) {
                if(this.$scope.isComponentInstanceSelected()) {
                    this.$scope.selectedInstanceConstraints = this.$scope.componentInstancesConstraints[this.$scope.currentComponent.selectedInstance.uniqueId] ?
                        this.$scope.componentInstancesConstraints[this.$scope.currentComponent.selectedInstance.uniqueId].properties || [] :
                        [];
                }
            }
        });

        this.$scope.updateSelectedInstanceConstraints = (constraintsList:Array<ConstraintObject>):void => {
            this.$scope.componentInstancesConstraints[this.$scope.currentComponent.selectedInstance.uniqueId].properties = constraintsList;
            this.$scope.selectedInstanceConstraints = this.$scope.componentInstancesConstraints[this.$scope.currentComponent.selectedInstance.uniqueId].properties;
        }

        this.$scope.loadConstraints = ():void => {
            this.loadConstraints();
        }

        this.$scope.registerCreateInstanceEvent(() => {
            this.initInstancesWithProperties();
        });

        this.$scope.registerChangeComponentInstanceNameEvent((updatedComponentInstance) => {
            this.$scope.currentComponent.selectedInstance = updatedComponentInstance;
        });

        this.$scope.$on('$destroy', this.$scope.unregisterCreateInstanceEvent);
        this.$scope.$on('$destroy', this.$scope.unregisterChangeComponentInstanceNameEvent);
    }

    private loadConstraints = ():void => {
        this.ComponentServiceNg2.getServiceFilterConstraints(this.$scope.service).subscribe((response) => {
            this.$scope.componentInstancesConstraints = response.nodeFilterData;
        });
    }

    public notifyDependencyEventsObserver = (isChecked: boolean):void => {
        this.eventListenerService.notifyObservers(DEPENDENCY_EVENTS.ON_DEPENDENCY_CHANGE, isChecked);
    }
}