Skip to content

Commit 49481d9

Browse files
committed
Move condition evaluation to JSF service, and add support for new function() from { functionbody: string } conditions.
1 parent 50d4428 commit 49481d9

File tree

4 files changed

+42
-58
lines changed

4 files changed

+42
-58
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular2-json-schema-form",
3-
"version": "0.6.0-alpha.6",
3+
"version": "0.6.0-alpha.7",
44
"author": {
55
"name": "David Schnell-Davis",
66
"email": "[email protected]"
Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { ChangeDetectionStrategy, Component, Input, OnInit } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
22

33
import { JsonSchemaFormService } from '../../json-schema-form.service';
4-
import { isDefined, JsonPointer } from '../../shared';
4+
import { hasValue, JsonPointer } from '../../shared';
55

66
@Component({
77
selector: 'flex-layout-root-widget',
@@ -17,14 +17,14 @@ import { isDefined, JsonPointer } from '../../shared';
1717
[fxFlexOrder]="layoutNode?.options?.fxFlexOrder"
1818
[fxFlexOffset]="layoutNode?.options?.fxFlexOffset"
1919
[fxFlexAlign]="layoutNode?.options?.fxFlexAlign">
20-
<select-framework-widget *ngIf="isConditionallyShown(layoutNode)"
20+
<select-framework-widget *ngIf="showWidget(layoutNode)"
2121
[dataIndex]="layoutNode?.arrayItem ? (dataIndex || []).concat(i) : (dataIndex || [])"
2222
[layoutIndex]="(layoutIndex || []).concat(i)"
2323
[layoutNode]="layoutNode"></select-framework-widget>
2424
<div>`,
2525
changeDetection: ChangeDetectionStrategy.Default,
2626
})
27-
export class FlexLayoutRootComponent implements OnInit {
27+
export class FlexLayoutRootComponent {
2828
@Input() dataIndex: number[];
2929
@Input() layoutIndex: number[];
3030
@Input() layout: any[];
@@ -34,7 +34,9 @@ export class FlexLayoutRootComponent implements OnInit {
3434
private jsf: JsonSchemaFormService
3535
) { }
3636

37-
ngOnInit() { }
37+
removeItem(item) {
38+
this.jsf.removeItem(item);
39+
}
3840

3941
// Set attributes for flexbox child
4042
// (container attributes are set in flex-layout-section.component)
@@ -44,32 +46,7 @@ export class FlexLayoutRootComponent implements OnInit {
4446
(node.options || {})[attribute] || ['1', '1', 'auto'][index];
4547
}
4648

47-
trackByItem(layoutNode: any) {
48-
return (layoutNode || {})._id;
49-
}
50-
51-
removeItem(item) {
52-
this.jsf.removeItem(item);
53-
}
54-
55-
isConditionallyShown(layoutNode: any): boolean {
56-
const arrayIndex = this.dataIndex[this.dataIndex.length - 1];
57-
let result = true;
58-
if (isDefined((layoutNode.options || {}).condition)) {
59-
if (typeof layoutNode.options.condition === 'string') {
60-
let pointer = layoutNode.options.condition
61-
if (isDefined(arrayIndex)) {
62-
pointer = pointer.replace('[arrayIndex]', `[${arrayIndex}]`);
63-
}
64-
pointer = JsonPointer.parseObjectPath(pointer);
65-
result = !!JsonPointer.get(this.jsf.data, pointer);
66-
if (!result && pointer[0] === 'model') {
67-
result = !!JsonPointer.get({ model: this.jsf.data }, pointer);
68-
}
69-
} else if (typeof layoutNode.options.condition === 'function') {
70-
result = layoutNode.options.condition(this.jsf.data);
71-
}
72-
}
73-
return result;
49+
showWidget(layoutNode: any): boolean {
50+
return this.jsf.evaluateCondition(layoutNode, this.dataIndex);
7451
}
7552
}

src/lib/src/json-schema-form.service.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,34 @@ export class JsonSchemaFormService {
431431
);
432432
}
433433

434+
evaluateCondition(layoutNode: any, dataIndex: number[]): boolean {
435+
const arrayIndex = dataIndex && dataIndex[dataIndex.length - 1];
436+
let result = true;
437+
if (hasValue((layoutNode.options || {}).condition)) {
438+
if (typeof layoutNode.options.condition === 'string') {
439+
let pointer = layoutNode.options.condition
440+
if (hasValue(arrayIndex)) {
441+
pointer = pointer.replace('[arrayIndex]', `[${arrayIndex}]`);
442+
}
443+
pointer = JsonPointer.parseObjectPath(pointer);
444+
result = !!JsonPointer.get(this.data, pointer);
445+
if (!result && pointer[0] === 'model') {
446+
result = !!JsonPointer.get({ model: this.data }, pointer);
447+
}
448+
} else if (typeof layoutNode.options.condition === 'function') {
449+
result = layoutNode.options.condition(this.data);
450+
} else if (typeof layoutNode.options.condition.functionBody === 'string') {
451+
try {
452+
const dynFn = new Function(
453+
'model', 'arrayIndices', layoutNode.options.condition.functionBody
454+
);
455+
result = dynFn(this.data, dataIndex);
456+
} catch (e) { }
457+
}
458+
}
459+
return result;
460+
}
461+
434462
initializeControl(ctx: any, bind = true): boolean {
435463
if (!isObject(ctx)) { return false; }
436464
if (isEmpty(ctx.options)) {

src/lib/src/widget-library/root.component.ts

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Component, Input, Host } from '@angular/core';
22

33
import { JsonSchemaFormService } from '../json-schema-form.service';
4-
import { isDefined, JsonPointer } from '../shared';
4+
import { hasValue, JsonPointer } from '../shared';
55

66
@Component({
77
selector: 'root-widget',
@@ -18,7 +18,7 @@ import { isDefined, JsonPointer } from '../shared';
1818
[layoutIndex]="(layoutIndex || []).concat(i)"
1919
[layoutNode]="layoutItem"
2020
[orderable]="isDraggable(layoutItem)">
21-
<select-framework-widget *ngIf="isConditionallyShown(layoutItem)"
21+
<select-framework-widget *ngIf="showWidget(layoutItem)"
2222
[dataIndex]="layoutItem?.arrayItem ? (dataIndex || []).concat(i) : (dataIndex || [])"
2323
[layoutIndex]="(layoutIndex || []).concat(i)"
2424
[layoutNode]="layoutItem"></select-framework-widget>
@@ -72,28 +72,7 @@ export class RootComponent {
7272
(node.options || {})[attribute] || ['1', '1', 'auto'][index];
7373
}
7474

75-
trackByItem(layoutItem: any) {
76-
return layoutItem && layoutItem._id;
77-
}
78-
79-
isConditionallyShown(layoutNode: any): boolean {
80-
const arrayIndex = this.dataIndex && this.dataIndex[this.dataIndex.length - 1];
81-
let result = true;
82-
if (isDefined((layoutNode.options || {}).condition)) {
83-
if (typeof layoutNode.options.condition === 'string') {
84-
let pointer = layoutNode.options.condition
85-
if (isDefined(arrayIndex)) {
86-
pointer = pointer.replace('[arrayIndex]', `[${arrayIndex}]`);
87-
}
88-
pointer = JsonPointer.parseObjectPath(pointer);
89-
result = !!JsonPointer.get(this.jsf.data, pointer);
90-
if (!result && pointer[0] === 'model') {
91-
result = !!JsonPointer.get({ model: this.jsf.data }, pointer);
92-
}
93-
} else if (typeof layoutNode.options.condition === 'function') {
94-
result = layoutNode.options.condition(this.jsf.data);
95-
}
96-
}
97-
return result;
75+
showWidget(layoutNode: any): boolean {
76+
return this.jsf.evaluateCondition(layoutNode, this.dataIndex);
9877
}
9978
}

0 commit comments

Comments
 (0)