/
row-cell.ts
414 lines (390 loc) · 11.6 KB
/
row-cell.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import {
CellTypes,
COLOR_DEFAULT_RESIZER,
ID_SEPARATOR,
KEY_COLLAPSE_TREE_ROWS,
KEY_GROUP_ROW_RESIZER,
} from '@/common/constant';
import { GuiIcon } from '@/common/icons';
import { HIT_AREA } from '@/facet/header/base';
import { ResizeInfo } from '@/facet/header/interface';
import { RowHeaderConfig } from '@/facet/header/row';
import { renderLine, renderRect, renderText } from '@/utils/g-renders';
import { getAllChildrenNodeHeight } from '@/utils/get-all-children-node-height';
import { isMobile } from '@/utils/is-mobile';
import { getAdjustPosition } from '@/utils/text-absorption';
import { IGroup } from '@antv/g-canvas';
import { GM } from '@antv/g-gesture';
import { get } from 'lodash';
import { getEllipsisText, measureTextWidth } from '../utils/text';
import { HeaderCell } from './header-cell';
export class RowCell extends HeaderCell {
protected headerConfig: RowHeaderConfig;
// 绘制完其他后,需要额外绘制的起始x坐标
protected lastStartDrawX: number;
protected actionIcons: GuiIcon[];
// TODO type define
// mobile event
private gm: GM;
public get cellType() {
return CellTypes.ROW_CELL;
}
public destroy(): void {
super.destroy();
this.gm?.destroy();
}
protected initCell() {
// 1、draw rect background
this.drawBackgroundColor();
this.drawInteractiveBgShape();
// draw text
this.lastStartDrawX = this.drawCellText();
// draw icon
this.drawIconInTree();
// draw bottom border
this.drawRectBorder();
// draw hot-spot rect
this.drawHotSpotInLeaf();
// draw action icon shapes: trend icon, drill-down icon ...
this.drawActionIcons();
}
protected drawBackgroundColor() {
const { rowCell: rowHeaderStyle } = this.spreadsheet.theme;
const bgColor = rowHeaderStyle.cell.backgroundColor;
const { x, y, height, width } = this.meta;
this.backgroundShape = renderRect(this, {
x,
y,
width,
height,
fill: bgColor,
stroke: 'transparent',
opacity: rowHeaderStyle.cell.backgroundColorOpacity,
});
}
// 交互使用的背景色
protected drawInteractiveBgShape() {
const { x, y, height, width } = this.meta;
this.stateShapes.set(
'interactiveBgShape',
renderRect(this, {
x,
y,
width,
height,
fill: 'transparent',
stroke: 'transparent',
}),
);
}
protected drawIconInTree() {
if (this.isTreeType() && !this.meta.isLeaf) {
const { offset, height } = this.headerConfig;
const {
x,
y,
height: cellHeight,
isCollapsed,
id,
hierarchy,
level,
} = this.meta;
const {
text: textCfg,
icon: iconCfg,
cell: cellCfg,
} = this.theme.rowCell;
const textIndent = this.getTextIndent();
const textY = getAdjustPosition(
y,
cellHeight,
offset,
height,
textCfg.fontSize,
);
const iconX = x + textIndent;
const iconY = textY + (textCfg.fontSize - iconCfg.size) / 2;
const icon = new GuiIcon({
type: isCollapsed ? 'plus' : 'MinusSquare',
x: iconX,
y: iconY,
width: iconCfg.size,
height: iconCfg.size,
});
icon.on('click', () => {
// 折叠行头时因scrollY没变,导致底层出现空白
if (!isCollapsed) {
const oldScrollY = this.spreadsheet.store.get('scrollY');
// 可视窗口高度
const viewportHeight = this.spreadsheet.facet.panelBBox.height || 0;
// 被折叠项的高度
const deleteHeight = getAllChildrenNodeHeight(this.meta);
// 折叠后真实高度
const realHeight = hierarchy.height - deleteHeight;
if (oldScrollY > 0 && oldScrollY + viewportHeight > realHeight) {
const currentScrollY = realHeight - viewportHeight;
this.spreadsheet.store.set(
'scrollY',
currentScrollY > 0 ? currentScrollY : 0,
);
}
}
this.spreadsheet.emit(KEY_COLLAPSE_TREE_ROWS, {
id,
isCollapsed: !isCollapsed,
node: this.meta,
});
});
// in mobile, we use this cell
this.gm = new GM(this, {
gestures: ['Tap'],
});
this.gm.on('tap', () => {
this.spreadsheet.emit(KEY_COLLAPSE_TREE_ROWS, {
id,
isCollapsed: !isCollapsed,
node: this.meta,
});
});
this.add(icon);
}
}
protected drawRectBorder() {
const { position, width, viewportWidth, scrollX } = this.headerConfig;
const { x, y } = this.meta;
// 1、bottom border
const textIndent = this.getTextIndent();
renderLine(
this,
{
x1: x + textIndent,
y1: y,
x2: position.x + width + viewportWidth + scrollX,
y2: y,
},
{
stroke: this.theme.rowCell.cell.horizontalBorderColor,
lineWidth: this.theme.rowCell.cell.horizontalBorderWidth,
opacity: this.theme.rowCell.cell.horizontalBorderOpacity,
},
);
}
protected isTreeType() {
return this.spreadsheet.isHierarchyTreeType();
}
protected getTextIndent() {
if (!this.isTreeType()) {
return 0;
}
const cellPadding = get(this.theme, 'rowCell.cell.padding');
const baseIndent = cellPadding.left;
const iconTheme = get(this.theme, 'rowCell.icon');
const iconWidth =
iconTheme.size + iconTheme.margin.left + iconTheme.margin.right;
let parent = this.meta.parent;
let multiplier = baseIndent;
while (parent) {
if (parent.height !== 0) {
multiplier += baseIndent + iconWidth;
}
parent = parent.parent;
}
return multiplier;
}
protected getRowTextStyle(isTotals: boolean, isLeaf: boolean) {
return isLeaf && !isTotals
? this.theme.rowCell.text
: this.theme.rowCell.bolderText;
}
protected getFormattedValue(value: string): string {
let content = value;
const formatter = this.spreadsheet.dataSet.getFieldFormatter(
this.meta.field,
);
if (formatter) {
content = formatter(value);
}
return content;
}
protected drawCellText() {
const { offset, height, linkFieldIds = [] } = this.headerConfig;
const {
label,
x,
y,
width: cellWidth,
height: cellHeight,
isLeaf,
isTotals,
} = this.meta;
const { text: textTheme, icon: iconTheme } = this.theme.rowCell;
const isTreeType = this.isTreeType();
// grid & is totals content is empty
const content = this.getFormattedValue(label);
const iconWidth =
iconTheme.size + iconTheme.margin.left + iconTheme.margin.right;
const textStyle = { ...this.getRowTextStyle(isTotals, isLeaf) };
textStyle.textAlign = 'left';
textStyle.textBaseline = 'top';
const cellPadding = get(this.theme, 'rowCell.cell.padding');
const totalPadding = cellPadding?.left + cellPadding?.right;
const textIndent = !isTreeType
? this.getTextIndent() + cellPadding?.left
: this.getTextIndent() + iconWidth;
const maxWidth = cellWidth - textIndent - totalPadding;
const text = getEllipsisText(content, maxWidth, textStyle);
const textX = x + textIndent;
const textY = getAdjustPosition(
y,
cellHeight,
offset,
height,
textTheme.fontSize,
);
this.textShape = renderText(
this,
[this.textShape],
textX,
textY,
text,
textStyle,
{ cursor: 'pointer' },
);
// handle link nodes
if (linkFieldIds.includes(this.meta.key)) {
const device = get(this.headerConfig, 'spreadsheet.options.style.device');
// 配置了链接跳转
if (!isMobile(device)) {
const textBBox = this.textShape.getBBox();
renderLine(
this,
{
x1: textBBox.bl.x,
y1: textBBox.bl.y + 1,
x2: textBBox.br.x,
y2: textBBox.br.y + 1,
},
{ stroke: textStyle.fill, lineWidth: 1 },
);
this.textShape.attr({
appendInfo: {
isRowHeaderText: true, // 标记为行头文本,方便做链接跳转直接识别
cellData: this.meta,
},
});
} else {
this.textShape.attr({
fill: textTheme.linkTextFill,
appendInfo: {
isRowHeaderText: true, // 标记为行头文本,方便做链接跳转直接识别
cellData: this.meta,
},
});
}
}
return textX + measureTextWidth(text, textStyle);
}
protected drawHotSpotInLeaf() {
if (this.meta.isLeaf) {
// 热区公用一个group
const prevResizer = this.spreadsheet.foregroundGroup.findById(
KEY_GROUP_ROW_RESIZER,
);
const resizer = (prevResizer ||
this.spreadsheet.foregroundGroup.addGroup({
id: KEY_GROUP_ROW_RESIZER,
})) as IGroup;
const { offset, position } = this.headerConfig;
const {
label,
x,
y,
width: cellWidth,
height: cellHeight,
parent,
} = this.meta;
resizer.addShape('rect', {
attrs: {
x: position.x + x,
y: position.y - offset + y + cellHeight - HIT_AREA / 2,
width: cellWidth,
fill: COLOR_DEFAULT_RESIZER,
height: HIT_AREA,
cursor: 'row-resize',
appendInfo: {
isResizer: true,
class: 'resize-trigger',
type: 'row',
affect: 'cell',
caption: parent.isTotals ? '' : label,
offsetX: position.x + x,
offsetY: position.y - offset + y,
width: cellWidth,
height: cellHeight,
} as ResizeInfo,
},
});
}
}
protected drawActionIcons() {
const rowActionIcons = this.spreadsheet.options.rowActionIcons;
if (!rowActionIcons) return;
const { iconTypes, display, action, customDisplayByRowName } =
rowActionIcons;
if (customDisplayByRowName) {
const { rowNames, mode } = customDisplayByRowName;
const rowIds = rowNames.map((rowName) => `root${ID_SEPARATOR}${rowName}`);
if (
(mode === 'omit' && rowIds.includes(this.meta.id)) ||
(mode === 'pick' && !rowIds.includes(this.meta.id))
)
return;
}
const showIcon = () => {
const level = this.meta.level;
const { level: rowLevel, operator } = display;
switch (operator) {
case '<':
return level < rowLevel;
case '<=':
return level <= rowLevel;
case '=':
return level === rowLevel;
case '>':
return level > rowLevel;
case '>=':
return level >= rowLevel;
default:
break;
}
};
if (
showIcon() &&
this.spreadsheet.isHierarchyTreeType() &&
this.spreadsheet.isPivotMode()
) {
const { x, y, height, width } = this.meta;
const { cell, text } = this.theme.rowCell;
for (let i = 0; i < iconTypes.length; i++) {
const iconRight =
(text.fontSize + cell.padding?.left) * (iconTypes.length - i);
const icon = new GuiIcon({
type: iconTypes[i],
x: x + width - iconRight,
y: y + (height - text.fontSize) / 2,
width: text.fontSize,
height: text.fontSize,
});
icon.set('visible', false);
icon.on('click', (e: Event) => {
action(iconTypes[i], this.meta, e);
});
this.add(icon);
if (!this.actionIcons) {
this.actionIcons = [];
}
this.actionIcons.push(icon);
}
}
}
}