dtree.js
12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
-----------------------------------------------------------------------------
dTree 2.0 | www.destroydrop.com/javascript/tree/
-----------------------------------------------------------------------------
Copyright (c) 2002 Geir Landrö
This script can be used freely as long as all copyright messages are
intact.
-----------------------------------------------------------------------------
*/
// * Temporary solution for IE6 select's bug
function hideSelect()
{
if (document.all)
{
document.all.ultimosacessos.style.visibility="hidden";
}
}
function unhideSelect()
{
if (document.all)
{
document.all.ultimosacessos.style.visibility="visible";
}
}
// * End
// Node object
function Node(id, pid, name, url, title, target, isopen, img)
{
this.id = id;
this.pid = pid;
this.name = name;
this.url = url; //'/gsan/' + url;
this.title = title;
this.target = target;
this.img = img;
this._io = isopen || false;
this._ls = false;
this._hc = false;
this._is = false;
}
// Tree object
function dTree(objName)
{
// Variables
// ----------------------------------------------------------------------------
this.arrNodes = [];
this.arrRecursed = [];
this.arrIcons = [];
this.rootNode = -1;
this.strOutput = '';
this.selectedNode = null;
this.instanceName = objName;
this.imgFolder = '/gsan/imagens/';
this.target = null;
this.hasLines = true;
this.clickSelect = true;
this.folderLinks = false;
this.useCookies = false;
// Functions
// ----------------------------------------------------------------------------
// Adds a new node to the node array
this.add = function(id, pid, name, url, title, target, isopen, img)
{
this.arrNodes[this.arrNodes.length] = new Node(id, pid, name, url, title, target, isopen, img);
}
// Outputs the tree to the page
this.draw = function()
{
if (document.getElementById)
{
this.preloadIcons();
if (this.useCookies) this.selectedNode = this.getSelected();
this.addNode(this.rootNode);
document.writeln(this.strOutput);
}
else
{
document.writeln('Browser not supported.');
}
}
this.openAll = function()
{
this.oAll(true);
}
this.closeAll = function()
{
this.oAll(false);
}
// Private
// ----------------------------------------------------------------------------
// Prealoads images that are used in the tree
this.preloadIcons = function()
{
if (this.hasLines)
{
this.arrIcons[0] = new Image();
this.arrIcons[0].src = this.imgFolder + 'plus.gif';
this.arrIcons[1] = new Image();
this.arrIcons[1].src = this.imgFolder + 'plusbottom.gif';
this.arrIcons[2] = new Image();
this.arrIcons[2].src = this.imgFolder + 'minus.gif';
this.arrIcons[3] = new Image();
this.arrIcons[3].src = this.imgFolder + 'minusbottom.gif';
}
else
{
this.arrIcons[0] = new Image();
this.arrIcons[0].src = this.imgFolder + 'nolines_plus.gif';
this.arrIcons[1] = new Image();
this.arrIcons[1].src = this.imgFolder + 'nolines_plus.gif';
this.arrIcons[2] = new Image();
this.arrIcons[2].src = this.imgFolder + 'nolines_minus.gif';
this.arrIcons[3] = new Image();
this.arrIcons[3].src = this.imgFolder + 'nolines_minus.gif';
}
this.arrIcons[4] = new Image();
this.arrIcons[4].src = this.imgFolder + 'folder.gif';
this.arrIcons[5] = new Image();
this.arrIcons[5].src = this.imgFolder + 'folderopen.gif';
}
// Recursive function that creates the tree structure
this.addNode = function(pNode)
{
for (var n=0; n<this.arrNodes.length; n++)
{
if (this.arrNodes[n].pid == pNode)
{
var cn = this.arrNodes[n];
cn._hc = this.hasChildren(cn);
cn._ls = (this.hasLines) ? this.lastSibling(cn) : false;
if (cn._hc && !cn._io && this.useCookies) cn._io = this.isOpen(cn.id);
if (this.clickSelect && cn.id == this.selectedNode)
{
cn._is = true;
this.selectedNode = n;
}
if (!this.folderLinks && cn._hc) cn.url = null;
// If the current node is not the root
if (this.rootNode != cn.pid)
{
// Write out line & empty icons
for (r=0; r<this.arrRecursed.length; r++)
this.strOutput += '<img src="' + this.imgFolder + ( (this.arrRecursed[r] == 1 && this.hasLines) ? 'line' : 'empty' ) + '.gif" alt="" />';
// Line & empty icons
(cn._ls) ? this.arrRecursed.push(0) : this.arrRecursed.push(1);
// Write out join icons
if (cn._hc)
{
this.strOutput += '<a href="javascript: ' + this.instanceName + '.o(' + n + ');">'
+ '<img id="j' + this.instanceName + n + '" src="' + this.imgFolder;
if (!this.hasLines)
this.strOutput += 'nolines_';
this.strOutput += ( (cn._io) ? ((cn._ls && this.hasLines) ? 'minusbottom' : 'minus') : ((cn._ls && this.hasLines) ? 'plusbottom' : 'plus' ) )
+ '.gif" alt="" /></a>';
}
else
this.strOutput += '<img src="' + this.imgFolder + ( (this.hasLines) ? ((cn._ls) ? 'joinbottom' : 'join' ) : 'empty') + '.gif" alt="" />';
}
// Start the node link
if (cn.url)
{
this.strOutput += '<a href="' + cn.url + '"';
if (cn.title) this.strOutput += ' title="' + cn.title + '"';
if (cn.target) this.strOutput += ' target="' + cn.target + '"';
if (this.target && !cn.target) this.strOutput += ' target="' + this.target + '"';
// If hightlight link is on
if (this.clickSelect)
{
if (cn._hc)
{
if (this.folderLinks)
this.strOutput += ' onclick="javascript: ' + this.instanceName + '.s(' + n + ');"';
}
else
{
this.strOutput += ' onclick="javascript: ' + this.instanceName + '.s(' + n + ');"';
}
}
this.strOutput += '>';
}
if ((!this.folderLinks || !cn.url) && cn._hc && cn.pid != this.rootNode)
{
this.strOutput += '<a href="javascript: ' + this.instanceName + '.o(' + n + ');">';
}
// Write out folder & page icons
this.strOutput += '<img id="i' + this.instanceName + n + '" src="' + this.imgFolder;
this.strOutput += (cn.img) ? cn.img : ((this.rootNode == cn.pid) ? 'base' : (cn._hc) ? ((cn._io) ? 'folderopen' : 'folder') : 'page') + '.gif';
this.strOutput += '" alt="" />';
// Write out span
this.strOutput += '<span id="s' + this.instanceName + n + '" class="' + ((this.clickSelect) ? ((cn._is ? 'nodeSel' : 'node')) : 'node') + '">';
// Write out node name
this.strOutput += cn.name;
this.strOutput += '</span>';
// Close the link
if (cn.url || (!this.folderLinks && cn._hc)) this.strOutput += '</a>';
this.strOutput += '<br />\n';
// If node has children write out divs and go deeper
if (cn._hc)
{
this.strOutput += '<div id="d' + this.instanceName + n + '" style="display:'
+ ((this.rootNode == cn.pid || cn._io) ? 'block' : 'none')
+ ';">\n';
this.addNode(cn.id);
this.strOutput += '</div>\n';
}
this.arrRecursed.pop();
}
}
}
// Checks if a node has any children
this.hasChildren = function(node)
{
for (n=0; n<this.arrNodes.length; n++)
if (this.arrNodes[n].pid == node.id) return true;
return false;
}
// Checks if a node is the last sibling
this.lastSibling = function(node)
{
var lastId;
for (n=0; n< this.arrNodes.length; n++)
if (this.arrNodes[n].pid == node.pid) lastId = this.arrNodes[n].id;
if (lastId==node.id) return true;
return false;
}
// Checks if a node id is in a cookie
this.isOpen = function(id)
{
openNodes = this.getCookie('co' + this.instanceName).split('.');
for (n=0;n<openNodes.length;n++)
if (openNodes[n] == id) return true;
return false;
}
// Checks if the node is selected
this.isSelected = function(id)
{
selectedNode = this.getCookie('cs' + this.instanceName);
if (selectedNode)
{
if (id==selectedNode)
{
this.selectedNode = id;
return true
}
}
return false;
}
// Returns the selected node
this.getSelected = function()
{
selectedNode = this.getCookie('cs' + this.instanceName);
if (selectedNode) return selectedNode;
return null;
}
// Highlights the selected node
this.s = function(id)
{
cn = this.arrNodes[id];
if (this.selectedNode != id)
{
if (this.selectedNode)
{
eOldSpan = document.getElementById("s" + this.instanceName + this.selectedNode);
eOldSpan.className = "node";
}
eNewSpan = document.getElementById("s" + this.instanceName + id);
eNewSpan.className = "nodeSel";
this.selectedNode = id;
if (this.useCookies) this.setCookie('cs' + this.instanceName, cn.id);
}
}
// Toggle Open or close
this.o = function(id)
{
cn = this.arrNodes[id];
(cn._io) ? this.nodeClose(id,cn._ls) : this.nodeOpen(id,cn._ls);
// * Temporary solution for IE6 select's bug
if (id == 1)
{
(cn._io) ? unhideSelect() : hideSelect();
}
// * End
cn._io = !cn._io;
if (this.useCookies) this.updateCookie();
}
// Open or close all nodes
this.oAll = function(open)
{
for (n=0;n<this.arrNodes.length;n++)
{
if (this.arrNodes[n]._hc && this.arrNodes[n].pid != this.rootNode)
{
if (open)
{
this.nodeOpen(n, this.arrNodes[n]._ls);
this.arrNodes[n]._io = true;
}
else
{
this.nodeClose(n, this.arrNodes[n]._ls);
this.arrNodes[n]._io = false;
}
}
}
if (this.useCookies) this.updateCookie();
}
// Open a node
this.nodeOpen = function(id, bottom)
{
eDiv = document.getElementById('d' + this.instanceName + id);
eJoin = document.getElementById('j' + this.instanceName + id);
eIcon = document.getElementById('i' + this.instanceName + id);
eJoin.src = (bottom) ? this.arrIcons[3].src : this.arrIcons[2].src;
if (!this.arrNodes[id].img) eIcon.src = this.arrIcons[5].src;
eDiv.style.display = 'block';
/*teste = document.getElementById('ultimosacessos');
teste.style.visibility = "hidden";*/
}
// Close a node
this.nodeClose = function(id, bottom)
{
eDiv = document.getElementById('d' + this.instanceName + id);
eJoin = document.getElementById('j' + this.instanceName + id);
eIcon = document.getElementById('i' + this.instanceName + id);
eJoin.src = (bottom) ? this.arrIcons[1].src : this.arrIcons[0].src;
if (!this.arrNodes[id].img) eIcon.src = this.arrIcons[4].src;
eDiv.style.display = 'none';
/*if(id == '1'){
teste = document.getElementById('ultimosacessos');
teste.style.visibility = "visible";
}*/
}
// Clears a cookie
this.clearCookie = function()
{
var now = new Date();
var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
this.setCookie('co'+this.instanceName, 'cookieValue', yesterday);
this.setCookie('cs'+this.instanceName, 'cookieValue', yesterday);
}
// Sets value in a cookie
this.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) {
document.cookie =
escape(cookieName) + '=' + escape(cookieValue)
+ (expires ? '; expires=' + expires.toGMTString() : '')
+ (path ? '; path=' + path : '')
+ (domain ? '; domain=' + domain : '')
+ (secure ? '; secure' : '');
}
// Gets a value from a cookie
this.getCookie = function(cookieName) {
var cookieValue = '';
var posName = document.cookie.indexOf(escape(cookieName) + '=');
if (posName != -1)
{
var posValue = posName + (escape(cookieName) + '=').length;
var endPos = document.cookie.indexOf(';', posValue);
if (endPos != -1)
cookieValue = unescape(document.cookie.substring(posValue, endPos));
else
cookieValue = unescape(document.cookie.substring(posValue));
}
return (cookieValue);
}
// Returns ids of open nodes as a string
this.updateCookie = function()
{
sReturn = '';
for (n=0;n<this.arrNodes.length;n++)
{
if (this.arrNodes[n]._io && this.arrNodes[n].pid != this.rootNode)
{
if (sReturn) sReturn += '.';
sReturn += this.arrNodes[n].id;
}
}
this.setCookie('co' + this.instanceName, sReturn);
}
// tree object ends
}
// Functions used by the dTree object but are not really a part of it
// ------------------------------------------------------------------------------------------------
// Push and pop for arrays is not implemented in Internet Explorer
if (!Array.prototype.push) {
Array.prototype.push = function array_push() {
for(var i=0;i<arguments.length;i++)
this[this.length]=arguments[i];
return this.length;
}
}
if (!Array.prototype.pop) {
Array.prototype.pop = function array_pop() {
lastElement = this[this.length-1];
this.length = Math.max(this.length-1,0);
return lastElement;
}
}