| 1 |
var _ = require("./lodash"); |
| 2 |
var util = require("./util"); |
| 3 |
|
| 4 |
module.exports = { |
| 5 |
run: run, |
| 6 |
cleanup: cleanup |
| 7 |
}; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
function run(g) { |
| 33 |
var root = util.addDummyNode(g, "root", {}, "_root"); |
| 34 |
var depths = treeDepths(g); |
| 35 |
var height = _.max(_.values(depths)) - 1; |
| 36 |
var nodeSep = 2 * height + 1; |
| 37 |
|
| 38 |
g.graph().nestingRoot = root; |
| 39 |
|
| 40 |
|
| 41 |
_.forEach(g.edges(), function(e) { g.edge(e).minlen *= nodeSep; }); |
| 42 |
|
| 43 |
|
| 44 |
var weight = sumWeights(g) + 1; |
| 45 |
|
| 46 |
|
| 47 |
_.forEach(g.children(), function(child) { |
| 48 |
dfs(g, root, nodeSep, weight, height, depths, child); |
| 49 |
}); |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
g.graph().nodeRankFactor = nodeSep; |
| 54 |
} |
| 55 |
|
| 56 |
function dfs(g, root, nodeSep, weight, height, depths, v) { |
| 57 |
var children = g.children(v); |
| 58 |
if (!children.length) { |
| 59 |
if (v !== root) { |
| 60 |
g.setEdge(root, v, { weight: 0, minlen: nodeSep }); |
| 61 |
} |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
var top = util.addBorderNode(g, "_bt"); |
| 66 |
var bottom = util.addBorderNode(g, "_bb"); |
| 67 |
var label = g.node(v); |
| 68 |
|
| 69 |
g.setParent(top, v); |
| 70 |
label.borderTop = top; |
| 71 |
g.setParent(bottom, v); |
| 72 |
label.borderBottom = bottom; |
| 73 |
|
| 74 |
_.forEach(children, function(child) { |
| 75 |
dfs(g, root, nodeSep, weight, height, depths, child); |
| 76 |
|
| 77 |
var childNode = g.node(child); |
| 78 |
var childTop = childNode.borderTop ? childNode.borderTop : child; |
| 79 |
var childBottom = childNode.borderBottom ? childNode.borderBottom : child; |
| 80 |
var thisWeight = childNode.borderTop ? weight : 2 * weight; |
| 81 |
var minlen = childTop !== childBottom ? 1 : height - depths[v] + 1; |
| 82 |
|
| 83 |
g.setEdge(top, childTop, { |
| 84 |
weight: thisWeight, |
| 85 |
minlen: minlen, |
| 86 |
nestingEdge: true |
| 87 |
}); |
| 88 |
|
| 89 |
g.setEdge(childBottom, bottom, { |
| 90 |
weight: thisWeight, |
| 91 |
minlen: minlen, |
| 92 |
nestingEdge: true |
| 93 |
}); |
| 94 |
}); |
| 95 |
|
| 96 |
if (!g.parent(v)) { |
| 97 |
g.setEdge(root, top, { weight: 0, minlen: height + depths[v] }); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
function treeDepths(g) { |
| 102 |
var depths = {}; |
| 103 |
function dfs(v, depth) { |
| 104 |
var children = g.children(v); |
| 105 |
if (children && children.length) { |
| 106 |
_.forEach(children, function(child) { |
| 107 |
dfs(child, depth + 1); |
| 108 |
}); |
| 109 |
} |
| 110 |
depths[v] = depth; |
| 111 |
} |
| 112 |
_.forEach(g.children(), function(v) { dfs(v, 1); }); |
| 113 |
return depths; |
| 114 |
} |
| 115 |
|
| 116 |
function sumWeights(g) { |
| 117 |
return _.reduce(g.edges(), function(acc, e) { |
| 118 |
return acc + g.edge(e).weight; |
| 119 |
}, 0); |
| 120 |
} |
| 121 |
|
| 122 |
function cleanup(g) { |
| 123 |
var graphLabel = g.graph(); |
| 124 |
g.removeNode(graphLabel.nestingRoot); |
| 125 |
delete graphLabel.nestingRoot; |
| 126 |
_.forEach(g.edges(), function(e) { |
| 127 |
var edge = g.edge(e); |
| 128 |
if (edge.nestingEdge) { |
| 129 |
g.removeEdge(e); |
| 130 |
} |
| 131 |
}); |
| 132 |
} |