[SDC-29] rebase continue work to align source
[sdc.git] / catalog-ui / src / app / view-models / workspace / tabs / composition / tabs / properties-and-attributes / properties-view-model.ts
1 'use strict';
2 import {
3     AttributeModel,
4     AttributesGroup,
5     Component,
6     ComponentInstance,
7     PropertyModel,
8     PropertiesGroup
9 } from "app/models";
10 import {ICompositionViewModelScope} from "../../composition-view-model";
11 import {ModalsHandler} from "app/utils";
12 import {ComponentServiceNg2} from "app/ng2/services/component-services/component.service";
13 import {ComponentGenericResponse} from "app/ng2/services/responses/component-generic-response";
14
15 interface IResourcePropertiesAndAttributesViewModelScope extends ICompositionViewModelScope {
16     properties:PropertiesGroup;
17     attributes:AttributesGroup;
18     propertiesMessage:string;
19     groupPropertiesByInstance:boolean;
20     showGroupsOfInstanceProperties:Array<boolean>;
21     addProperty():void;
22     updateProperty(property:PropertyModel):void;
23     deleteProperty(property:PropertyModel):void;
24     viewAttribute(attribute:AttributeModel):void;
25     groupNameByKey(key:string):string;
26     isPropertyOwner():boolean;
27     getComponentInstanceNameFromInstanceByKey(key:string):string;
28 }
29
30 export class ResourcePropertiesViewModel {
31
32     static '$inject' = [
33         '$scope',
34         '$filter',
35         '$uibModal',
36         'ModalsHandler',
37         'ComponentServiceNg2'
38
39     ];
40
41
42     constructor(private $scope:IResourcePropertiesAndAttributesViewModelScope,
43                 private $filter:ng.IFilterService,
44                 private $uibModal:ng.ui.bootstrap.IModalService,
45                 private ModalsHandler:ModalsHandler,
46                 private ComponentServiceNg2:ComponentServiceNg2) {
47
48         this.getComponentInstancesPropertiesAndAttributes();
49     }
50
51     private initComponentProperties = ():void => {
52         let result:PropertiesGroup = {};
53
54         if (this.$scope.selectedComponent) {
55             this.$scope.propertiesMessage = undefined;
56             this.$scope.groupPropertiesByInstance = false;
57             if (this.$scope.isComponentInstanceSelected()) {
58                 if (this.$scope.currentComponent.selectedInstance.originType === 'VF') {
59                     this.$scope.groupPropertiesByInstance = true;
60                 }
61                 result[this.$scope.currentComponent.selectedInstance.uniqueId] = this.$scope.currentComponent.componentInstancesProperties[this.$scope.currentComponent.selectedInstance.uniqueId];
62             } else if (this.$scope.currentComponent.isService()) {
63                 // Temporally fix to hide properties for service (UI stack when there are many properties)
64                 result = this.$scope.currentComponent.componentInstancesProperties;
65                 this.$scope.propertiesMessage = "Note: properties for service are disabled";
66             } else {
67                 let key = this.$scope.selectedComponent.uniqueId;
68                 result[key] = Array<PropertyModel>();
69                 let derived = Array<PropertyModel>();
70                 _.forEach(this.$scope.selectedComponent.properties, (property:PropertyModel) => {
71                     if (key == property.parentUniqueId) {
72                         result[key].push(property);
73                     } else {
74                         property.readonly = true;
75                         derived.push(property);
76                     }
77                 });
78                 if (derived.length) {
79                     result['derived'] = derived;
80                 }
81             }
82             this.$scope.properties = result;
83         }
84     };
85
86
87     private initComponentAttributes = ():void => {
88         let result:AttributesGroup = {};
89
90         if (this.$scope.selectedComponent) {
91             if (this.$scope.isComponentInstanceSelected()) {
92                 result[this.$scope.currentComponent.selectedInstance.uniqueId] = this.$scope.currentComponent.componentInstancesAttributes[this.$scope.currentComponent.selectedInstance.uniqueId];
93             } else if (this.$scope.currentComponent.isService()) {
94                 result = this.$scope.currentComponent.componentInstancesAttributes;
95             }
96             this.$scope.attributes = result;
97         }
98     };
99
100     /**
101      * This function is checking if the component is the value owner of the current property
102      * in order to notify the edit property modal which fields to disable
103      */
104     private isPropertyValueOwner = ():boolean => {
105         return this.$scope.currentComponent.isService() || !!this.$scope.currentComponent.selectedInstance;
106     };
107
108     /**
109      *  The function opens the edit property modal.
110      *  It checks if the property is from the VF or from one of it's resource instances and sends the needed property list.
111      *  For create property reasons an empty array is transferd
112      *
113      * @param property the wanted property to edit/create
114      */
115     private openEditPropertyModal = (property:PropertyModel):void => {
116         this.ModalsHandler.openEditPropertyModal(property,
117             this.$scope.component,
118             (this.$scope.isPropertyOwner() ?
119                 this.$scope.properties[property.parentUniqueId] :
120                 this.$scope.properties[property.resourceInstanceUniqueId]) || [],
121             this.isPropertyValueOwner()).then((updatedProperty:PropertyModel) => {
122                let oldProp = _.find(this.$scope.properties[updatedProperty.resourceInstanceUniqueId], (prop:PropertyModel) => {return prop.uniqueId == updatedProperty.uniqueId;});
123             oldProp.value = updatedProperty.value;
124         });
125     };
126
127     private openAttributeModal = (atrribute:AttributeModel):void => {
128
129         let modalOptions:ng.ui.bootstrap.IModalSettings = {
130             template: 'app/view-models/forms/attribute-form/attribute-form-view.html',
131             controller: 'Sdc.ViewModels.AttributeFormViewModel',
132             size: 'sdc-md',
133             backdrop: 'static',
134             keyboard: false,
135             resolve: {
136                 attribute: ():AttributeModel => {
137                     return atrribute;
138                 },
139                 component: ():Component => {
140                     return this.$scope.currentComponent;
141                 }
142             }
143         };
144         this.$uibModal.open(modalOptions);
145     };
146
147     private getComponentInstancesPropertiesAndAttributes = () => {
148
149         this.ComponentServiceNg2.getComponentInstanceAttributesAndProperties(this.$scope.currentComponent).subscribe((genericResponse:ComponentGenericResponse) => {
150             this.$scope.currentComponent.componentInstancesAttributes = genericResponse.componentInstancesAttributes;
151             this.$scope.currentComponent.componentInstancesProperties = genericResponse.componentInstancesProperties;
152             this.initScope();
153         });
154     };
155
156     private initScope = ():void => {
157
158
159         this.initComponentProperties();
160         this.initComponentAttributes();
161
162         this.$scope.$watchCollection('currentComponent.properties', (newData:any):void => {
163             this.initComponentProperties();
164         });
165
166         this.$scope.$watch('currentComponent.selectedInstance', (newInstance:ComponentInstance):void => {
167             if (angular.isDefined(newInstance)) {
168                 this.initComponentProperties();
169                 this.initComponentAttributes();
170
171             }
172         });
173
174         this.$scope.isPropertyOwner = ():boolean => {
175             return this.$scope.currentComponent && this.$scope.currentComponent.isResource() && !this.$scope.isComponentInstanceSelected();
176         };
177
178         this.$scope.updateProperty = (property:PropertyModel):void => {
179             this.openEditPropertyModal(property);
180         };
181
182         this.$scope.deleteProperty = (property:PropertyModel):void => {
183
184             let onOk = ():void => {
185                 this.$scope.currentComponent.deleteProperty(property.uniqueId);
186             };
187
188             let title:string = this.$filter('translate')("PROPERTY_VIEW_DELETE_MODAL_TITLE");
189             let message:string = this.$filter('translate')("PROPERTY_VIEW_DELETE_MODAL_TEXT", "{'name': '" + property.name + "'}");
190             this.ModalsHandler.openConfirmationModal(title, message, false).then(onOk);
191         };
192
193         this.$scope.viewAttribute = (attribute:AttributeModel):void => {
194             this.openAttributeModal(attribute);
195         };
196
197         this.$scope.groupNameByKey = (key:string):string => {
198             switch (key) {
199                 case 'derived':
200                     return "Derived";
201
202                 case this.$scope.currentComponent.uniqueId:
203                     return this.$filter("resourceName")(this.$scope.currentComponent.name);
204
205                 default:
206                     return this.$filter("resourceName")((_.find(this.$scope.currentComponent.componentInstances, {uniqueId: key})).name);
207             }
208         };
209
210         this.$scope.getComponentInstanceNameFromInstanceByKey = (key:string):string => {
211             let instanceName:string = "";
212             if (key !== undefined && this.$scope.selectedComponent.uniqueId == this.$scope.currentComponent.selectedInstance.componentUid) {
213                 instanceName = this.$filter("resourceName")((_.find(this.$scope.selectedComponent.componentInstances, {uniqueId: key})).name);
214             }
215             return instanceName;
216         };
217
218     }
219 }