argos/dmp-frontend/src/app/visibility-rules/visibility-rules.service.ts

53 lines
2.3 KiB
TypeScript

import { VisibilityRule } from './models/VisibilityRule';
import { VisibilityRulesContext } from './models/VisibilityRulesContext';
import { FormGroup } from '@angular/forms';
import { Injectable } from '@angular/core';
import { Rule } from '../models/Rule'
@Injectable()
export class VisibilityRulesService {
public formGroup: FormGroup;
public visibilityRuleContext: VisibilityRulesContext;
public fieldsPathMemory: any = {};
public isElementVisible(pathKey: string, id: string) {
if (!this.fieldsPathMemory[id] && pathKey) this.fieldsPathMemory[id] = pathKey;
let visibilityRule = this.visibilityRuleContext.getRulesFromKey(id);
if (!visibilityRule) return true;
if (this.checkElementVisibility(visibilityRule)) {
return true
} else {
this.clearValues(pathKey)
// if (pathKey && this.formGroup.get(pathKey + '.value')) this.formGroup.get(pathKey + '.value').patchValue(null)
return false;
}
}
public checkElementVisibility(visibilityRule: VisibilityRule): boolean {
let sourceVisibilityRules = visibilityRule.sourceVisibilityRules;
for (let i = 0; i < sourceVisibilityRules.length; i++) {
let sourceVisibilityRule = sourceVisibilityRules[i];
if (!this.formGroup.get(this.fieldsPathMemory[sourceVisibilityRule.sourceControlId] + '.value')) return false; //TODO
if (sourceVisibilityRule.sourceControlValue != '' + this.formGroup.get(this.fieldsPathMemory[sourceVisibilityRule.sourceControlId] + '.value').value) return false;
}
return true;
}
public buildVisibilityRules(item: Array<Rule>) {
this.visibilityRuleContext = new VisibilityRulesContext();
this.visibilityRuleContext.buildVisibilityRuleContext(item);
}
private getParentPath(parentId: string): string {
return '';
}
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 (var i = 0; i < this.formGroup.get(pathKey)["controls"].fields.length; i++)
this.clearValues(pathKey+'.fields.'+i);
}
}
}