2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
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";
35 interface IResourcePropertiesAndAttributesViewModelScope extends ICompositionViewModelScope {
36 properties:PropertiesGroup;
37 attributes:AttributesGroup;
38 propertiesMessage:string;
39 groupPropertiesByInstance:boolean;
40 showGroupsOfInstanceProperties:Array<boolean>;
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;
50 export class ResourcePropertiesViewModel {
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) {
68 this.getComponentInstancesPropertiesAndAttributes();
71 private initComponentProperties = ():void => {
72 let result:PropertiesGroup = {};
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;
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";
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);
94 property.readonly = true;
95 derived.push(property);
99 result['derived'] = derived;
102 this.$scope.properties = result;
107 private initComponentAttributes = ():void => {
108 let result:AttributesGroup = {};
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;
116 this.$scope.attributes = result;
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
124 private isPropertyValueOwner = ():boolean => {
125 return this.$scope.currentComponent.isService() || !!this.$scope.currentComponent.selectedInstance;
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
133 * @param property the wanted property to edit/create
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;
147 private openAttributeModal = (atrribute:AttributeModel):void => {
149 let modalOptions:ng.ui.bootstrap.IModalSettings = {
150 template: 'app/view-models/forms/attribute-form/attribute-form-view.html',
151 controller: 'Sdc.ViewModels.AttributeFormViewModel',
156 attribute: ():AttributeModel => {
159 component: ():Component => {
160 return this.$scope.currentComponent;
164 this.$uibModal.open(modalOptions);
167 private getComponentInstancesPropertiesAndAttributes = () => {
169 this.ComponentServiceNg2.getComponentInstanceAttributesAndProperties(this.$scope.currentComponent).subscribe((genericResponse:ComponentGenericResponse) => {
170 this.$scope.currentComponent.componentInstancesAttributes = genericResponse.componentInstancesAttributes;
171 this.$scope.currentComponent.componentInstancesProperties = genericResponse.componentInstancesProperties;
176 private initScope = ():void => {
179 this.initComponentProperties();
180 this.initComponentAttributes();
182 this.$scope.$watchCollection('currentComponent.properties', (newData:any):void => {
183 this.initComponentProperties();
186 this.$scope.$watch('currentComponent.selectedInstance', (newInstance:ComponentInstance):void => {
187 if (angular.isDefined(newInstance)) {
188 this.initComponentProperties();
189 this.initComponentAttributes();
194 this.$scope.isPropertyOwner = ():boolean => {
195 return this.$scope.currentComponent && this.$scope.currentComponent.isResource() && !this.$scope.isComponentInstanceSelected();
198 this.$scope.updateProperty = (property:PropertyModel):void => {
199 this.openEditPropertyModal(property);
202 this.$scope.deleteProperty = (property:PropertyModel):void => {
204 let onOk = ():void => {
205 this.$scope.currentComponent.deleteProperty(property.uniqueId);
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);
213 this.$scope.viewAttribute = (attribute:AttributeModel):void => {
214 this.openAttributeModal(attribute);
217 this.$scope.groupNameByKey = (key:string):string => {
222 case this.$scope.currentComponent.uniqueId:
223 return this.$filter("resourceName")(this.$scope.currentComponent.name);
226 return this.$filter("resourceName")((_.find(this.$scope.currentComponent.componentInstances, {uniqueId: key})).name);
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);