Browse Source

Field rule context. (#668)

pull/671/head
Sebastian Stehle 5 years ago
committed by GitHub
parent
commit
21df5421fb
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      frontend/app/features/content/pages/content/content-page.component.ts
  2. 2
      frontend/app/features/content/shared/references/content-creator.component.ts
  3. 4
      frontend/app/shared/state/contents.forms-helpers.ts
  4. 19
      frontend/app/shared/state/contents.forms.spec.ts
  5. 10
      frontend/app/shared/state/contents.forms.ts

9
frontend/app/features/content/pages/content/content-page.component.ts

@ -88,7 +88,7 @@ export class ContentPageComponent extends ResourceOwner implements CanComponentD
.subscribe(schema => { .subscribe(schema => {
this.schema = schema; this.schema = schema;
this.contentForm = new EditContentForm(this.languages, this.schema, this.formContext.user); this.contentForm = new EditContentForm(this.languages, this.schema, this.formContext);
})); }));
this.own( this.own(
@ -96,9 +96,10 @@ export class ContentPageComponent extends ResourceOwner implements CanComponentD
.subscribe(content => { .subscribe(content => {
const isNewContent = isOtherContent(content, this.content); const isNewContent = isOtherContent(content, this.content);
this.content = content; this.formContext['initialContent'] = content;
this.formContext['initialContent'] = this.content; this.content = content;
this.contentForm.setContext(this.formContext);
this.autoSaveKey = { this.autoSaveKey = {
schemaId: this.schema.id, schemaId: this.schema.id,
@ -251,7 +252,7 @@ export class ContentPageComponent extends ResourceOwner implements CanComponentD
this.contentsState.loadVersion(content, version) this.contentsState.loadVersion(content, version)
.subscribe(dto => { .subscribe(dto => {
if (compare) { if (compare) {
this.contentFormCompare = new EditContentForm(this.languages, this.schema, this.formContext.user); this.contentFormCompare = new EditContentForm(this.languages, this.schema, this.formContext);
this.contentFormCompare.load(dto.payload); this.contentFormCompare.load(dto.payload);
this.contentFormCompare.setEnabled(false); this.contentFormCompare.setEnabled(false);

2
frontend/app/features/content/shared/references/content-creator.component.ts

@ -66,7 +66,7 @@ export class ContentCreatorComponent extends ResourceOwner implements OnInit {
this.schema = schema; this.schema = schema;
this.contentsState.schema = schema; this.contentsState.schema = schema;
this.contentForm = new EditContentForm(this.languages, this.schema, this.formContext.user); this.contentForm = new EditContentForm(this.languages, this.schema, { user: this.formContext.user });
this.changeDetector.markForCheck(); this.changeDetector.markForCheck();
} }

4
frontend/app/shared/state/contents.forms-helpers.ts

@ -89,7 +89,7 @@ export class CompiledRule {
private readonly rule: FieldRule private readonly rule: FieldRule
) { ) {
try { try {
this.function = new Function(`return function(user, data, itemData) { return ${rule.condition} }`)(); this.function = new Function(`return function(user, ctx, data, itemData) { return ${rule.condition} }`)();
} catch { } catch {
this.function = () => false; this.function = () => false;
} }
@ -97,7 +97,7 @@ export class CompiledRule {
public eval(context: RuleContext) { public eval(context: RuleContext) {
try { try {
return this.function(context.user, context.data, context.itemData); return this.function(context.user, context, context.data, context.itemData);
} catch { } catch {
return false; return false;
} }

19
frontend/app/shared/state/contents.forms.spec.ts

@ -276,6 +276,25 @@ describe('ContentForm', () => {
expectForm(contentForm.form, 'field3.de', { invalid: false }); expectForm(contentForm.form, 'field3.de', { invalid: false });
}); });
it('should require field based on context condition', () => {
const contentForm = createForm([
createField({ id: 1, properties: createProperties('Number'), partitioning: 'invariant' }),
createField({ id: 2, properties: createProperties('Number'), partitioning: 'invariant' })
], [{
field: 'field1', action: 'Require', condition: 'ctx.value < 100'
}]);
contentForm.setContext({ value: 50 });
const field1 = contentForm.get('field1')!.get('iv');
expect(field1!.form.valid).toBeFalsy();
contentForm.setContext({ value: 120 });
expect(field1!.form.valid).toBeTruthy();
});
it('should require field based on condition', () => { it('should require field based on condition', () => {
const contentForm = createForm([ const contentForm = createForm([
createField({ id: 1, properties: createProperties('Number'), partitioning: 'invariant' }), createField({ id: 1, properties: createProperties('Number'), partitioning: 'invariant' }),

10
frontend/app/shared/state/contents.forms.ts

@ -92,7 +92,7 @@ export class EditContentForm extends Form<FormGroup, any> {
} }
constructor(languages: ReadonlyArray<AppLanguageDto>, schema: SchemaDetailsDto, constructor(languages: ReadonlyArray<AppLanguageDto>, schema: SchemaDetailsDto,
private readonly user: any = {}, debounce = 100 private context: any, debounce = 100
) { ) {
super(new FormGroup({})); super(new FormGroup({}));
@ -184,6 +184,12 @@ export class EditContentForm extends Form<FormGroup, any> {
this.updateState(this.value); this.updateState(this.value);
} }
public setContext(context?: any) {
this.context = context;
this.updateState(this.value);
}
public submitCompleted(options?: { newValue?: any, noReset?: boolean }) { public submitCompleted(options?: { newValue?: any, noReset?: boolean }) {
super.submitCompleted(options); super.submitCompleted(options);
@ -191,7 +197,7 @@ export class EditContentForm extends Form<FormGroup, any> {
} }
private updateState(data: any) { private updateState(data: any) {
const context = { user: this.user, data }; const context = { ...this.context || {}, data };
for (const field of Object.values(this.fields)) { for (const field of Object.values(this.fields)) {
field.updateState(context, { isDisabled: this.form.disabled }); field.updateState(context, { isDisabled: this.form.disabled });

Loading…
Cancel
Save