/
tooltip.ts
252 lines (232 loc) · 7.09 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
import type { TooltipStyleProps } from '@antv/component';
import { Tooltip as TooltipComponent } from '@antv/component';
import type { FederatedMouseEvent } from '@antv/g';
import type { RuntimeContext } from '../runtime/types';
import type { BehaviorEvent } from '../types';
import type { BasePluginOptions } from './base-plugin';
import { BasePlugin } from './base-plugin';
interface IG6GraphEvent extends BehaviorEvent<FederatedMouseEvent> {
targetType: 'node' | 'edge' | 'combo';
}
export type ContentModel = {
items?: { [key: string]: unknown }[];
};
export interface TooltipOptions
extends BasePluginOptions,
Pick<TooltipStyleProps, 'position' | 'offset' | 'enterable' | 'style' | 'container'> {
/** <zh/> 触发方式 | <en/> Event type that triggers display of tooltip */
trigger?: 'pointerenter' | 'click';
/** <zh/> 自定义内容 | <en/> Function for getting tooltip content */
getContent?: (evt: IG6GraphEvent, item: ContentModel) => HTMLElement | string;
/** <zh/> 触发类型 | <en/> Types of items for which tooltip is allowed to be displayed */
itemTypes?: ('node' | 'edge' | 'combo')[];
}
export class Tooltip extends BasePlugin<TooltipOptions> {
static defaultOptions: Partial<TooltipOptions> = {
trigger: 'pointerenter',
position: 'top-right',
enterable: false,
itemTypes: ['node', 'edge', 'combo'],
style: {
'.tooltip': {
visibility: 'hidden',
},
},
container: {
x: 0,
y: 0,
},
};
private currentTarget: string | null = null;
private tooltipElement: TooltipComponent | null = null;
private $element: HTMLElement = document.createElement('div');
constructor(context: RuntimeContext, options: TooltipOptions) {
super(context, Object.assign({}, Tooltip.defaultOptions, options));
this.render();
this.bindEvents();
}
public getEvents(trigger: 'pointerenter' | 'click'): { [key: string]: Function } {
if (trigger === 'click') {
return {
'node:click': this.onClick,
'edge:click': this.onClick,
'combo:click': this.onClick,
'canvas:click': this.onPointerLeave,
afterremoveitem: 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,
};
}
public update(options: Partial<TooltipOptions>) {
super.update(options);
this.unbundEvents(true);
if (this.tooltipElement) {
this.$element.removeChild(this.tooltipElement.HTMLTooltipElement);
}
this.tooltipElement = this.initTooltip();
}
private render() {
const { canvas } = this.context;
const $container = canvas.getContainer();
if (!$container) return;
this.$element.className = 'g6-tooltip';
$container.appendChild(this.$element);
this.tooltipElement = this.initTooltip();
}
private unbundEvents(isUpdate?: boolean) {
const { graph } = this.context;
const { trigger } = this.options;
/** The previous event binding needs to be removed when updating the trigger. */
const events = this.getEvents(!isUpdate ? trigger : trigger === 'click' ? 'pointerenter' : 'click');
Object.keys(events).forEach((eventName) => {
graph.off(eventName, events[eventName]);
});
}
private bindEvents() {
const { graph } = this.context;
const { trigger } = this.options;
const events = this.getEvents(trigger);
Object.keys(events).forEach((eventName) => {
graph.on(eventName, events[eventName]);
});
}
public onClick = (e: IG6GraphEvent) => {
const {
targetType,
target: { id },
} = e;
if (this.options.itemTypes.indexOf(targetType) === -1) return;
// click the same item twice, tooltip will be hidden
if (this.currentTarget === id) {
this.currentTarget = null;
this.hideTooltip(e);
} else {
this.currentTarget = id;
this.showTooltip(e);
}
};
public onPointerMove = (e: IG6GraphEvent) => {
const { targetType, target } = e;
if (this.options.itemTypes.indexOf(targetType) === -1) return;
if (!this.currentTarget || target.id === this.currentTarget) {
return;
}
this.showTooltip(e);
};
public onPointerLeave = (e: IG6GraphEvent) => {
this.hideTooltip(e);
this.currentTarget = null;
};
public onCanvasMove = (e: IG6GraphEvent) => {
this.hideTooltip(e);
this.currentTarget = null;
};
private onPointerEnter = (e: IG6GraphEvent) => {
const { targetType } = e;
if (this.options.itemTypes.indexOf(targetType) === -1) return;
this.showTooltip(e);
};
public showTooltip(e: IG6GraphEvent) {
const {
targetType,
client: { x, y },
target: { id, attributes },
} = e;
if (!this.tooltipElement) return;
const { getContent } = this.options;
const { model } = this.context;
const { color, stroke } = attributes;
this.currentTarget = id;
let items: { [key: string]: unknown }[] = [];
switch (targetType) {
case 'node':
items = model.getNodeData([id]);
break;
case 'edge':
items = model.getEdgeData([id]);
break;
case 'combo':
items = model.getComboData([id]);
break;
default:
break;
}
let tooltipContent: { [key: string]: unknown } = {};
if (getContent) {
tooltipContent.content = getContent(e, { items });
} else {
tooltipContent = {
title: targetType,
data: items.map((item) => {
return {
name: 'ID',
value: item.id || `${item.source} -> ${item.target}`,
color: color || stroke,
};
}),
};
}
this.tooltipElement.update({
x,
y,
style: {
'.tooltip': {
visibility: 'visible',
},
},
...tooltipContent,
});
}
public hideTooltip(e: IG6GraphEvent) {
const {
client: { x, y },
} = e;
if (!this.tooltipElement) return;
this.tooltipElement.hide(x, y);
}
private initTooltip = () => {
const { style, position, enterable, container } = this.options;
const { canvas } = this.context;
const { center } = this.context.canvas.getBounds();
const [x, y] = center;
const [width, height] = canvas.getSize();
const tooltipElement = new TooltipComponent({
className: 'tooltip',
style: {
x,
y,
container,
bounding: {
x: 0,
y: 0,
width,
height,
},
position,
enterable,
title: '',
offset: [10, 10],
style,
},
});
this.$element.appendChild(tooltipElement.HTMLTooltipElement);
return tooltipElement;
};
public destroy(): void {
this.unbundEvents();
this.$element.remove();
super.destroy();
}
}