|
@@ -48,7 +48,7 @@ export class DynaformComponent implements OnInit {
|
|
|
template?: TemplateRef<any>;
|
|
|
|
|
|
@Input()
|
|
|
- debug = false;
|
|
|
+ debug = true;
|
|
|
|
|
|
@Output()
|
|
|
call: EventEmitter<string> = new EventEmitter<string>();
|
|
@@ -58,6 +58,10 @@ export class DynaformComponent implements OnInit {
|
|
|
dynaFormRows: string[];
|
|
|
path: string[]; // path of current FormGroup - can be used to respond differently based on nesting level in template
|
|
|
|
|
|
+ // Colours for CSS in console
|
|
|
+ conRed = 'color: white; background-color: maroon; font-weight: bold;';
|
|
|
+ conGreen = 'color: white; background-color: green; font-weight: bold;';
|
|
|
+
|
|
|
constructor(
|
|
|
@Optional() private cc: ControlContainer,
|
|
|
) {}
|
|
@@ -65,16 +69,22 @@ export class DynaformComponent implements OnInit {
|
|
|
ngOnInit() {
|
|
|
// Get the formGroup from the formGroupName if necessary
|
|
|
if (!this.formGroup && this.formGroupName) {
|
|
|
- this.formGroup = <FormGroup>this.cc.control; // Get theFormGroup from the ControlContainer
|
|
|
+ this.formGroup = <FormGroup>this.cc.control; // Get theFormGroup from the inhected ControlContainer
|
|
|
}
|
|
|
if (!this.formGroup) {
|
|
|
throw new Error('Dynaform Component initialised without [formGroup] or formGroupName');
|
|
|
}
|
|
|
+ if (typeof this.formMetaData !== 'object') {
|
|
|
+ throw new Error('Dynaform: [meta] should be an object');
|
|
|
+ }
|
|
|
this.controlNames = Object.keys(this.formGroup.controls);
|
|
|
this.path = this.getFormGroupPath();
|
|
|
+ if (this.debug && this.path.length < 2) {
|
|
|
+ this.displayDebuggingInConsole();
|
|
|
+ }
|
|
|
|
|
|
// If we're given a formGroupName or nested FormGroup, and the form's full (or partial but fuller) metadata tree,
|
|
|
- // drill down to find the FormGroup's metadata
|
|
|
+ // drill down to find *this* FormGroup's metadata
|
|
|
const path = [...this.path]; // Clone to avoid mutating this.path
|
|
|
const metaDataKeysExpected = this.controlNames.join(',');
|
|
|
while (path.length && metaDataKeysExpected !== this.getContolKeysCSVFromMetadata(this.formMetaData)) {
|
|
@@ -84,8 +94,6 @@ export class DynaformComponent implements OnInit {
|
|
|
this.dynaFormRows = Object.keys(this.formMetaData);
|
|
|
|
|
|
// Check we've got a "FormGroup <---> MetaData" match
|
|
|
- //
|
|
|
- // const metaDataKeys = Object.keys(this.formMetaData).join(','); // OLD VERSION - before we introduced entities like ButtonGroup that don't create FormControls
|
|
|
const metaDataKeysFound = this.getContolKeysCSVFromMetadata(this.formMetaData);
|
|
|
if (metaDataKeysFound !== metaDataKeysExpected) {
|
|
|
throw new Error(`
|
|
@@ -148,7 +156,7 @@ export class DynaformComponent implements OnInit {
|
|
|
}
|
|
|
|
|
|
private getContolKeysCSVFromMetadata(metadata): string {
|
|
|
- // Return CSV of control keys references in nesting-levels metadata,
|
|
|
+ // Return CSV of control keys in current nesting-level's metadata,
|
|
|
// excluding metadata points that don't create FormControls, FromGroups or FormArrays
|
|
|
// (identified by their 'noFormControsl' flag)
|
|
|
// e.g. ButtonGroups, HTMLChunks, etc.
|
|
@@ -158,4 +166,11 @@ export class DynaformComponent implements OnInit {
|
|
|
.join(',');
|
|
|
}
|
|
|
|
|
|
+ private displayDebuggingInConsole(): void {
|
|
|
+ console.log('%c *** MetaData *** ', this.conGreen);
|
|
|
+ console.dir(this.formMetaData);
|
|
|
+ console.log('%c *** FormGroup *** ', this.conGreen);
|
|
|
+ console.dir(this.formGroup);
|
|
|
+ }
|
|
|
+
|
|
|
}
|