Hello All:
I'm having trouble with a recursive function.
The function is supposed to identify nested folders in a hierarchical
folder structure.
The function "searchForFolde rs()" is supposed to traverse sibling
nodes in each iteration, and for each sibling node, it calls itself
again, to see if there are child nodes of the current sibling.
The code below contains the function in question. For simplicity's
sake, I have replaced the images with text characters. It doesn't
look as good, but you should be able to see how the nesting works.
When you click on a "Folder", you will get debug text on the RHS of
the screen.
You will see there is an XML hierarchy. That is parsed by another
function in the page to write out the hierarchy. That is working
fine. The only functions that I am having trouble with are
"searchForFolde rs()" and "hideShowFolder ()".
Oh yeah, a lot of this is MS only script, so you'll need IE 5.5 or
above to view it correctly.
Thanks for any help you can give me.
Matt.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<LINK REL="stylesheet " TYPE="text/css" HREF="treecontr ol.css">
<title>Tree Control</title>
<XML ID="hierXml">
<main>
<tNode type="folder" label="Best Buyers">
<tNode type="object" label="Gold"/>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Gold"/>
<tNode type="object" label="Bronze"/>
</tNode>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Bronze"/>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Silver"/>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Bronze"/>
<tNode type="object" label="Gold"/>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Gold"/>
<tNode type="object" label="Gold"/>
</tNode>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Gold"/>
<tNode type="object" label="Gold"/>
</tNode>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Gold"/>
</tNode>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Gold"/>
</tNode>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Gold"/>
</tNode>
</tNode>
</tNode>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Gold"/>
</tNode>
</tNode>
</tNode>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Bronze"/>
</tNode>
</tNode>
</main>
</XML>
</head>
<body bgcolor="#ccccc c" onload="getXml( )">
<script src="treeContro lFns.js">
</script>
<script>
closeFolderArra y = new Array();
function hideShowFolder( ref)
{
//use pixelTop property to move DIVs up and down
//
//ref.style.pixel Top += 10;
//use visibility to hide DIVs
//
//ref.style.visib ility = "hidden";
//the children of the parent of the ref is all the folder/object
DIVs
//
//ref.parentNode. childNodes.leng th;
allNodes = ref.parentNode. childNodes;
moveInc = 0;
oneDivHeight = ref.offsetHeigh t;
moveArr = new Array();
//break up the clicked ref id into an array
clickedId = ref.id.split("_ ");
//lets find all the nested folders beneath that which was clicked
//anything that starts with the same array as the folder clicked
//and is a folder, will have to be closed
closeFolderArra y = new Array();
closeFolderArra y[closeFolderArra y.length] = ref.id;
debug.innerHTML = "";
searchForFolder s(clickedId);
debug.innerHTML += closeFolderArra y.join("<BR>");
sffInd = 0;
}
sffInd = 0;
function searchForFolder s(startId)
{
//indicate which iteration of sff we are in
sffInd++;
debug.innerHTML += "we are in iteration: "+sffInd+"\nsta rted with:
"+startId+"<br> <br>";
pushInd = 1;
startId[startId.length] = pushInd;
makeId = null;
makeId = startId.join("_ ");
if (!visArray[makeId])
{
return false;
}
while (visArray[makeId])
{
debug.innerHTML += "testing with "+makeId+"<br>< br>";
//test to see if present id is that of a folder
if (document.all(m akeId).getAttri bute("objtype") == "folder")
{
closeFolderArra y[closeFolderArra y.length] = makeId;
debug.innerHTML += "add "+makeId+" in iteration
"+sffInd+"<br>< br>";
}
//call next iteration of sff to look for child folders of present
object
if (visArray[makeId+"_1"])
{
tempArr = makeId.split("_ ");
debug.innerHTML += "startId is now "+startId+" and tempArr is
"+tempArr+"<br> <br>";
//searchForFolder s(tempArr);
}
//continue incrementing
startId[startId.length-1] = ++pushInd;
makeId = startId.join("_ ");
}
//return;
}
//active object
activeObj = "";
//graphic elements for folder points and object points
folderPrefix = " &nbs p;--[F] ";
objectPrefix = " &nbs p;----(o) ";
lastFolderPrefi x = " &nbs p;--[F] ";
lastObjectPrefi x = " &nbs p;----(o) ";
objectDivStart = "<div objtype=object style=\"visibil ity:visible\"
onclick=\"objec tClick(this);\" class=\"objectD iv\" ";
folderDivStart = "<div objtype=folder objstate=open
style=\"visibil ity:visible\" class=\"folderD iv\"
onclick=\"hideS howFolder(this) \" ";
divMid = " />";
textSpanStart=" <span class=\"textSpa n\" id=\"";
textSpanMid="\" />";
textSpanEnd = "</span>";
holderText = "";
id = "a_";
blankGif = "--";
iGif = "";
visArray = new Array();
//xml parsing and screen rendering
function getXml()
{
//if xml is available, call recurser function on the main node
if (hierXml.readyS tate == "complete")
{
mainNode = hierXml.childNo des(0);
mainChildInc = 0;
curMainNode = mainNode.childN odes(0);
recurser(curMai nNode,"","",1);
}
printer.innerHT ML = holderText;
}
function recurser(node,g fxPrefix,prefix Str,siblingCoun t)
{
//loop through sibling nodes
while (node)
{
//set the prefix string, which becomes the id and the map
//back to the XML object
localPrefixStr = prefixStr+sibli ngCount;
//set graphical vars based on what type of node this is
//and whether it is the last sibling
if (node.getAttrib ute("type") == "folder")
{
itemPrefix = (node == node.parentNode .lastChild) ?
lastFolderPrefi x : folderPrefix;
itemDivStart = folderDivStart;
}
else
{
itemPrefix = (node == node.parentNode .lastChild) ?
lastObjectPrefi x : objectPrefix;
itemDivStart = objectDivStart;
}
itemGfxPrefix = (node == node.parentNode .lastChild) ?
gfxPrefix+blank Gif : gfxPrefix+iGif;
visArray["a"+localPrefix Str] = "visible";
if (node.getAttrib ute("type") == "object")
{
}
//add HTML to display this node
holderText += itemDivStart+"i d=\"a"+localPre fixStr+"\""+div Mid+gfxPrefix+i temPrefix+textS panStart+"span_ a"+localPrefixS tr+textSpanMid+ node.getAttribu te("label")+"
("+localPrefixS tr+")"+textSpan End+"</div>";
//call this function to process any child nodes of the current
node
recurser(node.f irstChild,itemG fxPrefix,localP refixStr+"_",1) ;
//increment node and sibling count for while loop
node = node.nextSiblin g;
siblingCount++;
}
}
</script>
<table>
<tr>
<td>
<div class="instruct ions">
<ul>
<li>string in parantheses is the ID that indicates a node's
hierarchical placement
<li>selected object ID: <span style="font-weight:bold" id="counter">
</span>
<ul>
</div>
<div class="printer" id="printer">
</div>
</td>
<td>
<div id="debug">
</div>
</td>
</tr>
<table>
<br>
<br>
<br>
</body>
</html>
I'm having trouble with a recursive function.
The function is supposed to identify nested folders in a hierarchical
folder structure.
The function "searchForFolde rs()" is supposed to traverse sibling
nodes in each iteration, and for each sibling node, it calls itself
again, to see if there are child nodes of the current sibling.
The code below contains the function in question. For simplicity's
sake, I have replaced the images with text characters. It doesn't
look as good, but you should be able to see how the nesting works.
When you click on a "Folder", you will get debug text on the RHS of
the screen.
You will see there is an XML hierarchy. That is parsed by another
function in the page to write out the hierarchy. That is working
fine. The only functions that I am having trouble with are
"searchForFolde rs()" and "hideShowFolder ()".
Oh yeah, a lot of this is MS only script, so you'll need IE 5.5 or
above to view it correctly.
Thanks for any help you can give me.
Matt.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<LINK REL="stylesheet " TYPE="text/css" HREF="treecontr ol.css">
<title>Tree Control</title>
<XML ID="hierXml">
<main>
<tNode type="folder" label="Best Buyers">
<tNode type="object" label="Gold"/>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Gold"/>
<tNode type="object" label="Bronze"/>
</tNode>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Bronze"/>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Silver"/>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Bronze"/>
<tNode type="object" label="Gold"/>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Gold"/>
<tNode type="object" label="Gold"/>
</tNode>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Gold"/>
<tNode type="object" label="Gold"/>
</tNode>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Gold"/>
</tNode>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Gold"/>
</tNode>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Gold"/>
</tNode>
</tNode>
</tNode>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Gold"/>
</tNode>
</tNode>
</tNode>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Bronze"/>
</tNode>
</tNode>
</main>
</XML>
</head>
<body bgcolor="#ccccc c" onload="getXml( )">
<script src="treeContro lFns.js">
</script>
<script>
closeFolderArra y = new Array();
function hideShowFolder( ref)
{
//use pixelTop property to move DIVs up and down
//
//ref.style.pixel Top += 10;
//use visibility to hide DIVs
//
//ref.style.visib ility = "hidden";
//the children of the parent of the ref is all the folder/object
DIVs
//
//ref.parentNode. childNodes.leng th;
allNodes = ref.parentNode. childNodes;
moveInc = 0;
oneDivHeight = ref.offsetHeigh t;
moveArr = new Array();
//break up the clicked ref id into an array
clickedId = ref.id.split("_ ");
//lets find all the nested folders beneath that which was clicked
//anything that starts with the same array as the folder clicked
//and is a folder, will have to be closed
closeFolderArra y = new Array();
closeFolderArra y[closeFolderArra y.length] = ref.id;
debug.innerHTML = "";
searchForFolder s(clickedId);
debug.innerHTML += closeFolderArra y.join("<BR>");
sffInd = 0;
}
sffInd = 0;
function searchForFolder s(startId)
{
//indicate which iteration of sff we are in
sffInd++;
debug.innerHTML += "we are in iteration: "+sffInd+"\nsta rted with:
"+startId+"<br> <br>";
pushInd = 1;
startId[startId.length] = pushInd;
makeId = null;
makeId = startId.join("_ ");
if (!visArray[makeId])
{
return false;
}
while (visArray[makeId])
{
debug.innerHTML += "testing with "+makeId+"<br>< br>";
//test to see if present id is that of a folder
if (document.all(m akeId).getAttri bute("objtype") == "folder")
{
closeFolderArra y[closeFolderArra y.length] = makeId;
debug.innerHTML += "add "+makeId+" in iteration
"+sffInd+"<br>< br>";
}
//call next iteration of sff to look for child folders of present
object
if (visArray[makeId+"_1"])
{
tempArr = makeId.split("_ ");
debug.innerHTML += "startId is now "+startId+" and tempArr is
"+tempArr+"<br> <br>";
//searchForFolder s(tempArr);
}
//continue incrementing
startId[startId.length-1] = ++pushInd;
makeId = startId.join("_ ");
}
//return;
}
//active object
activeObj = "";
//graphic elements for folder points and object points
folderPrefix = " &nbs p;--[F] ";
objectPrefix = " &nbs p;----(o) ";
lastFolderPrefi x = " &nbs p;--[F] ";
lastObjectPrefi x = " &nbs p;----(o) ";
objectDivStart = "<div objtype=object style=\"visibil ity:visible\"
onclick=\"objec tClick(this);\" class=\"objectD iv\" ";
folderDivStart = "<div objtype=folder objstate=open
style=\"visibil ity:visible\" class=\"folderD iv\"
onclick=\"hideS howFolder(this) \" ";
divMid = " />";
textSpanStart=" <span class=\"textSpa n\" id=\"";
textSpanMid="\" />";
textSpanEnd = "</span>";
holderText = "";
id = "a_";
blankGif = "--";
iGif = "";
visArray = new Array();
//xml parsing and screen rendering
function getXml()
{
//if xml is available, call recurser function on the main node
if (hierXml.readyS tate == "complete")
{
mainNode = hierXml.childNo des(0);
mainChildInc = 0;
curMainNode = mainNode.childN odes(0);
recurser(curMai nNode,"","",1);
}
printer.innerHT ML = holderText;
}
function recurser(node,g fxPrefix,prefix Str,siblingCoun t)
{
//loop through sibling nodes
while (node)
{
//set the prefix string, which becomes the id and the map
//back to the XML object
localPrefixStr = prefixStr+sibli ngCount;
//set graphical vars based on what type of node this is
//and whether it is the last sibling
if (node.getAttrib ute("type") == "folder")
{
itemPrefix = (node == node.parentNode .lastChild) ?
lastFolderPrefi x : folderPrefix;
itemDivStart = folderDivStart;
}
else
{
itemPrefix = (node == node.parentNode .lastChild) ?
lastObjectPrefi x : objectPrefix;
itemDivStart = objectDivStart;
}
itemGfxPrefix = (node == node.parentNode .lastChild) ?
gfxPrefix+blank Gif : gfxPrefix+iGif;
visArray["a"+localPrefix Str] = "visible";
if (node.getAttrib ute("type") == "object")
{
}
//add HTML to display this node
holderText += itemDivStart+"i d=\"a"+localPre fixStr+"\""+div Mid+gfxPrefix+i temPrefix+textS panStart+"span_ a"+localPrefixS tr+textSpanMid+ node.getAttribu te("label")+"
("+localPrefixS tr+")"+textSpan End+"</div>";
//call this function to process any child nodes of the current
node
recurser(node.f irstChild,itemG fxPrefix,localP refixStr+"_",1) ;
//increment node and sibling count for while loop
node = node.nextSiblin g;
siblingCount++;
}
}
</script>
<table>
<tr>
<td>
<div class="instruct ions">
<ul>
<li>string in parantheses is the ID that indicates a node's
hierarchical placement
<li>selected object ID: <span style="font-weight:bold" id="counter">
</span>
<ul>
</div>
<div class="printer" id="printer">
</div>
</td>
<td>
<div id="debug">
</div>
</td>
</tr>
<table>
<br>
<br>
<br>
</body>
</html>
Comment