tree.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. [Discuz!] (C)2001-2099 Comsenz Inc.
  3. This is NOT a freeware, use is subject to license terms
  4. $Id: tree.js 23838 2011-08-11 06:51:58Z monkey $
  5. */
  6. var icon = new Object();
  7. icon.root = IMGDIR + '/tree_root.gif';
  8. icon.folder = IMGDIR + '/tree_folder.gif';
  9. icon.folderOpen = IMGDIR + '/tree_folderopen.gif';
  10. icon.file = IMGDIR + '/tree_file.gif';
  11. icon.empty = IMGDIR + '/tree_empty.gif';
  12. icon.line = IMGDIR + '/tree_line.gif';
  13. icon.lineMiddle = IMGDIR + '/tree_linemiddle.gif';
  14. icon.lineBottom = IMGDIR + '/tree_linebottom.gif';
  15. icon.plus = IMGDIR + '/tree_plus.gif';
  16. icon.plusMiddle = IMGDIR + '/tree_plusmiddle.gif';
  17. icon.plusBottom = IMGDIR + '/tree_plusbottom.gif';
  18. icon.minus = IMGDIR + '/tree_minus.gif';
  19. icon.minusMiddle = IMGDIR + '/tree_minusmiddle.gif';
  20. icon.minusBottom = IMGDIR + '/tree_minusbottom.gif';
  21. function treeNode(id, pid, name, url, target, open) {
  22. var obj = new Object();
  23. obj.id = id;
  24. obj.pid = pid;
  25. obj.name = name;
  26. obj.url = url;
  27. obj.target = target;
  28. obj.open = open;
  29. obj._isOpen = open;
  30. obj._lastChildId = 0;
  31. obj._pid = 0;
  32. return obj;
  33. }
  34. function dzTree(treeName) {
  35. this.nodes = new Array();
  36. this.openIds = getcookie('leftmenu_openids');
  37. this.pushNodes = new Array();
  38. this.addNode = function(id, pid, name, url, target, open) {
  39. var theNode = new treeNode(id, pid, name, url, target, open);
  40. this.pushNodes.push(id);
  41. if(!this.nodes[pid]) {
  42. this.nodes[pid] = new Array();
  43. }
  44. this.nodes[pid]._lastChildId = id;
  45. for(k in this.nodes) {
  46. if(this.openIds && this.openIds.indexOf('_' + theNode.id) != -1) {
  47. theNode._isOpen = true;
  48. }
  49. if(this.nodes[k].pid == id) {
  50. theNode._lastChildId = this.nodes[k].id;
  51. }
  52. }
  53. this.nodes[id] = theNode;
  54. };
  55. this.show = function() {
  56. var s = '<div class="tree">';
  57. s += this.createTree(this.nodes[0]);
  58. s += '</div>';
  59. document.write(s);
  60. };
  61. this.createTree = function(node, padding) {
  62. padding = padding ? padding : '';
  63. if(node.id == 0){
  64. var icon1 = '';
  65. } else {
  66. var icon1 = '<img src="' + this.getIcon1(node) + '" onclick="' + treeName + '.switchDisplay(\'' + node.id + '\')" id="icon1_' + node.id + '" style="cursor: pointer;">';
  67. }
  68. var icon2 = '<img src="' + this.getIcon2(node) + '" onclick="' + treeName + '.switchDisplay(\'' + node.id + '\')" id="icon2_' + node.id + '" style="cursor: pointer;">';
  69. var s = '<div class="node" id="node_' + node.id + '">' + padding + icon1 + icon2 + this.getName(node) + '</div>';
  70. s += '<div class="nodes" id="nodes_' + node.id + '" style="display:' + (node._isOpen ? '' : 'none') + '">';
  71. for(k in this.pushNodes) {
  72. var id = this.pushNodes[k];
  73. var theNode = this.nodes[id];
  74. if(theNode.pid == node.id) {
  75. if(node.id == 0){
  76. var thePadding = '';
  77. } else {
  78. var thePadding = padding + (node.id == this.nodes[node.pid]._lastChildId ? '<img src="' + icon.empty + '">' : '<img src="' + icon.line + '">');
  79. }
  80. if(!theNode._lastChildId) {
  81. var icon1 = '<img src="' + this.getIcon1(theNode) + '"' + ' id="icon1_' + theNode.id + '">';
  82. var icon2 = '<img src="' + this.getIcon2(theNode) + '" id="icon2_' + theNode.id + '">';
  83. s += '<div class="node" id="node_' + theNode.id + '">' + thePadding + icon1 + icon2 + this.getName(theNode) + '</div>';
  84. } else {
  85. s += this.createTree(theNode, thePadding);
  86. }
  87. }
  88. }
  89. s += '</div>';
  90. return s;
  91. };
  92. this.getIcon1 = function(theNode) {
  93. var parentNode = this.nodes[theNode.pid];
  94. var src = '';
  95. if(theNode._lastChildId) {
  96. if(theNode._isOpen) {
  97. if(theNode.id == 0) {
  98. return icon.minus;
  99. }
  100. if(theNode.id == parentNode._lastChildId) {
  101. src = icon.minusBottom;
  102. } else {
  103. src = icon.minusMiddle;
  104. }
  105. } else {
  106. if(theNode.id == 0) {
  107. return icon.plus;
  108. }
  109. if(theNode.id == parentNode._lastChildId) {
  110. src = icon.plusBottom;
  111. } else {
  112. src = icon.plusMiddle;
  113. }
  114. }
  115. } else {
  116. if(theNode.id == parentNode._lastChildId) {
  117. src = icon.lineBottom;
  118. } else {
  119. src = icon.lineMiddle;
  120. }
  121. }
  122. return src;
  123. };
  124. this.getIcon2 = function(theNode) {
  125. var src = '';
  126. if(theNode.id == 0 ) {
  127. return icon.root;
  128. }
  129. if(theNode._lastChildId) {
  130. if(theNode._isOpen) {
  131. src = icon.folderOpen;
  132. } else {
  133. src = icon.folder;
  134. }
  135. } else {
  136. src = icon.file;
  137. }
  138. return src;
  139. };
  140. this.getName = function(theNode) {
  141. if(theNode.url) {
  142. return '<a href="'+theNode.url+'" target="' + theNode.target + '"> '+theNode.name+'</a>';
  143. } else {
  144. return theNode.name;
  145. }
  146. };
  147. this.switchDisplay = function(nodeId) {
  148. eval('var theTree = ' + treeName);
  149. var theNode = theTree.nodes[nodeId];
  150. if($('nodes_' + nodeId).style.display == 'none') {
  151. theTree.openIds = updatestring(theTree.openIds, nodeId);
  152. setcookie('leftmenu_openids', theTree.openIds, 8640000000);
  153. theNode._isOpen = true;
  154. $('nodes_' + nodeId).style.display = '';
  155. $('icon1_' + nodeId).src = theTree.getIcon1(theNode);
  156. $('icon2_' + nodeId).src = theTree.getIcon2(theNode);
  157. } else {
  158. theTree.openIds = updatestring(theTree.openIds, nodeId, true);
  159. setcookie('leftmenu_openids', theTree.openIds, 8640000000);
  160. theNode._isOpen = false;
  161. $('nodes_' + nodeId).style.display = 'none';
  162. $('icon1_' + nodeId).src = theTree.getIcon1(theNode);
  163. $('icon2_' + nodeId).src = theTree.getIcon2(theNode);
  164. }
  165. };
  166. }