@adaptabletools/adaptable
Version:
Powerful AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
106 lines (105 loc) • 3.51 kB
JavaScript
import { WEIGHTED_AVERAGE_AGG_FN_NAME } from '../../../AdaptableState/Common/AggregationColumns';
const IGNORED_AGG_COLUMN_IDS = new Set(['Source', 'Uuid', 'AdaptableVersion']);
const GRAND_TOTAL_ROW_LABELS = {
off: 'None',
top: 'Top',
bottom: 'Bottom',
pinnedTop: 'Pinned Top',
pinnedBottom: 'Pinned Bottom',
};
const PIVOT_LAYOUT_TOTAL_LABELS = {
off: 'None',
before: 'Before',
after: 'After',
};
function getGrandTotalRowKey(layout) {
const value = layout.GrandTotalRow;
if (value == null || value === false) {
return 'off';
}
return value;
}
function getGrandTotalRowLabel(layout) {
return GRAND_TOTAL_ROW_LABELS[getGrandTotalRowKey(layout)];
}
function getPivotLayoutTotalKey(value) {
if (value == null || value === false) {
return 'off';
}
if (value === 'before') {
return 'before';
}
if (value === 'after') {
return 'after';
}
return 'off';
}
function getPivotLayoutTotalLabel(value) {
return PIVOT_LAYOUT_TOTAL_LABELS[getPivotLayoutTotalKey(value)];
}
function formatAggregationTag(columnId, aggFn, columnIdToFriendlyName) {
if (IGNORED_AGG_COLUMN_IDS.has(columnId)) {
return null;
}
let aggFnName = '';
if (typeof aggFn === 'string') {
aggFnName = aggFn;
}
else if (typeof aggFn === 'object' && aggFn?.type === 'weightedAverage') {
aggFnName = WEIGHTED_AVERAGE_AGG_FN_NAME;
}
if (!aggFnName) {
return null;
}
return `${aggFnName}(${columnIdToFriendlyName(columnId)})`;
}
export function getTableAggregationSummaryValues(layout, api) {
const columnIdToFriendlyName = (columnId) => api.columnApi.getFriendlyNameForColumnId(columnId);
const values = (layout.TableAggregationColumns || [])
.map(({ ColumnId, AggFunc }) => formatAggregationTag(ColumnId, AggFunc, columnIdToFriendlyName))
.filter((value) => Boolean(value));
if (getGrandTotalRowKey(layout) !== 'off') {
values.push(`Grand Total Row: ${getGrandTotalRowLabel(layout)}`);
}
return values;
}
export function getPivotAggregationSummaryValues(layout, api) {
const columnIdToFriendlyName = (columnId) => api.columnApi.getFriendlyNameForColumnId(columnId);
const values = (layout.PivotAggregationColumns || [])
.map(({ ColumnId, AggFunc }) => formatAggregationTag(ColumnId, AggFunc, columnIdToFriendlyName))
.filter((value) => Boolean(value));
if (getGrandTotalRowKey(layout) !== 'off') {
values.push(`Grand Total Row: ${getGrandTotalRowLabel(layout)}`);
}
if (getPivotLayoutTotalKey(layout.PivotGrandTotal) !== 'off') {
values.push(`Pivot Grand Total: ${getPivotLayoutTotalLabel(layout.PivotGrandTotal)}`);
}
if (getPivotLayoutTotalKey(layout.PivotColumnTotal) !== 'off') {
values.push(`Pivot Column Total: ${getPivotLayoutTotalLabel(layout.PivotColumnTotal)}`);
}
return values;
}
export function getLayoutTableAggregationViewItems(layout, api) {
const values = getTableAggregationSummaryValues(layout, api);
if (!values.length) {
return [];
}
return [
{
name: 'Aggregations',
values,
},
];
}
export function getLayoutPivotAggregationViewItems(layout, api) {
const values = getPivotAggregationSummaryValues(layout, api);
if (!values.length) {
return [];
}
return [
{
name: 'Aggregations',
values,
},
];
}