xtree.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*----------------------------------------------------------------------------\
  2. | Cross Browser Tree Widget 1.17 |
  3. |-----------------------------------------------------------------------------|
  4. | Created by Emil A Eklund |
  5. | (http://webfx.eae.net/contact.html#emil) |
  6. | For WebFX (http://webfx.eae.net/) |
  7. |-----------------------------------------------------------------------------|
  8. | An object based tree widget, emulating the one found in microsoft windows, |
  9. | with persistence using cookies. Works in IE 5+, Mozilla and konqueror 3. |
  10. |-----------------------------------------------------------------------------|
  11. | Copyright (c) 1999 - 2002 Emil A Eklund |
  12. |-----------------------------------------------------------------------------|
  13. | This software is provided "as is", without warranty of any kind, express or |
  14. | implied, including but not limited to the warranties of merchantability, |
  15. | fitness for a particular purpose and noninfringement. In no event shall the |
  16. | authors or copyright holders be liable for any claim, damages or other |
  17. | liability, whether in an action of contract, tort or otherwise, arising |
  18. | from, out of or in connection with the software or the use or other |
  19. | dealings in the software. |
  20. | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
  21. | This software is available under the three different licenses mentioned |
  22. | below. To use this software you must chose, and qualify, for one of those. |
  23. | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
  24. | The WebFX Non-Commercial License http://webfx.eae.net/license.html |
  25. | Permits anyone the right to use the software in a non-commercial context |
  26. | free of charge. |
  27. | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
  28. | The WebFX Commercial license http://webfx.eae.net/commercial.html |
  29. | Permits the license holder the right to use the software in a commercial |
  30. | context. Such license must be specifically obtained, however it's valid for |
  31. | any number of implementations of the licensed software. |
  32. | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
  33. | GPL - The GNU General Public License http://www.gnu.org/licenses/gpl.txt |
  34. | Permits anyone the right to use and modify the software without limitations |
  35. | as long as proper credits are given and the original and modified source |
  36. | code are included. Requires that the final product, software derivate from |
  37. | the original source or any software utilizing a GPL component, such as |
  38. | this, is also licensed under the GPL license. |
  39. |-----------------------------------------------------------------------------|
  40. | Dependencies: xtree.css (To set up the CSS of the tree classes) |
  41. |-----------------------------------------------------------------------------|
  42. | 2001-01-10 | Original Version Posted. |
  43. | 2001-03-18 | Added getSelected and get/setBehavior that can make it behave |
  44. | | more like windows explorer, check usage for more information. |
  45. | 2001-09-23 | Version 1.1 - New features included keyboard navigation (ie) |
  46. | | and the ability to add and remove nodes dynamically and some |
  47. | | other small tweaks and fixes. |
  48. | 2002-01-27 | Version 1.11 - Bug fixes and improved mozilla support. |
  49. | 2002-06-11 | Version 1.12 - Fixed a bug that prevented the indentation line |
  50. | | from updating correctly under some circumstances. This bug |
  51. | | happened when removing the last item in a subtree and items in |
  52. | | siblings to the remove subtree where not correctly updated. |
  53. | 2002-06-13 | Fixed a few minor bugs cased by the 1.12 bug-fix. |
  54. | 2002-08-20 | Added usePersistence flag to allow disable of cookies. |
  55. | 2002-10-23 | (1.14) Fixed a plus icon issue |
  56. | 2002-10-29 | (1.15) Last changes broke more than they fixed. This version |
  57. | | is based on 1.13 and fixes the bugs 1.14 fixed withou breaking |
  58. | | lots of other things. |
  59. | 2003-02-15 | The selected node can now be made visible even when the tree |
  60. | | control loses focus. It uses a new class declaration in the |
  61. | | css file '.webfx-tree-item a.selected-inactive', by default it |
  62. | | puts a light-gray rectangle around the selected node. |
  63. | 2003-03-16 | Adding target support after lots of lobbying... |
  64. |-----------------------------------------------------------------------------|
  65. | Created 2000-12-11 | All changes are in the log above. | Updated 2003-03-16 |
  66. \----------------------------------------------------------------------------*/
  67. var webFXTreeConfig = {
  68. rootIcon : 'images/foldericon.png',
  69. openRootIcon : 'images/openfoldericon.png',
  70. folderIcon : 'images/foldericon.png',
  71. openFolderIcon : 'images/openfoldericon.png',
  72. fileIcon : 'images/file.png',
  73. iIcon : 'images/I.png',
  74. lIcon : 'images/L.png',
  75. lMinusIcon : 'images/Lminus.png',
  76. lPlusIcon : 'images/Lplus.png',
  77. tIcon : 'images/T.png',
  78. tMinusIcon : 'images/Tminus.png',
  79. tPlusIcon : 'images/Tplus.png',
  80. blankIcon : 'images/blank.png',
  81. defaultText : 'Tree Item',
  82. defaultAction : 'javascript:void(0);',
  83. defaultBehavior : 'classic',
  84. usePersistence : true
  85. };
  86. var webFXTreeHandler = {
  87. idCounter : 0,
  88. idPrefix : "webfx-tree-object-",
  89. all : {},
  90. behavior : null,
  91. selected : null,
  92. onSelect : null, /* should be part of tree, not handler */
  93. getId : function() { return this.idPrefix + this.idCounter++; },
  94. toggle : function (oItem) { this.all[oItem.id.replace('-plus','')].toggle(); },
  95. select : function (oItem) { this.all[oItem.id.replace('-icon','')].select(); },
  96. focus : function (oItem) { this.all[oItem.id.replace('-anchor','')].focus(); },
  97. blur : function (oItem) { this.all[oItem.id.replace('-anchor','')].blur(); },
  98. keydown : function (oItem, e) { return this.all[oItem.id].keydown(e.keyCode); },
  99. cookies : new WebFXCookie(),
  100. insertHTMLBeforeEnd : function (oElement, sHTML) {
  101. if (oElement.insertAdjacentHTML != null) {
  102. oElement.insertAdjacentHTML("BeforeEnd", sHTML)
  103. return;
  104. }
  105. var df; // DocumentFragment
  106. var r = oElement.ownerDocument.createRange();
  107. r.selectNodeContents(oElement);
  108. r.collapse(false);
  109. df = r.createContextualFragment(sHTML);
  110. oElement.appendChild(df);
  111. }
  112. };
  113. /*
  114. * WebFXCookie class
  115. */
  116. function WebFXCookie() {
  117. if (document.cookie.length) { this.cookies = ' ' + document.cookie; }
  118. }
  119. WebFXCookie.prototype.setCookie = function (key, value) {
  120. document.cookie = key + "=" + escape(value);
  121. }
  122. WebFXCookie.prototype.getCookie = function (key) {
  123. if (this.cookies) {
  124. var start = this.cookies.indexOf(' ' + key + '=');
  125. if (start == -1) { return null; }
  126. var end = this.cookies.indexOf(";", start);
  127. if (end == -1) { end = this.cookies.length; }
  128. end -= start;
  129. var cookie = this.cookies.substr(start,end);
  130. return unescape(cookie.substr(cookie.indexOf('=') + 1, cookie.length - cookie.indexOf('=') + 1));
  131. }
  132. else { return null; }
  133. }
  134. /*
  135. * WebFXTreeAbstractNode class
  136. */
  137. function WebFXTreeAbstractNode(sText, sAction) {
  138. this.childNodes = [];
  139. this.id = webFXTreeHandler.getId();
  140. this.text = sText || webFXTreeConfig.defaultText;
  141. this.action = sAction || webFXTreeConfig.defaultAction;
  142. this._last = false;
  143. webFXTreeHandler.all[this.id] = this;
  144. }
  145. /*
  146. * To speed thing up if you're adding multiple nodes at once (after load)
  147. * use the bNoIdent parameter to prevent automatic re-indentation and call
  148. * the obj.ident() method manually once all nodes has been added.
  149. */
  150. WebFXTreeAbstractNode.prototype.add = function (node, bNoIdent) {
  151. node.parentNode = this;
  152. this.childNodes[this.childNodes.length] = node;
  153. var root = this;
  154. if (this.childNodes.length >= 2) {
  155. this.childNodes[this.childNodes.length - 2]._last = false;
  156. }
  157. while (root.parentNode) { root = root.parentNode; }
  158. if (root.rendered) {
  159. if (this.childNodes.length >= 2) {
  160. document.getElementById(this.childNodes[this.childNodes.length - 2].id + '-plus').src = ((this.childNodes[this.childNodes.length -2].folder)?((this.childNodes[this.childNodes.length -2].open)?webFXTreeConfig.tMinusIcon:webFXTreeConfig.tPlusIcon):webFXTreeConfig.tIcon);
  161. this.childNodes[this.childNodes.length - 2].plusIcon = webFXTreeConfig.tPlusIcon;
  162. this.childNodes[this.childNodes.length - 2].minusIcon = webFXTreeConfig.tMinusIcon;
  163. this.childNodes[this.childNodes.length - 2]._last = false;
  164. }
  165. this._last = true;
  166. var foo = this;
  167. while (foo.parentNode) {
  168. for (var i = 0; i < foo.parentNode.childNodes.length; i++) {
  169. if (foo.id == foo.parentNode.childNodes[i].id) { break; }
  170. }
  171. if (i == foo.parentNode.childNodes.length - 1) { foo.parentNode._last = true; }
  172. else { foo.parentNode._last = false; }
  173. foo = foo.parentNode;
  174. }
  175. webFXTreeHandler.insertHTMLBeforeEnd(document.getElementById(this.id + '-cont'), node.toString());
  176. if ((!this.folder) && (!this.openIcon)) {
  177. this.icon = webFXTreeConfig.folderIcon;
  178. this.openIcon = webFXTreeConfig.openFolderIcon;
  179. }
  180. if (!this.folder) { this.folder = true; this.collapse(true); }
  181. if (!bNoIdent) { this.indent(); }
  182. }
  183. return node;
  184. }
  185. WebFXTreeAbstractNode.prototype.toggle = function() {
  186. if (this.folder) {
  187. if (this.open) { this.collapse(); }
  188. else { this.expand(); }
  189. } }
  190. WebFXTreeAbstractNode.prototype.select = function() {
  191. document.getElementById(this.id + '-anchor').focus();
  192. }
  193. WebFXTreeAbstractNode.prototype.deSelect = function() {
  194. document.getElementById(this.id + '-anchor').className = '';
  195. webFXTreeHandler.selected = null;
  196. }
  197. WebFXTreeAbstractNode.prototype.focus = function() {
  198. if ((webFXTreeHandler.selected) && (webFXTreeHandler.selected != this)) { webFXTreeHandler.selected.deSelect(); }
  199. webFXTreeHandler.selected = this;
  200. if ((this.openIcon) && (webFXTreeHandler.behavior != 'classic')) { document.getElementById(this.id + '-icon').src = this.openIcon; }
  201. document.getElementById(this.id + '-anchor').className = 'selected';
  202. document.getElementById(this.id + '-anchor').focus();
  203. if (webFXTreeHandler.onSelect) { webFXTreeHandler.onSelect(this); }
  204. }
  205. WebFXTreeAbstractNode.prototype.blur = function() {
  206. if ((this.openIcon) && (webFXTreeHandler.behavior != 'classic')) { document.getElementById(this.id + '-icon').src = this.icon; }
  207. document.getElementById(this.id + '-anchor').className = 'selected-inactive';
  208. }
  209. WebFXTreeAbstractNode.prototype.doExpand = function() {
  210. if (webFXTreeHandler.behavior == 'classic') { document.getElementById(this.id + '-icon').src = this.openIcon; }
  211. if (this.childNodes.length) { document.getElementById(this.id + '-cont').style.display = 'block'; }
  212. this.open = true;
  213. if (webFXTreeConfig.usePersistence) {
  214. webFXTreeHandler.cookies.setCookie(this.id.substr(18,this.id.length - 18), '1');
  215. } }
  216. WebFXTreeAbstractNode.prototype.doCollapse = function() {
  217. if (webFXTreeHandler.behavior == 'classic') { document.getElementById(this.id + '-icon').src = this.icon; }
  218. if (this.childNodes.length) { document.getElementById(this.id + '-cont').style.display = 'none'; }
  219. this.open = false;
  220. if (webFXTreeConfig.usePersistence) {
  221. webFXTreeHandler.cookies.setCookie(this.id.substr(18,this.id.length - 18), '0');
  222. } }
  223. WebFXTreeAbstractNode.prototype.expandAll = function() {
  224. this.expandChildren();
  225. if ((this.folder) && (!this.open)) { this.expand(); }
  226. }
  227. WebFXTreeAbstractNode.prototype.expandChildren = function() {
  228. for (var i = 0; i < this.childNodes.length; i++) {
  229. this.childNodes[i].expandAll();
  230. } }
  231. WebFXTreeAbstractNode.prototype.collapseAll = function() {
  232. this.collapseChildren();
  233. if ((this.folder) && (this.open)) { this.collapse(true); }
  234. }
  235. WebFXTreeAbstractNode.prototype.collapseChildren = function() {
  236. for (var i = 0; i < this.childNodes.length; i++) {
  237. this.childNodes[i].collapseAll();
  238. } }
  239. WebFXTreeAbstractNode.prototype.indent = function(lvl, del, last, level, nodesLeft) {
  240. /*
  241. * Since we only want to modify items one level below ourself,
  242. * and since the rightmost indentation position is occupied by
  243. * the plus icon we set this to -2
  244. */
  245. if (lvl == null) { lvl = -2; }
  246. var state = 0;
  247. for (var i = this.childNodes.length - 1; i >= 0 ; i--) {
  248. state = this.childNodes[i].indent(lvl + 1, del, last, level);
  249. if (state) { return; }
  250. }
  251. if (del) {
  252. if ((level >= this._level) && (document.getElementById(this.id + '-plus'))) {
  253. if (this.folder) {
  254. document.getElementById(this.id + '-plus').src = (this.open)?webFXTreeConfig.lMinusIcon:webFXTreeConfig.lPlusIcon;
  255. this.plusIcon = webFXTreeConfig.lPlusIcon;
  256. this.minusIcon = webFXTreeConfig.lMinusIcon;
  257. }
  258. else if (nodesLeft) { document.getElementById(this.id + '-plus').src = webFXTreeConfig.lIcon; }
  259. return 1;
  260. } }
  261. var foo = document.getElementById(this.id + '-indent-' + lvl);
  262. if (foo) {
  263. if ((foo._last) || ((del) && (last))) { foo.src = webFXTreeConfig.blankIcon; }
  264. else { foo.src = webFXTreeConfig.iIcon; }
  265. }
  266. return 0;
  267. }
  268. /*
  269. * WebFXTree class
  270. */
  271. function WebFXTree(sText, sAction, sBehavior, sIcon, sOpenIcon) {
  272. this.base = WebFXTreeAbstractNode;
  273. this.base(sText, sAction);
  274. this.icon = sIcon || webFXTreeConfig.rootIcon;
  275. this.openIcon = sOpenIcon || webFXTreeConfig.openRootIcon;
  276. /* Defaults to open */
  277. if (webFXTreeConfig.usePersistence) {
  278. this.open = (webFXTreeHandler.cookies.getCookie(this.id.substr(18,this.id.length - 18)) == '0')?false:true;
  279. } else { this.open = true; }
  280. this.folder = true;
  281. this.rendered = false;
  282. this.onSelect = null;
  283. if (!webFXTreeHandler.behavior) { webFXTreeHandler.behavior = sBehavior || webFXTreeConfig.defaultBehavior; }
  284. }
  285. WebFXTree.prototype = new WebFXTreeAbstractNode;
  286. WebFXTree.prototype.setBehavior = function (sBehavior) {
  287. webFXTreeHandler.behavior = sBehavior;
  288. };
  289. WebFXTree.prototype.getBehavior = function (sBehavior) {
  290. return webFXTreeHandler.behavior;
  291. };
  292. WebFXTree.prototype.getSelected = function() {
  293. if (webFXTreeHandler.selected) { return webFXTreeHandler.selected; }
  294. else { return null; }
  295. }
  296. WebFXTree.prototype.remove = function() { }
  297. WebFXTree.prototype.expand = function() {
  298. this.doExpand();
  299. }
  300. WebFXTree.prototype.collapse = function(b) {
  301. if (!b) { this.focus(); }
  302. this.doCollapse();
  303. }
  304. WebFXTree.prototype.getFirst = function() {
  305. return null;
  306. }
  307. WebFXTree.prototype.getLast = function() {
  308. return null;
  309. }
  310. WebFXTree.prototype.getNextSibling = function() {
  311. return null;
  312. }
  313. WebFXTree.prototype.getPreviousSibling = function() {
  314. return null;
  315. }
  316. WebFXTree.prototype.keydown = function(key) {
  317. if (key == 39) {
  318. if (!this.open) { this.expand(); }
  319. else if (this.childNodes.length) { this.childNodes[0].select(); }
  320. return false;
  321. }
  322. if (key == 37) { this.collapse(); return false; }
  323. if ((key == 40) && (this.open) && (this.childNodes.length)) { this.childNodes[0].select(); return false; }
  324. return true;
  325. }
  326. WebFXTree.prototype.toString = function() {
  327. var str = "<div id=\"" + this.id + "\" ondblclick=\"webFXTreeHandler.toggle(this);\" class=\"webfx-tree-item\" onkeydown=\"return webFXTreeHandler.keydown(this, event)\">" +
  328. "<img id=\"" + this.id + "-icon\" class=\"webfx-tree-icon\" src=\"" + ((webFXTreeHandler.behavior == 'classic' && this.open)?this.openIcon:this.icon) + "\" onclick=\"webFXTreeHandler.select(this);\">" +
  329. "<a href=\"" + this.action + "\" id=\"" + this.id + "-anchor\" onfocus=\"webFXTreeHandler.focus(this);\" onblur=\"webFXTreeHandler.blur(this);\"" +
  330. (this.target ? " target=\"" + this.target + "\"" : "") +
  331. ">" + this.text + "</a></div>" +
  332. "<div id=\"" + this.id + "-cont\" class=\"webfx-tree-container\" style=\"display: " + ((this.open)?'block':'none') + ";\">";
  333. var sb = [];
  334. for (var i = 0; i < this.childNodes.length; i++) {
  335. sb[i] = this.childNodes[i].toString(i, this.childNodes.length);
  336. }
  337. this.rendered = true;
  338. return str + sb.join("") + "</div>";
  339. };
  340. /*
  341. * WebFXTreeItem class
  342. */
  343. function WebFXTreeItem(sText, sAction, eParent, sIcon, sOpenIcon) {
  344. this.base = WebFXTreeAbstractNode;
  345. this.base(sText, sAction);
  346. /* Defaults to close */
  347. if (webFXTreeConfig.usePersistence) {
  348. this.open = (webFXTreeHandler.cookies.getCookie(this.id.substr(18,this.id.length - 18)) == '1')?true:false;
  349. } else { this.open = false; }
  350. if (sIcon) { this.icon = sIcon; }
  351. if (sOpenIcon) { this.openIcon = sOpenIcon; }
  352. if (eParent) { eParent.add(this); }
  353. }
  354. WebFXTreeItem.prototype = new WebFXTreeAbstractNode;
  355. WebFXTreeItem.prototype.remove = function() {
  356. var iconSrc = document.getElementById(this.id + '-plus').src;
  357. var parentNode = this.parentNode;
  358. var prevSibling = this.getPreviousSibling(true);
  359. var nextSibling = this.getNextSibling(true);
  360. var folder = this.parentNode.folder;
  361. var last = ((nextSibling) && (nextSibling.parentNode) && (nextSibling.parentNode.id == parentNode.id))?false:true;
  362. this.getPreviousSibling().focus();
  363. this._remove();
  364. if (parentNode.childNodes.length == 0) {
  365. document.getElementById(parentNode.id + '-cont').style.display = 'none';
  366. parentNode.doCollapse();
  367. parentNode.folder = false;
  368. parentNode.open = false;
  369. }
  370. if (!nextSibling || last) { parentNode.indent(null, true, last, this._level, parentNode.childNodes.length); }
  371. if ((prevSibling == parentNode) && !(parentNode.childNodes.length)) {
  372. prevSibling.folder = false;
  373. prevSibling.open = false;
  374. iconSrc = document.getElementById(prevSibling.id + '-plus').src;
  375. iconSrc = iconSrc.replace('minus', '').replace('plus', '');
  376. document.getElementById(prevSibling.id + '-plus').src = iconSrc;
  377. document.getElementById(prevSibling.id + '-icon').src = webFXTreeConfig.fileIcon;
  378. }
  379. if (document.getElementById(prevSibling.id + '-plus')) {
  380. if (parentNode == prevSibling.parentNode) {
  381. iconSrc = iconSrc.replace('minus', '').replace('plus', '');
  382. document.getElementById(prevSibling.id + '-plus').src = iconSrc;
  383. } } }
  384. WebFXTreeItem.prototype._remove = function() {
  385. for (var i = this.childNodes.length - 1; i >= 0; i--) {
  386. this.childNodes[i]._remove();
  387. }
  388. for (var i = 0; i < this.parentNode.childNodes.length; i++) {
  389. if (this == this.parentNode.childNodes[i]) {
  390. for (var j = i; j < this.parentNode.childNodes.length; j++) {
  391. this.parentNode.childNodes[j] = this.parentNode.childNodes[j+1];
  392. }
  393. this.parentNode.childNodes.length -= 1;
  394. if (i + 1 == this.parentNode.childNodes.length) { this.parentNode._last = true; }
  395. break;
  396. } }
  397. webFXTreeHandler.all[this.id] = null;
  398. var tmp = document.getElementById(this.id);
  399. if (tmp) { tmp.parentNode.removeChild(tmp); }
  400. tmp = document.getElementById(this.id + '-cont');
  401. if (tmp) { tmp.parentNode.removeChild(tmp); }
  402. }
  403. WebFXTreeItem.prototype.expand = function() {
  404. this.doExpand();
  405. document.getElementById(this.id + '-plus').src = this.minusIcon;
  406. }
  407. WebFXTreeItem.prototype.collapse = function(b) {
  408. if (!b) { this.focus(); }
  409. this.doCollapse();
  410. document.getElementById(this.id + '-plus').src = this.plusIcon;
  411. }
  412. WebFXTreeItem.prototype.getFirst = function() {
  413. return this.childNodes[0];
  414. }
  415. WebFXTreeItem.prototype.getLast = function() {
  416. if (this.childNodes[this.childNodes.length - 1].open) { return this.childNodes[this.childNodes.length - 1].getLast(); }
  417. else { return this.childNodes[this.childNodes.length - 1]; }
  418. }
  419. WebFXTreeItem.prototype.getNextSibling = function() {
  420. for (var i = 0; i < this.parentNode.childNodes.length; i++) {
  421. if (this == this.parentNode.childNodes[i]) { break; }
  422. }
  423. if (++i == this.parentNode.childNodes.length) { return this.parentNode.getNextSibling(); }
  424. else { return this.parentNode.childNodes[i]; }
  425. }
  426. WebFXTreeItem.prototype.getPreviousSibling = function(b) {
  427. for (var i = 0; i < this.parentNode.childNodes.length; i++) {
  428. if (this == this.parentNode.childNodes[i]) { break; }
  429. }
  430. if (i == 0) { return this.parentNode; }
  431. else {
  432. if ((this.parentNode.childNodes[--i].open) || (b && this.parentNode.childNodes[i].folder)) { return this.parentNode.childNodes[i].getLast(); }
  433. else { return this.parentNode.childNodes[i]; }
  434. } }
  435. WebFXTreeItem.prototype.keydown = function(key) {
  436. if ((key == 39) && (this.folder)) {
  437. if (!this.open) { this.expand(); }
  438. else { this.getFirst().select(); }
  439. return false;
  440. }
  441. else if (key == 37) {
  442. if (this.open) { this.collapse(); }
  443. else { this.parentNode.select(); }
  444. return false;
  445. }
  446. else if (key == 40) {
  447. if (this.open) { this.getFirst().select(); }
  448. else {
  449. var sib = this.getNextSibling();
  450. if (sib) { sib.select(); }
  451. }
  452. return false;
  453. }
  454. else if (key == 38) { this.getPreviousSibling().select(); return false; }
  455. return true;
  456. }
  457. WebFXTreeItem.prototype.toString = function (nItem, nItemCount) {
  458. var foo = this.parentNode;
  459. var indent = '';
  460. if (nItem + 1 == nItemCount) { this.parentNode._last = true; }
  461. var i = 0;
  462. while (foo.parentNode) {
  463. foo = foo.parentNode;
  464. indent = "<img id=\"" + this.id + "-indent-" + i + "\" src=\"" + ((foo._last)?webFXTreeConfig.blankIcon:webFXTreeConfig.iIcon) + "\">" + indent;
  465. i++;
  466. }
  467. this._level = i;
  468. if (this.childNodes.length) { this.folder = 1; }
  469. else { this.open = false; }
  470. if ((this.folder) || (webFXTreeHandler.behavior != 'classic')) {
  471. if (!this.icon) { this.icon = webFXTreeConfig.folderIcon; }
  472. if (!this.openIcon) { this.openIcon = webFXTreeConfig.openFolderIcon; }
  473. }
  474. else if (!this.icon) { this.icon = webFXTreeConfig.fileIcon; }
  475. var label = this.text.replace(/</g, '&lt;').replace(/>/g, '&gt;');
  476. var str = "<div id=\"" + this.id + "\" ondblclick=\"webFXTreeHandler.toggle(this);\" class=\"webfx-tree-item\" onkeydown=\"return webFXTreeHandler.keydown(this, event)\">" +
  477. indent +
  478. "<img id=\"" + this.id + "-plus\" src=\"" + ((this.folder)?((this.open)?((this.parentNode._last)?webFXTreeConfig.lMinusIcon:webFXTreeConfig.tMinusIcon):((this.parentNode._last)?webFXTreeConfig.lPlusIcon:webFXTreeConfig.tPlusIcon)):((this.parentNode._last)?webFXTreeConfig.lIcon:webFXTreeConfig.tIcon)) + "\" onclick=\"webFXTreeHandler.toggle(this);\">" +
  479. "<img id=\"" + this.id + "-icon\" class=\"webfx-tree-icon\" src=\"" + ((webFXTreeHandler.behavior == 'classic' && this.open)?this.openIcon:this.icon) + "\" onclick=\"webFXTreeHandler.select(this);\">" +
  480. "<a href=\"" + this.action + "\" id=\"" + this.id + "-anchor\" onfocus=\"webFXTreeHandler.focus(this);\" onblur=\"webFXTreeHandler.blur(this);\"" +
  481. (this.target ? " target=\"" + this.target + "\"" : "") +
  482. ">" + label + "</a></div>" +
  483. "<div id=\"" + this.id + "-cont\" class=\"webfx-tree-container\" style=\"display: " + ((this.open)?'block':'none') + ";\">";
  484. var sb = [];
  485. for (var i = 0; i < this.childNodes.length; i++) {
  486. sb[i] = this.childNodes[i].toString(i,this.childNodes.length);
  487. }
  488. this.plusIcon = ((this.parentNode._last)?webFXTreeConfig.lPlusIcon:webFXTreeConfig.tPlusIcon);
  489. this.minusIcon = ((this.parentNode._last)?webFXTreeConfig.lMinusIcon:webFXTreeConfig.tMinusIcon);
  490. return str + sb.join("") + "</div>";
  491. }