@antv/s2
Version:
effective spreadsheet render core lib
160 lines • 5.45 kB
JavaScript
import { filter } from 'lodash';
import { EXTRA_FIELD, SERIES_NUMBER_FIELD, } from '../../common';
import { buildGridHierarchy } from './build-gird-hierarchy';
import { buildCustomTreeHierarchy } from './build-row-custom-tree-hierarchy';
import { buildRowTreeHierarchy } from './build-row-tree-hierarchy';
import { buildTableHierarchy } from './build-table-hierarchy';
import { Hierarchy } from './hierarchy';
const handleCustomTreeHierarchy = (params) => {
const { rootNode, hierarchy, fields, spreadsheet, isRowHeader } = params;
// 自定义行/列 需要去除额外添加的 EXTRA_FIELD 虚拟数值字段, 即不参与布局, 只用于定位数据
const withoutExtraFieldsTree = filter(fields, (field) => field !== EXTRA_FIELD);
// custom tree header
buildCustomTreeHierarchy({
spreadsheet,
tree: withoutExtraFieldsTree,
level: 0,
parentNode: rootNode,
hierarchy,
isRowHeader,
});
};
const handleGridRowColHierarchy = (params) => {
const { isValueInCols, moreThanOneValue, rootNode, hierarchy, fields, isRowHeader, isCustomTreeFields, spreadsheet, } = params;
// add new total measure in total node
let addTotalMeasureInTotal;
// add measure info in total query
let addMeasureInTotalQuery;
if (isRowHeader) {
addTotalMeasureInTotal = !isValueInCols && moreThanOneValue;
addMeasureInTotalQuery = !isValueInCols && !moreThanOneValue;
}
else {
addTotalMeasureInTotal = isValueInCols && moreThanOneValue;
addMeasureInTotalQuery = isValueInCols && !moreThanOneValue;
}
if (isCustomTreeFields) {
handleCustomTreeHierarchy(params);
}
else {
buildGridHierarchy({
spreadsheet,
addTotalMeasureInTotal,
addMeasureInTotalQuery,
parentNode: rootNode,
currentField: fields[0],
fields: fields,
hierarchy,
});
}
};
const handleTreeRowHierarchy = (params) => {
const { spreadsheet, rootNode, hierarchy, isCustomTreeFields } = params;
const { rows } = spreadsheet.dataSet.fields;
if (spreadsheet.isHierarchyTreeType() && !isCustomTreeFields) {
// row tree hierarchy(value must stay in colHeader)
buildRowTreeHierarchy({
level: 0,
currentField: rows === null || rows === void 0 ? void 0 : rows[0],
pivotMeta: spreadsheet.dataSet.rowPivotMeta,
parentNode: rootNode,
hierarchy,
spreadsheet,
});
}
else {
handleCustomTreeHierarchy(params);
}
};
const handleRowHeaderHierarchy = (params) => {
// 只有透视表有行头
const { spreadsheet } = params;
if (spreadsheet.isHierarchyTreeType()) {
handleTreeRowHierarchy(params);
}
else {
handleGridRowColHierarchy(params);
}
};
const handleTableHierarchy = (params) => {
var _a;
const { isCustomTreeFields, spreadsheet } = params;
const { enable } = (_a = spreadsheet.options.seriesNumber) !== null && _a !== void 0 ? _a : {};
if (isCustomTreeFields) {
const seriesNumberField = {
field: SERIES_NUMBER_FIELD,
title: spreadsheet.getSeriesNumberText(),
};
const fields = enable
? [seriesNumberField, ...params.fields]
: params.fields.filter((node) => (node === null || node === void 0 ? void 0 : node.field) !== SERIES_NUMBER_FIELD);
handleCustomTreeHierarchy(Object.assign(Object.assign({}, params), { fields }));
return;
}
buildTableHierarchy(params);
};
const handleColHeaderHierarchy = (params) => {
const { spreadsheet } = params;
const isPivotMode = spreadsheet.isPivotMode();
if (isPivotMode) {
handleGridRowColHierarchy(params);
}
else {
handleTableHierarchy(params);
}
};
/**
* Header Hierarchy
* - row header
* - tree layout
* - custom tree layout
* - grid layout
* - table layout
* - col header
* - grid layout
* - single value
* - total + sub_total
* - more than one value
* - total + sub_total
* - separate by values
* - table layout
* @param params
*/
export const buildHeaderHierarchy = (params) => {
const { isRowHeader, spreadsheet } = params;
const { rows = [], columns = [] } = spreadsheet.dataSet.fields;
const isValueInCols = spreadsheet.isValueInCols();
const moreThanOneValue = spreadsheet.dataSet.moreThanOneValue();
const hierarchy = new Hierarchy();
const fields = isRowHeader ? rows : columns;
const isCustomTreeFields = spreadsheet.isCustomHeaderFields(isRowHeader ? 'rows' : 'columns');
const headerParams = {
isValueInCols,
moreThanOneValue,
rootNode: hierarchy.rootNode,
hierarchy,
spreadsheet,
fields,
isRowHeader,
isCustomTreeFields,
};
if (isRowHeader) {
handleRowHeaderHierarchy(headerParams);
}
else {
handleColHeaderHierarchy(headerParams);
}
const getLeafNodes = () => {
if (!isRowHeader) {
return hierarchy.getLeaves();
}
return spreadsheet.isHierarchyTreeType()
? hierarchy.getNodes()
: hierarchy.getLeaves();
};
return {
hierarchy,
leafNodes: getLeafNodes(),
};
};
//# sourceMappingURL=build-header-hierarchy.js.map