All files / src/components/zd-iterable column.ts

100% Statements 99/99
100% Branches 22/22
100% Functions 15/15
100% Lines 96/96

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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 24794x               94x 94x 94x           94x 438x   438x               438x       438x     438x   438x   438x   438x     438x     438x           438x     438x                       438x     438x             438x   438x   438x 1x           438x             438x 438x 438x 438x 438x 438x 438x 438x 438x 438x 438x 438x 438x 438x 438x 438x 438x 438x 438x 438x 37x         37x 37x   438x             1x             4x             2x 1x     1x       37x   37x 37x 33x               4x       8x 8x 4x     4x       19x   19x 2x   17x   19x 17x 10x 10x     7x   7x 4x 4x 4x   3x     6x             355x       2x 2x 2x 1x   1x 1x 1x     1x     2x 2x 2x   2x 2x 2x             7x 7x       2x 2x      
import {
	Datasource,
	DatasourceFactory,
	FormatterParserProvider,
	IDictionary,
	Mask,
	MethodNotAssignedError,
} from '@zeedhi/core';
import debounce from 'lodash.debounce';
import { NonInitializedError } from '../../error/non-initialized';
import { Component } from '../zd-component/component';
import { ColumnAlign, ColumnType, IColumn } from './interfaces';
 
/**
 * Base class for Column.
 */
export class Column extends Component implements IColumn {
	public align: ColumnAlign = 'left';
 
	public label = '';
 
	public width?: string;
 
	public maxWidth?: string;
 
	public minWidth?: string;
 
	public mask = '';
 
	public type?: ColumnType;
 
	public actionFixed = false;
 
	/* column is fixed */
	public fixed = false;
 
	public sortable = true;
 
	public loading = false;
 
	public componentProps: any = {};
 
	/** column style property */
	public style: IDictionary = {};
 
	/** Type of XLS */
	public xlsType = '';
 
	/* Stores Datasource created on ComponentProps */
	protected lookupDatasource?: Datasource;
 
	/* Stores lookup data to prevent unnecessary requests */
	public lookupData: any = {};
 
	/* Stores lookup data count (only to be reactive and force column update)  */
	public lookupDataCount = 0;
 
	/* Stores data to lookup (it is not initialized to not be reactive) */
	protected dataToLookup?: any[];
 
	/**
	 * Column overflow behaviour when column has max width
	 * 'ellipsis' - show ellipsis (...) at end
	 * 'hidden' - just crop value
	 * 'wrap' - show value using multiple lines
	 * <2..5> - show value using multiple lines with <number> max lines (line-clamp)
	 */
	public overflow: string | number = 'ellipsis';
 
	/* Conditional properties */
	public conditions: IDictionary = {};
 
	/**
	 * Method to get the current column width, assigned by view layer.
	 */
	private viewGetWidth?: () => number;
 
	public helperText = '';
 
	protected defaultData: IDictionary[] = [];
 
	private lookup: (lookupColumn: string) => void = () => {
		throw new NonInitializedError(this.constructor.name);
	};
 
	/**
	 * Defines if the column should store the componentProps datasource.data in memory
	 */
	public storeData = true;
 
	/**
	 * Creates a new Column.
	 * @param props Column properties
	 */
	constructor(props: IColumn) {
		super(props);
		this.align = this.getInitValue('align', props.align, this.align);
		this.label = this.getInitValue('label', props.label, this.label);
		this.width = this.getInitValue('width', props.width, this.width);
		this.maxWidth = this.getInitValue('maxWidth', props.maxWidth, this.maxWidth);
		this.minWidth = this.getInitValue('minWidth', props.minWidth, this.minWidth);
		this.mask = this.getInitValue('mask', props.mask, this.mask);
		this.overflow = this.getInitValue('overflow', props.overflow, this.overflow);
		this.type = this.getInitValue('type', props.type, this.type);
		this.actionFixed = this.getInitValue('actionFixed', props.actionFixed, this.actionFixed);
		this.fixed = this.getInitValue('fixed', props.fixed, this.fixed);
		this.sortable = this.getInitValue('sortable', props.sortable, this.sortable);
		this.loading = this.getInitValue('loading', props.loading, this.loading);
		this.style = this.getInitValue('style', props.style, this.style);
		this.componentProps = this.getInitValue('componentProps', props.componentProps, this.componentProps);
		this.xlsType = this.getInitValue('xlsType', props.xlsType, this.xlsType);
		this.conditions = this.getInitValue('conditions', props.conditions, this.conditions);
		this.helperText = this.getInitValue('helperText', props.helperText, this.helperText);
		this.storeData = this.getInitValue('storeData', props.storeData, this.storeData);
		if (this.componentProps.datasource) {
			this.lookupDatasource = DatasourceFactory.factory({
				...this.componentProps.datasource,
				lazyLoad: true,
				loadAll: true,
			});
			this.lookupDatasource?.initialize();
			this.defaultData = this.lookupDatasource?.data || this.defaultData;
		}
		this.createAccessors();
	}
 
	/**
	 * Sets view getWidth method.
	 */
	public setViewGetWidth(viewGetWidth: () => number) {
		this.viewGetWidth = viewGetWidth;
	}
 
	/**
	 * * Sets a filter to lookupData.
	 */
	public setLookupDataFilter(filter: IDictionary) {
		this.lookupDatasource?.setFilter(filter);
	}
 
	/**
	 * Gets the column width.
	 */
	public getWidth() {
		if (this.viewGetWidth) {
			return this.viewGetWidth();
		}
 
		throw new MethodNotAssignedError('viewGetWidth');
	}
 
	public formatterByRow(row: IDictionary<any>, cellProps?: IDictionary) {
		const value = row[this.name];
 
		const formatterFn = FormatterParserProvider.getFormatter(`column_${this.componentProps.component}`);
		if (formatterFn) {
			return formatterFn({
				column: this,
				value,
				row,
				componentProps: { ...this.componentProps, ...cellProps },
			});
		}
 
		return this.formatter(value, cellProps);
	}
 
	public formatter(value: any, cellProps?: IDictionary) {
		const formatterFn = FormatterParserProvider.getFormatter(this.componentProps.component);
		if (formatterFn) {
			return formatterFn(value, { ...this.componentProps, ...cellProps });
		}
 
		return value ? Mask.getValueWithMask(value, this.mask) : value;
	}
 
	public getLookupData(lookupColumn: string, value: any) {
		const emptyRow = {};
 
		if (this.lookupData[value]) {
			return this.lookupData[value];
		}
		this.lookupData[value] = emptyRow;
 
		const dataIndex = this.lookupDatasource?.data.findIndex((row) => row[lookupColumn] === value) || 0;
		if (dataIndex !== -1) {
			this.lookupData[value] = this.lookupDatasource?.data[dataIndex];
			return this.lookupData[value];
		}
 
		const hasLookupColumn = this.lookupDatasource?.data.every((row) => Reflect.has(row, lookupColumn));
 
		if (hasLookupColumn) {
			this.dataToLookup = this.dataToLookup || [];
			this.dataToLookup.push(value);
			this.lookup(lookupColumn);
		} else {
			console.warn(`Row datasource do not have a key: ${lookupColumn}`);
		}
 
		return emptyRow;
	}
 
	/**
	 * Initialize the lookup property, properly setting the debounced function
	 */
	public initialize() {
		this.lookup = debounce(this.lookupFn, 100);
	}
 
	protected async lookupFn(lookupColumn: string, dataToLookup?: any[]) {
		let data: IDictionary[] = [];
		this.loading = true;
		if (this.storeData) {
			data = await this.lookupDatasource!.get();
		} else {
			const filteredData = Array.from(new Set(dataToLookup || this.dataToLookup)); // remove duplicates
			if (!dataToLookup) {
				this.dataToLookup = [];
			}
 
			data = await this.lookupDatasource!.addFilter(lookupColumn, filteredData);
		}
 
		const lookupData: IDictionary = {};
		data.forEach((row) => {
			lookupData[row[lookupColumn]] = row;
		});
		this.lookupData = { ...this.lookupData, ...lookupData };
		this.lookupDataCount += 1;
		this.loading = false;
	}
 
	/**
	 * Clear lookupData to force new requests
	 */
	public clearLookupData() {
		this.lookupData = {};
		if (this.componentProps.datasource) this.componentProps.datasource.data = this.defaultData;
	}
 
	public onBeforeDestroy() {
		super.onBeforeDestroy();
		this.lookupDatasource?.destroy();
	}
}