mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[ADF-4710] - adapt visibility condition to the new schema (#4884)
* [ADF-4710] - adapt new visibility condition schema * [ADF-4710] - fix unit tests * [ADF-4710] - support visibility for APS1 and APS2
This commit is contained in:
committed by
Eugenio Romano
parent
ffd4413335
commit
72b3a75a85
@@ -136,7 +136,6 @@ export class FormFieldModel extends FormWidgetModel {
|
||||
|
||||
constructor(form: FormModel, json?: any) {
|
||||
super(form, json);
|
||||
|
||||
if (json) {
|
||||
this.fieldType = json.fieldType;
|
||||
this.id = json.id;
|
||||
@@ -163,7 +162,7 @@ export class FormFieldModel extends FormWidgetModel {
|
||||
this.params = <FormFieldMetadata> json.params || {};
|
||||
this.hyperlinkUrl = json.hyperlinkUrl;
|
||||
this.displayText = json.displayText;
|
||||
this.visibilityCondition = <WidgetVisibilityModel> json.visibilityCondition;
|
||||
this.visibilityCondition = json.visibilityCondition ? new WidgetVisibilityModel(json.visibilityCondition) : undefined;
|
||||
this.enableFractions = <boolean> json.enableFractions;
|
||||
this.currency = json.currency;
|
||||
this.dateDisplayFormat = json.dateDisplayFormat || this.getDefaultDateFormat(json);
|
||||
|
@@ -16,13 +16,90 @@
|
||||
*/
|
||||
|
||||
export class WidgetVisibilityModel {
|
||||
leftFormFieldId: string;
|
||||
leftRestResponseId: string;
|
||||
rightRestResponseId?: string;
|
||||
rightFormFieldId?: string;
|
||||
leftRestResponseId?: string;
|
||||
leftFormFieldId?: string;
|
||||
operator: string;
|
||||
nextCondition: WidgetVisibilityModel;
|
||||
nextConditionOperator: string;
|
||||
operator: string;
|
||||
rightFormFieldId: string;
|
||||
rightRestResponseId: string;
|
||||
rightType: string;
|
||||
rightValue: string;
|
||||
|
||||
constructor(private json?: any) {
|
||||
if (json) {
|
||||
this.operator = json.operator;
|
||||
this.nextCondition = new WidgetVisibilityModel(json.nextCondition);
|
||||
this.nextConditionOperator = json.nextConditionOperator;
|
||||
this.rightRestResponseId = json.rightRestResponseId;
|
||||
this.rightFormFieldId = json.rightFormFieldId;
|
||||
this.leftFormFieldId = json.leftFormFieldId;
|
||||
this.leftRestResponseId = json.leftRestResponseId;
|
||||
} else {
|
||||
this.json = {};
|
||||
}
|
||||
}
|
||||
|
||||
set leftType(leftType: string) {
|
||||
this.json.leftType = leftType;
|
||||
}
|
||||
|
||||
set rightType(rightType: string) {
|
||||
this.json.rightType = rightType;
|
||||
}
|
||||
|
||||
set leftValue(leftValue: string) {
|
||||
this.json.leftValue = leftValue;
|
||||
}
|
||||
|
||||
set rightValue(rightValue: string) {
|
||||
this.json.rightValue = rightValue;
|
||||
}
|
||||
|
||||
get leftType() {
|
||||
if (this.leftFormFieldId) {
|
||||
return WidgetTypeEnum.field;
|
||||
} else if (this.leftRestResponseId) {
|
||||
return WidgetTypeEnum.variable;
|
||||
} else if ( !!this.json.leftType) {
|
||||
return this.json.leftType;
|
||||
}
|
||||
}
|
||||
|
||||
get leftValue() {
|
||||
if ( this.json.leftValue ) {
|
||||
return this.json.leftValue;
|
||||
} else if (this.leftFormFieldId) {
|
||||
return this.leftFormFieldId;
|
||||
} else {
|
||||
return this.leftRestResponseId;
|
||||
}
|
||||
}
|
||||
|
||||
get rightType() {
|
||||
if ( !!this.json.rightType ) {
|
||||
return this.json.rightType;
|
||||
} else if (this.json.rightValue) {
|
||||
return WidgetTypeEnum.value;
|
||||
} else if (this.rightRestResponseId) {
|
||||
return WidgetTypeEnum.variable;
|
||||
} else if (this.rightFormFieldId) {
|
||||
return WidgetTypeEnum.field;
|
||||
}
|
||||
}
|
||||
|
||||
get rightValue() {
|
||||
if (this.json.rightValue) {
|
||||
return this.json.rightValue;
|
||||
} else if (this.rightFormFieldId) {
|
||||
return this.rightFormFieldId;
|
||||
} else {
|
||||
return this.rightRestResponseId;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export enum WidgetTypeEnum {
|
||||
field = 'field',
|
||||
variable = 'variable',
|
||||
value = 'value'
|
||||
}
|
||||
|
1029
lib/core/form/services/widget-visibility-cloud.service.spec.ts
Normal file
1029
lib/core/form/services/widget-visibility-cloud.service.spec.ts
Normal file
File diff suppressed because it is too large
Load Diff
@@ -25,20 +25,16 @@ import {
|
||||
} from './../components/widgets/core/index';
|
||||
import { TaskProcessVariableModel } from './../models/task-process-variable.model';
|
||||
import { WidgetVisibilityModel } from './../models/widget-visibility.model';
|
||||
import {
|
||||
fakeFormJson,
|
||||
complexVisibilityJsonVisible,
|
||||
complexVisibilityJsonNotVisible,
|
||||
fakeTaskProcessVariableModels,
|
||||
formTest,
|
||||
formValues
|
||||
} from '../../mock';
|
||||
import { WidgetVisibilityService } from './widget-visibility.service';
|
||||
import { setupTestBed } from '../../testing/setupTestBed';
|
||||
import { CoreModule } from '../../core.module';
|
||||
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
|
||||
import { AlfrescoApiService } from '../../services/alfresco-api.service';
|
||||
import { AlfrescoApiServiceMock } from '../../mock/alfresco-api.service.mock';
|
||||
import { fakeTaskProcessVariableModels,
|
||||
fakeFormJson, formTest,
|
||||
formValues, complexVisibilityJsonVisible,
|
||||
complexVisibilityJsonNotVisible } from 'core/mock/form/widget-visibility.service.mock';
|
||||
|
||||
declare let jasmine: any;
|
||||
|
||||
@@ -196,10 +192,10 @@ describe('WidgetVisibilityService', () => {
|
||||
describe('should retrieve the process variables', () => {
|
||||
const fakeFormWithField = new FormModel(fakeFormJson);
|
||||
let visibilityObjTest: WidgetVisibilityModel;
|
||||
const chainedVisibilityObj = new WidgetVisibilityModel();
|
||||
const chainedVisibilityObj = new WidgetVisibilityModel({});
|
||||
|
||||
beforeEach(() => {
|
||||
visibilityObjTest = new WidgetVisibilityModel();
|
||||
visibilityObjTest = new WidgetVisibilityModel({});
|
||||
});
|
||||
|
||||
it('should return the process variables for task', (done) => {
|
||||
@@ -294,7 +290,7 @@ describe('WidgetVisibilityService', () => {
|
||||
visibilityObjTest.leftFormFieldId = 'LEFT_FORM_FIELD_ID';
|
||||
visibilityObjTest.operator = '!=';
|
||||
visibilityObjTest.rightRestResponseId = 'TEST_VAR_2';
|
||||
const isVisible = service.isFieldVisible(fakeFormWithField, visibilityObjTest);
|
||||
const isVisible = service.isFieldVisible(fakeFormWithField, new WidgetVisibilityModel(visibilityObjTest));
|
||||
|
||||
expect(isVisible).toBeTruthy();
|
||||
done();
|
||||
@@ -459,7 +455,7 @@ describe('WidgetVisibilityService', () => {
|
||||
expect(leftValue).toBe('value_2');
|
||||
});
|
||||
|
||||
it('should return an empty string for a value that is not on variable or form', () => {
|
||||
it('should return empty string for a value that is not on variable or form', () => {
|
||||
const leftValue = service.getLeftValue(fakeFormWithField, visibilityObjTest);
|
||||
|
||||
expect(leftValue).toBe('');
|
||||
@@ -943,9 +939,8 @@ describe('WidgetVisibilityService', () => {
|
||||
});
|
||||
|
||||
it('should be able to analyze a complex visibility JSON false', () => {
|
||||
const isVisible = service.isFieldVisible(complexVisibilityJsonNotVisibleModel,
|
||||
complexVisibilityJsonNotVisible.formDefinition.fields[2].fields[2][0].visibilityCondition);
|
||||
|
||||
const formField = new FormFieldModel(complexVisibilityJsonNotVisibleModel, complexVisibilityJsonNotVisible.formDefinition.fields[2].fields[2][0]);
|
||||
const isVisible = service.isFieldVisible(complexVisibilityJsonNotVisibleModel, new WidgetVisibilityModel(formField.visibilityCondition));
|
||||
expect(isVisible).toBe(false);
|
||||
});
|
||||
|
||||
|
@@ -22,7 +22,7 @@ import moment from 'moment-es6';
|
||||
import { Observable, from, throwError } from 'rxjs';
|
||||
import { FormFieldModel, FormModel, TabModel } from '../components/widgets/core/index';
|
||||
import { TaskProcessVariableModel } from '../models/task-process-variable.model';
|
||||
import { WidgetVisibilityModel } from '../models/widget-visibility.model';
|
||||
import { WidgetVisibilityModel, WidgetTypeEnum } from '../models/widget-visibility.model';
|
||||
import { map, catchError } from 'rxjs/operators';
|
||||
|
||||
@Injectable({
|
||||
@@ -52,7 +52,7 @@ export class WidgetVisibilityService {
|
||||
}
|
||||
|
||||
evaluateVisibility(form: FormModel, visibilityObj: WidgetVisibilityModel): boolean {
|
||||
const isLeftFieldPresent = visibilityObj && (visibilityObj.leftFormFieldId || visibilityObj.leftRestResponseId);
|
||||
const isLeftFieldPresent = visibilityObj && (visibilityObj.leftType || visibilityObj.leftValue);
|
||||
if (!isLeftFieldPresent || isLeftFieldPresent === 'null') {
|
||||
return true;
|
||||
} else {
|
||||
@@ -64,43 +64,43 @@ export class WidgetVisibilityService {
|
||||
const leftValue = this.getLeftValue(form, visibilityObj);
|
||||
const rightValue = this.getRightValue(form, visibilityObj);
|
||||
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;
|
||||
result = accumulator[0].value ? accumulator[0].value : result;
|
||||
|
||||
for (let i = 1; i < accumulator.length; i++) {
|
||||
result = this.evaluateLogicalOperation(
|
||||
accumulator[i - 1].operator,
|
||||
result,
|
||||
accumulator[i].value
|
||||
);
|
||||
if (accumulator[i - 1].operator && accumulator[i].value) {
|
||||
result = this.evaluateLogicalOperation(
|
||||
accumulator[i - 1].operator,
|
||||
result,
|
||||
accumulator[i].value
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
return !!result;
|
||||
|
||||
}
|
||||
|
||||
getLeftValue(form: FormModel, visibilityObj: WidgetVisibilityModel): string {
|
||||
let leftValue = '';
|
||||
if (visibilityObj.leftRestResponseId && visibilityObj.leftRestResponseId !== 'null') {
|
||||
leftValue = this.getVariableValue(form, visibilityObj.leftRestResponseId, this.processVarList);
|
||||
} else if (visibilityObj.leftFormFieldId) {
|
||||
leftValue = this.getFormValue(form, visibilityObj.leftFormFieldId);
|
||||
leftValue = leftValue ? leftValue : this.getVariableValue(form, visibilityObj.leftFormFieldId, this.processVarList);
|
||||
if (visibilityObj.leftType && visibilityObj.leftType === WidgetTypeEnum.variable) {
|
||||
leftValue = this.getVariableValue(form, visibilityObj.leftValue, this.processVarList);
|
||||
} else if (visibilityObj.leftType && visibilityObj.leftType === WidgetTypeEnum.field) {
|
||||
leftValue = this.getFormValue(form, visibilityObj.leftValue);
|
||||
leftValue = leftValue ? leftValue : this.getVariableValue(form, visibilityObj.leftValue, this.processVarList);
|
||||
}
|
||||
return leftValue;
|
||||
}
|
||||
|
||||
getRightValue(form: FormModel, visibilityObj: WidgetVisibilityModel): string {
|
||||
let valueFound = '';
|
||||
if (visibilityObj.rightRestResponseId) {
|
||||
valueFound = this.getVariableValue(form, visibilityObj.rightRestResponseId, this.processVarList);
|
||||
} else if (visibilityObj.rightFormFieldId) {
|
||||
valueFound = this.getFormValue(form, visibilityObj.rightFormFieldId);
|
||||
if (visibilityObj.rightType === WidgetTypeEnum.variable) {
|
||||
valueFound = this.getVariableValue(form, visibilityObj.rightValue, this.processVarList);
|
||||
} else if (visibilityObj.rightType === WidgetTypeEnum.field) {
|
||||
valueFound = this.getFormValue(form, visibilityObj.rightValue);
|
||||
} else {
|
||||
if (moment(visibilityObj.rightValue, 'YYYY-MM-DD', true).isValid()) {
|
||||
valueFound = visibilityObj.rightValue + 'T00:00:00.000Z';
|
||||
@@ -117,7 +117,6 @@ export class WidgetVisibilityService {
|
||||
if (!value) {
|
||||
value = this.searchValueInForm(form, fieldId);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -178,16 +177,7 @@ export class WidgetVisibilityService {
|
||||
}
|
||||
|
||||
private isSearchedField(field: FormFieldModel, fieldToFind: string): boolean {
|
||||
const formattedFieldName = this.removeLabel(field, fieldToFind);
|
||||
return field.id ? field.id.toUpperCase() === formattedFieldName.toUpperCase() : false;
|
||||
}
|
||||
|
||||
private removeLabel(field: FormFieldModel, fieldToFind): string {
|
||||
let formattedFieldName = fieldToFind || '';
|
||||
if (field.fieldType === 'RestFieldRepresentation' && fieldToFind.indexOf('_LABEL') > 0) {
|
||||
formattedFieldName = fieldToFind.substring(0, fieldToFind.length - 6);
|
||||
}
|
||||
return formattedFieldName;
|
||||
return (field.id && fieldToFind) ? field.id.toUpperCase() === fieldToFind.toUpperCase() : false;
|
||||
}
|
||||
|
||||
getVariableValue(form: FormModel, name: string, processVarList: TaskProcessVariableModel[]): string {
|
||||
|
Reference in New Issue
Block a user