/
corner.ts
397 lines (378 loc) · 11 KB
/
corner.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import { SimpleBBox, Group, Point } from '@antv/g-canvas';
import { get, last, includes, isEmpty } from 'lodash';
import { GuiIcon } from '../../common/icons';
import { i18n } from '../../common/i18n';
import { DetailCornerCell } from '../../cell';
import {
ICON_RADIUS,
KEY_SERIES_NUMBER_NODE,
KEY_GROUP_CORNER_RESIZER,
COLOR_DEFAULT_RESIZER,
} from '../../common/constant';
import { BaseDataSet } from '../../data-set';
import {
SpreadSheet,
Hierarchy,
Node,
CornerCell,
KEY_TREE_ROWS_COLLAPSE_ALL,
} from '../../index';
import {
LayoutResult,
S2Options,
SpreadSheetFacetCfg,
} from '../../common/interface';
import { BaseHeader, BaseHeaderConfig, HIT_AREA } from './base';
import { CornerData, ResizeInfo } from './interface';
import { translateGroup } from '../utils';
export interface CornerHeaderConfig extends BaseHeaderConfig {
// header's hierarchy type
hierarchyType: S2Options['hierarchyType'];
// the hierarchy collapse or not
hierarchyCollapse: boolean;
// column fields
cols: string[];
// series number width
seriesNumberWidth: number;
}
/**
* Corner Header for SpreadSheet
*/
export class CornerHeader extends BaseHeader<CornerHeaderConfig> {
/**
* Get corner Header by config
* @param viewportBBox
* @param cornerBBox
* @param seriesNumberWidth
* @param cfg
* @param layoutResult
* @param ss spreadsheet
*/
public static getCornerHeader(
viewportBBox: SimpleBBox,
cornerBBox: SimpleBBox,
seriesNumberWidth: number,
cfg: SpreadSheetFacetCfg,
layoutResult: LayoutResult,
ss: SpreadSheet,
) {
const { width, height } = viewportBBox;
const cornerWidth = cornerBBox.width;
const cornerHeight = cornerBBox.height;
const cornerNodes: Node[] = this.getCornerNodes(
{ x: cornerBBox.x, y: cornerBBox.y },
cornerWidth,
cornerHeight,
cfg.rows,
layoutResult.rowsHierarchy,
layoutResult.colsHierarchy,
cfg.dataSet,
seriesNumberWidth,
ss,
);
return new CornerHeader({
data: cornerNodes,
position: { x: cornerBBox.x, y: cornerBBox.y },
width: cornerWidth,
height: cornerHeight,
viewportWidth: width,
viewportHeight: height,
offset: 0,
hierarchyType: cfg.hierarchyType, // 是否为树状布局
hierarchyCollapse: cfg.hierarchyCollapse,
cols: cfg.cols,
seriesNumberWidth,
spreadsheet: ss,
});
}
public static getCornerNodes(
position: Point,
width: number,
height: number,
rows: string[],
rowsHierarchy: Hierarchy,
colsHierarchy: Hierarchy,
dataSet: BaseDataSet,
seriesNumberWidth: number,
ss: SpreadSheet,
): Node[] {
const cornerNodes: Node[] = [];
const isPivotMode = ss.isPivotMode();
// check if show series number node
if (seriesNumberWidth) {
// 1、spreadsheet must have at least one node in last level
// 2、listSheet don't have other conditions
if (
(isPivotMode && colsHierarchy?.sampleNodeForLastLevel) ||
!isPivotMode
) {
const sNode: Node = new Node({
key: KEY_SERIES_NUMBER_NODE, // mark series node
id: '',
value: i18n('序号'),
});
sNode.x = position?.x;
// different type different y
sNode.y = isPivotMode
? colsHierarchy?.sampleNodeForLastLevel?.y
: position?.y;
sNode.width = seriesNumberWidth;
// different type different height
sNode.height = isPivotMode
? colsHierarchy?.sampleNodeForLastLevel?.height
: height;
sNode.isPivotMode = isPivotMode;
cornerNodes.push(sNode);
}
}
// spreadsheet type tree mode
if (isPivotMode && ss.isHierarchyTreeType()) {
if (get(colsHierarchy, 'sampleNodeForLastLevel', undefined)) {
const drillDownFieldInLevel = ss.store.get('drillDownFieldInLevel', []);
const drillFields = drillDownFieldInLevel.map((d) => d.drillField);
const cNode: Node = new Node({
key: '',
id: '',
// 角头过滤下钻的维度
value: rows
.filter((value) => !includes(drillFields, value))
.map((key: string): string => dataSet.getFieldName(key))
.join('/'),
});
cNode.x = position.x + seriesNumberWidth;
cNode.y = colsHierarchy?.sampleNodeForLastLevel?.y;
cNode.width = width;
cNode.height = colsHierarchy?.sampleNodeForLastLevel?.height;
cNode.seriesNumberWidth = seriesNumberWidth;
cNode.isPivotMode = isPivotMode;
cornerNodes.push(cNode);
}
} else {
// spreadsheet type
// eslint-disable-next-line no-lonely-if
if (
isPivotMode &&
get(colsHierarchy, 'sampleNodeForLastLevel', undefined)
) {
rowsHierarchy.sampleNodesForAllLevels.forEach((rowNode) => {
const field = rows[rowNode.level];
const cNode: Node = new Node({
key: field,
id: '',
value: dataSet.getFieldName(field),
});
cNode.x = rowNode.x + seriesNumberWidth;
cNode.y = colsHierarchy.sampleNodeForLastLevel.y;
cNode.width = rowNode.width;
cNode.height = colsHierarchy.sampleNodeForLastLevel.height;
cNode.field = field;
cNode.isPivotMode = isPivotMode;
cNode.spreadsheet = ss;
cornerNodes.push(cNode);
});
} else {
// detail type
rowsHierarchy.sampleNodesForAllLevels.forEach((rowNode) => {
const field = rows[rowNode.level];
const cNode: Node = new Node({
key: field,
id: '',
value: dataSet.getFieldName(field),
});
cNode.x = rowNode.x + seriesNumberWidth;
cNode.y = position.y;
cNode.width = rowNode.width;
cNode.height = height;
cNode.field = field;
cNode.isPivotMode = isPivotMode;
cNode.spreadsheet = ss;
cornerNodes.push(cNode);
});
}
}
return cornerNodes;
}
constructor(cfg: CornerHeaderConfig) {
super(cfg);
}
/**
* Make cornerHeader scroll with hScrollBar
* @param scrollX
*/
public onCorScroll(scrollX: number, type: string): void {
this.headerConfig.scrollX = scrollX;
this.render(type);
}
public destroy(): void {
super.destroy();
}
protected layout() {
this.startRender();
this.handleHotsSpotArea();
}
protected startRender() {
const { data, spreadsheet } = this.headerConfig;
const cornerHeader = spreadsheet?.facet?.cfg?.cornerHeader;
const cornerCell = spreadsheet?.facet?.cfg?.cornerCell;
if (cornerHeader) {
cornerHeader(this, spreadsheet, this.headerConfig);
return;
}
// 背景
this.addBgRect();
this.addIcon();
data.forEach((item: Node) => {
let cell: Group;
if (cornerCell) {
cell = cornerCell(
item,
this.headerConfig.spreadsheet,
this.headerConfig,
);
}
if (isEmpty(cell)) {
if (spreadsheet.isPivotMode()) {
cell = new CornerCell(
item,
this.headerConfig.spreadsheet,
this.headerConfig,
);
} else {
cell = new DetailCornerCell(
item,
this.headerConfig.spreadsheet,
this.headerConfig,
);
}
}
this.add(cell);
});
}
protected offset() {
const { scrollX } = this.headerConfig;
translateGroup(this, -scrollX, 0);
}
protected clip(): void {
const cfg = this.headerConfig;
const { width, height, scrollX } = cfg;
this.setClip({
type: 'rect',
attrs: {
x: scrollX,
y: 0,
width,
height,
},
});
}
protected addBgRect() {
const cfg = this.headerConfig;
const { width, height, position } = cfg;
this.addShape('rect', {
attrs: {
x: position.x,
y: position.y,
width,
height,
fill: get(
this.headerConfig,
'spreadsheet.theme.header.cell.cornerBackgroundColor',
),
stroke: 'transparent',
},
});
}
/**
* 批量折叠或者展开,只有在树状结构有
*/
protected addIcon() {
if (
this.headerConfig.spreadsheet.isHierarchyTreeType() &&
this.headerConfig.spreadsheet.isPivotMode()
) {
// 只有交叉表才有icon
const { hierarchyCollapse, position, height, spreadsheet } =
this.headerConfig;
const colHeight = spreadsheet.options.style.colCfg.height;
const icon = new GuiIcon({
type: hierarchyCollapse ? 'plus' : 'MinusSquare',
x: position.x,
y: height - colHeight / 2 - ICON_RADIUS,
width: ICON_RADIUS * 2,
height: ICON_RADIUS * 2,
});
icon.on('click', () => {
this.headerConfig.spreadsheet.store.set('scrollY', 0);
this.headerConfig.spreadsheet.emit(
KEY_TREE_ROWS_COLLAPSE_ALL,
hierarchyCollapse,
);
});
// this.gm = new GM(icon, {
// gestures: ['Tap'],
// });
// this.gm.on('tap', () => {
// spreadsheet.emit('spreadsheet:collapsedRows', { id, isCollapsed: !isCollapsed });
// });
this.add(icon);
}
}
private handleHotsSpotArea() {
const { data, position, width, height, seriesNumberWidth } =
this.headerConfig;
const prevResizer = this.headerConfig.spreadsheet.foregroundGroup.findById(
KEY_GROUP_CORNER_RESIZER,
);
const resizer = (prevResizer ||
this.headerConfig.spreadsheet.foregroundGroup.addGroup({
id: KEY_GROUP_CORNER_RESIZER,
})) as Group;
const treeType = this.headerConfig.spreadsheet.isHierarchyTreeType();
if (!treeType) {
// do it in corner cell
} else if (treeType) {
resizer.addShape('rect', {
attrs: {
x: position.x + width - HIT_AREA / 2,
y: position.y,
width: HIT_AREA,
height: this.get('viewportHeight') + height,
fill: COLOR_DEFAULT_RESIZER,
cursor: 'col-resize',
appendInfo: {
isResizer: true,
class: 'resize-trigger',
type: 'col',
affect: 'tree',
offsetX: position.x + seriesNumberWidth,
offsetY: position.y,
width: width - seriesNumberWidth,
height,
} as ResizeInfo,
},
});
}
const cell: CornerData = get(data, '0', {});
resizer.addShape('rect', {
attrs: {
x: position.x,
y: position.y + cell.y + cell.height - HIT_AREA / 2,
width,
height: HIT_AREA,
fill: COLOR_DEFAULT_RESIZER,
cursor: 'row-resize',
appendInfo: {
isResizer: true,
class: 'resize-trigger',
type: 'row',
affect: 'field',
id: last(this.get('cols')),
offsetX: position.x,
offsetY: position.y + cell.y,
width: cell.width,
height: cell.height,
} as ResizeInfo,
},
});
}
}