1 import { Component, OnInit, ViewChild } from '@angular/core';
2 import { Select } from '@ngxs/store';
3 import { IAttributeModel } from 'app/models';
4 import * as _ from 'lodash';
5 import { SdcUiCommon, SdcUiComponents, SdcUiServices } from 'onap-ui-angular';
6 import { ModalComponent } from 'onap-ui-angular/dist/modals/modal.component';
7 import { AttributeModel } from '../../../../models';
8 import { Resource } from '../../../../models';
9 import { ModalsHandler } from '../../../../utils';
10 import { TopologyTemplateService } from '../../../services/component-services/topology-template.service';
11 import { TranslateService } from '../../../shared/translator/translate.service';
12 import { WorkspaceState } from '../../../store/states/workspace.state';
13 import { WorkspaceService } from '../workspace.service';
14 import { AttributeModalComponent } from './attribute-modal.component';
17 selector: 'attributes',
18 templateUrl: './attributes.component.html',
19 styleUrls: ['./attributes.component.less', '../../../../../assets/styles/table-style.less']
21 export class AttributesComponent implements OnInit {
23 @Select(WorkspaceState.isViewOnly)
26 @ViewChild('componentAttributesTable')
29 private componentType: string;
30 private componentUid: string;
32 private attributes: IAttributeModel[] = [];
33 private temp: IAttributeModel[] = [];
34 private customModalInstance: ModalComponent;
36 constructor(private workspaceService: WorkspaceService,
37 private topologyTemplateService: TopologyTemplateService,
38 private modalsHandler: ModalsHandler,
39 private modalService: SdcUiServices.ModalService,
40 private loaderService: SdcUiServices.LoaderService,
41 private translateService: TranslateService) {
43 this.componentType = this.workspaceService.metadata.componentType;
44 this.componentUid = this.workspaceService.metadata.uniqueId;
48 this.asyncInitComponent();
51 async asyncInitComponent() {
52 this.loaderService.activate();
53 const response = await this.topologyTemplateService.getComponentAttributes(this.componentType, this.componentUid);
54 this.attributes = response.attributes;
55 this.temp = [...response.attributes];
56 this.loaderService.deactivate();
59 getAttributes(): IAttributeModel[] {
60 return this.attributes;
63 addOrUpdateAttribute = async (attribute: AttributeModel, isEdit: boolean) => {
64 this.loaderService.activate();
65 let attributeFromServer: AttributeModel;
66 this.temp = [...this.attributes];
68 const deactivateLoader = () => {
69 this.loaderService.deactivate();
74 attributeFromServer = await this.topologyTemplateService
75 .updateAttributeAsync(this.componentType, this.componentUid, attribute)
76 .catch(deactivateLoader);
77 if (attributeFromServer) {
78 const indexOfUpdatedAttribute = _.findIndex(this.temp, (e) => e.uniqueId === attributeFromServer.uniqueId);
79 this.temp[indexOfUpdatedAttribute] = attributeFromServer;
82 attributeFromServer = await this.topologyTemplateService
83 .addAttributeAsync(this.componentType, this.componentUid, attribute)
84 .catch(deactivateLoader);
85 if (attributeFromServer) {
86 this.temp.push(attributeFromServer);
89 this.attributes = this.temp;
90 this.loaderService.deactivate();
93 deleteAttribute = async (attributeToDelete: AttributeModel) => {
94 this.loaderService.activate();
95 this.temp = [...this.attributes];
96 const res = await this.topologyTemplateService.deleteAttributeAsync(this.componentType, this.componentUid, attributeToDelete);
97 _.remove(this.temp, (attr) => attr.uniqueId === attributeToDelete.uniqueId);
98 this.attributes = this.temp;
99 this.loaderService.deactivate();
102 openAddEditModal(selectedRow: AttributeModel, isEdit: boolean) {
103 const component = new Resource(undefined, undefined, undefined);
104 component.componentType = this.componentType;
105 component.uniqueId = this.componentUid;
107 const title: string = this.translateService.translate('ATTRIBUTE_DETAILS_MODAL_TITLE');
108 const attributeModalConfig = {
111 type: SdcUiCommon.ModalType.custom,
116 // spinner_position: Placement.left,
118 callback: () => this.modalCallBack(isEdit),
122 ] as SdcUiCommon.IModalButtonComponent[]
125 this.customModalInstance = this.modalService.openCustomModal(attributeModalConfig, AttributeModalComponent, { attributeToEdit: selectedRow });
126 this.customModalInstance.innerModalContent.instance.
127 onValidationChange.subscribe((isValid) => this.customModalInstance.getButtonById('save').disabled = !isValid);
130 /***********************
131 * Call Backs from UI *
132 ***********************/
135 * Called when 'Add' is clicked
138 this.openAddEditModal(new AttributeModel(), false);
142 * Called when 'Edit' button is clicked
144 onEditAttribute(event, row) {
145 event.stopPropagation();
147 const attributeToEdit: AttributeModel = new AttributeModel(row);
148 this.openAddEditModal(attributeToEdit, true);
152 * Called when 'Delete' button is clicked
154 onDeleteAttribute(event, row: AttributeModel) {
155 event.stopPropagation();
157 this.deleteAttribute(row);
160 const title: string = this.translateService.translate('ATTRIBUTE_VIEW_DELETE_MODAL_TITLE');
161 const message: string = this.translateService.translate('ATTRIBUTE_VIEW_DELETE_MODAL_TEXT');
162 const okButton = new SdcUiComponents.ModalButtonComponent();
163 okButton.testId = 'OK';
164 okButton.text = 'OK';
165 okButton.type = SdcUiCommon.ButtonType.info;
166 okButton.closeModal = true;
167 okButton.callback = onOk;
169 this.modalService.openInfoModal(title, message, 'delete-modal', [okButton]);
173 if (event.type === 'click') {
174 this.table.rowDetail.toggleExpandRow(event.row);
179 * Callback from Modal after "Save" is clicked
181 * @param {boolean} isEdit - Whether modal is edit or add attribute
183 modalCallBack = (isEdit: boolean) => {
184 const attribute: AttributeModel = this.customModalInstance.innerModalContent.instance.attributeToEdit;
185 this.addOrUpdateAttribute(attribute, isEdit);