1 /*--------------------------------------------------|
2 | dTree 2.05 | www.destroydrop.com/javascript/tree/ |
3 |---------------------------------------------------|
4 | Copyright (c) 2002-2003 Geir Landr
\e$B!&
\e(B |
6 | This script can be used freely as long as all |
7 | copyright messages are intact. |
9 | Updated: 17.04.2003 |
10 |--------------------------------------------------*/
14 $strRel = '../../../';
15 include($strRel . 'config.php');
16 include($DIR_LIBS . 'PLUGINADMIN.php');
17 $oPluginAdmin = new PluginAdmin('Dtree');
18 $plug =& $oPluginAdmin->plugin;
19 $imgpath = $plug->getAdminURL();
20 // $imgpath = 'nucleus/plugins/dtree/';
22 if ($plug->getOption('folderLinks') == 'yes') {
23 $folderLinks = 'true,';
25 $folderLinks = 'false,';
27 if ($plug->getOption('useSelection') == 'yes') {
28 $useSelection = 'true,';
30 $useSelection = 'false,';
32 if ($plug->getOption('useCookies') == 'yes') {
33 $useCookies = 'true,';
35 $useCookies = 'false,';
37 if ($plug->getOption('useLines') == 'yes') {
42 if ($plug->getOption('useIcons') == 'yes') {
47 if ($plug->getOption('useStatusText') == 'yes') {
48 $useStatusText = 'true,';
50 $useStatusText = 'false,';
52 if ($plug->getOption('closeSameLevel') == 'yes') {
53 $closeSameLevel = 'true,';
55 $closeSameLevel = 'false,';
57 if ($plug->getOption('inOrder') == 'yes') {
66 var imgpath = "<?php echo $imgpath;?>";
68 function Node(id, pid, name, url, title, target, icon, iconOpen, open) {
84 this.iconOpen = iconOpen;
86 this._io = open || false;
102 function dTree(objName) {
107 folderLinks : <?php echo $folderLinks ?>
108 useSelection : <?php echo $useSelection ?>
109 useCookies : <?php echo $useCookies ?>
110 useLines : <?php echo $useLines ?>
111 useIcons : <?php echo $useIcons ?>
112 useStatusText : <?php echo $useStatusText ?>
113 closeSameLevel : <?php echo $closeSameLevel ?>
114 inOrder : <?php echo $inOrder ?>
116 // folderLinks : true,
117 // useSelection : false,
118 // useCookies : false,
121 // useStatusText : true,
122 // closeSameLevel : true,
128 root : imgpath + 'img/base.gif',
129 folder : imgpath + 'img/folder.gif',
130 folderOpen : imgpath + 'img/folderopen.gif',
131 // node : imgpath + 'img/page.gif',
132 node : imgpath + 'img/folder.gif',
133 empty : imgpath + 'img/empty.gif',
134 line : imgpath + 'img/line.gif',
135 join : imgpath + 'img/join.gif',
136 joinBottom : imgpath + 'img/joinbottom.gif',
137 plus : imgpath + 'img/plus.gif',
138 plusBottom : imgpath + 'img/plusbottom.gif',
139 minus : imgpath + 'img/minus.gif',
140 minusBottom : imgpath + 'img/minusbottom.gif',
141 nlPlus : imgpath + 'img/nolines_plus.gif',
142 nlMinus : imgpath + 'img/nolines_minus.gif'
152 this.root = new Node(-1);
154 this.selectedNode = null;
156 this.selectedFound = false;
158 this.completed = false;
164 // Adds a new node to the node array
166 dTree.prototype.add = function(id, pid, name, url, title, target, icon, iconOpen, open) {
168 this.aNodes[this.aNodes.length] = new Node(id, pid, name, url, title, target, icon, iconOpen, open);
174 // Open/close all nodes
176 dTree.prototype.openAll = function() {
182 dTree.prototype.closeAll = function() {
190 // Outputs the tree to the page
192 dTree.prototype.toString = function() {
194 var str = '<div class="dtree">\n';
196 if (document.getElementById) {
198 if (this.config.useCookies) this.selectedNode = this.getSelected();
200 str += this.addNode(this.root);
202 } else str += 'Browser not supported.';
206 if (!this.selectedFound) this.selectedNode = null;
208 this.completed = true;
216 // Creates the tree structure
218 dTree.prototype.addNode = function(pNode) {
224 if (this.config.inOrder) n = pNode._ai;
226 for (n; n<this.aNodes.length; n++) {
228 if (this.aNodes[n].pid == pNode.id) {
230 var cn = this.aNodes[n];
238 if (!cn.target && this.config.target) cn.target = this.config.target;
240 if (cn._hc && !cn._io && this.config.useCookies) cn._io = this.isOpen(cn.id);
242 if (!this.config.folderLinks && cn._hc) cn.url = null;
244 if (this.config.useSelection && cn.id == this.selectedNode && !this.selectedFound) {
248 this.selectedNode = n;
250 this.selectedFound = true;
254 str += this.node(cn, n);
268 // Creates the node icon, url and text
270 dTree.prototype.node = function(node, nodeId) {
272 var str = '<div class="dTreeNode">' + this.indent(node, nodeId);
274 if (this.config.useIcons) {
276 if (!node.icon) node.icon = (this.root.id == node.pid) ? this.icon.root : ((node._hc) ? this.icon.folder : this.icon.node);
278 if (!node.iconOpen) node.iconOpen = (node._hc) ? this.icon.folderOpen : this.icon.node;
280 if (this.root.id == node.pid) {
282 node.icon = this.icon.root;
284 node.iconOpen = this.icon.root;
288 str += '\n<img id="i' + this.obj + nodeId + '" src="' + ((node._io) ? node.iconOpen : node.icon) + '" alt="" />\n';
294 str += '<a id="s' + this.obj + nodeId + '" class="' + ((this.config.useSelection) ? ((node._is ? 'nodeSel' : 'node')) : 'node') + '" href="' + node.url + '"';
296 if (node.title) str += ' title="' + node.title + '"';
298 if (node.target) str += ' target="' + node.target + '"';
300 if (this.config.useStatusText) str += ' onmouseover="window.status=\'' + node.name + '\';return true;" onmouseout="window.status=\'\';return true;" ';
302 if (this.config.useSelection && ((node._hc && this.config.folderLinks) || !node._hc))
304 str += ' onclick="javascript: ' + this.obj + '.s(' + nodeId + ');"';
310 else if ((!this.config.folderLinks || !node.url) && node._hc && node.pid != this.root.id)
312 str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');" class="node">';
316 if (node.url || ((!this.config.folderLinks || !node.url) && node._hc)) str += '</a>\n';
322 str += '<div id="d' + this.obj + nodeId + '" class="clip" style="display:' + ((this.root.id == node.pid || node._io) ? 'block' : 'none') + ';">\n';
324 str += this.addNode(node);
338 // Adds the empty and line icons
340 dTree.prototype.indent = function(node, nodeId) {
344 if (this.root.id != node.pid) {
346 for (var n=0; n<this.aIndent.length; n++)
348 str += '\n<img src="' + ( (this.aIndent[n] == 1 && this.config.useLines) ? this.icon.line : this.icon.empty ) + '" alt="" />';
350 (node._ls) ? this.aIndent.push(0) : this.aIndent.push(1);
354 str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');"><img id="j' + this.obj + nodeId + '" src="';
356 if (!this.config.useLines) str += (node._io) ? this.icon.nlMinus : this.icon.nlPlus;
358 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 ) );
360 str += '" alt="" /></a>\n';
362 } else str += '<img src="' + ( (this.config.useLines) ? ((node._ls) ? this.icon.joinBottom : this.icon.join ) : this.icon.empty) + '" alt="" />\n';
372 // Checks if a node has any children and if it is the last sibling
374 dTree.prototype.setCS = function(node) {
378 for (var n=0; n<this.aNodes.length; n++) {
380 if (this.aNodes[n].pid == node.id) node._hc = true;
382 if (this.aNodes[n].pid == node.pid) lastId = this.aNodes[n].id;
386 if (lastId==node.id) node._ls = true;
392 // Returns the selected node
394 dTree.prototype.getSelected = function() {
396 var sn = this.getCookie('cs' + this.obj);
398 return (sn) ? sn : null;
404 // Highlights the selected node
406 dTree.prototype.s = function(id) {
408 if (!this.config.useSelection) return;
410 var cn = this.aNodes[id];
412 if (cn._hc && !this.config.folderLinks) return;
414 if (this.selectedNode != id) {
416 if (this.selectedNode || this.selectedNode==0) {
418 eOld = document.getElementById("s" + this.obj + this.selectedNode);
420 eOld.className = "node";
424 eNew = document.getElementById("s" + this.obj + id);
426 eNew.className = "nodeSel";
428 this.selectedNode = id;
430 if (this.config.useCookies) this.setCookie('cs' + this.obj, cn.id);
438 // Toggle Open or close
440 dTree.prototype.o = function(id) {
442 var cn = this.aNodes[id];
444 this.nodeStatus(!cn._io, id, cn._ls);
448 if (this.config.closeSameLevel) this.closeLevel(cn);
450 if (this.config.useCookies) this.updateCookie();
456 // Open or close all nodes
458 dTree.prototype.oAll = function(status) {
460 for (var n=0; n<this.aNodes.length; n++) {
462 if (this.aNodes[n]._hc && this.aNodes[n].pid != this.root.id) {
464 this.nodeStatus(status, n, this.aNodes[n]._ls)
466 this.aNodes[n]._io = status;
472 if (this.config.useCookies) this.updateCookie();
478 // Opens the tree to a specific node
480 dTree.prototype.openTo = function(nId, bSelect, bFirst) {
484 for (var n=0; n<this.aNodes.length; n++) {
486 if (this.aNodes[n].id == nId) {
498 var cn=this.aNodes[nId];
500 if (cn.pid==this.root.id || !cn._p) return;
506 if (this.completed && cn._hc) this.nodeStatus(true, cn._ai, cn._ls);
508 if (this.completed && bSelect) this.s(cn._ai);
510 else if (bSelect) this._sn=cn._ai;
512 this.openTo(cn._p._ai, false, true);
518 // Closes all nodes on the same level as certain node
520 dTree.prototype.closeLevel = function(node) {
522 for (var n=0; n<this.aNodes.length; n++) {
524 if (this.aNodes[n].pid == node.pid && this.aNodes[n].id != node.id && this.aNodes[n]._hc) {
526 this.nodeStatus(false, n, this.aNodes[n]._ls);
528 this.aNodes[n]._io = false;
530 this.closeAllChildren(this.aNodes[n]);
540 // Closes all children of a node
542 dTree.prototype.closeAllChildren = function(node) {
544 for (var n=0; n<this.aNodes.length; n++) {
546 if (this.aNodes[n].pid == node.id && this.aNodes[n]._hc) {
548 if (this.aNodes[n]._io) this.nodeStatus(false, n, this.aNodes[n]._ls);
550 this.aNodes[n]._io = false;
552 this.closeAllChildren(this.aNodes[n]);
562 // Change the status of a node(open or closed)
564 dTree.prototype.nodeStatus = function(status, id, bottom) {
566 eDiv = document.getElementById('d' + this.obj + id);
568 eJoin = document.getElementById('j' + this.obj + id);
570 if (this.config.useIcons) {
572 eIcon = document.getElementById('i' + this.obj + id);
574 eIcon.src = (status) ? this.aNodes[id].iconOpen : this.aNodes[id].icon;
578 eJoin.src = (this.config.useLines)?
580 ((status)?((bottom)?this.icon.minusBottom:this.icon.minus):((bottom)?this.icon.plusBottom:this.icon.plus)):
582 ((status)?this.icon.nlMinus:this.icon.nlPlus);
584 eDiv.style.display = (status) ? 'block': 'none';
592 // [Cookie] Clears a cookie
594 dTree.prototype.clearCookie = function() {
596 var now = new Date();
598 var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
600 this.setCookie('co'+this.obj, 'cookieValue', yesterday);
602 this.setCookie('cs'+this.obj, 'cookieValue', yesterday);
608 // [Cookie] Sets value in a cookie
610 dTree.prototype.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) {
614 escape(cookieName) + '=' + escape(cookieValue)
616 + (expires ? '; expires=' + expires.toGMTString() : '')
618 + (path ? '; path=' + path : '')
620 + (domain ? '; domain=' + domain : '')
622 + (secure ? '; secure' : '');
628 // [Cookie] Gets a value from a cookie
630 dTree.prototype.getCookie = function(cookieName) {
632 var cookieValue = '';
634 var posName = document.cookie.indexOf(escape(cookieName) + '=');
638 var posValue = posName + (escape(cookieName) + '=').length;
640 var endPos = document.cookie.indexOf(';', posValue);
642 if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
644 else cookieValue = unescape(document.cookie.substring(posValue));
648 return (cookieValue);
654 // [Cookie] Returns ids of open nodes as a string
656 dTree.prototype.updateCookie = function() {
660 for (var n=0; n<this.aNodes.length; n++) {
662 if (this.aNodes[n]._io && this.aNodes[n].pid != this.root.id) {
666 str += this.aNodes[n].id;
672 this.setCookie('co' + this.obj, str);
678 // [Cookie] Checks if a node id is in a cookie
680 dTree.prototype.isOpen = function(id) {
682 var aOpen = this.getCookie('co' + this.obj).split('.');
684 for (var n=0; n<aOpen.length; n++)
686 if (aOpen[n] == id) return true;
694 // If Push and pop is not implemented by the browser
696 if (!Array.prototype.push) {
698 Array.prototype.push = function array_push() {
700 for(var i=0;i<arguments.length;i++)
702 this[this.length]=arguments[i];
710 if (!Array.prototype.pop) {
712 Array.prototype.pop = function array_pop() {
714 lastElement = this[this.length-1];
716 this.length = Math.max(this.length-1,0);