1 /*--------------------------------------------------|
2 | dTree 2.05 | www.destroydrop.com/javascript/tree/ |
3 |---------------------------------------------------|
4 | Copyright (c) 2002-2003 Geir Landrö |
6 | This script can be used freely as long as all |
7 | copyright messages are intact. |
9 | Updated: 17.04.2003 |
10 |--------------------------------------------------*/
13 function Node(id, pid, name, isParent, url, dataname, datatype, title, target,
14 icon, iconOpen, open) {
15 this.isParent = isParent;
23 this.iconOpen = iconOpen;
24 this.dataname = dataname;
25 this.datatype = datatype;
26 this._io = open || false;
35 function dTree(objName) {
44 useStatusText : false,
45 closeSameLevel : false,
49 root : 'img/base.gif',
50 folder : 'img/folder.gif',
51 folderOpen : 'img/folderopen.gif',
52 node : 'img/page.gif',
53 empty : 'img/empty.gif',
54 line : 'img/line.gif',
55 join : 'img/join.gif',
56 joinBottom : 'img/joinbottom.gif',
57 plus : 'img/plus.gif',
58 plusBottom : 'img/plusbottom.gif',
59 minus : 'img/minus.gif',
60 minusBottom : 'img/minusbottom.gif',
61 nlPlus : 'img/nolines_plus.gif',
62 nlMinus : 'img/nolines_minus.gif'
67 this.root = new Node(-1);
68 this.selectedNode = null;
69 this.selectedFound = false;
70 this.completed = false;
73 // Adds a new node to the node array
74 dTree.prototype.add = function(id, pid, name, isParent, url, datatype,
75 title, target, icon, iconOpen, open) {
76 this.aNodes[this.aNodes.length] = new Node(id, pid, name, isParent, url,
77 datatype, title, target, icon,
81 // Open/close all nodes
82 dTree.prototype.openAll = function() {
85 dTree.prototype.closeAll = function() {
89 // Outputs the tree to the page
90 dTree.prototype.toString = function() {
91 var str = '<div class="dtree" id="' + this.obj + '">\n';
92 if (document.getElementById) {
93 if (this.config.useCookies)
94 this.selectedNode = this.getSelected();
95 str += this.addNode(this.root);
96 } else str += 'Browser not supported.';
98 if (!this.selectedFound) this.selectedNode = null;
99 this.completed = true;
103 // Creates the tree structure
104 dTree.prototype.addNode = function(pNode) {
107 if (this.config.inOrder) n = pNode._ai;
108 for (n; n<this.aNodes.length; n++) {
109 if (this.aNodes[n].pid == pNode.id) {
110 var cn = this.aNodes[n];
114 if (!cn.target && this.config.target) cn.target = this.config.target;
115 if (cn._hc && !cn._io && this.config.useCookies) cn._io = this.isOpen(cn.id);
116 if (!this.config.folderLinks && cn._hc) cn.url = null;
117 if (this.config.useSelection && cn.id == this.selectedNode && !this.selectedFound) {
119 this.selectedNode = n;
120 this.selectedFound = true;
122 str += this.node(cn, n);
129 // Creates the node icon, url and text
130 dTree.prototype.node = function(node, nodeId) {
133 if (this.root.id != node.pid || !this.config.hideRoot) {
134 str += '<div class="dTreeNode"';
135 if (node.datatype) str += ' datatype="' + node.datatype + '"';
136 if (node.dataname) str += ' dataname="' + node.dataname + '"';
137 str += '>' + this.indent(node, nodeId);
139 str += '<a id="s' + this.obj + nodeId + '" class="node" href="' + node.url + '"';
140 if (node.title) str += ' title="' + node.title + '"';
141 if (node.target) str += ' target="' + node.target + '"';
142 if (this.config.useStatusText) str += ' onmouseover="window.status=\'' + node.name + '\';return true;" onmouseout="window.status=\'\';return true;" ';
143 if (this.config.useSelection && ((node._hc && this.config.folderLinks) || !node._hc))
144 str += ' onclick="' + this.obj + '.s(' + nodeId + ');"';
147 else if ((!this.config.folderLinks || !node.url) && node._hc && node.pid != this.root.id)
148 str += '<a href="#" onclick="' + this.obj + '.o(' + nodeId + ');" class="node">';
149 if (this.config.useIcons) {
150 if (!node.icon) node.icon = (this.root.id == node.pid) ? this.icon.root : ((node._hc) ? this.icon.folder : this.icon.node);
151 if (!node.iconOpen) node.iconOpen = (node._hc) ? this.icon.folderOpen : this.icon.node;
152 if (this.root.id == node.pid) {
153 node.icon = this.icon.root;
154 node.iconOpen = this.icon.root;
156 str += '<img id="i' + this.obj + nodeId + '" src="' + ((node._io) ? node.iconOpen : node.icon) + '" alt="" />';
158 str += '<span class="nodeName';
161 str += '">' + node.name + '</span>';
162 if (node.url || ((!this.config.folderLinks || !node.url) && node._hc)) str += '</a>';
166 str += '<div id="d' + this.obj + nodeId + '" class="clip" style="display:' + ((this.root.id == node.pid || node._io) ? 'block' : 'none') + ';">';
167 str += this.addNode(node);
174 // Adds the empty and line icons
175 dTree.prototype.indent = function(node, nodeId) {
177 if (this.root.id != node.pid)
179 for (var n=0; n<this.aIndent.length; n++)
180 str += '<img src="' + ( (this.aIndent[n] == 1 && this.config.useLines) ? this.icon.line : this.icon.empty ) + '" alt="" />';
181 (node._ls) ? this.aIndent.push(0) : this.aIndent.push(1);
184 str += '<a href="#" onclick="return ' + this.obj + '.o(' + nodeId + ');"><img id="j' + this.obj + nodeId + '" src="';
185 if (!this.config.useLines) str += (node._io) ? this.icon.nlMinus : this.icon.nlPlus;
186 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 ) );
187 str += '" alt="" /></a>';
190 str += '<img src="' + ( (this.config.useLines) ? ((node._ls) ? this.icon.joinBottom : this.icon.join ) : this.icon.empty) + '" alt="" />';
195 // Checks if a node has any children and if it is the last sibling
196 dTree.prototype.setCS = function(node) {
198 for (var n=0; n<this.aNodes.length; n++) {
199 if (this.aNodes[n].pid == node.id) node._hc = true;
200 if (this.aNodes[n].pid == node.pid) lastId = this.aNodes[n].id;
202 if (lastId==node.id) node._ls = true;
205 // Returns the selected node
206 dTree.prototype.getSelected = function() {
207 var sn = this.getCookie('cs' + this.obj);
208 return (sn) ? sn : null;
211 // Highlights the selected node
212 dTree.prototype.s = function(id) {
213 if (!this.config.useSelection) return;
214 var cn = this.aNodes[id];
215 if (cn._hc && !this.config.folderLinks) return;
216 if (this.selectedNode != id) {
217 if (this.selectedNode || this.selectedNode==0) {
218 eOld = document.getElementById("s" + this.obj + this.selectedNode);
221 eNew = document.getElementById("s" + this.obj + id);
223 this.selectedNode = id;
224 if (this.config.useCookies) this.setCookie('cs' + this.obj, cn.id);
228 // Toggle Open or close
229 dTree.prototype.o = function(id) {
230 var cn = this.aNodes[id];
231 this.nodeStatus(!cn._io, id, cn._ls);
233 if (this.config.closeSameLevel) this.closeLevel(cn);
234 if (this.config.useCookies) this.updateCookie();
239 // Open or close all nodes
240 dTree.prototype.oAll = function(status) {
241 for (var n=0; n<this.aNodes.length; n++) {
242 if (this.aNodes[n]._hc && this.aNodes[n].pid != this.root.id) {
243 this.nodeStatus(status, n, this.aNodes[n]._ls)
244 this.aNodes[n]._io = status;
247 if (this.config.useCookies) this.updateCookie();
250 // Opens the tree to a specific node
251 dTree.prototype.openTo = function(nId, bSelect, bFirst) {
253 for (var n=0; n<this.aNodes.length; n++) {
254 if (this.aNodes[n].id == nId) {
260 var cn=this.aNodes[nId];
261 if (cn.pid==this.root.id || !cn._p) return;
264 if (this.completed && cn._hc) this.nodeStatus(true, cn._ai, cn._ls);
265 if (this.completed && bSelect) this.s(cn._ai);
266 else if (bSelect) this._sn=cn._ai;
267 this.openTo(cn._p._ai, false, true);
270 // Closes all nodes on the same level as certain node
271 dTree.prototype.closeLevel = function(node) {
272 for (var n=0; n<this.aNodes.length; n++) {
273 if (this.aNodes[n].pid == node.pid && this.aNodes[n].id != node.id && this.aNodes[n]._hc) {
274 this.nodeStatus(false, n, this.aNodes[n]._ls);
275 this.aNodes[n]._io = false;
276 this.closeAllChildren(this.aNodes[n]);
281 // Closes all children of a node
282 dTree.prototype.closeAllChildren = function(node) {
283 for (var n=0; n<this.aNodes.length; n++) {
284 if (this.aNodes[n].pid == node.id && this.aNodes[n]._hc) {
285 if (this.aNodes[n]._io) this.nodeStatus(false, n, this.aNodes[n]._ls);
286 this.aNodes[n]._io = false;
287 this.closeAllChildren(this.aNodes[n]);
292 // Change the status of a node(open or closed)
293 dTree.prototype.nodeStatus = function(status, id, bottom) {
294 eDiv = document.getElementById('d' + this.obj + id);
295 eJoin = document.getElementById('j' + this.obj + id);
296 if (this.config.useIcons) {
297 eIcon = document.getElementById('i' + this.obj + id);
298 eIcon.src = (status) ? this.aNodes[id].iconOpen : this.aNodes[id].icon;
300 eJoin.src = (this.config.useLines)?
301 ((status)?((bottom)?this.icon.minusBottom:this.icon.minus):((bottom)?this.icon.plusBottom:this.icon.plus)):
302 ((status)?this.icon.nlMinus:this.icon.nlPlus);
303 eDiv.style.display = (status) ? 'block': 'none';
307 // [Cookie] Clears a cookie
308 dTree.prototype.clearCookie = function() {
309 var now = new Date();
310 var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
311 this.setCookie('co'+this.obj, 'cookieValue', yesterday);
312 this.setCookie('cs'+this.obj, 'cookieValue', yesterday);
315 // [Cookie] Sets value in a cookie
316 dTree.prototype.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) {
318 escape(cookieName) + '=' + escape(cookieValue)
319 + (expires ? '; expires=' + expires.toGMTString() : '')
320 + (path ? '; path=' + path : '')
321 + (domain ? '; domain=' + domain : '')
322 + (secure ? '; secure' : '');
325 // [Cookie] Gets a value from a cookie
326 dTree.prototype.getCookie = function(cookieName) {
327 var cookieValue = '';
328 var posName = document.cookie.indexOf(escape(cookieName) + '=');
330 var posValue = posName + (escape(cookieName) + '=').length;
331 var endPos = document.cookie.indexOf(';', posValue);
332 if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
333 else cookieValue = unescape(document.cookie.substring(posValue));
335 return (cookieValue);
338 // [Cookie] Returns ids of open nodes as a string
339 dTree.prototype.updateCookie = function() {
341 for (var n=0; n<this.aNodes.length; n++) {
342 if (this.aNodes[n]._io && this.aNodes[n].pid != this.root.id) {
344 str += this.aNodes[n].id;
347 this.setCookie('co' + this.obj, str);
350 // [Cookie] Checks if a node id is in a cookie
351 dTree.prototype.isOpen = function(id) {
352 var aOpen = this.getCookie('co' + this.obj).split('.');
353 for (var n=0; n<aOpen.length; n++)
354 if (aOpen[n] == id) return true;
358 // If Push and pop is not implemented by the browser
359 if (!Array.prototype.push) {
360 Array.prototype.push = function array_push() {
361 for(var i=0;i<arguments.length;i++)
362 this[this.length]=arguments[i];
367 if (!Array.prototype.pop) {
368 Array.prototype.pop = function array_pop() {
369 lastElement = this[this.length-1];
370 this.length = Math.max(this.length-1,0);