1 /*--------------------------------------------------|
3 | dTree 2.05 | www.destroydrop.com/javascript/tree/ |
5 |---------------------------------------------------|
7 | Copyright (c) 2002-2003 Geir Landrö |
11 | This script can be used freely as long as all |
13 | copyright messages are intact. |
17 | Updated: 17.04.2003 |
19 |--------------------------------------------------*/
25 function Node(id, pid, name, url, title, target, icon, iconOpen, open) {
33 this.iconOpen = iconOpen;
34 this._io = open || false;
43 function dTree(objName) {
51 useStatusText : false,
52 closeSameLevel : false,
57 root : '/plugins/projects_hierarchy/img/base.gif',
58 folder : '/plugins/projects_hierarchy/img/folder.gif',
59 folderOpen : '/plugins/projects_hierarchy/img/folderopen.gif',
60 node : '/plugins/projects_hierarchy/img/page.gif',
61 empty : '/plugins/projects_hierarchy/img/empty.gif',
62 line : '/plugins/projects_hierarchy/img/line.gif',
63 join : '/plugins/projects_hierarchy/img/join.gif',
64 joinBottom : '/plugins/projects_hierarchy/img/joinbottom.gif',
65 plus : '/plugins/projects_hierarchy/img/plus.gif',
66 plusBottom : '/plugins/projects_hierarchy/img/plusbottom.gif',
67 minus : '/plugins/projects_hierarchy/img/minus.gif',
68 minusBottom : '/plugins/projects_hierarchy/img/minusbottom.gif',
69 nlPlus : '/plugins/projects_hierarchy/img/nolines_plus.gif',
70 nlMinus : '/plugins/projects_hierarchy/img/nolines_minus.gif'
76 this.root = new Node(-1);
77 this.selectedNode = null;
78 this.selectedFound = false;
79 this.completed = false;
82 // Adds a new node to the node array
83 dTree.prototype.add = function(id, pid, name, url, title, target, icon, iconOpen, open) {
84 this.aNodes[this.aNodes.length] = new Node(id, pid, name, url, title, target, icon, iconOpen, open);
87 // Open/close all nodes
88 dTree.prototype.openAll = function() {
92 dTree.prototype.closeAll = function() {
96 // Outputs the tree to the page
97 dTree.prototype.toString = function() {
98 var str = '<div class="dtree">\n';
99 if (document.getElementById) {
100 if (this.config.useCookies) this.selectedNode = this.getSelected();
101 str += this.addNode(this.root);
102 } else str += 'Browser not supported.';
104 if (!this.selectedFound) this.selectedNode = null;
105 this.completed = true;
109 // Creates the tree structure
110 dTree.prototype.addNode = function(pNode) {
113 if (this.config.inOrder) n = pNode._ai;
114 for (n; n<this.aNodes.length; n++) {
115 if (this.aNodes[n].pid == pNode.id) {
116 var cn = this.aNodes[n];
120 if (!cn.target && this.config.target) cn.target = this.config.target;
121 if (cn._hc && !cn._io && this.config.useCookies) cn._io = this.isOpen(cn.id);
122 if (!this.config.folderLinks && cn._hc) cn.url = null;
123 if (this.config.useSelection && cn.id == this.selectedNode && !this.selectedFound) {
125 this.selectedNode = n;
126 this.selectedFound = true;
128 str += this.node(cn, n);
135 // Creates the node icon, url and text
136 dTree.prototype.node = function(node, nodeId) {
137 var str = '<div class="dTreeNode">' + this.indent(node, nodeId);
138 if (this.config.useIcons) {
139 if (!node.icon) node.icon = (this.root.id == node.pid) ? this.icon.root : ((node._hc) ? this.icon.folder : this.icon.node);
140 if (!node.iconOpen) node.iconOpen = (node._hc) ? this.icon.folderOpen : this.icon.node;
141 if (this.root.id == node.pid) {
142 node.icon = this.icon.root;
143 node.iconOpen = this.icon.root;
146 str += '<img id="i' + this.obj + nodeId + '" src="' + ((node._io) ? node.iconOpen : node.icon) + '" alt="" />';
150 str += '<a id="s' + this.obj + nodeId + '" class="' + ((this.config.useSelection) ? ((node._is ? 'nodeSel' : 'node')) : 'node') + '" href="' + node.url + '"';
151 if (node.title) str += ' title="' + node.title + '"';
152 if (node.target) str += ' target="' + node.target + '"';
153 if (this.config.useStatusText) str += ' onmouseover="window.status=\'' + node.name + '\';return true;" onmouseout="window.status=\'\';return true;" ';
154 if (this.config.useSelection && ((node._hc && this.config.folderLinks) || !node._hc))
155 str += ' onclick="javascript: ' + this.obj + '.s(' + nodeId + ');"';
159 else if ((!this.config.folderLinks || !node.url) && node._hc && node.pid != this.root.id)
160 str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');" class="node">';
163 if (node.url || ((!this.config.folderLinks || !node.url) && node._hc)) str += '</a>';
168 str += '<div id="d' + this.obj + nodeId + '" class="clip" style="display:' + ((this.root.id == node.pid || node._io) ? 'block' : 'none') + ';">';
169 str += this.addNode(node);
177 // Adds the empty and line icons
178 dTree.prototype.indent = function(node, nodeId) {
180 if (this.root.id != node.pid) {
181 for (var n=0; n<this.aIndent.length; n++)
182 str += '<img src="' + ( (this.aIndent[n] == 1 && this.config.useLines) ? this.icon.line : this.icon.empty ) + '" alt="" />';
184 (node._ls) ? this.aIndent.push(0) : this.aIndent.push(1);
186 str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');"><img id="j' + this.obj + nodeId + '" src="';
187 if (!this.config.useLines) str += (node._io) ? this.icon.nlMinus : this.icon.nlPlus;
189 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 ) );
191 str += '" alt="" /></a>';
193 } else str += '<img src="' + ( (this.config.useLines) ? ((node._ls) ? this.icon.joinBottom : this.icon.join ) : this.icon.empty) + '" alt="" />';
198 // Checks if a node has any children and if it is the last sibling
199 dTree.prototype.setCS = function(node) {
201 for (var n=0; n<this.aNodes.length; n++) {
202 if (this.aNodes[n].pid == node.id) node._hc = true;
203 if (this.aNodes[n].pid == node.pid) lastId = this.aNodes[n].id;
205 if (lastId==node.id) node._ls = true;
208 // Returns the selected node
209 dTree.prototype.getSelected = function() {
210 var sn = this.getCookie('cs' + this.obj);
211 return (sn) ? sn : null;
214 // Highlights the selected node
215 dTree.prototype.s = function(id) {
216 if (!this.config.useSelection) return;
217 var cn = this.aNodes[id];
218 if (cn._hc && !this.config.folderLinks) return;
219 if (this.selectedNode != id) {
220 if (this.selectedNode || this.selectedNode==0) {
221 eOld = document.getElementById("s" + this.obj + this.selectedNode);
222 eOld.className = "node";
224 eNew = document.getElementById("s" + this.obj + id);
225 eNew.className = "nodeSel";
226 this.selectedNode = id;
227 if (this.config.useCookies) this.setCookie('cs' + this.obj, cn.id);
231 // Toggle Open or close
232 dTree.prototype.o = function(id) {
233 var cn = this.aNodes[id];
234 this.nodeStatus(!cn._io, id, cn._ls);
237 if (this.config.closeSameLevel) this.closeLevel(cn);
238 if (this.config.useCookies) this.updateCookie();
241 // Open or close all nodes
242 dTree.prototype.oAll = function(status) {
243 for (var n=0; n<this.aNodes.length; n++) {
244 if (this.aNodes[n]._hc && this.aNodes[n].pid != this.root.id) {
245 this.nodeStatus(status, n, this.aNodes[n]._ls)
246 this.aNodes[n]._io = status;
249 if (this.config.useCookies) this.updateCookie();
252 // Opens the tree to a specific node
253 dTree.prototype.openTo = function(nId, bSelect, bFirst) {
255 for (var n=0; n<this.aNodes.length; n++) {
256 if (this.aNodes[n].id == nId) {
263 var cn=this.aNodes[nId];
264 if (cn.pid==this.root.id || !cn._p) return;
267 if (this.completed && cn._hc) this.nodeStatus(true, cn._ai, cn._ls);
268 if (this.completed && bSelect) this.s(cn._ai);
269 else if (bSelect) this._sn=cn._ai;
270 this.openTo(cn._p._ai, false, true);
273 // Closes all nodes on the same level as certain node
274 dTree.prototype.closeLevel = function(node) {
275 for (var n=0; n<this.aNodes.length; n++) {
276 if (this.aNodes[n].pid == node.pid && this.aNodes[n].id != node.id && this.aNodes[n]._hc) {
277 this.nodeStatus(false, n, this.aNodes[n]._ls);
278 this.aNodes[n]._io = false;
279 this.closeAllChildren(this.aNodes[n]);
284 // Closes all children of a node
285 dTree.prototype.closeAllChildren = function(node) {
286 for (var n=0; n<this.aNodes.length; n++) {
287 if (this.aNodes[n].pid == node.id && this.aNodes[n]._hc) {
288 if (this.aNodes[n]._io) this.nodeStatus(false, n, this.aNodes[n]._ls);
289 this.aNodes[n]._io = false;
290 this.closeAllChildren(this.aNodes[n]);
295 // Change the status of a node(open or closed)
296 dTree.prototype.nodeStatus = function(status, id, bottom) {
297 eDiv = document.getElementById('d' + this.obj + id);
298 eJoin = document.getElementById('j' + this.obj + id);
299 if (this.config.useIcons) {
300 eIcon = document.getElementById('i' + this.obj + id);
301 eIcon.src = (status) ? this.aNodes[id].iconOpen : this.aNodes[id].icon;
303 eJoin.src = (this.config.useLines)?
304 ((status)?((bottom)?this.icon.minusBottom:this.icon.minus):((bottom)?this.icon.plusBottom:this.icon.plus)):
305 ((status)?this.icon.nlMinus:this.icon.nlPlus);
306 eDiv.style.display = (status) ? 'block': 'none';
309 // [Cookie] Clears a cookie
310 dTree.prototype.clearCookie = function() {
311 var now = new Date();
312 var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
313 this.setCookie('co'+this.obj, 'cookieValue', yesterday);
314 this.setCookie('cs'+this.obj, 'cookieValue', yesterday);
317 // [Cookie] Sets value in a cookie
318 dTree.prototype.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) {
320 escape(cookieName) + '=' + escape(cookieValue)
321 + (expires ? '; expires=' + expires.toGMTString() : '')
322 + (path ? '; path=' + path : '')
323 + (domain ? '; domain=' + domain : '')
324 + (secure ? '; secure' : '');
327 // [Cookie] Gets a value from a cookie
328 dTree.prototype.getCookie = function(cookieName) {
329 var cookieValue = '';
330 var posName = document.cookie.indexOf(escape(cookieName) + '=');
332 var posValue = posName + (escape(cookieName) + '=').length;
333 var endPos = document.cookie.indexOf(';', posValue);
334 if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
335 else cookieValue = unescape(document.cookie.substring(posValue));
337 return (cookieValue);
340 // [Cookie] Returns ids of open nodes as a string
341 dTree.prototype.updateCookie = function() {
343 for (var n=0; n<this.aNodes.length; n++) {
344 if (this.aNodes[n]._io && this.aNodes[n].pid != this.root.id) {
348 str += this.aNodes[n].id;
351 this.setCookie('co' + this.obj, str);
354 // [Cookie] Checks if a node id is in a cookie
355 dTree.prototype.isOpen = function(id) {
356 var aOpen = this.getCookie('co' + this.obj).split('.');
357 for (var n=0; n<aOpen.length; n++)
358 if (aOpen[n] == id) return true;
364 // If Push and pop is not implemented by the browser
365 if (!Array.prototype.push) {
366 Array.prototype.push = function array_push() {
367 for(var i=0;i<arguments.length;i++)
368 this[this.length]=arguments[i];
373 if (!Array.prototype.pop) {
374 Array.prototype.pop = function array_pop() {
375 lastElement = this[this.length-1];
376 this.length = Math.max(this.length-1,0);