a7caeaa9fb7e2691648454054e5b78d3f5d86f31
[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 import {InputFEModel} from "../../../../models/properties-inputs/input-fe-model";
30
31 @Component({
32   selector: 'outputs-table',
33   templateUrl: './outputs-table.component.html',
34   styleUrls: ['./outputs-table.component.less']
35 })
36 export class OutputsTableComponent implements OnInit {
37   @Select(WorkspaceState.isViewOnly)
38   isViewOnly$: boolean;
39
40   @ViewChild('componentOutputsTable')
41   private table: any;
42
43   @Input() outputs: Array<OutputFEModel>;
44   @Input() instanceNamesMap: Map<string, InstanceFeDetails>;
45   @Input() readonly: boolean;
46   @Input() isLoading: boolean;
47   @Input() componentType: string;
48   @Output() outputChanged: EventEmitter<any> = new EventEmitter<any>();
49   @Output() deleteOutput: EventEmitter<any> = new EventEmitter<any>();
50   @Input() feAttributesMap: InstanceFeAttributesMap;
51
52   deleteMsgTitle: string;
53   deleteMsgBodyTxt: string;
54   modalDeleteBtn: string;
55   modalCancelBtn: string;
56   sortBy: string;
57   reverse: boolean;
58   selectedOutputToDelete: OutputFEModel;
59
60   constructor(private modalService: ModalService,
61               private dataTypeService: DataTypeService,
62               private translateService: TranslateService) {
63   }
64
65   ngOnInit() {
66     this.translateService.languageChangedObservable.subscribe((lang) => {
67       this.deleteMsgTitle = this.translateService.translate('DELETE_OUTPUT_TITLE');
68       this.modalDeleteBtn = this.translateService.translate('MODAL_DELETE');
69       this.modalCancelBtn = this.translateService.translate('MODAL_CANCEL');
70
71     });
72   }
73
74   sort = (sortBy) => {
75     this.reverse = (this.sortBy === sortBy) ? !this.reverse : true;
76     let reverse = this.reverse ? 1 : -1;
77     this.sortBy = sortBy;
78     let instanceNameMapTemp = this.instanceNamesMap;
79     let itemIdx1Val = "";
80     let itemIdx2Val = "";
81     this.outputs.sort(function (itemIdx1, itemIdx2) {
82       if (sortBy == 'instanceUniqueId') {
83         itemIdx1Val = (itemIdx1[sortBy] && instanceNameMapTemp[itemIdx1[sortBy]] !== undefined) ? instanceNameMapTemp[itemIdx1[sortBy]].name : "";
84         itemIdx2Val = (itemIdx2[sortBy] && instanceNameMapTemp[itemIdx2[sortBy]] !== undefined) ? instanceNameMapTemp[itemIdx2[sortBy]].name : "";
85       } else {
86         itemIdx1Val = itemIdx1[sortBy];
87         itemIdx2Val = itemIdx2[sortBy];
88       }
89       if (itemIdx1Val < itemIdx2Val) {
90         return -1 * reverse;
91       } else if (itemIdx1Val > itemIdx2Val) {
92         return 1 * reverse;
93       } else {
94         return 0;
95       }
96     });
97   };
98
99   onOutputChanged = (output, event) => {
100     output.updateDefaultValueObj(event.value, event.isValid);
101     this.outputChanged.emit(output);
102   };
103
104   onRequiredChanged = (output: OutputFEModel, event) => {
105     this.outputChanged.emit(output);
106   }
107
108   onDeleteOutput = () => {
109     this.deleteOutput.emit(this.selectedOutputToDelete);
110     this.modalService.closeCurrentModal();
111   };
112
113   openDeleteModal = (output: OutputFEModel) => {
114     this.selectedOutputToDelete = output;
115     this.modalService.createActionModal("Delete Output", "Are you sure you want to delete this output?", "Delete", this.onDeleteOutput, "Close").instance.open();
116   }
117
118   getConstraints(output: OutputFEModel): string[] {
119     if (output.outputPath) {
120       const pathValuesName = output.outputPath.split('#');
121       const rootPropertyName = pathValuesName[0];
122       const propertyName = pathValuesName[1];
123       let filteredRootPropertyType = _.values(this.feAttributesMap)[0].filter(property =>
124           property.name == rootPropertyName);
125       if (filteredRootPropertyType.length > 0) {
126         let rootPropertyType = filteredRootPropertyType[0].type;
127         return this.dataTypeService.getConstraintsByParentTypeAndUniqueID(rootPropertyType, propertyName);
128       } else {
129         return null;
130       }
131
132     } else {
133       return null;
134     }
135   }
136
137   checkInstanceFeAttributesMapIsFilled() {
138     return _.keys(this.feAttributesMap).length > 0
139   }
140
141 }