1 /*--------------------------------------------------|
\r
3 | dTree 2.05 | www.destroydrop.com/javascript/tree/ |
\r
5 |---------------------------------------------------|
\r
7 | Copyright (c) 2002-2003 Geir Landrö |
\r
11 | This script can be used freely as long as all |
\r
13 | copyright messages are intact. |
\r
17 | Updated: 17.04.2003 |
\r
19 |--------------------------------------------------*/
\r
25 function Node(id, pid, name, url, title, target, icon, iconOpen, open) {
\r
37 this.target = target;
\r
41 this.iconOpen = iconOpen;
\r
43 this._io = open || false;
\r
61 function dTree(objName) {
\r
69 useSelection : true,
\r
77 useStatusText : false,
\r
79 closeSameLevel : false,
\r
87 root : 'img/base.gif',
\r
89 folder : 'img/folder.gif',
\r
91 folderOpen : 'img/folderopen.gif',
\r
93 node : 'img/page.gif',
\r
95 empty : 'img/empty.gif',
\r
97 line : 'img/line.gif',
\r
99 join : 'img/join.gif',
\r
101 joinBottom : 'img/joinbottom.gif',
\r
103 plus : 'img/plus.gif',
\r
105 plusBottom : 'img/plusbottom.gif',
\r
107 minus : 'img/minus.gif',
\r
109 minusBottom : 'img/minusbottom.gif',
\r
111 nlPlus : 'img/nolines_plus.gif',
\r
113 nlMinus : 'img/nolines_minus.gif'
\r
117 this.obj = objName;
\r
123 this.root = new Node(-1);
\r
125 this.selectedNode = null;
\r
127 this.selectedFound = false;
\r
129 this.completed = false;
\r
135 // Adds a new node to the node array
\r
137 dTree.prototype.add = function(id, pid, name, url, title, target, icon, iconOpen, open) {
\r
139 this.aNodes[this.aNodes.length] = new Node(id, pid, name, url, title, target, icon, iconOpen, open);
\r
145 // Open/close all nodes
\r
147 dTree.prototype.openAll = function() {
\r
153 dTree.prototype.closeAll = function() {
\r
161 // Outputs the tree to the page
\r
163 dTree.prototype.toString = function() {
\r
165 var str = '<div class="dtree">\n';
\r
167 if (document.getElementById) {
\r
169 if (this.config.useCookies) this.selectedNode = this.getSelected();
\r
171 str += this.addNode(this.root);
\r
173 } else str += 'Browser not supported.';
\r
177 if (!this.selectedFound) this.selectedNode = null;
\r
179 this.completed = true;
\r
187 // Creates the tree structure
\r
189 dTree.prototype.addNode = function(pNode) {
\r
195 if (this.config.inOrder) n = pNode._ai;
\r
197 for (n; n<this.aNodes.length; n++) {
\r
199 if (this.aNodes[n].pid == pNode.id) {
\r
201 var cn = this.aNodes[n];
\r
209 if (!cn.target && this.config.target) cn.target = this.config.target;
\r
211 if (cn._hc && !cn._io && this.config.useCookies) cn._io = this.isOpen(cn.id);
\r
213 if (!this.config.folderLinks && cn._hc) cn.url = null;
\r
215 if (this.config.useSelection && cn.id == this.selectedNode && !this.selectedFound) {
\r
219 this.selectedNode = n;
\r
221 this.selectedFound = true;
\r
225 str += this.node(cn, n);
\r
239 // Creates the node icon, url and text
\r
241 dTree.prototype.node = function(node, nodeId) {
\r
243 var str = '<div class="dTreeNode">' + this.indent(node, nodeId);
\r
245 if (this.config.useIcons) {
\r
247 if (!node.icon) node.icon = (this.root.id == node.pid) ? this.icon.root : ((node._hc) ? this.icon.folder : this.icon.node);
\r
249 if (!node.iconOpen) node.iconOpen = (node._hc) ? this.icon.folderOpen : this.icon.node;
\r
251 if (this.root.id == node.pid) {
\r
253 node.icon = this.icon.root;
\r
255 node.iconOpen = this.icon.root;
\r
259 str += '<img id="i' + this.obj + nodeId + '" src="' + ((node._io) ? node.iconOpen : node.icon) + '" alt="" />';
\r
265 str += '<a id="s' + this.obj + nodeId + '" class="' + ((this.config.useSelection) ? ((node._is ? 'nodeSel' : 'node')) : 'node') + '" href="' + node.url + '"';
\r
267 if (node.title) str += ' title="' + node.title + '"';
\r
269 if (node.target) str += ' target="' + node.target + '"';
\r
271 if (this.config.useStatusText) str += ' onmouseover="window.status=\'' + node.name + '\';return true;" onmouseout="window.status=\'\';return true;" ';
\r
273 if (this.config.useSelection && ((node._hc && this.config.folderLinks) || !node._hc))
\r
275 str += ' onclick="javascript: ' + this.obj + '.s(' + nodeId + ');"';
\r
281 else if ((!this.config.folderLinks || !node.url) && node._hc && node.pid != this.root.id)
\r
283 str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');" class="node">';
\r
287 if (node.url || ((!this.config.folderLinks || !node.url) && node._hc)) str += '</a>';
\r
293 str += '<div id="d' + this.obj + nodeId + '" class="clip" style="display:' + ((this.root.id == node.pid || node._io) ? 'block' : 'none') + ';">';
\r
295 str += this.addNode(node);
\r
301 this.aIndent.pop();
\r
309 // Adds the empty and line icons
\r
311 dTree.prototype.indent = function(node, nodeId) {
\r
315 if (this.root.id != node.pid) {
\r
317 for (var n=0; n<this.aIndent.length; n++)
\r
319 str += '<img src="' + ( (this.aIndent[n] == 1 && this.config.useLines) ? this.icon.line : this.icon.empty ) + '" alt="" />';
\r
321 (node._ls) ? this.aIndent.push(0) : this.aIndent.push(1);
\r
325 str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');"><img id="j' + this.obj + nodeId + '" src="';
\r
327 if (!this.config.useLines) str += (node._io) ? this.icon.nlMinus : this.icon.nlPlus;
\r
329 else str += ( (node._io) ? ((node._ls && this.config.useLines) ? this.icon.minusBottom : this.icon.minus) : ((node._ls && this.config.useLines) ? this.icon.plusBottom : this.icon.plus ) );
\r
331 str += '" alt="" /></a>';
\r
333 } else str += '<img src="' + ( (this.config.useLines) ? ((node._ls) ? this.icon.joinBottom : this.icon.join ) : this.icon.empty) + '" alt="" />';
\r
343 // Checks if a node has any children and if it is the last sibling
\r
345 dTree.prototype.setCS = function(node) {
\r
349 for (var n=0; n<this.aNodes.length; n++) {
\r
351 if (this.aNodes[n].pid == node.id) node._hc = true;
\r
353 if (this.aNodes[n].pid == node.pid) lastId = this.aNodes[n].id;
\r
357 if (lastId==node.id) node._ls = true;
\r
363 // Returns the selected node
\r
365 dTree.prototype.getSelected = function() {
\r
367 var sn = this.getCookie('cs' + this.obj);
\r
369 return (sn) ? sn : null;
\r
375 // Highlights the selected node
\r
377 dTree.prototype.s = function(id) {
\r
379 if (!this.config.useSelection) return;
\r
381 var cn = this.aNodes[id];
\r
383 if (cn._hc && !this.config.folderLinks) return;
\r
385 if (this.selectedNode != id) {
\r
387 if (this.selectedNode || this.selectedNode==0) {
\r
389 eOld = document.getElementById("s" + this.obj + this.selectedNode);
\r
391 eOld.className = "node";
\r
395 eNew = document.getElementById("s" + this.obj + id);
\r
397 eNew.className = "nodeSel";
\r
399 this.selectedNode = id;
\r
401 if (this.config.useCookies) this.setCookie('cs' + this.obj, cn.id);
\r
409 // Toggle Open or close
\r
411 dTree.prototype.o = function(id) {
\r
413 var cn = this.aNodes[id];
\r
415 this.nodeStatus(!cn._io, id, cn._ls);
\r
419 if (this.config.closeSameLevel) this.closeLevel(cn);
\r
421 if (this.config.useCookies) this.updateCookie();
\r
427 // Open or close all nodes
\r
429 dTree.prototype.oAll = function(status) {
\r
431 for (var n=0; n<this.aNodes.length; n++) {
\r
433 if (this.aNodes[n]._hc && this.aNodes[n].pid != this.root.id) {
\r
435 this.nodeStatus(status, n, this.aNodes[n]._ls)
\r
437 this.aNodes[n]._io = status;
\r
443 if (this.config.useCookies) this.updateCookie();
\r
449 // Opens the tree to a specific node
\r
451 dTree.prototype.openTo = function(nId, bSelect, bFirst) {
\r
455 for (var n=0; n<this.aNodes.length; n++) {
\r
457 if (this.aNodes[n].id == nId) {
\r
469 var cn=this.aNodes[nId];
\r
471 if (cn.pid==this.root.id || !cn._p) return;
\r
477 if (this.completed && cn._hc) this.nodeStatus(true, cn._ai, cn._ls);
\r
479 if (this.completed && bSelect) this.s(cn._ai);
\r
481 else if (bSelect) this._sn=cn._ai;
\r
483 this.openTo(cn._p._ai, false, true);
\r
489 // Closes all nodes on the same level as certain node
\r
491 dTree.prototype.closeLevel = function(node) {
\r
493 for (var n=0; n<this.aNodes.length; n++) {
\r
495 if (this.aNodes[n].pid == node.pid && this.aNodes[n].id != node.id && this.aNodes[n]._hc) {
\r
497 this.nodeStatus(false, n, this.aNodes[n]._ls);
\r
499 this.aNodes[n]._io = false;
\r
501 this.closeAllChildren(this.aNodes[n]);
\r
511 // Closes all children of a node
\r
513 dTree.prototype.closeAllChildren = function(node) {
\r
515 for (var n=0; n<this.aNodes.length; n++) {
\r
517 if (this.aNodes[n].pid == node.id && this.aNodes[n]._hc) {
\r
519 if (this.aNodes[n]._io) this.nodeStatus(false, n, this.aNodes[n]._ls);
\r
521 this.aNodes[n]._io = false;
\r
523 this.closeAllChildren(this.aNodes[n]);
\r
533 // Change the status of a node(open or closed)
\r
535 dTree.prototype.nodeStatus = function(status, id, bottom) {
\r
537 eDiv = document.getElementById('d' + this.obj + id);
\r
539 eJoin = document.getElementById('j' + this.obj + id);
\r
541 if (this.config.useIcons) {
\r
543 eIcon = document.getElementById('i' + this.obj + id);
\r
545 eIcon.src = (status) ? this.aNodes[id].iconOpen : this.aNodes[id].icon;
\r
549 eJoin.src = (this.config.useLines)?
\r
551 ((status)?((bottom)?this.icon.minusBottom:this.icon.minus):((bottom)?this.icon.plusBottom:this.icon.plus)):
\r
553 ((status)?this.icon.nlMinus:this.icon.nlPlus);
\r
555 eDiv.style.display = (status) ? 'block': 'none';
\r
563 // [Cookie] Clears a cookie
\r
565 dTree.prototype.clearCookie = function() {
\r
567 var now = new Date();
\r
569 var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
\r
571 this.setCookie('co'+this.obj, 'cookieValue', yesterday);
\r
573 this.setCookie('cs'+this.obj, 'cookieValue', yesterday);
\r
579 // [Cookie] Sets value in a cookie
\r
581 dTree.prototype.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) {
\r
585 escape(cookieName) + '=' + escape(cookieValue)
\r
587 + (expires ? '; expires=' + expires.toGMTString() : '')
\r
589 + (path ? '; path=' + path : '')
\r
591 + (domain ? '; domain=' + domain : '')
\r
593 + (secure ? '; secure' : '');
\r
599 // [Cookie] Gets a value from a cookie
\r
601 dTree.prototype.getCookie = function(cookieName) {
\r
603 var cookieValue = '';
\r
605 var posName = document.cookie.indexOf(escape(cookieName) + '=');
\r
607 if (posName != -1) {
\r
609 var posValue = posName + (escape(cookieName) + '=').length;
\r
611 var endPos = document.cookie.indexOf(';', posValue);
\r
613 if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
\r
615 else cookieValue = unescape(document.cookie.substring(posValue));
\r
619 return (cookieValue);
\r
625 // [Cookie] Returns ids of open nodes as a string
\r
627 dTree.prototype.updateCookie = function() {
\r
631 for (var n=0; n<this.aNodes.length; n++) {
\r
633 if (this.aNodes[n]._io && this.aNodes[n].pid != this.root.id) {
\r
635 if (str) str += '.';
\r
637 str += this.aNodes[n].id;
\r
643 this.setCookie('co' + this.obj, str);
\r
649 // [Cookie] Checks if a node id is in a cookie
\r
651 dTree.prototype.isOpen = function(id) {
\r
653 var aOpen = this.getCookie('co' + this.obj).split('.');
\r
655 for (var n=0; n<aOpen.length; n++)
\r
657 if (aOpen[n] == id) return true;
\r
665 // If Push and pop is not implemented by the browser
\r
667 if (!Array.prototype.push) {
\r
669 Array.prototype.push = function array_push() {
\r
671 for(var i=0;i<arguments.length;i++)
\r
673 this[this.length]=arguments[i];
\r
675 return this.length;
\r
681 if (!Array.prototype.pop) {
\r
683 Array.prototype.pop = function array_pop() {
\r
685 lastElement = this[this.length-1];
\r
687 this.length = Math.max(this.length-1,0);
\r
689 return lastElement;
\r