summaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/view-models/workspace/tabs/composition/tabs/properties-and-attributes/properties-view-model.ts
blob: 90cb556c75d4446f1fe41b5e3683069bf9e28b63 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
'use strict';
import {
    AttributeModel,
    AttributesGroup,
    Component,
    ComponentInstance,
    PropertyModel,
    PropertiesGroup
} from "app/models";
import {ICompositionViewModelScope} from "../../composition-view-model";
import {ModalsHandler} from "app/utils";
import {ComponentServiceNg2} from "app/ng2/services/component-services/component.service";
import {ComponentGenericResponse} from "app/ng2/services/responses/component-generic-response";

interface IResourcePropertiesAndAttributesViewModelScope extends ICompositionViewModelScope {
    properties:PropertiesGroup;
    attributes:AttributesGroup;
    propertiesMessage:string;
    groupPropertiesByInstance:boolean;
    showGroupsOfInstanceProperties:Array<boolean>;
    addProperty():void;
    updateProperty(property:PropertyModel):void;
    deleteProperty(property:PropertyModel):void;
    viewAttribute(attribute:AttributeModel):void;
    groupNameByKey(key:string):string;
    isPropertyOwner():boolean;
    getComponentInstanceNameFromInstanceByKey(key:string):string;
}

export class ResourcePropertiesViewModel {

    static '$inject' = [
        '$scope',
        '$filter',
        '$uibModal',
        'ModalsHandler',
        'ComponentServiceNg2'

    ];


    constructor(private $scope:IResourcePropertiesAndAttributesViewModelScope,
                private $filter:ng.IFilterService,
                private $uibModal:ng.ui.bootstrap.IModalService,
                private ModalsHandler:ModalsHandler,
                private ComponentServiceNg2:ComponentServiceNg2) {

        this.getComponentInstancesPropertiesAndAttributes();
    }

    private initComponentProperties = ():void => {
        let result:PropertiesGroup = {};

        if (this.$scope.selectedComponent) {
            this.$scope.propertiesMessage = undefined;
            this.$scope.groupPropertiesByInstance = false;
            if (this.$scope.isComponentInstanceSelected()) {
                if (this.$scope.currentComponent.selectedInstance.originType === 'VF') {
                    this.$scope.groupPropertiesByInstance = true;
                }
                result[this.$scope.currentComponent.selectedInstance.uniqueId] = this.$scope.currentComponent.componentInstancesProperties[this.$scope.currentComponent.selectedInstance.uniqueId];
            } else if (this.$scope.currentComponent.isService()) {
                // Temporally fix to hide properties for service (UI stack when there are many properties)
                result = this.$scope.currentComponent.componentInstancesProperties;
                this.$scope.propertiesMessage = "Note: properties for service are disabled";
            } else {
                let key = this.$scope.selectedComponent.uniqueId;
                result[key] = Array<PropertyModel>();
                let derived = Array<PropertyModel>();
                _.forEach(this.$scope.selectedComponent.properties, (property:PropertyModel) => {
                    if (key == property.parentUniqueId) {
                        result[key].push(property);
                    } else {
                        property.readonly = true;
                        derived.push(property);
                    }
                });
                if (derived.length) {
                    result['derived'] = derived;
                }
            }
            this.$scope.properties = result;
        }
    };


    private initComponentAttributes = ():void => {
        let result:AttributesGroup = {};

        if (this.$scope.selectedComponent) {
            if (this.$scope.isComponentInstanceSelected()) {
                result[this.$scope.currentComponent.selectedInstance.uniqueId] = this.$scope.currentComponent.componentInstancesAttributes[this.$scope.currentComponent.selectedInstance.uniqueId];
            } else if (this.$scope.currentComponent.isService()) {
                result = this.$scope.currentComponent.componentInstancesAttributes;
            }
            this.$scope.attributes = result;
        }
    };

    /**
     * This function is checking if the component is the value owner of the current property
     * in order to notify the edit property modal which fields to disable
     */
    private isPropertyValueOwner = ():boolean => {
        return this.$scope.currentComponent.isService() || !!this.$scope.currentComponent.selectedInstance;
    };

    /**
     *  The function opens the edit property modal.
     *  It checks if the property is from the VF or from one of it's resource instances and sends the needed property list.
     *  For create property reasons an empty array is transferd
     *
     * @param property the wanted property to edit/create
     */
    private openEditPropertyModal = (property:PropertyModel):void => {
        this.ModalsHandler.openEditPropertyModal(property,
            this.$scope.component,
            (this.$scope.isPropertyOwner() ?
                this.$scope.properties[property.parentUniqueId] :
                this.$scope.properties[property.resourceInstanceUniqueId]) || [],
            this.isPropertyValueOwner()).then((updatedProperty:PropertyModel) => {
               let oldProp = _.find(this.$scope.properties[updatedProperty.resourceInstanceUniqueId], (prop:PropertyModel) => {return prop.uniqueId == updatedProperty.uniqueId;});
            oldProp.value = updatedProperty.value;
        });
    };

    private openAttributeModal = (atrribute:AttributeModel):void => {

        let modalOptions:ng.ui.bootstrap.IModalSettings = {
            template: 'app/view-models/forms/attribute-form/attribute-form-view.html',
            controller: 'Sdc.ViewModels.AttributeFormViewModel',
            size: 'sdc-md',
            backdrop: 'static',
            keyboard: false,
            resolve: {
                attribute: ():AttributeModel => {
                    return atrribute;
                },
                component: ():Component => {
                    return this.$scope.currentComponent;
                }
            }
        };
        this.$uibModal.open(modalOptions);
    };

    private getComponentInstancesPropertiesAndAttributes = () => {

        this.ComponentServiceNg2.getComponentInstanceAttributesAndProperties(this.$scope.currentComponent).subscribe((genericResponse:ComponentGenericResponse) => {
            this.$scope.currentComponent.componentInstancesAttributes = genericResponse.componentInstancesAttributes;
            this.$scope.currentComponent.componentInstancesProperties = genericResponse.componentInstancesProperties;
            this.initScope();
        });
    };

    private initScope = ():void => {


        this.initComponentProperties();
        this.initComponentAttributes();

        this.$scope.$watchCollection('currentComponent.properties', (newData:any):void => {
            this.initComponentProperties();
        });

        this.$scope.$watch('currentComponent.selectedInstance', (newInstance:ComponentInstance):void => {
            if (angular.isDefined(newInstance)) {
                this.initComponentProperties();
                this.initComponentAttributes();

            }
        });

        this.$scope.isPropertyOwner = ():boolean => {
            return this.$scope.currentComponent && this.$scope.currentComponent.isResource() && !this.$scope.isComponentInstanceSelected();
        };

        this.$scope.updateProperty = (property:PropertyModel):void => {
            this.openEditPropertyModal(property);
        };

        this.$scope.deleteProperty = (property:PropertyModel):void => {

            let onOk = ():void => {
                this.$scope.currentComponent.deleteProperty(property.uniqueId);
            };

            let title:string = this.$filter('translate')("PROPERTY_VIEW_DELETE_MODAL_TITLE");
            let message:string = this.$filter('translate')("PROPERTY_VIEW_DELETE_MODAL_TEXT", "{'name': '" + property.name + "'}");
            this.ModalsHandler.openConfirmationModal(title, message, false).then(onOk);
        };

        this.$scope.viewAttribute = (attribute:AttributeModel):void => {
            this.openAttributeModal(attribute);
        };

        this.$scope.groupNameByKey = (key:string):string => {
            switch (key) {
                case 'derived':
                    return "Derived";

                case this.$scope.currentComponent.uniqueId:
                    return this.$filter("resourceName")(this.$scope.currentComponent.name);

                default:
                    return this.$filter("resourceName")((_.find(this.$scope.currentComponent.componentInstances, {uniqueId: key})).name);
            }
        };

        this.$scope.getComponentInstanceNameFromInstanceByKey = (key:string):string => {
            let instanceName:string = "";
            if (key !== undefined && this.$scope.selectedComponent.uniqueId == this.$scope.currentComponent.selectedInstance.componentUid) {
                instanceName = this.$filter("resourceName")((_.find(this.$scope.selectedComponent.componentInstances, {uniqueId: key})).name);
            }
            return instanceName;
        };

    }
}