Skip to content

Instantly share code, notes, and snippets.

@AlphaGit
Created April 19, 2014 18:48
Show Gist options
  • Save AlphaGit/11093696 to your computer and use it in GitHub Desktop.
Save AlphaGit/11093696 to your computer and use it in GitHub Desktop.
Sugiyama algorithm: add fake nodes when needed
var fakeNodeData = { __fake: true };
sugiyamaService.insertFakeNodeBeforeNext = function(list, currentNode, nextNode) {
var fakeNode = sugiyamaService.createNode(fakeNodeData);
sugiyamaService.disconnectNodes(currentNode, nextNode);
sugiyamaService.connectNodes(fakeNode, nextNode);
sugiyamaService.connectNodes(currentNode, fakeNode);
list.push(fakeNode);
};
sugiyamaService.addFakeNodes = function(columns) {
// check all columns except the last one
for (var columnIndex = 0; columnIndex < columns.length - 1; columnIndex++) {
var nextColumn = columns[columnIndex + 1];
for (var rowIndex = 0; rowIndex < columns[columnIndex].length; rowIndex++) {
var currentNode = columns[columnIndex][rowIndex];
for (var nextNodeIndex = 0; nextNodeIndex < currentNode.nextNodes.length; nextNodeIndex++) {
var nextNode = currentNode.nextNodes[nextNodeIndex];
if (nextColumn.indexOf(nextNode) === -1) {
sugiyamaService.insertFakeNodeBeforeNext(columns[columnIndex + 1], currentNode, nextNode);
}
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment