/
hover-event.ts
155 lines (143 loc) · 4.08 KB
/
hover-event.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
import { ColCell, RowCell } from '@/cell';
import { S2Event } from '@/common/constant';
import {
HOVER_FOCUS_TIME,
InteractionStateName,
} from '@/common/constant/interaction';
import { S2CellType, ViewMeta } from '@/common/interface';
import { getTooltipData } from '@/utils/tooltip';
import { getActiveHoverRowColCells } from '@/utils/interaction/hover-event';
import { Event } from '@antv/g-canvas';
import { get, isEmpty, forEach } from 'lodash';
import { BaseEvent } from './base-event';
/**
* @description Hover event for data cells, row cells and col cells
*/
export class HoverEvent extends BaseEvent {
protected bindEvents() {
this.bindDataCellHover();
this.bindRowCellHover();
this.bindColCellHover();
}
private updateRowColCells(meta: ViewMeta) {
const { rowId, colId } = meta;
if (colId) {
// update colHeader cells
const allColHeaderCells = getActiveHoverRowColCells(
colId,
this.interaction.getAllColHeaderCells(),
);
forEach(allColHeaderCells, (cell: ColCell) => {
cell.update();
});
}
if (rowId) {
// update rowHeader cells
const allRowHeaderCells = getActiveHoverRowColCells(
rowId,
this.interaction.getAllRowHeaderCells(),
this.spreadsheet.isHierarchyTreeType(),
);
forEach(allRowHeaderCells, (cell: RowCell) => {
cell.update();
});
}
}
private bindDataCellHover() {
this.spreadsheet.on(S2Event.DATA_CELL_HOVER, (event: Event) => {
const cell = this.spreadsheet.getCell(event.target) as S2CellType;
if (isEmpty(cell)) return;
const meta = cell.getMeta() as ViewMeta;
this.interaction.changeState({
cells: [cell],
stateName: InteractionStateName.HOVER,
});
if (this.spreadsheet.options.hoverHighlight) {
// highlight all the row and column cells which the cell belongs to
this.updateRowColCells(meta);
if (this.interaction.hoverTimer) {
window.clearTimeout(this.interaction.hoverTimer);
this.changeStateToHoverFocus(cell, event, meta);
} else {
this.changeStateToHoverFocus(cell, event, meta);
}
}
});
}
private bindRowCellHover() {
this.spreadsheet.on(S2Event.ROW_CELL_HOVER, (event: Event) => {
this.handleHeaderHover(event);
});
}
private bindColCellHover() {
this.spreadsheet.on(S2Event.COL_CELL_HOVER, (event: Event) => {
this.handleHeaderHover(event);
});
}
/**
* @description change the data cell state from hover to hover focus
* @param cell
* @param event
* @param meta
*/
private changeStateToHoverFocus(
cell: S2CellType,
event: Event,
meta: ViewMeta,
) {
this.interaction.hoverTimer = window.setTimeout(() => {
this.interaction.changeState({
cells: [cell],
stateName: InteractionStateName.HOVER_FOCUS,
});
this.handleTooltip(event, meta);
}, HOVER_FOCUS_TIME);
}
/**
* @description handle the row or column header hover state
* @param event
*/
private handleHeaderHover(event: Event) {
const cell = this.spreadsheet.getCell(event.target) as S2CellType;
if (isEmpty(cell)) return;
const meta = cell.getMeta() as ViewMeta;
this.interaction.changeState({
cells: [cell],
stateName: InteractionStateName.HOVER,
});
cell.update();
this.handleTooltip(event, meta);
}
/**
* @description handle the the tooltip
* @param event
* @param meta
*/
private handleTooltip(event: Event, meta: ViewMeta) {
const position = {
x: event.clientX,
y: event.clientY,
};
const currentCellMeta = get(meta, 'data.0');
const isTotals = get(meta, 'isTotals', false);
if (isTotals) {
return;
}
const options = {
isTotals,
enterable: true,
hideSummary: true,
};
const tooltipData = getTooltipData(
this.spreadsheet,
[currentCellMeta],
options,
);
const showOptions = {
position,
data: tooltipData,
options,
};
this.spreadsheet.showTooltip(showOptions);
}
}