dtree.js 12 KB

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