From 32bcc898d004a9a0f654ed99ed9c9d30477172cc Mon Sep 17 00:00:00 2001 From: Slonick Date: Wed, 19 Jul 2023 20:41:45 +0300 Subject: [PATCH 1/3] Fixed nested arrays; Fixed lint; --- .../json-schema-form-patch.directive.ts | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/ui/src/app/core/directives/json-schema-form-patch.directive.ts b/ui/src/app/core/directives/json-schema-form-patch.directive.ts index c17937d45..fcbbd9705 100644 --- a/ui/src/app/core/directives/json-schema-form-patch.directive.ts +++ b/ui/src/app/core/directives/json-schema-form-patch.directive.ts @@ -11,18 +11,18 @@ export class JsonSchemaFormPatchDirective { constructor( @Host() @Self() @Optional() public jsonSchemaForm: JsonSchemaFormComponent) { - let buildLayout_original = jsonSchemaForm.jsf.buildLayout.bind(jsonSchemaForm.jsf) + const buildLayoutOriginal = jsonSchemaForm.jsf.buildLayout.bind(jsonSchemaForm.jsf); jsonSchemaForm.jsf.buildLayout = (widgetLibrary: any) => { - buildLayout_original(widgetLibrary); + buildLayoutOriginal(widgetLibrary); if (jsonSchemaForm.jsf.formValues && this.jsfPatch) { return this.fixNestedArrayLayout( jsonSchemaForm.jsf.layout, - jsonSchemaForm.jsf.formValues + jsonSchemaForm.jsf.formValues, ); } - } + }; } @@ -34,9 +34,9 @@ export class JsonSchemaFormPatchDirective { private fixArray(items: any | any[], formData: any, refPointer: string) { if (Array.isArray(items)) { - items.filter(x => x.name !== "_bridge" && (x.dataType == "array" || x.arrayItem)).forEach(item => { + items.filter(x => x.name !== '_bridge' && (x.dataType === 'array' || x.arrayItem)).forEach(item => { this.fixNestedArray(item, formData, refPointer); - }) + }); } else { this.fixNestedArray(items, formData, refPointer); } @@ -45,9 +45,9 @@ export class JsonSchemaFormPatchDirective { private fixNestedArray(item: any, formData: any, refPointer: string) { if (item.items && Array.isArray(item.items)) { - const ref = item.items.find(x => x.type === "$ref"); + const ref = item.items.find(x => x.type === '$ref'); if (ref) { - const dataItems = item.items.filter(x => x.type === "section"); + const dataItems = item.items.filter(x => x.type === 'section'); const template = dataItems.length > 0 ? dataItems.reduce((a, b) => a.id > b.id ? a : b) : this.getItemTemplateFromRef(ref); @@ -58,7 +58,7 @@ export class JsonSchemaFormPatchDirective { // add missing items while (item.items.length - 1 < data.length) { const newItem = cloneDeep(template); - newItem._id = uniqueId("new_"); + newItem._id = uniqueId('new_'); item.items.unshift(newItem); } @@ -72,6 +72,11 @@ export class JsonSchemaFormPatchDirective { } else { this.fixArray(item.items, formData, refPointer); } + + item.items.filter(i => i.items && Array.isArray(i.items)).forEach(i => { + console.log(i); + this.fixArray(i.items, formData, refPointer); + }); } } @@ -85,9 +90,9 @@ export class JsonSchemaFormPatchDirective { } private getItemTemplateFromRef(ref: any) { - const templateNode = { - "type": "section", - "items": [] + const templateNode: { type: string; items: any[] } = { + type: 'section', + items: [], }; const item = cloneDeep(ref); From b80513e7c64ba911143eaab4c154024f36f61b7e Mon Sep 17 00:00:00 2001 From: Slonick Date: Wed, 19 Jul 2023 20:43:48 +0300 Subject: [PATCH 2/3] Removed console.log() --- ui/src/app/core/directives/json-schema-form-patch.directive.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/ui/src/app/core/directives/json-schema-form-patch.directive.ts b/ui/src/app/core/directives/json-schema-form-patch.directive.ts index fcbbd9705..ca0ac6b24 100644 --- a/ui/src/app/core/directives/json-schema-form-patch.directive.ts +++ b/ui/src/app/core/directives/json-schema-form-patch.directive.ts @@ -74,7 +74,6 @@ export class JsonSchemaFormPatchDirective { } item.items.filter(i => i.items && Array.isArray(i.items)).forEach(i => { - console.log(i); this.fixArray(i.items, formData, refPointer); }); } From de89e825f71a93376c3034c63669834fd0d5601e Mon Sep 17 00:00:00 2001 From: Slonick Date: Thu, 20 Jul 2023 09:58:01 +0300 Subject: [PATCH 3/3] Fixed nested arrays #3 --- .../json-schema-form-patch.directive.ts | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/ui/src/app/core/directives/json-schema-form-patch.directive.ts b/ui/src/app/core/directives/json-schema-form-patch.directive.ts index ca0ac6b24..f19e6cf5c 100644 --- a/ui/src/app/core/directives/json-schema-form-patch.directive.ts +++ b/ui/src/app/core/directives/json-schema-form-patch.directive.ts @@ -32,9 +32,15 @@ export class JsonSchemaFormPatchDirective { } private fixArray(items: any | any[], formData: any, refPointer: string) { - if (Array.isArray(items)) { - items.filter(x => x.name !== '_bridge' && (x.dataType === 'array' || x.arrayItem)).forEach(item => { + const configItems = items.filter(x => x.name !== '_bridge'); + const nestedItems = configItems + .filter(x => x.items && Array.isArray(x.items)) + .flatMap(x => x.items) + .filter(x => x.dataType === 'array' || x.arrayItem); + + const allItems = configItems.concat(nestedItems); + allItems.filter(x => x.dataType === 'array' || x.arrayItem).forEach(item => { this.fixNestedArray(item, formData, refPointer); }); } else { @@ -47,13 +53,17 @@ export class JsonSchemaFormPatchDirective { const ref = item.items.find(x => x.type === '$ref'); if (ref) { - const dataItems = item.items.filter(x => x.type === 'section'); + const dataItems = item.items.filter(x => x.type === 'section' || x.type === 'div'); const template = dataItems.length > 0 ? dataItems.reduce((a, b) => a.id > b.id ? a : b) : this.getItemTemplateFromRef(ref); const data = this.getDataFromPointer(formData, ref.dataPointer.replace(refPointer, '')); + if (data === null) { + return; + } + if (Array.isArray(data)) { // add missing items while (item.items.length - 1 < data.length) { @@ -83,7 +93,13 @@ export class JsonSchemaFormPatchDirective { let value = data; dataPointer.substring(1).split(/\//).filter(x => x !== '-') - .forEach((key: string) => value = value[key]); + .forEach((key: string) => { + try { + value = value[key]; + } catch { + value = null; + } + }); return value; }