﻿<?xml version="1.0" encoding="utf-8" ?> 
<application-components>
   <application-component id = "tree" participant-id ="tree">
      <![CDATA[
         component_init:function(){
					Hemi.include("hemi.css");
					Hemi.css.loadStyleSheet(g_application_path + "Styles/TreeStyles.css","Tree");
								   
					this.scopeHandler("loadxml",0,0,1);
					this.scopeHandler("expandnode",0,0,1);
					this.scopeHandler("switchbranch",0,0,1);
					
					this.getProperties().tree_loaded = 0;
					this.getProperties().current_path = 0;
					this.getProperties().current_group_id = 0;
					this.getProperties().current_group_name = 0;
					this.getObjects().current_node = 0;
					this.getObjects().cache = [];
					var dir_id = this.getContainer().getAttribute("DirectoryId");
					if(dir_id != null && dir_id.length > 0) this.RePopulateTree();
				 },
				 component_destroy : function(){
						 this._prehandle_loadxml = 0;
						 this._prehandle_expandnode = 0;
						 this._prehandle_switchbranch = 0;
				 },
				 GetCurrentGroupName : function(){
						return this.getProperties().current_group_name;
				 },
				 GetCurrentGroupId : function(){
						 return this.getProperties().current_group_id;
				 },
				 GetCurrentPath : function(){
						 return this.getProperties().current_path;
				 },
				 RePopulateTree : function(){
						this.getObjects().cache = [];
						Hemi.xml.removeChildren(this.getContainer());
						this.PopulateTree(this.getContainer(),this.getContainer().getAttribute("DirectoryId"),this.getContainer());
				 },
				 PopulateTree : function(oParent, sId, oSubParent){
						var sReq = "";
						if(typeof sId == "number"){
									sReq = "ID-" + sId;
									this.getObjects().sub_parent = oSubParent;
						}
						else if(typeof sId == "string"){
									sReq = sId;
						}
						this.getObjects().parent = oParent;
                        /// alert(oParent);
						if(this.getObjects().cache[sReq]) this._handle_loadxml("direct",this.getObjects().cache[sReq]);
						Hemi.xml.getXml(g_application_path + "Explorer/Directory/" + sReq + "?is-xml=1&ts=" + (new Date()).getTime(),this._prehandle_loadxml,1,0,0);
				},
				_handle_viewerload : function(s, v){
						if(this.getProperties().tree_loaded) this.serveTransaction("treeload",this);
				},
				_handle_contextload : function(s, v){
						if(this.getProperties().tree_loaded) this.serveTransaction("treeload",this);
				},
				_handle_loadxml : function(s, v){
					try{
						var xGroups = (s == "direct" ? v : v.xdom), _p = this.getObjects();
						if(xGroups.getElementsByTagName("Login-Required").length > 0){
							alert("Login required to browse persistent storage.");
							return;
						}
						var oGroups = xGroups.getElementsByTagName("Groups");
						var oGroups = (oGroups.length > 0 ? oGroups[0] : null);
						if(!oGroups) return;
						if(_p.sub_parent){
							var oSubGroup = 0;
							for(var c = 0; c < oGroups.childNodes.length; c++){
								if(oGroups.childNodes[c].nodeType == 1 && oGroups.childNodes[c].nodeName == "Group"){
									oSubGroup = oGroups.childNodes[c];
									break;
								}
							}
							if(!oSubGroup) return;
							_p.sub_parent.group_path = this.GetElementText(oSubGroup,"Path");
							this.PrintSubDataTree(_p.sub_parent, oSubGroup);
							this.SetActiveLeaf(_p.sub_parent);
						}
						else{
							this.PrintTree(this.getObjects().parent, oGroups);
							this.getObjects().parent.group_path = this.GetElementText(oGroups,"Path");
							this.getObjects().parent.group_name = this.GetElementText(oGroups,"Name");
							this.getObjects().parent.group_id = this.GetElementText(oGroups,"Id");
							this.SetActiveLeaf(this.getObjects().parent);
						}

						this.getObjects().parent =0;
						this.getObjects().sub_parent = 0;
						if(!this.getProperties().tree_loaded){
							this.getProperties().tree_loaded = 1;
							this.serveTransaction("treeload",this);
						}
						else{
							this.serveTransaction("treenavigate",this);
						}
					}
					catch(e){
						alert(e.name + "\n" + e.number + "\n" + e.description + "\n" + e.message);
					}
				},
					
				GetBoolElementText : function(oParent, sName){
					var b = 0;
					var sText = this.GetElementText(oParent,sName);
					if(sText && sText.match(/^true$/i) || sText.match(/^1$/)) b = 1;
					return b;
				},
				
				GetElementText : function(oParent, sName){
						var aN = oParent.getElementsByTagName(sName);
						if(aN.length == 0) return ""; // was 0;
						return Hemi.xml.getInnerText(aN[0]);
				},
				SelectLeaf : function(sName){
					var aLi = this.getContainer().getElementsByTagName("li");
					//alert(sName);
					for(var i = 0; i < aLi.length; i++){
						if(aLi[i].group_name == sName){
							this.SetActiveLeaf(aLi[i]);
							if(!aLi[i].is_populated){
								this._handle_expandnode(aLi[i].firstChild, 1);
							}
							else{
								this._handle_switchbranch(aLi[i].firstChild, 1);
							}
							break;
						}
					}
				},
				SetActiveLeaf : function(oLi){
						if(this.getObjects().current_node && this.getObjects().current_node != oLi){

								this.getObjects().current_node.firstChild.className = this.getObjects().current_node.firstChild.className.replace(/\s?branch\_selected/,"");
						}
					  this.getObjects().current_node = oLi;
						if(!oLi.firstChild.className.match(/\s?branch\_selected/)) oLi.firstChild.className += " branch_selected";
						this.getProperties().current_path = oLi.group_path;
						this.getProperties().current_group_name = oLi.group_name;
						this.getProperties().current_group_id = oLi.group_id;
				},
				_handle_switchbranch : function(e, b){
					e = (e ? e : event);
					var o = b ? e : (e.target ? e.target : e.srcElement);
					if(!o){
						return;
					}
					var oP = o.parentNode;
					if(oP.className.match(/branch\_empty/)){
					}
					else if(oP.className.match(/branch\_open/)) oP.className = "branch_close";
					else if(oP.className.match(/branch\_close/)) oP.className = "branch_open";
					this.SetActiveLeaf(oP);
					this.serveTransaction("treenavigate",this);
					
					/*
					if(oP.className.match(/branch\_open/)) oP.className = "branch_close";
					else oP.className = "branch_open";
					*/
					return false;
				},
				_handle_expandnode : function(e,b){
					var oLink = (b ? e : Hemi.event.getEventSource(e));
					oLink.onclick = this._prehandle_switchbranch;
					
					var oLi = oLink.parentNode;
					oLi.is_populated = 1;
					oLi.className = "branch_open";
					this.PopulateTree(oLi,oLi.group_id,oLi);
					//this.SetActiveLeaf(oLi);
					
				},
				PrintTree : function(oXhtmlParent, oXmlParentNode, iDepth){
					if(!iDepth){
					
						var oUL = document.createElement("ul");
						oUL.className = "tree";
						oXhtmlParent.appendChild(oUL);
						iDepth = 1;
						oXhtmlParent = oUL;
					}
					
					for(var i = 0; i < oXmlParentNode.childNodes.length; i++){
						var oC = oXmlParentNode.childNodes[i];
						if(oC.nodeType != 1) continue;
						if(oC.nodeName == "Group"){
							var oLI = document.createElement("li");
							
							oXhtmlParent.appendChild(oLI);
							var sName = this.GetElementText(oC, "Name");
							var sId = this.GetElementText(oC, "Id");
							oLI.group_id = parseInt(sId);
							oLI.group_name = sName;
							oLI.parent_id = parseInt(this.GetElementText(oC,"ParentId"));
							oLI.policy = this.ImportPolicy(oC);
							
							var oA = document.createElement("a");
							oA.setAttribute("href","#");
							var isPop = this.GetBoolElementText(oC, "IsPopulated");
							if(oLI.policy && !oLI.policy.read){
								oLI.className = "branch_lock";
							}
							else{
								oLI.is_populated = isPop;
								if(isPop){
									oLI.className = "branch_open";
									oLI.group_path = this.GetElementText(oC,"Path");
									oA.onclick = this._prehandle_switchbranch;
								}
								else{
									oA.onclick = this._prehandle_expandnode;
									oLI.className = "branch_close";
								}
								this.InstrumentDraggable(oLI);
							}
							oA.appendChild(document.createTextNode(sName));
							oLI.appendChild(oA);
							this.PrintSubDataTree(oLI, oC);

						}
					}
					
				},
				PrintSubDataTree : function(oXhtmlParent, oXmlNode){
					
					var oSub = this.GetChildNode(oXmlNode,"SubDirectories");
					var oDat = this.GetChildNode(oXmlNode,"DataList");
					if((!oSub || oSub.getElementsByTagName("Group").length == 0) && (!oDat || oDat.getElementsByTagName("Data").length == 0) && oXhtmlParent.is_populated){
						oXhtmlParent.className = "branch_empty";
						//oXhtmlParent.firstChild.onclick = null;
						return;
					}
					var oSUL = document.createElement("ul");
					oSUL.className = "tree";
					oXhtmlParent.appendChild(oSUL);
					if(oSub) this.PrintTree(oSUL, oSub, 1);
					var aDat = [];
					if(oDat) aDat = oDat.getElementsByTagName("Data");
					for(var c = 0; c < aDat.length; c++){
						oLI = document.createElement("li");
						var oA = document.createElement("a");
						oA.setAttribute("href","#");
						oLI.appendChild(oA);
						oA.appendChild(document.createTextNode(this.GetElementText(aDat[c],"Name")));
						oLI.className = "leaf";
						oLI.data_id = parseInt(this.GetElementText(aDat[c],"Id"));
						oLI.policy = this.ImportPolicy(aDat[c]);
						this.InstrumentDraggable(oLI);
						oSUL.appendChild(oLI);
					}
				},
				InstrumentDraggable : function(oNode){
					
					var oX = Hemi.object.xhtml.newInstance(oNode,1);
					var oA = Hemi.app.comp.newInstance(0,0,oX.getObjectId(),0,0,1);
					oA.setAsync(0);
					oA.loadComponent("draggable","Components/component.draggable.xml");
					
				},
				GetChildNode : function(oXmlNode, sName){
					var oC = 0;
					for(var i = 0; i < oXmlNode.childNodes.length; i++){
						if(oXmlNode.childNodes[i].nodeType == 1 && oXmlNode.childNodes[i].nodeName == sName){
							oC = oXmlNode.childNodes[i];
							break;
						}
					}
					return oC;
				},
				ImportPolicy : function(oXmlNode){
					var oP = this.GetChildNode(oXmlNode,"Policy");
					if(!oP) return 0;
					
					for(var i = 0; i < oXmlNode.length; i++){
						
					}
					var bRead = this.GetBoolElementText(oP,"Read");
					var bWrite = this.GetBoolElementText(oP,"Write");
					var bChange = this.GetBoolElementText(oP,"Change");
					var bDelete = this.GetBoolElementText(oP,"Delete");
					var sStatement = this.GetElementText(oP,"Statement");
					return {
						read:bRead,
						write:bWrite,
						change:bChange,
						del:bDelete,
						statement:sStatement
					};
					
				}
			]]>
		</application-component>
</application-components>