[ADF-1671] visibility fix use id instead of name during comparison (#2441)

* visibility fix use id instead of name during comparison

* fix tslint problems
This commit is contained in:
Eugenio Romano
2017-10-06 15:39:08 +01:00
committed by Denys Vuika
parent 3cd5eafdde
commit 48f0ccfc4f
2 changed files with 34 additions and 30 deletions
@@ -353,14 +353,14 @@ describe('WidgetVisibilityService', () => {
}); });
it('should be able to retrieve a field value searching in the form', () => { it('should be able to retrieve a field value searching in the form', () => {
let formValue = service.searchForm(stubFormWithFields, 'FIELD_WITH_CONDITION'); let formValue = service.searchValueInForm(stubFormWithFields, 'FIELD_WITH_CONDITION');
expect(formValue).not.toBeNull(); expect(formValue).not.toBeNull();
expect(formValue).toBe('field_with_condition_value'); expect(formValue).toBe('field_with_condition_value');
}); });
it('should return empty string if the field value is not in the form', () => { it('should return empty string if the field value is not in the form', () => {
let formValue = service.searchForm(stubFormWithFields, 'FIELD_MYSTERY'); let formValue = service.searchValueInForm(stubFormWithFields, 'FIELD_MYSTERY');
expect(formValue).not.toBeUndefined(); expect(formValue).not.toBeUndefined();
expect(formValue).toBe(''); expect(formValue).toBe('');
@@ -19,7 +19,7 @@ import { Injectable } from '@angular/core';
import * as moment from 'moment'; import * as moment from 'moment';
import { AlfrescoApiService, LogService } from 'ng2-alfresco-core'; import { AlfrescoApiService, LogService } from 'ng2-alfresco-core';
import { Observable } from 'rxjs/Rx'; import { Observable } from 'rxjs/Rx';
import { ContainerColumnModel, ContainerModel, FormFieldModel, FormModel, TabModel } from '../components/widgets/core/index'; import { FormFieldModel, FormModel, TabModel } from '../components/widgets/core/index';
import { TaskProcessVariableModel } from '../models/task-process-variable.model'; import { TaskProcessVariableModel } from '../models/task-process-variable.model';
import { WidgetVisibilityModel } from '../models/widget-visibility.model'; import { WidgetVisibilityModel } from '../models/widget-visibility.model';
@@ -98,43 +98,46 @@ export class WidgetVisibilityService {
return valueFound; return valueFound;
} }
getFormValue(form: FormModel, field: string) { getFormValue(form: FormModel, fieldId: string) {
let value = this.getFieldValue(form.values, field); let value = this.getFieldValue(form.values, fieldId);
return value ? value : this.searchForm(form, field);
if (!value) {
value = this.searchValueInForm(form, fieldId);
} }
getFieldValue(valueList: any, fieldName: string) { return value;
let dropDownFilterByName, valueFound = ''; }
if (fieldName && fieldName.indexOf('_LABEL') > 0) {
dropDownFilterByName = fieldName.substring(0, fieldName.length - 6); getFieldValue(valueList: any, fieldId: string) {
let dropDownFilterByName, valueFound;
if (fieldId && fieldId.indexOf('_LABEL') > 0) {
dropDownFilterByName = fieldId.substring(0, fieldId.length - 6);
if (valueList[dropDownFilterByName]) { if (valueList[dropDownFilterByName]) {
valueFound = valueList[dropDownFilterByName].name; valueFound = valueList[dropDownFilterByName].name;
} }
} else if (valueList[fieldName] && valueList[fieldName].id) { } else if (valueList[fieldId] && valueList[fieldId].id) {
valueFound = valueList[fieldName].id; valueFound = valueList[fieldId].id;
} else { } else {
valueFound = valueList[fieldName]; valueFound = valueList[fieldId];
} }
return valueFound; return valueFound;
} }
searchForm(form: FormModel, name: string) { searchValueInForm(form: FormModel, fieldId: string) {
let fieldValue = ''; let fieldValue = '';
form.fields.forEach((containerModel: ContainerModel) => { form.getFormFields().forEach((formField: FormFieldModel) => {
containerModel.field.columns.forEach((containerColumnModel: ContainerColumnModel) => { if (this.isSearchedField(formField, fieldId)) {
let fieldFound = containerColumnModel.fields.find(field => this.isSearchedField(field, name)); fieldValue = this.getObjectValue(formField);
if (fieldFound) {
fieldValue = this.getObjectValue(fieldFound);
if (!fieldValue) { if (!fieldValue) {
if (fieldFound.value && fieldFound.value.id) { if (formField.value && formField.value.id) {
fieldValue = fieldFound.value.id; fieldValue = formField.value.id;
} else { } else {
fieldValue = fieldFound.value; fieldValue = formField.value;
} }
} }
} }
}); });
});
return fieldValue; return fieldValue;
} }
@@ -147,18 +150,19 @@ export class WidgetVisibilityService {
if (option) { if (option) {
value = option.name; value = option.name;
} else { } else {
value = field.value; value = field.value;
} }
} }
return value; return value;
} }
private isSearchedField(field: FormFieldModel, fieldToFind: string) { private isSearchedField(field: FormFieldModel, fieldToFind: string): boolean {
let forrmattedFieldName = this.removeLabel(field, fieldToFind); let formattedFieldName = this.removeLabel(field, fieldToFind);
return field.name ? field.name.toUpperCase() === forrmattedFieldName.toUpperCase() : false; return field.id ? field.id.toUpperCase() === formattedFieldName.toUpperCase() : false;
} }
private removeLabel(field: FormFieldModel, fieldToFind) { private removeLabel(field: FormFieldModel, fieldToFind): string {
let formattedFieldName = fieldToFind || ''; let formattedFieldName = fieldToFind || '';
if (field.fieldType === 'RestFieldRepresentation' && fieldToFind.indexOf('_LABEL') > 0) { if (field.fieldType === 'RestFieldRepresentation' && fieldToFind.indexOf('_LABEL') > 0) {
formattedFieldName = fieldToFind.substring(0, fieldToFind.length - 6); formattedFieldName = fieldToFind.substring(0, fieldToFind.length - 6);