Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed nested arrays #3 #1511

Merged
merged 4 commits into from
Jul 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions ui/src/app/core/directives/json-schema-form-patch.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -47,13 +53,19 @@ 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) {
Expand Down Expand Up @@ -83,7 +95,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;
}
Expand Down