 
       #layout($scope.xxx)
       # ---
-      # generated by js2coffee 2.0.1+      # generated by js2coffee 2.0.1
+
+      get_flare = (links) ->
+        source_h = {} # hash of source nodes
+        target_h = {} # hash of target nodes
+
+
+        _.each links, (l) ->
+          target_h[l.target] = 1
+          if source_h[l.source]
+            source_h[l.source].push l.target
+          else
+            source_h[l.source] = [l.target]
+
+        root = get_root(source_h, target_h)
+
+        flare = {name: root, children: [_.uniq source_h[root]]}
+        console.log flare
+        # _.each _.keys(source_h), (k) ->
+        #   _.each _.keys(source_h), (k2) ->
+        #     #console.log $.inArray(k, source_h[k2])        console.log root
+
+
+      get_root = (sources, targets) ->
+        root = String
+        _.each _.keys(sources), (k) ->
+          root = k unless targets[k]
+        root
        new joint.shapes.basic.Rect(
          id: node.id
          size:
            width: 100
            height: height
          attrs:
            label: node.label
            hits:  node.hits
            message: node.text
            responses: node.responses
            text:
              text: (node.label || ( if node.text then node.text[0..6] + "...") || "") + " " + node.hits

    scope:
      nodes: '=',
    link: ($scope, $el, $attrs) ->



      # Helpers.
      # --------

      buildGraph = (data) ->

        flare = get_flare data.links


        elements = []
        links = []
        _.each data.nodes, (node) ->
