@adaptabletools/adaptable
Version:
Powerful AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
812 lines (811 loc) • 35.3 kB
JavaScript
import { ColumnApiModule, } from 'ag-grid-enterprise';
import { ACTION_COLUMN_TYPE, CALCULATED_COLUMN_TYPE, FDC3_COLUMN_TYPE, FREE_TEXT_COLUMN_TYPE, } from '../AdaptableState/Common/AdaptableColumn';
import { ACCESS_LEVEL_HIDDEN, ADAPTABLE_FDC3_ACTION_COLUMN_FRIENDLY_NAME, } from '../Utilities/Constants/GeneralConstants';
import { createUuid } from '../AdaptableState/Uuid';
import ArrayExtensions from '../Utilities/Extensions/ArrayExtensions';
import * as ModuleConstants from '../Utilities/Constants/ModuleConstants';
import { agGridDataTypeDefinitions, ALL_ADAPTABLE_DATA_TYPES } from './agGridDataTypeDefinitions';
import { isPivotGrandTotal } from '../Api/Implementation/ColumnApiImpl';
import { isPivotColumnTotal } from '../layout-manager/src/isPivotColumnTotal';
import { isPivotAggTotalColumn } from '../layout-manager/src/isPivotAggTotalColumn';
import { ONLY_AGG_FN_NAME, WEIGHTED_AVERAGE_AGG_FN_NAME, } from '../AdaptableState/Common/AggregationColumns';
const DANGER_AG_GRID_BEANS_MAP = {};
const getColumnApiModule = () => ColumnApiModule;
export class AgGridAdapter {
_adaptableInstance;
DANGER_USE_GETTER_gridApi;
DANGER_gridApi_from_args;
DANGER_updateGridOptionsMonkeyPatcher;
DANGER_doFiltersPassMonkeyPatcher;
DANGER_isAggFilterPresentMonkeyPatcher;
DANGER_getDefaultFuncLabelMonkeyPatcher;
activePivotColumnFilters_MEMO = new WeakMap();
initialGridOptions;
_agGridId;
constructor(_adaptableInstance, config) {
this._adaptableInstance = _adaptableInstance;
const columnApiModuleReference = config?.getAgGridColumnApiModuleReference?.() ?? getColumnApiModule();
const ColumnDefFactory_Prototype = columnApiModuleReference?.beans?.[0]?.prototype;
if (!ColumnDefFactory_Prototype) {
console.error(`CRITICAL: could not get hold of AG Grid beans, this should never happen!`);
}
else {
const ColumnDefFactory_Prototye_preWireBeans = ColumnDefFactory_Prototype.preWireBeans;
ColumnDefFactory_Prototype.preWireBeans = function (beans) {
ColumnDefFactory_Prototye_preWireBeans?.apply(this, arguments);
const gridId = beans?.context?.getId();
if (!gridId) {
console.error('CRITICAL: No gridId found in beans, this should never happen!');
}
DANGER_AG_GRID_BEANS_MAP[gridId] = beans;
};
}
}
destroy() {
delete DANGER_AG_GRID_BEANS_MAP[this._agGridId];
this.initialGridOptions = null;
this.DANGER_gridApi_from_args = null;
this.DANGER_USE_GETTER_gridApi = null;
this.DANGER_updateGridOptionsMonkeyPatcher = null;
this.DANGER_doFiltersPassMonkeyPatcher = null;
this.DANGER_isAggFilterPresentMonkeyPatcher = null;
this.DANGER_getDefaultFuncLabelMonkeyPatcher = null;
this.activePivotColumnFilters_MEMO = null;
this._adaptableInstance = null;
}
get adaptableOptions() {
return this._adaptableInstance.adaptableOptions;
}
get adaptableApi() {
return this._adaptableInstance.api;
}
get logger() {
return this._adaptableInstance.logger;
}
getActivePivotColumnFilters() {
const layoutColumnFilters = this.adaptableApi.layoutApi.internalApi.getCurrentLayout_perfOptimized()?.ColumnFilters;
if (!layoutColumnFilters) {
return [];
}
const memoizedColumnFilters = this.activePivotColumnFilters_MEMO.get(layoutColumnFilters);
if (memoizedColumnFilters) {
return memoizedColumnFilters;
}
const pivotColumnFilters = this.adaptableApi.filterApi.columnFilterApi
.getActiveColumnFilters()
.filter((columnFilter) => this.adaptableApi.columnApi.isPivotResultColumn(columnFilter.ColumnId) ||
this.adaptableApi.columnApi.isAutoRowGroupColumnForSingle(columnFilter.ColumnId) ||
this.adaptableApi.columnApi.isPivotAggColumnWithNoPivotColumns(columnFilter.ColumnId))
.filter((columnFilter) => this.adaptableApi.filterApi.columnFilterApi.isColumnFilterActive(columnFilter))
.filter((columnFilter) => this.adaptableApi.expressionApi.internalApi.shouldEvaluatePredicatesInAdaptableQL('ColumnFilter', columnFilter, columnFilter.Predicates));
this.activePivotColumnFilters_MEMO.set(layoutColumnFilters, pivotColumnFilters);
return pivotColumnFilters;
}
get agGridOptionsService() {
return this._adaptableInstance.agGridOptionsService;
}
setAgGridId(agGridId) {
this._agGridId = agGridId;
}
setAgGridApi(gridApi) {
this.DANGER_USE_GETTER_gridApi = gridApi;
}
getAgGridApi(skipLogging) {
if (this.DANGER_USE_GETTER_gridApi) {
return this.DANGER_USE_GETTER_gridApi;
}
if (this.DANGER_gridApi_from_args) {
return this.DANGER_gridApi_from_args;
}
if (!skipLogging) {
console.error('AgGridApi is not available yet');
}
}
monkeyPatchingGridOptionsUpdates() {
const agGridOptionsService = this.DANGER_getPrivateAgGridBeans()?.gos;
if (!agGridOptionsService) {
return;
}
const agGridOptionsServicePrototype = Object.getPrototypeOf(agGridOptionsService);
const GridOptionsService_updateGridOptions = agGridOptionsServicePrototype.updateGridOptions;
const self = this;
this.DANGER_updateGridOptionsMonkeyPatcher = function ({ options, force, source = 'api', }) {
const passedColumnDefs = options.columnDefs;
if (passedColumnDefs) {
const colDefsWithSpecialColumns = self.getColumnDefinitionsInclSpecialColumns(passedColumnDefs);
const allDisplayedColIds = self
.getAgGridApi()
.getAllDisplayedColumns()
.map((col) => col.getColId());
self.patchColDefs(colDefsWithSpecialColumns, (colDef) => {
if (self.adaptableApi.columnApi.internalApi.isSpecialColumn(colDef.colId)) {
colDef.hide = !allDisplayedColIds.includes(colDef.colId);
}
});
if (self.adaptableApi.optionsApi.getFilterOptions().useAdaptableFiltering) {
const hasActiveFilters = self.adaptableApi.filterApi.columnFilterApi.getActiveColumnFilters().length;
if (hasActiveFilters) {
self.patchColDefs(colDefsWithSpecialColumns, (colDef) => {
if (colDef.filter !== false) {
const originalColDef = self.getAgGridApi().getColumnDef(colDef.colId);
if (originalColDef) {
colDef.filter = originalColDef.filter;
}
}
});
}
}
options['columnDefs'] = colDefsWithSpecialColumns;
self.logger.info(`Added special columns on GridOptions.columnDefs update (source="${source}").`);
}
const passedContext = options.context;
if (passedContext) {
passedContext['__adaptable'] = self._adaptableInstance;
passedContext['adaptableApi'] = self.adaptableApi;
}
GridOptionsService_updateGridOptions.apply(this, arguments);
};
agGridOptionsService.updateGridOptions = this.DANGER_updateGridOptionsMonkeyPatcher;
}
monkeyPatchingAggColumnFilters() {
if (!this.adaptableApi.optionsApi.getFilterOptions().useAdaptableFiltering) {
return;
}
const agGridColumnFilterService = this.DANGER_getPrivateAgGridBeans()?.colFilter;
if (!agGridColumnFilterService) {
this.logger.consoleError('Failed to initialize ColumnFilterService. Filtering and related features will not function correctly.');
return;
}
const self = this;
const original_doFilterPass = agGridColumnFilterService.doFiltersPass;
this.DANGER_doFiltersPassMonkeyPatcher = function (rowNode, colIdToSkip, targetAggregates) {
if (!targetAggregates) {
return original_doFilterPass.call(this, rowNode, colIdToSkip);
}
if (!self.adaptableApi.layoutApi.isCurrentLayoutPivot()) {
return true;
}
const pivotColumnFilters = self.getActivePivotColumnFilters();
try {
if (pivotColumnFilters.length > 0) {
for (const columnFilter of pivotColumnFilters) {
const columnFilterEvaluationResult = self.adaptableApi.filterApi.columnFilterApi.internalApi.evaluateColumnFilter(columnFilter, rowNode);
if (!columnFilterEvaluationResult) {
return false;
}
}
}
}
catch (ex) {
self.logger.error(ex);
return true;
}
return true;
};
agGridColumnFilterService.doFiltersPass = this.DANGER_doFiltersPassMonkeyPatcher;
this.DANGER_isAggFilterPresentMonkeyPatcher = function () {
return self.getActivePivotColumnFilters().length > 0;
};
agGridColumnFilterService.isAggFilterPresent = this.DANGER_isAggFilterPresentMonkeyPatcher;
}
monkeyPatchingAggFuncLabels() {
const agGridAggFuncService = this.DANGER_getPrivateAgGridBeans()?.aggFuncSvc;
if (!agGridAggFuncService) {
return;
}
const original_getDefaultFuncLabel = agGridAggFuncService.getDefaultFuncLabel;
if (typeof original_getDefaultFuncLabel !== 'function') {
return;
}
this.DANGER_getDefaultFuncLabelMonkeyPatcher = function (fctName) {
if (fctName === 'only') {
return 'Only';
}
if (fctName === 'weightedAvg') {
return 'Weighted Average';
}
return original_getDefaultFuncLabel.call(this, fctName);
};
agGridAggFuncService.getDefaultFuncLabel = this.DANGER_getDefaultFuncLabelMonkeyPatcher;
}
DANGER_getPrivateAgGridBeans() {
const beans = DANGER_AG_GRID_BEANS_MAP[this._agGridId];
if (!beans) {
this.logger.consoleError('Failed to access AG Grid internal beans. Adaptable will not function correctly.');
}
return beans;
}
DANGER_getCreateSparkline() {
const registry = this.DANGER_getPrivateAgGridBeans()?.registry;
const sparklineComponent = registry?.getUserComponent('agSparklineCellRenderer', 'agSparklineCellRenderer');
return sparklineComponent?.params?.createSparkline;
}
DANGER_getLiveGridOptions() {
return this.DANGER_getPrivateAgGridBeans()?.gridOptions;
}
getAgGridRootElement() {
return this.DANGER_getPrivateAgGridBeans()?.eGridDiv;
}
grabAgGridApiOnTheFly(args) {
if (this.DANGER_USE_GETTER_gridApi || this.DANGER_gridApi_from_args) {
return;
}
if (Array.isArray(args) &&
args[0] &&
typeof args[0].api === 'object' &&
typeof args[0].api?.getGridId === 'function') {
this.DANGER_gridApi_from_args = args[0].api;
}
}
updateGridOptions(options) {
if (this.getAgGridApi()?.isDestroyed()) {
return;
}
this.getAgGridApi()?.updateGridOptions(options);
}
getGridOption(key) {
if (this.getAgGridApi()?.isDestroyed()) {
return;
}
return this.getAgGridApi()?.getGridOption(key);
}
setGridOption(key, value) {
if (this.getAgGridApi()?.isDestroyed()) {
return;
}
this.getAgGridApi()?.setGridOption(key, value);
}
getUserGridOptionsProperty(propertyName) {
return this.agGridOptionsService.getUserGridOptionsProperty(propertyName);
}
getColumnDefinitionsInclSpecialColumns(agGridColDefs) {
const allColDefs = this.enhanceColDefsWithSpecialColumns(agGridColDefs ?? this.getAgGridApi().getColumnDefs());
return allColDefs;
}
enhanceColDefsWithSpecialColumns(agGridColDefs) {
this.assignColumnIdsToColDefs(agGridColDefs);
const specialColDefs = this.getSpecialColDefs();
const isSpecialColDef = (colDef) => {
const { type } = colDef;
const colTypes = Array.isArray(type) ? type : [type];
return colTypes.some((colType) => [
ACTION_COLUMN_TYPE,
CALCULATED_COLUMN_TYPE,
FREE_TEXT_COLUMN_TYPE,
FDC3_COLUMN_TYPE,
].includes(colType));
};
const processedSpecialColDefIds = [];
const mapColDefs = (colDefs) => {
return colDefs.map((colDef) => {
if (this._adaptableInstance.agGridColumnAdapter.isColGroupDef(colDef)) {
colDef.children = mapColDefs(colDef.children);
return colDef;
}
else {
if (!isSpecialColDef(colDef)) {
return { minWidth: 10, ...colDef };
}
const newlyCreatedSpecialColDef = specialColDefs.find((specialColDef) => specialColDef.colId === colDef.colId);
if (newlyCreatedSpecialColDef) {
processedSpecialColDefIds.push(colDef.colId);
const mergedColDef = {
minWidth: 10,
...colDef,
...newlyCreatedSpecialColDef,
};
return mergedColDef;
}
else {
return colDef;
}
}
});
};
let resultColDefs = mapColDefs(agGridColDefs);
specialColDefs.forEach((specialColDef) => {
if (!processedSpecialColDefIds.includes(specialColDef.colId)) {
resultColDefs.push(specialColDef);
}
});
resultColDefs = resultColDefs.filter((colDef) => {
if (isSpecialColDef(colDef)) {
return specialColDefs.some((specialColDef) => specialColDef.colId === colDef.colId);
}
return true;
});
return resultColDefs;
}
getSpecialColDefs() {
const specialColDefs = [
...this.adaptableApi.calculatedColumnApi.internalApi.getColDefsForCalculatedColumns(),
...this.adaptableApi.actionColumnApi.internalApi.getColDefsForActionColumns(),
...this.adaptableApi.freeTextColumnApi.internalApi.getColDefsForFreeTextColumns(),
...this.adaptableApi.fdc3Api.internalApi.getFdc3ActionColDefs(),
];
this.assignColumnIdsToColDefs(specialColDefs);
return specialColDefs;
}
deriveSelectedCellInfoFromAgGrid() {
const selected = this.getAgGridApi().getCellRanges();
const columns = [];
const gridCells = [];
selected.forEach((rangeSelection) => {
let shouldIncludeRange = true;
if (rangeSelection.startRow && rangeSelection.endRow) {
let isStartRowPin = rangeSelection.startRow.rowPinned != null;
let isEndRowPin = rangeSelection.endRow.rowPinned != null;
if (isStartRowPin) {
if (isEndRowPin) {
shouldIncludeRange = false;
}
this.logger.consoleWarn('Pinned rows cannot be selected in AG Grid.');
}
if (shouldIncludeRange) {
const y1 = Math.min(rangeSelection.startRow.rowIndex, rangeSelection.endRow.rowIndex);
const y2 = Math.max(rangeSelection.startRow.rowIndex, rangeSelection.endRow.rowIndex);
for (const column of rangeSelection.columns) {
if (column != null) {
const colId = column.getColId();
const selectedColumn = this.adaptableApi.columnApi.getColumnWithColumnId(colId);
if (selectedColumn && !columns.includes(selectedColumn)) {
columns.push(selectedColumn);
}
for (let rowIndex = y1; rowIndex <= y2; rowIndex++) {
const rowNode = this.getAgGridApi().getDisplayedRowAtIndex(rowIndex);
if (rowNode && !this.isPinnedRowNode(rowNode)) {
const selectedCell = this.adaptableApi.gridApi.getGridCellFromRowNode(rowNode, colId);
gridCells.push(selectedCell);
}
}
}
}
}
}
});
const selectedCellInfo = {
columns,
gridCells,
};
return selectedCellInfo;
}
deriveSelectedRowInfoFromAgGrid() {
const nodes = this.getAgGridApi().getSelectedNodes();
const selectedRows = [];
if (this._adaptableInstance.isInPivotMode()) {
return undefined;
}
if (ArrayExtensions.IsNotNullOrEmpty(nodes)) {
nodes.forEach((node) => {
const rowInfo = {
isMaster: !!(node.master != null && node.master == true),
isExpanded: !!(node.expanded != null && node.expanded == true),
isGroup: !!(node.group != null && node.group == true),
isSelected: true,
isDisplayed: node.displayed == true,
rowGroupLevel: node.level,
};
const gridRow = {
primaryKeyValue: this.adaptableApi.gridApi.getPrimaryKeyValueForRowNode(node),
rowData: node.data,
rowNode: node,
rowInfo,
};
selectedRows.push(gridRow);
});
}
return { gridRows: selectedRows };
}
isPinnedRowNode(rowNode) {
if (!rowNode) {
return false;
}
if (rowNode.isRowPinned()) {
return true;
}
return false;
}
createAdaptableColumnFromAgGridColumn(agGridColumn, colsToGroups) {
const colId = agGridColumn.getColId();
const colDef = agGridColumn.getColDef();
const { columnApi } = this.adaptableApi;
const ColumnId = colId;
const pkColumn = this.adaptableOptions.primaryKey;
const ColumnGroup = colsToGroups?.[ColumnId];
const isRealColumnGroup = ColumnGroup
? ColumnGroup.columnGroupId !== ColumnGroup.friendlyName
: false;
const isFdc3MainActionColumn = this.adaptableApi.fdc3Api.internalApi.isFdc3MainActionColumn(colId);
let friendlyName;
const isGeneratedRowGroupColumn = columnApi.isAutoRowGroupColumn(ColumnId);
const isGeneratedSelectionColumn = columnApi.isSelectionColumn(ColumnId);
const isGeneratedPivotResultColumn = columnApi.isPivotResultColumn(ColumnId) && !agGridColumn.isPrimary();
const colExists = columnApi.doesColumnExist(ColumnId) ||
isGeneratedRowGroupColumn ||
isGeneratedPivotResultColumn;
if (colExists) {
friendlyName = columnApi.getFriendlyNameForColumnId(ColumnId);
}
else {
const displayName = this.getAgGridApi().getDisplayNameForColumn(agGridColumn, 'header');
const columnFriendlyName = this.adaptableOptions.columnOptions.columnFriendlyName;
const customFriendlyName = typeof columnFriendlyName === 'function'
? columnFriendlyName({
colId: colId,
agColumn: agGridColumn,
columnGroup: isRealColumnGroup ? ColumnGroup : undefined,
displayName: displayName,
...this.adaptableApi.internalApi.buildBaseContext(),
})
: null;
friendlyName =
customFriendlyName ??
(isFdc3MainActionColumn ? ADAPTABLE_FDC3_ACTION_COLUMN_FRIENDLY_NAME : displayName);
if (this.adaptableOptions.columnOptions.addColumnGroupToColumnFriendlyName &&
ColumnGroup &&
ColumnGroup.columnGroupId !== ColumnGroup.friendlyName) {
friendlyName += ' [' + ColumnGroup.friendlyName + ']';
}
}
const dataType = isGeneratedPivotResultColumn
? 'number'
: this.deriveAdaptableColumnDataType(agGridColumn, false);
const isTreeColumn = this.isTreeColumn(isGeneratedRowGroupColumn);
const isUIHiddenColumn = this.adaptableApi.columnApi.internalApi.isColumnUIHidden(colDef);
const visible = !isUIHiddenColumn || agGridColumn.isVisible();
const isGenerated = isGeneratedRowGroupColumn || isGeneratedPivotResultColumn || isGeneratedSelectionColumn;
const abColumn = {
Uuid: createUuid(),
isTreeColumn,
columnId: ColumnId,
field: colDef.field,
resizable: colDef.resizable !== false,
friendlyName: friendlyName,
isPrimaryKey: ColumnId === pkColumn,
dataType,
visible,
isUIHiddenColumn,
readOnly: this.isColumnReadonly(colDef),
columnGroup: ColumnGroup,
fieldOnly: isGenerated ? false : this.isColumnFieldonly(colDef),
sortable: isGenerated ? false : this.isColumnSortable(colDef),
filterable: this.isColumnFilterable(colDef),
queryable: true,
exportable: true,
groupable: isGenerated ? false : this.isColumnRowGroupable(colDef),
pivotable: isGenerated ? false : this.isColumnPivotable(colDef),
aggregatable: isGenerated ? false : this.isColumnAggregetable(colDef),
availableAggregationFunctions: null,
aggregationFunction: null,
moveable: this.isColumnMoveable(colDef),
hideable: this.isColumnHideable(colDef),
isGrouped: isGenerated ? false : this.isColumnRowGrouped(colDef),
isGeneratedRowGroupColumn,
isGeneratedSelectionColumn,
isGeneratedPivotResultColumn,
isFixed: this.isColumnFixed(colDef),
pinned: this.getColumnPinnedPosition(colDef),
columnTypes: this.getColumnTypes(colDef),
isSparkline: this.isColumnSparkline(colDef),
isCalculatedColumn: this.isCalculatedColumn(colDef),
isFreeTextColumn: this.isFreeTextColumn(colDef),
isActionColumn: this.isActionColumn(colDef),
isPivotTotalColumn: this.isPivotTotalColumn(colDef),
};
abColumn.queryable = this.isColumnQueryable(abColumn);
abColumn.exportable = this.isColumnExportable(abColumn);
if (abColumn.aggregatable) {
abColumn.availableAggregationFunctions = this.getColumnAggregationFunctions(colDef);
Object.defineProperty(abColumn, 'userAllowedAggFuncs', {
get: () => this.getUserAllowedAggFuncs(ColumnId),
enumerable: true,
configurable: true,
});
if (typeof colDef.aggFunc === 'string') {
abColumn.aggregationFunction = colDef.aggFunc;
}
}
return abColumn;
}
getUserAllowedAggFuncs(columnId) {
const userColumnAllowed = this._adaptableInstance?.agGridColumnAdapter?.getUserColDefProperty(columnId, 'allowedAggFuncs');
const userDefaultColDef = this.getUserGridOptionsProperty('defaultColDef');
const userAllowed = userColumnAllowed ?? userDefaultColDef?.allowedAggFuncs;
return Array.isArray(userAllowed) ? [...userAllowed] : undefined;
}
deriveAdaptableColumnDataType(agColumn, logWarning = true) {
if (!agColumn) {
this.logger.warn('Column is undefined. Defaulting data type to "text".');
return 'text';
}
if (this.adaptableApi.columnApi.isAutoRowGroupColumnForSingle(agColumn.getId())) {
return 'groupColumn';
}
if (this.adaptableApi.columnApi.isSelectionColumn(agColumn.getId())) {
return 'unknown';
}
if (this.adaptableApi.columnApi.isAutoRowGroupColumnForMulti(agColumn.getId())) {
const rowGroupKey = agColumn.getColDef().showRowGroup;
if (typeof rowGroupKey !== 'string') {
return 'unknown';
}
const groupedColumn = this.adaptableApi.agGridApi.getColumn(rowGroupKey);
if (!groupedColumn) {
return 'unknown';
}
return this.deriveAdaptableColumnDataType(groupedColumn, logWarning);
}
const colDefType = [].concat(agColumn.getColDef()?.type || []).filter(Boolean);
const skippedSpecialCols = ['actionColumn', 'fdc3Column'];
if (skippedSpecialCols.some((specialColType) => colDefType.includes(specialColType))) {
return 'unknown';
}
let dataType = 'unknown';
const colDefDataType = agColumn.getColDef().cellDataType;
if (typeof colDefDataType === 'string' && ALL_ADAPTABLE_DATA_TYPES.includes(colDefDataType)) {
return colDefDataType;
}
let row = this.getAgGridApi().getDisplayedRowAtIndex(0);
if (row == null) {
this.logger.consoleError(`No data in grid. Returning type "unknown" for column "${agColumn.getColId()}". This will affect features such as Filters and Column Formats.`);
return 'unknown';
}
const value = this._agGridApi_getValue(agColumn, row);
switch (typeof value) {
case 'string':
dataType = 'text';
break;
case 'number':
dataType = 'number';
break;
case 'boolean':
dataType = 'boolean';
break;
case 'object':
dataType = 'object';
break;
}
if (value instanceof Date) {
dataType = 'date';
}
else if (Array.isArray(value)) {
const arrayDataType = ALL_ADAPTABLE_DATA_TYPES.find((arrayType) => {
const dataTypeDefinition = agGridDataTypeDefinitions[arrayType];
const dataTypeMatching = dataTypeDefinition?.dataTypeMatcher?.(value);
return dataTypeMatching;
});
if (arrayDataType) {
dataType = arrayDataType;
}
}
this.logger.consoleWarn(`No explicit type for column "${agColumn.getColId()}". Inferred type from first row value: "${dataType}".`);
return dataType;
}
isColumnReadonly(colDef) {
if (!colDef) {
return true;
}
if (typeof colDef.editable === 'function') {
return false;
}
return !colDef.editable;
}
isColumnFieldonly(colDef) {
if (colDef.hide == true && colDef.initialHide == true && colDef.lockVisible == true) {
return true;
}
return false;
}
isColumnSortable(colDef) {
if (colDef) {
return colDef.sortable ?? true;
}
return false;
}
isColumnRowGroupable(colDef) {
if (colDef && colDef.enableRowGroup != null) {
return colDef.enableRowGroup;
}
return false;
}
isColumnPivotable(colDef) {
if (colDef && colDef.enablePivot != null) {
return colDef.enablePivot;
}
return false;
}
isColumnAggregetable(colDef) {
if (colDef && colDef.enableValue != null) {
return colDef.enableValue;
}
return false;
}
getColumnAggregationFunctions(colDef) {
const defaultAggFuncs = ['sum', 'min', 'max', 'count', 'avg', 'first', 'last'];
const hasAllowedAggFuncs = Array.isArray(colDef.allowedAggFuncs);
let result = hasAllowedAggFuncs ? [...colDef.allowedAggFuncs] : [...defaultAggFuncs];
const gridOptionsAggFuncs = this.adaptableApi.agGridApi.getGridOption('aggFuncs') || {};
const gridOptionsAggFuncNames = Object.keys(gridOptionsAggFuncs);
if (!hasAllowedAggFuncs) {
const customAggFuncNames = gridOptionsAggFuncNames.filter((name) => name !== WEIGHTED_AVERAGE_AGG_FN_NAME && name !== ONLY_AGG_FN_NAME);
const avgIndex = result.indexOf('avg');
if (avgIndex >= 0) {
result.splice(avgIndex + 1, 0, WEIGHTED_AVERAGE_AGG_FN_NAME);
}
else {
result.push(WEIGHTED_AVERAGE_AGG_FN_NAME);
}
result.push(...customAggFuncNames);
result.push(ONLY_AGG_FN_NAME);
}
return [...new Set(result)];
}
isTreeColumn(isGeneratedRowGroupColumn) {
return this.adaptableApi.gridApi.isTreeDataGrid() ? isGeneratedRowGroupColumn : false;
}
isColumnMoveable(colDef) {
if (!colDef) {
return false;
}
if (colDef.suppressMovable != null && colDef.suppressMovable == true) {
return false;
}
if (this.isColumnFixed(colDef)) {
return false;
}
return true;
}
isColumnQueryable(abColumn) {
return (this.adaptableApi.expressionApi.isColumnQueryable(abColumn));
}
isColumnExportable(abColumn) {
return this.adaptableApi.exportApi.isColumnExportable(abColumn);
}
isColumnHideable(colDef) {
if (!colDef) {
return false;
}
if (this.adaptableApi.gridApi.isTreeDataGrid() &&
this.adaptableApi.columnApi.isAutoRowGroupColumn(colDef.colId)) {
return false;
}
if (this.adaptableApi.columnApi.isSelectionColumn(colDef.colId)) {
return false;
}
if (colDef.lockVisible != null && colDef.lockVisible == true) {
return false;
}
return true;
}
isCalculatedColumn(colDef) {
return (this.adaptableApi.calculatedColumnApi.getCalculatedColumnForColumnId(colDef.colId) != null);
}
isFreeTextColumn(colDef) {
return this.adaptableApi.freeTextColumnApi.getFreeTextColumnForColumnId(colDef.colId) != null;
}
isActionColumn(colDef) {
return this.adaptableApi.actionColumnApi.getActionColumnForColumnId(colDef.colId) != null;
}
isPivotTotalColumn(colDef) {
return (isPivotGrandTotal(colDef.colId) ||
isPivotColumnTotal(colDef.colId) ||
isPivotAggTotalColumn(colDef));
}
isColumnFilterable(colDef) {
if (this.adaptableApi.entitlementApi.getEntitlementAccessLevelForModule(ModuleConstants.ColumnFilterModuleId) == ACCESS_LEVEL_HIDDEN) {
return false;
}
return colDef != null && colDef.filter != null && colDef.filter != false;
}
getColumnTypes(colDef) {
if (!colDef.type) {
return [];
}
const allTypes = typeof colDef.type === 'string' ? [colDef.type] : colDef.type;
return allTypes;
}
getColumnPinnedPosition(colDef) {
return colDef.pinned
? colDef.pinned === 'left' || colDef.pinned === true
? 'left'
: 'right'
: false;
}
isColumnFixed(colDef) {
if (!colDef) {
return false;
}
if (colDef.lockPosition != null && colDef.lockPosition == true) {
return true;
}
if (colDef.lockPinned != null && colDef.lockPinned == true) {
return true;
}
return false;
}
isColumnRowGrouped(colDef) {
if (!colDef) {
return false;
}
if (colDef.rowGroup != null && colDef.rowGroup == true) {
return true;
}
if (colDef.rowGroupIndex != null) {
return true;
}
return false;
}
isColumnSparkline(colDef) {
return colDef?.cellRenderer === 'agSparklineCellRenderer';
}
isVisibleNode(rowNode) {
const foundNode = this.getAgGridApi()
?.getRenderedNodes()
.find((n) => n.id == rowNode.id);
return foundNode != null;
}
getFlattenedColDefs(colDefs = []) {
const flattenedColDefs = [];
colDefs.forEach((colDef) => {
if (colDef.children) {
flattenedColDefs.push(...this.getFlattenedColDefs(colDef.children));
}
else {
flattenedColDefs.push(colDef);
}
});
this.assignColumnIdsToColDefs(flattenedColDefs);
return flattenedColDefs;
}
assignColumnIdsToColDefs(colDefs = []) {
const assignColId = (colDef) => {
if (!colDef) {
return;
}
if (colDef.field && !colDef.colId) {
colDef.colId = colDef.field;
}
if (!colDef.colId) {
this.logger.warn('Column definition is missing colId. Provide either a "field" or "colId" property.', colDef);
}
};
this.patchColDefs(colDefs, assignColId);
}
patchColDefs(colDefs = [], patchFn) {
const applyPatch = (colDef) => {
if (!colDef) {
return;
}
if (!colDef.children) {
patchFn(colDef);
}
if (colDef.children) {
colDef.children.forEach((childColDef) => applyPatch(childColDef));
}
};
colDefs.forEach((colDef) => applyPatch(colDef));
}
traverseColDefs(colDefs, modifyFn) {
const applyModification = (colDef) => {
if ('children' in colDef) {
const updatedChildren = colDef.children.map(applyModification).filter(Boolean);
return { ...colDef, children: updatedChildren };
}
else {
return modifyFn(colDef);
}
};
return colDefs.map(applyModification).filter(Boolean);
}
getDefaultColumnDefinition() {
return this.getAgGridApi(true)?.getGridOption('defaultColDef') ?? {};
}
_agGridApi_getValue(colKey, rowNode, gridApi) {
gridApi = gridApi || this.getAgGridApi();
return gridApi.getCellValue({ colKey, rowNode });
}
_agGridApi_getFormattedValue(colKey, rowNode, gridApi) {
gridApi = gridApi || this.getAgGridApi();
return gridApi.getCellValue({ colKey, rowNode, useFormatter: true });
}
}