<?xml version="1.0" encoding="UTF-8"?>
<Template id="Hierarchy Example">
<!-- Enter valid XHTML -->

<p>
<input type = "button" value = "Org" onclick = "${this}.DoOrg()" />
<input type = "button" value = "JSON" onclick = "${this}.DoToJSON()" />
<input type = "button" value = "Eval" onclick = "${this}.EvalHierarchy()" />
<input type = "button" value = "Clear Hierarchy" onclick = "${this}.DoClearHierarchy()" />
<input type = "text" rid = "last_id" style ="width:25px;" value = "0" />
<input type = "text" rid = "node_name" value = "" />
<input type = "button" value = "Add" onclick = "${this}.DoAdd()" />
<input type = "button" value = "Render" onclick = "${this}.GetComponentByRID('hierarchy').Render()" />
</p>
<div rid = "canvas" acid = "canvas" width = "600" height = "400"  appcomp_path = "/DWAC/steve/Components/canvas"></div>
<span rid = "hierarchy" acid = "hierarchy" appcomp_path = "/DWAC/steve/Components/hierarchy"></span>
<span rid = "hierarchy-decorator" acid = "hierarchy-decorator" appcomp_path = "/DWAC/steve/Components/hierarchy-decorator"></span>
<textarea rid = "hierarchy-text" style = "width:100%;height:200px;"></textarea>

<import-xml src = "/DWAC/steve/Fragments/ID-139" id = "TemplateTools" />

<embedded-script><![CDATA[
DoToJSON : function()
{
   this.GetElementByRID("hierarchy-text").value = this.GetComponentByRID("hierarchy").NodeHierarchyToJSON();
},

DoClear : function()
{
   this.GetComponentByRID("hierarchy").getPointers().canvas.Clear();
},

DoOrg : function()
{
   this.DoClear();
 this.GetComponentByRID("hierarchy").Render();
},

DoClearHierarchy : function()
{
   this.GetComponentByRID("hierarchy").ClearHierarchy();
},

template_init : function()
{
   this.GetComponentByRID("hierarchy").getPointers().hierarchy_decorator = this.GetComponentByRID("hierarchy-decorator");
   this.GetComponentByRID("hierarchy-decorator").SetHierarchy(this.GetComponentByRID("hierarchy"));
   this.GetComponentByRID("hierarchy").SetCanvasComponent(this.GetComponentByRID("canvas"));
   this.GetComponentByRID("canvas").Resize(600,400);
   //this.LoadDemo();
},

DoAdd : function()
{
   var oHC = this.GetComponentByRID("hierarchy");
   var sName = this.GetElementByRID("node_name").value;
   if(!sName.length) sName = "Node";
   var iLastId = parseInt(this.GetElementByRID("last_id").value);
   if(isNaN(iLastId)) iLastId = 0;
   var oP = 0;
   if(iLastId >= 0) oP = oHC.getPointers().nodes[iLastId];
   var oNode = oHC.NewOrganizationNode(++oHC.getStatus().node_counter,sName,oP);
   this.GetElementByRID("last_id").value = oNode.index;
   oHC.Render();
},

LoadHierarchy : function(vNode, oParent)
{
   var oOrg = this.GetComponentByRID("hierarchy").NewOrganizationNode(vNode.id,vNode.name,oParent);
   for(var i = 0; i < vNode.nodes.length; i++){
      this.LoadHierarchy(vNode.nodes[i],oOrg);
      }
},

EvalHierarchy : function()
{
   this.DoClearHierarchy();
   var sVal = this.GetElementByRID("hierarchy-text").value;
   var x;
   eval("x=" + sVal);
   if(x.Hierarchy) x = x.Hierarchy;
   for(var i = 0; i < x.length; i++){
      this.LoadHierarchy(x[i]);
      }
   this.GetComponentByRID("hierarchy").Render();
}
]]></embedded-script>
</Template>
