mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-31 17:38:48 +00:00
Merge pull request #868 from Alfresco/dev-valbano-856
Added fix for visibility on dropdown
This commit is contained in:
@@ -34,7 +34,7 @@ describe('ActivitiForm', () => {
|
||||
'upgradeAllRegistered'
|
||||
]);
|
||||
visibilityService = jasmine.createSpyObj('WidgetVisibilityService', [
|
||||
'updateVisibilityForForm', 'getTaskProcessVariableModelsForTask'
|
||||
'refreshVisibility', 'getTaskProcessVariable'
|
||||
]);
|
||||
window['componentHandler'] = componentHandler;
|
||||
|
||||
@@ -143,7 +143,7 @@ describe('ActivitiForm', () => {
|
||||
formComponent.loadForm();
|
||||
|
||||
expect(formComponent.getFormByTaskId).toHaveBeenCalledWith(taskId);
|
||||
expect(visibilityService.getTaskProcessVariableModelsForTask).toHaveBeenCalledWith(taskId);
|
||||
expect(visibilityService.getTaskProcessVariable).toHaveBeenCalledWith(taskId);
|
||||
});
|
||||
|
||||
it('should get form definition by form id on load', () => {
|
||||
@@ -623,15 +623,15 @@ describe('ActivitiForm', () => {
|
||||
it('should check visibility only if field with form provided', () => {
|
||||
|
||||
formComponent.checkVisibility(null);
|
||||
expect(visibilityService.updateVisibilityForForm).not.toHaveBeenCalled();
|
||||
expect(visibilityService.refreshVisibility).not.toHaveBeenCalled();
|
||||
|
||||
let field = new FormFieldModel(null);
|
||||
formComponent.checkVisibility(field);
|
||||
expect(visibilityService.updateVisibilityForForm).not.toHaveBeenCalled();
|
||||
expect(visibilityService.refreshVisibility).not.toHaveBeenCalled();
|
||||
|
||||
field = new FormFieldModel(new FormModel());
|
||||
formComponent.checkVisibility(field);
|
||||
expect(visibilityService.updateVisibilityForForm).toHaveBeenCalledWith(field.form);
|
||||
expect(visibilityService.refreshVisibility).toHaveBeenCalledWith(field.form);
|
||||
});
|
||||
|
||||
});
|
||||
|
@@ -277,7 +277,7 @@ export class ActivitiForm implements OnInit, AfterViewChecked, OnChanges {
|
||||
loadForm() {
|
||||
if (this.taskId) {
|
||||
this.getFormByTaskId(this.taskId);
|
||||
this.visibilityService.getTaskProcessVariableModelsForTask(this.taskId);
|
||||
this.visibilityService.getTaskProcessVariable(this.taskId);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -414,7 +414,7 @@ export class ActivitiForm implements OnInit, AfterViewChecked, OnChanges {
|
||||
|
||||
checkVisibility(field: FormFieldModel) {
|
||||
if (field && field.form) {
|
||||
this.visibilityService.updateVisibilityForForm(field.form);
|
||||
this.visibilityService.refreshVisibility(field.form);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,97 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 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 '../../components/widgets/core/index';
|
||||
|
||||
export var formTest = new FormModel({});
|
||||
|
||||
export var fakeTaskProcessVariableModels = [
|
||||
{id: 'TEST_VAR_1', type: 'string', value: 'test_value_1'},
|
||||
{id: 'TEST_VAR_2', type: 'string', value: 'test_value_2'},
|
||||
{id: 'TEST_VAR_3', type: 'string', value: 'test_value_3'}
|
||||
];
|
||||
|
||||
export var formValues: FormValues = {
|
||||
'test_1': 'value_1',
|
||||
'test_2': 'value_2',
|
||||
'test_3': 'value_1',
|
||||
'test_4': 'dropdown_id',
|
||||
'test_5': 'dropdown_label',
|
||||
'dropdown': {'id': 'dropdown_id', 'name': 'dropdown_label'}
|
||||
};
|
||||
|
||||
export var fakeFormJson = {
|
||||
id: '9999',
|
||||
name: 'FORM_VISIBILITY',
|
||||
processDefinitionId: 'PROCESS_TEST:9:9999',
|
||||
processDefinitionName: 'PROCESS_TEST',
|
||||
processDefinitionKey: 'PROCESS_TEST',
|
||||
taskId: '999',
|
||||
taskName: 'TEST',
|
||||
fields: [
|
||||
{
|
||||
fieldType: 'ContainerRepresentation',
|
||||
id: '000000000000000000',
|
||||
name: 'Label',
|
||||
type: 'container',
|
||||
value: null,
|
||||
numberOfColumns: 2,
|
||||
fields: {
|
||||
1: [
|
||||
{
|
||||
fieldType: 'FormFieldRepresentation',
|
||||
id: 'FIELD_TEST',
|
||||
name: 'FIELD_TEST',
|
||||
type: 'text',
|
||||
value: 'RIGHT_FORM_FIELD_VALUE',
|
||||
visibilityCondition: null,
|
||||
isVisible: true
|
||||
},
|
||||
{
|
||||
fieldType: 'FormFieldRepresentation',
|
||||
id: 'FIELD_WITH_CONDITION',
|
||||
name: 'FIELD_WITH_CONDITION',
|
||||
type: 'text',
|
||||
value: 'field_with_condition_value',
|
||||
visibilityCondition: null,
|
||||
isVisible: true
|
||||
},
|
||||
{
|
||||
fieldType: 'FormFieldRepresentation',
|
||||
id: 'LEFT_FORM_FIELD_ID',
|
||||
name: 'LEFT_FORM_FIELD_NAME',
|
||||
type: 'text',
|
||||
value: 'LEFT_FORM_FIELD_VALUE',
|
||||
visibilityCondition: null,
|
||||
isVisible: true
|
||||
}
|
||||
],
|
||||
2: [
|
||||
{
|
||||
fieldType: 'FormFieldRepresentation',
|
||||
id: 'RIGHT_FORM_FIELD_ID',
|
||||
name: 'RIGHT_FORM_FIELD_NAME',
|
||||
type: 'text',
|
||||
value: 'RIGHT_FORM_FIELD_VALUE',
|
||||
visibilityCondition: null,
|
||||
isVisible: true
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
File diff suppressed because it is too large
Load Diff
@@ -18,7 +18,7 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Response, Http, Headers, RequestOptions } from '@angular/http';
|
||||
import { Observable } from 'rxjs/Rx';
|
||||
import { AlfrescoSettingsService } from 'ng2-alfresco-core';
|
||||
import { AlfrescoSettingsService, AlfrescoAuthenticationService } from 'ng2-alfresco-core';
|
||||
import { FormModel, FormFieldModel, TabModel } from '../components/widgets/core/index';
|
||||
import { WidgetVisibilityModel } from '../models/widget-visibility.model';
|
||||
import { TaskProcessVariableModel } from '../models/task-process-variable.model';
|
||||
@@ -29,129 +29,136 @@ export class WidgetVisibilityService {
|
||||
private processVarList: TaskProcessVariableModel[];
|
||||
|
||||
constructor(private http: Http,
|
||||
private alfrescoSettingsService: AlfrescoSettingsService) {
|
||||
}
|
||||
|
||||
public updateVisibilityForForm(form: FormModel) {
|
||||
if ( form && form.tabs && form.tabs.length > 0) {
|
||||
form.tabs.map( tabModel => this.refreshVisibilityForTab(tabModel) );
|
||||
}
|
||||
if ( form && form.fields.length > 0 ) {
|
||||
form.fields
|
||||
.map(
|
||||
contModel =>
|
||||
contModel.columns
|
||||
.map(
|
||||
contColModel =>
|
||||
contColModel
|
||||
.fields.map(
|
||||
field =>
|
||||
this.refreshVisibilityForField(field) )
|
||||
)
|
||||
);
|
||||
}
|
||||
private alfrescoSettingsService: AlfrescoSettingsService,
|
||||
private authService: AlfrescoAuthenticationService) {
|
||||
}
|
||||
|
||||
public refreshVisibilityForField(field: FormFieldModel) {
|
||||
if ( field.visibilityCondition ) {
|
||||
field.isVisible = this.getVisiblityForField(field.form, field.visibilityCondition);
|
||||
public refreshVisibility(form: FormModel) {
|
||||
if (form && form.tabs && form.tabs.length > 0) {
|
||||
form.tabs.map(tabModel => this.refreshTabVisibility(tabModel));
|
||||
}
|
||||
if (form && form.fields.length > 0) {
|
||||
form.fields.map(contModel =>
|
||||
contModel.columns.map(contColModel =>
|
||||
contColModel.fields.map(field => this.refreshFieldVisibility(field))));
|
||||
}
|
||||
}
|
||||
|
||||
public refreshVisibilityForTab(tab: TabModel) {
|
||||
if ( tab.visibilityCondition ) {
|
||||
tab.isVisible = this.getVisiblityForField(tab.form, tab.visibilityCondition);
|
||||
refreshFieldVisibility(field: FormFieldModel) {
|
||||
if (field.visibilityCondition) {
|
||||
field.isVisible = this.evaluateVisibility(field.form, field.visibilityCondition);
|
||||
}
|
||||
}
|
||||
|
||||
public getVisiblityForField(form: FormModel, visibilityObj: WidgetVisibilityModel): boolean {
|
||||
refreshTabVisibility(tab: TabModel) {
|
||||
if (tab.visibilityCondition) {
|
||||
tab.isVisible = this.evaluateVisibility(tab.form, tab.visibilityCondition);
|
||||
}
|
||||
}
|
||||
|
||||
evaluateVisibility(form: FormModel, visibilityObj: WidgetVisibilityModel): boolean {
|
||||
let isLeftFieldPresent = visibilityObj.leftFormFieldId || visibilityObj.leftRestResponseId;
|
||||
if ( !isLeftFieldPresent || isLeftFieldPresent === 'null' ) {
|
||||
if (!isLeftFieldPresent || isLeftFieldPresent === 'null') {
|
||||
return true;
|
||||
}else {
|
||||
return this.evaluateVisibilityForField(form, visibilityObj);
|
||||
} else {
|
||||
return this.isFieldVisible(form, visibilityObj);
|
||||
}
|
||||
}
|
||||
|
||||
evaluateVisibilityForField(form: FormModel, visibilityObj: WidgetVisibilityModel): boolean {
|
||||
let leftValue = this.getLeftValue(form, visibilityObj);
|
||||
let rightValue = this.getRightValue(form, visibilityObj);
|
||||
let actualResult = this.evaluateCondition(leftValue, rightValue, visibilityObj.operator);
|
||||
if ( visibilityObj.nextCondition ) {
|
||||
return this.evaluateLogicalOperation(visibilityObj.nextConditionOperator,
|
||||
actualResult,
|
||||
this.evaluateVisibilityForField(
|
||||
form, visibilityObj.nextCondition)
|
||||
);
|
||||
}else {
|
||||
isFieldVisible(form: FormModel, visibilityObj: WidgetVisibilityModel): boolean {
|
||||
let leftValue = this.getLeftValue(form, visibilityObj);
|
||||
let rightValue = this.getRightValue(form, visibilityObj);
|
||||
let actualResult = this.evaluateCondition(leftValue, rightValue, visibilityObj.operator);
|
||||
if (visibilityObj.nextCondition) {
|
||||
return this.evaluateLogicalOperation(
|
||||
visibilityObj.nextConditionOperator,
|
||||
actualResult,
|
||||
this.isFieldVisible(form, visibilityObj.nextCondition)
|
||||
);
|
||||
} else {
|
||||
return actualResult;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getLeftValue(form: FormModel, visibilityObj: WidgetVisibilityModel) {
|
||||
if ( visibilityObj.leftRestResponseId ) {
|
||||
return this.getValueFromVariable(form, visibilityObj.leftRestResponseId, this.processVarList);
|
||||
if (visibilityObj.leftRestResponseId) {
|
||||
return this.getVariableValue(form, visibilityObj.leftRestResponseId, this.processVarList);
|
||||
}
|
||||
return this.getValueOField(form, visibilityObj.leftFormFieldId);
|
||||
return this.getFormValue(form, visibilityObj.leftFormFieldId);
|
||||
}
|
||||
|
||||
getRightValue(form: FormModel, visibilityObj: WidgetVisibilityModel) {
|
||||
let valueFound = null;
|
||||
if ( visibilityObj.rightRestResponseId ) {
|
||||
valueFound = this.getValueFromVariable(form, visibilityObj.rightRestResponseId, this.processVarList);
|
||||
}else if ( visibilityObj.rightFormFieldId ) {
|
||||
valueFound = this.getValueOField(form, visibilityObj.rightFormFieldId);
|
||||
}else {
|
||||
valueFound = visibilityObj.rightValue;
|
||||
if (visibilityObj.rightRestResponseId) {
|
||||
valueFound = this.getVariableValue(form, visibilityObj.rightRestResponseId, this.processVarList);
|
||||
} else if (visibilityObj.rightFormFieldId) {
|
||||
valueFound = this.getFormValue(form, visibilityObj.rightFormFieldId);
|
||||
} else {
|
||||
valueFound = visibilityObj.rightValue;
|
||||
}
|
||||
return valueFound;
|
||||
}
|
||||
|
||||
getValueOField(form: FormModel, field: string) {
|
||||
let value = form.values[field] ?
|
||||
form.values[field] :
|
||||
this.getFormValueByName(form, field);
|
||||
return value;
|
||||
getFormValue(form: FormModel, field: string) {
|
||||
let value = this.getValue(form.values, field);
|
||||
value = value && value.id ? value.id : value;
|
||||
return value ? value : this.searchForm(form, field);
|
||||
}
|
||||
|
||||
getFormValueByName(form: FormModel, name: string) {
|
||||
for (let columns of form.json.fields) {
|
||||
for ( let i in columns.fields ) {
|
||||
if ( columns.fields.hasOwnProperty( i ) ) {
|
||||
let res = columns.fields[i].find(field => field.id === name);
|
||||
if ( res ) {
|
||||
return res.value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
getValue(values: any, fieldName: string) {
|
||||
return this.getFieldValue(values, fieldName) ||
|
||||
this.getDropDownName(values, fieldName);
|
||||
}
|
||||
|
||||
getValueFromVariable( form: FormModel, name: string, processVarList: TaskProcessVariableModel[] ) {
|
||||
getFieldValue(valueList: any, fieldName: string) {
|
||||
return fieldName ? valueList[fieldName] : fieldName;
|
||||
}
|
||||
|
||||
getDropDownName(valueList: any, fieldName: string) {
|
||||
let dropDownFilterByName;
|
||||
if (fieldName && fieldName.indexOf('_LABEL') > 0) {
|
||||
dropDownFilterByName = fieldName.substring(0, fieldName.length - 6);
|
||||
}
|
||||
return ( dropDownFilterByName && valueList[dropDownFilterByName] ) ?
|
||||
valueList[dropDownFilterByName].name : dropDownFilterByName;
|
||||
}
|
||||
|
||||
searchForm(form: FormModel, name: string) {
|
||||
let res;
|
||||
form.json.fields.forEach(columns => {
|
||||
for (let i in columns.fields) {
|
||||
if (columns.fields.hasOwnProperty(i)) {
|
||||
res = columns.fields[i].find(field => field.id === name);
|
||||
if (res) {
|
||||
return res.value;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
return res ? res.value : res;
|
||||
}
|
||||
|
||||
getVariableValue(form: FormModel, name: string, processVarList: TaskProcessVariableModel[]) {
|
||||
return this.getFormVariableValue(form, name) ||
|
||||
this.getProcessVariableValue(name, processVarList);
|
||||
this.getProcessVariableValue(name, processVarList);
|
||||
}
|
||||
|
||||
private getFormVariableValue(form: FormModel, name: string ) {
|
||||
if ( form.json.variables) {
|
||||
let variableFromForm = form.json.variables.find(formVar => formVar.name === name);
|
||||
if ( variableFromForm ) {
|
||||
return variableFromForm ? variableFromForm.value : variableFromForm;
|
||||
}
|
||||
private getFormVariableValue(form: FormModel, name: string) {
|
||||
if (form.json.variables) {
|
||||
let formVariable = form.json.variables.find(formVar => formVar.name === name);
|
||||
return formVariable ? formVariable.value : formVariable;
|
||||
}
|
||||
}
|
||||
|
||||
private getProcessVariableValue(name: string, processVarList: TaskProcessVariableModel[]) {
|
||||
if ( this.processVarList ) {
|
||||
let variableFromProcess = this.processVarList.find(variable => variable.id === name);
|
||||
if ( variableFromProcess ) {
|
||||
return variableFromProcess ? variableFromProcess.value : variableFromProcess;
|
||||
}
|
||||
if (this.processVarList) {
|
||||
let processVariable = this.processVarList.find(variable => variable.id === name);
|
||||
return processVariable ? processVariable.value : processVariable;
|
||||
}
|
||||
}
|
||||
|
||||
evaluateLogicalOperation(logicOp, previousValue, newValue): boolean {
|
||||
switch ( logicOp ) {
|
||||
switch (logicOp) {
|
||||
case 'and':
|
||||
return previousValue && newValue;
|
||||
case 'or' :
|
||||
@@ -161,19 +168,19 @@ export class WidgetVisibilityService {
|
||||
case 'or-not':
|
||||
return previousValue || !newValue;
|
||||
default:
|
||||
console.error( 'NO valid operation! wrong op request : ' + logicOp );
|
||||
console.error('NO valid operation! wrong op request : ' + logicOp);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
evaluateCondition(leftValue, rightValue, operator): boolean {
|
||||
switch ( operator ) {
|
||||
switch (operator) {
|
||||
case '==':
|
||||
return String(leftValue) === String(rightValue);
|
||||
return leftValue + '' === rightValue + '';
|
||||
case '<':
|
||||
return leftValue < rightValue;
|
||||
case '!=':
|
||||
return String(leftValue) !== String(rightValue);
|
||||
return leftValue + '' !== rightValue + '';
|
||||
case '>':
|
||||
return leftValue > rightValue;
|
||||
case '>=':
|
||||
@@ -185,32 +192,26 @@ export class WidgetVisibilityService {
|
||||
case '!empty':
|
||||
return leftValue ? leftValue !== '' : false;
|
||||
default:
|
||||
console.error( 'NO valid operation!' );
|
||||
console.error('NO valid operation!');
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
getTaskProcessVariableModelsForTask(taskId: string): Observable<TaskProcessVariableModel[]> {
|
||||
getTaskProcessVariable(taskId: string): Observable<TaskProcessVariableModel[]> {
|
||||
let url = `${this.alfrescoSettingsService.getBPMApiBaseUrl()}/app/rest/task-forms/${taskId}/variables`;
|
||||
let options = this.getRequestOptions();
|
||||
return this.http
|
||||
.get(url, options)
|
||||
.map( (response: Response) => this.processVarList = <TaskProcessVariableModel[]> response.json())
|
||||
.map((response: Response) => this.processVarList = <TaskProcessVariableModel[]> response.json())
|
||||
.catch(this.handleError);
|
||||
}
|
||||
|
||||
getTaskProcessVariableModelForTaskByName(taskId: string, processVarName: string): Observable<TaskProcessVariableModel> {
|
||||
return this.getTaskProcessVariableModelsForTask(taskId)
|
||||
.map(
|
||||
(variables: TaskProcessVariableModel[]) =>
|
||||
variables.find(variable => variable.id === processVarName));
|
||||
}
|
||||
|
||||
private getHeaders(): Headers {
|
||||
return new Headers({
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json'
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': this.authService.getTicketBpm()
|
||||
});
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user