All files / src/components/tek-grid grouped-view-navigator.ts

100% Statements 42/42
100% Branches 27/27
100% Functions 8/8
100% Lines 38/38

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    112x     112x           115x 115x       1x       1x       1x       16x   12x 12x 45x 9x   36x 36x 36x         11x 2x 2x   9x   9x   7x   7x   7x   7x 3x 3x     4x       8x 2x 2x   6x   6x   5x   5x   5x 2x 2x     3x      
import { IEventParam } from '@zeedhi/core';
import { IDictionary } from 'packages/core/dist/types';
import { Direction, IViewNavigator, ViewNavigator } from '../zd-grid';
import { ITekGrid } from './interfaces';
 
export class GroupedViewNavigator implements IViewNavigator {
	private viewNavigator: ViewNavigator;
 
	protected grid: ITekGrid;
 
	constructor(iterable: ITekGrid) {
		this.viewNavigator = new ViewNavigator();
		this.grid = iterable;
	}
 
	navigateLeft(param: IEventParam<any>): void {
		this.viewNavigator.navigateLeft(param);
	}
 
	navigateRight(param: IEventParam<any>): void {
		this.viewNavigator.navigateRight(param);
	}
 
	setViewNavigate(viewNavigate: (direction: Direction, event?: Event) => void): void {
		this.viewNavigator.setViewNavigate(viewNavigate);
	}
 
	private getRowIndex(groupedData: IDictionary[], index?: number) {
		if (index) return index;
 
		const { currentRow } = this.grid.datasource;
		return groupedData.findIndex((row) => {
			if (currentRow.group) {
				return row.group && row.groupValue === currentRow.groupValue;
			}
			const rowKey = this.grid.getRowKey(row);
			const currentRowKey = this.grid.getRowKey(currentRow);
			return rowKey && rowKey === currentRowKey;
		});
	}
 
	public navigateUp(index?: number) {
		if (!this.grid.isGrouped() || this.grid.cellSelection) {
			this.viewNavigator.navigateUp();
			return;
		}
		const groupedData = this.grid.getGroupedData();
 
		if (!groupedData.length || index === -1) return;
 
		let rowIndex: number = this.getRowIndex(groupedData, index);
 
		if (rowIndex === -1) rowIndex = groupedData.length;
 
		const newRow = groupedData[rowIndex - 1];
 
		if (!newRow || !this.grid.isItemVisible(newRow) || newRow.groupFooter) {
			this.navigateUp(rowIndex - 1);
			return;
		}
 
		this.grid.setCurrentRow(newRow);
	}
 
	public navigateDown(index?: number) {
		if (!this.grid.isGrouped() || this.grid.cellSelection) {
			this.viewNavigator.navigateDown();
			return;
		}
		const groupedData = this.grid.getGroupedData();
 
		if (!groupedData.length || index === groupedData.length) return;
 
		const rowIndex = this.getRowIndex(groupedData, index);
 
		const newRow = groupedData[rowIndex + 1];
 
		if (!newRow || !this.grid.isItemVisible(newRow) || newRow.groupFooter) {
			this.navigateDown(rowIndex + 1);
			return;
		}
 
		this.grid.setCurrentRow(newRow);
	}
}