argos/dmp-frontend/src/app/ui/description/editor/description-editor.model.ts

751 lines
30 KiB
TypeScript
Raw Normal View History

2024-02-08 16:23:03 +01:00
import { FormControl, UntypedFormArray, UntypedFormBuilder, UntypedFormGroup, Validators } from "@angular/forms";
2023-12-06 22:10:01 +01:00
import { DescriptionStatus } from "@app/core/common/enum/description-status";
2024-04-19 17:37:06 +02:00
import { DescriptionTemplateFieldType } from "@app/core/common/enum/description-template-field-type";
2024-02-07 13:15:25 +01:00
import { IsActive } from "@app/core/common/enum/is-active.enum";
import { DescriptionTemplate, DescriptionTemplateField, DescriptionTemplateFieldSet, DescriptionTemplateSection } from "@app/core/model/description-template/description-template";
import { Description, DescriptionExternalIdentifier, DescriptionExternalIdentifierPersist, DescriptionField, DescriptionFieldPersist, DescriptionPersist, DescriptionPropertyDefinition, DescriptionPropertyDefinitionFieldSet, DescriptionPropertyDefinitionFieldSetItem, DescriptionPropertyDefinitionFieldSetItemPersist, DescriptionPropertyDefinitionFieldSetPersist, DescriptionPropertyDefinitionPersist, DescriptionReference, DescriptionReferencePersist } from "@app/core/model/description/description";
2023-12-06 22:10:01 +01:00
import { ReferencePersist } from "@app/core/model/reference/reference";
import { BaseEditorModel } from "@common/base/base-form-editor-model";
import { BackendErrorValidator } from '@common/forms/validation/custom-validator';
import { ValidationErrorModel } from '@common/forms/validation/error-model/validation-error-model';
import { Validation, ValidationContext } from '@common/forms/validation/validation-context';
import { Guid } from "@common/types/guid";
export class DescriptionEditorModel extends BaseEditorModel implements DescriptionPersist {
label: string;
dmpId: Guid;
dmpDescriptionTemplateId: Guid;
descriptionTemplateId: Guid;
status: DescriptionStatus;
description: string;
2024-02-08 16:23:03 +01:00
properties: DescriptionPropertyDefinitionEditorModel = new DescriptionPropertyDefinitionEditorModel(this.validationErrorModel);
tags: string[] = [];
2023-12-06 22:10:01 +01:00
permissions: string[];
public validationErrorModel: ValidationErrorModel = new ValidationErrorModel();
protected formBuilder: UntypedFormBuilder = new UntypedFormBuilder();
constructor() { super(); }
2024-01-25 19:55:42 +01:00
public fromModel(item: Description, descriptionTemplate?: DescriptionTemplate): DescriptionEditorModel {
2023-12-06 22:10:01 +01:00
if (item) {
super.fromModel(item);
this.label = item.label;
this.dmpId = item.dmp?.id;
this.dmpDescriptionTemplateId = item.dmpDescriptionTemplate?.id;
this.descriptionTemplateId = item.descriptionTemplate?.id;
2024-02-08 08:40:16 +01:00
this.status = item.status ?? DescriptionStatus.Draft;
2023-12-06 22:10:01 +01:00
this.description = item.description;
this.tags = item.descriptionTags?.map(x => x.tag?.label);
2024-02-08 16:23:03 +01:00
this.properties = new DescriptionPropertyDefinitionEditorModel(this.validationErrorModel).fromModel(item.properties, descriptionTemplate, item.descriptionReferences);
2023-12-06 22:10:01 +01:00
}
return this;
}
buildForm(context: ValidationContext = null, disabled: boolean = false): UntypedFormGroup {
if (context == null) { context = this.createValidationContext(); }
return this.formBuilder.group({
id: [{ value: this.id, disabled: disabled }, context.getValidation('id').validators],
label: [{ value: this.label, disabled: disabled }, context.getValidation('label').validators],
dmpId: [{ value: this.dmpId, disabled: disabled }, context.getValidation('dmpId').validators],
dmpDescriptionTemplateId: [{ value: this.dmpDescriptionTemplateId, disabled: disabled }, context.getValidation('dmpDescriptionTemplateId').validators],
descriptionTemplateId: [{ value: this.descriptionTemplateId, disabled: disabled }, context.getValidation('descriptionTemplateId').validators],
status: [{ value: this.status, disabled: disabled }, context.getValidation('status').validators],
description: [{ value: this.description, disabled: disabled }, context.getValidation('description').validators],
tags: [{ value: this.tags, disabled: disabled }, context.getValidation('tags').validators],
2024-02-02 09:29:38 +01:00
properties: this.buildProperties(),
2023-12-06 22:10:01 +01:00
hash: [{ value: this.hash, disabled: disabled }, context.getValidation('hash').validators]
});
}
2024-02-02 09:29:38 +01:00
buildProperties() {
return this.properties.buildForm({
rootPath: `properties.`
});
}
2023-12-06 22:10:01 +01:00
createValidationContext(): ValidationContext {
const baseContext: ValidationContext = new ValidationContext();
const baseValidationArray: Validation[] = new Array<Validation>();
baseValidationArray.push({ key: 'id', validators: [BackendErrorValidator(this.validationErrorModel, 'id')] });
baseValidationArray.push({ key: 'label', validators: [Validators.required, BackendErrorValidator(this.validationErrorModel, 'label')] });
baseValidationArray.push({ key: 'dmpId', validators: [Validators.required, BackendErrorValidator(this.validationErrorModel, 'dmpId')] });
baseValidationArray.push({ key: 'dmpDescriptionTemplateId', validators: [Validators.required, BackendErrorValidator(this.validationErrorModel, 'dmpDescriptionTemplateId')] });
baseValidationArray.push({ key: 'descriptionTemplateId', validators: [Validators.required, BackendErrorValidator(this.validationErrorModel, 'descriptionTemplateId')] });
baseValidationArray.push({ key: 'status', validators: [Validators.required, BackendErrorValidator(this.validationErrorModel, 'status')] });
2024-03-15 08:39:36 +01:00
baseValidationArray.push({ key: 'description', validators: [BackendErrorValidator(this.validationErrorModel, 'description')] });
baseValidationArray.push({ key: 'tags', validators: [BackendErrorValidator(this.validationErrorModel, 'tags')] });
2023-12-06 22:10:01 +01:00
baseValidationArray.push({ key: 'hash', validators: [] });
baseContext.validation = baseValidationArray;
return baseContext;
}
2024-02-08 16:23:03 +01:00
static reApplyPropertiesValidators(params: {
formGroup: UntypedFormGroup,
validationErrorModel: ValidationErrorModel,
}): void {
const { formGroup, validationErrorModel } = params;
const control = formGroup?.get('properties');
DescriptionPropertyDefinitionEditorModel.reapplyValidators({
formGroup: control.get('fieldSets') as UntypedFormGroup,
2024-02-08 16:23:03 +01:00
rootPath: `properties.`,
validationErrorModel: validationErrorModel
});
}
2023-12-06 22:10:01 +01:00
}
2024-02-02 09:29:38 +01:00
export class DescriptionPropertyDefinitionEditorModel implements DescriptionPropertyDefinitionPersist {
fieldSets: Map<string, DescriptionPropertyDefinitionFieldSetEditorModel> = new Map<string, DescriptionPropertyDefinitionFieldSetEditorModel>;
2023-12-06 22:10:01 +01:00
protected formBuilder: UntypedFormBuilder = new UntypedFormBuilder();
constructor(
public validationErrorModel: ValidationErrorModel = new ValidationErrorModel()
) { }
2024-02-07 13:15:25 +01:00
public fromModel(item: DescriptionPropertyDefinition, descriptionTemplate: DescriptionTemplate, descriptionReferences: DescriptionReference[]): DescriptionPropertyDefinitionEditorModel {
this.fieldSets = this.calculateProperties(item, descriptionTemplate, descriptionReferences);
2024-02-02 09:29:38 +01:00
return this;
}
buildForm(params?: {
context?: ValidationContext,
disabled?: boolean,
rootPath?: string
}): UntypedFormGroup {
let { context = null, disabled = false, rootPath } = params ?? {}
if (context == null) {
context = DescriptionPropertyDefinitionEditorModel.createValidationContext({
validationErrorModel: this.validationErrorModel,
rootPath
});
}
const formGroup = this.formBuilder.group({});
const fieldSetsFormGroup = this.formBuilder.group({});
if (this.fieldSets.size > 0) {
this.fieldSets.forEach((value, key) => fieldSetsFormGroup.addControl(key.toString(), value.buildForm({
rootPath: `${rootPath}fieldSets[${key}].`
})), context.getValidation('fieldSets'));
formGroup.addControl('fieldSets', fieldSetsFormGroup);
}
return formGroup;
}
static createValidationContext(params: {
rootPath?: string,
validationErrorModel: ValidationErrorModel
}): ValidationContext {
const { rootPath = '', validationErrorModel } = params;
const baseContext: ValidationContext = new ValidationContext();
const baseValidationArray: Validation[] = new Array<Validation>();
baseValidationArray.push({ key: 'fieldSets', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}fieldSets`)] });
baseContext.validation = baseValidationArray;
return baseContext;
}
2024-02-08 16:23:03 +01:00
static reapplyValidators(params: {
formGroup: UntypedFormGroup,
2024-02-08 16:23:03 +01:00
validationErrorModel: ValidationErrorModel,
rootPath: string
}): void {
2024-02-21 16:21:17 +01:00
const { formGroup, rootPath, validationErrorModel } = params;
const keys = Object.keys(formGroup.value as Object);
keys.forEach((key) => {
const formArray = formGroup?.get(key);
DescriptionPropertyDefinitionFieldSetEditorModel.reapplyValidators({
formArray: formArray.get('items') as UntypedFormArray,
rootPath: `${rootPath}fieldSets[${key}].`,
validationErrorModel: validationErrorModel
})
2024-02-21 16:21:17 +01:00
});
2024-02-08 16:23:03 +01:00
}
2024-02-07 13:15:25 +01:00
private calculateProperties(item: DescriptionPropertyDefinition, descriptionTemplate: DescriptionTemplate, descriptionReferences: DescriptionReference[]): Map<string, DescriptionPropertyDefinitionFieldSetEditorModel> {
2024-02-02 09:29:38 +01:00
let result: Map<string, DescriptionPropertyDefinitionFieldSetEditorModel> = new Map<string, DescriptionPropertyDefinitionFieldSetEditorModel>();
if (descriptionTemplate) (
descriptionTemplate.definition.pages.forEach(definitionPage => {
definitionPage.sections.forEach(definitionSection => {
2024-02-07 13:15:25 +01:00
const sectionResult = this.calculateSectionProperties(definitionSection, item, descriptionReferences);
2024-02-02 09:29:38 +01:00
if (sectionResult != null) {
result = new Map([...result, ...sectionResult]);
}
})
})
)
return result;
}
2024-02-07 13:15:25 +01:00
private calculateSectionProperties(definitionSection: DescriptionTemplateSection, item: DescriptionPropertyDefinition, descriptionReferences: DescriptionReference[]): Map<string, DescriptionPropertyDefinitionFieldSetEditorModel> {
2024-02-02 09:29:38 +01:00
if (definitionSection == null) return null;
let result: Map<string, DescriptionPropertyDefinitionFieldSetEditorModel> = new Map<string, DescriptionPropertyDefinitionFieldSetEditorModel>();
definitionSection?.fieldSets?.forEach(definitionFieldSet => {
2024-02-07 13:15:25 +01:00
const fieldSetResult = this.calculateFieldSetProperties(definitionFieldSet, item, descriptionReferences);
2024-02-02 09:29:38 +01:00
if (fieldSetResult != null) {
2024-02-02 17:43:24 +01:00
result.set(definitionFieldSet.id, fieldSetResult);
2024-02-02 09:29:38 +01:00
}
});
if (definitionSection.sections != null && definitionSection.sections?.length > 0) {
definitionSection.sections.forEach(nestedDefinitionSection => {
2024-02-07 13:15:25 +01:00
const nestedSectionResult = this.calculateSectionProperties(nestedDefinitionSection, item, descriptionReferences);
2024-02-02 09:29:38 +01:00
if (nestedSectionResult != null) {
result = new Map([...result, ...nestedSectionResult]);
}
});
}
return result;
}
2024-02-07 13:15:25 +01:00
public calculateFieldSetProperties(definitionFieldSet: DescriptionTemplateFieldSet, item: DescriptionPropertyDefinition, descriptionReferences: DescriptionReference[]): DescriptionPropertyDefinitionFieldSetEditorModel {
2024-02-02 09:29:38 +01:00
if (definitionFieldSet == null) return null;
// current saved values
const fieldSetValue: DescriptionPropertyDefinitionFieldSet = item?.fieldSets[definitionFieldSet.id] ?? {};
// new item case, where we need to add controls for all the containing fields.
if (fieldSetValue.items == null || fieldSetValue.items?.length == 0) {
const fields = new Map<string, DescriptionField>();
definitionFieldSet.fields.forEach(definitionField => {
2024-02-07 13:15:25 +01:00
fields.set(definitionField.id, {
textValue: undefined,
textListValue: undefined,
dateValue: undefined,
externalIdentifier: undefined,
2024-02-21 16:52:50 +01:00
references: undefined
2024-02-07 13:15:25 +01:00
});
2024-02-02 09:29:38 +01:00
})
fieldSetValue.items = [{
2024-02-08 16:48:44 +01:00
fields: fields,
ordinal: 0
2024-02-02 09:29:38 +01:00
} as DescriptionPropertyDefinitionFieldSetItem]
}
2024-04-18 16:12:41 +02:00
// preffiling item case we ned to ensure tha all fields of fieldset are contained
for (let i = 0; i < definitionFieldSet.fields.length; i++) {
const definitionField = definitionFieldSet.fields[i];
for (let j = 0; j < fieldSetValue.items.length; j++) {
const fieldSetValueItem = fieldSetValue.items[j];
const descriptionField = fieldSetValueItem.fields[definitionField.id];
if (!descriptionField) {
fieldSetValueItem.fields[definitionField.id] = {
textValue: undefined,
textListValue: undefined,
dateValue: undefined,
externalIdentifier: undefined,
references: undefined
};
}
}
}
2024-02-08 16:23:03 +01:00
return new DescriptionPropertyDefinitionFieldSetEditorModel(this.validationErrorModel).fromModel(fieldSetValue, descriptionReferences, definitionFieldSet);
2024-02-02 09:29:38 +01:00
}
}
export class DescriptionPropertyDefinitionFieldSetEditorModel implements DescriptionPropertyDefinitionFieldSetPersist {
items?: DescriptionPropertyDefinitionFieldSetItemEditorModel[] = [];
protected formBuilder: UntypedFormBuilder = new UntypedFormBuilder();
constructor(
public validationErrorModel: ValidationErrorModel = new ValidationErrorModel()
) { }
2024-02-07 13:15:25 +01:00
public fromModel(item: DescriptionPropertyDefinitionFieldSet, descriptionReferences: DescriptionReference[], definitionFieldSet: DescriptionTemplateFieldSet): DescriptionPropertyDefinitionFieldSetEditorModel {
2023-12-06 22:10:01 +01:00
if (item) {
2024-02-08 16:23:03 +01:00
if (item.items) { item.items.map(x => this.items.push(new DescriptionPropertyDefinitionFieldSetItemEditorModel(this.validationErrorModel).fromModel(x, descriptionReferences, definitionFieldSet))); }
2023-12-06 22:10:01 +01:00
}
return this;
}
buildForm(params?: {
context?: ValidationContext,
disabled?: boolean,
rootPath?: string
}): UntypedFormGroup {
let { context = null, disabled = false, rootPath } = params ?? {}
if (context == null) {
2024-02-02 09:29:38 +01:00
context = DescriptionPropertyDefinitionFieldSetEditorModel.createValidationContext({
validationErrorModel: this.validationErrorModel,
rootPath
});
}
return this.formBuilder.group({
items: this.formBuilder.array(
(this.items ?? []).map(
(item, index) => item.buildForm({
rootPath: `${rootPath}items[${index}].`
})
2024-02-08 16:23:03 +01:00
), context.getValidation('items').validators
2024-02-02 09:29:38 +01:00
)
});
}
static createValidationContext(params: {
rootPath?: string,
validationErrorModel: ValidationErrorModel
}): ValidationContext {
const { rootPath = '', validationErrorModel } = params;
const baseContext: ValidationContext = new ValidationContext();
const baseValidationArray: Validation[] = new Array<Validation>();
baseValidationArray.push({ key: 'items', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}items`)] });
baseContext.validation = baseValidationArray;
return baseContext;
}
2024-02-08 16:23:03 +01:00
static reapplyValidators(params: {
formArray: UntypedFormArray,
validationErrorModel: ValidationErrorModel,
rootPath: string
}): void {
const { validationErrorModel, rootPath, formArray } = params;
formArray?.controls?.forEach(
(control, index) => DescriptionPropertyDefinitionFieldSetItemEditorModel.reapplyValidators({
formGroup: control as UntypedFormGroup,
rootPath: `${rootPath}items[${index}].`,
validationErrorModel: validationErrorModel
})
);
}
2024-02-02 09:29:38 +01:00
}
export class DescriptionPropertyDefinitionFieldSetItemEditorModel implements DescriptionPropertyDefinitionFieldSetItemPersist {
fields: Map<string, DescriptionFieldEditorModel> = new Map<string, DescriptionFieldEditorModel>;
comment?: string;
ordinal?: number;
protected formBuilder: UntypedFormBuilder = new UntypedFormBuilder();
constructor(
public validationErrorModel: ValidationErrorModel = new ValidationErrorModel()
) { }
2024-02-07 13:15:25 +01:00
public fromModel(item: DescriptionPropertyDefinitionFieldSetItem, descriptionReferences: DescriptionReference[], definitionFieldSet: DescriptionTemplateFieldSet): DescriptionPropertyDefinitionFieldSetItemEditorModel {
2024-02-02 09:29:38 +01:00
if (item) {
this.comment = item.comment;
this.ordinal = item.ordinal;
2024-02-21 16:21:17 +01:00
if (item.fields) {
//TODO: don't like it. Find a common way to parse it either its Map or json.
if (item.fields instanceof Map)
new Map(item.fields)?.forEach((value, key) => this.fields.set(key, new DescriptionFieldEditorModel(this.validationErrorModel).fromModel(value, definitionFieldSet?.fields?.find(x => x.id == key), descriptionReferences)));
else
Object.keys(item.fields)?.forEach((key) => this.fields.set(key, new DescriptionFieldEditorModel(this.validationErrorModel).fromModel(item.fields[key], definitionFieldSet?.fields?.find(x => x.id == key), descriptionReferences)));
}
2024-02-02 09:29:38 +01:00
}
return this;
}
buildForm(params?: {
context?: ValidationContext,
disabled?: boolean,
rootPath?: string
}): UntypedFormGroup {
let { context = null, disabled = false, rootPath } = params ?? {}
if (context == null) {
context = DescriptionPropertyDefinitionFieldSetItemEditorModel.createValidationContext({
2023-12-06 22:10:01 +01:00
validationErrorModel: this.validationErrorModel,
rootPath
});
}
2023-12-20 08:20:38 +01:00
const formGroup = this.formBuilder.group({});
2024-02-08 16:23:03 +01:00
formGroup.addControl('comment', new FormControl({ value: this.comment, disabled: disabled }, context.getValidation('comment').validators));
2024-02-08 16:14:25 +01:00
formGroup.addControl('ordinal', new FormControl({ value: this.ordinal, disabled: disabled }, context.getValidation('ordinal').validators));
2024-02-02 09:29:38 +01:00
const fieldsFormGroup = this.formBuilder.group({});
this.fields.forEach((value, key) => fieldsFormGroup.addControl(key.toString(), value.buildForm({
rootPath: `${rootPath}fields[${key}].`
})), context.getValidation('fields')
2023-12-20 08:20:38 +01:00
)
2024-02-02 09:29:38 +01:00
formGroup.addControl('fields', fieldsFormGroup);
2023-12-20 08:20:38 +01:00
return formGroup;
2023-12-06 22:10:01 +01:00
}
static createValidationContext(params: {
rootPath?: string,
validationErrorModel: ValidationErrorModel
}): ValidationContext {
const { rootPath = '', validationErrorModel } = params;
const baseContext: ValidationContext = new ValidationContext();
const baseValidationArray: Validation[] = new Array<Validation>();
baseValidationArray.push({ key: 'fields', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}fields`)] });
2024-02-02 09:29:38 +01:00
baseValidationArray.push({ key: 'comment', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}comment`)] });
baseValidationArray.push({ key: 'ordinal', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}ordinal`)] });
2023-12-06 22:10:01 +01:00
baseContext.validation = baseValidationArray;
return baseContext;
}
2024-02-08 16:23:03 +01:00
static reapplyValidators(params: {
formGroup: UntypedFormGroup,
validationErrorModel: ValidationErrorModel,
rootPath: string
}): void {
const { formGroup, rootPath, validationErrorModel } = params;
const context = DescriptionPropertyDefinitionFieldSetItemEditorModel.createValidationContext({
rootPath,
validationErrorModel
});
const fields = formGroup?.get('fields') as UntypedFormGroup;
const keys = Object.keys(fields.value as Object);
keys.forEach((key) => {
const control = fields?.get(key);
DescriptionFieldEditorModel.reapplyValidators({
formGroup: control as UntypedFormGroup,
2024-02-09 15:16:46 +01:00
rootPath: `${rootPath}fields[${key}].`,
validationErrorModel: validationErrorModel
})
2024-02-21 16:21:17 +01:00
});
2024-02-08 16:23:03 +01:00
['comment', 'ordinal'].forEach(keyField => {
const control = formGroup?.get(keyField);
control?.clearValidators();
control?.addValidators(context.getValidation(keyField).validators);
});
2024-02-08 16:23:03 +01:00
}
2023-12-06 22:10:01 +01:00
}
export class DescriptionFieldEditorModel implements DescriptionFieldPersist {
2024-02-07 13:15:25 +01:00
textValue: string;
textListValue: string[];
dateValue: Date;
references: ReferencePersist[] = [];
2024-02-07 13:15:25 +01:00
reference: ReferencePersist;
externalIdentifier?: DescriptionExternalIdentifierEditorModel = new DescriptionExternalIdentifierEditorModel(this.validationErrorModel);
2023-12-06 22:10:01 +01:00
protected formBuilder: UntypedFormBuilder = new UntypedFormBuilder();
constructor(
public validationErrorModel: ValidationErrorModel = new ValidationErrorModel()
) { }
2024-02-07 13:15:25 +01:00
public fromModel(item: DescriptionField, descriptionTemplateField: DescriptionTemplateField, descriptionReferences: DescriptionReference[]): DescriptionFieldEditorModel {
2023-12-06 22:10:01 +01:00
if (item) {
2024-02-07 13:15:25 +01:00
this.textValue = item.textValue;
this.textListValue = item.textListValue;
this.dateValue = item.dateValue;
2024-04-08 17:59:42 +02:00
const references = descriptionReferences?.filter(x => x.data?.fieldId == descriptionTemplateField?.id && x.isActive == IsActive.Active).map(x => {
return {
id: x.reference.id,
label: x.reference.label,
reference: x.reference.reference,
source: x.reference.source,
typeId: x.reference.type.id,
description: x.reference.source,
definition: x.reference.definition,
abbreviation: x.reference.abbreviation,
sourceType: x.reference.sourceType
}
});
if (descriptionTemplateField.data.multipleSelect == true) {
this.references = references;
} else {
if (references?.length == 1) this.reference = references[0];
if (references?.length > 1) {
console.error("multiple references on single reference field: " + references);
this.reference = references[0];
}
}
2024-02-08 16:23:03 +01:00
this.externalIdentifier = new DescriptionExternalIdentifierEditorModel(this.validationErrorModel).fromModel(item.externalIdentifier);
2023-12-06 22:10:01 +01:00
}
return this;
}
buildForm(params?: {
context?: ValidationContext,
disabled?: boolean,
rootPath?: string
}): UntypedFormGroup {
let { context = null, disabled = false, rootPath } = params ?? {}
if (context == null) {
context = DescriptionFieldEditorModel.createValidationContext({
validationErrorModel: this.validationErrorModel,
rootPath
});
}
return this.formBuilder.group({
2024-02-07 13:15:25 +01:00
textValue: [{ value: this.textValue, disabled: disabled }, context.getValidation('textValue').validators],
textListValue: [{ value: this.textListValue, disabled: disabled }, context.getValidation('textListValue').validators],
dateValue: [{ value: this.dateValue, disabled: disabled }, context.getValidation('dateValue').validators],
references: [{ value: this.references, disabled: disabled }, context.getValidation('references').validators],
reference: [{ value: this.reference, disabled: disabled }, context.getValidation('reference').validators],
externalIdentifier: this.externalIdentifier.buildForm({
rootPath: `${rootPath}externalIdentifier.`
}),
2023-12-06 22:10:01 +01:00
});
}
static createValidationContext(params: {
rootPath?: string,
validationErrorModel: ValidationErrorModel
}): ValidationContext {
const { rootPath = '', validationErrorModel } = params;
const baseContext: ValidationContext = new ValidationContext();
const baseValidationArray: Validation[] = new Array<Validation>();
baseValidationArray.push({ key: 'textValue', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}textValue`)] });
baseValidationArray.push({ key: 'textListValue', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}textListValue`)] });
baseValidationArray.push({ key: 'dateValue', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}dateValue`)] });
baseValidationArray.push({ key: 'references', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}references`)] });
baseValidationArray.push({ key: 'reference', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}references`)] });
baseValidationArray.push({ key: 'externalIdentifier', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}externalIdentifier`)] });
2023-12-06 22:10:01 +01:00
baseContext.validation = baseValidationArray;
return baseContext;
}
2024-02-08 16:23:03 +01:00
static reapplyValidators(params: {
formGroup: UntypedFormGroup,
validationErrorModel: ValidationErrorModel,
rootPath: string
}): void {
const { formGroup, rootPath, validationErrorModel } = params;
const context = DescriptionFieldEditorModel.createValidationContext({
2024-02-08 16:23:03 +01:00
rootPath,
validationErrorModel
});
['textValue', 'textListValue', 'dateValue'].forEach(keyField => {
const control = formGroup?.get(keyField);
control?.clearValidators();
control?.addValidators(context.getValidation(keyField).validators);
})
DescriptionExternalIdentifierEditorModel.reapplyValidators({
formGroup: formGroup?.get('externalIdentifier') as UntypedFormGroup,
rootPath: `${rootPath}externalIdentifier.`,
validationErrorModel: validationErrorModel
});
}
2024-02-07 13:15:25 +01:00
}
export class DescriptionExternalIdentifierEditorModel implements DescriptionExternalIdentifierPersist {
identifier: string;
type: string;
protected formBuilder: UntypedFormBuilder = new UntypedFormBuilder();
constructor(
public validationErrorModel: ValidationErrorModel = new ValidationErrorModel()
) { }
public fromModel(item: DescriptionExternalIdentifier): DescriptionExternalIdentifierEditorModel {
if (item) {
this.identifier = item.identifier;
this.type = item.type;
}
return this;
}
buildForm(params?: {
context?: ValidationContext,
disabled?: boolean,
rootPath?: string
}): UntypedFormGroup {
let { context = null, disabled = false, rootPath } = params ?? {}
if (context == null) {
context = DescriptionExternalIdentifierEditorModel.createValidationContext({
validationErrorModel: this.validationErrorModel,
rootPath
});
}
2023-12-06 22:10:01 +01:00
2024-02-07 13:15:25 +01:00
return this.formBuilder.group({
identifier: [{ value: this.identifier, disabled: disabled }, context.getValidation('identifier').validators],
type: [{ value: this.type, disabled: disabled }, context.getValidation('type').validators],
});
}
static createValidationContext(params: {
rootPath?: string,
validationErrorModel: ValidationErrorModel
}): ValidationContext {
const { rootPath = '', validationErrorModel } = params;
const baseContext: ValidationContext = new ValidationContext();
const baseValidationArray: Validation[] = new Array<Validation>();
baseValidationArray.push({ key: 'identifier', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}identifier`)] });
baseValidationArray.push({ key: 'type', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}type`)] });
2024-02-07 13:15:25 +01:00
baseContext.validation = baseValidationArray;
return baseContext;
}
2024-02-08 16:23:03 +01:00
static reapplyValidators(params: {
formGroup: UntypedFormGroup,
validationErrorModel: ValidationErrorModel,
rootPath: string
}): void {
const { formGroup, rootPath, validationErrorModel } = params;
const context = DescriptionExternalIdentifierEditorModel.createValidationContext({
rootPath,
validationErrorModel
});
['identifier', 'type'].forEach(keyField => {
const control = formGroup?.get(keyField);
control?.clearValidators();
control?.addValidators(context.getValidation(keyField).validators);
});
}
2023-12-06 22:10:01 +01:00
}
export class DescriptionReferenceEditorModel implements DescriptionReferencePersist {
id: Guid;
reference?: ReferencePersist;
hash: string;
protected formBuilder: UntypedFormBuilder = new UntypedFormBuilder();
constructor(
public validationErrorModel: ValidationErrorModel = new ValidationErrorModel()
) { }
fromModel(item: DescriptionReference): DescriptionReferenceEditorModel {
this.id = item.id;
//TODO: refactor reference type
//this.reference = item.reference;
2023-12-06 22:10:01 +01:00
this.hash = item.hash;
return this;
}
buildForm(params?: {
context?: ValidationContext,
disabled?: boolean,
rootPath?: string
}): UntypedFormGroup {
let { context = null, disabled = false, rootPath } = params ?? {}
if (context == null) {
context = DescriptionReferenceEditorModel.createValidationContext({
validationErrorModel: this.validationErrorModel,
rootPath
});
}
return this.formBuilder.group({
id: [{ value: this.id, disabled: disabled }, context.getValidation('id').validators],
reference: [{ value: this.reference, disabled: disabled }, context.getValidation('reference').validators],
hash: [{ value: this.hash, disabled: disabled }, context.getValidation('hash').validators]
});
}
static createValidationContext(params: {
rootPath?: string,
validationErrorModel: ValidationErrorModel
}): ValidationContext {
const { rootPath = '', validationErrorModel } = params;
const baseContext: ValidationContext = new ValidationContext();
const baseValidationArray: Validation[] = new Array<Validation>();
baseValidationArray.push({ key: 'id', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}id`)] });
baseValidationArray.push({ key: 'reference', validators: [BackendErrorValidator(validationErrorModel, `${rootPath}reference`)] });
baseValidationArray.push({ key: 'hash', validators: [] });
baseContext.validation = baseValidationArray;
return baseContext;
}
2024-02-08 16:23:03 +01:00
static reapplyValidators(params: {
formGroup: UntypedFormGroup,
validationErrorModel: ValidationErrorModel,
rootPath: string
}): void {
const { formGroup, rootPath, validationErrorModel } = params;
const context = DescriptionExternalIdentifierEditorModel.createValidationContext({
rootPath,
validationErrorModel
});
['id', 'reference'].forEach(keyField => {
const control = formGroup?.get(keyField);
control?.clearValidators();
control?.addValidators(context.getValidation(keyField).validators);
});
}
2024-04-19 17:37:06 +02:00
}
export class DescriptionFieldIndicator {
pageId: string;
sectionId: string;
fieldSetId: string;
fieldId: string;
type: string;
constructor(pageId: string, sectionId: string, fieldSetId: string, fieldId: string, type: DescriptionTemplateFieldType, multipleSelect: boolean = false) {
this.pageId = pageId;
this.sectionId = sectionId;
this.fieldSetId = fieldSetId;
this.fieldId = fieldId;
switch (type) {
case DescriptionTemplateFieldType.FREE_TEXT:
case DescriptionTemplateFieldType.BOOLEAN_DECISION:
case DescriptionTemplateFieldType.CHECK_BOX:
case DescriptionTemplateFieldType.RADIO_BOX:
case DescriptionTemplateFieldType.TEXT_AREA:
case DescriptionTemplateFieldType.UPLOAD:
case DescriptionTemplateFieldType.RICH_TEXT_AREA:
this.type = "textValue";
break;
case DescriptionTemplateFieldType.DATASET_IDENTIFIER:
case DescriptionTemplateFieldType.VALIDATION:
this.type = "externalIdentifier";
break;
case DescriptionTemplateFieldType.DATE_PICKER:
this.type = "dateValue";
break;
case DescriptionTemplateFieldType.EXTERNAL_DATASETS:
this.type = "";
break;
case DescriptionTemplateFieldType.INTERNAL_ENTRIES_DESCRIPTIONS:
if (multipleSelect) this.type = "textListValue";
else this.type = "textValue"
break;
case DescriptionTemplateFieldType.INTERNAL_ENTRIES_DMPS:
if (multipleSelect) this.type = "textListValue";
else this.type = "textValue";
break;
case DescriptionTemplateFieldType.REFERENCE_TYPES:
if (multipleSelect) this.type = "references";
else this.type = "reference";
break;
case DescriptionTemplateFieldType.SELECT:
if (multipleSelect) this.type = "textListValue";
else this.type = "textValue";
break;
case DescriptionTemplateFieldType.TAGS:
this.type = "textListValue";
break;
}
}
}