<!DOCTYPE html>
<meta charset="utf-8">
<body>
  <link href="../static/css/collapse.css" rel="stylesheet">
  <link href="../static/css/tiros.css" rel="stylesheet">
  <link rel="stylesheet" type="text/css" href="http://cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.min.css"></link>

   <div class="lookup">
        <div id="searchName"></div>
    </div>

<script src="http://cdnjs.cloudflare.com/ajax/libs/d3/3.4.13/d3.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.min.js"></script>

<script>
$("#searchName").on("select2-selecting", function(e) {
    clearAll(root);
    expandAll(root);
    update(root);
    searchField = "d.name";
    searchText = e.object.text;
    searchTree(root);
    root.children.forEach(collapseAllNotFound);
    update(root);
})


//===============================================
function select2DataCollectName(d) {
    if (d.children)
        d.children.forEach(select2DataCollectName);
    else if (d._children)
        d._children.forEach(select2DataCollectName);
    select2Data.push(d.name);
}

//===============================================
function searchTree(d) {
    if (d.children)
        d.children.forEach(searchTree);
    else if (d._children)
        d._children.forEach(searchTree);
    var searchFieldValue = eval(searchField);
    if (searchFieldValue && searchFieldValue.match(searchText)) {
            // Walk parent chain
            var ancestors = [];
            var parent = d;
            while (typeof(parent) !== "undefined") {
                ancestors.push(parent);
		//console.log(parent);
                parent.class = "found";
                parent = parent.parent;
            }
	    //console.log(ancestors);
    }
}

//===============================================
function clearAll(d) {
    d.class = "";
    if (d.children)
        d.children.forEach(clearAll);
    else if (d._children)
        d._children.forEach(clearAll);
}

//===============================================
function collapse(d) {
    if (d.children) {
        d._children = d.children;
        d._children.forEach(collapse);
        d.children = null;
    }
}

//===============================================
function collapseAllNotFound(d) {
    if (d.children) {
    	if (d.class !== "found") {
        	d._children = d.children;
        	d._children.forEach(collapseAllNotFound);
        	d.children = null;
	} else
        	d.children.forEach(collapseAllNotFound);
    }
}

//===============================================
function expandAll(d) {
    if (d._children) {
        d.children = d._children;
        d.children.forEach(expandAll);
        d._children = null;
    } else if (d.children)
        d.children.forEach(expandAll);
}

function showInfo(d){
  var html = "";
    if(d.properties) {
      for(var key in d.properties) {
        if (key!="color"){
        html += "<p align=\"left\"><b>"+key.replace(/_/g, " ")+"</b>: " + d.properties[key] + "</p>";
       }
     }
  }
 tooltipdiv.transition()
        .duration(100)
        .style("opacity", .9);
  tooltipdiv.html(html)
        .style("left", (d3.event.pageX) + "px")
        .style("top", (d3.event.pageY - 28) + "px");
}

//===============================================
// Toggle children on click.
function toggle(d) {

  if (d.children) {
    d._children = d.children;
    d.children = null;
  } else {
    d.children = d._children;
    d._children = null;
  }
  $("#searchName").select2("val", "");
  clearAll(root);
  update(d);
}

//===============================================
var margin = {top: 20, right: 120, bottom: 20, left: 120},
    width = 960 - margin.right - margin.left,
    height = 800 - margin.top - margin.bottom;

var i = 0,
    duration = 750,
    root;

var tooltipdiv = d3.select("body").append("div")
        .attr("class", "tooltip")
        .style("opacity", 0);

var tree = d3.layout.tree()
    .size([height, width]);

var diagonal = d3.svg.diagonal()
    .projection(function(d) { return [d.y, d.x]; });

var svg = d3.select('body').append("svg")
    .attr("width", width + margin.right + margin.left)
    .attr("height", height + margin.top + margin.bottom)
    .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var metadata = d3.select('body').append("div").attr("class", "njg-metadata"),
    metadataInner = metadata.append("div").attr("class", "njg-inner"),
    html = "";

metadata.attr("class", "njg-metadata").style("display", "block");
html += "<h3> Tiros Snapshot Visualization </h3>";
html += "<p><b>Snapshot View</b>: <span> Collapse </span></p>";
html += "<form action=\"snap_view\" method=\"post\" enctype=\"multipart/form-data\">"
html += "<select name=\"selection\" onchange=\"this.form.submit()\">"
html+=  "<option value=\"\">Query View</option>"
html+= "<option value=\"vsi\">VPC-Subnet-Instance</option>"
html+= "<option value=\"acl\">ACL</option>"
html+= "</select>"
html+="</form>"
html+="</br></br>"
html += "<form action=\"collapse_view\" method=\"post\" enctype=\"multipart/form-data\">"
html += "<select name=\"selection\" onchange=\"this.form.submit()\">"
html+=  "<option value=\"\">Search View</option>"
html+= "<option value=\"vsi_collapse\">VPC-Subnet-Instance</option>"
html+= "</select>"
html+="</form>"


metadataInner.html(html);
metadata.classed("njg-hidden", false);

d3.json('{{snapshot_file}}', function(error, flare) {
  root = flare;
  root.x0 = height / 2;
  root.y0 = 0;

  select2Data = [];
  select2DataCollectName(root);
  select2DataObject = [];
  select2Data.sort(function(a, b) {
            if (a > b) return 1; // sort
            if (a < b) return -1;
            return 0;
        })
        .filter(function(item, i, ar) {
            return ar.indexOf(item) === i;
        }) // remove duplicate items
        .filter(function(item, i, ar) {
            select2DataObject.push({
                "id": i,
                "text": item
            });
        });
    select2Data.sort(function(a, b) {
            if (a > b) return 1; // sort
            if (a < b) return -1;
            return 0;
        })
        .filter(function(item, i, ar) {
            return ar.indexOf(item) === i;
        }) // remove duplicate items
        .filter(function(item, i, ar) {
            select2DataObject.push({
                "id": i,
                "text": item
            });
        });
  $("#searchName").select2({
        data: select2DataObject,
        placeholder: "Search Asset",
        containerCssClass: "search"
  });

  function collapse(d) {
    if (d.children) {
      d._children = d.children;
      d._children.forEach(collapse);
      d.children = null;
    }
  }

  root.children.forEach(collapse);
  update(root);
});

d3.select(self.frameElement).style("height", "800px");

function update(source) {

  // Compute the new tree layout.
  var nodes = tree.nodes(root).reverse(),
      links = tree.links(nodes);

  // Normalize for fixed-depth.
  nodes.forEach(function(d) { d.y = d.depth * 180; });

  // Update the nodes…
  var node = svg.selectAll("g.node")
      .data(nodes, function(d) { return d.id || (d.id = ++i); });

  // Enter any new nodes at the parent's previous position.
  var nodeEnter = node.enter().append("g")
      .attr("class", "node")
      .attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
      .on("click", toggle)
      .on("mouseover", showInfo)
      .on("mouseout", function(d) {
            tooltipdiv.transition()
                .duration(500)
                .style("opacity", 0);
        });


  nodeEnter.append("svg:image")
          .attr("xlink:href", function(d) {
            if (d.name == "TirosSnapshot"){
              return '../static/img/aws.png';
            }
            switch(d.properties['color']){
              case 'vpc': return '../static/img/vpc.png';
              case 'subnet':return '../static/img/subnet.png';
              case 'instance': return '../static/img/instance.png';
              case 'sg': return '../static/img/sg.png';
              default: return d._children ? "lightsteelblue" : "#fff";
          }})
          .attr("x", "-12px")
          .attr("y", "-12px")
          .attr("width", "24px")
          .attr("height", "24px");

  nodeEnter.append("text")
      .attr("x", function(d) { return d.children || d._children ? -10 : 10; })
      .attr("dy", ".35em")
      .attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; })
      .text(function(d) { return d.name; })
      .style("fill-opacity", 1e-6);

  // Transition nodes to their new position.
  var nodeUpdate = node.transition()
      .duration(duration)
      .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });

  nodeUpdate.select("circle")
      .attr("r", 4.5)
      .style("fill", function(d) {
            if (d.class === "found") {
                return "#ff4136"; //red
            } else if (d._children) {
                return "lightsteelblue";
            } else {
                return "#fff";
            }
        })
        .style("stroke", function(d) {
            if (d.class === "found") {
                return "#ff4136"; //red
            }
        });

  nodeUpdate.select("text")
      .style("fill-opacity", 1);

  // Transition exiting nodes to the parent's new position.
  var nodeExit = node.exit().transition()
      .duration(duration)
      .attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
      .remove();

  nodeExit.select("circle")
      .attr("r", 1e-6);

  nodeExit.select("text")
      .style("fill-opacity", 1e-6);

  // Update the links…
  var link = svg.selectAll("path.link")
      .data(links, function(d) { return d.target.id; });

  // Enter any new links at the parent's previous position.
  link.enter().insert("path", "g")
      .attr("class", "link")
      .attr("d", function(d) {
        var o = {x: source.x0, y: source.y0};
        return diagonal({source: o, target: o});
      });

  // Transition links to their new position.
  link.transition()
      .duration(duration)
      .attr("d", diagonal)
      .style("stroke", function(d) {
            if (d.target.class === "found") {
                return "#ff4136";
            }
        });

  // Transition exiting nodes to the parent's new position.
  link.exit().transition()
      .duration(duration)
      .attr("d", function(d) {
        var o = {x: source.x, y: source.y};
        return diagonal({source: o, target: o});
      })
      .remove();

  // Stash the old positions for transition.
  nodes.forEach(function(d) {
    d.x0 = d.x;
    d.y0 = d.y;
  });
}
</script>
</body>
</html>
