argos/dmp-frontend/src/app/ui/misc/dataset-description-form/visibility-rules/visibility-rules.service.ts

152 lines
5.5 KiB
TypeScript
Raw Normal View History

2019-01-18 18:03:45 +01:00
import { ApplicationRef, Injectable, NgZone } from '@angular/core';
2018-05-28 11:50:42 +02:00
import { FormGroup } from '@angular/forms';
2019-01-18 18:03:45 +01:00
import { DatasetProfileDefinitionModel } from '../../../../core/model/dataset-profile-definition/dataset-profile-definition';
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 {
2018-10-05 17:00:54 +02:00
public formGroup: FormGroup;
2019-01-18 18:03:45 +01:00
private visibilityRuleContext: VisibilityRulesContext;
private fieldsPathMemory: any = {};
2018-10-05 17:00:54 +02:00
private elementVisibilityMap = new Map<String, boolean>();
private initialModel: DatasetProfileDefinitionModel;
private currentModel: DatasetProfileDefinitionModel;
2019-01-18 18:03:45 +01:00
constructor(
public applicationReference: ApplicationRef,
public ngZone: NgZone
) {
2018-10-05 17:00:54 +02:00
}
public setModel(model: DatasetProfileDefinitionModel) {
2019-01-18 18:03:45 +01:00
this.initialModel = model;
2018-10-05 17:00:54 +02:00
this.currentModel = model;
//this.visibilityRuleContext.rules.forEach(item => this.evaluateVisibility(item))
}
public triggerVisibilityEvaluation() {
this.visibilityRuleContext.rules.forEach(item => this.evaluateVisibility(item));
}
public getFormGroup(id: string): FormGroup {
const pathKeyArray = this.search('pages', this.initialModel.pages, id).split('.');
pathKeyArray.pop();
const pathKey = pathKeyArray.join('.');
if (!this.fieldsPathMemory[id] && pathKey) { this.fieldsPathMemory[id] = pathKey; }
return (<FormGroup>this.formGroup.get(pathKey));
}
public checkElementVisibility(id: string): boolean {
return !this.elementVisibilityMap.has(id) || this.elementVisibilityMap.get(id);
2018-10-05 17:00:54 +02:00
}
public buildVisibilityRules(item: Array<Rule>) {
this.visibilityRuleContext = new VisibilityRulesContext();
2019-01-18 18:03:45 +01:00
this.visibilityRuleContext.buildVisibilityRuleContext(item || []);
2018-10-05 17:00:54 +02:00
}
public updateValueAndVisibility(id: string) {
const visibilityRules = this.visibilityRuleContext.rules.filter(item => item.sourceVisibilityRules.filter(source => source.sourceControlId === id).length > 0);
visibilityRules.forEach(item => this.evaluateVisibility(item));
2018-10-05 17:00:54 +02:00
}
private evaluateVisibility(visibilityRule: VisibilityRule) {
for (let i = 0; i < visibilityRule.sourceVisibilityRules.length; i++) {
const pathKey = this.fieldsPathMemory[visibilityRule.sourceVisibilityRules[i].sourceControlId];
if (this.formGroup.get(pathKey + '.value') && (this.parseValue(this.formGroup.get(pathKey + '.value').value) !== this.parseValue(visibilityRule.sourceVisibilityRules[i].sourceControlValue))) {
2018-10-05 17:00:54 +02:00
if (this.formGroup.get(pathKey).parent.get('id')) {
if (!this.checkElementVisibility(this.formGroup.get(pathKey).parent.get('id').value)) {
const targetPathKey = this.fieldsPathMemory[visibilityRule.targetControlId];
this.getObject(this.currentModel, 'id', visibilityRule.targetControlId, this.currentModel, true);
this.elementVisibilityMap.set(visibilityRule.targetControlId, false);
this.clearValues(targetPathKey);
return;
}
} else {
const targetPathKey = this.fieldsPathMemory[visibilityRule.targetControlId];
this.getObject(this.currentModel, 'id', visibilityRule.targetControlId, this.currentModel, true);
this.elementVisibilityMap.set(visibilityRule.targetControlId, false);
this.clearValues(targetPathKey);
return;
}
}
}
const obj = this.getObject(this.initialModel, 'id', visibilityRule.targetControlId, this.initialModel);
const targetObjPathKey = this.fieldsPathMemory[visibilityRule.targetControlId] ? this.fieldsPathMemory[visibilityRule.targetControlId] : this.search('pages', this.initialModel.pages, obj);
this.updateValue(this.currentModel, obj, targetObjPathKey);
this.elementVisibilityMap.set(visibilityRule.targetControlId, true);
}
private clearValues(pathKey) {
if (pathKey && this.formGroup.get(pathKey + '.value')) { this.formGroup.get(pathKey + '.value').patchValue(null); }
if (pathKey && this.formGroup.get(pathKey)['controls'].fields) {
for (let i = 0; i < this.formGroup.get(pathKey)['controls'].fields.length; i++) {
this.clearValues(pathKey + '.fields.' + i);
}
}
}
parseValue(value: any) {
if (typeof value === 'string') {
if (value === 'true') { return true; } else if (value === 'false') { return false; } else { return this.translate(value); }
} else { return value; }
}
2018-10-05 17:00:54 +02:00
updateValue(obj, value, path) {
let i;
path = path.split('.');
for (i = 0; i < path.length - 1; i++) {
obj = obj[path[i]];
}
for (let propIndex = 0; propIndex < obj.length; propIndex++) {
if (obj[propIndex] && obj[propIndex]['id'] === value['id']) { return; }
}
obj[path[i]] = value;
}
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;
}
private getObject(obj, key, val, parent, deleteObj = false) {
for (const i in obj) {
if (!obj.hasOwnProperty(i)) { continue; }
if (typeof obj[i] === 'object') {
const returnObj = this.getObject(obj[i], key, val, obj, deleteObj);
if (returnObj) { return returnObj; }
} else if (i === key && obj[key] === val) {
//console.log(obj[key])
if (deleteObj) { parent[parent.indexOf(obj)] = null; }
return obj;
}
}
}
private translate(item: any) {
try {
return JSON.parse(item).value;
} catch (error) {
return item;
}
}
2018-05-28 11:50:42 +02:00
}