1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
/* This code is based on the one originally provided by
Geir Landr in his dTree 2.05 package. You can get it
at : www.destroydrop.com/javascript/tree/.
Therefore, the DTDDoc team considers that this code is
Copyright (c) 2002-2003 Geir Landr. Since the original
author didn't clearly forbids copies of this part, we
assume we're not doing anything wrong in porviding it
to you, in a modified or non-modified form.
*/
/*
Geir Landr : Orignal version, for dTree.
Michael Koehrsen (10/2004) : Original modification to
allow DTDDoc to use this.
Stefan Champailler (10/2004) : Make sure that the first
level of the tree is not shown (aesthetic stuff).
*/
//----------------------------------------------------------------
// CCTree
// Implements a DHTML tree with the following features:
// - Supports a general directed graph as the underlying model.
// - Supports a concept of an "always open" node.
//----------------------------------------------------------------
// Private class _CCTreeModelNode
function _CCTreeModelNode(id,label,link,alwaysOpen,initiallyOpen)
{
this.id = id;
this.label = label;
this.link = link;
this.alwaysOpen = alwaysOpen;
this.initiallyOpen = initiallyOpen;
// children and childLabels are parallel arrays.
// By default, childLabels[i] == children[i].label,
// but the link operation may optionally specify
// a child label that is specific to that parent-child
// relationship.
this.children = new Array();
this.childLabels = new Array();
}
_CCTreeModelNode.prototype.addChild = function(child,childLabel)
{
this.children.push(child);
if (childLabel) this.childLabels.push(childLabel);
else this.childLabels.push(child.label);
}
// Private class _CCDisplayNode
function _CCDisplayNode(modelNode,parentNode,treeId,label)
{
this.modelNode = modelNode;
this.parentNode = parentNode;
this.treeId = treeId;
if (label) this.label = label;
else this.label = modelNode.label;
this.isLastChild = false;
if (this.parentNode) {
this.id = this.parentNode.id + ":" + this.modelNode.id;
/* Stefan Champailler : This little fix is clever ! Let's check the
following tree:
alpha (1) -+-> beta (69)
\-> beta (69).
This describes a tree with three nodes. Two of them are links
(beta). In that case, both the "beta" node have an id of "1:69".
So if one calls openNode on any of them, what happens is that only
one actually gets opened. To prevent that, I change the id of the
node on the fly to make sure there are only unique id's.
Please note this code is not very efficient (and yes, the right
number of slash will be added :)) */
for( var k=0; k<parentNode.children.length; k++) {
if( parentNode.children[k].id == this.id)
this.id = this.id + "/";
}
/* end of fix */
}
else this.id = this.modelNode.id;
CCTree.trees[this.treeId].allDisplayNodes[this.id] = this;
this.isOpen = this.modelNode.alwaysOpen || this.modelNode.initiallyOpen;
if (this.isOpen)
{
this.constructChildren();
}
}
_CCDisplayNode.prototype.toInnerHTML = function()
{
var indent = "";
if (this.isOpen && !this.children) this.constructChildren();
if (this.isLastChild)
{
if (this.modelNode.alwaysOpen)
indent = this.imageTag("joinbottom.gif");
else if (this.isOpen)
indent = this.imageTag("minusbottom.gif","close")
else if (this.modelNode.children.length)
indent = this.imageTag("plusbottom.gif","open");
else
indent = this.imageTag("joinbottom.gif");
}
else
{
if (this.modelNode.alwaysOpen)
indent = this.imageTag("join.gif");
else if (this.isOpen)
indent = this.imageTag("minus.gif","close");
else if (this.modelNode.children.length)
indent = this.imageTag("plus.gif","open");
else
indent = this.imageTag("join.gif");
}
/* Construct a horizontal line of the tree */
var currAnc = this.parentNode;
while (currAnc)
{
if (currAnc.isLastChild)
indent = this.imageTag("empty.gif") + indent;
else
indent = this.imageTag("line.gif") + indent;
currAnc = currAnc.parentNode;
}
var result = indent + this.nodeContent();
/* Recurse deeper in the tree */
if (this.isOpen && this.children)
{
var ix;
for (ix in this.children)
{
result += this.children[ix];
}
}
return result;
}
_CCDisplayNode.prototype.toString = function()
{
return "<div class='cctree-node' id='" + this.divId() + "'>" + this.toInnerHTML() + "</div>";
}
_CCDisplayNode.prototype.constructChildren = function()
{
if (this.modelNode.children.length > 0)
{
this.children = new Array();
var ix;
for (ix in this.modelNode.children)
{
this.children.push(new _CCDisplayNode(this.modelNode.children[ix],
this,
this.treeId,
this.modelNode.childLabels[ix]));
}
this.children[this.children.length-1].isLastChild = true;
}
}
_CCDisplayNode.prototype.imageTag = function(imgName,action)
{
var href = null;
if (action == "open") href="CCTree.trees[" + this.treeId + "].openNode('" + this.id + "')";
if (action == "close") href="CCTree.trees[" + this.treeId + "].closeNode('" + this.id + "')";
if (href) return "<a href=\"javascript:" + href + "\"><img src='img/" + imgName + "' border='0'></a>";
else return "<img src='img/" + imgName + "'>";
}
_CCDisplayNode.prototype.divId = function()
{
return "CCTree_" + this.treeId + "_" + this.id;
}
_CCDisplayNode.prototype.nodeContent = function()
{
var target = "";
if (CCTree.trees[this.treeId].linkTarget) target = " target='" + CCTree.trees[this.treeId].linkTarget + "'";
if (this.modelNode.link)
return "<a href='" + this.modelNode.link + "'" + target + ">" + this.label + "</a>";
else return this.label;
}
_CCDisplayNode.prototype.open = function()
{
this.isOpen = true;
// document.all is known to work on IE but not on Konqueror or Mozilla.
// So I've changed it to something more portable.
//document.all[this.divId()].innerHTML = this.toInnerHTML();
document.getElementById(this.divId()).innerHTML = this.toInnerHTML();
}
_CCDisplayNode.prototype.close = function()
{
this.isOpen = false;
//document.all[this.divId()].innerHTML = this.toInnerHTML();
document.getElementById(this.divId()).innerHTML = this.toInnerHTML();
}
// Public class CCTree
CCTree = function(linkTarget)
{
// may have multiple roots:
this.rootModelNodes = new Array();
this.allModelNodes = new Array();
this.treeId = CCTree.trees.length;
this.allDisplayNodes = new Array(); // indexed by id
this.linkTarget = linkTarget;
CCTree.trees.push(this);
}
// static variables
CCTree.trees = new Array();
CCTree.prototype.addRootNode = function (id,label,link,alwaysOpen,initiallyOpen)
{
this.rootModelNodes[id] = this.addNode(id,label,link,alwaysOpen,initiallyOpen);
}
CCTree.prototype.addNode = function(id,label,link,alwaysOpen,initiallyOpen)
{
var newNode = new _CCTreeModelNode(id,label,link,alwaysOpen,initiallyOpen);
this.allModelNodes[id] = newNode;
return newNode;
}
CCTree.prototype.linkNodes = function(parentId,childId,childLabel)
{
this.allModelNodes[parentId].addChild(this.allModelNodes[childId],childLabel);
}
CCTree.prototype.constructDisplayNodes = function()
{
this.rootDisplayNodes = new Array();
var ix;
for (ix in this.rootModelNodes)
{
this.rootDisplayNodes.push(new _CCDisplayNode(this.rootModelNodes[ix],null,this.treeId));
}
this.rootDisplayNodes[this.rootDisplayNodes.length-1].isLastChild = true;
}
CCTree.prototype.openNode = function(displayNodeId)
{
this.allDisplayNodes[displayNodeId].open();
}
CCTree.prototype.closeNode = function(displayNodeId)
{
this.allDisplayNodes[displayNodeId].close();
}
CCTree.prototype.toString = function()
{
this.constructDisplayNodes();
var ix;
var result = "";
for (ix in this.rootDisplayNodes)
{
result += this.rootDisplayNodes[ix].toString();
}
return result;
}
|