~bac/juju-gui/trunkcopy

« back to all changes in this revision

Viewing changes to lib/d3/examples/bundle/packages.js

  • Committer: kapil.foss at gmail
  • Date: 2012-07-13 18:45:59 UTC
  • Revision ID: kapil.foss@gmail.com-20120713184559-2xl7be17egsrz0c9
reshape

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
(function() {
2
 
  packages = {
3
 
 
4
 
    // Lazily construct the package hierarchy from class names.
5
 
    root: function(classes) {
6
 
      var map = {};
7
 
 
8
 
      function find(name, data) {
9
 
        var node = map[name], i;
10
 
        if (!node) {
11
 
          node = map[name] = data || {name: name, children: []};
12
 
          if (name.length) {
13
 
            node.parent = find(name.substring(0, i = name.lastIndexOf(".")));
14
 
            node.parent.children.push(node);
15
 
            node.key = name.substring(i + 1);
16
 
          }
17
 
        }
18
 
        return node;
19
 
      }
20
 
 
21
 
      classes.forEach(function(d) {
22
 
        find(d.name, d);
23
 
      });
24
 
 
25
 
      return map[""];
26
 
    },
27
 
 
28
 
    // Return a list of imports for the given array of nodes.
29
 
    imports: function(nodes) {
30
 
      var map = {},
31
 
          imports = [];
32
 
 
33
 
      // Compute a map from name to node.
34
 
      nodes.forEach(function(d) {
35
 
        map[d.name] = d;
36
 
      });
37
 
 
38
 
      // For each import, construct a link from the source to target node.
39
 
      nodes.forEach(function(d) {
40
 
        if (d.imports) d.imports.forEach(function(i) {
41
 
          imports.push({source: map[d.name], target: map[i]});
42
 
        });
43
 
      });
44
 
 
45
 
      return imports;
46
 
    }
47
 
 
48
 
  };
49
 
})();