2019-01-18 18:03:45 +01:00
|
|
|
import { ApplicationRef, Injectable, NgZone } from '@angular/core';
|
2019-02-08 09:48:28 +01:00
|
|
|
import { AbstractControl, FormArray, FormControl, FormGroup } from '@angular/forms';
|
2021-04-05 10:37:00 +02:00
|
|
|
import { DatasetProfileFieldViewStyle } from '@app/core/common/enum/dataset-profile-field-view-style';
|
2019-01-31 11:29:35 +01:00
|
|
|
import { isNumeric } from 'rxjs/internal/util/isNumeric';
|
2019-01-18 18:03:45 +01:00
|
|
|
import { Rule } from '../../../../core/model/dataset-profile-definition/rule';
|
|
|
|
import { VisibilityRule } from './models/visibility-rule';
|
|
|
|
import { VisibilityRulesContext } from './models/visibility-rules-context';
|
|
|
|
|
2018-05-28 11:50:42 +02:00
|
|
|
@Injectable()
|
|
|
|
export class VisibilityRulesService {
|
|
|
|
|
2019-01-31 11:29:35 +01:00
|
|
|
|
2019-01-18 18:03:45 +01:00
|
|
|
private visibilityRuleContext: VisibilityRulesContext;
|
2019-02-08 09:48:28 +01:00
|
|
|
private form: AbstractControl;
|
2018-10-05 17:00:54 +02:00
|
|
|
private elementVisibilityMap = new Map<String, boolean>();
|
|
|
|
|
2019-01-18 18:03:45 +01:00
|
|
|
constructor(
|
|
|
|
public applicationReference: ApplicationRef,
|
|
|
|
public ngZone: NgZone
|
|
|
|
) {
|
2018-10-05 17:00:54 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public checkElementVisibility(id: string): boolean {
|
2019-01-28 14:47:31 +01:00
|
|
|
if (this.visibilityRuleContext.rules.filter(item => item.targetControlId === id).length === 0) { return true; }
|
2019-01-29 08:47:50 +01:00
|
|
|
return this.elementVisibilityMap.has(id) ? this.elementVisibilityMap.get(id) : false;
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
2019-02-08 09:48:28 +01:00
|
|
|
public buildVisibilityRules(item: Array<Rule>, form: AbstractControl) {
|
2018-10-05 17:00:54 +02:00
|
|
|
this.visibilityRuleContext = new VisibilityRulesContext();
|
2019-01-18 18:03:45 +01:00
|
|
|
this.visibilityRuleContext.buildVisibilityRuleContext(item || []);
|
2019-02-08 09:48:28 +01:00
|
|
|
this.form = form;
|
2020-02-25 15:12:57 +01:00
|
|
|
this.resetVisibilityRules();
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
2019-01-28 14:47:31 +01:00
|
|
|
public updateValueAndVisibility(id: string, value: any) {
|
2018-10-05 17:00:54 +02:00
|
|
|
const visibilityRules = this.visibilityRuleContext.rules.filter(item => item.sourceVisibilityRules.filter(source => source.sourceControlId === id).length > 0);
|
2019-01-28 14:47:31 +01:00
|
|
|
visibilityRules.forEach(item => this.evaluateVisibility(item, value));
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
2019-01-28 14:47:31 +01:00
|
|
|
private evaluateVisibility(visibilityRule: VisibilityRule, value: any) {
|
2021-04-05 10:37:00 +02:00
|
|
|
|
|
|
|
if (value instanceof Array){
|
|
|
|
|
|
|
|
const parsedSourceControlValues = visibilityRule.sourceVisibilityRules.map(e=>this.parseValue(e.sourceControlValue));
|
|
|
|
const parsedValues = value.map(e=>this.parseValue(e));
|
|
|
|
|
|
|
|
const isVisible = parsedValues.map(v=>parsedSourceControlValues.includes(v)).reduce((acc,current)=> acc|| current, false);
|
|
|
|
|
|
|
|
|
|
|
|
if(isVisible){
|
|
|
|
this.elementVisibilityMap.set(visibilityRule.targetControlId, true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-10-05 17:00:54 +02:00
|
|
|
for (let i = 0; i < visibilityRule.sourceVisibilityRules.length; i++) {
|
2019-01-29 08:47:50 +01:00
|
|
|
if (value != null && (this.parseValue(value) !== this.parseValue(visibilityRule.sourceVisibilityRules[i].sourceControlValue))) {
|
2019-01-28 14:47:31 +01:00
|
|
|
this.elementVisibilityMap.set(visibilityRule.targetControlId, false);
|
2019-02-08 09:48:28 +01:00
|
|
|
this.resetControlWithId(this.form, visibilityRule.targetControlId);
|
|
|
|
//this.updateValueAndVisibility(visibilityRule.targetControlId, null);
|
2019-02-07 09:26:00 +01:00
|
|
|
// this.clearValues(targetPathKey);
|
2019-01-28 14:47:31 +01:00
|
|
|
return;
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this.elementVisibilityMap.set(visibilityRule.targetControlId, true);
|
2019-02-08 09:48:28 +01:00
|
|
|
//this.updateValueAndVisibility(visibilityRule.targetControlId, null);
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
2020-02-25 15:12:57 +01:00
|
|
|
private resetVisibilityRules() {
|
|
|
|
this.elementVisibilityMap.clear();
|
|
|
|
this.elementVisibilityMap = new Map<String, boolean>();
|
|
|
|
}
|
|
|
|
|
2018-10-22 11:37:55 +02:00
|
|
|
parseValue(value: any) {
|
|
|
|
if (typeof value === 'string') {
|
2019-01-29 08:47:50 +01:00
|
|
|
if (isNumeric(value)) { return value; }
|
|
|
|
else if (value === 'true') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (value === 'false') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else { return this.translate(value); }
|
2018-10-22 11:37:55 +02:00
|
|
|
} else { return value; }
|
|
|
|
}
|
2018-10-05 17:00:54 +02:00
|
|
|
|
2019-01-31 11:29:35 +01:00
|
|
|
search(path, obj, target) {
|
|
|
|
for (const k in obj) {
|
|
|
|
if (obj.hasOwnProperty(k)) {
|
|
|
|
if (obj[k] === target) {
|
|
|
|
return path + '.' + k;
|
|
|
|
} else if (typeof obj[k] === 'object') {
|
|
|
|
const result = this.search(path + '.' + k, obj[k], target);
|
|
|
|
if (result) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-28 14:47:31 +01:00
|
|
|
|
2019-01-31 11:29:35 +01:00
|
|
|
scanIfChildsOfCompositeFieldHasVisibleItems(compositeFieldParent: FormGroup): boolean {
|
|
|
|
let isVisible = false;
|
|
|
|
(<FormArray>(compositeFieldParent.get('fields'))).controls.forEach(element => {
|
|
|
|
if (this.checkElementVisibility(element.get('id').value)) {
|
|
|
|
isVisible = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return isVisible;
|
|
|
|
}
|
2018-11-01 17:02:15 +01:00
|
|
|
|
|
|
|
private translate(item: any) {
|
|
|
|
try {
|
|
|
|
return JSON.parse(item).value;
|
|
|
|
} catch (error) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
}
|
2019-02-08 09:48:28 +01:00
|
|
|
|
|
|
|
private resetControlWithId(formControl: AbstractControl, id: string) {
|
|
|
|
if (formControl instanceof FormGroup) {
|
|
|
|
if ((formControl as FormGroup).contains('id') && (formControl as FormGroup).contains('value') && (formControl as FormGroup).get('id').value === id) {
|
|
|
|
this.resetFieldFormGroup(formControl);
|
|
|
|
} if ((formControl as FormGroup).contains('id') && (formControl as FormGroup).contains('fields') && (formControl as FormGroup).get('id').value === id) {
|
|
|
|
this.resetCompositeFieldFormGroup(formControl);
|
|
|
|
} else {
|
|
|
|
Object.keys(formControl.controls).forEach(item => {
|
|
|
|
const control = formControl.get(item);
|
|
|
|
this.resetControlWithId(control, id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else if (formControl instanceof FormArray) {
|
|
|
|
formControl.controls.forEach(item => {
|
|
|
|
this.resetControlWithId(item, id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private resetFieldFormGroup(formGroup: FormGroup) {
|
2021-04-05 10:37:00 +02:00
|
|
|
const renderStyle = formGroup.getRawValue().viewStyle.renderStyle;
|
|
|
|
if(renderStyle ===DatasetProfileFieldViewStyle.Validation || renderStyle === DatasetProfileFieldViewStyle.DatasetIdentifier){
|
|
|
|
formGroup.get('value').setValue({identifier:'',type:'' });
|
|
|
|
}else{
|
|
|
|
formGroup.get('value').setValue(formGroup.get('defaultValue').value ? this.parseValue(formGroup.get('defaultValue').value.value) : undefined);
|
|
|
|
}
|
|
|
|
|
2019-02-08 09:48:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private resetCompositeFieldFormGroup(formGroup: FormGroup) {
|
|
|
|
(formGroup.get('fields') as FormArray).controls.forEach((element: FormGroup) => {
|
|
|
|
this.resetFieldFormGroup(element);
|
|
|
|
});
|
|
|
|
(formGroup.get('multiplicityItems') as FormArray).controls.splice(0);
|
|
|
|
}
|
2018-05-28 11:50:42 +02:00
|
|
|
}
|