aef25c51ce0f0054bdb2d062d7ca4dd83780a5eb
[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 /// <reference path="../../../../../../references"/>
21 module Sdc.ViewModels {
22     'use strict';
23
24     interface IResourcePropertiesAndAttributesViewModelScope extends ICompositionViewModelScope {
25         properties: Models.PropertiesGroup;
26         attributes: Models.AttributesGroup;
27         propertiesMessage: string;
28         addProperty(): void;
29         updateProperty(property:Models.PropertyModel): void;
30         deleteProperty(property:Models.PropertyModel): void;
31         viewAttribute(attribute:Models.AttributeModel): void;
32         groupNameByKey(key:string): string;
33         isPropertyOwner():boolean;
34     }
35
36     export class ResourcePropertiesViewModel {
37
38         static '$inject' = [
39             '$scope',
40             '$filter',
41             '$modal',
42             '$templateCache',
43             'ModalsHandler'
44         ];
45
46
47         constructor(private $scope:IResourcePropertiesAndAttributesViewModelScope,
48                     private $filter:ng.IFilterService,
49                     private $modal:ng.ui.bootstrap.IModalService,
50                     private $templateCache:ng.ITemplateCacheService,
51                     private ModalsHandler: Utils.ModalsHandler) {
52
53             this.initScope();
54         }
55
56         private initComponentProperties = ():void => {
57             let result:Models.PropertiesGroup = {};
58
59             if(this.$scope.selectedComponent){
60                 this.$scope.propertiesMessage = undefined;
61                 if(this.$scope.isComponentInstanceSelected()){
62                     if (this.$scope.currentComponent.selectedInstance.originType==='VF') {
63                         // Temporally fix to hide properties for VF (UI stack when there are many properties)
64                         this.$scope.propertiesMessage = "Note: properties for VF are disabled";
65                     } else {
66                         result[this.$scope.currentComponent.selectedInstance.uniqueId] = this.$scope.currentComponent.componentInstancesProperties[this.$scope.currentComponent.selectedInstance.uniqueId];
67                     }
68                 }else if(this.$scope.currentComponent.isService()){
69                     // Temporally fix to hide properties for service (UI stack when there are many properties)
70                     //result = this.$scope.currentComponent.componentInstancesProperties;
71                     this.$scope.propertiesMessage = "Note: properties for service are disabled";
72                 }else{
73                     let key = this.$scope.selectedComponent.uniqueId;
74                     result[key]= Array<Models.PropertyModel>();
75                     let derived = Array<Models.PropertyModel>();
76                     _.forEach(this.$scope.selectedComponent.properties, (property:Models.PropertyModel) => {
77                         if(key == property.parentUniqueId){
78                             result[key].push(property);
79                         }else{
80                             property.readonly = true;
81                             derived.push(property);
82                         }
83                     });
84                     if(derived.length){
85                         result['derived']= derived;
86                     }
87                 }
88                 this.$scope.properties = result;
89             }
90         };
91
92
93         private initComponentAttributes = ():void => {
94             let result:Models.AttributesGroup = {};
95
96             if(this.$scope.selectedComponent){
97                 if(this.$scope.isComponentInstanceSelected()){
98                     result[this.$scope.currentComponent.selectedInstance.uniqueId] = this.$scope.currentComponent.componentInstancesAttributes[this.$scope.currentComponent.selectedInstance.uniqueId];
99                 }else if(this.$scope.currentComponent.isService()){
100                     result = this.$scope.currentComponent.componentInstancesAttributes;
101                 }
102                 this.$scope.attributes = result;
103             }
104         };
105
106         private openEditPropertyModal = (property:Models.PropertyModel):void => {
107             let viewModelsHtmlBasePath:string = '/app/scripts/view-models/';
108
109             let modalOptions:ng.ui.bootstrap.IModalSettings = {
110                 template: this.$templateCache.get(viewModelsHtmlBasePath + 'forms/property-form/property-form-view.html'),
111                 controller: 'Sdc.ViewModels.PropertyFormViewModel',
112                 size: 'sdc-l',
113                 backdrop: 'static',
114                 keyboard: false,
115                 resolve: {
116                     property: ():Models.PropertyModel => {
117                         return property;
118                     },
119                     component: ():Models.Components.Component => {
120                         return this.$scope.currentComponent;
121                     },
122                     filteredProperties: ():Array<Models.PropertyModel> => {
123                         return this.$scope.selectedComponent.properties
124                     }
125                 }
126             };
127
128
129             let modalInstance:ng.ui.bootstrap.IModalServiceInstance = this.$modal.open(modalOptions);
130             modalInstance
131                 .result
132                 .then(():void => {
133                   //  this.initComponentProperties();
134                 });
135         };
136
137         private openAttributeModal = (atrribute:Models.AttributeModel):void => {
138             let viewModelsHtmlBasePath:string = '/app/scripts/view-models/';
139
140             let modalOptions:ng.ui.bootstrap.IModalSettings = {
141                 template: this.$templateCache.get(viewModelsHtmlBasePath + 'forms/attribute-form/attribute-form-view.html'),
142                 controller: 'Sdc.ViewModels.AttributeFormViewModel',
143                 size: 'sdc-md',
144                 backdrop: 'static',
145                 keyboard: false,
146                 resolve: {
147                     attribute: ():Models.AttributeModel => {
148                         return atrribute;
149                     },
150                     component: ():Models.Components.Component => {
151                         return this.$scope.currentComponent;
152                     }
153                 }
154             };
155             this.$modal.open(modalOptions);
156         };
157
158
159
160
161         private initScope = ():void => {
162             this.initComponentProperties();
163             this.initComponentAttributes();
164
165             this.$scope.$watchCollection('currentComponent.componentInstancesProperties', (newData:any):void => {
166                 this.initComponentProperties();
167             });
168
169             this.$scope.$watchCollection('currentComponent.properties', (newData:any):void => {
170                 this.initComponentProperties();
171             });
172
173             this.$scope.$watch('currentComponent.selectedInstance', (newInstance:Models.ComponentsInstances.ComponentInstance):void => {
174                 if (angular.isDefined(newInstance)) {
175                     this.initComponentProperties();
176                     this.initComponentAttributes();
177                 }
178             });
179
180             this.$scope.$watchCollection('currentComponent.componentInstancesAttributes', (newData:any):void => {
181                 this.initComponentAttributes();
182             });
183
184             this.$scope.isPropertyOwner = ():boolean => {
185                 return this.$scope.currentComponent && this.$scope.currentComponent.isResource() &&
186                     !this.$scope.isComponentInstanceSelected();
187             };
188
189             this.$scope.addProperty = ():void => {
190                 let property = new Models.PropertyModel();
191                 this.openEditPropertyModal(property);
192             };
193
194             this.$scope.updateProperty = (property:Models.PropertyModel):void => {
195                 this.openEditPropertyModal(property);
196             };
197
198             this.$scope.deleteProperty = (property:Models.PropertyModel):void => {
199
200                 let onOk = ():void => {
201                     this.$scope.currentComponent.deleteProperty(property.uniqueId);
202                 };
203
204                 let title:string =  this.$filter('translate')("PROPERTY_VIEW_DELETE_MODAL_TITLE");
205                 let message:string = this.$filter('translate')("PROPERTY_VIEW_DELETE_MODAL_TEXT", "{'name': '" + property.name + "'}");
206                 this.ModalsHandler.openConfirmationModal(title, message, false).then(onOk);
207             };
208
209             this.$scope.viewAttribute = (attribute:Models.AttributeModel):void => {
210                 this.openAttributeModal(attribute);
211             };
212
213             this.$scope.groupNameByKey = (key:string):string => {
214                 switch (key){
215                     case 'derived':
216                         return "Derived";
217
218                     case this.$scope.currentComponent.uniqueId:
219                         return this.$filter("resourceName")(this.$scope.currentComponent.name);
220
221                     default:
222                         return this.$filter("resourceName")((_.find(this.$scope.currentComponent.componentInstances, {uniqueId:key})).name);
223                 }
224             };
225
226         }
227     }
228 }