2019-11-05 20:06:36 +08:00
|
|
|
|
---
|
2020-11-13 21:53:34 +08:00
|
|
|
|
title: 复合交互及其自定义 Behavior
|
|
|
|
|
order: 13
|
2019-11-05 20:06:36 +08:00
|
|
|
|
---
|
|
|
|
|
|
2019-12-02 19:58:02 +08:00
|
|
|
|
Behavior 指 G6 中的复合交互,一般 Behavior 包含一个或多个事件的监听与处理以及一系列对图中元素的操作。
|
2019-11-05 20:06:36 +08:00
|
|
|
|
|
2019-12-02 19:58:02 +08:00
|
|
|
|
Behavior 默认包含 `shouldBegin`,`shouldUpdate`,`shouldEnd` 三个回调,代表是否开始行为,是否更新元素,是否进行结束行为,当返回值为 `false` 时阻止默认行为。
|
2019-11-05 20:06:36 +08:00
|
|
|
|
|
2020-08-10 19:08:49 +08:00
|
|
|
|
所有内置 Behavior 及其参数参见 [内置的 Behavior 教程](/zh/docs/manual/middle/states/defaultBehavior)。当 [内置 Behavior](/zh/docs/manual/middle/states/defaultBehavior) 不能满足需求时,使用 `registerBehavior(behaviorName, behavior)` 方法注册自定义的交互行为。详见 [Behavior API](/zh/docs/api/Behavior)。本文将介绍如何自定义 Behavior。
|
2020-05-21 10:32:57 +08:00
|
|
|
|
|
2020-08-10 19:08:49 +08:00
|
|
|
|
```ts
|
|
|
|
|
// highlight-start
|
|
|
|
|
G6.registerBehavior(behaviorName: string, behavior: BehaviorOption)
|
|
|
|
|
// highlight-end
|
2020-08-05 15:43:31 +08:00
|
|
|
|
|
2020-08-10 19:08:49 +08:00
|
|
|
|
// Custom a type of Behavior
|
2019-11-08 19:43:55 +08:00
|
|
|
|
G6.registerBehavior('behaviorName', {
|
2020-08-10 19:08:49 +08:00
|
|
|
|
// Bind the event and its callback
|
2019-11-08 19:43:55 +08:00
|
|
|
|
getEvents() {
|
|
|
|
|
return {
|
2020-08-05 15:43:31 +08:00
|
|
|
|
'node:click': 'onClick',
|
|
|
|
|
mousemove: 'onMousemove',
|
2019-11-08 19:43:55 +08:00
|
|
|
|
'edge:click': 'onEdgeClick',
|
2020-02-14 10:10:54 +08:00
|
|
|
|
};
|
2019-11-08 19:43:55 +08:00
|
|
|
|
},
|
2020-08-05 15:43:31 +08:00
|
|
|
|
/**
|
2020-08-10 19:08:49 +08:00
|
|
|
|
* Handle the callback for node:click
|
2020-08-05 15:43:31 +08:00
|
|
|
|
* @override
|
2020-08-10 19:08:49 +08:00
|
|
|
|
* @param {Object} evt The handler
|
2020-08-05 15:43:31 +08:00
|
|
|
|
*/
|
|
|
|
|
onClick(evt) {
|
|
|
|
|
const node = evt.item;
|
|
|
|
|
const graph = this.graph;
|
|
|
|
|
const point = { x: evt.x, y: evt.y };
|
|
|
|
|
const model = node.getModel();
|
2020-02-14 10:10:54 +08:00
|
|
|
|
// TODO
|
2019-11-08 19:43:55 +08:00
|
|
|
|
},
|
2020-08-05 15:43:31 +08:00
|
|
|
|
/**
|
2020-08-10 19:08:49 +08:00
|
|
|
|
* Handle the callback for mousemove
|
2020-08-05 15:43:31 +08:00
|
|
|
|
* @override
|
2020-08-10 19:08:49 +08:00
|
|
|
|
* @param {Object} evt The handler
|
2020-08-05 15:43:31 +08:00
|
|
|
|
*/
|
|
|
|
|
onMousemove(evt) {
|
2020-02-14 10:10:54 +08:00
|
|
|
|
// TODO
|
2019-11-08 19:43:55 +08:00
|
|
|
|
},
|
2020-08-05 15:43:31 +08:00
|
|
|
|
/**
|
2020-08-10 19:08:49 +08:00
|
|
|
|
* Handle the callback for :click
|
2020-08-05 15:43:31 +08:00
|
|
|
|
* @override
|
2020-08-10 19:08:49 +08:00
|
|
|
|
* @param {Object} evt The handler
|
2020-08-05 15:43:31 +08:00
|
|
|
|
*/
|
|
|
|
|
onEdgeClick(evt) {
|
2020-02-14 10:10:54 +08:00
|
|
|
|
// TODO
|
|
|
|
|
},
|
2019-12-02 19:58:02 +08:00
|
|
|
|
});
|
2019-11-08 19:43:55 +08:00
|
|
|
|
```
|
2019-11-05 20:52:30 +08:00
|
|
|
|
|
2020-08-10 19:08:49 +08:00
|
|
|
|
## 参数
|
|
|
|
|
|
|
|
|
|
| 名称 | 类型 | 是否必选 | 描述 |
|
|
|
|
|
| --- | --- | --- | --- |
|
|
|
|
|
| behaviorName | String | true | 自定义 Behavior 的名称。 |
|
|
|
|
|
| behavior | BehaviorOption | true | 自定义 behavior 时的配置项,配置项中包括的方法及作用具体请参考:[Behavior API](/zh/docs/api/Behavior)。 |
|
|
|
|
|
|
|
|
|
|
### BehaviorOption.getEvents()
|
2020-02-14 10:10:54 +08:00
|
|
|
|
|
2019-12-02 19:58:02 +08:00
|
|
|
|
自定义 Behavior 时,定义事件及处理事件的方法。
|
2019-11-05 20:06:36 +08:00
|
|
|
|
|
2020-02-14 10:10:54 +08:00
|
|
|
|
`getEvents()` 方法中可以使用的事件请参考[Event 文档](/zh/docs/api/Event)。
|
2019-11-05 20:06:36 +08:00
|
|
|
|
|
2019-11-08 19:43:55 +08:00
|
|
|
|
**用法**
|
2020-02-14 10:10:54 +08:00
|
|
|
|
|
2019-11-05 20:06:36 +08:00
|
|
|
|
```javascript
|
|
|
|
|
G6.registerBehavior('behaviorName', {
|
|
|
|
|
getEvents() {
|
|
|
|
|
return {
|
|
|
|
|
'node:click': 'onNodeClick',
|
|
|
|
|
'edge:click': 'onEdgeClick',
|
|
|
|
|
'mousemove': 'onMouseMove'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2020-08-10 19:08:49 +08:00
|
|
|
|
### BehaviorOption.onNodeClick(evt)
|
2020-02-14 10:10:54 +08:00
|
|
|
|
|
2019-12-02 19:58:02 +08:00
|
|
|
|
`onNodeClick`、`onEdgeClick` 和 `onMouseMove` 都属于自定义方法,用于处理 `node:click`、`edge:click`、`mousemove` 事件。
|
2019-11-05 20:06:36 +08:00
|
|
|
|
|
2019-11-08 19:43:55 +08:00
|
|
|
|
**参数**
|
|
|
|
|
|
2020-02-14 10:10:54 +08:00
|
|
|
|
| 名称 | 类型 | 是否必选 | 描述 |
|
|
|
|
|
| ---- | ----- | -------- | -------------------------------------- |
|
|
|
|
|
| evt | Event | 否 | 包含事件句柄、当前操作对象及各坐标值等 |
|
2019-11-05 20:06:36 +08:00
|
|
|
|
|
2019-12-02 19:58:02 +08:00
|
|
|
|
**参数 `evt` 包括以下属性:**
|
2019-11-05 20:06:36 +08:00
|
|
|
|
|
2020-02-14 10:10:54 +08:00
|
|
|
|
| 名称 | 类型 | 描述 |
|
|
|
|
|
| ---------------- | ---------- | ------------------ |
|
|
|
|
|
| x | Number | 元素视口 x 坐标 |
|
|
|
|
|
| y | Number | 元素视口 y 坐标 |
|
|
|
|
|
| canvasX | Number | 元素 Canvas x 坐标 |
|
|
|
|
|
| canvasY | Number | 元素 Canvas y 坐标 |
|
|
|
|
|
| clientX | Number | 屏幕/页面 x 坐标 |
|
|
|
|
|
| clientY | Number | 屏幕/页面 y 坐标 |
|
|
|
|
|
| event | MouseEvent | 事件句柄 |
|
|
|
|
|
| target | Shape | 当前对象 |
|
|
|
|
|
| type | String | 操作类型 |
|
|
|
|
|
| currentTarget | Object | |
|
|
|
|
|
| item | Shape | 操作的目标元素 |
|
|
|
|
|
| removed | Boolean | 是否删除/销毁 |
|
|
|
|
|
| timeStamp | Number | 时间戳 |
|
|
|
|
|
| bubbles | Boolean | 是否支持事件冒泡 |
|
|
|
|
|
| defaultPrevented | Boolean | 是否阻止默认事件 |
|
|
|
|
|
| cancelable | Boolean | 是否取消 |
|
2019-11-08 19:43:55 +08:00
|
|
|
|
|
|
|
|
|
**用法**
|
2020-02-14 10:10:54 +08:00
|
|
|
|
|
2019-11-05 20:06:36 +08:00
|
|
|
|
```javascript
|
|
|
|
|
G6.registerBehavior('behaviorName', {
|
|
|
|
|
getEvents() {
|
|
|
|
|
return {
|
|
|
|
|
'node:click': 'onNodeClick',
|
|
|
|
|
'edge:click': 'onEdgeClick',
|
2020-02-14 10:10:54 +08:00
|
|
|
|
mousemove: 'onMouseMove',
|
|
|
|
|
};
|
2019-11-05 20:06:36 +08:00
|
|
|
|
},
|
|
|
|
|
onNodeClick(evt) {
|
2020-02-14 10:10:54 +08:00
|
|
|
|
// TODO
|
2019-11-05 20:06:36 +08:00
|
|
|
|
},
|
|
|
|
|
onEdgeClick(evt) {
|
2020-02-14 10:10:54 +08:00
|
|
|
|
// TODO
|
2019-11-05 20:06:36 +08:00
|
|
|
|
},
|
|
|
|
|
onMouseMove(evt) {
|
2020-02-14 10:10:54 +08:00
|
|
|
|
// TODO
|
|
|
|
|
},
|
2019-12-02 19:58:02 +08:00
|
|
|
|
});
|
2019-11-05 20:06:36 +08:00
|
|
|
|
```
|
|
|
|
|
|
2020-08-10 19:08:49 +08:00
|
|
|
|
### BehaviorOption.getDefaultCfg()
|
2020-02-14 10:10:54 +08:00
|
|
|
|
|
2019-12-02 19:58:02 +08:00
|
|
|
|
定义自定义 Behavior 时的默认参数,会与用户传入的参数进行合并。
|
2019-11-05 20:06:36 +08:00
|
|
|
|
|
|
|
|
|
**提示:该方法是可选的**。
|
|
|
|
|
|
2019-11-08 19:43:55 +08:00
|
|
|
|
**用法**
|
2020-02-14 10:10:54 +08:00
|
|
|
|
|
2019-11-05 20:06:36 +08:00
|
|
|
|
```javascript
|
|
|
|
|
G6.registerBehavior('behaviorName', {
|
|
|
|
|
getDefaultCfg() {
|
|
|
|
|
return {
|
|
|
|
|
trigger: 'click' // mouseneter or click
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
2019-11-05 20:52:30 +08:00
|
|
|
|
|
2020-08-10 19:08:49 +08:00
|
|
|
|
### BehaviorOption.shouldBegin(evt)
|
2020-02-14 10:10:54 +08:00
|
|
|
|
|
2019-12-02 19:58:02 +08:00
|
|
|
|
是否阻止行为发生,默认返回 `true`,不阻止行为,需要在处理逻辑中自行调用。
|
2019-11-05 20:06:36 +08:00
|
|
|
|
|
2019-11-08 19:43:55 +08:00
|
|
|
|
**用法**
|
2020-02-14 10:10:54 +08:00
|
|
|
|
|
2019-11-05 20:06:36 +08:00
|
|
|
|
```javascript
|
|
|
|
|
G6.registerBehavior('behaviorName', {
|
|
|
|
|
shouldBegin() {
|
|
|
|
|
// 这里可以根据业务自定义
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2020-08-10 19:08:49 +08:00
|
|
|
|
### BehaviorOption.shouldUpdate(evt)
|
2020-02-14 10:10:54 +08:00
|
|
|
|
|
2019-12-02 19:58:02 +08:00
|
|
|
|
是否更新数据及更改视图,默认返回 `true`,允许更新,如果返回 `false`,则不更新数据和视图。
|
2019-11-05 20:06:36 +08:00
|
|
|
|
|
2019-11-08 19:43:55 +08:00
|
|
|
|
**用法**
|
2020-02-14 10:10:54 +08:00
|
|
|
|
|
2019-11-05 20:06:36 +08:00
|
|
|
|
```javascript
|
|
|
|
|
const graph = new G6.Graph({
|
|
|
|
|
container: 'mountNode',
|
|
|
|
|
width: 500,
|
|
|
|
|
height: 500,
|
|
|
|
|
modes: {
|
2020-02-14 10:10:54 +08:00
|
|
|
|
default: [
|
|
|
|
|
'drag-canvas',
|
|
|
|
|
{
|
|
|
|
|
type: 'self-behavior',
|
2020-08-17 14:51:02 +08:00
|
|
|
|
shouldUpdate: (e) => {
|
2020-02-14 10:10:54 +08:00
|
|
|
|
if (e.target.type !== 'text') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
2019-11-05 20:06:36 +08:00
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2020-08-10 19:08:49 +08:00
|
|
|
|
### BehaviorOption.shouldEnd(evt)
|
2020-02-14 10:10:54 +08:00
|
|
|
|
|
2019-12-02 19:58:02 +08:00
|
|
|
|
是否结束行为,默认返回 `true`。
|