Subversion Repositories svnkaklik

Rev

Go to most recent revision | Details | Last modification | View Log

Rev Author Line No. Line
36 kaklik 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 getSizes() {
13
  var len = 0;
14
  for (var n=0; n<d.aNodes.length; n++) {
15
    var c = document.getElementById("bd" + n);
16
    if(c.checked)
17
      len += d.aNodes[n].size;
18
  }
19
  return len;
20
}
21
 
22
function drawSel() {
23
 
24
  $rsize = "";
25
    if (sel > (1024 * 1024 * 1024))
26
      {
27
        rsize = Math.round((sel / (1024 * 1024 * 1024))*100)/100 + " GB";
28
      }
29
    else if (sel < 1024 * 1024)
30
      {
31
        rsize = Math.round((sel / 1024)*10)/10 + " KB";
32
      }
33
    else
34
      {
35
        rsize = Math.round((sel / (1024 * 1024))*10)/10 + " MB";
36
      }
37
 
38
  document.getElementById("sel").innerHTML = String(sel) + " (" + rsize +")";
39
 
40
}
41
 
42
function chg(node,status,recursion) {
43
 
44
  var n=0;
45
 
46
  if (typeof status == 'undefined') {
47
    while(d.aNodes[n].id != node && n<d.aNodes.length) n++;
48
    c = document.getElementById("bd" + n);
49
    status = c.checked;
50
    c.checked == true ? sel+=d.aNodes[n].size : sel-=d.aNodes[n].size;
51
  }
52
 
53
  for (n=0; n<d.aNodes.length; n++) {
54
		if (d.aNodes[n].pid == node) {
55
          c = document.getElementById("bd" + n);
56
          if(status != c.checked) status == true ? sel+=d.aNodes[n].size : sel-=d.aNodes[n].size;
57
          c.checked = status;
58
   		  chg(d.aNodes[n].id,status,true);
59
		}
60
	}
61
 
62
  if (typeof recursion == 'undefined')
63
    drawSel();
64
 
65
}
66
 
67
 
68
// Node object
69
function Node(id, pid, name, url, title, target, icon, iconOpen, open, prio, size) {
70
	this.id = id;
71
	this.pid = pid;
72
	this.name = name;
73
	this.url = url;
74
	this.title = title;
75
	this.target = target;
76
	this.icon = icon;
77
	this.iconOpen = iconOpen;
78
	this._io = open || false;
79
    this.prio = prio || -1;
80
    this.size = size || 0;
81
	this._is = false;
82
	this._ls = false;
83
	this._hc = false;
84
	this._ai = 0;
85
	this._p;
86
};
87
 
88
// Tree object
89
function dTree(objName) {
90
	this.config = {
91
		target        : null,
92
		folderLinks   : true,
93
		useSelection  : true,
94
		useCookies    : true,
95
		useLines      : true,
96
		useIcons      : true,
97
		useStatusText : false,
98
		closeSameLevel: false,
99
		inOrder       : false
100
	}
101
	this.icon = {
102
		root          : 'images/dtree/base.gif',
103
		folder        : 'images/dtree/folder.gif',
104
		folderOpen    : 'images/dtree/folderopen.gif',
105
		node          : 'images/dtree/page.gif',
106
		empty         : 'images/dtree/empty.gif',
107
		line          : 'images/dtree/line.gif',
108
		join          : 'images/dtree/join.gif',
109
		joinBottom    : 'images/dtree/joinbottom.gif',
110
		plus          : 'images/dtree/plus.gif',
111
		plusBottom    : 'images/dtree/plusbottom.gif',
112
		minus         : 'images/dtree/minus.gif',
113
		minusBottom   : 'images/dtree/minusbottom.gif',
114
		nlPlus        : 'images/dtree/nolines_plus.gif',
115
		nlMinus       : 'images/dtree/nolines_minus.gif'
116
	};
117
	this.obj = objName;
118
	this.aNodes = [];
119
	this.aIndent = [];
120
	this.root = new Node(-1);
121
	this.selectedNode = null;
122
	this.selectedFound = false;
123
	this.completed = false;
124
};
125
 
126
// Adds a new node to the node array
127
dTree.prototype.add = function(id, pid, name, prio, size, url, title, target, icon, iconOpen, open) {
128
	this.aNodes[this.aNodes.length] = new Node(id, pid, name, url, title, target, icon, iconOpen, open, prio, size);
129
};
130
 
131
// Open/close all nodes
132
dTree.prototype.openAll = function() {
133
	this.oAll(true);
134
};
135
dTree.prototype.closeAll = function() {
136
	this.oAll(false);
137
};
138
 
139
// Outputs the tree to the page
140
dTree.prototype.toString = function() {
141
	var str = '<div class="dtree">\n';
142
	if (document.getElementById) {
143
		if (this.config.useCookies) this.selectedNode = this.getSelected();
144
		str += this.addNode(this.root);
145
	} else str += 'Browser not supported.';
146
	str += '</div>';
147
	if (!this.selectedFound) this.selectedNode = null;
148
	this.completed = true;
149
	return str;
150
};
151
 
152
// Creates the tree structure
153
dTree.prototype.addNode = function(pNode) {
154
	var str = '';
155
	var n=0;
156
	if (this.config.inOrder) n = pNode._ai;
157
	for (n; n<this.aNodes.length; n++) {
158
		if (this.aNodes[n].pid == pNode.id) {
159
			var cn = this.aNodes[n];
160
			cn._p = pNode;
161
			cn._ai = n;
162
			this.setCS(cn);
163
			if (!cn.target && this.config.target) cn.target = this.config.target;
164
			if (cn._hc && !cn._io && this.config.useCookies) cn._io = this.isOpen(cn.id);
165
			if (!this.config.folderLinks && cn._hc) cn.url = null;
166
			if (this.config.useSelection && cn.id == this.selectedNode && !this.selectedFound) {
167
					cn._is = true;
168
					this.selectedNode = n;
169
					this.selectedFound = true;
170
			}
171
			str += this.node(cn, n);
172
			if (cn._ls) break;
173
		}
174
	}
175
	return str;
176
};
177
 
178
// Creates the node icon, url and text
179
dTree.prototype.node = function(node, nodeId) {
180
	var str = '<div class="dTreeNode">' + this.indent(node, nodeId);
181
	if (this.config.useIcons) {
182
		if (!node.icon) node.icon = (this.root.id == node.pid) ? this.icon.root : ((node._hc) ? this.icon.folder : this.icon.node);
183
		if (!node.iconOpen) node.iconOpen = (node._hc) ? this.icon.folderOpen : this.icon.node;
184
		if (this.root.id == node.pid) {
185
			node.icon = this.icon.root;
186
			node.iconOpen = this.icon.root;
187
		}
188
		str += '<img id="i' + this.obj + nodeId + '" src="' + ((node._io) ? node.iconOpen : node.icon) + '" alt="" />';
189
	}
190
// dirty hack by alatar :)
191
    str += '<input type="checkbox" id="b' + this.obj + nodeId + '" name="files[]" onClick=" chg(' +  node.id + '); return true;" value="' + node.id + '"'+((node.prio==1)?" checked":"")+' />';
192
// alatar end
193
	if (node.url) {
194
		str += '<a id="s' + this.obj + nodeId + '" class="' + ((this.config.useSelection) ? ((node._is ? 'nodeSel' : 'node')) : 'node') + '" href="' + node.url + '"';
195
		if (node.title) str += ' title="' + node.title + '"';
196
		if (node.target) str += ' target="' + node.target + '"';
197
		if (this.config.useStatusText) str += ' onMouseOver="window.status=\'' + node.name + '\';return true;" onMouseOut="window.status=\'\';return true;" ';
198
		if (this.config.useSelection && ((node._hc && this.config.folderLinks) || !node._hc))
199
			str += ' onclick="javascript: ' + this.obj + '.s(' + nodeId + ');"';
200
		str += '>';
201
	}
202
	else if ((!this.config.folderLinks || !node.url) && node._hc && node.pid != this.root.id)
203
		str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');" class="node" >';
204
	str += node.name;
205
	if (node.url || ((!this.config.folderLinks || !node.url) && node._hc)) str += '</a>';
206
	str += '</div>';
207
	if (node._hc) {
208
		str += '<div id="d' + this.obj + nodeId + '" class="clip" style="display:' + ((this.root.id == node.pid || node._io) ? 'block' : 'none') + ';">';
209
		str += this.addNode(node);
210
		str += '</div>';
211
	}
212
	this.aIndent.pop();
213
 
214
	return str;
215
};
216
 
217
// Adds the empty and line icons
218
dTree.prototype.indent = function(node, nodeId) {
219
	var str = '';
220
	if (this.root.id != node.pid) {
221
		for (var n=0; n<this.aIndent.length; n++)
222
			str += '<img src="' + ( (this.aIndent[n] == 1 && this.config.useLines) ? this.icon.line : this.icon.empty ) + '" alt="" />';
223
		(node._ls) ? this.aIndent.push(0) : this.aIndent.push(1);
224
		if (node._hc) {
225
			str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');"><img id="j' + this.obj + nodeId + '" src="';
226
			if (!this.config.useLines) str += (node._io) ? this.icon.nlMinus : this.icon.nlPlus;
227
			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 ) );
228
			str += '" alt="" /></a>';
229
		} else str += '<img src="' + ( (this.config.useLines) ? ((node._ls) ? this.icon.joinBottom : this.icon.join ) : this.icon.empty) + '" alt="" />';
230
	}
231
	return str;
232
};
233
 
234
// Checks if a node has any children and if it is the last sibling
235
dTree.prototype.setCS = function(node) {
236
	var lastId;
237
	for (var n=0; n<this.aNodes.length; n++) {
238
		if (this.aNodes[n].pid == node.id) node._hc = true;
239
		if (this.aNodes[n].pid == node.pid) lastId = this.aNodes[n].id;
240
	}
241
	if (lastId==node.id) node._ls = true;
242
};
243
 
244
// Returns the selected node
245
dTree.prototype.getSelected = function() {
246
	var sn = this.getCookie('cs' + this.obj);
247
	return (sn) ? sn : null;
248
};
249
 
250
// Highlights the selected node
251
dTree.prototype.s = function(id) {
252
	if (!this.config.useSelection) return;
253
	var cn = this.aNodes[id];
254
	if (cn._hc && !this.config.folderLinks) return;
255
	if (this.selectedNode != id) {
256
		if (this.selectedNode || this.selectedNode==0) {
257
			eOld = document.getElementById("s" + this.obj + this.selectedNode);
258
			eOld.className = "node";
259
		}
260
		eNew = document.getElementById("s" + this.obj + id);
261
		eNew.className = "nodeSel";
262
		this.selectedNode = id;
263
		if (this.config.useCookies) this.setCookie('cs' + this.obj, cn.id);
264
	}
265
};
266
 
267
// Toggle Open or close
268
dTree.prototype.o = function(id) {
269
	var cn = this.aNodes[id];
270
	this.nodeStatus(!cn._io, id, cn._ls);
271
	cn._io = !cn._io;
272
	if (this.config.closeSameLevel) this.closeLevel(cn);
273
	if (this.config.useCookies) this.updateCookie();
274
};
275
 
276
// Open or close all nodes
277
dTree.prototype.oAll = function(status) {
278
	for (var n=0; n<this.aNodes.length; n++) {
279
		if (this.aNodes[n]._hc && this.aNodes[n].pid != this.root.id) {
280
			this.nodeStatus(status, n, this.aNodes[n]._ls)
281
			this.aNodes[n]._io = status;
282
		}
283
	}
284
	if (this.config.useCookies) this.updateCookie();
285
};
286
 
287
// Opens the tree to a specific node
288
dTree.prototype.openTo = function(nId, bSelect, bFirst) {
289
	if (!bFirst) {
290
		for (var n=0; n<this.aNodes.length; n++) {
291
			if (this.aNodes[n].id == nId) {
292
				nId=n;
293
				break;
294
			}
295
		}
296
	}
297
	var cn=this.aNodes[nId];
298
	if (cn.pid==this.root.id || !cn._p) return;
299
	cn._io = true;
300
	cn._is = bSelect;
301
	if (this.completed && cn._hc) this.nodeStatus(true, cn._ai, cn._ls);
302
	if (this.completed && bSelect) this.s(cn._ai);
303
	else if (bSelect) this._sn=cn._ai;
304
	this.openTo(cn._p._ai, false, true);
305
};
306
 
307
// Closes all nodes on the same level as certain node
308
dTree.prototype.closeLevel = function(node) {
309
	for (var n=0; n<this.aNodes.length; n++) {
310
		if (this.aNodes[n].pid == node.pid && this.aNodes[n].id != node.id && this.aNodes[n]._hc) {
311
			this.nodeStatus(false, n, this.aNodes[n]._ls);
312
			this.aNodes[n]._io = false;
313
			this.closeAllChildren(this.aNodes[n]);
314
		}
315
	}
316
}
317
 
318
// Closes all children of a node
319
dTree.prototype.closeAllChildren = function(node) {
320
	for (var n=0; n<this.aNodes.length; n++) {
321
		if (this.aNodes[n].pid == node.id && this.aNodes[n]._hc) {
322
			if (this.aNodes[n]._io) this.nodeStatus(false, n, this.aNodes[n]._ls);
323
			this.aNodes[n]._io = false;
324
			this.closeAllChildren(this.aNodes[n]);
325
		}
326
	}
327
}
328
 
329
// Change the status of a node(open or closed)
330
dTree.prototype.nodeStatus = function(status, id, bottom) {
331
	eDiv	= document.getElementById('d' + this.obj + id);
332
	eJoin	= document.getElementById('j' + this.obj + id);
333
	if (this.config.useIcons) {
334
		eIcon	= document.getElementById('i' + this.obj + id);
335
		eIcon.src = (status) ? this.aNodes[id].iconOpen : this.aNodes[id].icon;
336
	}
337
	eJoin.src = (this.config.useLines)?
338
	((status)?((bottom)?this.icon.minusBottom:this.icon.minus):((bottom)?this.icon.plusBottom:this.icon.plus)):
339
	((status)?this.icon.nlMinus:this.icon.nlPlus);
340
	eDiv.style.display = (status) ? 'block': 'none';
341
};
342
 
343
// [Cookie] Clears a cookie
344
dTree.prototype.clearCookie = function() {
345
	var now = new Date();
346
	var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
347
	this.setCookie('co'+this.obj, 'cookieValue', yesterday);
348
	this.setCookie('cs'+this.obj, 'cookieValue', yesterday);
349
};
350
 
351
// [Cookie] Sets value in a cookie
352
dTree.prototype.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) {
353
	document.cookie =
354
		escape(cookieName) + '=' + escape(cookieValue)
355
		+ (expires ? '; expires=' + expires.toGMTString() : '')
356
		+ (path ? '; path=' + path : '')
357
		+ (domain ? '; domain=' + domain : '')
358
		+ (secure ? '; secure' : '');
359
};
360
 
361
// [Cookie] Gets a value from a cookie
362
dTree.prototype.getCookie = function(cookieName) {
363
	var cookieValue = '';
364
	var posName = document.cookie.indexOf(escape(cookieName) + '=');
365
	if (posName != -1) {
366
		var posValue = posName + (escape(cookieName) + '=').length;
367
		var endPos = document.cookie.indexOf(';', posValue);
368
		if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
369
		else cookieValue = unescape(document.cookie.substring(posValue));
370
	}
371
	return (cookieValue);
372
};
373
 
374
// [Cookie] Returns ids of open nodes as a string
375
dTree.prototype.updateCookie = function() {
376
	var str = '';
377
	for (var n=0; n<this.aNodes.length; n++) {
378
		if (this.aNodes[n]._io && this.aNodes[n].pid != this.root.id) {
379
			if (str) str += '.';
380
			str += this.aNodes[n].id;
381
		}
382
	}
383
	this.setCookie('co' + this.obj, str);
384
};
385
 
386
// [Cookie] Checks if a node id is in a cookie
387
dTree.prototype.isOpen = function(id) {
388
	var aOpen = this.getCookie('co' + this.obj).split('.');
389
	for (var n=0; n<aOpen.length; n++)
390
		if (aOpen[n] == id) return true;
391
	return false;
392
};
393
 
394
// If Push and pop is not implemented by the browser
395
if (!Array.prototype.push) {
396
	Array.prototype.push = function array_push() {
397
		for(var i=0;i<arguments.length;i++)
398
			this[this.length]=arguments[i];
399
		return this.length;
400
	}
401
};
402
if (!Array.prototype.pop) {
403
	Array.prototype.pop = function array_pop() {
404
		lastElement = this[this.length-1];
405
		this.length = Math.max(this.length-1,0);
406
		return lastElement;
407
	}
408
};