wujingjing
2024-04-22 f106e4dffb8279cb90726e83e7edd631f4c77699
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
/**
 * @author claude
 * @date 2018/3/24
 * @description 通通来自于 behavior 里注册的事件
 */
 
import { IGroup } from '@antv/g6';
 
// import defaultStyles from '../defaultStyles';
 
/**
 * @description 恢复节点/边/锚点默认样式
 */
function setStyle(item, nodeStyle, text?, textStyle?) {
    item.attr(nodeStyle);
    if (text) {
        text.attr(textStyle);
    }
}
 
function getItemStyle(type, group, state = 'hover') {
    const item = group.get('item');
    const attrs = group.getFirst().attr();
    const originStyle = type === 'node' ? item.get('originStyle') : item.get('originStyle')['edge-shape'];
    const activeStyle = attrs[`${type}State:${state}`];
    const defaultStyle = attrs[`${type}State:default`];
 
    if (type === 'edge' && defaultStyle && defaultStyle.lineWidth == null) {
        defaultStyle.lineWidth = 1;
    }
 
    return {
        activeStyle,
        defaultStyle,
        originStyle,
    };
}
 
const events = {
    /**
     * @description 锚点事件
     * 显示/隐藏锚点
     */
    anchorShow(value, group) {
        // 锚点全局开关
        // changeData 时由于实例没销毁, 这里需要处理异常
        if (group.get('children')) {
            const { anchorControls } = group.get('children')[0].cfg.attrs;
 
            if (anchorControls && anchorControls.hide) return false;
        }
 
        if (value) {
            group.showAnchor(group);
        } else {
            group.clearAnchor(group);
        }
    },
 
    /**
     * @description 锚点激活事件
     */
    anchorActived(value, group) {
        // 锚点全局开关
        if (group.get('children')) {
            const { anchorControls } = group.get('children')[0].cfg.attrs;
 
            if (anchorControls && anchorControls.hide) return false;
        }
 
        if (value) {
            const model = group.get('item').getModel();
            const { anchorPoints, anchorHotsoptStyles } = model;
 
            group.showAnchor(group);
 
            this.getAnchorPoints({ anchorPoints }).forEach((p, i) => {
                const bbox = group.get('children')[0].getBBox();
                // 激活元素
                const hotspot = group.addShape('circle', {
                    zIndex: 0,
                    attrs: {
                        x: bbox.minX + bbox.width * p[0],
                        y: bbox.minY + bbox.height * p[1],
                        r: 0,
                        opacity: 0.5,
                        fill: '#1890ff',
                        ...anchorHotsoptStyles,
                    },
                    nodeId: group.get('item').get('id'),
                    className: 'node-anchor-bg',
                    draggable: true,
                    isAnchor: true,
                    index: i,
                });
 
                // 锚点动画
                hotspot.animate(
                    { r: 11 },
                    {
                        duration: 200,
                    }
                );
 
                group.sort(); // 将group中的元素按照 zIndex 从大到小排序
                group.anchorShapes.push(hotspot);
            });
 
            group.anchorShapes.filter((item) => {
                if (item.get('className') === 'node-anchor') {
                    item.toFront();
                }
                if (item.get('className') === 'node-anchor-group') {
                    item.attr({
                        r: ((anchorHotsoptStyles && anchorHotsoptStyles.r) || 11) + 2,
                    });
                    item.toFront();
                }
            });
        } else {
            // 移除
            group.clearAnchor(group);
        }
    },
 
    /**
     * @description 边多状态事件
     */
    nodeState(value, group) {
        if (value === false) {
            // 清除所有状态
            events['nodeState:default'].call(this, true, group);
        } else {
            events[`nodeState:${value}`] && events[`nodeState:${value}`].call(this, value, group);
        }
    },
 
    /**
     * @description 节点恢复默认状态事件
     */
    'nodeState:default'(value, group) {
        if (value) {
            const node = group.getChildByIndex(0);
            const text = group.getChildByIndex(1);
            const { defaultStyle } = getItemStyle.call(this, 'node', group);
 
            if (!defaultStyle) return;
            const textStyle = defaultStyle.labelCfg && defaultStyle.labelCfg.style ? defaultStyle.labelCfg.style : {};
 
            setStyle(node, defaultStyle, text, textStyle);
        }
    },
 
    /**
     * @description 节点selected事件
     */
    'nodeState:selected'(value, group) {
        const node = group.getChildByIndex(0);
        const text = group.getChildByIndex(1);
        const { activeStyle, defaultStyle } = getItemStyle.call(this, 'node', group, 'selected');
 
        if (!activeStyle) return;
 
        if (value) {
            const textStyle = activeStyle.labelCfg && activeStyle.labelCfg.style ? activeStyle.labelCfg.style : {};
 
            setStyle(node, activeStyle, text, textStyle);
        } else {
            const textStyle = defaultStyle.labelCfg && defaultStyle.labelCfg.style ? defaultStyle.labelCfg.style : {};
 
            setStyle(node, defaultStyle, text, textStyle);
        }
    },
 
    /**
     * @description 节点hover事件
     */
    'nodeState:hover'(value, group) {
        const node = group.getChildByIndex(0);
        const { activeStyle, defaultStyle } = getItemStyle.call(this, 'node', group, 'hover');
 
        if (!activeStyle) return;
        if (value) {
            setStyle(node, activeStyle);
        } else {
            setStyle(node, defaultStyle);
        }
    },
 
    /**
     * @description 边多状态事件
     */
    edgeState(value, group) {
        if (value === false) {
            // 清除所有状态
            events['edgeState:default'].call(this, true, group);
        } else {
            events[`edgeState:${value}`] && events[`edgeState:${value}`].call(this, value, group);
        }
    },
 
    /**
     * @description 边恢复默认状态事件
     */
    'edgeState:default'(value, group: IGroup) {
        if (value) {
            const { activeStyle, defaultStyle, originStyle } = getItemStyle.call(this, 'edge', group);
            const edge = group.getChildByIndex(0);
            const { endArrow, startArrow } = edge.get('attrs');
            // group.get('item').getModel();
            if (defaultStyle) {
                // 停止内部动画
                // this.stopAnimate(group, activeStyle && activeStyle.animationType ? activeStyle.animationType : 'dash');
                setStyle(edge, { ...defaultStyle, animationType: activeStyle.animationType || 'dash' });
 
                if (endArrow) {
                    edge.attr(
                        'endArrow',
                        endArrow === true
                            ? true
                            : {
                                    path: endArrow.path,
                                    fill: defaultStyle.stroke || originStyle.stroke,
                              }
                    );
                }
                if (startArrow) {
                    edge.attr(
                        'startArrow',
                        startArrow === true
                            ? true
                            : {
                                    path: startArrow.path,
                                    fill: defaultStyle.stroke || originStyle.stroke,
                              }
                    );
                }
            }
        }
    },
 
    /**
     * @description edge hover事件
     */
    'edgeState:hover'(value, group) {
        const path = group.getChildByIndex(0);
        const { endArrow, startArrow } = path.get('attrs');
        const { activeStyle, defaultStyle, originStyle } = getItemStyle.call(this, 'edge', group, 'hover');
 
        if (!activeStyle) return;
        if (value) {
            if (activeStyle.animate === true) {
                this.runAnimate(group, activeStyle.animationType || 'dash');
            } else if (typeof activeStyle.animate === 'function') {
                activeStyle.animate.call(this, group);
            } else {
                setStyle(path, activeStyle);
                if (endArrow) {
                    path.attr(
                        'endArrow',
                        endArrow === true
                            ? true
                            : {
                                    path: endArrow.path,
                                    fill: activeStyle.stroke || originStyle.stroke,
                              }
                    );
                }
                if (startArrow) {
                    path.attr(
                        'startArrow',
                        startArrow === true
                            ? true
                            : {
                                    path: startArrow.path,
                                    fill: activeStyle.stroke || originStyle.stroke,
                              }
                    );
                }
            }
        } else {
            if (activeStyle.animate === true) {
                // 停止动画
                this.stopAnimate(group, activeStyle.animationType || 'dash');
            } else if (typeof activeStyle.animate === 'function') {
                activeStyle.animate.call(this, group, 'stop');
            } else {
                setStyle(path, defaultStyle);
                if (endArrow) {
                    path.attr(
                        'endArrow',
                        endArrow === true
                            ? true
                            : {
                                    path: endArrow.path,
                                    fill: defaultStyle.stroke || activeStyle.stroke || originStyle.stroke,
                              }
                    );
                }
                if (startArrow) {
                    path.attr(
                        'startArrow',
                        startArrow === true
                            ? true
                            : {
                                    path: startArrow.path,
                                    fill: defaultStyle.stroke || activeStyle.stroke || originStyle.stroke,
                              }
                    );
                }
            }
        }
    },
 
    /**
     * @description edge 选中事件
     */
    'edgeState:selected'(value, group) {
        const path = group.getChildByIndex(0);
        const { endArrow, startArrow } = path.get('attrs');
        const { activeStyle, defaultStyle, originStyle } = getItemStyle.call(this, 'edge', group, 'selected');
 
        if (!activeStyle) return;
        if (value) {
            if (activeStyle.animate === true) {
                // 执行内部动画
                this.runAnimate(group, activeStyle.animationType || 'dash');
            } else if (typeof activeStyle.animate === 'function') {
                // 执行外部动画
                activeStyle.animate.call(this, group);
            } else {
                setStyle(path, activeStyle);
                if (endArrow) {
                    path.attr(
                        'endArrow',
                        endArrow === true
                            ? true
                            : {
                                    path: endArrow.path,
                                    fill: activeStyle.stroke || originStyle.stroke,
                              }
                    );
                }
                if (startArrow) {
                    path.attr(
                        'startArrow',
                        startArrow === true
                            ? true
                            : {
                                    path: startArrow.path,
                                    fill: activeStyle.stroke || originStyle.stroke,
                              }
                    );
                }
            }
        } else {
            if (activeStyle.animate === true) {
                // 停止内部动画
                this.stopAnimate(group, activeStyle.animationType || 'dash');
            } else if (typeof activeStyle.animate === 'function') {
                // 停止外部动画
                activeStyle.animate.call(this, group, 'stop');
            } else {
                setStyle(path, defaultStyle);
                if (endArrow) {
                    path.attr(
                        'endArrow',
                        endArrow === true
                            ? true
                            : {
                                    path: endArrow.path,
                                    fill: defaultStyle.stroke || activeStyle.stroke || originStyle.stroke,
                              }
                    );
                }
                if (startArrow) {
                    path.attr(
                        'startArrow',
                        startArrow === true
                            ? true
                            : {
                                    path: startArrow.path,
                                    fill: defaultStyle.stroke || activeStyle.stroke || originStyle.stroke,
                              }
                    );
                }
            }
        }
    },
};
 
export default events;