diff --git a/frontend/src/app/ui/admin/reference-type/editor/reference-type-editor.component.ts b/frontend/src/app/ui/admin/reference-type/editor/reference-type-editor.component.ts index 457e2e565..8ebaa77dc 100644 --- a/frontend/src/app/ui/admin/reference-type/editor/reference-type-editor.component.ts +++ b/frontend/src/app/ui/admin/reference-type/editor/reference-type-editor.component.ts @@ -30,6 +30,7 @@ import { ReferenceTypeEditorModel } from './reference-type-editor.model'; import { ReferenceTypeEditorResolver } from './reference-type-editor.resolver'; import { ReferenceTypeEditorService } from './reference-type-editor.service'; import { RouterUtilsService } from '@app/core/services/router/router-utils.service'; +import { StaticEditorModel, StaticOptionEditorModel } from '@app/ui/external-fetcher/external-fetcher-source-editor.model'; @Component({ selector: 'app-reference-type-editor-component', @@ -220,10 +221,22 @@ export class ReferenceTypeEditorComponent extends BaseEditor 0) { + for (let i = 0; i < fieldsFormArray.length; i++) { + this.addOption(sourceIndex, formArray.length - 1, fieldsFormArray.at(i).get('code').value); + } + } + } + + removeStaticItem(sourceIndex: number, staticIndex: number): void { + const formArray = (this.formGroup.get('definition').get('sources') as FormArray).at(sourceIndex).get('items') as FormArray; + formArray.removeAt(staticIndex); + + ReferenceTypeEditorModel.reApplyDefinitionSourcesValidators( + { + formGroup: this.formGroup, + validationErrorModel: this.editorModel.validationErrorModel + } + ); + formArray.markAsDirty(); + } + + + // Options + + addOption(sourceIndex: number, staticIndex: number, code: string): void { + const formArray = ((this.formGroup.get('definition').get('sources') as FormArray).at(sourceIndex).get('items') as FormArray).at(staticIndex).get('options') as FormArray; + + if (formArray && formArray.length > 0) { + for (let i = 0; i < formArray.length; i++) { + if (formArray.at(i).get('code').getRawValue() == code) { + return; + } + } + } + + const option = new StaticOptionEditorModel(this.editorModel.validationErrorModel); + formArray.push(option.buildForm({rootPath: 'definition.sources[' + sourceIndex + 'items[' + staticIndex + '].options[' + formArray.length + '].'})); + formArray.at(formArray.length -1 ).get('code').patchValue(code); + } + + removeOption(sourceIndex: number, staticIndex: number, optionIndex: number): void { + const formArray = ((this.formGroup.get('definition').get('sources') as FormArray).at(sourceIndex).get('items') as FormArray).at(staticIndex).get('options') as FormArray; + formArray.removeAt(optionIndex); + + ReferenceTypeEditorModel.reApplyDefinitionSourcesValidators( + { + formGroup: this.formGroup, + validationErrorModel: this.editorModel.validationErrorModel + } + ); + formArray.markAsDirty(); + } } diff --git a/frontend/src/app/ui/external-fetcher/external-fetcher-source.component.ts b/frontend/src/app/ui/external-fetcher/external-fetcher-source.component.ts index 0dbca5dc3..c09c84850 100644 --- a/frontend/src/app/ui/external-fetcher/external-fetcher-source.component.ts +++ b/frontend/src/app/ui/external-fetcher/external-fetcher-source.component.ts @@ -35,7 +35,6 @@ export class ExternalFetcherSourceComponent extends BaseComponent implements OnI ) { super(); } ngOnInit() { - if (this.referenceTypeSourceIndex != null && (this.formGroup.get('items') as FormArray).length == 0) this.addStaticItem(); } ngOnChanges(changes: SimpleChanges) {