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;
75 // Adds a new node to the node array
76 dTree.prototype.add = function(id, pid, name, isParent, url, datatype,
77 title, target, icon, iconOpen, open) {
78 this.aNodes[this.aNodes.length] = new Node(id, pid, name, isParent, url,
79 datatype, title, target, icon,
83 // Open/close all nodes
84 dTree.prototype.openAll = function() {
87 dTree.prototype.closeAll = function() {
91 // Outputs the tree to the page
92 dTree.prototype.toString = function() {
93 var str = '<div class="dtree" id="' + this.obj + '">\n';
94 if (document.getElementById) {
95 if (this.config.useCookies)
96 this.selectedNode = this.getSelected();
97 str += this.addNode(this.root);
98 } else str += 'Browser not supported.';
100 if (!this.selectedFound) this.selectedNode = null;
101 this.completed = true;
105 // Creates the tree structure
106 dTree.prototype.addNode = function(pNode) {
109 if (this.config.inOrder) n = pNode._ai;
110 for (n; n<this.aNodes.length; n++) {
111 if (this.aNodes[n].pid == pNode.id) {
112 var cn = this.aNodes[n];
116 if (!cn.target && this.config.target) cn.target = this.config.target;
117 if (cn._hc && !cn._io && this.config.useCookies) cn._io = this.isOpen(cn.id);
118 if (!this.config.folderLinks && cn._hc) cn.url = null;
119 if (this.config.useSelection && cn.id == this.selectedNode && !this.selectedFound) {
121 this.selectedNode = n;
122 this.selectedFound = true;
124 str += this.node(cn, n);
131 // Creates the node icon, url and text
132 dTree.prototype.node = function(node, nodeId) {
135 if (this.root.id != node.pid || !this.config.hideRoot) {
136 str += '<div class="dTreeNode"';
137 if (node.datatype) str += ' datatype="' + node.datatype + '"';
138 if (node.dataname) str += ' dataname="' + node.dataname + '"';
139 str += '>' + this.indent(node, nodeId);
141 str += '<a id="s' + this.obj + nodeId + '" class="node" href="' + node.url + '"';
142 if (node.title) str += ' title="' + node.title + '"';
143 if (node.target) str += ' target="' + node.target + '"';
144 if (this.config.useStatusText) str += ' onmouseover="window.status=\'' + node.name + '\';return true;" onmouseout="window.status=\'\';return true;" ';
145 if (this.config.useSelection && ((node._hc && this.config.folderLinks) || !node._hc))
146 str += ' onclick="' + this.obj + '.s(' + nodeId + ');"';
149 else if ((!this.config.folderLinks || !node.url) && node._hc && node.pid != this.root.id)
150 str += '<a href="#" onclick="' + this.obj + '.o(' + nodeId + ');" class="node">';
151 if (this.config.useIcons) {
152 if (!node.icon) node.icon = (this.root.id == node.pid) ? this.icon.root : ((node._hc) ? this.icon.folder : this.icon.node);
153 if (!node.iconOpen) node.iconOpen = (node._hc) ? this.icon.folderOpen : this.icon.node;
154 if (this.root.id == node.pid) {
155 node.icon = this.icon.root;
156 node.iconOpen = this.icon.root;
158 str += '<img id="i' + this.obj + nodeId + '" src="' + ((node._io) ? node.iconOpen : node.icon) + '" alt="" />';
160 str += '<span class="nodeName';
163 str += '">' + node.name + '</span>';
164 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);
176 // Adds the empty and line icons
177 dTree.prototype.indent = function(node, nodeId) {
179 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="" />';
183 (node._ls) ? this.aIndent.push(0) : this.aIndent.push(1);
186 str += '<a href="#" onclick="return ' + 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;
188 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 ) );
189 str += '" alt="" /></a>';
192 str += '<img src="' + ( (this.config.useLines) ? ((node._ls) ? this.icon.joinBottom : this.icon.join ) : this.icon.empty) + '" alt="" />';
197 // Checks if a node has any children and if it is the last sibling
198 dTree.prototype.setCS = function(node) {
200 for (var n=0; n<this.aNodes.length; n++) {
201 if (this.aNodes[n].pid == node.id) node._hc = true;
202 if (this.aNodes[n].pid == node.pid) lastId = this.aNodes[n].id;
204 if (lastId==node.id) node._ls = true;
207 // Returns the selected node
208 dTree.prototype.getSelected = function() {
209 var sn = this.getCookie('cs' + this.obj);
210 return (sn) ? sn : null;
213 // Highlights the selected node
214 dTree.prototype.s = function(id) {
215 if (!this.config.useSelection) return;
216 var cn = this.aNodes[id];
217 if (cn._hc && !this.config.folderLinks) return;
218 if (this.selectedNode != id) {
219 if (this.selectedNode || this.selectedNode==0) {
220 eOld = document.getElementById("s" + this.obj + this.selectedNode);
223 eNew = document.getElementById("s" + this.obj + id);
225 this.selectedNode = id;
226 if (this.config.useCookies) this.setCookie('cs' + this.obj, cn.id);
230 // Toggle Open or close
231 dTree.prototype.o = function(id) {
232 var cn = this.aNodes[id];
233 this.nodeStatus(!cn._io, id, cn._ls);
235 if (this.config.closeSameLevel) this.closeLevel(cn);
236 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) {
262 var cn=this.aNodes[nId];
263 if (cn.pid==this.root.id || !cn._p) return;
266 if (this.completed && cn._hc) this.nodeStatus(true, cn._ai, cn._ls);
267 if (this.completed && bSelect) this.s(cn._ai);
268 else if (bSelect) this._sn=cn._ai;
269 this.openTo(cn._p._ai, false, true);
272 // Closes all nodes on the same level as certain node
273 dTree.prototype.closeLevel = function(node) {
274 for (var n=0; n<this.aNodes.length; n++) {
275 if (this.aNodes[n].pid == node.pid && this.aNodes[n].id != node.id && this.aNodes[n]._hc) {
276 this.nodeStatus(false, n, this.aNodes[n]._ls);
277 this.aNodes[n]._io = false;
278 this.closeAllChildren(this.aNodes[n]);
283 // Closes all children of a node
284 dTree.prototype.closeAllChildren = function(node) {
285 for (var n=0; n<this.aNodes.length; n++) {
286 if (this.aNodes[n].pid == node.id && this.aNodes[n]._hc) {
287 if (this.aNodes[n]._io) this.nodeStatus(false, n, this.aNodes[n]._ls);
288 this.aNodes[n]._io = false;
289 this.closeAllChildren(this.aNodes[n]);
294 // Change the status of a node(open or closed)
295 dTree.prototype.nodeStatus = function(status, id, bottom) {
296 eDiv = document.getElementById('d' + this.obj + id);
297 eJoin = document.getElementById('j' + this.obj + id);
298 if (this.config.useIcons) {
299 eIcon = document.getElementById('i' + this.obj + id);
300 eIcon.src = (status) ? this.aNodes[id].iconOpen : this.aNodes[id].icon;
302 eJoin.src = (this.config.useLines)?
303 ((status)?((bottom)?this.icon.minusBottom:this.icon.minus):((bottom)?this.icon.plusBottom:this.icon.plus)):
304 ((status)?this.icon.nlMinus:this.icon.nlPlus);
305 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) {
346 str += this.aNodes[n].id;
349 this.setCookie('co' + this.obj, str);
352 // [Cookie] Checks if a node id is in a cookie
353 dTree.prototype.isOpen = function(id) {
354 var aOpen = this.getCookie('co' + this.obj).split('.');
355 for (var n=0; n<aOpen.length; n++)
356 if (aOpen[n] == id) return true;
360 // If Push and pop is not implemented by the browser
361 if (!Array.prototype.push) {
362 Array.prototype.push = function array_push() {
363 for(var i=0;i<arguments.length;i++)
364 this[this.length]=arguments[i];
369 if (!Array.prototype.pop) {
370 Array.prototype.pop = function array_pop() {
371 lastElement = this[this.length-1];
372 this.length = Math.max(this.length-1,0);