Skip to content

Commit

Permalink
Fixed nested arrays settings (#1504)
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Kononenko committed Jul 19, 2023
1 parent bce11c6 commit 7f2fa91
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<ng-container>
<json-schema-form *ngIf="configSchema.schema" [options]="jsonFormOptions" [schema]="configSchema.schema"
<json-schema-form *ngIf="configSchema.schema" [jsfPatch]="configSchema.fixArrays" [options]="jsonFormOptions" [schema]="configSchema.schema"
[layout]="configSchema.layout" [form]="configSchema.form" [data]="currentData" framework="bootstrap-4"
(onChanges)="onChanges($event)" (isValid)="validChange($event)" class="ng-bs4-validate">
</json-schema-form>
</ng-container>
</ng-container>
3 changes: 3 additions & 0 deletions ui/src/app/core/core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { ConfirmComponent } from './components/confirm/confirm.component';
import { SchemaFormComponent } from './components/schema-form/schema-form.component';
import { QrcodeComponent } from './components/qrcode/qrcode.component';
import { RtlDirective } from './directives/rtl.directive';
import { JsonSchemaFormPatchDirective } from './directives/json-schema-form-patch.directive';

@NgModule({
declarations: [
Expand All @@ -26,6 +27,7 @@ import { RtlDirective } from './directives/rtl.directive';
HrefTargetBlankDirective,
LongClickDirective,
RtlDirective,
JsonSchemaFormPatchDirective,
BackupRestoreComponent,
ScheduledBackupsComponent,
ConfirmComponent,
Expand All @@ -47,6 +49,7 @@ import { RtlDirective } from './directives/rtl.directive';
HrefTargetBlankDirective,
LongClickDirective,
RtlDirective,
JsonSchemaFormPatchDirective,
],
})
export class CoreModule { }
97 changes: 97 additions & 0 deletions ui/src/app/core/directives/json-schema-form-patch.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import {Directive, Host, Input, Optional, Self} from '@angular/core';
import {JsonSchemaFormComponent} from '@oznu/ngx-bs4-jsonform';
import {cloneDeep, merge, uniqueId} from 'lodash-es';

@Directive({
selector: '[jsfPatch]',
})
export class JsonSchemaFormPatchDirective {
@Input() jsfPatch = false;

constructor(
@Host() @Self() @Optional() public jsonSchemaForm: JsonSchemaFormComponent) {

let buildLayout_original = jsonSchemaForm.jsf.buildLayout.bind(jsonSchemaForm.jsf)

Check failure on line 14 in ui/src/app/core/directives/json-schema-form-patch.directive.ts

View workflow job for this annotation

GitHub Actions / lint / ESLint

'buildLayout_original' is never reassigned. Use 'const' instead

Check failure on line 14 in ui/src/app/core/directives/json-schema-form-patch.directive.ts

View workflow job for this annotation

GitHub Actions / lint / ESLint

Missing semicolon

jsonSchemaForm.jsf.buildLayout = (widgetLibrary: any) => {

buildLayout_original(widgetLibrary);
if (jsonSchemaForm.jsf.formValues && this.jsfPatch) {
return this.fixNestedArrayLayout(
jsonSchemaForm.jsf.layout,
jsonSchemaForm.jsf.formValues

Check failure on line 22 in ui/src/app/core/directives/json-schema-form-patch.directive.ts

View workflow job for this annotation

GitHub Actions / lint / ESLint

Missing trailing comma
);
}
}

Check failure on line 25 in ui/src/app/core/directives/json-schema-form-patch.directive.ts

View workflow job for this annotation

GitHub Actions / lint / ESLint

Missing semicolon

}

private fixNestedArrayLayout(builtLayout: any[], formData: any) {
this.fixArray(builtLayout, formData, '');
return builtLayout;
}

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 => {

Check failure on line 37 in ui/src/app/core/directives/json-schema-form-patch.directive.ts

View workflow job for this annotation

GitHub Actions / lint / ESLint

Strings must use singlequote

Check failure on line 37 in ui/src/app/core/directives/json-schema-form-patch.directive.ts

View workflow job for this annotation

GitHub Actions / lint / ESLint

Expected '===' and instead saw '=='

Check failure on line 37 in ui/src/app/core/directives/json-schema-form-patch.directive.ts

View workflow job for this annotation

GitHub Actions / lint / ESLint

Strings must use singlequote
this.fixNestedArray(item, formData, refPointer);
})

Check failure on line 39 in ui/src/app/core/directives/json-schema-form-patch.directive.ts

View workflow job for this annotation

GitHub Actions / lint / ESLint

Missing semicolon
} else {
this.fixNestedArray(items, formData, refPointer);
}
}

private fixNestedArray(item: any, formData: any, refPointer: string) {
if (item.items && Array.isArray(item.items)) {

const ref = item.items.find(x => x.type === "$ref");

Check failure on line 48 in ui/src/app/core/directives/json-schema-form-patch.directive.ts

View workflow job for this annotation

GitHub Actions / lint / ESLint

Strings must use singlequote
if (ref) {
const dataItems = item.items.filter(x => x.type === "section");

Check failure on line 50 in ui/src/app/core/directives/json-schema-form-patch.directive.ts

View workflow job for this annotation

GitHub Actions / lint / ESLint

Strings must use singlequote
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 (Array.isArray(data)) {
// add missing items
while (item.items.length - 1 < data.length) {
const newItem = cloneDeep(template);
newItem._id = uniqueId("new_");

item.items.unshift(newItem);
}

data.forEach((d: any, index: number) => {
this.fixArray(item.items[index], d, ref.dataPointer);
});
} else {
this.fixArray(item.items, formData, ref.dataPointer);
}
} else {
this.fixArray(item.items, formData, refPointer);
}
}
}

private getDataFromPointer(data: any, dataPointer: string) {
let value = data;

dataPointer.substring(1).split(/\//).filter(x => x !== '-')
.forEach((key: string) => value = value[key]);

return value;
}

private getItemTemplateFromRef(ref: any) {
const templateNode = {
"type": "section",
"items": []
};

const item = cloneDeep(ref);
merge(item, templateNode);
return item;
}
}

0 comments on commit 7f2fa91

Please sign in to comment.