Skip to content

Commit

Permalink
Fixed nested arrays settings #2 (#1510)
Browse files Browse the repository at this point in the history
* Fixed nested arrays;
Fixed lint;

* Removed console.log()
  • Loading branch information
Artem Kononenko committed Jul 19, 2023
1 parent 7f2fa91 commit 9dae60b
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions ui/src/app/core/directives/json-schema-form-patch.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
}
};

}

Expand All @@ -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);
}
Expand All @@ -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);
Expand All @@ -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);
}
Expand All @@ -72,6 +72,10 @@ export class JsonSchemaFormPatchDirective {
} else {
this.fixArray(item.items, formData, refPointer);
}

item.items.filter(i => i.items && Array.isArray(i.items)).forEach(i => {
this.fixArray(i.items, formData, refPointer);
});
}
}

Expand All @@ -85,9 +89,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);
Expand Down

0 comments on commit 9dae60b

Please sign in to comment.