diff options
author | Adam Spiers <git@adamspiers.org> | 2015-01-09 18:58:47 +0000 |
---|---|---|
committer | Adam Spiers <git@adamspiers.org> | 2015-01-10 15:19:51 +0000 |
commit | ad9a15d600713d72555ee539eac61064c395a5a5 (patch) | |
tree | 3d5dc351d49f73b0c3699add62822f3d186f3ba3 /html | |
parent | 45581669750c1787a2b105f2b52e373f4432efbc (diff) | |
download | git-deps-ad9a15d600713d72555ee539eac61064c395a5a5.tar.gz |
split out data-handling into git-deps-data.js
Diffstat (limited to 'html')
-rw-r--r-- | html/git-deps.html | 1 | ||||
-rw-r--r-- | html/js/git-deps-data.js | 99 | ||||
-rw-r--r-- | html/js/git-deps-graph.js | 99 |
3 files changed, 100 insertions, 99 deletions
diff --git a/html/git-deps.html b/html/git-deps.html index c74026a..add778b 100644 --- a/html/git-deps.html +++ b/html/git-deps.html @@ -13,6 +13,7 @@ <script language="javascript" type="text/javascript" src="js/noty.js"></script> <script language="javascript" type="text/javascript" src="js/fullscreen.js"></script> + <script language="javascript" type="text/javascript" src="js/git-deps-data.js"></script> <script language="javascript" type="text/javascript" src="js/git-deps-graph.js"></script> <link rel="stylesheet" type="text/css" href="css/animate.css" /> diff --git a/html/js/git-deps-data.js b/html/js/git-deps-data.js new file mode 100644 index 0000000..b970f23 --- /dev/null +++ b/html/js/git-deps-data.js @@ -0,0 +1,99 @@ +// The list of nodes, links, and constraints to feed into WebCola. +// These will be dynamically built as we retrieve them via XHR. +var nodes = [], links = [], constraints = []; + +// WebCola requires links to refer to nodes by index within the +// nodes array, so as nodes are dynamically added, we need to +// be able to retrieve their index efficiently in order to add +// links to/from them. This also allows us to avoid adding the +// same node twice. +var node_index = {}; + +// Constraints will be added to try to keep siblings at the same y +// position. For this we need to track siblings, which we do by +// mapping each parent to an array of its siblings in this hash. +// It also enables us to deduplicate links across multiple XHRs. +var deps = {}; + +// Returns 1 iff a node was added, otherwise 0. +function add_node(commit) { + if (commit.sha1 in node_index) { + return 0; + } + nodes.push(commit); + node_index[commit.sha1] = nodes.length - 1; + return 1; +} + +// Returns 1 iff a dependency was added, otherwise 0. +function add_dependency(parent_sha1, child_sha1) { + if (! (parent_sha1 in deps)) { + deps[parent_sha1] = {}; + } + if (child_sha1 in deps[parent_sha1]) { + // We've already got this link, presumably + // from a previous XHR. + return 0; + } + + deps[parent_sha1][child_sha1] = true; + + add_link(parent_sha1, child_sha1); + + return 1; +} + +function add_link(parent_sha1, child_sha1) { + var pi = node_index[parent_sha1]; + var ci = node_index[child_sha1]; + + var link = { + source: pi, + target: ci, + value: 1 // no idea what WebCola needs this for + }; + + links.push(link); +} + +function build_constraints() { + constraints = []; // FIXME: only rebuild constraints which changed + for (var parent_sha1 in deps) { + constraints.push(build_constraint(parent_sha1)); + } +} + +function build_constraint(parent_sha1) { + constraint = { + axis: 'y', + type: 'alignment', + offsets: [], + parent: parent_sha1 + }; + for (var child_sha1 in deps[parent_sha1]) { + constraint.offsets.push({ + node: node_index[child_sha1], + offset: 0 + }); + } + return constraint; +} + +// Returns true iff new data was added. +function add_data(data) { + var new_nodes = 0, new_deps = 0; + $.each(data.commits, function (i, commit) { + new_nodes += add_node(commit); + }); + $.each(data.dependencies, function (i, dep) { + new_deps += add_dependency(dep.parent, dep.child); + }); + + if (new_nodes > 0 || new_deps > 0) { + build_constraints(); + return [new_nodes, new_deps, data.root]; + } + + return false; +} + diff --git a/html/js/git-deps-graph.js b/html/js/git-deps-graph.js index 90ff12b..6257324 100644 --- a/html/js/git-deps-graph.js +++ b/html/js/git-deps-graph.js @@ -16,23 +16,6 @@ d3cola //.jaccardLinkLengths(100); .avoidOverlaps(true); -// The list of nodes, links, and constraints to feed into WebCola. -// These will be dynamically built as we retrieve them via XHR. -var nodes = [], links = [], constraints = []; - -// WebCola requires links to refer to nodes by index within the -// nodes array, so as nodes are dynamically added, we need to -// be able to retrieve their index efficiently in order to add -// links to/from them. This also allows us to avoid adding the -// same node twice. -var node_index = {}; - -// Constraints will be added to try to keep siblings at the same y -// position. For this we need to track siblings, which we do by -// mapping each parent to an array of its siblings in this hash. -// It also enables us to deduplicate links across multiple XHRs. -var deps = {}; - // d3 visualization elements. Kept global to aid in-browser debugging. var container, svg, fg, node, path, tip, tip_template; var zoom; @@ -135,88 +118,6 @@ function zoom_to_fit() { redraw(true); } -// Returns 1 iff a node was added, otherwise 0. -function add_node(commit) { - if (commit.sha1 in node_index) { - return 0; - } - nodes.push(commit); - node_index[commit.sha1] = nodes.length - 1; - return 1; -} - -// Returns 1 iff a dependency was added, otherwise 0. -function add_dependency(parent_sha1, child_sha1) { - if (! (parent_sha1 in deps)) { - deps[parent_sha1] = {}; - } - if (child_sha1 in deps[parent_sha1]) { - // We've already got this link, presumably - // from a previous XHR. - return 0; - } - - deps[parent_sha1][child_sha1] = true; - - add_link(parent_sha1, child_sha1); - - return 1; -} - -function add_link(parent_sha1, child_sha1) { - var pi = node_index[parent_sha1]; - var ci = node_index[child_sha1]; - - var link = { - source: pi, - target: ci, - value: 1 // no idea what WebCola needs this for - }; - - links.push(link); -} - -function build_constraints() { - constraints = []; // FIXME: only rebuild constraints which changed - for (var parent_sha1 in deps) { - constraints.push(build_constraint(parent_sha1)); - } -} - -function build_constraint(parent_sha1) { - constraint = { - axis: 'y', - type: 'alignment', - offsets: [], - parent: parent_sha1 - }; - for (var child_sha1 in deps[parent_sha1]) { - constraint.offsets.push({ - node: node_index[child_sha1], - offset: 0 - }); - } - return constraint; -} - -// Returns true iff new data was added. -function add_data(data) { - var new_nodes = 0, new_deps = 0; - $.each(data.commits, function (i, commit) { - new_nodes += add_node(commit); - }); - $.each(data.dependencies, function (i, dep) { - new_deps += add_dependency(dep.parent, dep.child); - }); - - if (new_nodes > 0 || new_deps > 0) { - build_constraints(); - return [new_nodes, new_deps, data.root]; - } - - return false; -} - function add_commitish(commitish) { if (! svg) { init_svg(); |