All files / src/components/zd-grid iterable-table.ts

100% Statements 25/25
100% Branches 4/4
100% Functions 8/8
100% Lines 23/23

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  93x       93x             253x     253x               4x   4x 4x       3x 3x     253x     12x 1x 1x   1x   1x   11x       197x       4x 2x     4x 2x 2x 2x      
import { IDictionary } from '@zeedhi/core';
import { DeleteRowsError } from '../../error/delete-rows';
import { GridColumn } from './grid-column';
import { IGrid, IIterableTable } from './interfaces';
 
export class IterableTable implements IIterableTable {
	protected iterable: IGrid;
 
	/**
	 * Available order types
	 * @public
	 */
	public orderTypes: { asc: 'asc'; desc: 'desc' } = { asc: 'asc', desc: 'desc' };
 
	constructor(iterable: IGrid) {
		this.iterable = iterable;
	}
 
	/**
	 * Changes column order
	 * @async
	 */
	public async changeOrder(sortBy: { key: string; order: 'asc' | 'desc' }[]) {
		const order = sortBy.map((sort) => `${sort.key}.${sort.order}`);
 
		await this.iterable.setOrder(order);
		this.iterable.changeLayout();
	}
 
	public selectCell(row: IDictionary, column: GridColumn) {
		this.iterable.setCurrentRow(row);
		this.iterable.currentColumn = column;
	}
 
	protected rowStyleConditions = new Map();
 
	public reapplyRowStyleConditions(row: IDictionary<any>) {
		if (typeof this.iterable.rowStyleConditions === 'function') {
			const conditions = this.iterable.rowStyleConditions(row);
			const key = this.iterable.getRowKey(row);
 
			this.rowStyleConditions.set(key, conditions);
 
			return conditions;
		}
		return {};
	}
 
	public getRowStyleConditions(row: IDictionary): IDictionary {
		return this.rowStyleConditions.get(this.iterable.getRowKey(row)) || {};
	}
 
	public async deleteRows() {
		if (this.iterable.selectAllPages) {
			throw new DeleteRowsError();
		}
 
		const response = await Promise.all(this.iterable.selectedRows.map((row) => this.iterable.delete(row)));
		await this.iterable.reload();
		this.iterable.selectedRows = [];
		return response;
	}
}