tree.js
5.38 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
/*
[Discuz!] (C)2001-2099 Comsenz Inc.
This is NOT a freeware, use is subject to license terms
$Id: tree.js 23838 2011-08-11 06:51:58Z monkey $
*/
var icon = new Object();
icon.root = IMGDIR + '/tree_root.gif';
icon.folder = IMGDIR + '/tree_folder.gif';
icon.folderOpen = IMGDIR + '/tree_folderopen.gif';
icon.file = IMGDIR + '/tree_file.gif';
icon.empty = IMGDIR + '/tree_empty.gif';
icon.line = IMGDIR + '/tree_line.gif';
icon.lineMiddle = IMGDIR + '/tree_linemiddle.gif';
icon.lineBottom = IMGDIR + '/tree_linebottom.gif';
icon.plus = IMGDIR + '/tree_plus.gif';
icon.plusMiddle = IMGDIR + '/tree_plusmiddle.gif';
icon.plusBottom = IMGDIR + '/tree_plusbottom.gif';
icon.minus = IMGDIR + '/tree_minus.gif';
icon.minusMiddle = IMGDIR + '/tree_minusmiddle.gif';
icon.minusBottom = IMGDIR + '/tree_minusbottom.gif';
function treeNode(id, pid, name, url, target, open) {
var obj = new Object();
obj.id = id;
obj.pid = pid;
obj.name = name;
obj.url = url;
obj.target = target;
obj.open = open;
obj._isOpen = open;
obj._lastChildId = 0;
obj._pid = 0;
return obj;
}
function dzTree(treeName) {
this.nodes = new Array();
this.openIds = getcookie('leftmenu_openids');
this.pushNodes = new Array();
this.addNode = function(id, pid, name, url, target, open) {
var theNode = new treeNode(id, pid, name, url, target, open);
this.pushNodes.push(id);
if(!this.nodes[pid]) {
this.nodes[pid] = new Array();
}
this.nodes[pid]._lastChildId = id;
for(k in this.nodes) {
if(this.openIds && this.openIds.indexOf('_' + theNode.id) != -1) {
theNode._isOpen = true;
}
if(this.nodes[k].pid == id) {
theNode._lastChildId = this.nodes[k].id;
}
}
this.nodes[id] = theNode;
};
this.show = function() {
var s = '<div class="tree">';
s += this.createTree(this.nodes[0]);
s += '</div>';
document.write(s);
};
this.createTree = function(node, padding) {
padding = padding ? padding : '';
if(node.id == 0){
var icon1 = '';
} else {
var icon1 = '<img src="' + this.getIcon1(node) + '" onclick="' + treeName + '.switchDisplay(\'' + node.id + '\')" id="icon1_' + node.id + '" style="cursor: pointer;">';
}
var icon2 = '<img src="' + this.getIcon2(node) + '" onclick="' + treeName + '.switchDisplay(\'' + node.id + '\')" id="icon2_' + node.id + '" style="cursor: pointer;">';
var s = '<div class="node" id="node_' + node.id + '">' + padding + icon1 + icon2 + this.getName(node) + '</div>';
s += '<div class="nodes" id="nodes_' + node.id + '" style="display:' + (node._isOpen ? '' : 'none') + '">';
for(k in this.pushNodes) {
var id = this.pushNodes[k];
var theNode = this.nodes[id];
if(theNode.pid == node.id) {
if(node.id == 0){
var thePadding = '';
} else {
var thePadding = padding + (node.id == this.nodes[node.pid]._lastChildId ? '<img src="' + icon.empty + '">' : '<img src="' + icon.line + '">');
}
if(!theNode._lastChildId) {
var icon1 = '<img src="' + this.getIcon1(theNode) + '"' + ' id="icon1_' + theNode.id + '">';
var icon2 = '<img src="' + this.getIcon2(theNode) + '" id="icon2_' + theNode.id + '">';
s += '<div class="node" id="node_' + theNode.id + '">' + thePadding + icon1 + icon2 + this.getName(theNode) + '</div>';
} else {
s += this.createTree(theNode, thePadding);
}
}
}
s += '</div>';
return s;
};
this.getIcon1 = function(theNode) {
var parentNode = this.nodes[theNode.pid];
var src = '';
if(theNode._lastChildId) {
if(theNode._isOpen) {
if(theNode.id == 0) {
return icon.minus;
}
if(theNode.id == parentNode._lastChildId) {
src = icon.minusBottom;
} else {
src = icon.minusMiddle;
}
} else {
if(theNode.id == 0) {
return icon.plus;
}
if(theNode.id == parentNode._lastChildId) {
src = icon.plusBottom;
} else {
src = icon.plusMiddle;
}
}
} else {
if(theNode.id == parentNode._lastChildId) {
src = icon.lineBottom;
} else {
src = icon.lineMiddle;
}
}
return src;
};
this.getIcon2 = function(theNode) {
var src = '';
if(theNode.id == 0 ) {
return icon.root;
}
if(theNode._lastChildId) {
if(theNode._isOpen) {
src = icon.folderOpen;
} else {
src = icon.folder;
}
} else {
src = icon.file;
}
return src;
};
this.getName = function(theNode) {
if(theNode.url) {
return '<a href="'+theNode.url+'" target="' + theNode.target + '"> '+theNode.name+'</a>';
} else {
return theNode.name;
}
};
this.switchDisplay = function(nodeId) {
eval('var theTree = ' + treeName);
var theNode = theTree.nodes[nodeId];
if($('nodes_' + nodeId).style.display == 'none') {
theTree.openIds = updatestring(theTree.openIds, nodeId);
setcookie('leftmenu_openids', theTree.openIds, 8640000000);
theNode._isOpen = true;
$('nodes_' + nodeId).style.display = '';
$('icon1_' + nodeId).src = theTree.getIcon1(theNode);
$('icon2_' + nodeId).src = theTree.getIcon2(theNode);
} else {
theTree.openIds = updatestring(theTree.openIds, nodeId, true);
setcookie('leftmenu_openids', theTree.openIds, 8640000000);
theNode._isOpen = false;
$('nodes_' + nodeId).style.display = 'none';
$('icon1_' + nodeId).src = theTree.getIcon1(theNode);
$('icon2_' + nodeId).src = theTree.getIcon2(theNode);
}
};
}