dtree.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. //liuyang
  2. // Node object
  3. function Node(id, pid, name, url, title, target, icon, iconOpen, open) {
  4. this.id = id;
  5. this.pid = pid;
  6. this.name = name;
  7. this.url = url;
  8. this.title = title;
  9. this.target = target;
  10. this.icon = icon;
  11. this.iconOpen = iconOpen;
  12. this._io = open || false;
  13. this._is = false;
  14. this._ls = false;
  15. this._hc = false;
  16. this._ai = 0;
  17. this._p;
  18. };
  19. // Tree object
  20. function dTree(objName) {
  21. this.config = {
  22. target : null,
  23. folderLinks : true,
  24. useSelection : true,
  25. useCookies : true,
  26. useLines : true,
  27. useIcons : true,
  28. useStatusText : false,
  29. closeSameLevel : false,
  30. inOrder : false
  31. }
  32. this.icon = {
  33. root : '../images/tree/base.gif',
  34. folder : '../images/tree/folder.gif',
  35. folderOpen : '../images/tree/folderopen.gif',
  36. node : '../images/tree/page.gif',
  37. empty : '../images/tree/empty.gif',
  38. line : '../images/tree/line.gif',
  39. join : '../images/tree/join.gif',
  40. joinBottom : '../images/tree/joinbottom.gif',
  41. plus : '../images/tree/plus.gif',
  42. plusBottom : '../images/tree/plusbottom.gif',
  43. minus : '../images/tree/minus.gif',
  44. minusBottom : '../images/tree/minusbottom.gif',
  45. nlPlus : '../images/tree/nolines_plus.gif',
  46. nlMinus : '../images/tree/nolines_minus.gif'
  47. };
  48. this.obj = objName;
  49. this.aNodes = [];
  50. this.aIndent = [];
  51. this.root = new Node(-1);
  52. this.selectedNode = null;
  53. this.selectedFound = false;
  54. this.completed = false;
  55. };
  56. // Adds a new node to the node array
  57. dTree.prototype.add = function(id, pid, name, url, title, target, icon, iconOpen, open) {
  58. this.aNodes[this.aNodes.length] = new Node(id, pid, name, url, title, target, icon, iconOpen, open);
  59. };
  60. // Open/close all nodes
  61. dTree.prototype.openAll = function() {
  62. this.oAll(true);
  63. };
  64. dTree.prototype.closeAll = function() {
  65. this.oAll(false);
  66. };
  67. // Outputs the tree to the page
  68. dTree.prototype.toString = function() {
  69. var str = '<div class="dtree">\n';
  70. if (document.getElementById) {
  71. if (this.config.useCookies) this.selectedNode = this.getSelected();
  72. str += this.addNode(this.root);
  73. } else str += 'Browser not supported.';
  74. str += '</div>';
  75. if (!this.selectedFound) this.selectedNode = null;
  76. this.completed = true;
  77. return str;
  78. };
  79. // Creates the tree structure
  80. dTree.prototype.addNode = function(pNode) {
  81. var str = '';
  82. var n=0;
  83. if (this.config.inOrder) n = pNode._ai;
  84. for (n; n<this.aNodes.length; n++) {
  85. if (this.aNodes[n].pid == pNode.id) {
  86. var cn = this.aNodes[n];
  87. cn._p = pNode;
  88. cn._ai = n;
  89. this.setCS(cn);
  90. if (!cn.target && this.config.target) cn.target = this.config.target;
  91. if (cn._hc && !cn._io && this.config.useCookies) cn._io = this.isOpen(cn.id);
  92. if (!this.config.folderLinks && cn._hc) cn.url = null;
  93. if (this.config.useSelection && cn.id == this.selectedNode && !this.selectedFound) {
  94. cn._is = true;
  95. this.selectedNode = n;
  96. this.selectedFound = true;
  97. }
  98. str += this.node(cn, n);
  99. if (cn._ls) break;
  100. }
  101. }
  102. return str;
  103. };
  104. // Creates the node icon, url and text
  105. dTree.prototype.node = function(node, nodeId) {
  106. var str = '<div class="dTreeNode">' + this.indent(node, nodeId);
  107. if (this.config.useIcons) {
  108. if (!node.icon) node.icon = (this.root.id == node.pid) ? this.icon.root : ((node._hc) ? this.icon.folder : this.icon.node);
  109. if (!node.iconOpen) node.iconOpen = (node._hc) ? this.icon.folderOpen : this.icon.node;
  110. if (this.root.id == node.pid) {
  111. node.icon = this.icon.root;
  112. node.iconOpen = this.icon.root;
  113. }
  114. str += '<img id="i' + this.obj + nodeId + '" src="' + ((node._io) ? node.iconOpen : node.icon) + '" alt="" />';
  115. }
  116. if (node.url) {
  117. str += '<a id="s' + this.obj + nodeId + '" class="' + ((this.config.useSelection) ? ((node._is ? 'nodeSel' : 'node')) : 'node') + '" href="' + node.url + '"';
  118. if (node.title) str += ' title="' + node.title + '"';
  119. if (node.target) str += ' target="' + node.target + '"';
  120. if (this.config.useStatusText) str += ' onmouseover="window.status=\'' + node.name + '\';return true;" onmouseout="window.status=\'\';return true;" ';
  121. if (this.config.useSelection && ((node._hc && this.config.folderLinks) || !node._hc))
  122. str += ' onclick="javascript: ' + this.obj + '.s(' + nodeId + ');"';
  123. str += '>';
  124. }
  125. else if ((!this.config.folderLinks || !node.url) && node._hc && node.pid != this.root.id)
  126. str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');" class="node">';
  127. str += node.name;
  128. if (node.url || ((!this.config.folderLinks || !node.url) && node._hc)) str += '</a>';
  129. str += '</div>';
  130. if (node._hc) {
  131. str += '<div id="d' + this.obj + nodeId + '" class="clip" style="display:' + ((this.root.id == node.pid || node._io) ? 'block' : 'none') + ';">';
  132. str += this.addNode(node);
  133. str += '</div>';
  134. }
  135. this.aIndent.pop();
  136. return str;
  137. };
  138. // Adds the empty and line icons
  139. dTree.prototype.indent = function(node, nodeId) {
  140. var str = '';
  141. if (this.root.id != node.pid) {
  142. for (var n=0; n<this.aIndent.length; n++)
  143. str += '<img src="' + ( (this.aIndent[n] == 1 && this.config.useLines) ? this.icon.line : this.icon.empty ) + '" alt="" />';
  144. (node._ls) ? this.aIndent.push(0) : this.aIndent.push(1);
  145. if (node._hc) {
  146. str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');"><img id="j' + this.obj + nodeId + '" src="';
  147. if (!this.config.useLines) str += (node._io) ? this.icon.nlMinus : this.icon.nlPlus;
  148. 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 ) );
  149. str += '" alt="" /></a>';
  150. } else str += '<img src="' + ( (this.config.useLines) ? ((node._ls) ? this.icon.joinBottom : this.icon.join ) : this.icon.empty) + '" alt="" />';
  151. }
  152. return str;
  153. };
  154. // Checks if a node has any children and if it is the last sibling
  155. dTree.prototype.setCS = function(node) {
  156. var lastId;
  157. for (var n=0; n<this.aNodes.length; n++) {
  158. if (this.aNodes[n].pid == node.id) node._hc = true;
  159. if (this.aNodes[n].pid == node.pid) lastId = this.aNodes[n].id;
  160. }
  161. if (lastId==node.id) node._ls = true;
  162. };
  163. // Returns the selected node
  164. dTree.prototype.getSelected = function() {
  165. var sn = this.getCookie('cs' + this.obj);
  166. return (sn) ? sn : null;
  167. };
  168. // Highlights the selected node
  169. dTree.prototype.s = function(id) {
  170. if (!this.config.useSelection) return;
  171. var cn = this.aNodes[id];
  172. if (cn._hc && !this.config.folderLinks) return;
  173. if (this.selectedNode != id) {
  174. if (this.selectedNode || this.selectedNode==0) {
  175. eOld = document.getElementById("s" + this.obj + this.selectedNode);
  176. eOld.className = "node";
  177. }
  178. eNew = document.getElementById("s" + this.obj + id);
  179. eNew.className = "nodeSel";
  180. this.selectedNode = id;
  181. if (this.config.useCookies) this.setCookie('cs' + this.obj, cn.id);
  182. }
  183. };
  184. // Toggle Open or close
  185. dTree.prototype.o = function(id) {
  186. var cn = this.aNodes[id];
  187. this.nodeStatus(!cn._io, id, cn._ls);
  188. cn._io = !cn._io;
  189. if (this.config.closeSameLevel) this.closeLevel(cn);
  190. if (this.config.useCookies) this.updateCookie();
  191. };
  192. // Open or close all nodes
  193. dTree.prototype.oAll = function(status) {
  194. for (var n=0; n<this.aNodes.length; n++) {
  195. if (this.aNodes[n]._hc && this.aNodes[n].pid != this.root.id) {
  196. this.nodeStatus(status, n, this.aNodes[n]._ls)
  197. this.aNodes[n]._io = status;
  198. }
  199. }
  200. if (this.config.useCookies) this.updateCookie();
  201. };
  202. // Opens the tree to a specific node
  203. dTree.prototype.openTo = function(nId, bSelect, bFirst) {
  204. if (!bFirst) {
  205. for (var n=0; n<this.aNodes.length; n++) {
  206. if (this.aNodes[n].id == nId) {
  207. nId=n;
  208. break;
  209. }
  210. }
  211. }
  212. var cn=this.aNodes[nId];
  213. if (cn.pid==this.root.id || !cn._p) return;
  214. cn._io = true;
  215. cn._is = bSelect;
  216. if (this.completed && cn._hc) this.nodeStatus(true, cn._ai, cn._ls);
  217. if (this.completed && bSelect) this.s(cn._ai);
  218. else if (bSelect) this._sn=cn._ai;
  219. this.openTo(cn._p._ai, false, true);
  220. };
  221. // Closes all nodes on the same level as certain node
  222. dTree.prototype.closeLevel = function(node) {
  223. for (var n=0; n<this.aNodes.length; n++) {
  224. if (this.aNodes[n].pid == node.pid && this.aNodes[n].id != node.id && this.aNodes[n]._hc) {
  225. this.nodeStatus(false, n, this.aNodes[n]._ls);
  226. this.aNodes[n]._io = false;
  227. this.closeAllChildren(this.aNodes[n]);
  228. }
  229. }
  230. }
  231. // Closes all children of a node
  232. dTree.prototype.closeAllChildren = function(node) {
  233. for (var n=0; n<this.aNodes.length; n++) {
  234. if (this.aNodes[n].pid == node.id && this.aNodes[n]._hc) {
  235. if (this.aNodes[n]._io) this.nodeStatus(false, n, this.aNodes[n]._ls);
  236. this.aNodes[n]._io = false;
  237. this.closeAllChildren(this.aNodes[n]);
  238. }
  239. }
  240. }
  241. // Change the status of a node(open or closed)
  242. dTree.prototype.nodeStatus = function(status, id, bottom) {
  243. eDiv = document.getElementById('d' + this.obj + id);
  244. eJoin = document.getElementById('j' + this.obj + id);
  245. if (this.config.useIcons) {
  246. eIcon = document.getElementById('i' + this.obj + id);
  247. eIcon.src = (status) ? this.aNodes[id].iconOpen : this.aNodes[id].icon;
  248. }
  249. eJoin.src = (this.config.useLines)?
  250. ((status)?((bottom)?this.icon.minusBottom:this.icon.minus):((bottom)?this.icon.plusBottom:this.icon.plus)):
  251. ((status)?this.icon.nlMinus:this.icon.nlPlus);
  252. eDiv.style.display = (status) ? 'block': 'none';
  253. };
  254. // [Cookie] Clears a cookie
  255. dTree.prototype.clearCookie = function() {
  256. var now = new Date();
  257. var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
  258. this.setCookie('co'+this.obj, 'cookieValue', yesterday);
  259. this.setCookie('cs'+this.obj, 'cookieValue', yesterday);
  260. };
  261. // [Cookie] Sets value in a cookie
  262. dTree.prototype.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) {
  263. document.cookie =
  264. escape(cookieName) + '=' + escape(cookieValue)
  265. + (expires ? '; expires=' + expires.toGMTString() : '')
  266. + (path ? '; path=' + path : '')
  267. + (domain ? '; domain=' + domain : '')
  268. + (secure ? '; secure' : '');
  269. };
  270. // [Cookie] Gets a value from a cookie
  271. dTree.prototype.getCookie = function(cookieName) {
  272. var cookieValue = '';
  273. var posName = document.cookie.indexOf(escape(cookieName) + '=');
  274. if (posName != -1) {
  275. var posValue = posName + (escape(cookieName) + '=').length;
  276. var endPos = document.cookie.indexOf(';', posValue);
  277. if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
  278. else cookieValue = unescape(document.cookie.substring(posValue));
  279. }
  280. return (cookieValue);
  281. };
  282. // [Cookie] Returns ids of open nodes as a string
  283. dTree.prototype.updateCookie = function() {
  284. var str = '';
  285. for (var n=0; n<this.aNodes.length; n++) {
  286. if (this.aNodes[n]._io && this.aNodes[n].pid != this.root.id) {
  287. if (str) str += '.';
  288. str += this.aNodes[n].id;
  289. }
  290. }
  291. this.setCookie('co' + this.obj, str);
  292. };
  293. // [Cookie] Checks if a node id is in a cookie
  294. dTree.prototype.isOpen = function(id) {
  295. var aOpen = this.getCookie('co' + this.obj).split('.');
  296. for (var n=0; n<aOpen.length; n++)
  297. if (aOpen[n] == id) return true;
  298. return false;
  299. };
  300. // If Push and pop is not implemented by the browser
  301. if (!Array.prototype.push) {
  302. Array.prototype.push = function array_push() {
  303. for(var i=0;i<arguments.length;i++)
  304. this[this.length]=arguments[i];
  305. return this.length;
  306. }
  307. };
  308. if (!Array.prototype.pop) {
  309. Array.prototype.pop = function array_pop() {
  310. lastElement = this[this.length-1];
  311. this.length = Math.max(this.length-1,0);
  312. return lastElement;
  313. }
  314. };