source: Dev/trunk/d3/examples/bundle/packages.js @ 76

Last change on this file since 76 was 76, checked in by fpvanagthoven, 14 years ago

d3

File size: 1.2 KB
Line 
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})();
Note: See TracBrowser for help on using the repository browser.