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