Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Tree
  2. // ---------------------------------
  3. function Tree(parent, width, do_animate) {
  4. this.width = width;
  5. this.nodes = {};
  6. this.allnodes = {};
  7. this.cur_node;
  8. this.is_root = 1;
  9. this.do_animate = do_animate;
  10. var me = this;
  11. this.exp_img = 'images/icons/plus.gif';
  12. this.col_img = 'images/icons/minus.gif';
  13. this.body = $a(parent, 'div');
  14. if(width)$w(this.body, width);
  15. this.addNode = function(parent, id, imagesrc, onclick, onexpand, opts, label) {
  16. var t = new TreeNode(me, parent, id, imagesrc, onclick, onexpand, opts, label);
  17. if(!parent) {
  18. me.nodes[id]=t; // add to roots
  19. } else {
  20. parent.nodes[id]=t; // add to the node
  21. }
  22. me.allnodes[id] = t;
  23. // note: this will only be for groups
  24. if(onexpand)
  25. t.create_expimage();
  26. t.expanded_once = 0;
  27. return t;
  28. }
  29. var me = this;
  30. this.collapseall = function() {
  31. for(n in me.allnodes) {
  32. me.allnodes[n].collapse();
  33. }
  34. }
  35. }
  36. function TreeNode(tree, parent, id, imagesrc, onclick, onexpand, opts, label) {
  37. var me = this;
  38. if(!parent) parent = tree;
  39. this.parent = parent;
  40. this.nodes = {};
  41. this.onclick = onclick;
  42. this.onexpand = onexpand;
  43. this.text = label ? label : id;
  44. this.tree = tree;
  45. if(opts)
  46. this.opts = opts;
  47. else
  48. this.opts = {
  49. show_exp_img:1
  50. ,show_icon:1
  51. ,label_style:{padding:'2px', cursor: 'pointer', fontSize:'11px'}
  52. ,onselect_style:{fontWeight: 'bold'}
  53. ,ondeselect_style:{fontWeight: 'normal'}
  54. } // only useful for 1st node in the tree
  55. var tc = 1;
  56. if(this.opts.show_exp_img) tc+=1;
  57. if(!this.parent.tab) {
  58. this.parent.tab = make_table(this.parent.body, 2, tc, '100%');
  59. $y(this.parent.tab,{tableLayout:'fixed',borderCollapse: 'collapse'});
  60. } else {
  61. this.parent.tab.append_row(); this.parent.tab.append_row();
  62. }
  63. var mytab = this.parent.tab;
  64. // expand / collapse
  65. if(this.opts.show_exp_img) {
  66. this.exp_cell=$td(mytab,mytab.rows.length-2, 0);
  67. $y(this.exp_cell, {cursor:'pointer', textAlign:'center', verticalAlign:'middle',width:'20px'});
  68. this.exp_cell.innerHTML = ' ';
  69. } else {
  70. // pass
  71. }
  72. this.create_expimage = function() {
  73. if(!me.opts.show_exp_img) return; // no expand image
  74. if(!me.expimage) {
  75. me.exp_cell.innerHTML='';
  76. me.expimage = $a(me.exp_cell, 'img');
  77. me.expimage.src = me.exp_img ? me.exp_img : me.tree.exp_img;
  78. me.expimage.onclick = me.toggle;
  79. }
  80. }
  81. // label
  82. this.label = $a($td(mytab, mytab.rows.length-2, tc-1), 'div');
  83. $y(this.label, this.opts.label_style);
  84. // image
  85. if(this.opts.show_icon) { // for second row, where children will come icon to be included
  86. var t2 = make_table($a(this.label,'div'), 1, 2, '100%', ['20px',null]);
  87. $y(t2,{borderCollapse:'collapse'});
  88. this.img_cell = $td(t2, 0, 0);
  89. $y(this.img_cell, {cursor:'pointer',verticalAlign:'middle',width:'20px'});
  90. if(!imagesrc) imagesrc = "images/icons/folder.gif";
  91. this.usrimg = $a(this.img_cell, 'img');
  92. this.usrimg.src = imagesrc;
  93. this.label = $td(t2, 0, 1);
  94. $y(this.label,{verticalAlign:'middle'});
  95. }
  96. this.loading_div = $a($td(mytab, mytab.rows.length-1, this.opts.show_exp_img ? 1 : 0), "div", "comment", {fontSize:'11px'});
  97. $dh(this.loading_div);
  98. this.loading_div.innerHTML = 'Loading...';
  99. this.body = $a($td(mytab, mytab.rows.length-1, this.opts.show_exp_img ? 1 : 0), "div", '', {overflow:'hidden', display:'none'});
  100. this.select = function() {
  101. me.show_selected();
  102. if(me.onclick)me.onclick(me);
  103. }
  104. this.show_selected = function() {
  105. if(me.tree.cur_node)me.tree.cur_node.deselect();
  106. if(me.opts.onselect_style) $y(me.label,me.opts.onselect_style)
  107. //me.label.style.fontWeight = 'bold';
  108. me.tree.cur_node = me;
  109. }
  110. this.deselect = function() {
  111. if(me.opts.ondeselect_style) $y(me.label,me.opts.ondeselect_style)
  112. //me.label.style.fontWeight = 'normal';
  113. me.tree.cur_node=null
  114. }
  115. this.expanded = 0;
  116. this.toggle = function() {
  117. if(me.expanded)
  118. me.collapse();
  119. else
  120. me.expand();
  121. }
  122. this.collapse = function() {
  123. me.expanded = 0;
  124. $(me.body).slideUp();
  125. me.expimage.src = me.exp_img ? me.exp_img : me.tree.exp_img;
  126. }
  127. this.expand = function() {
  128. if(me.onexpand && !me.expanded_once){
  129. me.onexpand(me);
  130. if(!me.tree.do_animate) me.show_expanded(); // else to be called from expand (for animation)
  131. } else {
  132. me.show_expanded();
  133. }
  134. me.expanded = 1;
  135. me.expanded_once = 1;
  136. me.expimage.src = me.col_img ? me.col_img : me.tree.col_img;
  137. }
  138. this.show_expanded = function() {
  139. if(me.tree.do_animate && (!keys(me.nodes).length)) return; // no children
  140. $(me.body).slideDown();
  141. }
  142. this.setlabel = function(l) {
  143. me.label.value = l;
  144. me.label.innerHTML = l;
  145. }
  146. this.setlabel(this.text);
  147. this.setcolor = function(c) {
  148. this.backColor = c;
  149. if(cur_node!=this)
  150. $bg(this.body,this.backColor);
  151. }
  152. this.label.onclick= function(e) { me.select(); }
  153. this.label.ondblclick = function(e) { me.select(); if(me.ondblclick)me.ondblclick(me); }
  154. this.clear_child_nodes = function() {
  155. if(this.tab){
  156. this.tab.parentNode.removeChild(this.tab);
  157. delete this.tab;
  158. }
  159. this.expanded_once = 0;
  160. }
  161. }