[AAE-8086] Propagate form events (#7572)

* [AAE-8086] Propagate events

* [AAE-8086] Add form rules manager to the form renderer

* [AAE-8086] Extensibility improvements

* [AAE-8086] Fix wrong import

* [AAE-8087] Add form actions

* [AAE-8086] Initialize form rules manager on form renderer component changes

* [AAE-8087] Fix form actions

* [AAE-8087] Fix unit tests for field visibility

* trigger travis
This commit is contained in:
Pablo Martinez Garcia
2022-04-14 11:59:12 +02:00
committed by GitHub
parent 48c3fac018
commit a02a8a4ad9
18 changed files with 464 additions and 34 deletions

View File

@@ -24,7 +24,7 @@ import { FormFieldModel } from './form-field.model';
import { FormOutcomeModel } from './form-outcome.model';
import { FormModel } from './form.model';
import { TabModel } from './tab.model';
import { fakeMetadataForm, fakeViewerForm } from 'process-services-cloud/src/lib/form/mocks/cloud-form.mock';
import { cloudFormMock, fakeMetadataForm, fakeViewerForm } from 'process-services-cloud/src/lib/form/mocks/cloud-form.mock';
import { Node } from '@alfresco/js-api';
import { UploadWidgetContentLinkModel } from './upload-widget-content-link.model';
import { AlfrescoApiService } from '../../../../services';
@@ -607,8 +607,8 @@ describe('FormModel', () => {
expect(form.values['pfx_property_one']).toBe('testValue');
expect(form.values['pfx_property_two']).toBe(true);
expect(form.values['pfx_property_three']).toEqual({ id: 'opt_1', name: 'Option 1'});
expect(form.values['pfx_property_four']).toEqual({ id: 'option_2', name: 'Option: 2'});
expect(form.values['pfx_property_three']).toEqual({ id: 'opt_1', name: 'Option 1' });
expect(form.values['pfx_property_four']).toEqual({ id: 'option_2', name: 'Option: 2' });
expect(form.values['pfx_property_five']).toEqual('green');
expect(form.values['pfx_property_six']).toEqual('text-value');
expect(form.values['pfx_property_seven']).toBeNull();
@@ -649,4 +649,53 @@ describe('FormModel', () => {
expect(form.values['cmfb85b2a7295ba41209750bca176ccaf9a']).toBeNull();
});
});
describe('Form actions', () => {
let form: FormModel;
let fieldId: string;
let field: FormFieldModel;
beforeEach(() => {
form = new FormModel(cloudFormMock);
fieldId = 'text1';
field = form.getFieldById(fieldId);
});
it('should change field visibility', () => {
const originalValue = field.isVisible;
form.changeFieldVisibility(fieldId, !originalValue);
expect(field.isVisible).toEqual(!originalValue);
});
it('should change field disabled', () => {
const originalValue = field.readOnly;
form.changeFieldDisabled(fieldId, !originalValue);
expect(field.readOnly).toEqual(!originalValue);
});
it('should change field required', () => {
const originalValue = field.required;
form.changeFieldRequired(fieldId, !originalValue);
expect(field.required).toEqual(!originalValue);
});
it('should change field value', () => {
form.changeFieldValue(fieldId, 'newValue');
expect(field.value).toEqual('newValue');
});
it('should change variable value', () => {
form.changeVariableValue('FormVarStrId', 'newValue');
expect(form.getFormVariable('FormVarStrId').value).toEqual('newValue');
});
});
});

View File

@@ -33,6 +33,7 @@ import { FormFieldTemplates } from './form-field-templates';
import { UploadWidgetContentLinkModel } from './upload-widget-content-link.model';
import { FormValidationService } from '../../../services/form-validation-service.interface';
import { ProcessFormModel } from './process-form-model.interface';
import { WidgetTypeEnum, WidgetVisibilityModel } from '../../../models/widget-visibility.model';
export interface FormRepresentationModel {
[key: string]: any;
@@ -419,4 +420,44 @@ export class FormModel implements ProcessFormModel {
viewer.value = viewer.parseValue(viewer.json);
});
}
changeFieldVisibility(fieldId: string, visibility: boolean): void {
const visibilityRule: WidgetVisibilityModel = new WidgetVisibilityModel();
const field = this.getFieldById(fieldId);
if (!!field) {
visibilityRule.operator = visibility ? 'empty' : '!empty';
visibilityRule.leftType = WidgetTypeEnum.field;
field.visibilityCondition = visibilityRule;
field.isVisible = false;
}
}
changeFieldDisabled(fieldId: string, disabled: boolean): void {
const field = this.getFieldById(fieldId);
if (!!field) {
field.readOnly = this.readOnly || disabled;
}
}
changeFieldRequired(fieldId: string, required: boolean): void {
const field = this.getFieldById(fieldId);
if (!!field) {
field.required = required;
}
}
changeFieldValue(fieldId: string, value: any): void {
const field = this.getFieldById(fieldId);
if (!!field) {
field.value = value;
}
}
changeVariableValue(variableId: string, value: any): void {
const variable = this.getFormVariable(variableId);
if (!!variable) {
variable.value = value;
}
}
}

View File

@@ -54,7 +54,16 @@ describe('WidgetComponent', () => {
element.click();
});
});
it('should click event be redirect on the form rules event service', (done) => {
widget.formService.formRulesEvent.subscribe((event) => {
expect(event.type).toEqual('click');
done();
});
element.click();
});
});
it('should check field', () => {
expect(widget.hasField()).toBeFalsy();
@@ -64,7 +73,7 @@ describe('WidgetComponent', () => {
it('should send an event after view init', (done) => {
const fakeForm = new FormModel();
const fakeField = new FormFieldModel(fakeForm, {id: 'fakeField', value: 'fakeValue'});
const fakeField = new FormFieldModel(fakeForm, { id: 'fakeField', value: 'fakeValue' });
widget.field = fakeField;
widget.fieldChanged.subscribe((field) => {
@@ -79,7 +88,7 @@ describe('WidgetComponent', () => {
it('should send an event when a field is changed', (done) => {
const fakeForm = new FormModel();
const fakeField = new FormFieldModel(fakeForm, {id: 'fakeField', value: 'fakeValue'});
const fakeField = new FormFieldModel(fakeForm, { id: 'fakeField', value: 'fakeValue' });
widget.fieldChanged.subscribe((field) => {
expect(field).not.toBe(null);
expect(field.id).toBe('fakeField');
@@ -96,10 +105,10 @@ describe('WidgetComponent', () => {
widget.field = new FormFieldModel(null);
expect(widget.isRequired()).toBeFalsy();
widget.field = new FormFieldModel(null, {required: false});
widget.field = new FormFieldModel(null, { required: false });
expect(widget.isRequired()).toBeFalsy();
widget.field = new FormFieldModel(null, {required: true});
widget.field = new FormFieldModel(null, { required: true });
expect(widget.isRequired()).toBeTruthy();
});
});

View File

@@ -18,6 +18,8 @@
/* eslint-disable @angular-eslint/component-selector */
import { AfterViewInit, Component, EventEmitter, Input, Output, ViewEncapsulation } from '@angular/core';
import { FormFieldEvent } from '../../events/form-field.event';
import { FormRulesEvent } from '../../events/form-rules.event';
import { FormService } from './../../services/form.service';
import { FormFieldModel } from './core/index';
@@ -106,6 +108,7 @@ export class WidgetComponent implements AfterViewInit {
event(event: Event): void {
this.formService.formEvents.next(event);
this.formService.formRulesEvent.next(new FormRulesEvent(event?.type, new FormFieldEvent(this.field?.form, this.field), event));
}
markAsTouched() {