dtree.js 12 KB

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