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=========================================================
20 /// <reference path="../../../../../../references"/>
21 module Sdc.ViewModels {
24 interface IResourcePropertiesAndAttributesViewModelScope extends ICompositionViewModelScope {
25 properties: Models.PropertiesGroup;
26 attributes: Models.AttributesGroup;
27 propertiesMessage: string;
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;
36 export class ResourcePropertiesViewModel {
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) {
56 private initComponentProperties = ():void => {
57 let result:Models.PropertiesGroup = {};
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";
66 result[this.$scope.currentComponent.selectedInstance.uniqueId] = this.$scope.currentComponent.componentInstancesProperties[this.$scope.currentComponent.selectedInstance.uniqueId];
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";
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);
80 property.readonly = true;
81 derived.push(property);
85 result['derived']= derived;
88 this.$scope.properties = result;
93 private initComponentAttributes = ():void => {
94 let result:Models.AttributesGroup = {};
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;
102 this.$scope.attributes = result;
106 private openEditPropertyModal = (property:Models.PropertyModel):void => {
107 let viewModelsHtmlBasePath:string = '/app/scripts/view-models/';
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',
116 property: ():Models.PropertyModel => {
119 component: ():Models.Components.Component => {
120 return this.$scope.currentComponent;
122 filteredProperties: ():Array<Models.PropertyModel> => {
123 return this.$scope.selectedComponent.properties
129 let modalInstance:ng.ui.bootstrap.IModalServiceInstance = this.$modal.open(modalOptions);
133 // this.initComponentProperties();
137 private openAttributeModal = (atrribute:Models.AttributeModel):void => {
138 let viewModelsHtmlBasePath:string = '/app/scripts/view-models/';
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',
147 attribute: ():Models.AttributeModel => {
150 component: ():Models.Components.Component => {
151 return this.$scope.currentComponent;
155 this.$modal.open(modalOptions);
161 private initScope = ():void => {
162 this.initComponentProperties();
163 this.initComponentAttributes();
165 this.$scope.$watchCollection('currentComponent.componentInstancesProperties', (newData:any):void => {
166 this.initComponentProperties();
169 this.$scope.$watchCollection('currentComponent.properties', (newData:any):void => {
170 this.initComponentProperties();
173 this.$scope.$watch('currentComponent.selectedInstance', (newInstance:Models.ComponentsInstances.ComponentInstance):void => {
174 if (angular.isDefined(newInstance)) {
175 this.initComponentProperties();
176 this.initComponentAttributes();
180 this.$scope.$watchCollection('currentComponent.componentInstancesAttributes', (newData:any):void => {
181 this.initComponentAttributes();
184 this.$scope.isPropertyOwner = ():boolean => {
185 return this.$scope.currentComponent && this.$scope.currentComponent.isResource() &&
186 !this.$scope.isComponentInstanceSelected();
189 this.$scope.addProperty = ():void => {
190 let property = new Models.PropertyModel();
191 this.openEditPropertyModal(property);
194 this.$scope.updateProperty = (property:Models.PropertyModel):void => {
195 this.openEditPropertyModal(property);
198 this.$scope.deleteProperty = (property:Models.PropertyModel):void => {
200 let onOk = ():void => {
201 this.$scope.currentComponent.deleteProperty(property.uniqueId);
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);
209 this.$scope.viewAttribute = (attribute:Models.AttributeModel):void => {
210 this.openAttributeModal(attribute);
213 this.$scope.groupNameByKey = (key:string):string => {
218 case this.$scope.currentComponent.uniqueId:
219 return this.$filter("resourceName")(this.$scope.currentComponent.name);
222 return this.$filter("resourceName")((_.find(this.$scope.currentComponent.componentInstances, {uniqueId:key})).name);