dtree.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 || false;
  22. this._is = false;
  23. this._ls = false;
  24. this._hc = false;
  25. this._ai = 0;
  26. this._p;
  27. };
  28. // Tree object// imagePath parameter added by SquirrelMail Team
  29. function dTree(objName, imagePath) {
  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 : imagePath+'/base.png',
  43. folder : imagePath+'/folder.png',
  44. folderOpen : imagePath+'/folderopen.png',
  45. node : imagePath+'/page.png',
  46. empty : imagePath+'/empty.png',
  47. line : imagePath+'/line.png',
  48. join : imagePath+'/join.png',
  49. joinBottom : imagePath+'/joinbottom.png',
  50. plus : imagePath+'/plus_mid.png',
  51. plusBottom : imagePath+'/plusbottom.png',
  52. minus : imagePath+'/minus_mid.png',
  53. minusBottom : imagePath+'/minusbottom.png',
  54. nlPlus : imagePath+'/nolines_plus.png',
  55. nlMinus : imagePath+'/nolines_minus.png'
  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; this.imagePath = imagePath;
  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. //FIXME: if node.name contains a hyperlink, either the plugin that put it there should be responsible for adding the </a> for the main folder link or the line below should be changed to detect inner links and close the main one before any; for now, it seems to work anyway although I think the resultant HTML is invalid (hyperlink within a hyperlink and extra, out of place closing </a> at the end)
  138. if (node.url || ((!this.config.folderLinks || !node.url) && node._hc)) str += '</a>';
  139. str += '</div>';
  140. if (node._hc) {
  141. str += '<div id="d' + this.obj + nodeId + '" class="clip" style="display:' + ((this.root.id == node.pid || node._io) ? 'block' : 'none') + ';">';
  142. str += this.addNode(node);
  143. str += '</div>';
  144. }
  145. this.aIndent.pop();
  146. return str;
  147. };
  148. // Adds the empty and line icons
  149. dTree.prototype.indent = function(node, nodeId) {
  150. var str = '';
  151. if (this.root.id != node.pid) {
  152. for (var n=0; n<this.aIndent.length; n++)
  153. str += '<img src="' + ( (this.aIndent[n] == 1 && this.config.useLines) ? this.icon.line : this.icon.empty ) + '" alt="" />';
  154. (node._ls) ? this.aIndent.push(0) : this.aIndent.push(1);
  155. if (node._hc) {
  156. str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');"><img id="j' + this.obj + nodeId + '" src="';
  157. if (!this.config.useLines) str += (node._io) ? this.icon.nlMinus : this.icon.nlPlus;
  158. 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 ) );
  159. str += '" alt="" /></a>';
  160. } else str += '<img src="' + ( (this.config.useLines) ? ((node._ls) ? this.icon.joinBottom : this.icon.join ) : this.icon.empty) + '" alt="" />';
  161. }
  162. return str;
  163. };
  164. // Checks if a node has any children and if it is the last sibling
  165. dTree.prototype.setCS = function(node) {
  166. var lastId;
  167. for (var n=0; n<this.aNodes.length; n++) {
  168. if (this.aNodes[n].pid == node.id) node._hc = true;
  169. if (this.aNodes[n].pid == node.pid) lastId = this.aNodes[n].id;
  170. }
  171. if (lastId==node.id) node._ls = true;
  172. };
  173. // Returns the selected node
  174. dTree.prototype.getSelected = function() {
  175. var sn = this.getCookie('cs' + this.obj);
  176. return (sn) ? sn : null;
  177. };
  178. // Highlights the selected node
  179. dTree.prototype.s = function(id) {
  180. if (!this.config.useSelection) return;
  181. var cn = this.aNodes[id];
  182. if (cn._hc && !this.config.folderLinks) return;
  183. if (this.selectedNode != id) {
  184. if (this.selectedNode || this.selectedNode==0) {
  185. eOld = document.getElementById("s" + this.obj + this.selectedNode);
  186. eOld.className = "node";
  187. }
  188. eNew = document.getElementById("s" + this.obj + id);
  189. eNew.className = "nodeSel";
  190. this.selectedNode = id;
  191. if (this.config.useCookies) this.setCookie('cs' + this.obj, cn.id);
  192. }
  193. };
  194. // Toggle Open or close
  195. dTree.prototype.o = function(id) {
  196. var cn = this.aNodes[id];
  197. this.nodeStatus(!cn._io, id, cn._ls);
  198. cn._io = !cn._io;
  199. if (this.config.closeSameLevel) this.closeLevel(cn);
  200. if (this.config.useCookies) this.updateCookie();
  201. };
  202. // Open or close all nodes
  203. dTree.prototype.oAll = function(status) {
  204. for (var n=0; n<this.aNodes.length; n++) {
  205. if (this.aNodes[n]._hc && this.aNodes[n].pid != this.root.id) {
  206. this.nodeStatus(status, n, this.aNodes[n]._ls)
  207. this.aNodes[n]._io = status;
  208. }
  209. }
  210. if (this.config.useCookies) this.updateCookie();
  211. };
  212. // Opens the tree to a specific node
  213. dTree.prototype.openTo = function(nId, bSelect, bFirst) {
  214. if (!bFirst) {
  215. for (var n=0; n<this.aNodes.length; n++) {
  216. if (this.aNodes[n].id == nId) {
  217. nId=n;
  218. break;
  219. }
  220. }
  221. }
  222. var cn=this.aNodes[nId];
  223. if (cn.pid==this.root.id || !cn._p) return;
  224. cn._io = true;
  225. cn._is = bSelect;
  226. if (this.completed && cn._hc) this.nodeStatus(true, cn._ai, cn._ls);
  227. if (this.completed && bSelect) this.s(cn._ai);
  228. else if (bSelect) this._sn=cn._ai;
  229. this.openTo(cn._p._ai, false, true);
  230. };
  231. // Closes all nodes on the same level as certain node
  232. dTree.prototype.closeLevel = function(node) {
  233. for (var n=0; n<this.aNodes.length; n++) {
  234. if (this.aNodes[n].pid == node.pid && this.aNodes[n].id != node.id && this.aNodes[n]._hc) {
  235. this.nodeStatus(false, n, this.aNodes[n]._ls);
  236. this.aNodes[n]._io = false;
  237. this.closeAllChildren(this.aNodes[n]);
  238. }
  239. }
  240. }
  241. // Closes all children of a node
  242. dTree.prototype.closeAllChildren = function(node) {
  243. for (var n=0; n<this.aNodes.length; n++) {
  244. if (this.aNodes[n].pid == node.id && this.aNodes[n]._hc) {
  245. if (this.aNodes[n]._io) this.nodeStatus(false, n, this.aNodes[n]._ls);
  246. this.aNodes[n]._io = false;
  247. this.closeAllChildren(this.aNodes[n]);
  248. }
  249. }
  250. }
  251. // Change the status of a node(open or closed)
  252. dTree.prototype.nodeStatus = function(status, id, bottom) {
  253. eDiv = document.getElementById('d' + this.obj + id);
  254. eJoin = document.getElementById('j' + this.obj + id);
  255. if (this.config.useIcons) {
  256. eIcon = document.getElementById('i' + this.obj + id);
  257. eIcon.src = (status) ? this.aNodes[id].iconOpen : this.aNodes[id].icon;
  258. }
  259. eJoin.src = (this.config.useLines)?
  260. ((status)?((bottom)?this.icon.minusBottom:this.icon.minus):((bottom)?this.icon.plusBottom:this.icon.plus)):
  261. ((status)?this.icon.nlMinus:this.icon.nlPlus);
  262. eDiv.style.display = (status) ? 'block': 'none';
  263. };
  264. // [Cookie] Clears a cookie
  265. dTree.prototype.clearCookie = function() {
  266. var now = new Date();
  267. var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
  268. this.setCookie('co'+this.obj, 'cookieValue', yesterday);
  269. this.setCookie('cs'+this.obj, 'cookieValue', yesterday);
  270. };
  271. // [Cookie] Sets value in a cookie
  272. // FIXME: although setCookie() supports the secure flag, it isn't used when called anywhere in this file; ideally it should correspond to how the flag is determined in the SM core in sqsetcookie() (including the admin config that turns the secure flag off)... also, would be good to add the HTTP only flag
  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. };