visibly performance improvements
This commit is contained in:
parent
09c428bd99
commit
308f1364af
|
@ -15,6 +15,9 @@ export class VisibilityRulesService {
|
|||
public isVisibleMap: { [key: string]: boolean } = null;
|
||||
private _isVisibleMap: { [key: string]: boolean } = null;
|
||||
|
||||
private allDescriptionTemplateFields: DescriptionTemplateField[] = null;
|
||||
private allDescriptionTemplateFieldSets: DescriptionTemplateFieldSet[] = null;
|
||||
|
||||
constructor(
|
||||
protected formService: FormService
|
||||
) {
|
||||
|
@ -23,6 +26,8 @@ export class VisibilityRulesService {
|
|||
public setContext(definition: DescriptionTemplateDefinition, form: AbstractControl) {
|
||||
this.definition = definition;
|
||||
this.form = form;
|
||||
this.allDescriptionTemplateFields = null;
|
||||
this.allDescriptionTemplateFieldSets = null;
|
||||
this.rulesBySources = null;
|
||||
this._isVisibleMap = null;
|
||||
this.calculateVisibility();
|
||||
|
@ -62,11 +67,13 @@ export class VisibilityRulesService {
|
|||
if (this.definition == null || this.form == null) return;
|
||||
|
||||
this.initRules();
|
||||
this.buildTargetVisibility();
|
||||
this.expandVisibilityToChildren();
|
||||
this.setDefaultVisibilityForNotCaclucted();
|
||||
this.hideParentIfAllChildrenAreHidden();
|
||||
this.ensureFieldSetVisibility();
|
||||
const propertyDefinition: DescriptionPropertyDefinitionPersist = this.formService.getValue(this.form.getRawValue()) as DescriptionPropertyDefinitionPersist;
|
||||
|
||||
this.buildTargetVisibility(propertyDefinition);
|
||||
this.expandVisibilityToChildren(propertyDefinition);
|
||||
this.setDefaultVisibilityForNotCaclucted(propertyDefinition);
|
||||
this.hideParentIfAllChildrenAreHidden(propertyDefinition);
|
||||
this.ensureFieldSetVisibility(propertyDefinition);
|
||||
this.isVisibleMap = this._isVisibleMap;
|
||||
}
|
||||
|
||||
|
@ -95,13 +102,16 @@ export class VisibilityRulesService {
|
|||
}
|
||||
|
||||
private getAllDescriptionTemplateDefinitionFields(definition: DescriptionTemplateDefinition): DescriptionTemplateField[] {
|
||||
if (this.allDescriptionTemplateFields != null) return this.allDescriptionTemplateFields;
|
||||
|
||||
let fields: DescriptionTemplateField[] = [];
|
||||
if (definition.pages == null) return fields;
|
||||
for (let i = 0; i < definition.pages.length; i++) {
|
||||
const item = definition.pages[i];
|
||||
fields = [...fields, ...this.getAllDescriptionTemplatePageFields(item)];
|
||||
}
|
||||
return fields;
|
||||
this.allDescriptionTemplateFields = fields;
|
||||
return this.allDescriptionTemplateFields;
|
||||
}
|
||||
|
||||
private getAllDescriptionTemplatePageFields(definition: DescriptionTemplatePage): DescriptionTemplateField[] {
|
||||
|
@ -141,13 +151,15 @@ export class VisibilityRulesService {
|
|||
}
|
||||
|
||||
private getAllDescriptionTemplateDefinitionFieldSets(definition: DescriptionTemplateDefinition): DescriptionTemplateFieldSet[] {
|
||||
if (this.allDescriptionTemplateFieldSets != null) return this.allDescriptionTemplateFieldSets;
|
||||
let fieldSets: DescriptionTemplateFieldSet[] = [];
|
||||
if (definition.pages == null) return fieldSets;
|
||||
for (let i = 0; i < definition.pages.length; i++) {
|
||||
const item = definition.pages[i];
|
||||
fieldSets = [...fieldSets, ...this.getAllDescriptionTemplatePageFieldSets(item)];
|
||||
}
|
||||
return fieldSets;
|
||||
this.allDescriptionTemplateFieldSets = fieldSets;
|
||||
return this.allDescriptionTemplateFieldSets;
|
||||
}
|
||||
|
||||
private getAllDescriptionTemplatePageFieldSets(definition: DescriptionTemplatePage): DescriptionTemplateFieldSet[] {
|
||||
|
@ -177,9 +189,8 @@ export class VisibilityRulesService {
|
|||
return fieldSets;
|
||||
}
|
||||
|
||||
private buildTargetVisibility(){
|
||||
private buildTargetVisibility(propertyDefinition: DescriptionPropertyDefinitionPersist){
|
||||
this._isVisibleMap = {};
|
||||
const propertyDefinition: DescriptionPropertyDefinitionPersist = this.formService.getValue(this.form.getRawValue()) as DescriptionPropertyDefinitionPersist;
|
||||
this.rulesBySources.forEach((ruleForSource: RuleWithTarget[], ruleForSourceKey: string) => {
|
||||
for (let i = 0; i < ruleForSource.length; i++) {
|
||||
const rule = ruleForSource[i];
|
||||
|
@ -189,10 +200,11 @@ export class VisibilityRulesService {
|
|||
for (let j = 0; j < propertyDefinitionFieldSet.items.length; j++) {
|
||||
const definitionFieldSetItem = propertyDefinitionFieldSet.items[j];
|
||||
if (definitionFieldSetItem?.fields != null) {
|
||||
new Map(Object.entries(definitionFieldSetItem.fields)).forEach((field: DescriptionFieldPersist, key: string) => {
|
||||
const fieldsMap = new Map(Object.entries(definitionFieldSetItem.fields));
|
||||
fieldsMap.forEach((field: DescriptionFieldPersist, key: string) => {
|
||||
if (rule.source == key){
|
||||
|
||||
if (new Map(Object.entries(definitionFieldSetItem.fields)).has(rule.target)){ //Rule applies only for current multiple item
|
||||
if (fieldsMap.has(rule.target)){ //Rule applies only for current multiple item
|
||||
const fieldKey = this.buildVisibilityKey(rule.target, definitionFieldSetItem.ordinal);
|
||||
const currentState = this._isVisibleMap[fieldKey] ?? false;
|
||||
this._isVisibleMap[fieldKey] = currentState || this.ruleIsTrue(rule, field);
|
||||
|
@ -274,17 +286,17 @@ export class VisibilityRulesService {
|
|||
return false;
|
||||
}
|
||||
|
||||
private expandVisibilityToChildren(){
|
||||
private expandVisibilityToChildren(propertyDefinition: DescriptionPropertyDefinitionPersist){
|
||||
if (this.definition?.pages == null) return;
|
||||
for (let i = 0; i < this.definition?.pages.length; i++) {
|
||||
const pageEntity = this.definition?.pages[i];
|
||||
const fieldKey = this.buildVisibilityKey(pageEntity.id, null);
|
||||
const currentValue: boolean | null = this._isVisibleMap[fieldKey] ?? null;
|
||||
this.expandPageVisibility(pageEntity, currentValue);
|
||||
this.expandPageVisibility(pageEntity, propertyDefinition, currentValue);
|
||||
}
|
||||
}
|
||||
|
||||
private expandPageVisibility(pageEntity : DescriptionTemplatePage, parentVisibility : boolean | null){
|
||||
private expandPageVisibility(pageEntity : DescriptionTemplatePage, propertyDefinition: DescriptionPropertyDefinitionPersist, parentVisibility : boolean | null){
|
||||
if (pageEntity.sections == null) return;
|
||||
for (let i = 0; i < pageEntity.sections.length; i++) {
|
||||
const sectionEntity = pageEntity.sections[i];
|
||||
|
@ -293,17 +305,17 @@ export class VisibilityRulesService {
|
|||
if (currentValue != null){
|
||||
if (parentVisibility != null && !parentVisibility) { //Parent is hidden so all childs should be hidden
|
||||
this._isVisibleMap[fieldKey] = false;
|
||||
this.expandSectionVisibility(sectionEntity, currentValue);
|
||||
this.expandSectionVisibility(sectionEntity, propertyDefinition, currentValue);
|
||||
} else {
|
||||
this.expandSectionVisibility(sectionEntity, currentValue);
|
||||
this.expandSectionVisibility(sectionEntity, propertyDefinition, currentValue);
|
||||
}
|
||||
} else {
|
||||
if (parentVisibility != null) this._isVisibleMap[fieldKey] = parentVisibility;
|
||||
this.expandSectionVisibility(sectionEntity, parentVisibility);
|
||||
this.expandSectionVisibility(sectionEntity, propertyDefinition, parentVisibility);
|
||||
}
|
||||
}
|
||||
}
|
||||
private expandSectionVisibility(sectionEntity: DescriptionTemplateSection, parentVisibility : boolean | null){
|
||||
private expandSectionVisibility(sectionEntity: DescriptionTemplateSection, propertyDefinition: DescriptionPropertyDefinitionPersist, parentVisibility : boolean | null){
|
||||
if (sectionEntity.sections != null) {
|
||||
for (let i = 0; i < sectionEntity.sections.length; i++) {
|
||||
const subSectionEntity = sectionEntity.sections[i];
|
||||
|
@ -312,23 +324,23 @@ export class VisibilityRulesService {
|
|||
if (currentValue != null){
|
||||
if (parentVisibility != null && !parentVisibility) { //Parent is hidden so all childs should be hidden
|
||||
this._isVisibleMap[fieldKey] = false;
|
||||
this.expandSectionVisibility(subSectionEntity, currentValue);
|
||||
this.expandSectionVisibility(subSectionEntity, propertyDefinition, currentValue);
|
||||
} else {
|
||||
this.expandSectionVisibility(subSectionEntity, currentValue);
|
||||
this.expandSectionVisibility(subSectionEntity, propertyDefinition, currentValue);
|
||||
}
|
||||
} else {
|
||||
if (parentVisibility != null) this._isVisibleMap[fieldKey] = parentVisibility;
|
||||
this.expandSectionVisibility(subSectionEntity, parentVisibility);
|
||||
this.expandSectionVisibility(subSectionEntity, propertyDefinition, parentVisibility);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (sectionEntity.fieldSets != null) {
|
||||
|
||||
const propertyDefinition = this.formService.getValue(this.form.getRawValue()) as DescriptionPropertyDefinitionPersist;
|
||||
for (let i = 0; i < sectionEntity.fieldSets.length; i++) {
|
||||
const fieldSetEntity = sectionEntity.fieldSets[i];
|
||||
if (propertyDefinition.fieldSets != null && new Map(Object.entries(propertyDefinition.fieldSets)).has(fieldSetEntity.id)) {
|
||||
const propertyDefinitionFieldSet = new Map(Object.entries(propertyDefinition.fieldSets)).get(fieldSetEntity.id)
|
||||
const fieldSetsMap = propertyDefinition.fieldSets != null ? new Map(Object.entries(propertyDefinition.fieldSets)) : null;
|
||||
if (fieldSetsMap != null && fieldSetsMap.has(fieldSetEntity.id)) {
|
||||
const propertyDefinitionFieldSet = fieldSetsMap.get(fieldSetEntity.id)
|
||||
if (propertyDefinitionFieldSet.items != null && propertyDefinitionFieldSet.items.length > 0) {
|
||||
for (let j = 0; j < propertyDefinitionFieldSet.items.length; j++) {
|
||||
const definitionFieldSetItem = propertyDefinitionFieldSet.items[j];
|
||||
|
@ -369,18 +381,18 @@ export class VisibilityRulesService {
|
|||
}
|
||||
}
|
||||
|
||||
private setDefaultVisibilityForNotCaclucted() {
|
||||
private setDefaultVisibilityForNotCaclucted(propertyDefinition: DescriptionPropertyDefinitionPersist) {
|
||||
if (this.definition?.pages == null) return;
|
||||
for (let i = 0; i < this.definition?.pages.length; i++) {
|
||||
const pageEntity = this.definition?.pages[i];
|
||||
const fieldKey = this.buildVisibilityKey(pageEntity.id, null);
|
||||
const currentValue: boolean | null = this._isVisibleMap[fieldKey] ?? null;
|
||||
if (currentValue == null) this._isVisibleMap[fieldKey] = true;
|
||||
this.setDefaultPageVisibility(pageEntity);
|
||||
this.setDefaultPageVisibility(pageEntity, propertyDefinition);
|
||||
}
|
||||
}
|
||||
|
||||
private setDefaultPageVisibility(pageEntity: DescriptionTemplatePage) {
|
||||
private setDefaultPageVisibility(pageEntity: DescriptionTemplatePage, propertyDefinition: DescriptionPropertyDefinitionPersist) {
|
||||
if (pageEntity.sections == null) return;
|
||||
for (let i = 0; i < pageEntity.sections.length; i++) {
|
||||
const sectionEntity = pageEntity.sections[i];
|
||||
|
@ -388,12 +400,12 @@ export class VisibilityRulesService {
|
|||
const currentValue: boolean | null = this._isVisibleMap[fieldKey] ?? null;
|
||||
if (currentValue == null){
|
||||
this._isVisibleMap[fieldKey] = true;
|
||||
this.setDefaultSectionVisibility(sectionEntity);
|
||||
this.setDefaultSectionVisibility(sectionEntity, propertyDefinition);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private setDefaultSectionVisibility(sectionEntity: DescriptionTemplateSection) {
|
||||
private setDefaultSectionVisibility(sectionEntity: DescriptionTemplateSection, propertyDefinition: DescriptionPropertyDefinitionPersist) {
|
||||
if (sectionEntity.sections != null) {
|
||||
for (let i = 0; i < sectionEntity.sections.length; i++) {
|
||||
const subSectionEntity = sectionEntity.sections[i];
|
||||
|
@ -401,17 +413,16 @@ export class VisibilityRulesService {
|
|||
const currentValue: boolean | null = this._isVisibleMap[fieldKey] ?? null;
|
||||
if (currentValue == null){
|
||||
this._isVisibleMap[fieldKey] = true;
|
||||
this.setDefaultSectionVisibility(subSectionEntity);
|
||||
this.setDefaultSectionVisibility(subSectionEntity, propertyDefinition);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (sectionEntity.fieldSets != null) {
|
||||
const propertyDefinition = this.formService.getValue(this.form.getRawValue()) as DescriptionPropertyDefinitionPersist;
|
||||
for (let i = 0; i < sectionEntity.fieldSets.length; i++) {
|
||||
const fieldSetEntity = sectionEntity.fieldSets[i];
|
||||
|
||||
if (propertyDefinition.fieldSets != null && new Map(Object.entries(propertyDefinition.fieldSets)).has(fieldSetEntity.id)) {
|
||||
const propertyDefinitionFieldSet = new Map(Object.entries(propertyDefinition.fieldSets)).get(fieldSetEntity.id)
|
||||
const fieldSetsMap = propertyDefinition.fieldSets != null ? new Map(Object.entries(propertyDefinition.fieldSets)) : null;
|
||||
if (fieldSetsMap != null && fieldSetsMap.has(fieldSetEntity.id)) {
|
||||
const propertyDefinitionFieldSet = fieldSetsMap.get(fieldSetEntity.id)
|
||||
if (propertyDefinitionFieldSet.items != null && propertyDefinitionFieldSet.items.length > 0) {
|
||||
for (let j = 0; j < propertyDefinitionFieldSet.items.length; j++) {
|
||||
const definitionFieldSetItem = propertyDefinitionFieldSet.items[j];
|
||||
|
@ -441,12 +452,12 @@ export class VisibilityRulesService {
|
|||
}
|
||||
}
|
||||
|
||||
private hideParentIfAllChildrenAreHidden() {
|
||||
private hideParentIfAllChildrenAreHidden(propertyDefinition: DescriptionPropertyDefinitionPersist) {
|
||||
if (this.definition?.pages == null) return;
|
||||
for (let i = 0; i < this.definition?.pages.length; i++) {
|
||||
const pageEntity = this.definition?.pages[i];
|
||||
const fieldKey = this.buildVisibilityKey(pageEntity.id, null);
|
||||
const isCurrentHidden = this.isHiddenPageVisibilityIfAllChildrenIsHidden(pageEntity);
|
||||
const isCurrentHidden = this.isHiddenPageVisibilityIfAllChildrenIsHidden(pageEntity, propertyDefinition);
|
||||
|
||||
if (isCurrentHidden && (this._isVisibleMap[fieldKey] ?? true)) {
|
||||
this._isVisibleMap[fieldKey] = false;
|
||||
|
@ -454,13 +465,13 @@ export class VisibilityRulesService {
|
|||
}
|
||||
}
|
||||
|
||||
private isHiddenPageVisibilityIfAllChildrenIsHidden(pageEntity: DescriptionTemplatePage): boolean{
|
||||
private isHiddenPageVisibilityIfAllChildrenIsHidden(pageEntity: DescriptionTemplatePage, propertyDefinition: DescriptionPropertyDefinitionPersist): boolean{
|
||||
let isHidden = true;
|
||||
if (pageEntity?.sections == null) return;
|
||||
for (let i = 0; i < pageEntity.sections.length; i++) {
|
||||
const sectionEntity = pageEntity.sections[i];
|
||||
const fieldKey = this.buildVisibilityKey(sectionEntity.id, null);
|
||||
const isCurrentHidden = this.isHiddenSectionIfAllChildrenIsHidden(sectionEntity);
|
||||
const isCurrentHidden = this.isHiddenSectionIfAllChildrenIsHidden(sectionEntity, propertyDefinition);
|
||||
if (isCurrentHidden && (this._isVisibleMap[fieldKey] ?? true)) {
|
||||
this._isVisibleMap[fieldKey] = false;
|
||||
}
|
||||
|
@ -469,13 +480,13 @@ export class VisibilityRulesService {
|
|||
return isHidden;
|
||||
}
|
||||
|
||||
private isHiddenSectionIfAllChildrenIsHidden(sectionEntity: DescriptionTemplateSection): boolean{
|
||||
private isHiddenSectionIfAllChildrenIsHidden(sectionEntity: DescriptionTemplateSection, propertyDefinition: DescriptionPropertyDefinitionPersist): boolean{
|
||||
let isHidden = true;
|
||||
if (sectionEntity.sections != null) {
|
||||
for (let i = 0; i < sectionEntity.sections.length; i++) {
|
||||
const subSectionEntity = sectionEntity.sections[i];
|
||||
const fieldKey = this.buildVisibilityKey(subSectionEntity.id, null);
|
||||
const isCurrentHidden = this.isHiddenSectionIfAllChildrenIsHidden(subSectionEntity);
|
||||
const isCurrentHidden = this.isHiddenSectionIfAllChildrenIsHidden(subSectionEntity, propertyDefinition);
|
||||
if (isCurrentHidden && (this._isVisibleMap[fieldKey] ?? true)) {
|
||||
this._isVisibleMap[fieldKey] = false;
|
||||
}
|
||||
|
@ -483,11 +494,11 @@ export class VisibilityRulesService {
|
|||
}
|
||||
}
|
||||
if (sectionEntity.fieldSets != null) {
|
||||
const propertyDefinition = this.formService.getValue(this.form.getRawValue()) as DescriptionPropertyDefinitionPersist;
|
||||
for (let i = 0; i < sectionEntity.fieldSets.length; i++) {
|
||||
const fieldSetEntity = sectionEntity.fieldSets[i];
|
||||
if (propertyDefinition.fieldSets != null && new Map(Object.entries(propertyDefinition.fieldSets)).has(fieldSetEntity.id)) {
|
||||
const propertyDefinitionFieldSet = new Map(Object.entries(propertyDefinition.fieldSets)).get(fieldSetEntity.id)
|
||||
const fieldSetsMap = propertyDefinition.fieldSets != null ? new Map(Object.entries(propertyDefinition.fieldSets)) : null;
|
||||
if (fieldSetsMap != null && fieldSetsMap.has(fieldSetEntity.id)) {
|
||||
const propertyDefinitionFieldSet = fieldSetsMap.get(fieldSetEntity.id)
|
||||
if (propertyDefinitionFieldSet.items != null && propertyDefinitionFieldSet.items.length > 0) {
|
||||
for (let j = 0; j < propertyDefinitionFieldSet.items.length; j++) {
|
||||
const definitionFieldSetItem = propertyDefinitionFieldSet.items[j];
|
||||
|
@ -519,36 +530,36 @@ export class VisibilityRulesService {
|
|||
return true;
|
||||
}
|
||||
|
||||
private ensureFieldSetVisibility() {
|
||||
private ensureFieldSetVisibility(propertyDefinition: DescriptionPropertyDefinitionPersist) {
|
||||
if (this.definition?.pages == null) return;
|
||||
for (let i = 0; i < this.definition?.pages.length; i++) {
|
||||
const pageEntity = this.definition?.pages[i];
|
||||
this.ensurePageFieldSetVisibility(pageEntity);
|
||||
this.ensurePageFieldSetVisibility(pageEntity, propertyDefinition);
|
||||
}
|
||||
}
|
||||
|
||||
private ensurePageFieldSetVisibility(pageEntity: DescriptionTemplatePage){
|
||||
private ensurePageFieldSetVisibility(pageEntity: DescriptionTemplatePage, propertyDefinition: DescriptionPropertyDefinitionPersist){
|
||||
if (pageEntity?.sections == null) return;
|
||||
for (let i = 0; i < pageEntity.sections.length; i++) {
|
||||
const sectionEntity = pageEntity.sections[i];
|
||||
this.ensureSectionFieldSetVisibility(sectionEntity);
|
||||
this.ensureSectionFieldSetVisibility(sectionEntity, propertyDefinition);
|
||||
}
|
||||
}
|
||||
|
||||
private ensureSectionFieldSetVisibility(sectionEntity: DescriptionTemplateSection){
|
||||
private ensureSectionFieldSetVisibility(sectionEntity: DescriptionTemplateSection, propertyDefinition: DescriptionPropertyDefinitionPersist){
|
||||
if (sectionEntity.sections != null) {
|
||||
for (let i = 0; i < sectionEntity.sections.length; i++) {
|
||||
const subSectionEntity = sectionEntity.sections[i];
|
||||
this.ensureSectionFieldSetVisibility(subSectionEntity);
|
||||
this.ensureSectionFieldSetVisibility(subSectionEntity, propertyDefinition);
|
||||
}
|
||||
}
|
||||
if (sectionEntity.fieldSets != null) {
|
||||
const propertyDefinition = this.formService.getValue(this.form.getRawValue()) as DescriptionPropertyDefinitionPersist;
|
||||
for (let i = 0; i < sectionEntity.fieldSets.length; i++) {
|
||||
const fieldSetEntity = sectionEntity.fieldSets[i];
|
||||
let isHidden = true;
|
||||
if (propertyDefinition.fieldSets != null && new Map(Object.entries(propertyDefinition.fieldSets)).has(fieldSetEntity.id)) {
|
||||
const propertyDefinitionFieldSet = new Map(Object.entries(propertyDefinition.fieldSets)).get(fieldSetEntity.id)
|
||||
const fieldSetsMap = propertyDefinition.fieldSets != null ? new Map(Object.entries(propertyDefinition.fieldSets)) : null;
|
||||
if (fieldSetsMap != null && fieldSetsMap.has(fieldSetEntity.id)) {
|
||||
const propertyDefinitionFieldSet = fieldSetsMap.get(fieldSetEntity.id)
|
||||
if (propertyDefinitionFieldSet.items != null && propertyDefinitionFieldSet.items.length > 0) {
|
||||
for (let j = 0; j < propertyDefinitionFieldSet.items.length; j++) {
|
||||
const definitionFieldSetItem = propertyDefinitionFieldSet.items[j];
|
||||
|
|
Loading…
Reference in New Issue