aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/view-models/forms/property-forms/module-property-modal/module-property-model.ts
blob: 7359ac0e919b51aa572294af432ce77777f82732 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
 * Created by obarda on 1/18/2017.
 */
'use strict';
import {PropertyModel, DisplayModule, Component, Resource, Service, ComponentInstance} from "app/models";
import {UNIQUE_GROUP_PROPERTIES_NAME} from "app/utils";
import {IPropertyFormBaseViewScope, PropertyFormBaseView} from "../base-property-form/property-form-base-model";
import {DataTypesService} from "app/services/data-types-service";

export interface IModulePropertyViewScope extends IPropertyFormBaseViewScope {
    onValueChange():void;
}

export class ModulePropertyView extends PropertyFormBaseView {

    static '$inject' = [
        '$scope',
        '$templateCache',
        '$uibModalInstance',
        '$injector',
        'originalProperty',
        'component',
        'selectedModule',
        'Sdc.Services.DataTypesService',
        '$q'
    ];

    constructor(protected $scope:IModulePropertyViewScope,
                protected $templateCache:ng.ITemplateCacheService,
                protected $uibModalInstance:ng.ui.bootstrap.IModalServiceInstance,
                protected $injector:ng.auto.IInjectorService,
                protected originalProperty:PropertyModel,
                protected component:Component,
                private selectedModule:DisplayModule,
                protected DataTypesService:DataTypesService,
                private $q:ng.IQService) {
        super($scope, $uibModalInstance, $injector, originalProperty, component, selectedModule.properties, DataTypesService);

        this.$templateCache.put("module-property-view.html", require('app/view-models/forms/property-forms/module-property-modal/module-property-view.html'));
        this.$scope.innerViewSrcUrl = "module-property-view.html";
        this.initChildScope();
    }

    private findPropertyByName = (propertyName:string):PropertyModel => {
        let property:PropertyModel = _.find(this.filteredProperties, (property:PropertyModel) => {
            return property.name === propertyName;
        });
        return property;
    };

    save(isNeedToCloseModal):ng.IPromise<boolean> {

        let deferred = this.$q.defer();

        let onSuccess = (properties:Array<PropertyModel>):void => {
            deferred.resolve(true);
            if (isNeedToCloseModal === true) {
                this.$scope.close();
            }
        };

        let onFailed = ():void => {
            deferred.resolve(false);
        };

        let property = _.find(this.selectedModule.properties, (property) => {
            return property.uniqueId === this.$scope.property.uniqueId;
        });
        if (property.value !== this.$scope.property.value) {
            if (this.component.isResource()) {
                (<Resource>this.component).updateResourceGroupProperties(this.selectedModule, [this.$scope.property]).then(onSuccess, onFailed); // for now we only update one property at a time
            }
            if (this.component.isService()) {
                // Find the component instance of the group instance
                let componentInstance:ComponentInstance = _.find(this.component.componentInstances, (componentInstance:ComponentInstance) => {
                    let groupInstance = _.find(componentInstance.groupInstances, {uniqueId: this.selectedModule.groupInstanceUniqueId});
                    return groupInstance !== undefined;

                });
                (<Service>this.component).updateGroupInstanceProperties(componentInstance.uniqueId, this.selectedModule, [this.$scope.property]).then(onSuccess, onFailed); // for now we only update one property at a time
            }
        } else {
            deferred.resolve(true);
        }
        return deferred.promise;
    }

    onPropertyChange():void {
        this.initValidation();
    }

    protected initValidation = ():void => {

        this.$scope.isDeleteDisable = true;
        this.$scope.isNameDisable = true;
        this.$scope.isTypeSelectorDisable = true;
        this.$scope.isDescriptionDisable = true;

        switch (this.$scope.property.name) {
            case UNIQUE_GROUP_PROPERTIES_NAME.IS_BASE:
            case UNIQUE_GROUP_PROPERTIES_NAME.VF_MODULE_TYPE:
            case UNIQUE_GROUP_PROPERTIES_NAME.VOLUME_GROUP:
            case UNIQUE_GROUP_PROPERTIES_NAME.VF_MODULE_LABEL:
                this.$scope.property.readonly = true;
                break;
            case UNIQUE_GROUP_PROPERTIES_NAME.VF_MODULE_DESCRIPTION:
                if (this.component.isService()) {
                    this.$scope.property.readonly = true;
                } else {
                    this.$scope.property.readonly = false;
                }
                break;
        }
    };

    private isUniqueProperty = ():boolean => {
        return this.$scope.property.name === UNIQUE_GROUP_PROPERTIES_NAME.MIN_VF_MODULE_INSTANCES ||
            this.$scope.property.name === UNIQUE_GROUP_PROPERTIES_NAME.MAX_VF_MODULE_INSTANCES ||
            this.$scope.property.name === UNIQUE_GROUP_PROPERTIES_NAME.INITIAL_COUNT;
    };


    private initChildScope = ():void => {

        this.initValidation();

        // put default value when instance value is empty
        this.$scope.onValueChange = ():void => {

            if (!this.$scope.property.value) { // Resetting to default value
                if (this.isPropertyValueOwner()) {
                    if (this.component.isService()) {
                        this.$scope.property.value = this.$scope.property.parentValue;
                    } else {
                        this.$scope.property.value = this.$scope.property.defaultValue;
                    }
                }
            }

            if (this.isUniqueProperty()) {

                let isValid = true;
                let maxProperty:PropertyModel = this.findPropertyByName(UNIQUE_GROUP_PROPERTIES_NAME.MAX_VF_MODULE_INSTANCES);
                let minProperty:PropertyModel = this.findPropertyByName(UNIQUE_GROUP_PROPERTIES_NAME.MIN_VF_MODULE_INSTANCES);
                let initialCountProperty:PropertyModel = this.findPropertyByName(UNIQUE_GROUP_PROPERTIES_NAME.INITIAL_COUNT);

                let maxPropertyValue = parseInt(maxProperty.value);
                let minPropertyValue = parseInt(minProperty.value);
                let initialCountPropertyValue = parseInt(initialCountProperty.value);
                let propertyValue = parseInt(this.$scope.property.value);
                let parentPropertyValue = parseInt(this.$scope.property.parentValue);

                switch (this.$scope.property.name) {

                    case UNIQUE_GROUP_PROPERTIES_NAME.MIN_VF_MODULE_INSTANCES:
                        if (!maxPropertyValue || maxPropertyValue === null) {
                            isValid = propertyValue <= initialCountPropertyValue;
                        }
                        else {
                            isValid = propertyValue && (propertyValue <= maxPropertyValue && propertyValue <= initialCountPropertyValue);
                        }
                        this.$scope.forms.editForm["value"].$setValidity('maxValidation', isValid);
                        if (this.component.isService()) {
                            if (!parentPropertyValue || parentPropertyValue === null) {
                                isValid = true;
                            } else {
                                isValid = propertyValue >= parentPropertyValue;
                                this.$scope.forms.editForm["value"].$setValidity('minValidationVfLevel', isValid);
                            }
                        }
                        break;
                    case UNIQUE_GROUP_PROPERTIES_NAME.MAX_VF_MODULE_INSTANCES:
                        if (!minPropertyValue || minPropertyValue === null) {
                            isValid = propertyValue >= initialCountPropertyValue;
                        } else {
                            isValid = !propertyValue || (propertyValue >= minPropertyValue && propertyValue >= initialCountPropertyValue);
                        }
                        this.$scope.forms.editForm["value"].$setValidity('minValidation', isValid);
                        if (this.component.isService()) {
                            if (!parentPropertyValue || parentPropertyValue === null) {
                                isValid = true;
                            }
                            else {
                                isValid = propertyValue <= parentPropertyValue;
                                this.$scope.forms.editForm["value"].$setValidity('maxValidationVfLevel', isValid);
                            }
                        }
                        break;
                    case UNIQUE_GROUP_PROPERTIES_NAME.INITIAL_COUNT:
                        if ((!minPropertyValue || minPropertyValue === null) && (!maxPropertyValue || maxPropertyValue === null)) {
                            isValid = true;
                        } else if (!minPropertyValue || minPropertyValue === null) {
                            isValid = propertyValue <= maxPropertyValue;
                        } else if (!maxPropertyValue || maxPropertyValue === null) {
                            isValid = propertyValue >= minPropertyValue;
                        } else {
                            isValid = minPropertyValue <= propertyValue && propertyValue <= maxPropertyValue;
                        }
                        this.$scope.forms.editForm["value"].$setValidity('minOrMaxValidation', isValid);
                        break;
                }
            }
            ;
        }
    }
}