/
build-header-hierarchy.ts
157 lines (149 loc) · 4.24 KB
/
build-header-hierarchy.ts
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import {
BuildHeaderParams,
BuildHeaderResult,
} from 'src/facet/layout/interface';
import { Hierarchy } from 'src/facet/layout/hierarchy';
import { Node } from 'src/facet/layout/node';
import { buildRowTreeHierarchy } from 'src/facet/layout/build-row-tree-hierarchy';
import { buildGridHierarchy } from 'src/facet/layout/build-gird-hierarchy';
import { PivotDataSet } from '@/data-set';
import { buildRowCustomTreeHierarchy } from '@/facet/layout/build-row-custom-tree-hierarchy';
import { SpreadSheet } from '@/sheet-type';
import { SpreadSheetFacetCfg } from '@/common/interface';
interface HeaderParams {
isValueInCols: boolean;
isPivotMode: boolean;
moreThanOneValue: boolean;
hierarchy: Hierarchy;
rootNode: Node;
spreadsheet: SpreadSheet;
facetCfg: SpreadSheetFacetCfg;
fields: string[];
}
const handleGridRowColHierarchy = (params: HeaderParams) => {
const {
isValueInCols,
moreThanOneValue,
rootNode,
facetCfg,
hierarchy,
fields,
} = params;
// row grid hierarchy
const addTotalMeasureInTotal = !isValueInCols && moreThanOneValue;
// value in rows and only has one value(or none)
const addMeasureInTotalQuery = !isValueInCols && !moreThanOneValue;
buildGridHierarchy({
addTotalMeasureInTotal,
addMeasureInTotalQuery,
parentNode: rootNode,
currentField: fields[0],
fields,
facetCfg,
hierarchy,
});
};
const handleCustomTreeRowHierarchy = (params: HeaderParams) => {
const { facetCfg, rootNode, hierarchy } = params;
const customTreeItems = facetCfg.dataSet.fields.customTreeItems;
// row custom tree header
buildRowCustomTreeHierarchy({
customTreeItems: customTreeItems,
facetCfg,
level: 0,
parentNode: rootNode,
hierarchy,
});
};
const handleTreeRowHierarchy = (params: HeaderParams) => {
const { facetCfg, rootNode, hierarchy } = params;
const { hierarchyType, rows, dataSet } = facetCfg;
if (hierarchyType === 'tree') {
// row tree hierarchy(value must stay in colHeader)
buildRowTreeHierarchy({
level: 0,
currentField: rows[0],
pivotMeta: (dataSet as PivotDataSet).rowPivotMeta,
facetCfg,
parentNode: rootNode,
hierarchy,
});
} else {
handleCustomTreeRowHierarchy(params);
}
};
const handleRowHeaderHierarchy = (params: HeaderParams) => {
const { isPivotMode, spreadsheet } = params;
if (isPivotMode) {
if (spreadsheet.isHierarchyTreeType()) {
handleTreeRowHierarchy(params);
} else {
handleGridRowColHierarchy(params);
}
} else {
// TODO 光生哥看下是否需要更改
throw new Error('There are not header hierarchy in table mode');
}
};
const handleColHeaderHierarchy = (params: HeaderParams) => {
const { isPivotMode } = params;
if (isPivotMode) {
handleGridRowColHierarchy(params);
} else {
// TODO 光生哥看下是否需要更改
throw new Error('There are not header hierarchy in table mode');
}
};
/**
* 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: BuildHeaderParams,
): BuildHeaderResult => {
const { isRowHeader, facetCfg } = params;
const { values, spreadsheet, rows, cols } = facetCfg;
const isValueInCols = spreadsheet.dataCfg.fields.valueInCols;
const isPivotMode = spreadsheet.isPivotMode();
const moreThanOneValue = values.length > 1;
const rootNode = Node.rootNode();
const hierarchy = new Hierarchy();
const headParams = {
isValueInCols,
isPivotMode,
moreThanOneValue,
rootNode,
hierarchy,
spreadsheet,
facetCfg,
fields: isRowHeader ? rows : cols,
} as HeaderParams;
if (isRowHeader) {
handleRowHeaderHierarchy(headParams);
} else {
handleColHeaderHierarchy(headParams);
}
const getLeafNodes = () => {
if (!isRowHeader) return hierarchy.getLeaves();
return spreadsheet.isHierarchyTreeType()
? hierarchy.getNodes()
: hierarchy.getLeaves();
};
return {
hierarchy,
leafNodes: getLeafNodes(),
} as BuildHeaderResult;
};