Skip to content

Commit

Permalink
Don't run layout if !nodes.length
Browse files Browse the repository at this point in the history
  • Loading branch information
richardwestenra committed Apr 7, 2020
1 parent 80f0f12 commit 78dbf90
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions src/selectors/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const getVisibleSidebar = state => state.visible.sidebar;
export const getGraph = createSelector(
[getVisibleNodes, getVisibleEdges, getHasVisibleLayers],
(nodes, edges, hasVisibleLayers) => {
if (!nodes.length || !edges.length) {
return;
}

const ranker = hasVisibleLayers ? 'none' : null;
const graph = new dagre.graphlib.Graph().setGraph({
ranker: hasVisibleLayers ? ranker : null,
Expand Down Expand Up @@ -48,31 +52,34 @@ export const getGraph = createSelector(
export const getLayoutNodes = createSelector(
[getGraph, getNodeType, getNodeLayer, getNodeActive],
(graph, nodeType, nodeLayer, nodeActive) =>
graph.nodes().map(nodeID => {
const node = graph.node(nodeID);
return Object.assign({}, node, {
layer: nodeLayer[nodeID],
type: nodeType[nodeID],
order: node.x + node.y * 9999,
active: nodeActive[nodeID]
});
})
graph
? graph.nodes().map(nodeID => {
const node = graph.node(nodeID);
return Object.assign({}, node, {
layer: nodeLayer[nodeID],
type: nodeType[nodeID],
order: node.x + node.y * 9999,
active: nodeActive[nodeID]
});
})
: []
);

/**
* Reformat edge data for use on the chart
*/
export const getLayoutEdges = createSelector(
[getGraph],
graph => graph.edges().map(edge => Object.assign({}, graph.edge(edge)))
graph =>
graph ? graph.edges().map(edge => Object.assign({}, graph.edge(edge))) : []
);

/**
* Get width, height and margin of graph
*/
export const getGraphSize = createSelector(
[getGraph],
graph => graph.graph()
graph => (graph ? graph.graph() : {})
);

/**
Expand Down Expand Up @@ -118,11 +125,7 @@ export const getChartSize = createSelector(
export const getZoomPosition = createSelector(
[getGraphSize, getChartSize],
(graph, chart) => {
if (
!Object.keys(chart).length ||
!Number.isFinite(graph.width) ||
!Number.isFinite(graph.width)
) {
if (!chart.width || !graph.width) {
return {
scale: 1,
translateX: 0,
Expand Down

0 comments on commit 78dbf90

Please sign in to comment.