import {GridFactory} from '../grid-cellrenderer/grid-factory'; import {IGridCellParams} from '../grid-cellrenderer/grid-cell-params'; import {Const} from '../utils/const'; export class AgGridComponentConst { // Key for cell type (must be used in data of grid item) public static CELL_KEY_TYPE = 'cellKeyType'; // Cell renderers public static CELL_RENDERER_CHECKBOX = 'gridCheckboxRenderer'; public static CELL_RENDERER_TEXT = 'gridTextRenderer'; public static CELL_RENDERER_DATE = 'gridDateRenderer'; public static CELL_RENDERER_INPUT = 'gridInputRenderer'; public static CELL_RENDERER_SELECT = 'gridSelectRenderer'; public static CELL_RENDERER_BLOCKED = 'gridBlockedRenderer'; public static CELL_RENDERER_DEFAULT = ''; // Cell editors public static CELL_EDITOR_CHECKBOX = 'gridCheckboxEditor'; public static CELL_EDITOR_TEXT = 'gridTextEditor'; public static CELL_EDITOR_DATE = 'gridDateEditor'; public static CELL_EDITOR_INPUT = 'gridInputEditor'; public static CELL_EDITOR_SELECT = 'gridSelectEditor'; public static CELL_EDITOR_BLOCKED = 'gridBlockedEditor'; public static CELL_EDITOR_DEFAULT = ''; // Cell types public static CELL_TYPE_DEFAULT = 'default'; public static CELL_TYPE_CHECKBOX = 'checkbox'; public static CELL_TYPE_TEXT = 'text'; public static CELL_TYPE_DATE = 'date'; public static CELL_TYPE_INPUT = 'input'; public static CELL_TYPE_SELECT = 'select'; public static CELL_TYPE_BLOCKED = 'blocked'; // Cell units public static CELL_UNIT_PERCENT = '%'; public static CELL_UNIT_EURO = '€'; public static CELL_UNIT_DAYS = ' Tage'; public static CELL_UNIT_SQUARE_METER: string = ' ' + Const.UNIT_SQUARE_METER; // Cell value types public static CELL_VALUE_TYPE_BOOL = 'bool'; public static CELL_VALUE_TYPE_INT = 'int'; public static CELL_VALUE_TYPE_FLOAT = 'float'; public static CELL_VALUE_TYPE_STRING = 'string'; public static validCellUnits: string[] = [ AgGridComponentConst.CELL_UNIT_PERCENT, AgGridComponentConst.CELL_UNIT_EURO, AgGridComponentConst.CELL_UNIT_DAYS, AgGridComponentConst.CELL_UNIT_SQUARE_METER, ]; public static validCellValueTypes: string[] = [ AgGridComponentConst.CELL_VALUE_TYPE_BOOL, AgGridComponentConst.CELL_VALUE_TYPE_INT, AgGridComponentConst.CELL_VALUE_TYPE_FLOAT, AgGridComponentConst.CELL_VALUE_TYPE_STRING, ]; public static gridInputError: string; /** * Sets input error */ public static setGridInputError(errorMessage: string = null): void { AgGridComponentConst.gridInputError = errorMessage; if (null !== errorMessage) { setTimeout(() => { AgGridComponentConst.setGridInputError(); }, 10); } } /** * Returns a cell renderer for given type with given parameters */ public static cellRendererSelector(params: any, gridCellParams: IGridCellParams = null): {} { if (null === gridCellParams) { gridCellParams = GridFactory.getGridCellParams(); } AgGridComponentConst.checkCellValueType(gridCellParams.type); AgGridComponentConst.checkCellUnit(gridCellParams.unit); switch (params.data[AgGridComponentConst.CELL_KEY_TYPE]) { case AgGridComponentConst.CELL_TYPE_CHECKBOX: return {component: AgGridComponentConst.CELL_RENDERER_CHECKBOX, params: gridCellParams}; case AgGridComponentConst.CELL_TYPE_TEXT: return {component: AgGridComponentConst.CELL_RENDERER_TEXT, params: gridCellParams}; case AgGridComponentConst.CELL_TYPE_INPUT: return {component: AgGridComponentConst.CELL_RENDERER_INPUT, params: gridCellParams}; case AgGridComponentConst.CELL_TYPE_DATE: return {component: AgGridComponentConst.CELL_RENDERER_DATE, params: gridCellParams}; case AgGridComponentConst.CELL_TYPE_SELECT: return {component: AgGridComponentConst.CELL_RENDERER_SELECT, params: gridCellParams}; case AgGridComponentConst.CELL_TYPE_BLOCKED: return {component: AgGridComponentConst.CELL_RENDERER_BLOCKED, params: gridCellParams}; case AgGridComponentConst.CELL_TYPE_DEFAULT: // Nothing specific, so ag grid will take it's own default cell renderer return; default: throw new Error('Unknown cell type'); } } /** * Returns a cell editor for given type with given parameters */ public static cellEditorSelector(params: any, gridCellParams: IGridCellParams = null) { if (null === gridCellParams) { gridCellParams = GridFactory.getGridCellParams(); } AgGridComponentConst.checkCellValueType(gridCellParams.type); AgGridComponentConst.checkCellUnit(gridCellParams.unit); switch (params.data[AgGridComponentConst.CELL_KEY_TYPE]) { case AgGridComponentConst.CELL_TYPE_CHECKBOX: // NOTE: Overwrite any given type for checkbox with boolean, since checkbox can only hold boolean values gridCellParams.type = AgGridComponentConst.CELL_VALUE_TYPE_BOOL; return {component: AgGridComponentConst.CELL_EDITOR_CHECKBOX, params: gridCellParams}; case AgGridComponentConst.CELL_TYPE_TEXT: return {component: AgGridComponentConst.CELL_EDITOR_TEXT, params: gridCellParams}; case AgGridComponentConst.CELL_TYPE_INPUT: return {component: AgGridComponentConst.CELL_EDITOR_INPUT, params: gridCellParams}; case AgGridComponentConst.CELL_TYPE_DATE: return {component: AgGridComponentConst.CELL_EDITOR_DATE, params: gridCellParams}; case AgGridComponentConst.CELL_TYPE_SELECT: return {component: AgGridComponentConst.CELL_EDITOR_SELECT, params: gridCellParams}; case AgGridComponentConst.CELL_TYPE_BLOCKED: return {component: AgGridComponentConst.CELL_EDITOR_BLOCKED, params: gridCellParams}; case AgGridComponentConst.CELL_TYPE_DEFAULT: // Nothing specific, so ag grid will take it's own default cell editor return; default: throw new Error('Unknown cell type'); } } /** * Checks if cell value type is valid * @param {string} type */ private static checkCellValueType(type: string = null): void { if (null !== type && AgGridComponentConst.validCellValueTypes.indexOf(type) < 0) { throw new Error('Unknown cell value type'); } } /** * Checks if cell unit is valid * @param {string} type */ private static checkCellUnit(type: string = null): void { if (null !== type && AgGridComponentConst.validCellUnits.indexOf(type) < 0) { throw new Error('Unknown cell value type'); } } }