feat(utils): getOutgoers function

This commit is contained in:
moklick
2019-07-26 01:17:12 +02:00
parent a6d191355a
commit 71afccbaf3
3 changed files with 21 additions and 5 deletions

View File

@@ -1,6 +1,6 @@
import React, { PureComponent } from 'react';
import Graph, { isEdge, removeElements } from '../src';
import Graph, { isEdge, removeElements, getOutgoers } from '../src';
// import Graph from '../dist/ReactGraph';
const SpecialNode = ({ data, onChange, styles }) => (
@@ -57,7 +57,7 @@ class App extends PureComponent {
}
onLoad(graphInstance) {
console.log('graph loaded:', this.graphInstance);
console.log('graph loaded:', graphInstance);
window.rg = graphInstance;
this.graphInstance = graphInstance;
@@ -89,6 +89,11 @@ class App extends PureComponent {
}));
}
onElementClick(element) {
console.log('click', element);
console.log('outgoers', getOutgoers(element, this.state.elements));
}
onZoomIn() {
this.graphInstance.zoomIn();
}
@@ -107,7 +112,7 @@ class App extends PureComponent {
return (
<Graph
elements={this.state.elements}
onElementClick={node => console.log('clicked', node)}
onElementClick={element => this.onElementClick(element)}
onElementsRemove={elements => this.onElementsRemove(elements)}
style={{ width: '100%', height: '100%' }}
onLoad={graphInstance => this.onLoad(graphInstance)}

View File

@@ -2,6 +2,15 @@ export const isEdge = element => element.source && element.target;
export const isNode = element => !element.source && !element.target;
export const getOutgoers = (node, elements) => {
if (!isNode(node)) {
return [];
}
const outgoerIds = elements.filter(e => e.source === node.id).map(e => e.target);
return elements.filter(e => outgoerIds.includes(e.id));
};
export const removeElements = (elements, elementsToRemove) => {
const nodeIdsToRemove = elementsToRemove.filter(isNode).map(n => n.id);
@@ -12,7 +21,7 @@ export const removeElements = (elements, elementsToRemove) => {
!nodeIdsToRemove.includes(e.source)
);
});
}
};
export const parseElements = e => {
e.type = e.type || 'default';

View File

@@ -2,11 +2,13 @@ import ReactGraph from './ReactGraph';
import {
isNode as _isNode ,
isEdge as _isEdge,
removeElements as _removeElements
removeElements as _removeElements,
getOutgoers as _getOutgoers
} from './graph-utils';
export const isNode = _isNode;
export const isEdge = _isEdge;
export const removeElements = _removeElements;
export const getOutgoers = _getOutgoers;
export default ReactGraph;