From c360878b5a0b8598cb5b65af54ee38ec9e7073c5 Mon Sep 17 00:00:00 2001 From: ewandor Date: Tue, 31 Jan 2023 23:53:43 +0100 Subject: [PATCH] Field customizer --- .../src/common/crud/card/card.component.ts | 46 +++++++++++++++++-- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/front/app/src/common/crud/card/card.component.ts b/front/app/src/common/crud/card/card.component.ts index c4669b30..bea5316e 100644 --- a/front/app/src/common/crud/card/card.component.ts +++ b/front/app/src/common/crud/card/card.component.ts @@ -4,11 +4,46 @@ import { FormlyFieldConfig } from '@ngx-formly/core' import { ActivatedRoute, Router } from '@angular/router'; import { CrudService } from '../crud.service' -import { BehaviorSubject } from "rxjs"; -import {CrudFormlyJsonschemaService} from "../crud-formly-jsonschema.service"; +import {BehaviorSubject, NotFoundError} from "rxjs"; +import { CrudFormlyJsonschemaService } from "../crud-formly-jsonschema.service"; -export interface Model { - email: string; +export class FieldCustomizer { + protected customizers = {}; + + applyCustomizers(form: FormlyFieldConfig[]): FormlyFieldConfig[] { + for (const [property, customizer] of Object.entries(this.customizers)) { + const f = this.getFieldByPath(form, property); + this.apply(f, customizer) + } + return form + } + + apply(field: FormlyFieldConfig, customizer: any) { + if (customizer.hasOwnProperty("props")) { + for (const [property, value] of Object.entries(customizer.props)) { + field.props![property] = value; + } + } + } + + getFieldByPath(form: FormlyFieldConfig[], path: string): FormlyFieldConfig { + let field = form[0]; + let array = path.split('.'); + for (const k in array) { + field = this.getFieldByKey(field, array[k]) + } + + return field; + } + + getFieldByKey(field: FormlyFieldConfig, key: string): FormlyFieldConfig { + for (let i=0; i < field.fieldGroup!.length; i++) { + if (field.fieldGroup![i].key == key) { + return field.fieldGroup![i] + } + } + throw new NotFoundError(`field ${key} not found in fieldgroup`) + } } @Component({ @@ -20,6 +55,7 @@ export class CardComponent implements OnInit { @Input() resource: string | undefined; @Input() resource_id: string | null = null; @Input() schema: string | undefined; + @Input() field_customizer: FieldCustomizer = new FieldCustomizer(); @Input() is_modal: Boolean = false; @Output() resourceCreated: EventEmitter = new EventEmitter(); @@ -65,7 +101,7 @@ export class CardComponent implements OnInit { } fields$.subscribe((fields: any) => { - this.fields = [fields] + this.fields = this.field_customizer.applyCustomizers([fields]); this._formLoading$.next(false); }); }