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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
/* This file is a part of CanDyDOC fileset.
File is written by Victor Nakoryakov and placed into the public domain.
This file is javascript with classes that represents native style tree control. */
var pmNone = 0;
var pmPlus = 1;
var pmMinus = 2;
var hlNone = 0;
var hlGrey = 1;
var hlSelected = 2;
function TreeView(hrefMode)
{
this.domEntry = document.createElement("div");
this.children = new Array();
this.selection = null;
this.hrefMode = hrefMode;
this.createBranch = function(text, iconSrc)
{
var root = new TreeNode(text, iconSrc, this.hrefMode);
root.owner = this;
this.children[ this.children.length ] = root;
this.domEntry.appendChild( root.domEntry );
return root;
}
this.branch = function(text)
{
var ret = null;
for (var i = 0; i < this.children.length; ++i)
if (this.children[i].textElement.data == text)
{
ret = this.children[i];
break;
}
return ret;
}
this.domEntry.style.fontSize = "10px";
this.domEntry.style.cursor = "default";
this.domEntry.style.whiteSpace = "nowrap";
}
var idCounter = 0;
function TreeNode(text, iconSrc, hrefMode)
{
this.id = idCounter++;
this.parentNode = null;
this.children = new Array();
this.domEntry = document.createElement("div");
this.icon = document.createElement("img");
this.textElement = document.createTextNode(text);
this.textSpan = document.createElement("span");
this.lineDiv = document.createElement("div");
this.hierarchyImgs = new Array();
this.onclick = null;
function createIcon()
{
var img = document.createElement("img");
img.style.verticalAlign = "middle";
img.style.position = "relative";
img.style.top = "-1px";
img.width = 16;
img.height = 16;
return img;
}
function createHierarchyImage()
{
var img = createIcon();
img.pointsTop = false;
img.pointsBottom = false;
img.pointsRight = false;
img.pmState = pmNone;
return img;
}
function genHierarchyImageSrc(hierarchyImg)
{
var name = "";
if (hierarchyImg.pointsTop)
name += "t";
if (hierarchyImg.pointsBottom)
name += "b";
if (hierarchyImg.pointsRight)
name += "r";
if (hierarchyImg.pmState == pmPlus)
name += "p";
else if (hierarchyImg.pmState == pmMinus)
name += "m";
if (name == "")
name = "shim";
return "../../candydoc/img/tree/" + name + ".gif";
}
function setSrc(icon, src)
{
icon.src = src;
// After src change width and height are reseted in IE.
// Bug workaround:
icon.width = 16;
icon.height = 16;
}
this.createChild = function(text, iconSrc)
{
var child = new TreeNode(text, iconSrc, this.owner.hrefMode);
this.children[ this.children.length ] = child;
this.domEntry.appendChild( child.domEntry );
child.parentNode = this;
child.owner = this.owner;
// insert hierarchy images according to deepness level
// of created child.
if (this.children.length > 1)
{
// there were already added child before. So copy `level-1`
// hierarchy images from it.
var prevAddedChild = this.children[ this.children.length - 2 ];
for (var i = 0; i < prevAddedChild.hierarchyImgs.length - 1; ++i)
{
var prevAddedChildImg = prevAddedChild.hierarchyImgs[i];
var img = createHierarchyImage();
setSrc(img, prevAddedChildImg.src);
img.pointsTop = prevAddedChildImg.pointsTop;
img.pointsBottom = prevAddedChildImg.pointsBottom;
img.pointsRight = prevAddedChildImg.pointsRight;
img.pmState = prevAddedChildImg.pmState;
child.hierarchyImgs[ child.hierarchyImgs.length ] = img;
child.lineDiv.insertBefore(img, child.icon);
}
// change last hierarchy image of prevAddedChild from |_ to |-
var lastHierarchyImg = prevAddedChild.hierarchyImgs[ prevAddedChild.hierarchyImgs.length - 1 ];
lastHierarchyImg.pointsBottom = true;
setSrc(lastHierarchyImg, genHierarchyImageSrc(lastHierarchyImg));
// change hierarchy images of prevAddedChild's children on it's last
// level to |
prevAddedChild.addHierarchyTBLine(prevAddedChild.hierarchyImgs.length - 1);
}
else
{
// this is a first child. So copy `level-2`
// hierarchy images from parent, i.e. this.
for (var i = 0; i < this.hierarchyImgs.length - 1; ++i)
{
var parentImg = this.hierarchyImgs[i];
var img = createHierarchyImage();
setSrc(img, parentImg.src);
img.pointsTop = parentImg.pointsTop;
img.pointsBottom = parentImg.pointsBottom;
img.pointsRight = parentImg.pointsRight;
img.pmState = parentImg.pmState;
child.hierarchyImgs[ child.hierarchyImgs.length ] = img;
child.lineDiv.insertBefore(img, child.icon);
}
if (this.hierarchyImgs.length > 0) // we are not root
{
// change last hierarchy image of parent (i.e. this): add minus to it
var lastHierarchyImg = this.hierarchyImgs[ this.hierarchyImgs.length - 1];
lastHierarchyImg.pmState = pmMinus;
setSrc(lastHierarchyImg, genHierarchyImageSrc(lastHierarchyImg));
lastHierarchyImg.owner = this;
lastHierarchyImg.onclick = new Function("e", "this.owner.processPMClick(e);");
// make decision on image on `level-1`. It depends on parent's (ie this)
// image on same level.
var parentL1HierarchyImg = lastHierarchyImg;
var l1HierarchyImg = createHierarchyImage();
if (parentL1HierarchyImg.pointsBottom)
{
l1HierarchyImg.pointsTop = true;
l1HierarchyImg.pointsBottom = true;
}
setSrc(l1HierarchyImg, genHierarchyImageSrc(l1HierarchyImg));
child.hierarchyImgs[ child.hierarchyImgs.length ] = l1HierarchyImg;
child.lineDiv.insertBefore(l1HierarchyImg, child.icon);
}
}
// in any case on last level our child will have icon |_
var img = createHierarchyImage();
img.pointsTop = true;
img.pointsRight = true;
setSrc(img, genHierarchyImageSrc(img));
child.hierarchyImgs[ child.hierarchyImgs.length ] = img;
child.lineDiv.insertBefore(img, child.icon);
return child;
}
this.lastChild = function()
{
return this.children[ this.children.length - 1 ];
}
this.child = function(text)
{
var ret = null;
for (var i = 0; i < this.children.length; ++i)
if (this.children[i].textElement.data == text)
{
ret = this.children[i];
break;
}
return ret;
}
this.addHierarchyTBLine = function(level)
{
for (var i = 0; i < this.children.length; ++i)
{
var img = this.children[i].hierarchyImgs[level];
img.pointsTop = true;
img.pointsBottom = true;
setSrc(img, genHierarchyImageSrc(img));
this.children[i].addHierarchyTBLine(level);
}
}
this.expand = function()
{
var img = this.hierarchyImgs[ this.hierarchyImgs.length - 1 ];
if (img.pmState == pmPlus)
{
img.pmState = pmMinus;
setSrc(img, genHierarchyImageSrc(img));
for (var i = 0; i < this.children.length; ++i)
this.children[i].domEntry.style.display = "";
}
}
this.collapse = function()
{
var img = this.hierarchyImgs[ this.hierarchyImgs.length - 1 ];
if (img.pmState == pmMinus)
{
img.pmState = pmPlus;
setSrc(img, genHierarchyImageSrc(img));
for (var i = 0; i < this.children.length; ++i)
this.children[i].domEntry.style.display = "none";
}
}
this.toggle = function()
{
var img = this.hierarchyImgs[ this.hierarchyImgs.length - 1 ];
if (img.pmState == pmMinus)
this.collapse();
else
this.expand();
}
this.select = function()
{
if (this.owner.selection != this)
{
if (this.owner.selection)
this.owner.selection.setHighlight(hlNone);
this.owner.selection = this;
this.setHighlight(hlSelected);
}
}
this.setHighlight = function(mode)
{
if (mode == hlNone)
{
this.textSpan.style.backgroundColor = "";
this.textSpan.style.color = "";
this.textSpan.style.border = "";
}
else if (mode == hlGrey)
{
this.textSpan.style.backgroundColor = "#aaaaaa";
this.textSpan.style.color = "";
this.textSpan.style.border = "";
}
else if (mode == hlSelected)
{
this.textSpan.style.backgroundColor = "3399cc";
this.textSpan.style.color = "white";
this.textSpan.style.border = "dotted 1px red";
}
}
this.setOnclick = function(proc)
{
this.onclick = proc;
}
this.setRef = function(url)
{
if (this.anchor)
this.anchor.href = url;
}
this.processPMClick = function(e)
{
this.toggle();
// prevent this line selection, stop bubbling
if (e)
e.stopPropagation(); // Mozilla way
if (window.event)
window.event.cancelBubble = true; // IE way
}
this.processOnclick = function()
{
this.select();
if (this.onclick instanceof Function)
this.onclick();
}
///////////////////////////////////////////////////////////////////////////
if (iconSrc)
this.icon.src = iconSrc;
else
{
this.icon.width = 0;
this.icon.height = 0;
}
this.icon.style.verticalAlign = "middle";
this.icon.style.position = "relative";
this.icon.style.top = "-1px";
this.icon.style.paddingRight = "2px";
if (!hrefMode)
{
this.textSpan.appendChild( this.textElement );
}
else
{
this.anchor = document.createElement("a");
this.anchor.appendChild( this.textElement );
this.textSpan.appendChild( this.anchor );
}
this.lineDiv.appendChild( this.icon );
this.lineDiv.appendChild( this.textSpan );
this.domEntry.appendChild( this.lineDiv );
this.lineDiv.owner = this;
if (!hrefMode)
this.lineDiv.onclick = new Function("this.owner.processOnclick();");
}
|