diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-form-control.component.html b/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-form-control.component.html index a044a78e8e..7b8501e7af 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-form-control.component.html +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-form-control.component.html @@ -317,6 +317,18 @@ > + + + + diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-form-control.component.spec.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-form-control.component.spec.ts index 3e24916899..7fc756c470 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-form-control.component.spec.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-form-control.component.spec.ts @@ -38,6 +38,7 @@ import { DynamicScrollableDropdownModel } from './models/scrollable-dropdown/dyn import { DynamicTagModel } from './models/tag/dynamic-tag.model'; import { DynamicTypeaheadModel } from './models/typeahead/dynamic-typeahead.model'; import { DynamicQualdropModel } from './models/ds-dynamic-qualdrop.model'; +import { DynamicLookupNameModel } from './models/lookup/dynamic-lookup-name.model'; describe('DsDynamicFormControlComponent test suite', () => { @@ -79,7 +80,8 @@ describe('DsDynamicFormControlComponent test suite', () => { submissionScope: '' }), new DynamicDsDatePickerModel({id: 'datepicker'}), - new DynamicLookupModel({id: 'lookup', separator: ','}), + new DynamicLookupModel({id: 'lookup'}), + new DynamicLookupNameModel({id: 'lookupName'}), new DynamicQualdropModel({id: 'combobox', readOnly: false}) ]; const testModel = formModel[8]; @@ -272,6 +274,8 @@ describe('DsDynamicFormControlComponent test suite', () => { expect(testFn(formModel[23])).toEqual(NGBootstrapFormControlType.Lookup); - expect(testFn(formModel[24])).toEqual(NGBootstrapFormControlType.Group); + expect(testFn(formModel[24])).toEqual(NGBootstrapFormControlType.LookupName); + + expect(testFn(formModel[25])).toEqual(NGBootstrapFormControlType.Group); }); }); diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-form-control.component.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-form-control.component.ts index 5bd8402702..660ae9c244 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-form-control.component.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-form-control.component.ts @@ -39,6 +39,7 @@ import { DYNAMIC_FORM_CONTROL_TYPE_LOOKUP } from './models/lookup/dynamic-lookup import { DynamicListCheckboxGroupModel } from './models/list/dynamic-list-checkbox-group.model'; import { DynamicListRadioGroupModel } from './models/list/dynamic-list-radio-group.model'; import { isNotEmpty } from '../../../empty.util'; +import { DYNAMIC_FORM_CONTROL_TYPE_LOOKUP_NAME } from './models/lookup/dynamic-lookup-name.model'; export const enum NGBootstrapFormControlType { @@ -60,6 +61,7 @@ export const enum NGBootstrapFormControlType { Relation = 16, // 'RELATION' Date = 17, // 'DATE' Lookup = 18, // LOOKUP + LookupName = 19, // LOOKUP_NAME } @Component({ @@ -144,6 +146,9 @@ export class DsDynamicFormControlComponent extends DynamicFormControlComponent i case DYNAMIC_FORM_CONTROL_TYPE_LOOKUP: return NGBootstrapFormControlType.Lookup; + case DYNAMIC_FORM_CONTROL_TYPE_LOOKUP_NAME: + return NGBootstrapFormControlType.LookupName; + default: return null; } diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/models/lookup/dynamic-lookup-name.model.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/models/lookup/dynamic-lookup-name.model.ts new file mode 100644 index 0000000000..64590ec9e0 --- /dev/null +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/models/lookup/dynamic-lookup-name.model.ts @@ -0,0 +1,26 @@ +import { DynamicFormControlLayout, serializable } from '@ng-dynamic-forms/core'; +import { DynamicLookupModel, DynamicLookupModelConfig } from './dynamic-lookup.model'; + +export const DYNAMIC_FORM_CONTROL_TYPE_LOOKUP_NAME = 'LOOKUP_NAME'; + +export interface DynamicLookupNameModelConfig extends DynamicLookupModelConfig { + separator?: string; + firstPlaceholder?: string; + secondPlaceholder?: string; +} + +export class DynamicLookupNameModel extends DynamicLookupModel { + + @serializable() separator: string; + @serializable() secondPlaceholder: string; + @serializable() readonly type: string = DYNAMIC_FORM_CONTROL_TYPE_LOOKUP_NAME; + + constructor(config: DynamicLookupNameModelConfig, layout?: DynamicFormControlLayout) { + + super(config, layout); + + this.separator = config.separator || ','; + this.placeholder = config.firstPlaceholder || 'form.last-name'; + this.secondPlaceholder = config.secondPlaceholder || 'form.first-name'; + } +} diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/models/lookup/dynamic-lookup.component.html b/src/app/shared/form/builder/ds-dynamic-form-ui/models/lookup/dynamic-lookup.component.html index 14f468b0f8..710639ff88 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/models/lookup/dynamic-lookup.component.html +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/models/lookup/dynamic-lookup.component.html @@ -3,7 +3,7 @@ (openChange)="openChange($event);"> -
+
@@ -42,7 +42,7 @@
-
+
@@ -63,7 +63,7 @@ (input)="onInput($event)">
-
+
= new EventEmitter(); @@ -31,7 +32,6 @@ export class DsDynamicLookupComponent implements OnDestroy, OnInit { public loading = false; public pageInfo: PageInfo; public optionsList: any; - public isLookupName: boolean; public name2: string; protected searchOptions: IntegrationSearchOptions; @@ -51,8 +51,7 @@ export class DsDynamicLookupComponent implements OnDestroy, OnInit { 1); // Switch Lookup/LookupName - if (this.model.separator) { - this.isLookupName = true; + if (this.isLookupName()) { this.name2 = this.model.name + '2'; } @@ -107,8 +106,8 @@ export class DsDynamicLookupComponent implements OnDestroy, OnInit { } if (hasValue(displayValue)) { - if (this.isLookupName) { - const values = displayValue.split(this.model.separator); + if (this.isLookupName()) { + const values = displayValue.split((this.model as DynamicLookupNameModel).separator); this.firstInputValue = (values[0] || '').trim(); this.secondInputValue = (values[1] || '').trim(); @@ -121,7 +120,7 @@ export class DsDynamicLookupComponent implements OnDestroy, OnInit { protected getCurrentValue(): string { let result = ''; - if (!this.isLookupName) { + if (!this.isLookupName()) { result = this.firstInputValue; } else { if (isNotEmpty(this.firstInputValue)) { @@ -130,7 +129,7 @@ export class DsDynamicLookupComponent implements OnDestroy, OnInit { if (isNotEmpty(this.secondInputValue)) { result = isEmpty(result) ? this.secondInputValue - : this.firstInputValue + this.model.separator + ' ' + this.secondInputValue; + : this.firstInputValue + (this.model as DynamicLookupNameModel).separator + ' ' + this.secondInputValue; } } return result; @@ -163,7 +162,7 @@ export class DsDynamicLookupComponent implements OnDestroy, OnInit { protected resetFields() { this.firstInputValue = ''; - if (this.isLookupName) { + if (this.isLookupName()) { this.secondInputValue = ''; } } @@ -181,6 +180,10 @@ export class DsDynamicLookupComponent implements OnDestroy, OnInit { return this.model.authorityOptions.closed && hasValue(this.model.value); } + isLookupName() { + return (this.model instanceof DynamicLookupNameModel); + } + isSearchDisabled() { // if (this.firstInputValue === '' // && (this.isLookupName ? this.secondInputValue === '' : true)) { diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/models/lookup/dynamic-lookup.model.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/models/lookup/dynamic-lookup.model.ts index 1d9f7ed9e9..e14dedb2ac 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/models/lookup/dynamic-lookup.model.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/models/lookup/dynamic-lookup.model.ts @@ -1,14 +1,11 @@ import { AUTOCOMPLETE_OFF, DynamicFormControlLayout, serializable } from '@ng-dynamic-forms/core'; -import { AuthorityValueModel } from '../../../../../../core/integration/models/authority-value.model'; import { DsDynamicInputModel, DsDynamicInputModelConfig } from '../ds-dynamic-input.model'; -import { AuthorityOptions } from '../../../../../../core/integration/models/authority-options.model'; export const DYNAMIC_FORM_CONTROL_TYPE_LOOKUP = 'LOOKUP'; export interface DynamicLookupModelConfig extends DsDynamicInputModelConfig { maxOptions?: number; value?: any; - separator: string; } export class DynamicLookupModel extends DsDynamicInputModel { @@ -16,10 +13,6 @@ export class DynamicLookupModel extends DsDynamicInputModel { @serializable() maxOptions: number; @serializable() readonly type: string = DYNAMIC_FORM_CONTROL_TYPE_LOOKUP; @serializable() value: any; - @serializable() separator: string; // Defined only for lookup-name - - @serializable() placeholder: string; - @serializable() placeholder2: string; constructor(config: DynamicLookupModelConfig, layout?: DynamicFormControlLayout) { @@ -27,11 +20,7 @@ export class DynamicLookupModel extends DsDynamicInputModel { this.autoComplete = AUTOCOMPLETE_OFF; this.maxOptions = config.maxOptions || 10; - this.separator = config.separator; // Defined only for lookup-name this.valueUpdates.next(config.value); - // this.valueUpdates.subscribe(() => { - // this.setInputsValue(); - // }); } } diff --git a/src/app/shared/form/builder/form-builder.service.spec.ts b/src/app/shared/form/builder/form-builder.service.spec.ts index c31d5c39aa..26d2ac73e3 100644 --- a/src/app/shared/form/builder/form-builder.service.spec.ts +++ b/src/app/shared/form/builder/form-builder.service.spec.ts @@ -47,6 +47,7 @@ import { DynamicRowGroupModel } from './ds-dynamic-form-ui/models/ds-dynamic-row import { DsDynamicInputModel } from './ds-dynamic-form-ui/models/ds-dynamic-input.model'; import { FormFieldMetadataValueObject } from './models/form-field-metadata-value.model'; import { DynamicConcatModel } from './ds-dynamic-form-ui/models/ds-dynamic-concat.model'; +import { DynamicLookupNameModel } from './ds-dynamic-form-ui/models/lookup/dynamic-lookup-name.model'; describe('FormBuilderService test suite', () => { @@ -243,7 +244,9 @@ describe('FormBuilderService test suite', () => { new DynamicDsDatePickerModel({id: 'testDate'}), - new DynamicLookupModel({id: 'testLookup', separator: ','}), + new DynamicLookupModel({id: 'testLookup'}), + + new DynamicLookupNameModel({id: 'testLookupName'}), new DynamicQualdropModel({id: 'testCombobox', readOnly: false}) ]; diff --git a/src/app/shared/form/builder/parsers/date-field-parser.spec.ts b/src/app/shared/form/builder/parsers/date-field-parser.spec.ts index aef4797676..430e6df3cb 100644 --- a/src/app/shared/form/builder/parsers/date-field-parser.spec.ts +++ b/src/app/shared/form/builder/parsers/date-field-parser.spec.ts @@ -36,7 +36,7 @@ describe('DateFieldParser test suite', () => { expect(parser instanceof DateFieldParser).toBe(true); }); - it('should return a DynamicDsDatePickerModel object', () => { + it('should return a DynamicDsDatePickerModel object when repeatable option is false', () => { const parser = new DateFieldParser(field, initFormValues, readOnly); const fieldModel = parser.parse(); diff --git a/src/app/shared/form/builder/parsers/dropdown-field-parser.spec.ts b/src/app/shared/form/builder/parsers/dropdown-field-parser.spec.ts index 700c824051..52d886d405 100644 --- a/src/app/shared/form/builder/parsers/dropdown-field-parser.spec.ts +++ b/src/app/shared/form/builder/parsers/dropdown-field-parser.spec.ts @@ -36,7 +36,7 @@ describe('DropdownFieldParser test suite', () => { expect(parser instanceof DropdownFieldParser).toBe(true); }); - it('should return a DynamicScrollableDropdownModel object', () => { + it('should return a DynamicScrollableDropdownModel object when repeatable option is false', () => { const parser = new DropdownFieldParser(field, initFormValues, readOnly, authorityUuid); const fieldModel = parser.parse(); diff --git a/src/app/shared/form/builder/parsers/group-field-parser.spec.ts b/src/app/shared/form/builder/parsers/group-field-parser.spec.ts new file mode 100644 index 0000000000..43cf72b61c --- /dev/null +++ b/src/app/shared/form/builder/parsers/group-field-parser.spec.ts @@ -0,0 +1,108 @@ +import { FormFieldModel } from '../models/form-field.model'; +import { GroupFieldParser } from './group-field-parser'; +import { DynamicGroupModel } from '../ds-dynamic-form-ui/models/dynamic-group/dynamic-group.model'; +import { FormFieldMetadataValueObject } from '../models/form-field-metadata-value.model'; + +describe('GroupFieldParser test suite', () => { + let field: FormFieldModel; + let initFormValues = {}; + + const authorityUuid = 'testScopeUUID'; + const readOnly = false; + const submissionScope = 'WORKSPACE'; + + beforeEach(() => { + field = { + input: { + type: 'group' + }, + rows: [ + { + fields: [ + { + input: { + type: 'onebox' + }, + label: 'Author', + mandatory: 'false', + repeatable: false, + hints: 'Enter the name of the author.', + selectableMetadata: [ + { + metadata: 'author' + } + ], + languageCodes: [] + }, + { + input: { + type: 'onebox' + }, + label: 'Affiliation', + mandatory: false, + repeatable: true, + hints: 'Enter the affiliation of the author.', + selectableMetadata: [ + { + metadata: 'affiliation' + } + ], + languageCodes: [] + } + ] + } + ], + label: 'Authors', + mandatory: 'true', + repeatable: false, + mandatoryMessage: 'Entering at least the first author is mandatory.', + hints: 'Enter the names of the authors of this item.', + selectableMetadata: [ + { + metadata: 'author' + } + ], + languageCodes: [] + } as FormFieldModel; + + }); + + it('should init parser properly', () => { + const parser = new GroupFieldParser(field, initFormValues, readOnly, submissionScope, authorityUuid); + + expect(parser instanceof GroupFieldParser).toBe(true); + }); + + it('should return a DynamicGroupModel object', () => { + const parser = new GroupFieldParser(field, initFormValues, readOnly, submissionScope, authorityUuid); + + const fieldModel = parser.parse(); + + expect(fieldModel instanceof DynamicGroupModel).toBe(true); + }); + + it('should throw when rows configuration is empty', () => { + field.rows = null; + const parser = new GroupFieldParser(field, initFormValues, readOnly, submissionScope, authorityUuid); + + expect(() => parser.parse()) + .toThrow(); + }); + + it('should set group init value properly', () => { + initFormValues = { + author: [new FormFieldMetadataValueObject('test author')], + affiliation: [new FormFieldMetadataValueObject('test affiliation')] + }; + const parser = new GroupFieldParser(field, initFormValues, readOnly, submissionScope, authorityUuid); + + const fieldModel = parser.parse(); + const expectedValue = [{ + author: new FormFieldMetadataValueObject('test author'), + affiliation: new FormFieldMetadataValueObject('test affiliation') + }]; + + expect(fieldModel.value).toEqual(expectedValue); + }); + +}); diff --git a/src/app/shared/form/builder/parsers/list-field-parser.spec.ts b/src/app/shared/form/builder/parsers/list-field-parser.spec.ts new file mode 100644 index 0000000000..65184e5433 --- /dev/null +++ b/src/app/shared/form/builder/parsers/list-field-parser.spec.ts @@ -0,0 +1,71 @@ +import { FormFieldModel } from '../models/form-field.model'; +import { FormFieldMetadataValueObject } from '../models/form-field-metadata-value.model'; +import { ListFieldParser } from './list-field-parser'; +import { DynamicListCheckboxGroupModel } from '../ds-dynamic-form-ui/models/list/dynamic-list-checkbox-group.model'; +import { DynamicListRadioGroupModel } from '../ds-dynamic-form-ui/models/list/dynamic-list-radio-group.model'; + +describe('ListFieldParser test suite', () => { + let field: FormFieldModel; + let initFormValues = {}; + + const authorityUuid = 'testScopeUUID'; + const readOnly = false; + + beforeEach(() => { + field = { + input: { + type: 'list' + }, + label: 'Type', + mandatory: 'false', + repeatable: true, + hints: 'Select the type.', + selectableMetadata: [ + { + metadata: 'type', + authority: 'type_programme', + closed: false + } + ], + languageCodes: [] + } as FormFieldModel; + + }); + + it('should init parser properly', () => { + const parser = new ListFieldParser(field, initFormValues, readOnly, authorityUuid); + + expect(parser instanceof ListFieldParser).toBe(true); + }); + + it('should return a DynamicListCheckboxGroupModel object when repeatable option is true', () => { + const parser = new ListFieldParser(field, initFormValues, readOnly, authorityUuid); + + const fieldModel = parser.parse(); + + expect(fieldModel instanceof DynamicListCheckboxGroupModel).toBe(true); + }); + + it('should return a DynamicListRadioGroupModel object when repeatable option is false', () => { + field.repeatable = false; + const parser = new ListFieldParser(field, initFormValues, readOnly, authorityUuid); + + const fieldModel = parser.parse(); + + expect(fieldModel instanceof DynamicListRadioGroupModel).toBe(true); + }); + + it('should set init value properly', () => { + initFormValues = { + type: [new FormFieldMetadataValueObject('test type')], + }; + const expectedValue = [new FormFieldMetadataValueObject('test type')]; + + const parser = new ListFieldParser(field, initFormValues, readOnly, authorityUuid); + + const fieldModel = parser.parse(); + + expect(fieldModel.value).toEqual(expectedValue); + }); + +}); diff --git a/src/app/shared/form/builder/parsers/lookup-field-parser.spec.ts b/src/app/shared/form/builder/parsers/lookup-field-parser.spec.ts new file mode 100644 index 0000000000..209ff318ea --- /dev/null +++ b/src/app/shared/form/builder/parsers/lookup-field-parser.spec.ts @@ -0,0 +1,61 @@ +import { FormFieldModel } from '../models/form-field.model'; +import { FormFieldMetadataValueObject } from '../models/form-field-metadata-value.model'; +import { LookupFieldParser } from './lookup-field-parser'; +import { DynamicLookupModel } from '../ds-dynamic-form-ui/models/lookup/dynamic-lookup.model'; + +describe('LookupFieldParser test suite', () => { + let field: FormFieldModel; + let initFormValues = {}; + + const authorityUuid = 'testScopeUUID'; + const readOnly = false; + + beforeEach(() => { + field = { + input: { + type: 'lookup' + }, + label: 'Journal', + mandatory: 'false', + repeatable: false, + hints: 'Enter the name of the journal where the item has been published, if any.', + selectableMetadata: [ + { + metadata: 'journal', + authority: 'JOURNALAuthority', + closed: false + } + ], + languageCodes: [] + } as FormFieldModel; + + }); + + it('should init parser properly', () => { + const parser = new LookupFieldParser(field, initFormValues, readOnly, authorityUuid); + + expect(parser instanceof LookupFieldParser).toBe(true); + }); + + it('should return a DynamicLookupModel object when repeatable option is false', () => { + const parser = new LookupFieldParser(field, initFormValues, readOnly, authorityUuid); + + const fieldModel = parser.parse(); + + expect(fieldModel instanceof DynamicLookupModel).toBe(true); + }); + + it('should set init value properly', () => { + initFormValues = { + journal: [new FormFieldMetadataValueObject('test journal')], + }; + const expectedValue = new FormFieldMetadataValueObject('test journal'); + + const parser = new LookupFieldParser(field, initFormValues, readOnly, authorityUuid); + + const fieldModel = parser.parse(); + + expect(fieldModel.value).toEqual(expectedValue); + }); + +}); diff --git a/src/app/shared/form/builder/parsers/lookup-field-parser.ts b/src/app/shared/form/builder/parsers/lookup-field-parser.ts index 9a9637997d..1962c3928e 100644 --- a/src/app/shared/form/builder/parsers/lookup-field-parser.ts +++ b/src/app/shared/form/builder/parsers/lookup-field-parser.ts @@ -19,8 +19,8 @@ export class LookupFieldParser extends FieldParser { this.setValues(lookupModelConfig, fieldValue, true); - const lookupModel = new DynamicLookupModel(lookupModelConfig); - return lookupModel; + return new DynamicLookupModel(lookupModelConfig); + } } } diff --git a/src/app/shared/form/builder/parsers/lookup-name-field-parser.spec.ts b/src/app/shared/form/builder/parsers/lookup-name-field-parser.spec.ts new file mode 100644 index 0000000000..e5d1df7dc1 --- /dev/null +++ b/src/app/shared/form/builder/parsers/lookup-name-field-parser.spec.ts @@ -0,0 +1,63 @@ +import { FormFieldModel } from '../models/form-field.model'; +import { FormFieldMetadataValueObject } from '../models/form-field-metadata-value.model'; +import { LookupFieldParser } from './lookup-field-parser'; +import { DynamicLookupModel } from '../ds-dynamic-form-ui/models/lookup/dynamic-lookup.model'; +import { LookupNameFieldParser } from './lookup-name-field-parser'; +import { DynamicLookupNameModel } from '../ds-dynamic-form-ui/models/lookup/dynamic-lookup-name.model'; + +describe('LookupNameFieldParser test suite', () => { + let field: FormFieldModel; + let initFormValues = {}; + + const authorityUuid = 'testScopeUUID'; + const readOnly = false; + + beforeEach(() => { + field = { + input: { + type: 'lookup-name' + }, + label: 'Author', + mandatory: 'false', + repeatable: false, + hints: 'Enter the name of the author.', + selectableMetadata: [ + { + metadata: 'author', + authority: 'RPAuthority', + closed: false + } + ], + languageCodes: [] + } as FormFieldModel; + + }); + + it('should init parser properly', () => { + const parser = new LookupNameFieldParser(field, initFormValues, readOnly, authorityUuid); + + expect(parser instanceof LookupNameFieldParser).toBe(true); + }); + + it('should return a DynamicLookupNameModel object when repeatable option is false', () => { + const parser = new LookupNameFieldParser(field, initFormValues, readOnly, authorityUuid); + + const fieldModel = parser.parse(); + + expect(fieldModel instanceof DynamicLookupNameModel).toBe(true); + }); + + it('should set init value properly', () => { + initFormValues = { + author: [new FormFieldMetadataValueObject('test author')], + }; + const expectedValue = new FormFieldMetadataValueObject('test author'); + + const parser = new LookupNameFieldParser(field, initFormValues, readOnly, authorityUuid); + + const fieldModel = parser.parse(); + + expect(fieldModel.value).toEqual(expectedValue); + }); + +}); diff --git a/src/app/shared/form/builder/parsers/lookup-name-field-parser.ts b/src/app/shared/form/builder/parsers/lookup-name-field-parser.ts index 8ae8d78b2b..d687ddf0c4 100644 --- a/src/app/shared/form/builder/parsers/lookup-name-field-parser.ts +++ b/src/app/shared/form/builder/parsers/lookup-name-field-parser.ts @@ -1,22 +1,29 @@ import { FormFieldModel } from '../models/form-field.model'; -import { FormFieldMetadataValueObject } from '../models/form-field-metadata-value.model'; -import { LookupFieldParser } from './lookup-field-parser'; +import { FieldParser } from './field-parser'; +import { + DynamicLookupNameModel, + DynamicLookupNameModelConfig +} from '../ds-dynamic-form-ui/models/lookup/dynamic-lookup-name.model'; -export class LookupNameFieldParser extends LookupFieldParser { +export class LookupNameFieldParser extends FieldParser { constructor(protected configData: FormFieldModel, protected initFormValues, protected readOnly: boolean, protected authorityUuid: string) { - super(configData, initFormValues, readOnly, authorityUuid); + super(configData, initFormValues, readOnly); } - public modelFactory(fieldValue: FormFieldMetadataValueObject | any): any { - const lookupModel = super.modelFactory(fieldValue); - lookupModel.separator = ','; - lookupModel.placeholder = 'form.last-name'; - lookupModel.placeholder2 = 'form.first-name'; - return lookupModel; + public modelFactory(fieldValue: any): any { + if (this.configData.selectableMetadata[0].authority) { + const lookupModelConfig: DynamicLookupNameModelConfig = this.initModel(); + this.setAuthorityOptions(lookupModelConfig, this.authorityUuid); + + this.setValues(lookupModelConfig, fieldValue, true); + + return new DynamicLookupNameModel(lookupModelConfig); + } } + } diff --git a/src/app/shared/form/builder/parsers/name-field-parser.spec.ts b/src/app/shared/form/builder/parsers/name-field-parser.spec.ts index e512dac2f2..e565ea881d 100644 --- a/src/app/shared/form/builder/parsers/name-field-parser.spec.ts +++ b/src/app/shared/form/builder/parsers/name-field-parser.spec.ts @@ -66,7 +66,7 @@ describe('NameFieldParser test suite', () => { expect(parser instanceof NameFieldParser).toBe(true); }); - it('should return a DynamicConcatModel object', () => { + it('should return a DynamicConcatModel object when repeatable option is false', () => { const parser = new NameFieldParser(field2, initFormValues, readOnly); const fieldModel = parser.parse(); diff --git a/src/app/shared/form/builder/parsers/series-field-parser.spec.ts b/src/app/shared/form/builder/parsers/series-field-parser.spec.ts index 384ae880dc..21889a1b53 100644 --- a/src/app/shared/form/builder/parsers/series-field-parser.spec.ts +++ b/src/app/shared/form/builder/parsers/series-field-parser.spec.ts @@ -31,7 +31,7 @@ describe('SeriesFieldParser test suite', () => { expect(parser instanceof SeriesFieldParser).toBe(true); }); - it('should return a DynamicConcatModel object', () => { + it('should return a DynamicConcatModel object when repeatable option is false', () => { const parser = new SeriesFieldParser(field, initFormValues, readOnly); const fieldModel = parser.parse(); diff --git a/src/app/shared/form/form.service.spec.ts b/src/app/shared/form/form.service.spec.ts index 7155743764..a3f8e09e84 100644 --- a/src/app/shared/form/form.service.spec.ts +++ b/src/app/shared/form/form.service.spec.ts @@ -112,11 +112,23 @@ describe('FormService test suite', () => { }); it('should add error to field', () => { - const control = builderService.getFormControlById('description', formGroup, formModel); - const model = builderService.findById('description', formModel); + let control = builderService.getFormControlById('description', formGroup, formModel); + let model = builderService.findById('description', formModel); + let errorKeys: string[]; service.addErrorToField(control, model, 'Test error message'); - const errorKeys = Object.keys(control.errors); + errorKeys = Object.keys(control.errors); + + expect(errorKeys.length).toBe(1); + + expect(control.hasError(errorKeys[0])).toBe(true); + + expect(formGroup.controls.description.touched).toBe(true); + + control = builderService.getFormControlById('title', formGroup, formModel); + model = builderService.findById('title', formModel); + service.addErrorToField(control, model, 'error.required'); + errorKeys = Object.keys(control.errors); expect(errorKeys.length).toBe(1); @@ -126,11 +138,26 @@ describe('FormService test suite', () => { }); it('should remove error from field', () => { - const control = builderService.getFormControlById('description', formGroup, formModel); - const model = builderService.findById('description', formModel); + let control = builderService.getFormControlById('description', formGroup, formModel); + let model = builderService.findById('description', formModel); + let errorKeys: string[]; service.addErrorToField(control, model, 'Test error message'); - const errorKeys = Object.keys(control.errors); + errorKeys = Object.keys(control.errors); + + service.removeErrorFromField(control, model, errorKeys[0]); + + expect(errorKeys.length).toBe(1); + + expect(control.hasError(errorKeys[0])).toBe(false); + + expect(formGroup.controls.description.touched).toBe(false); + + control = builderService.getFormControlById('title', formGroup, formModel); + model = builderService.findById('title', formModel); + + service.addErrorToField(control, model, 'error.required'); + errorKeys = Object.keys(control.errors); service.removeErrorFromField(control, model, errorKeys[0]);