2018-05-28 11:50:42 +02:00
|
|
|
import { VisibilityRule } from './models/VisibilityRule';
|
|
|
|
import { VisibilityRulesContext } from './models/VisibilityRulesContext';
|
|
|
|
import { FormGroup } from '@angular/forms';
|
2018-09-06 14:50:38 +02:00
|
|
|
import { Injectable, ApplicationRef, NgZone } from '@angular/core';
|
2018-05-28 11:50:42 +02:00
|
|
|
import { Rule } from '../../models/Rule';
|
2018-09-06 14:50:38 +02:00
|
|
|
import { DatasetProfileDefinitionModel } from '../../models/DatasetProfileDefinitionModel';
|
|
|
|
import { JsonSerializer } from '../JsonSerializer';
|
2018-05-28 11:50:42 +02:00
|
|
|
@Injectable()
|
|
|
|
export class VisibilityRulesService {
|
|
|
|
|
|
|
|
public formGroup: FormGroup;
|
|
|
|
public visibilityRuleContext: VisibilityRulesContext;
|
|
|
|
public fieldsPathMemory: any = {};
|
|
|
|
private elementVisibilityMap = new Map<String, boolean>();
|
2018-09-06 14:50:38 +02:00
|
|
|
private initialModel: DatasetProfileDefinitionModel;
|
|
|
|
private currentModel: DatasetProfileDefinitionModel;
|
2018-05-28 11:50:42 +02:00
|
|
|
|
2018-09-06 14:50:38 +02:00
|
|
|
constructor(public applicationReference: ApplicationRef, public ngZone: NgZone) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public setModel(model: DatasetProfileDefinitionModel) {
|
|
|
|
this.initialModel = JsonSerializer.fromJSONObject(model, DatasetProfileDefinitionModel);
|
|
|
|
this.currentModel = model
|
2018-09-18 14:41:24 +02:00
|
|
|
//this.visibilityRuleContext.rules.forEach(item => this.evaluateVisibility(item))
|
|
|
|
}
|
2018-05-28 11:50:42 +02:00
|
|
|
|
2018-09-18 14:41:24 +02:00
|
|
|
public triggerVisibilityEvaluation() {
|
|
|
|
this.visibilityRuleContext.rules.forEach(item => this.evaluateVisibility(item))
|
2018-09-06 14:50:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public getFormGroup(id: string): FormGroup {
|
|
|
|
let pathKeyArray = this.search("pages", this.initialModel.pages, id).split(".")
|
|
|
|
|
|
|
|
pathKeyArray.pop();
|
|
|
|
let pathKey = pathKeyArray.join(".");
|
|
|
|
if (!this.fieldsPathMemory[id] && pathKey) this.fieldsPathMemory[id] = pathKey;
|
|
|
|
return (<FormGroup>this.formGroup.get(pathKey));
|
2018-05-28 11:50:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public checkElementVisibility(id: string): boolean {
|
|
|
|
if (!this.elementVisibilityMap.has(id) || this.elementVisibilityMap.get(id)) return true;
|
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public buildVisibilityRules(item: Array<Rule>) {
|
|
|
|
this.visibilityRuleContext = new VisibilityRulesContext();
|
|
|
|
this.visibilityRuleContext.buildVisibilityRuleContext(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
public updateValueAndVisibility(id: string) {
|
|
|
|
let visibilityRules = this.visibilityRuleContext.rules.filter(item => item.sourceVisibilityRules.filter(source => source.sourceControlId === id).length > 0);
|
|
|
|
visibilityRules.forEach(item => this.evaluateVisibility(item))
|
|
|
|
}
|
|
|
|
|
|
|
|
private evaluateVisibility(visibilityRule: VisibilityRule) {
|
|
|
|
for (let i = 0; i < visibilityRule.sourceVisibilityRules.length; i++) {
|
|
|
|
let pathKey = this.fieldsPathMemory[visibilityRule.sourceVisibilityRules[i].sourceControlId];
|
2018-09-18 14:41:24 +02:00
|
|
|
if (this.formGroup.get(pathKey + '.value') && ((this.formGroup.get(pathKey + '.value').value == null || this.formGroup.get(pathKey + '.value').value == null) || "" + this.formGroup.get(pathKey + '.value').value != "" + visibilityRule.sourceVisibilityRules[i].sourceControlValue)) {
|
2018-05-28 11:50:42 +02:00
|
|
|
if (this.formGroup.get(pathKey).parent.get("id")) {
|
|
|
|
if (!this.checkElementVisibility(this.formGroup.get(pathKey).parent.get("id").value)) {
|
2018-09-06 14:50:38 +02:00
|
|
|
let targetPathKey = this.fieldsPathMemory[visibilityRule.targetControlId]
|
|
|
|
this.getObject(this.currentModel, "id", visibilityRule.targetControlId, this.currentModel, true)
|
2018-05-28 11:50:42 +02:00
|
|
|
this.elementVisibilityMap.set(visibilityRule.targetControlId, false)
|
2018-09-06 14:50:38 +02:00
|
|
|
this.clearValues(targetPathKey)
|
2018-05-28 11:50:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2018-09-06 14:50:38 +02:00
|
|
|
let targetPathKey = this.fieldsPathMemory[visibilityRule.targetControlId]
|
|
|
|
this.getObject(this.currentModel, "id", visibilityRule.targetControlId, this.currentModel, true)
|
2018-05-28 11:50:42 +02:00
|
|
|
this.elementVisibilityMap.set(visibilityRule.targetControlId, false)
|
2018-09-06 14:50:38 +02:00
|
|
|
this.clearValues(targetPathKey)
|
2018-05-28 11:50:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-06 14:50:38 +02:00
|
|
|
let obj = this.getObject(this.initialModel, "id", visibilityRule.targetControlId, this.initialModel)
|
|
|
|
let targetObjPathKey = this.fieldsPathMemory[visibilityRule.targetControlId] ? this.fieldsPathMemory[visibilityRule.targetControlId] : this.search("pages", this.initialModel.pages, obj);
|
|
|
|
this.updateValue(this.currentModel, obj, targetObjPathKey)
|
2018-05-28 11:50:42 +02:00
|
|
|
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 (var i = 0; i < this.formGroup.get(pathKey)["controls"].fields.length; i++)
|
|
|
|
this.clearValues(pathKey + '.fields.' + i);
|
|
|
|
}
|
2018-09-06 14:50:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// deleteFromModel(path: string, obj: any) {
|
|
|
|
// if (!path) return
|
|
|
|
// const _obj = JSON.parse(JSON.stringify(obj));
|
|
|
|
// const keys = path.split('.');
|
|
|
|
|
|
|
|
// keys.reduce((acc, key, index) => {
|
|
|
|
// if (index === keys.length - 1) {
|
|
|
|
// delete acc[key];
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
// return acc[key];
|
|
|
|
// }, _obj);
|
|
|
|
// return _obj;
|
|
|
|
// }
|
|
|
|
|
|
|
|
updateValue(obj, value, path) {
|
|
|
|
var i;
|
|
|
|
path = path.split('.');
|
2018-09-18 14:41:24 +02:00
|
|
|
|
|
|
|
for (i = 0; i < path.length - 1; i++) {
|
2018-09-06 14:50:38 +02:00
|
|
|
obj = obj[path[i]];
|
2018-09-18 14:41:24 +02:00
|
|
|
}
|
2018-09-06 14:50:38 +02:00
|
|
|
|
|
|
|
for (let propIndex = 0; propIndex < obj.length; propIndex++) {
|
2018-09-18 14:41:24 +02:00
|
|
|
if (obj[propIndex] && obj[propIndex]["id"] === value["id"]) return
|
2018-09-06 14:50:38 +02:00
|
|
|
}
|
2018-09-18 14:41:24 +02:00
|
|
|
obj[path[i]] = value;
|
2018-09-06 14:50:38 +02:00
|
|
|
}
|
2018-05-28 11:50:42 +02:00
|
|
|
|
2018-09-06 14:50:38 +02:00
|
|
|
search(path, obj, target) {
|
|
|
|
for (var k in obj) {
|
|
|
|
if (obj.hasOwnProperty(k))
|
|
|
|
if (obj[k] === target)
|
|
|
|
return path + "." + k
|
|
|
|
else if (typeof obj[k] === "object") {
|
|
|
|
var result = this.search(path + "." + k, obj[k], target);
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private getObject(obj, key, val, parent, deleteObj = false) {
|
|
|
|
for (var i in obj) {
|
|
|
|
if (!obj.hasOwnProperty(i)) continue;
|
|
|
|
if (typeof obj[i] == 'object') {
|
|
|
|
let returnObj = this.getObject(obj[i], key, val, obj, deleteObj);
|
|
|
|
if (returnObj) return returnObj
|
|
|
|
} else if (i == key && obj[key] == val) {
|
|
|
|
//console.log(obj[key])
|
2018-09-18 14:41:24 +02:00
|
|
|
if (deleteObj) parent[parent.indexOf(obj)] = null
|
2018-09-06 14:50:38 +02:00
|
|
|
return obj
|
|
|
|
}
|
|
|
|
}
|
2018-05-28 11:50:42 +02:00
|
|
|
}
|
|
|
|
}
|