mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-05-19 17:14:57 +00:00
Added fix for visibility on dropdown
This commit is contained in:
parent
8b1b5e9926
commit
82632e5e89
File diff suppressed because it is too large
Load Diff
@ -15,13 +15,13 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
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 { FormModel, FormFieldModel, TabModel } from '../components/widgets/core/index';
|
||||
import { WidgetVisibilityModel } from '../models/widget-visibility.model';
|
||||
import { TaskProcessVariableModel } from '../models/task-process-variable.model';
|
||||
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 {FormModel, FormFieldModel, TabModel} from '../components/widgets/core/index';
|
||||
import {WidgetVisibilityModel} from '../models/widget-visibility.model';
|
||||
import {TaskProcessVariableModel} from '../models/task-process-variable.model';
|
||||
|
||||
@Injectable()
|
||||
export class WidgetVisibilityService {
|
||||
@ -30,66 +30,66 @@ export class WidgetVisibilityService {
|
||||
|
||||
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) )
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public refreshVisibilityForField(field: FormFieldModel) {
|
||||
if ( field.visibilityCondition ) {
|
||||
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))
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
refreshVisibilityForField(field: FormFieldModel) {
|
||||
if (field.visibilityCondition) {
|
||||
field.isVisible = this.getVisiblityForField(field.form, field.visibilityCondition);
|
||||
}
|
||||
}
|
||||
|
||||
public refreshVisibilityForTab(tab: TabModel) {
|
||||
if ( tab.visibilityCondition ) {
|
||||
refreshVisibilityForTab(tab: TabModel) {
|
||||
if (tab.visibilityCondition) {
|
||||
tab.isVisible = this.getVisiblityForField(tab.form, tab.visibilityCondition);
|
||||
}
|
||||
}
|
||||
|
||||
public getVisiblityForField(form: FormModel, visibilityObj: WidgetVisibilityModel): boolean {
|
||||
getVisiblityForField(form: FormModel, visibilityObj: WidgetVisibilityModel): boolean {
|
||||
let isLeftFieldPresent = visibilityObj.leftFormFieldId || visibilityObj.leftRestResponseId;
|
||||
if ( !isLeftFieldPresent || isLeftFieldPresent === 'null' ) {
|
||||
if (!isLeftFieldPresent || isLeftFieldPresent === 'null') {
|
||||
return true;
|
||||
}else {
|
||||
} else {
|
||||
return this.evaluateVisibilityForField(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 {
|
||||
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 {
|
||||
return actualResult;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getLeftValue(form: FormModel, visibilityObj: WidgetVisibilityModel) {
|
||||
if ( visibilityObj.leftRestResponseId ) {
|
||||
if (visibilityObj.leftRestResponseId) {
|
||||
return this.getValueFromVariable(form, visibilityObj.leftRestResponseId, this.processVarList);
|
||||
}
|
||||
return this.getValueOField(form, visibilityObj.leftFormFieldId);
|
||||
@ -97,61 +97,78 @@ export class WidgetVisibilityService {
|
||||
|
||||
getRightValue(form: FormModel, visibilityObj: WidgetVisibilityModel) {
|
||||
let valueFound = null;
|
||||
if ( visibilityObj.rightRestResponseId ) {
|
||||
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;
|
||||
} else if (visibilityObj.rightFormFieldId) {
|
||||
valueFound = this.getValueOField(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;
|
||||
let value = this.getValueFromFormValues(form.values, field);
|
||||
value = value && value.id ? value.id : value;
|
||||
return value ? value : this.getFormValueByName(form, field);
|
||||
}
|
||||
|
||||
getValueFromFormValues(values: any, fieldName: string) {
|
||||
return this.getFieldValue(values, fieldName) ||
|
||||
this.getDropDownValueForLabel(values, fieldName);
|
||||
}
|
||||
|
||||
getFieldValue(valueList: any, fieldName: string) {
|
||||
return fieldName ? valueList[fieldName] : fieldName;
|
||||
}
|
||||
|
||||
getDropDownValueForLabel(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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getValueFromVariable( form: FormModel, name: string, processVarList: TaskProcessVariableModel[] ) {
|
||||
getValueFromVariable(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 variableFromForm = form.json.variables.find(formVar => formVar.name === name);
|
||||
if (variableFromForm) {
|
||||
return variableFromForm ? variableFromForm.value : variableFromForm;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 variableFromProcess = this.processVarList.find(variable => variable.id === name);
|
||||
if (variableFromProcess) {
|
||||
return variableFromProcess ? variableFromProcess.value : variableFromProcess;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
evaluateLogicalOperation(logicOp, previousValue, newValue): boolean {
|
||||
switch ( logicOp ) {
|
||||
switch (logicOp) {
|
||||
case 'and':
|
||||
return previousValue && newValue;
|
||||
case 'or' :
|
||||
@ -161,13 +178,13 @@ 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);
|
||||
case '<':
|
||||
@ -185,7 +202,7 @@ export class WidgetVisibilityService {
|
||||
case '!empty':
|
||||
return leftValue ? leftValue !== '' : false;
|
||||
default:
|
||||
console.error( 'NO valid operation!' );
|
||||
console.error('NO valid operation!');
|
||||
break;
|
||||
}
|
||||
return;
|
||||
@ -196,17 +213,10 @@ export class WidgetVisibilityService {
|
||||
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',
|
||||
|
Loading…
x
Reference in New Issue
Block a user