84769d7a62219db51a278b3b520b47b328083174
[sdc.git] /
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(() => {
122         });
123     };
124
125     private openAttributeModal = (atrribute:AttributeModel):void => {
126
127         let modalOptions:ng.ui.bootstrap.IModalSettings = {
128             template: 'app/view-models/forms/attribute-form/attribute-form-view.html',
129             controller: 'Sdc.ViewModels.AttributeFormViewModel',
130             size: 'sdc-md',
131             backdrop: 'static',
132             keyboard: false,
133             resolve: {
134                 attribute: ():AttributeModel => {
135                     return atrribute;
136                 },
137                 component: ():Component => {
138                     return this.$scope.currentComponent;
139                 }
140             }
141         };
142         this.$uibModal.open(modalOptions);
143     };
144
145     private getComponentInstancesPropertiesAndAttributes = () => {
146
147         this.ComponentServiceNg2.getComponentInstanceAttributesAndProperties(this.$scope.currentComponent).subscribe((genericResponse:ComponentGenericResponse) => {
148             this.$scope.currentComponent.componentInstancesAttributes = genericResponse.componentInstancesAttributes;
149             this.$scope.currentComponent.componentInstancesProperties = genericResponse.componentInstancesProperties;
150             this.initScope();
151         });
152     };
153
154     private initScope = ():void => {
155
156
157         this.initComponentProperties();
158         this.initComponentAttributes();
159
160         this.$scope.$watchCollection('currentComponent.properties', (newData:any):void => {
161             this.initComponentProperties();
162         });
163
164         this.$scope.$watch('currentComponent.selectedInstance', (newInstance:ComponentInstance):void => {
165             if (angular.isDefined(newInstance)) {
166                 this.initComponentProperties();
167                 this.initComponentAttributes();
168
169             }
170         });
171
172         this.$scope.isPropertyOwner = ():boolean => {
173             return this.$scope.currentComponent && this.$scope.currentComponent.isResource() && !this.$scope.isComponentInstanceSelected();
174         };
175
176         this.$scope.updateProperty = (property:PropertyModel):void => {
177             this.openEditPropertyModal(property);
178         };
179
180         this.$scope.deleteProperty = (property:PropertyModel):void => {
181
182             let onOk = ():void => {
183                 this.$scope.currentComponent.deleteProperty(property.uniqueId);
184             };
185
186             let title:string = this.$filter('translate')("PROPERTY_VIEW_DELETE_MODAL_TITLE");
187             let message:string = this.$filter('translate')("PROPERTY_VIEW_DELETE_MODAL_TEXT", "{'name': '" + property.name + "'}");
188             this.ModalsHandler.openConfirmationModal(title, message, false).then(onOk);
189         };
190
191         this.$scope.viewAttribute = (attribute:AttributeModel):void => {
192             this.openAttributeModal(attribute);
193         };
194
195         this.$scope.groupNameByKey = (key:string):string => {
196             switch (key) {
197                 case 'derived':
198                     return "Derived";
199
200                 case this.$scope.currentComponent.uniqueId:
201                     return this.$filter("resourceName")(this.$scope.currentComponent.name);
202
203                 default:
204                     return this.$filter("resourceName")((_.find(this.$scope.currentComponent.componentInstances, {uniqueId: key})).name);
205             }
206         };
207
208         this.$scope.getComponentInstanceNameFromInstanceByKey = (key:string):string => {
209             let instanceName:string = "";
210             if (key !== undefined && this.$scope.selectedComponent.uniqueId == this.$scope.currentComponent.selectedInstance.componentUid) {
211                 instanceName = this.$filter("resourceName")((_.find(this.$scope.selectedComponent.componentInstances, {uniqueId: key})).name);
212             }
213             return instanceName;
214         };
215
216     }
217 }