Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix example node/edge deletion #340

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 50 additions & 37 deletions src/examples/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ type IGraphProps = {};

type IGraphState = {
graph: any,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, pls don't use any, instead of mentioned relevant type. Let me know if you need any help, Thanks.

selected: any,
selected: SelectionT | null,
totalNodes: number,
copiedNode: null | INode,
Expand All @@ -246,8 +245,6 @@ class Graph extends React.Component<IGraphProps, IGraphState> {
graph: sample,
layoutEngineType: undefined,
selected: null,
selectedNodes: null,
selectedEdges: null,
totalNodes: sample.nodes.length,
allowMultiselect: true,
locationOverrides: {},
Expand Down Expand Up @@ -381,35 +378,6 @@ class Graph extends React.Component<IGraphProps, IGraphState> {
this.setState({ graph });
};

// Deletes a node from the graph
onDeleteNode = (viewNode: INode, nodeId: string, nodeArr: INode[]) => {
// Note: onDeleteEdge is also called from react-digraph for connected nodes
const graph = this.state.graph;

graph.nodes = nodeArr;

this.deleteEdgesForNode(nodeId);

this.setState({ graph, selected: null });
};

// Whenever a node is deleted the consumer must delete any connected edges.
// react-digraph won't call deleteEdge for multi-selected edges, only single edge selections.
deleteEdgesForNode(nodeID: string) {
const { graph } = this.state;
const edgesToDelete = graph.edges.filter(
edge => edge.source === nodeID || edge.target === nodeID
);

const newEdges = graph.edges.filter(
edge => edge.source !== nodeID && edge.target !== nodeID
);

edgesToDelete.forEach(edge => {
this.onDeleteEdge(edge, newEdges);
});
}

// Creates a new node between two edges
onCreateEdge = (sourceViewNode: INode, targetViewNode: INode) => {
const graph = this.state.graph;
Expand Down Expand Up @@ -461,11 +429,56 @@ class Graph extends React.Component<IGraphProps, IGraphState> {
});
};

// Called when an edge is deleted
onDeleteEdge = (viewEdge: IEdge, edges: IEdge[]) => {
onDeleteSelected = (selection: SelectionT) => {
const graph = this.state.graph;
let newEdges = this.state.graph.edges;

// If there are selected nodes, first delete
// all edges associated with this node
if (selection.nodes) {
selection.nodes.forEach(selectedNode => {
newEdges = newEdges.filter(testEdge => {
// If there is an edge with a source or target pointing
// to this node, filter it out
if (
testEdge.source === selectedNode.id ||
testEdge.target === selectedNode.id
) {
return false;
}

return true;
});
});
}

// Delete all selected nodes
const newNodes = this.state.graph.nodes.filter(testNode => {
const testNodeId = testNode.id;

// Filter out if the selection contains this node
if (selection.nodes && selection.nodes.has(testNodeId)) {
return false;
}

return true;
});

// Delete all selected edges
newEdges = newEdges.filter(testEdge => {
const testEdgeId = `${testEdge.source}_${testEdge.target}`;

// Filter out if the selection contains this edge
if (selection.edges && selection.edges.has(testEdgeId)) {
return false;
}

return true;
});

graph.nodes = newNodes;
graph.edges = newEdges;

graph.edges = edges;
this.setState({
graph,
selected: null,
Expand Down Expand Up @@ -640,10 +653,10 @@ class Graph extends React.Component<IGraphProps, IGraphState> {
onSelect={this.onSelect}
onCreateNode={this.onCreateNode}
onUpdateNode={this.onUpdateNode}
onDeleteNode={this.onDeleteNode}
onCreateEdge={this.onCreateEdge}
onSwapEdge={this.onSwapEdge}
onDeleteEdge={this.onDeleteEdge}
canDeleteSelected={() => true}
onDeleteSelected={this.onDeleteSelected}
onUndo={this.onUndo}
onCopySelected={this.onCopySelected}
onPasteSelected={this.onPasteSelected}
Expand Down