argos/dmp-frontend/src/app/core/pipes/field-value.pipe.ts

80 lines
3.1 KiB
TypeScript

import { DatePipe } from "@angular/common";
import { Pipe, PipeTransform } from "@angular/core";
import { DescriptionTemplateFieldType } from "../common/enum/description-template-field-type";
import { Reference } from "../model/reference/reference";
@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.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(/&nbsp;/g, ' ').replace(/(\r\n|\n|\r| +(?= ))|\s\s+/gm, " ").replace(/<[^>]*>/g, '');
}
break;
case DescriptionTemplateFieldType.TEXT_AREA:
return value;
case DescriptionTemplateFieldType.REFERENCE_TYPES:
return (value as Reference)?.label;
case DescriptionTemplateFieldType.EXTERNAL_DATASETS:
case DescriptionTemplateFieldType.TAGS:
return this.parseJson(value);
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];
}
}
}