import { DatePipe } from "@angular/common"; import { Pipe, PipeTransform } from "@angular/core"; import { DescriptionTemplateFieldType } from "../common/enum/description-template-field-type"; @Pipe({ name: 'fieldValue' }) export class FieldValuePipe implements PipeTransform { constructor(private date: DatePipe) { } transform(controlValue: any): string | null { let value = controlValue?.value; let renderStyle = controlValue?.viewStyle?.renderStyle; if (renderStyle && value) { switch (renderStyle) { case DescriptionTemplateFieldType.CURRENCY: if (value) { return JSON.parse(value).name; } break; case DescriptionTemplateFieldType.BOOLEAN_DECISION: return value == 'true' ? 'DATASET-PROFILE-EDITOR.STEPS.FORM.FIELD.DEFAULT-VALUES.BOOLEAN-DECISION.YES' : 'DATASET-PROFILE-EDITOR.STEPS.FORM.FIELD.DEFAULT-VALUES.BOOLEAN-DECISION.NO'; case DescriptionTemplateFieldType.CHECK_BOX: return value ? 'DATASET-PROFILE-EDITOR.STEPS.FORM.FIELD.DEFAULT-VALUES.BOOLEAN-DECISION.YES' : 'DATASET-PROFILE-EDITOR.STEPS.FORM.FIELD.DEFAULT-VALUES.BOOLEAN-DECISION.NO'; case DescriptionTemplateFieldType.RADIO_BOX: if (value && controlValue.data.options) { return controlValue.data.options.find(option => option.value === value).label; } break; case DescriptionTemplateFieldType.DATE_PICKER: return this.date.transform(controlValue.value, 'dd/MM/yyyy'); case DescriptionTemplateFieldType.FREE_TEXT: return value; case DescriptionTemplateFieldType.EXTERNAL_SELECT: case DescriptionTemplateFieldType.SELECT: if (value && controlValue.data.options && !controlValue.data.multipleSelect) { return controlValue.data.options.find(option => value == option.value).label; } else if (value && controlValue.data.options && controlValue.data.multipleSelect) { return controlValue.data.options.filter(option => value.includes(option.value)).map(option => option.label).join(','); } break; case DescriptionTemplateFieldType.RICH_TEXT_AREA: if (value) { return value.replace(/ /g, ' ').replace(/(\r\n|\n|\r| +(?= ))|\s\s+/gm, " ").replace(/<[^>]*>/g, ''); } break; case DescriptionTemplateFieldType.TEXT_AREA: return value; case DescriptionTemplateFieldType.REGISTRIES: case DescriptionTemplateFieldType.SERVICES: case DescriptionTemplateFieldType.RESEARCHERS: case DescriptionTemplateFieldType.ORGANIZATIONS: case DescriptionTemplateFieldType.EXTERNAL_DATASETS: case DescriptionTemplateFieldType.DATA_REPOSITORIES: case DescriptionTemplateFieldType.PUB_REPOSITORIES: case DescriptionTemplateFieldType.JOURNAL_REPOSITORIES: case DescriptionTemplateFieldType.TAXONOMIES: case DescriptionTemplateFieldType.LICENSES: case DescriptionTemplateFieldType.PUBLICATIONS: case DescriptionTemplateFieldType.TAGS: return this.parseJson(value); case DescriptionTemplateFieldType.INTERNAL_DMP_ENTRIES_RESEARCHERS: case DescriptionTemplateFieldType.INTERNAL_DMP_ENTRIES_DMPS: case DescriptionTemplateFieldType.INTERNAL_ENTRIES_DESCRIPTIONS: return this.parseJson(value, 'label'); case DescriptionTemplateFieldType.DATASET_IDENTIFIER: case DescriptionTemplateFieldType.VALIDATION: if (value && value.identifier) { return value.identifier; } break; default: return null; } } return null; } public parseJson(value: any, field: string = 'name') { if (Array.isArray(value)) { return value.map(element => JSON.parse(element)[field]).join(','); } else { return JSON.parse(value)[field]; } } }