import { Component, Inject, OnChanges, OnInit, SimpleChanges } from '@angular/core'; import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog'; import { ConstControlsList, IFormBuilderCell } from '../../../models/control'; @Component({ selector: 'dr-set-options', templateUrl: './set-options.component.html', styleUrls: ['./set-options.component.scss'] }) export class SetOptionsComponent implements OnInit, OnChanges { formSelected: number = 0; showFormTable = true; control: IFormBuilderCell; optionObj: { endpoint?: string, options?: any[], optionsString?: string; valueName?: string, descriptionName?: string, }; constructor( public dialogRef: MatDialogRef, @Inject(MAT_DIALOG_DATA) public data: IFormBuilderCell) { this.control = data; } ngOnInit(): void { this.optionObj = { ...this.control }; if (!this.optionObj.options) { this.optionObj.options = [...ConstControlsList.find(f => f.type === 'SELECT').options]; } if (((!this.optionObj.descriptionName) && (this.optionObj.valueName)) || ((this.optionObj.descriptionName) && (!this.optionObj.valueName)) || (this.optionObj.descriptionName === this.optionObj.valueName)) { if (this.optionObj.options && this.optionObj.options.length > 0 && typeof (this.optionObj.options[0]) !== 'object') { this.optionObj.valueName ||= this.optionObj.descriptionName; this.optionObj.options = this.optionObj.options.map(m => ({ [this.optionObj.valueName]: m })); delete this.optionObj.descriptionName; } } this.optionObj.optionsString = JSON.stringify(this.optionObj.options); } ngOnChanges(changes: SimpleChanges) { console.warn(changes.optionObj); } fnHaveKey(): boolean { const ks: string[] = []; this.optionObj.options.forEach(f => Object.keys(f).forEach(k => (!ks.includes(k)) ? ks.push(k) : null)); return (ks.includes(this.optionObj.descriptionName) || ks.includes(this.optionObj.valueName)); } fnInvalidJson(): string { try { const a = JSON.parse(this.optionObj.optionsString); return ''; } catch { return 'Invalid format. Insert a valid JSON.'; } } fnOptionsName(): string[] { const names: string[] = []; this.optionObj.options.forEach(f => Object.keys(f).forEach(k => (!names.includes(k)) ? names.push(k) : null)); try { JSON.parse(this.optionObj.optionsString).forEach(f => Object.keys(f).forEach(k => (!names.includes(k)) ? names.push(k) : null)); } catch { } return names; } fnTabChange(index: number): void { if (this.formSelected === 0) { this.optionObj.options = JSON.parse(this.optionObj.optionsString); } if (this.formSelected === 1) { this.optionObj.optionsString = JSON.stringify(this.optionObj.options); } this.formSelected = index; } onSubmit(): void { if (((!this.optionObj.descriptionName) && (this.optionObj.valueName)) || ((this.optionObj.descriptionName) && (!this.optionObj.valueName)) || (this.optionObj.descriptionName === this.optionObj.valueName)) { this.optionObj.valueName ||= this.optionObj.descriptionName; this.optionObj.valueName ||= this.optionObj.options.length > 0 && this.optionObj.options[0] ? Object.keys(this.optionObj.options[0])[0] : this.optionObj.valueName; this.optionObj.options = this.optionObj.options.map(m => m[this.optionObj.valueName]); delete this.optionObj.descriptionName; } this.control.valueName = this.optionObj.valueName; this.control.descriptionName = this.optionObj.descriptionName; switch (this.formSelected) { case 0: // Json this.control.options = JSON.parse(this.optionObj.optionsString); delete this.control.endpoint; break; case 1: // Table this.control.options = this.optionObj.options; delete this.control.endpoint; break; case 2: // Endpoint this.control.endpoint = this.optionObj.endpoint; delete this.control.options; break; } this.dialogRef.close(this.control); } }