1 import { Component, Input, OnInit } from '@angular/core';
2 import { Store } from '@ngxs/store';
6 Component as TopologyTemplate,
12 import { CompositionService } from 'app/ng2/pages/composition/composition.service';
13 import { WorkspaceService } from 'app/ng2/pages/workspace/workspace.service';
14 import { GroupByPipe } from 'app/ng2/pipes/groupBy.pipe';
15 import { ResourceNamePipe } from 'app/ng2/pipes/resource-name.pipe';
16 import { TopologyTemplateService } from 'app/ng2/services/component-services/topology-template.service';
17 import { ComponentGenericResponse } from 'app/ng2/services/responses/component-generic-response';
18 import { TranslateService } from 'app/ng2/shared/translator/translate.service';
19 import { ModalsHandler } from 'app/utils';
20 import { SdcUiCommon, SdcUiComponents, SdcUiServices } from 'onap-ui-angular';
21 import {SelectedComponentType, TogglePanelLoadingAction} from "../../../common/store/graph.actions";
24 selector: 'properties-tab',
25 templateUrl: './properties-tab.component.html',
26 styleUrls: ['./properties-tab.component.less']
28 export class PropertiesTabComponent implements OnInit {
29 attributes: AttributesGroup;
30 isComponentInstanceSelected: boolean;
31 properties: PropertiesGroup;
32 groupPropertiesByInstance: boolean;
33 propertiesMessage: string;
34 metadata: ComponentMetadata;
35 objectKeys = Object.keys;
37 @Input() isViewOnly: boolean;
38 @Input() componentType: SelectedComponentType;
39 @Input() component: FullComponentInstance | TopologyTemplate;
40 @Input() input: {title: string};
42 constructor(private store: Store,
43 private workspaceService: WorkspaceService,
44 private compositionService: CompositionService,
45 private modalsHandler: ModalsHandler,
46 private topologyTemplateService: TopologyTemplateService,
47 private modalService: SdcUiServices.ModalService,
48 private translateService: TranslateService,
49 private groupByPipe: GroupByPipe) {
53 this.metadata = this.workspaceService.metadata;
54 this.isComponentInstanceSelected = this.componentType === SelectedComponentType.COMPONENT_INSTANCE;
55 this.getComponentInstancesPropertiesAndAttributes();
58 public isPropertyOwner = (): boolean => {
59 return this.component instanceof TopologyTemplate && this.component.isResource();
62 public updateProperty = (property: PropertyModel): void => {
63 this.openEditPropertyModal(property);
66 public deleteProperty = (property: PropertyModel): void => {
68 const onOk: Function = (): void => {
69 this.store.dispatch(new TogglePanelLoadingAction({isLoading: true}));
70 this.topologyTemplateService.deleteProperty(this.component.componentType, this.component.uniqueId, property.uniqueId)
71 .subscribe((response) => {
72 this.store.dispatch(new TogglePanelLoadingAction({isLoading: false}));
73 this.component.properties = this.component.properties.filter((prop) => prop.uniqueId !== property.uniqueId);
74 this.initComponentProperties();
76 this.store.dispatch(new TogglePanelLoadingAction({isLoading: false}));
80 const title: string = this.translateService.translate('PROPERTY_VIEW_DELETE_MODAL_TITLE');
81 const message: string = this.translateService.translate('PROPERTY_VIEW_DELETE_MODAL_TEXT', {name: property.name});
85 type: SdcUiCommon.ButtonType.info,
87 closeModal: true} as SdcUiComponents.ModalButtonComponent;
88 this.modalService.openInfoModal(title, message, 'delete-modal', [okButton]);
91 public groupNameByKey = (key: string): string => {
96 case this.metadata.uniqueId:
97 return ResourceNamePipe.getDisplayName(this.metadata.name);
100 return this.getComponentInstanceNameFromInstanceByKey(key);
104 public getComponentInstanceNameFromInstanceByKey = (key: string): string => {
105 let instanceName: string = '';
106 const componentInstance = this.compositionService.getComponentInstances().find((item) => item.uniqueId === key);
107 if (key !== undefined && componentInstance) {
109 instanceName = ResourceNamePipe.getDisplayName(componentInstance.name);
114 private getComponentInstancesPropertiesAndAttributes = () => {
115 this.topologyTemplateService.getComponentInstanceAttributesAndProperties(
116 this.workspaceService.metadata.uniqueId,
117 this.workspaceService.metadata.componentType)
118 .subscribe((genericResponse: ComponentGenericResponse) => {
119 this.compositionService.componentInstancesAttributes = genericResponse.componentInstancesAttributes || new AttributesGroup();
120 this.compositionService.componentInstancesProperties = genericResponse.componentInstancesProperties;
121 this.initPropertiesAndAttributes();
125 private initComponentProperties = (): void => {
126 let result: PropertiesGroup = {};
128 this.propertiesMessage = undefined;
129 this.groupPropertiesByInstance = false;
130 if (this.component instanceof FullComponentInstance) {
131 result[this.component.uniqueId] = _.orderBy(this.compositionService.componentInstancesProperties[this.component.uniqueId], ['name']);
132 if (this.component.originType === 'VF') {
133 this.groupPropertiesByInstance = true;
134 result[this.component.uniqueId] = Array.from(this.groupByPipe.transform(result[this.component.uniqueId], 'path'));
136 } else if (this.metadata.isService()) {
137 // Temporally fix to hide properties for service (UI stack when there are many properties)
138 result = this.compositionService.componentInstancesProperties;
139 this.propertiesMessage = 'Note: properties for service are disabled';
141 const componentUid = this.component.uniqueId;
142 result[componentUid] = Array<PropertyModel>();
143 const derived = Array<PropertyModel>();
144 _.forEach(this.component.properties, (property: PropertyModel) => {
145 if (componentUid === property.parentUniqueId) {
146 result[componentUid].push(property);
148 property.readonly = true;
149 derived.push(property);
152 if (derived.length) {
153 result['derived'] = derived;
155 this.objectKeys(result).forEach((key) => { result[key] = _.orderBy(result[key], ['name']); });
157 this.properties = result;
160 private initComponentAttributes = (): void => {
161 let result: AttributesGroup = {};
163 if (this.component) {
164 if (this.component instanceof FullComponentInstance) {
165 result[this.component.uniqueId] = this.compositionService.componentInstancesAttributes[this.component.uniqueId] || [];
166 } else if (this.metadata.isService()) {
167 result = this.compositionService.componentInstancesAttributes;
169 result[this.component.uniqueId] = (this.component as TopologyTemplate).attributes;
171 this.attributes = result;
172 this.objectKeys(this.attributes).forEach((key) => {
173 this.attributes[key] = _.orderBy(this.attributes[key], ['name']);
180 * This function is checking if the component is the value owner of the current property
181 * in order to notify the edit property modal which fields to disable
183 private isPropertyValueOwner = (): boolean => {
184 return this.metadata.isService() || !!this.component;
188 * The function opens the edit property modal.
189 * It checks if the property is from the VF or from one of it's resource instances and sends the needed property list.
190 * For create property reasons an empty array is transferd
192 * @param property the wanted property to edit/create
194 private openEditPropertyModal = (property: PropertyModel): void => {
195 this.modalsHandler.newOpenEditPropertyModal(property,
196 (this.isPropertyOwner() ?
197 this.properties[property.parentUniqueId] :
198 this.properties[property.resourceInstanceUniqueId]) || [],
199 this.isPropertyValueOwner(), 'component', property.resourceInstanceUniqueId).then((updatedProperty: PropertyModel) => {
200 if (updatedProperty) {
201 const oldProp = _.find(this.properties[updatedProperty.resourceInstanceUniqueId],
202 (prop: PropertyModel) => prop.uniqueId === updatedProperty.uniqueId);
203 oldProp.value = updatedProperty.value;
208 private initPropertiesAndAttributes = (): void => {
209 this.initComponentProperties();
210 this.initComponentAttributes();