Javascript aid

Soldato
Joined
7 Apr 2004
Posts
4,212
Hey,

The following code is something I found to display data in a treeview style control, please could someone tell me how i can adjust it so that when when an item is added the tree node isnt already expanded, by default on creation all tree paths are expanded. Basically i need to call the ParentFold function but i have no idea what the param is as it is only used once and is passed as 'this' on the onclick event. This code is all in a .js.

Code:
var FoldOut = new String("FoldOut.gif");
var FoldIn = new String("FoldIn.gif");
var StaticImage = new String("ListItem.gif");
var ImageFolder = new String("Images");
var LeftIndent = 20;
var TreeStarted = false;
var TreeParentName = new String();
var TreeNames = new Array();
var tmpHTML = new String();
var TreeHTML = new String();
var HasAbsoluteParent = false;

function NewTree(ParentItem)
{
	if(!ParentItem || ParentItem == ""){
		ParentItem = "NoParent";
	}
	if(TreeStarted == true){
		alert("You are still building a tree, please end the tree before starting another one");
		return false;
	}
	if(ParentItem != "NoParent"){
		if(CheckTreeNames(ParentItem) == false){
			alert("The Parent you are trying to add this tree to does not exist, please make sure you have created it and then put the code AFTER the parent tree code");
			return false;
		}else{
			TreeParentName = ParentItem;
		}
	}
	TreeStarted = true;
	if(TreeParentName != ""){
		tmpHTML = "<blockquote style = \"MARGIN-RIGHT: 0px; MARGIN-LEFT:" + LeftIndent + "px; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px;\">"
	}
}

function AddStandardItem(Name,ItemData,StaticImg,ItemCursor,Event)
{
	var IsParent = false;
	if(ItemCursor == "" || !ItemCursor){
		ItemCursor = "default";
	}
	if(Event != ""){
		Event = "onclick = \"" + Event + "\"";
	}
	if(!ItemData || ItemData == ""){
		alert("You must enter in some data for the item!");
		return false;
	}
	if(StaticImg == "" || !StaticImg){
		StaticImg = ImageFolder + "/" + StaticImage;
	}else{
		StaticImg = ImageFolder + "/" + StaticImg;
	}
	if(!Name || Name == ""){
		IsParent = false;
	}else{
		if(CheckTreeNames(Name) == true){
			alert("A tree item with this name already exists, please rename it");
			return false;
		}else{
			IsParent = true;
		}	
	}
	if(IsParent == true){
		AddTreeName(Name);
		tmpHTML += "<span id = \"" + Name + "\" onclick = \"ParentFold(this)\"  class = \"ParentNode\" style =\"cursor: " + ItemCursor + ";\"><img alt = \"Parent Node\" src=\"" + ImageFolder + "/" + FoldOut + "\" abs = \"middle\">&nbsp;" + ItemData + "</span><br />";
		tmpHTML += "%" + Name + "%";
	}else{
		tmpHTML += "<span class = \"ChildNode\" " + Event + " style =\"cursor: " + ItemCursor + ";\"><img alt = \"Child Node\" src=\"" + StaticImg + "\" abs = \"middle\">&nbsp;" + ItemData + "</span><br />";
	}
}

function EndTree()
{
	var rExpression;
	var rConstruct;
	if(TreeStarted != true){
		alert("you have not started a tree yet, please start one before trying to finish one");
		return false;
	}
	if(TreeParentName != ""){
		tmpHTML += "</blockquote>";
	}
	//tmpHTML += "</ul>";
	if(TreeParentName != ""){
		rConstruct = "%" + TreeParentName + "%";
		rExpression = rConstruct;
		TreeHTML = TreeHTML.replace(rExpression, tmpHTML);
	}else{
		TreeHTML += tmpHTML;
	}
	TreeStarted = false;
	TreeParentName = "";
	tmpHTML = "";
}

function GenerateTreeCode(Owner){
	if(TreeHTML != ""){
		if(!Owner || Owner == ""){
			document.write(TreeHTML);
		}else{
			document.getElementById(Owner).innerHTML = TreeHTML;
		}
	}else{
		alert("NO TREE CODE CREATED!");
	}
	TreeHTML = "";
}


function CheckTreeNames(TreeName)
{
	var FoundName = false;
	if(TreeNames.length > 0){
		for(var i = 0; i<TreeNames.length;i++)
		{
			if(TreeName == TreeNames[i])
			{
				FoundName = true;
			}
		}
	}
	return FoundName;
}

function AddTreeName(TreeName)
{
	if(TreeNames.length == 0)
	{
		TreeNames[0] = TreeName;
	}else{
		TreeNames[parseInt(TreeNames.length)] = TreeName;
	}
}

function ParentFold(Item)
{
	var ChildItem = new String();
	var TotalLoop = 0;
	ChildItem = Item.nextSibling;
	do{
		TotalLoop++;
		if(ChildItem.tagName != "BLOCKQUOTE"){
			ChildItem = ChildItem.nextSibling;
		}
		if(TotalLoop > 5){
			break;
		}
	}while(ChildItem.tagName != "BLOCKQUOTE");
	if(ChildItem.tagName == "BLOCKQUOTE"){
		if(ChildItem.style.display == "none"){
			ChildItem.style.display = "";
			Item.getElementsByTagName("IMG").item(0).src = "Images/FoldOut.gif";
		}else{
			ChildItem.style.display = "none";
			Item.getElementsByTagName("IMG").item(0).src = "Images/FoldIn.gif";
			
		}
	}
}

Cheers
Jack
 
Back
Top Bottom