Moving customization to json level

This commit is contained in:
2023-02-01 14:10:09 +01:00
parent 35c37749f3
commit 43ad5c3048
5 changed files with 22 additions and 60 deletions

View File

@@ -7,45 +7,6 @@ import { CrudService } from '../crud.service'
import {BehaviorSubject, NotFoundError} from "rxjs";
import { CrudFormlyJsonschemaService } from "../crud-formly-jsonschema.service";
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({
selector: 'crud-card',
templateUrl: './card.component.html',
@@ -55,7 +16,6 @@ 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<string> = new EventEmitter();
@@ -101,7 +61,7 @@ export class CardComponent implements OnInit {
}
fields$.subscribe((fields: any) => {
this.fields = this.field_customizer.applyCustomizers([fields]);
this.fields = [fields];
this._formLoading$.next(false);
});
}