dtree.js 13 KB

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