-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
tooltip.ts
343 lines (326 loc) · 9.27 KB
/
tooltip.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
import type { TooltipStyleProps } from '@antv/component';
import { Tooltip as TooltipComponent } from '@antv/component';
import { get } from '@antv/util';
import type { RuntimeContext } from '../runtime/types';
import type { ElementDatum, ElementType, ID, IElementEvent } from '../types';
import type { BasePluginOptions } from './base-plugin';
import { BasePlugin } from './base-plugin';
export interface TooltipOptions
extends BasePluginOptions,
Pick<TooltipStyleProps, 'position' | 'offset' | 'enterable' | 'style' | 'container' | 'title'> {
/**
* <zh/> 触发行为,可选 hover | click
* - `'hover'`:鼠标移入元素时触发
* - `'click'`:鼠标点击元素时触发
*
* <en/> Trigger behavior, optional hover | click
* - `'hover'`:mouse hover element
* - `'click'`:mouse click element
* @defaultValue 'hover
*/
trigger?: 'hover' | 'click';
/**
* <zh/> 自定义内容
*
* <en/> Function for getting tooltip content
*/
getContent?: (event: IElementEvent, items: ElementDatum[]) => HTMLElement | string;
/**
* <zh/> 是否启用
*
* <en/> Is enable
* @defaultValue true
*/
enable?: boolean | ((event: IElementEvent) => boolean);
}
export class Tooltip extends BasePlugin<TooltipOptions> {
static defaultOptions: Partial<TooltipOptions> = {
trigger: 'hover',
position: 'top-right',
enterable: false,
enable: true,
offset: [10, 10],
style: {
'.tooltip': {
visibility: 'hidden',
},
},
};
private currentTarget: string | null = null;
private tooltipElement: TooltipComponent | null = null;
private container: HTMLElement | null = null;
constructor(context: RuntimeContext, options: TooltipOptions) {
super(context, Object.assign({}, Tooltip.defaultOptions, options));
this.render();
this.bindEvents();
}
/**
* <zh/> 获取事件及处理事件的方法
*
* <en/> Get event and handle event methods
* @returns <zh/> 事件及处理事件的方法 | <en/> Event and handling event methods
*/
private getEvents(): { [key: string]: (event: IElementEvent) => void } {
if (this.options.trigger === 'click') {
return {
'node:click': this.onClick,
'edge:click': this.onClick,
'combo:click': this.onClick,
'canvas:click': this.onPointerLeave,
contextmenu: this.onPointerLeave,
drag: this.onPointerLeave,
};
}
return {
'node:pointerenter': this.onPointerEnter,
'node:pointermove': this.onPointerMove,
'canvas:pointermove': this.onCanvasMove,
'edge:pointerenter': this.onPointerEnter,
'edge:pointermove': this.onPointerMove,
'combo:pointerenter': this.onPointerEnter,
'combo:pointermove': this.onPointerMove,
contextmenu: this.onPointerLeave,
'node:drag': this.onPointerLeave,
};
}
/**
* <zh/> 更新tooltip配置
*
* <en/> Update the tooltip configuration
* @param options - <zh/> 配置项 | <en/> options
* @internal
*/
public update(options: Partial<TooltipOptions>) {
this.unbindEvents();
super.update(options);
if (this.tooltipElement) {
this.container?.removeChild(this.tooltipElement.HTMLTooltipElement);
}
this.tooltipElement = this.initTooltip();
this.bindEvents();
}
private render() {
const { canvas } = this.context;
const $container = canvas.getContainer();
if (!$container) return;
this.container = $container;
this.tooltipElement = this.initTooltip();
}
private unbindEvents() {
const { graph } = this.context;
/** The previous event binding needs to be removed when updating the trigger. */
const events = this.getEvents();
Object.keys(events).forEach((eventName) => {
graph.off(eventName, events[eventName]);
});
}
private bindEvents() {
const { graph } = this.context;
const events = this.getEvents();
Object.keys(events).forEach((eventName) => {
graph.on(eventName, events[eventName]);
});
}
private isEnable = (event: IElementEvent) => {
const { enable } = this.options;
if (typeof enable === 'function') {
return enable(event);
}
return enable;
};
/**
* <zh/> 点击事件
*
* <en/> Click event
* @param event - <zh/> 元素 | <en/> element
*/
public onClick = (event: IElementEvent) => {
const {
target: { id },
} = event;
// click the same item twice, tooltip will be hidden
if (this.currentTarget === id) {
this.currentTarget = null;
this.hide(event);
} else {
this.currentTarget = id;
this.show(event);
}
};
/**
* <zh/> 在目标元素(node/edge/combo)上移动
*
* <en/> Move on target element (node/edge/combo)
* @param event - <zh/> 目标元素 | <en/> target element
*/
public onPointerMove = (event: IElementEvent) => {
const { target } = event;
if (!this.currentTarget || target.id === this.currentTarget) {
return;
}
this.show(event);
};
/**
* <zh/> 点击画布/触发拖拽/出现上下文菜单隐藏tooltip
*
* <en/> Hide tooltip when clicking canvas/triggering drag/appearing context menu
* @param event - <zh/> 目标元素 | <en/> target element
*/
public onPointerLeave = (event: IElementEvent) => {
this.hide(event);
this.currentTarget = null;
};
/**
* <zh/> 移动画布
*
* <en/> Move canvas
* @param event - <zh/> 目标元素 | <en/> target element
*/
public onCanvasMove = (event: IElementEvent) => {
this.hide(event);
this.currentTarget = null;
};
private onPointerEnter = (event: IElementEvent) => {
this.show(event);
};
/**
* <zh/> 显示目标元素的提示框
*
* <en/> Show tooltip of target element
* @param id - <zh/> 元素 ID | <en/> element ID
*/
public showById = (id: ID) => {
const event = {
target: { id },
} as IElementEvent;
this.show(event);
};
private getElementData = (id: ID, targetType: ElementType) => {
const { model } = this.context;
switch (targetType) {
case 'node':
return model.getNodeData([id]);
case 'edge':
return model.getEdgeData([id]);
case 'combo':
return model.getComboData([id]);
default:
return [];
}
};
/**
* <zh/> 在目标元素上显示tooltip
*
* <en/> Show tooltip on target element
* @param event - <zh/> 目标元素 | <en/> target element
* @internal
*/
public show = (event: IElementEvent) => {
const {
client,
target: { id },
} = event;
const targetType = this.context.graph.getElementType(id);
if (!this.tooltipElement || !this.isEnable(event)) return;
const { getContent, title } = this.options;
this.currentTarget = id;
const items: ElementDatum[] = this.getElementData(id, targetType as ElementType);
let x;
let y;
if (client) {
x = client.x;
y = client.y;
} else {
const style = get(items, '0.style', { x: 0, y: 0 });
x = style.x;
y = style.y;
}
let tooltipContent: { [key: string]: unknown } = {};
if (getContent) {
tooltipContent.content = getContent(event, items);
} else {
const style = this.context.graph.getElementRenderStyle(id);
const color = targetType === 'node' ? style.fill : style.stroke;
tooltipContent = {
title: title || targetType,
data: items.map((item) => {
return {
name: 'ID',
value: item.id || `${item.source} -> ${item.target}`,
color,
};
}),
};
}
this.tooltipElement.update({
x,
y,
style: {
'.tooltip': {
visibility: 'visible',
},
},
...tooltipContent,
});
};
/**
* <zh/> 隐藏tooltip
*
* <en/> Hidden tooltip
* @param event - <zh/> 目标元素,不传则为外部调用 | <en/> Target element, not passed in as external call
*/
public hide = (event?: IElementEvent) => {
// if e is undefined, hide the tooltip, external call
if (!event) {
this.tooltipElement?.hide();
return;
}
if (!this.tooltipElement || !this.isEnable(event)) return;
const {
client: { x, y },
} = event;
this.tooltipElement.hide(x, y);
};
private initTooltip = () => {
const { canvas } = this.context;
const { center } = canvas.getBounds();
const $container = canvas.getContainer() as HTMLElement;
const { top, left } = $container.getBoundingClientRect();
const { style, position, enterable, container = { x: -left, y: -top }, title, offset } = this.options;
const [x, y] = center;
const [width, height] = canvas.getSize();
const tooltipElement = new TooltipComponent({
className: 'tooltip',
style: {
x,
y,
container,
title,
bounding: {
x: 0,
y: 0,
width,
height,
},
position,
enterable,
offset,
style,
},
});
this.container?.appendChild(tooltipElement.HTMLTooltipElement);
return tooltipElement;
};
/**
* <zh/> 销毁tooltip
*
* <en/> Destroy tooltip
*/
public destroy(): void {
this.unbindEvents();
if (this.tooltipElement) {
this.container?.removeChild(this.tooltipElement.HTMLTooltipElement);
}
super.destroy();
}
}