toolkit/javascript/d3/examples/bundle/packages.js
changeset 47 c0b4a8b5a012
equal deleted inserted replaced
46:efd9c589177a 47:c0b4a8b5a012
       
     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 })();