Fix outputs of complex type display and delete
[sdc.git] / catalog-ui / src / app / ng2 / components / logic / outputs-table / outputs-table.component.ts
1 /*
2 * ============LICENSE_START=======================================================
3 *  Copyright (C) 2021 Nordix Foundation. All rights reserved.
4 *  ================================================================================
5 *  Licensed under the Apache License, Version 2.0 (the "License");
6 *  you may not use this file except in compliance with the License.
7 *  You may obtain a copy of the License at
8 *
9 *        http://www.apache.org/licenses/LICENSE-2.0
10 *  Unless required by applicable law or agreed to in writing, software
11 *  distributed under the License is distributed on an "AS IS" BASIS,
12 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 *  See the License for the specific language governing permissions and
14 *  limitations under the License.
15 *
16 *  SPDX-License-Identifier: Apache-2.0
17 *  ============LICENSE_END=========================================================
18 */
19
20 import {Component, EventEmitter, Input, OnInit, Output, ViewChild} from '@angular/core';
21 import {InstanceFeDetails} from "../../../../models/instance-fe-details";
22 import {ModalService} from "../../../services/modal.service";
23 import {DataTypeService} from "../../../services/data-type.service";
24 import {TranslateService} from "../../../shared/translator/translate.service";
25 import {InstanceFeAttributesMap} from "app/models/attributes-outputs/attribute-fe-map";
26 import {Select} from "@ngxs/store";
27 import {WorkspaceState} from "../../../store/states/workspace.state";
28 import {OutputFEModel} from "app/models/attributes-outputs/output-fe-model";
29
30 @Component({
31   selector: 'outputs-table',
32   templateUrl: './outputs-table.component.html',
33   styleUrls: ['./outputs-table.component.less']
34 })
35 export class OutputsTableComponent implements OnInit {
36   @Select(WorkspaceState.isViewOnly)
37   isViewOnly$: boolean;
38
39   @ViewChild('componentOutputsTable')
40   private table: any;
41
42   @Input() outputs: Array<OutputFEModel>;
43   @Input() instanceNamesMap: Map<string, InstanceFeDetails>;
44   @Input() readonly: boolean;
45   @Input() isLoading: boolean;
46   @Input() componentType: string;
47   @Output() outputChanged: EventEmitter<any> = new EventEmitter<any>();
48   @Output() deleteOutput: EventEmitter<any> = new EventEmitter<any>();
49   @Input() feAttributesMap: InstanceFeAttributesMap;
50
51   deleteMsgTitle: string;
52   modalDeleteBtn: string;
53   modalCancelBtn: string;
54   sortBy: string;
55   reverse: boolean;
56   selectedOutputToDelete: OutputFEModel;
57
58   constructor(private modalService: ModalService,
59               private dataTypeService: DataTypeService,
60               private translateService: TranslateService) {
61   }
62
63   ngOnInit() {
64     this.translateService.languageChangedObservable.subscribe((lang) => {
65       this.deleteMsgTitle = this.translateService.translate('DELETE_OUTPUT_TITLE');
66       this.modalDeleteBtn = this.translateService.translate('MODAL_DELETE');
67       this.modalCancelBtn = this.translateService.translate('MODAL_CANCEL');
68
69     });
70   }
71
72   sort = (sortBy) => {
73     this.reverse = (this.sortBy === sortBy) ? !this.reverse : true;
74     let reverse = this.reverse ? 1 : -1;
75     this.sortBy = sortBy;
76     let instanceNameMapTemp = this.instanceNamesMap;
77     let itemIdx1Val = "";
78     let itemIdx2Val = "";
79     this.outputs.sort(function (itemIdx1, itemIdx2) {
80       if (sortBy == 'instanceUniqueId') {
81         itemIdx1Val = (itemIdx1[sortBy] && instanceNameMapTemp[itemIdx1[sortBy]] !== undefined) ? instanceNameMapTemp[itemIdx1[sortBy]].name : "";
82         itemIdx2Val = (itemIdx2[sortBy] && instanceNameMapTemp[itemIdx2[sortBy]] !== undefined) ? instanceNameMapTemp[itemIdx2[sortBy]].name : "";
83       } else {
84         itemIdx1Val = itemIdx1[sortBy];
85         itemIdx2Val = itemIdx2[sortBy];
86       }
87       if (itemIdx1Val < itemIdx2Val) {
88         return -1 * reverse;
89       } else if (itemIdx1Val > itemIdx2Val) {
90         return 1 * reverse;
91       } else {
92         return 0;
93       }
94     });
95   };
96
97   onOutputChanged = (output, event) => {
98     output.updateDefaultValueObj(event.value, event.isValid);
99     this.outputChanged.emit(output);
100   };
101
102   onRequiredChanged = (output: OutputFEModel, event) => {
103     this.outputChanged.emit(output);
104   }
105
106   onDeleteOutput = () => {
107     this.deleteOutput.emit(this.selectedOutputToDelete);
108     this.modalService.closeCurrentModal();
109   };
110
111   openDeleteModal = (output: OutputFEModel) => {
112     this.selectedOutputToDelete = output;
113     this.modalService.createActionModal("Delete Output", "Are you sure you want to delete this output?", "Delete", this.onDeleteOutput, "Close").instance.open();
114   }
115
116   getConstraints(output: OutputFEModel): string[] {
117     if (output.outputPath) {
118       const pathValuesName = output.outputPath.split('#');
119       const rootPropertyName = pathValuesName[0];
120       const propertyName = pathValuesName[1];
121       let filteredRootPropertyType = _.values(this.feAttributesMap)[0].filter(property =>
122           property.name == rootPropertyName);
123       if (filteredRootPropertyType.length > 0) {
124         let rootPropertyType = filteredRootPropertyType[0].type;
125         return this.dataTypeService.getConstraintsByParentTypeAndUniqueID(rootPropertyType, propertyName);
126       } else {
127         return null;
128       }
129
130     } else {
131       return null;
132     }
133   }
134
135   checkInstanceFeAttributesMapIsFilled() {
136     return _.keys(this.feAttributesMap).length > 0
137   }
138
139 }