Skip to content

Commit

Permalink
feat(site): change event listener
Browse files Browse the repository at this point in the history
  • Loading branch information
pomelo-nwu committed May 31, 2020
1 parent a1975c7 commit 832cc2b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,19 @@ const chunk = (arr, size) =>
const App = () => {
const [state, setState] = React.useState({
selected: [],
data: Utils.mock(20).random().graphin(),
data: Utils.mock(20)
.random()
.graphin(),
});

const { data, selected } = state;
const graphRef = React.createRef(null);
React.useEffect(() => {
const { graph } = graphRef.current;

// TODO:框选进行关系扩散,暂时不支持多选
const onNodeSelectChange = (e) => {
if (e.target) {
console.info('圈选是targets参数'); //eslint-disable-line
return;
}
const nodes = e.targets.nodes.map((node) => {
const onNodeSelectChange = e => {
const nodes = e.selectedItems.nodes.map(node => {
return node.get('model');
});

setState({
...state,
selected: nodes,
Expand All @@ -52,7 +47,7 @@ const App = () => {
const findConnectionData = { nodes: [], edges: [] };
// 1度扩散,中间经历一个节点
const sortArray = chunk(selected, 2);
sortArray.forEach((arr) => {
sortArray.forEach(arr => {
const [source, target = selected[0]] = arr;

const relativeNode = {
Expand Down
14 changes: 10 additions & 4 deletions packages/graphin-site/examples/advanced/node-expand/demo/Dagre.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,21 @@ const App = () => {
const graphRef = React.createRef(null);
React.useEffect(() => {
const { graph } = graphRef.current;
const onNodeClick = e => {
// 按住Shift框选,按住Option键 多选,进行关系扩散
const onNodeSelectChange = e => {
console.log('nodeselectchange', e);
const nodes = e.selectedItems.nodes.map(node => {
return node.get('model');
});
setState({
...state,
selected: [e.item.get('model')],
selected: nodes,
});
};
graph.on('node:click', onNodeClick);

graph.on('nodeselectchange', onNodeSelectChange);
return () => {
graph.off('node:click', onNodeClick);
graph.off('nodeselectchange', onNodeSelectChange);
};
}, [state]);

Expand Down
21 changes: 4 additions & 17 deletions packages/graphin-site/examples/advanced/node-expand/demo/Force.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,20 @@ const App = () => {
React.useEffect(() => {
const { graph } = graphRef.current;

const onNodeClick = e => {
setState({
...state,
selected: [e.item.get('model')],
});
};

graph.on('node:click', onNodeClick);
// TODO:框选进行关系扩散,暂时不支持多选
// 按住Shift框选,按住Option键 多选,进行关系扩散
const onNodeSelectChange = e => {
if (e.target) {
console.info('圈选是targets参数');
return;
}
const nodes = e.targets.nodes.map(node => {
console.log('nodeselectchange', e);
const nodes = e.selectedItems.nodes.map(node => {
return node.get('model');
});

setState({
...state,
selected: nodes,
});
};
graph.on('nodeselectchange', onNodeSelectChange);

graph.on('nodeselectchange', onNodeSelectChange);
return () => {
graph.off('node:click', onNodeClick);
graph.off('nodeselectchange', onNodeSelectChange);
};
}, [state]);
Expand Down
24 changes: 12 additions & 12 deletions packages/graphin/src/layout/force/ForceLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class ForceLayout {
this.props = {
stiffness: 200.0,
enableWorker: false,
defSpringLen: (edge) => {
defSpringLen: edge => {
return edge.data.spring || 200;
},
repulsion: 200.0 * 5,
Expand Down Expand Up @@ -181,7 +181,7 @@ class ForceLayout {
if (!options) {
return;
}
Object.keys(options).forEach((key) => {
Object.keys(options).forEach(key => {
this.props[key] = options[key];
});
};
Expand Down Expand Up @@ -224,7 +224,7 @@ class ForceLayout {
/** 初始化点和边的信息 */
const { width, height } = this.props;

this.nodes.forEach((node) => {
this.nodes.forEach(node => {
const x = node.data.x || width / 2;
const y = node.data.y || height / 2;
const vec = new Vector(x, y);
Expand All @@ -240,7 +240,7 @@ class ForceLayout {
this.nodePoints.set(node.id, new Point(vec, String(node.id), node.data, mass));
});

this.edges.forEach((edge) => {
this.edges.forEach(edge => {
const source = this.nodePoints.get(edge.source.id) as Point;
const target = this.nodePoints.get(edge.target.id) as Point;
const length = this.props.defSpringLen(edge, source, target);
Expand All @@ -264,7 +264,7 @@ class ForceLayout {
calTotalEnergy = () => {
let energy = 0.0;

this.nodes.forEach((node) => {
this.nodes.forEach(node => {
const point = this.nodePoints.get(node.id) as Point;
const speed = point.v.magnitude();

Expand Down Expand Up @@ -387,7 +387,7 @@ class ForceLayout {
render = () => {
const render = this.registers.get('render');
const nodes: NodeType[] = [];
this.nodePoints.forEach((node) => {
this.nodePoints.forEach(node => {
nodes.push({
...(this.nodeSet[node.id] && this.nodeSet[node.id].data),
x: node.p.x,
Expand Down Expand Up @@ -454,7 +454,7 @@ class ForceLayout {
};

updateHookesLaw = () => {
this.edges.forEach((edge) => {
this.edges.forEach(edge => {
const spring = this.edgeSprings.get(edge.id);
const v = spring.target.p.subtract(spring.source.p);
const displacement = spring.length - v.magnitude();
Expand All @@ -472,7 +472,7 @@ class ForceLayout {

point.updateAcc(direction.scalarMultip(-radio));
};
this.nodes.forEach((node) => {
this.nodes.forEach(node => {
// 默认的向心力指向画布中心
const degree = (node.data && node.data.layout && node.data.layout.degree) as number;
const leafNode = degree === 1;
Expand Down Expand Up @@ -515,7 +515,7 @@ class ForceLayout {
};

updateVelocity = (interval: number) => {
this.nodes.forEach((node) => {
this.nodes.forEach(node => {
const point = this.nodePoints.get(node.id);
point.v = point.v
.add(point.a.scalarMultip(interval)) // 根据加速度求速度公式 V_curr= a*@t + V_pre
Expand All @@ -530,7 +530,7 @@ class ForceLayout {

updatePosition = (interval: number) => {
let sum = 0;
this.nodes.forEach((node) => {
this.nodes.forEach(node => {
const point = this.nodePoints.get(node.id);
const distance = point.v.scalarMultip(interval);
sum = sum + distance.magnitude();
Expand Down Expand Up @@ -559,7 +559,7 @@ class ForceLayout {
* @param {[type]} data [description]
*/
addNodes = (data: NodeType[]) => {
data.forEach((node) => {
data.forEach(node => {
this.addNode(new Node(node));
});
};
Expand Down Expand Up @@ -639,7 +639,7 @@ class ForceLayout {
const mass = this.getMass(node);
this.nodePoints.set(node.id, new Point(vec, node.id, node.data, mass));

this.edges.forEach((edge) => {
this.edges.forEach(edge => {
const source = this.nodePoints.get(edge.source.id);
const target = this.nodePoints.get(edge.target.id);
if (source.id === node.id || target.id === node.id) {
Expand Down

0 comments on commit 832cc2b

Please sign in to comment.