All files / src/components/zd-iterable conditions-manager.ts

100% Statements 59/59
100% Branches 30/30
100% Functions 14/14
100% Lines 57/57

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 14794x 94x               94x                 214x   214x     214x 214x   214x 378x                   418x 418x 53x 53x 9x   44x     418x       409x 409x       387x 387x 387x 387x 357x     30x 36x   36x 22x     36x 9x                           79x 79x 28x   28x 1x 1x     27x   79x               66x 66x 51x     15x   15x 12x 12x   15x       25x 25x   25x 66x   66x     25x   25x               232x 232x 232x   232x   16x      
import { IDictionary, IEvents, Event as ZdEvent } from '@zeedhi/core';
import set from 'lodash.set';
import { IComponent, IComponentRender } from '../zd-component/interfaces';
import { Column } from './column';
import { IConditionsManager } from './interfaces';
 
export type ConditionFunction = IEvents<{ row: IDictionary; column: Column }>;
export type AppliedConditions = { root: Partial<IComponentRender>; children: IDictionary<Partial<IComponentRender>> };
 
export class ConditionsManager implements IConditionsManager {
	private uniqueKey: string;
 
	private columns: Column[];
 
	/**
	 * Dictionary of rowKey ponting to Dictionary of colName pointing to Dictionary of prop pointing to the
	 * condition (applied) return value
	 */
	private cellsApplied: IDictionary<IDictionary<AppliedConditions>> = {};
 
	private factoredConditions: IDictionary<{ root: ConditionFunction; children: IDictionary<ConditionFunction> }> = {};
 
	constructor(uniqueKey: string, columns: Column[]) {
		this.uniqueKey = uniqueKey;
		this.columns = columns;
 
		this.columns.forEach((column) => {
			this.createConditions(column);
		});
	}
 
	/**
	 * Return plain conditions object
	 * @param conditions Conditions object
	 * @param parentPath Path to parent property
	 */
	private getPlainConditions(conditions: IDictionary<any>, parentPath = '') {
		let plainData: IDictionary<string> = {};
		Object.keys(conditions).forEach((key) => {
			const fullKey = parentPath ? [parentPath, key].join('.') : key;
			if (typeof conditions[key] === 'object') {
				plainData = { ...plainData, ...this.getPlainConditions(conditions[key], fullKey) };
			} else {
				plainData[fullKey] = conditions[key];
			}
		});
		return plainData;
	}
 
	private factorConditions(conditions: IDictionary<any>) {
		const actionPlainConditions = this.getPlainConditions(conditions);
		return ZdEvent.factory(actionPlainConditions);
	}
 
	private createConditions(column: Column, children: IComponent[] = column.children, parentPath = '') {
		this.factoredConditions[column.name] = { root: {}, children: {} };
		this.factoredConditions[column.name].root = this.factorConditions(column.conditions);
		this.factoredConditions[column.name].children = {};
		if (!children || children.length === 0) {
			return;
		}
 
		children.forEach((child) => {
			const path = parentPath ? `${parentPath}.${child.name}` : child.name;
 
			if (child.conditions) {
				this.factoredConditions[column.name].children[path] = this.factorConditions(child.conditions);
			}
 
			if (child.children) {
				this.createConditions(column, child.children, path);
			}
		});
	}
 
	/**
	 * Apply conditions
	 * @param row Datasource row
	 */
	public applyCondition(
		row: IDictionary<any>,
		column: Column,
		factoredConditions: ConditionFunction = this.factoredConditions[column.name]?.root,
	) {
		const appliedConditions: Partial<IComponentRender> = {};
		Object.keys(factoredConditions || {}).forEach((condition: string) => {
			const factoredCondition = factoredConditions[condition];
 
			if (typeof factoredCondition !== 'function') {
				set(appliedConditions, condition, factoredCondition);
				return;
			}
 
			set(appliedConditions, condition, factoredCondition({ row, column }));
		});
		return appliedConditions;
	}
 
	/**
	 * Apply conditions on action columns
	 * @param row Datasource row
	 */
	private applyConditions(row: IDictionary<any>, column: Column): AppliedConditions {
		const appliedConditions: AppliedConditions = { root: this.applyCondition(row, column), children: {} };
		if (!column.children || column.children.length === 0) {
			return appliedConditions;
		}
 
		const childrenFactoredConditions = this.factoredConditions[column.name]?.children;
 
		Object.keys(childrenFactoredConditions || {}).forEach((action) => {
			const factoredConditions = childrenFactoredConditions[action];
			appliedConditions.children[action] = { ...this.applyCondition(row, column, factoredConditions) };
		});
		return appliedConditions;
	}
 
	public reapplyConditions(row: IDictionary<any>, columns: Column[]) {
		const rowKey = row[this.uniqueKey];
		const cellsApplied = { ...this.cellsApplied };
 
		columns.forEach((column: Column) => {
			if (!cellsApplied[rowKey]) cellsApplied[rowKey] = {};
 
			cellsApplied[rowKey][column.name] = this.applyConditions(row, column);
		});
 
		this.cellsApplied = cellsApplied;
 
		return cellsApplied;
	}
 
	public getAppliedConditions({
		row,
		column,
		path = '',
	}: { row: IDictionary; column: Column; path?: string }): Partial<IComponentRender> {
		const rowKey = row[this.uniqueKey];
		const rowConditions = this.cellsApplied[rowKey];
		const conditions = rowConditions ? rowConditions[column.name] : undefined;
 
		if (!conditions) return {};
 
		return path ? conditions.children[path] : conditions.root;
	}
}