All files / src/components/zd-grid-editable grid-editable-events.ts

100% Statements 32/32
100% Branches 13/13
100% Functions 8/8
100% Lines 31/31

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        93x           123x   123x     123x                   4x 3x 3x             4x       2x 2x   2x             2x                     5x 5x 1x 1x 1x 1x     4x       4x 4x 2x 2x 2x 2x     2x       4x   3x 3x                   4x                   2x                  
import { IDictionary } from '@zeedhi/core';
import { GridColumnEditable } from './grid-column-editable';
import { IGridEditable, IGridEditableEvents } from './interfaces';
 
export class GridEditableEvents implements IGridEditableEvents {
	protected gridEditable: IGridEditable;
 
	/**
	 * Flag to prevent row click
	 */
	protected preventRowClick = false;
 
	protected preventRowDoubleClick = false;
 
	constructor(gridEditable: IGridEditable) {
		this.gridEditable = gridEditable;
	}
 
	/**
	 * Dispatches row click event
	 * @param row Grid row
	 * @param event DOM event
	 * @param element DOM Element
	 */
	public rowClick(row: IDictionary<any>, event: Event, element: HTMLElement) {
		if (!this.preventRowClick) {
			this.gridEditable.setCurrentRow(row);
			this.gridEditable.callEvent('rowClick', {
				event,
				element,
				row,
				component: this.gridEditable,
			});
		}
		this.preventRowClick = false;
	}
 
	public rowDoubleClick(row: IDictionary<any>, event: Event, element: HTMLElement) {
		if (!this.preventRowDoubleClick) {
			this.gridEditable.setCurrentRow(row);
 
			this.gridEditable.callEvent('rowDoubleClick', {
				event,
				element,
				row,
				component: this,
			});
		}
		this.preventRowDoubleClick = false;
	}
 
	/**
	 * Dispatches cell click Event
	 * @param row Grid row
	 * @param column Grid column
	 * @param event DOM event
	 * @param element DOM Element
	 */
	public cellClick(row: IDictionary<any>, column: GridColumnEditable, event: Event, element: HTMLElement) {
		const canEdit = this.gridEditable.getCanEditRow(row) ?? true;
		if (column.editable && canEdit) {
			this.preventRowClick = true;
			this.gridEditable.setCurrentRow(row);
			this.inlineEdit(row, column, event, element);
			return;
		}
 
		this.cellClickEvent(row, column, event, element);
	}
 
	cellDoubleClick(row: IDictionary<any>, column: GridColumnEditable, event: Event, element: HTMLElement): void {
		const canEdit = this.gridEditable.getCanEditRow(row) ?? true;
		if (column.editable && canEdit) {
			this.preventRowDoubleClick = true;
			this.gridEditable.setCurrentRow(row);
			this.inlineEdit(row, column, event, element);
			return;
		}
 
		this.cellDoubleClickEvent(row, column, event, element);
	}
 
	inlineEdit(row: IDictionary, column: GridColumnEditable, event?: Event, element?: HTMLElement): void {
		if (this.gridEditable.editing) return;
 
		this.gridEditable.editing = true;
		this.gridEditable.callEvent('inlineEdit', {
			event,
			element,
			row,
			column,
			component: this.gridEditable,
		});
	}
 
	cellClickEvent(row: IDictionary<any>, column: GridColumnEditable, event: Event, element: HTMLElement): void {
		this.preventRowClick = this.gridEditable.callEvent('cellClick', {
			event,
			element,
			row,
			column,
			component: this.gridEditable,
		});
	}
 
	cellDoubleClickEvent(row: IDictionary<any>, column: GridColumnEditable, event: Event, element: HTMLElement): void {
		this.preventRowDoubleClick = this.gridEditable.callEvent('cellDoubleClick', {
			event,
			element,
			row,
			column,
			component: this.gridEditable,
		});
	}
}