From 3587d83d58a7f7e41b65f6cb4aed5c383cb2531d Mon Sep 17 00:00:00 2001 From: David Kaltschmidt Date: Tue, 10 Nov 2015 18:15:32 +0100 Subject: [PATCH] Position internet nodes above/below graph --- client/app/scripts/charts/nodes-layout.js | 73 +++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/client/app/scripts/charts/nodes-layout.js b/client/app/scripts/charts/nodes-layout.js index 05daf643cc..1068b40948 100644 --- a/client/app/scripts/charts/nodes-layout.js +++ b/client/app/scripts/charts/nodes-layout.js @@ -251,6 +251,78 @@ function shiftLayoutToCenter(layout, opts) { return layout; } +/** + * Position the internet nodes above/below the rest of the graph + * @param {Object} layout Layout with nodes and edges + * @param {Object} opts Options with scale + * @return {Object} modified layout + */ +function positionInternet(layout, opts) { + const options = opts || {}; + const scale = options.scale || (val => val * 2); + const delta = scale(2.5); + const IN = 'in-theinternet'; + const OUT = 'out-theinternet'; + let nodes = layout.nodes; + let edges = layout.edges; + + if (nodes.has(IN)) { + // shift IN node to the top + const minYNode = nodes.minBy(node => node.get('y')); + nodes = nodes.setIn([IN, 'y'], minYNode.get('y') - delta); + + // adjust negative coordinates + const minY = minYNode.get('y') - delta; + if (minY < 0) { + nodes = nodes.map(node => { + return node.set('y', node.get('y') - minY + delta); + }); + } + + // adjust edges that connect to IN node + const inNode = nodes.get(IN); + edges = edges.map(edge => { + if (edge.get('source') === IN) { + const points = edge.get('points'); + points[0] = {x: inNode.get('x'), y: inNode.get('y')}; + return edge.set('points', points); + } + if (edge.get('target') === IN) { + const points = edge.get('points'); + points[points.length - 1] = {x: inNode.get('x'), y: inNode.get('y')}; + return edge.set('points', points); + } + return edge; + }); + } + + if (nodes.has(OUT)) { + // shift OUT node to the bottom + const maxYNode = nodes.maxBy(node => node.get('y')); + nodes = nodes.setIn([OUT, 'y'], maxYNode.get('y') + delta); + + // adjust edges that connect to OUT node + const outNode = nodes.get(OUT); + edges = edges.map(edge => { + if (edge.get('source') === OUT) { + const points = edge.get('points'); + points[0] = {x: outNode.get('x'), y: outNode.get('y')}; + return edge.set('points', points); + } + if (edge.get('target') === OUT) { + const points = edge.get('points'); + points[points.length - 1] = {x: outNode.get('x'), y: outNode.get('y')}; + return edge.set('points', points); + } + return edge; + }); + } + + layout.edges = edges; + layout.nodes = nodes; + return layout; +} + /** * Adds `points` array to edge based on location of source and target * @param {Map} edge new edge @@ -373,6 +445,7 @@ export function doLayout(immNodes, immEdges, opts) { if (!layout) { return layout; } + layout = positionInternet(layout, opts); layout = layoutSingleNodes(layout, opts); layout = shiftLayoutToCenter(layout, opts); }