]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/cgi-bin/support/dtree.js
adding dTree support files
[Evergreen.git] / Open-ILS / src / cgi-bin / support / dtree.js
1 /*--------------------------------------------------|
2 | dTree 2.05 | www.destroydrop.com/javascript/tree/ |
3 |---------------------------------------------------|
4 | Copyright (c) 2002-2003 Geir Landrö               |
5 |                                                   |
6 | This script can be used freely as long as all     |
7 | copyright messages are intact.                    |
8 |                                                   |
9 | Updated: 17.04.2003                               |
10 |--------------------------------------------------*/
11
12 function Node(id, pid, name, url, title, target, icon, iconOpen, open) {
13         this.id = id;
14         this.pid = pid;
15         this.name = name;
16         this.url = url;
17         this.title = title;
18         this.target = target;
19         this.icon = icon;
20         this.iconOpen = iconOpen;
21         this._io = open || false;
22         this._is = false;
23         this._ls = false;
24         this._hc = false;
25         this._ai = 0;
26         this._p;
27 };
28
29 function dTree(objName) {
30         this.config = {
31                 target                                  : null,
32                 folderLinks                     : true,
33                 useSelection            : true,
34                 useCookies                      : true,
35                 useLines                                : true,
36                 useIcons                                : true,
37                 useStatusText           : false,
38                 closeSameLevel  : false,
39                 inOrder                                 : false
40         }
41         this.icon = {
42                 root                            : 'support/base.gif',
43                 folder                  : 'support/folder.gif',
44                 folderOpen              : 'support/folderopen.gif',
45                 node                            : 'support/page.gif',
46                 empty                           : 'support/empty.gif',
47                 line                            : 'support/line.gif',
48                 join                            : 'support/join.gif',
49                 joinBottom              : 'support/joinbottom.gif',
50                 plus                            : 'support/plus.gif',
51                 plusBottom              : 'support/plusbottom.gif',
52                 minus                           : 'support/minus.gif',
53                 minusBottom             : 'support/minusbottom.gif',
54                 nlPlus                  : 'support/nolines_plus.gif',
55                 nlMinus                 : 'support/nolines_minus.gif'
56         };
57         this.obj = objName;
58         this.aNodes = [];
59         this.aIndent = [];
60         this.root = new Node(-1);
61         this.selectedNode = null;
62         this.selectedFound = false;
63         this.completed = false;
64 };
65
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);
68 };
69
70 dTree.prototype.openAll = function() {
71         this.oAll(true);
72 };
73 dTree.prototype.closeAll = function() {
74         this.oAll(false);
75 };
76
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.';
83         str += '</div>';
84         if (!this.selectedFound) this.selectedNode = null;
85         this.completed = true;
86         return str;
87 };
88
89 dTree.prototype.addNode = function(pNode) {
90         var str = '';
91         var n=0;
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];
96                         cn._p = pNode;
97                         cn._ai = n;
98                         this.setCS(cn);
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) {
103                                         cn._is = true;
104                                         this.selectedNode = n;
105                                         this.selectedFound = true;
106                         }
107                         str += this.node(cn, n);
108                         if (cn._ls) break;
109                 }
110         }
111         return str;
112 };
113
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;
122                 }
123                 str += '<img id="i' + this.obj + nodeId + '" src="' + ((node._io) ? node.iconOpen : node.icon) + '" alt="" />';
124         }
125         if (node.url) {
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 + ');"';
132                 str += '>';
133         }
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">';
136         str += node.name;
137         if (node.url || ((!this.config.folderLinks || !node.url) && node._hc)) str += '</a>';
138         str += '</div>';
139         if (node._hc) {
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);
142                 str += '</div>';
143         }
144         this.aIndent.pop();
145         return str;
146 };
147
148 dTree.prototype.indent = function(node, nodeId) {
149         var str = '';
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);
154                 if (node._hc) {
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="" />';
160         }
161         return str;
162 };
163
164 dTree.prototype.setCS = function(node) {
165         var lastId;
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;
169         }
170         if (lastId==node.id) node._ls = true;
171 };
172
173 dTree.prototype.getSelected = function() {
174         var sn = this.getCookie('cs' + this.obj);
175         return (sn) ? sn : null;
176 };
177
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";
186                 }
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);
191         }
192 };
193
194 dTree.prototype.o = function(id) {
195         var cn = this.aNodes[id];
196         this.nodeStatus(!cn._io, id, cn._ls);
197         cn._io = !cn._io;
198         if (this.config.closeSameLevel) this.closeLevel(cn);
199         if (this.config.useCookies) this.updateCookie();
200 };
201
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;
207                 }
208         }
209         if (this.config.useCookies) this.updateCookie();
210 };
211
212 dTree.prototype.openTo = function(nId, bSelect, bFirst) {
213         if (!bFirst) {
214                 for (var n=0; n<this.aNodes.length; n++) {
215                         if (this.aNodes[n].id == nId) {
216                                 nId=n;
217                                 break;
218                         }
219                 }
220         }
221         var cn=this.aNodes[nId];
222         if (cn.pid==this.root.id || !cn._p) return;
223         cn._io = true;
224         cn._is = bSelect;
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);
229 };
230
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]);
237                 }
238         }
239 }
240
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]);          
247                 }
248         }
249 }
250
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;
257         }
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';
262 };
263
264
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);
270 };
271
272 dTree.prototype.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) {
273         document.cookie =
274                 escape(cookieName) + '=' + escape(cookieValue)
275                 + (expires ? '; expires=' + expires.toGMTString() : '')
276                 + (path ? '; path=' + path : '')
277                 + (domain ? '; domain=' + domain : '')
278                 + (secure ? '; secure' : '');
279 };
280
281 dTree.prototype.getCookie = function(cookieName) {
282         var cookieValue = '';
283         var posName = document.cookie.indexOf(escape(cookieName) + '=');
284         if (posName != -1) {
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));
289         }
290         return (cookieValue);
291 };
292
293 dTree.prototype.updateCookie = function() {
294         var str = '';
295         for (var n=0; n<this.aNodes.length; n++) {
296                 if (this.aNodes[n]._io && this.aNodes[n].pid != this.root.id) {
297                         if (str) str += '.';
298                         str += this.aNodes[n].id;
299                 }
300         }
301         this.setCookie('co' + this.obj, str);
302 };
303
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;
308         return false;
309 };
310
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];
315                 return this.length;
316         }
317 };
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);
322                 return lastElement;
323         }
324 };