mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[ADF-4782] VisibilityConditions - fix chaining conditions (#4971)
* [ADF-4782] VisibilityConditions - fix chaining conditions * add return types
This commit is contained in:
committed by
Eugenio Romano
parent
0bca6607a9
commit
bf828b6389
@@ -34,7 +34,7 @@ import { AlfrescoApiServiceMock } from '../../mock/alfresco-api.service.mock';
|
|||||||
import {
|
import {
|
||||||
fakeFormJson, fakeTaskProcessVariableModels,
|
fakeFormJson, fakeTaskProcessVariableModels,
|
||||||
formTest, formValues, complexVisibilityJsonVisible,
|
formTest, formValues, complexVisibilityJsonVisible,
|
||||||
complexVisibilityJsonNotVisible } from 'core/mock/form/widget-visibility-cloud.service.mock';
|
nextConditionForm, complexVisibilityJsonNotVisible } from 'core/mock/form/widget-visibility-cloud.service.mock';
|
||||||
|
|
||||||
declare let jasmine: any;
|
declare let jasmine: any;
|
||||||
|
|
||||||
@@ -187,6 +187,23 @@ describe('WidgetVisibilityCloudService', () => {
|
|||||||
booleanResult = service.evaluateCondition(null, null, undefined);
|
booleanResult = service.evaluateCondition(null, null, undefined);
|
||||||
expect(booleanResult).toBeUndefined();
|
expect(booleanResult).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should evaluate true visibility condition with next condition operator', (done) => {
|
||||||
|
const myForm = new FormModel(nextConditionForm);
|
||||||
|
service.refreshVisibility(myForm);
|
||||||
|
const nextConditionFormVIsibility = myForm.getFieldById('Text4');
|
||||||
|
expect(nextConditionFormVIsibility.isVisible).toBeTruthy();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should evaluate false visibility condition with next condition operator', (done) => {
|
||||||
|
const myForm = new FormModel(nextConditionForm);
|
||||||
|
myForm.getFieldById('Text3').value = 'wrong value';
|
||||||
|
service.refreshVisibility(myForm);
|
||||||
|
const nextConditionFormVIsibility = myForm.getFieldById('Text4');
|
||||||
|
expect(nextConditionFormVIsibility.isVisible).toBeFalsy();
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('should retrieve the process variables', () => {
|
describe('should retrieve the process variables', () => {
|
||||||
|
@@ -190,11 +190,12 @@ describe('WidgetVisibilityService', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('should retrieve the process variables', () => {
|
describe('should retrieve the process variables', () => {
|
||||||
let fakeFormWithField = new FormModel(fakeFormJson);
|
let fakeFormWithField: FormModel;
|
||||||
let visibilityObjTest: WidgetVisibilityModel;
|
let visibilityObjTest: WidgetVisibilityModel;
|
||||||
const chainedVisibilityObj = new WidgetVisibilityModel({});
|
const chainedVisibilityObj = new WidgetVisibilityModel({});
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
fakeFormWithField = new FormModel(fakeFormJson);
|
||||||
visibilityObjTest = new WidgetVisibilityModel({});
|
visibilityObjTest = new WidgetVisibilityModel({});
|
||||||
fakeFormWithField = new FormModel(fakeFormJson);
|
fakeFormWithField = new FormModel(fakeFormJson);
|
||||||
});
|
});
|
||||||
@@ -342,7 +343,8 @@ describe('WidgetVisibilityService', () => {
|
|||||||
|
|
||||||
describe('should return the value of the field', () => {
|
describe('should return the value of the field', () => {
|
||||||
let visibilityObjTest: WidgetVisibilityModel;
|
let visibilityObjTest: WidgetVisibilityModel;
|
||||||
let fakeFormWithField = new FormModel(fakeFormJson);
|
let fakeFormWithField: FormModel;
|
||||||
|
|
||||||
const jsonFieldFake = {
|
const jsonFieldFake = {
|
||||||
id: 'FAKE_FORM_FIELD_ID',
|
id: 'FAKE_FORM_FIELD_ID',
|
||||||
value: 'FAKE_FORM_FIELD_VALUE',
|
value: 'FAKE_FORM_FIELD_VALUE',
|
||||||
@@ -358,6 +360,7 @@ describe('WidgetVisibilityService', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
fakeFormWithField = new FormModel(fakeFormJson);
|
||||||
visibilityObjTest = new WidgetVisibilityModel();
|
visibilityObjTest = new WidgetVisibilityModel();
|
||||||
fakeFormWithField = new FormModel(fakeFormJson);
|
fakeFormWithField = new FormModel(fakeFormJson);
|
||||||
formTest.values = formValues;
|
formTest.values = formValues;
|
||||||
@@ -925,13 +928,14 @@ describe('WidgetVisibilityService', () => {
|
|||||||
|
|
||||||
describe('Visibility based on form variables', () => {
|
describe('Visibility based on form variables', () => {
|
||||||
|
|
||||||
const fakeFormWithVariables = new FormModel(fakeFormJson);
|
let fakeFormWithVariables = new FormModel(fakeFormJson);
|
||||||
const complexVisibilityModel = new FormModel(complexVisibilityJsonVisible);
|
const complexVisibilityModel = new FormModel(complexVisibilityJsonVisible);
|
||||||
const complexVisibilityJsonNotVisibleModel = new FormModel(complexVisibilityJsonNotVisible);
|
const complexVisibilityJsonNotVisibleModel = new FormModel(complexVisibilityJsonNotVisible);
|
||||||
let visibilityObjTest: WidgetVisibilityModel;
|
let visibilityObjTest: WidgetVisibilityModel;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
visibilityObjTest = new WidgetVisibilityModel();
|
visibilityObjTest = new WidgetVisibilityModel();
|
||||||
|
fakeFormWithVariables = new FormModel(fakeFormJson);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should set visibility to true when validation for string variables succeeds', () => {
|
it('should set visibility to true when validation for string variables succeeds', () => {
|
||||||
@@ -986,7 +990,7 @@ describe('WidgetVisibilityService', () => {
|
|||||||
it('should set visibility to true when validation for date variables succeeds', () => {
|
it('should set visibility to true when validation for date variables succeeds', () => {
|
||||||
visibilityObjTest.leftRestResponseId = 'dob';
|
visibilityObjTest.leftRestResponseId = 'dob';
|
||||||
visibilityObjTest.operator = '==';
|
visibilityObjTest.operator = '==';
|
||||||
visibilityObjTest.rightValue = '2019-05-13';
|
visibilityObjTest.rightValue = '2019-05-13T00:00:00.000Z';
|
||||||
const isVisible = service.isFieldVisible(fakeFormWithVariables, visibilityObjTest);
|
const isVisible = service.isFieldVisible(fakeFormWithVariables, visibilityObjTest);
|
||||||
|
|
||||||
expect(isVisible).toBeTruthy();
|
expect(isVisible).toBeTruthy();
|
||||||
@@ -995,7 +999,7 @@ describe('WidgetVisibilityService', () => {
|
|||||||
it('should set visibility to false when validation for date variables fails', () => {
|
it('should set visibility to false when validation for date variables fails', () => {
|
||||||
visibilityObjTest.leftRestResponseId = 'dob';
|
visibilityObjTest.leftRestResponseId = 'dob';
|
||||||
visibilityObjTest.operator = '==';
|
visibilityObjTest.operator = '==';
|
||||||
visibilityObjTest.rightValue = '2019-05-15';
|
visibilityObjTest.rightValue = '2019-05-15T00:00:00.000Z';
|
||||||
const isVisible = service.isFieldVisible(fakeFormWithVariables, visibilityObjTest);
|
const isVisible = service.isFieldVisible(fakeFormWithVariables, visibilityObjTest);
|
||||||
|
|
||||||
expect(isVisible).toBeFalsy();
|
expect(isVisible).toBeFalsy();
|
||||||
@@ -1004,7 +1008,7 @@ describe('WidgetVisibilityService', () => {
|
|||||||
it('should validate visiblity for form fields by finding the field with id', () => {
|
it('should validate visiblity for form fields by finding the field with id', () => {
|
||||||
visibilityObjTest.leftRestResponseId = '0207b649-ff07-4f3a-a589-d10afa507b9b';
|
visibilityObjTest.leftRestResponseId = '0207b649-ff07-4f3a-a589-d10afa507b9b';
|
||||||
visibilityObjTest.operator = '==';
|
visibilityObjTest.operator = '==';
|
||||||
visibilityObjTest.rightValue = '2019-05-13';
|
visibilityObjTest.rightValue = '2019-05-13T00:00:00.000Z';
|
||||||
const isVisible = service.isFieldVisible(fakeFormWithVariables, visibilityObjTest);
|
const isVisible = service.isFieldVisible(fakeFormWithVariables, visibilityObjTest);
|
||||||
|
|
||||||
expect(isVisible).toBeTruthy();
|
expect(isVisible).toBeTruthy();
|
||||||
|
@@ -67,14 +67,17 @@ export class WidgetVisibilityService {
|
|||||||
const leftValue = this.getLeftValue(form, visibilityObj);
|
const leftValue = this.getLeftValue(form, visibilityObj);
|
||||||
const rightValue = this.getRightValue(form, visibilityObj);
|
const rightValue = this.getRightValue(form, visibilityObj);
|
||||||
const actualResult = this.evaluateCondition(leftValue, rightValue, visibilityObj.operator);
|
const actualResult = this.evaluateCondition(leftValue, rightValue, visibilityObj.operator);
|
||||||
accumulator.push({ value: actualResult, operator: visibilityObj.nextConditionOperator });
|
|
||||||
if (visibilityObj.nextCondition) {
|
|
||||||
result = this.isFieldVisible(form, visibilityObj.nextCondition, accumulator);
|
|
||||||
} else {
|
|
||||||
result = accumulator[0].value ? accumulator[0].value : result;
|
|
||||||
|
|
||||||
|
if (this.isValidOperator(visibilityObj.nextConditionOperator)) {
|
||||||
|
accumulator.push({ value: actualResult, operator: visibilityObj.nextConditionOperator });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.isValidCondition(visibilityObj.nextCondition)) {
|
||||||
|
result = this.isFieldVisible(form, visibilityObj.nextCondition, accumulator);
|
||||||
|
} else if (accumulator[0] !== undefined) {
|
||||||
|
result = accumulator[0].value;
|
||||||
for (let i = 1; i < accumulator.length; i++) {
|
for (let i = 1; i < accumulator.length; i++) {
|
||||||
if (accumulator[i - 1].operator && accumulator[i].value) {
|
if (accumulator[i] !== undefined) {
|
||||||
result = this.evaluateLogicalOperation(
|
result = this.evaluateLogicalOperation(
|
||||||
accumulator[i - 1].operator,
|
accumulator[i - 1].operator,
|
||||||
result,
|
result,
|
||||||
@@ -82,6 +85,8 @@ export class WidgetVisibilityService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
result = actualResult;
|
||||||
}
|
}
|
||||||
return !!result;
|
return !!result;
|
||||||
|
|
||||||
@@ -287,6 +292,14 @@ export class WidgetVisibilityService {
|
|||||||
return res || {};
|
return res || {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private isValidOperator(operator: string): boolean {
|
||||||
|
return operator !== undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
private isValidCondition(condition: WidgetVisibilityModel): boolean {
|
||||||
|
return !!(condition && condition.operator);
|
||||||
|
}
|
||||||
|
|
||||||
private handleError(err) {
|
private handleError(err) {
|
||||||
this.logService.error('Error while performing a call');
|
this.logService.error('Error while performing a call');
|
||||||
return throwError('Error while performing a call - Server error');
|
return throwError('Error while performing a call - Server error');
|
||||||
|
@@ -15,23 +15,6 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*!
|
|
||||||
* @license
|
|
||||||
* Copyright 2019 Alfresco Software, Ltd.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the 'License');
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an 'AS IS' BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import { FormModel, FormValues } from '../../form/components/widgets/core/index';
|
import { FormModel, FormValues } from '../../form/components/widgets/core/index';
|
||||||
|
|
||||||
export let formTest = new FormModel({});
|
export let formTest = new FormModel({});
|
||||||
@@ -863,3 +846,111 @@ export let complexVisibilityJsonNotVisible = {
|
|||||||
'gridsterForm': false
|
'gridsterForm': false
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export let nextConditionForm = {
|
||||||
|
id: '9999',
|
||||||
|
name: 'FORM_PROCESS_VARIABLE_VISIBILITY',
|
||||||
|
processDefinitionId: 'PROCESS_TEST:9:9999',
|
||||||
|
processDefinitionName: 'PROCESS_TEST',
|
||||||
|
processDefinitionKey: 'PROCESS_TEST',
|
||||||
|
taskId: '999',
|
||||||
|
taskName: 'TEST',
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
'id': '60114002-0da2-4513-ab65-845b4e55d3c8',
|
||||||
|
'name': 'Label',
|
||||||
|
'type': 'container',
|
||||||
|
'tab': null,
|
||||||
|
'numberOfColumns': 2,
|
||||||
|
'fields': {
|
||||||
|
'1': [
|
||||||
|
{
|
||||||
|
'id': 'Text1',
|
||||||
|
'name': 'Text1',
|
||||||
|
'type': 'text',
|
||||||
|
'required': false,
|
||||||
|
'colspan': 1,
|
||||||
|
'placeholder': null,
|
||||||
|
'minLength': 0,
|
||||||
|
'maxLength': 0,
|
||||||
|
'regexPattern': null,
|
||||||
|
'visibilityCondition': null,
|
||||||
|
'params': {
|
||||||
|
'existingColspan': 1,
|
||||||
|
'maxColspan': 2
|
||||||
|
},
|
||||||
|
'value': 'multiline'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'id': 'Text3',
|
||||||
|
'name': 'Text3',
|
||||||
|
'type': 'text',
|
||||||
|
'required': false,
|
||||||
|
'colspan': 1,
|
||||||
|
'placeholder': null,
|
||||||
|
'minLength': 0,
|
||||||
|
'maxLength': 0,
|
||||||
|
'regexPattern': null,
|
||||||
|
'visibilityCondition': null,
|
||||||
|
'params': {
|
||||||
|
'existingColspan': 1,
|
||||||
|
'maxColspan': 2
|
||||||
|
},
|
||||||
|
'value': 'one'
|
||||||
|
}
|
||||||
|
],
|
||||||
|
'2': [
|
||||||
|
{
|
||||||
|
'id': 'Text2',
|
||||||
|
'name': 'Text2',
|
||||||
|
'type': 'text',
|
||||||
|
'required': false,
|
||||||
|
'colspan': 1,
|
||||||
|
'placeholder': null,
|
||||||
|
'minLength': 0,
|
||||||
|
'maxLength': 0,
|
||||||
|
'regexPattern': null,
|
||||||
|
'visibilityCondition': null,
|
||||||
|
'params': {
|
||||||
|
'existingColspan': 1,
|
||||||
|
'maxColspan': 2
|
||||||
|
},
|
||||||
|
'value': 'one'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'id': 'Text4',
|
||||||
|
'name': 'Text4',
|
||||||
|
'type': 'text',
|
||||||
|
'required': false,
|
||||||
|
'colspan': 1,
|
||||||
|
'placeholder': null,
|
||||||
|
'minLength': 0,
|
||||||
|
'maxLength': 0,
|
||||||
|
'regexPattern': null,
|
||||||
|
'visibilityCondition': {
|
||||||
|
'leftType': 'field',
|
||||||
|
'leftValue': 'Text1',
|
||||||
|
'operator': '==',
|
||||||
|
'rightValue': 'multiline',
|
||||||
|
'rightType': 'value',
|
||||||
|
'nextConditionOperator': 'and',
|
||||||
|
'nextCondition': {
|
||||||
|
'leftType': 'field',
|
||||||
|
'leftValue': 'Text2',
|
||||||
|
'operator': '==',
|
||||||
|
'rightValue': 'Text3',
|
||||||
|
'rightType': 'field',
|
||||||
|
'nextConditionOperator': '',
|
||||||
|
'nextCondition': null
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'params': {
|
||||||
|
'existingColspan': 1,
|
||||||
|
'maxColspan': 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
@@ -14,24 +14,6 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*!
|
|
||||||
* @license
|
|
||||||
* Copyright 2019 Alfresco Software, Ltd.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the 'License');
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an 'AS IS' BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import { FormModel, FormValues } from '../../form/components/widgets/core/index';
|
import { FormModel, FormValues } from '../../form/components/widgets/core/index';
|
||||||
|
|
||||||
export let formTest = new FormModel({});
|
export let formTest = new FormModel({});
|
||||||
|
Reference in New Issue
Block a user