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 |--------------------------------------------------*/
12 function Node(id, pid, name, url, title, target, icon, iconOpen, open) {
20 this.iconOpen = iconOpen;
21 this._io = open || false;
29 function dTree(objName) {
37 useStatusText : false,
38 closeSameLevel : false,
42 root : '../../../images/orgtree/base.gif',
43 folder : '../../../images/orgtree/folder.gif',
44 folderOpen : '../../../images/orgtree/folderopen.gif',
45 node : '../../../images/orgtree/page.gif',
46 empty : '../../../images/orgtree/empty.gif',
47 line : '../../../images/orgtree/line.gif',
48 join : '../../../images/orgtree/join.gif',
49 joinBottom : '../../../images/orgtree/joinbottom.gif',
50 plus : '../../../images/orgtree/plus.gif',
51 plusBottom : '../../../images/orgtree/plusbottom.gif',
52 minus : '../../../images/orgtree/minus.gif',
53 minusBottom : '../../../images/orgtree/minusbottom.gif',
54 nlPlus : '../../../images/orgtree/nolines_plus.gif',
55 nlMinus : '../../../images/orgtree/nolines_minus.gif'
60 this.root = new Node(-1);
61 this.selectedNode = null;
62 this.selectedFound = false;
63 this.completed = false;
66 dTree.prototype.add = function(id, pid, name, url, title, target, icon, iconOpen, open) {
67 this.aNodes[this.aNodes.length] = new Node(id, pid, name, url, title, target, icon, iconOpen, open);
70 dTree.prototype.openAll = function() {
73 dTree.prototype.closeAll = function() {
77 dTree.prototype.toString = function() {
78 var str = '<div class="dtree">\n';
79 if (document.getElementById) {
80 if (this.config.useCookies) this.selectedNode = this.getSelected();
81 str += this.addNode(this.root);
82 } else str += 'Browser not supported.';
84 if (!this.selectedFound) this.selectedNode = null;
85 this.completed = true;
89 dTree.prototype.addNode = function(pNode) {
92 if (this.config.inOrder) n = pNode._ai;
93 for (n; n<this.aNodes.length; n++) {
94 if (this.aNodes[n].pid == pNode.id) {
95 var cn = this.aNodes[n];
99 if (!cn.target && this.config.target) cn.target = this.config.target;
100 if (cn._hc && !cn._io && this.config.useCookies) cn._io = this.isOpen(cn.id);
101 if (!this.config.folderLinks && cn._hc) cn.url = null;
102 if (this.config.useSelection && cn.id == this.selectedNode && !this.selectedFound) {
104 this.selectedNode = n;
105 this.selectedFound = true;
107 str += this.node(cn, n);
114 dTree.prototype.node = function(node, nodeId) {
115 var str = '<div class="dTreeNode">' + this.indent(node, nodeId);
116 if (this.config.useIcons) {
117 if (!node.icon) node.icon = (this.root.id == node.pid) ? this.icon.root : ((node._hc) ? this.icon.folder : this.icon.node);
118 if (!node.iconOpen) node.iconOpen = (node._hc) ? this.icon.folderOpen : this.icon.node;
119 if (this.root.id == node.pid) {
120 node.icon = this.icon.root;
121 node.iconOpen = this.icon.root;
123 str += '<img id="i' + this.obj + nodeId + '" src="' + ((node._io) ? node.iconOpen : node.icon) + '" alt="" />';
126 str += '<a id="s' + this.obj + nodeId + '" class="' + ((this.config.useSelection) ? ((node._is ? 'nodeSel' : 'node')) : 'node') + '" href="' + node.url + '"';
127 if (node.title) str += ' title="' + node.title + '"';
128 if (node.target) str += ' target="' + node.target + '"';
129 if (this.config.useStatusText) str += ' onmouseover="window.status=\'' + node.name + '\';return true;" onmouseout="window.status=\'\';return true;" ';
130 if (this.config.useSelection && ((node._hc && this.config.folderLinks) || !node._hc))
131 str += ' onclick="javascript: ' + this.obj + '.s(' + nodeId + ');"';
134 else if ((!this.config.folderLinks || !node.url) && node._hc && node.pid != this.root.id)
135 str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');" class="node">';
137 if (node.url || ((!this.config.folderLinks || !node.url) && node._hc)) str += '</a>';
140 str += '<div id="d' + this.obj + nodeId + '" class="clip" style="display:' + ((this.root.id == node.pid || node._io) ? 'block' : 'none') + ';">';
141 str += this.addNode(node);
148 dTree.prototype.indent = function(node, nodeId) {
150 if (this.root.id != node.pid) {
151 for (var n=0; n<this.aIndent.length; n++)
152 str += '<img src="' + ( (this.aIndent[n] == 1 && this.config.useLines) ? this.icon.line : this.icon.empty ) + '" alt="" />';
153 (node._ls) ? this.aIndent.push(0) : this.aIndent.push(1);
155 str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');"><img id="j' + this.obj + nodeId + '" src="';
156 if (!this.config.useLines) str += (node._io) ? this.icon.nlMinus : this.icon.nlPlus;
157 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 ) );
158 str += '" alt="" /></a>';
159 } else str += '<img src="' + ( (this.config.useLines) ? ((node._ls) ? this.icon.joinBottom : this.icon.join ) : this.icon.empty) + '" alt="" />';
164 dTree.prototype.setCS = function(node) {
166 for (var n=0; n<this.aNodes.length; n++) {
167 if (this.aNodes[n].pid == node.id) node._hc = true;
168 if (this.aNodes[n].pid == node.pid) lastId = this.aNodes[n].id;
170 if (lastId==node.id) node._ls = true;
173 dTree.prototype.getSelected = function() {
174 var sn = this.getCookie('cs' + this.obj);
175 return (sn) ? sn : null;
178 dTree.prototype.s = function(id) {
179 if (!this.config.useSelection) return;
180 var cn = this.aNodes[id];
181 if (cn._hc && !this.config.folderLinks) return;
182 if (this.selectedNode != id) {
183 if (this.selectedNode || this.selectedNode==0) {
184 eOld = document.getElementById("s" + this.obj + this.selectedNode);
185 eOld.className = "node";
187 eNew = document.getElementById("s" + this.obj + id);
188 eNew.className = "nodeSel";
189 this.selectedNode = id;
190 if (this.config.useCookies) this.setCookie('cs' + this.obj, cn.id);
194 dTree.prototype.o = function(id) {
195 var cn = this.aNodes[id];
196 this.nodeStatus(!cn._io, id, cn._ls);
198 if (this.config.closeSameLevel) this.closeLevel(cn);
199 if (this.config.useCookies) this.updateCookie();
202 dTree.prototype.oAll = function(status) {
203 for (var n=0; n<this.aNodes.length; n++) {
204 if (this.aNodes[n]._hc && this.aNodes[n].pid != this.root.id) {
205 this.nodeStatus(status, n, this.aNodes[n]._ls)
206 this.aNodes[n]._io = status;
209 if (this.config.useCookies) this.updateCookie();
212 dTree.prototype.openTo = function(nId, bSelect, bFirst) {
214 for (var n=0; n<this.aNodes.length; n++) {
215 if (this.aNodes[n].id == nId) {
221 var cn=this.aNodes[nId];
222 if (cn.pid==this.root.id || !cn._p) return;
225 if (this.completed && cn._hc) this.nodeStatus(true, cn._ai, cn._ls);
226 if (this.completed && bSelect) this.s(cn._ai);
227 else if (bSelect) this._sn=cn._ai;
228 this.openTo(cn._p._ai, false, true);
231 dTree.prototype.closeLevel = function(node) {
232 for (var n=0; n<this.aNodes.length; n++) {
233 if (this.aNodes[n].pid == node.pid && this.aNodes[n].id != node.id && this.aNodes[n]._hc) {
234 this.nodeStatus(false, n, this.aNodes[n]._ls);
235 this.aNodes[n]._io = false;
236 this.closeAllChildren(this.aNodes[n]);
241 dTree.prototype.closeAllChildren = function(node) {
242 for (var n=0; n<this.aNodes.length; n++) {
243 if (this.aNodes[n].pid == node.id && this.aNodes[n]._hc) {
244 if (this.aNodes[n]._io) this.nodeStatus(false, n, this.aNodes[n]._ls);
245 this.aNodes[n]._io = false;
246 this.closeAllChildren(this.aNodes[n]);
251 dTree.prototype.nodeStatus = function(status, id, bottom) {
252 eDiv = document.getElementById('d' + this.obj + id);
253 eJoin = document.getElementById('j' + this.obj + id);
254 if (this.config.useIcons) {
255 eIcon = document.getElementById('i' + this.obj + id);
256 eIcon.src = (status) ? this.aNodes[id].iconOpen : this.aNodes[id].icon;
258 eJoin.src = (this.config.useLines)?
259 ((status)?((bottom)?this.icon.minusBottom:this.icon.minus):((bottom)?this.icon.plusBottom:this.icon.plus)):
260 ((status)?this.icon.nlMinus:this.icon.nlPlus);
261 eDiv.style.display = (status) ? 'block': 'none';
265 dTree.prototype.clearCookie = function() {
266 var now = new Date();
267 var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
268 this.setCookie('co'+this.obj, 'cookieValue', yesterday);
269 this.setCookie('cs'+this.obj, 'cookieValue', yesterday);
272 dTree.prototype.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) {
274 escape(cookieName) + '=' + escape(cookieValue)
275 + (expires ? '; expires=' + expires.toGMTString() : '')
276 + (path ? '; path=' + path : '')
277 + (domain ? '; domain=' + domain : '')
278 + (secure ? '; secure' : '');
281 dTree.prototype.getCookie = function(cookieName) {
282 var cookieValue = '';
283 var posName = document.cookie.indexOf(escape(cookieName) + '=');
285 var posValue = posName + (escape(cookieName) + '=').length;
286 var endPos = document.cookie.indexOf(';', posValue);
287 if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
288 else cookieValue = unescape(document.cookie.substring(posValue));
290 return (cookieValue);
293 dTree.prototype.updateCookie = function() {
295 for (var n=0; n<this.aNodes.length; n++) {
296 if (this.aNodes[n]._io && this.aNodes[n].pid != this.root.id) {
298 str += this.aNodes[n].id;
301 this.setCookie('co' + this.obj, str);
304 dTree.prototype.isOpen = function(id) {
305 var aOpen = this.getCookie('co' + this.obj).split('.');
306 for (var n=0; n<aOpen.length; n++)
307 if (aOpen[n] == id) return true;
311 if (!Array.prototype.push) {
312 Array.prototype.push = function array_push() {
313 for(var i=0;i<arguments.length;i++)
314 this[this.length]=arguments[i];
318 if (!Array.prototype.pop) {
319 Array.prototype.pop = function array_pop() {
320 lastElement = this[this.length-1];
321 this.length = Math.max(this.length-1,0);