3 * ============LICENSE_START=======================================================
4 * Copyright (C) 2022 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 import {Component, Inject, Input, OnInit} from '@angular/core';
23 import {DataTypeModel} from "../../../../models/data-types";
24 import {DataTypeService} from "../../../services/data-type.service";
25 import {PropertyBEModel} from "../../../../models/properties-inputs/property-be-model";
26 import {Subject} from "rxjs";
27 import {debounceTime, distinctUntilChanged} from "rxjs/operators";
28 import {ModalService} from "../../../services/modal.service";
29 import {ModalModel} from "../../../../models/modal";
30 import {ButtonModel} from "../../../../models/button";
31 import {TranslateService} from "../../../shared/translator/translate.service";
32 import {AddPropertyComponent, PropertyValidationEvent} from "./add-property/add-property.component";
33 import {IWorkspaceViewModelScope} from "../../../../view-models/workspace/workspace-view-model";
34 import {SdcUiServices} from "onap-ui-angular/dist";
35 import {PropertyModel} from "../../../../models/properties";
36 import {SdcUiCommon, SdcUiComponents} from "onap-ui-angular";
37 import {ToscaTypeHelper} from "../../../../utils/tosca-type-helper";
40 selector: 'app-type-workspace-properties',
41 templateUrl: './type-workspace-properties.component.html',
42 styleUrls: ['./type-workspace-properties.component.less']
44 export class TypeWorkspacePropertiesComponent implements OnInit {
45 @Input() isViewOnly = true;
46 @Input() dataType: DataTypeModel = new DataTypeModel();
49 derivedFromName: string;
50 properties: Array<PropertyBEModel> = [];
51 filteredProperties: Array<PropertyBEModel> = [];
52 tableHeadersList: Array<TableHeader> = [];
53 tableSortBy: string = 'name';
54 tableColumnReverse: boolean = false;
55 tableFilterTerm: string = undefined;
56 tableSearchTermUpdate = new Subject<string>();
58 constructor(@Inject('$scope') private $scope: IWorkspaceViewModelScope,
59 @Inject('$state') private $state: ng.ui.IStateService,
60 protected dataTypeService: DataTypeService,
61 private modalServiceSdcUI: SdcUiServices.ModalService,
62 private modalService: ModalService,
63 private translateService: TranslateService) {
68 this.initProperties();
69 this.tableSearchTermUpdate.pipe(
71 distinctUntilChanged())
72 .subscribe(searchTerm => {
73 this.filter(searchTerm);
77 private initTable(): void {
78 this.tableHeadersList = [
79 {title: 'Name', property: 'name'},
80 {title: 'Type', property: 'type'},
81 {title: 'Schema', property: 'schema.property.type'},
82 {title: 'Required', property: 'required'},
83 {title: 'Description', property: 'description'},
85 this.tableSortBy = this.tableHeadersList[0].property;
88 private initProperties(): void {
89 this.dataTypeService.findAllProperties(this.dataType.uniqueId).subscribe(properties => {
90 this.showPropertiesMap(properties);
94 onUpdateSort(property: string): void {
95 if (this.tableSortBy === property) {
96 this.tableColumnReverse = !this.tableColumnReverse;
98 this.tableColumnReverse = false;
99 this.tableSortBy = property;
104 private sort(): void {
105 const field = this.tableSortBy;
106 this.filteredProperties.sort((property1, property2) => {
108 if (property1[field] > property2[field]) {
110 } else if (property1[field] < property2[field]) {
113 return this.tableColumnReverse ? result * -1 : result;
117 private filter(searchTerm?: string): void {
119 searchTerm = searchTerm.toLowerCase();
120 this.filteredProperties = this.properties.filter(property =>
121 property.name.toLowerCase().includes(searchTerm)
122 || property.type.toLowerCase().includes(searchTerm)
123 || (property.getSchemaType() && property.getSchemaType().toLowerCase().includes(searchTerm))
124 || (property.description && property.description.toLowerCase().includes(searchTerm))
127 this.filteredProperties = Array.from(this.properties);
132 private addProperty(property: PropertyBEModel) {
133 this.properties.push(property);
137 private updateProperty(oldProperty: PropertyBEModel, newProperty: PropertyBEModel) {
138 this.properties.forEach((value,index)=>{
139 if(value.name == oldProperty.name) this.properties.splice(index,1);
141 this.properties.push(newProperty);
145 onClickAddProperty() {
146 this.openAddPropertyModal(null, false);
149 private openAddPropertyModal(property?: PropertyBEModel, readOnly: boolean = false) {
150 const modalTitle = this.translateService.translate(property ? 'PROPERTY_EDIT_MODAL_TITLE' : 'PROPERTY_ADD_MODAL_TITLE');
151 const modalButtons = [];
152 let disableSaveButtonFlag = true;
153 let propertyFromModal: PropertyBEModel = undefined;
154 const modal = this.modalService.createCustomModal(new ModalModel(
162 modalButtons.push(new ButtonModel(this.translateService.translate('MODAL_CLOSE'), 'outline grey', () => {
163 this.modalService.closeCurrentModal();
166 modalButtons.push(new ButtonModel(this.translateService.translate('MODAL_SAVE'), 'blue',
168 disableSaveButtonFlag = true;
170 this.dataTypeService.updateProperty(this.dataType.uniqueId, propertyFromModal).subscribe(property => {
171 this.updateProperty(propertyFromModal, new PropertyBEModel(property));
175 this.dataTypeService.createProperty(this.dataType.uniqueId, propertyFromModal).subscribe(property => {
176 this.addProperty(new PropertyBEModel(property));
179 this.modalService.closeCurrentModal();
182 return disableSaveButtonFlag
186 modalButtons.push(new ButtonModel(this.translateService.translate('MODAL_CANCEL'), 'outline grey', () => {
187 this.modalService.closeCurrentModal();
191 this.modalService.addDynamicContentToModalAndBindInputs(modal, AddPropertyComponent, {
192 'readOnly': readOnly,
193 'property': property,
194 'model': this.dataType.model
196 modal.instance.dynamicContent.instance.onValidityChange.subscribe((validationEvent: PropertyValidationEvent) => {
197 disableSaveButtonFlag = !validationEvent.isValid;
198 if (validationEvent.isValid) {
199 propertyFromModal = validationEvent.property;
202 modal.instance.open();
205 onNameClick(property: PropertyBEModel) {
206 this.openAddPropertyModal(property, this.isViewOnly);
209 private showPropertiesMap(properties: Array<PropertyBEModel>): void {
210 this.properties = properties.map(value => {
211 const property = new PropertyBEModel(value);
212 if (property.defaultValue) {
213 if (!this.isTypeSimple(property.type) && typeof property.defaultValue === 'string') {
214 property.defaultValue = JSON.parse(property.defaultValue);
216 property.defaultValue = property.defaultValue;
221 this.filteredProperties = Array.from(this.properties);
225 public isTypeSimple(value:any): boolean {
226 return ToscaTypeHelper.isTypeSimple(value);
229 onConstraintChange = (constraints: any): void => {
230 if (!this.$scope.invalidMandatoryFields) {
231 this.$scope.footerButtons[0].disabled = !constraints.valid;
233 this.$scope.footerButtons[0].disabled = this.$scope.invalidMandatoryFields;
235 if (!constraints.constraints || constraints.constraints.length == 0) {
236 this.$scope.editPropertyModel.property.propertyConstraints = null;
237 this.$scope.editPropertyModel.property.constraints = null;
240 this.$scope.editPropertyModel.property.propertyConstraints = this.serializePropertyConstraints(constraints.constraints);
241 this.$scope.editPropertyModel.property.constraints = constraints.constraints;
244 private serializePropertyConstraints(constraints: any[]): string[] {
246 let stringConstraints = new Array();
247 constraints.forEach((constraint) => {
248 stringConstraints.push(JSON.stringify(constraint));
250 return stringConstraints;
255 delete(property: PropertyModel) {
256 let onOk: Function = (): void => {
257 this.dataTypeService.deleteProperty(this.dataType.uniqueId, property.uniqueId).subscribe((response) => {
258 const props = this.properties;
259 props.splice(props.findIndex(p => p.uniqueId === response), 1);
262 console.error(error);
265 let title: string = this.translateService.translate("PROPERTY_VIEW_DELETE_MODAL_TITLE");
266 let message: string = this.translateService.translate("PROPERTY_VIEW_DELETE_MODAL_TEXT", {'name': property.name});
270 type: SdcUiCommon.ButtonType.info,
273 } as SdcUiComponents.ModalButtonComponent;
274 this.modalServiceSdcUI.openInfoModal(title, message, 'delete-modal', [okButton]);
278 interface TableHeader {