Tree2.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. o2.widget = o2.widget || {};
  2. o2.require("o2.widget.Tree", null, false);
  3. o2.widget.Tree2 = new Class({
  4. Extends: o2.widget.Tree,
  5. });
  6. o2.widget.Tree2.Node = new Class({
  7. Implements: [Options, Events],
  8. options: {
  9. "expand": true,
  10. "title": "",
  11. "text": "",
  12. "action": "",
  13. "icon": "folder.png"
  14. },
  15. imgs: {
  16. "expand": "expand.gif",
  17. "collapse":"collapse.gif",
  18. "blank": "blank.gif"
  19. },
  20. tree: null,
  21. level: 0,
  22. levelNode:[],
  23. initialize: function(tree, options){
  24. this.setOptions(options);
  25. if (options.icon=="none") this.options.icon = "";
  26. this.tree = tree;
  27. this.levelNode = [];
  28. this.children = [];
  29. this.parentNode = null;
  30. this.previousSibling = null;
  31. this.nextSibling = null;
  32. this.firstChild = null;
  33. this.node = new Element("div",{
  34. "styles": this.tree.css.treeNode
  35. });
  36. this.itemNode = new Element("div", {
  37. "styles": this.tree.css.treeItemNode
  38. }).inject(this.node);
  39. this.childrenNode = new Element("div", {
  40. "styles": this.tree.css.treeChildrenNode
  41. }).inject(this.node);
  42. if (!this.options.expand){
  43. this.childrenNode.setStyle("display", "none");
  44. }
  45. },
  46. setText: function(value){
  47. var textDivNode = this.textNode.getElement("div");
  48. if (textDivNode) textDivNode.set("text", value);
  49. },
  50. setTitle: function(value){
  51. var textDivNode = this.textNode.getElement("div");
  52. if (textDivNode) textDivNode.set("title", value);
  53. },
  54. load: function(){
  55. this.nodeTable = new Element("table", {
  56. "border": "0",
  57. "cellpadding": "0",
  58. "cellspacing": "0",
  59. "styles": this.tree.css.nodeTable
  60. }).inject(this.itemNode);
  61. var tbody = new Element("tbody").inject(this.nodeTable);
  62. this.nodeArea = new Element("tr").inject(tbody);
  63. this.createLevelNode();
  64. this.createOperateNode();
  65. this.createIconNode();
  66. this.createTextNode();
  67. },
  68. createLevelNode: function(){
  69. for (var i=0; i<this.level; i++){
  70. var td = new Element("td",{
  71. "styles": this.tree.css.blankLevelNode
  72. }).inject(this.nodeArea);
  73. // var img = new Element("img", {
  74. // "src": o2.tree.path+this.tree.options.style+"/"+this.imgs.blank,
  75. // "width": td.getStyle("width"),
  76. // "height": td.getStyle("height"),
  77. // "border": "0"
  78. // }).inject(td);
  79. this.levelNode.push(td);
  80. }
  81. },
  82. createOperateNode: function(){
  83. this.operateNode = new Element("td",{
  84. "styles": this.tree.css.operateNode
  85. }).inject(this.nodeArea);
  86. this.operateNode.addEvent("click", function(){
  87. this.expandOrCollapse();
  88. }.bind(this));
  89. var img = new Element("img", {
  90. "src": this.tree.path+this.tree.options.style+"/"+this.imgs.blank,
  91. "width": this.operateNode.getStyle("width"),
  92. "height": this.operateNode.getStyle("height"),
  93. "border": "0",
  94. "styles": {
  95. //"margin-top": "6px"
  96. }
  97. }).inject(this.operateNode);
  98. },
  99. createIconNode: function(){
  100. if (this.options.icon){
  101. this.iconNode = new Element("td",{
  102. "styles": this.tree.css.iconNode
  103. }).inject(this.nodeArea);
  104. this.iconNode.setStyle("background", "url("+this.tree.path+this.tree.options.style+"/"+this.options.icon+") center center no-repeat");
  105. //
  106. //var img = new Element("img",{
  107. // "src": this.tree.path+this.tree.options.style+"/"+this.options.icon
  108. //});
  109. //img.inject(this.iconNode);
  110. }
  111. },
  112. createTextNode: function(){
  113. this.textNode = new Element("td",{
  114. "styles": this.tree.css.textNode
  115. }).inject(this.nodeArea);
  116. // var width = this.tree.container.getSize().x - (this.level*20+40);
  117. // this.textNode.setStyle("width", ""+width+"px");
  118. var textDivNode = new Element("div", {
  119. "styles": this.tree.css.textDivNode,
  120. // "html": this.options.text,
  121. "title": this.options.title
  122. });
  123. if (this.tree.options.text=="html"){
  124. textDivNode.set("html", this.options.text);
  125. }else{
  126. textDivNode.set("text", this.options.text);
  127. }
  128. textDivNode.addEvent("click", function(e){
  129. this.clickNode(e);
  130. }.bind(this));
  131. textDivNode.inject(this.textNode);
  132. },
  133. clickNode: function(e){
  134. this.selectNode(e);
  135. this.doAction(e);
  136. },
  137. selectNode: function(){
  138. if (this.tree.currentNode){
  139. var textDivNode = this.tree.currentNode.textNode.getElement("div");
  140. textDivNode.setStyles(this.tree.css.textDivNode);
  141. }
  142. var textDivNode = this.textNode.getElement("div");
  143. textDivNode.setStyles(this.tree.css.textDivNodeSelected);
  144. this.tree.currentNode = this;
  145. },
  146. doAction: function(e){
  147. if (typeOf(this.options.action)=="string"){
  148. Browser.exec(this.options.action);
  149. }else if(typeOf(this.options.action)=="function"){
  150. this.options.action.apply(this, [this]);
  151. }
  152. },
  153. setOperateIcon: function(){
  154. var imgStr = (this.options.expand) ? this.imgs.expand : this.imgs.collapse;
  155. imgStr = this.tree.path+this.tree.options.style+"/"+imgStr;
  156. if (!this.firstChild) imgStr = this.tree.path+this.tree.options.style+"/"+this.imgs.blank;
  157. var img = this.operateNode.getElement("img");
  158. if (!img){
  159. img = new Element("img", {
  160. "src": imgStr,
  161. "width": this.operateNode.getStyle("width"),
  162. "height": this.operateNode.getStyle("height"),
  163. "border": "0"
  164. }).inject(this.operateNode);
  165. }else{
  166. img.set("src", imgStr);
  167. }
  168. },
  169. insertChild: function(obj){
  170. var treeNode = new o2.widget.Tree.Node(this.tree, obj);
  171. var tmpTreeNode = this.previousSibling;
  172. this.previousSibling = treeNode;
  173. treeNode.nextSibling = this;
  174. treeNode.previousSibling = tmpTreeNode;
  175. if (tmpTreeNode){
  176. tmpTreeNode.nextSibling = treeNode;
  177. }else{
  178. this.parentNode.firstChild = treeNode;
  179. }
  180. treeNode.parentNode = this.parentNode;
  181. treeNode.level = this.level;
  182. treeNode.load();
  183. treeNode.node.inject(this.node, "before");
  184. this.parentNode.children.push(treeNode);
  185. return treeNode;
  186. },
  187. appendChild: function(obj){
  188. var treeNode = new o2.widget.Tree.Node(this.tree, obj);
  189. if (this.children.length){
  190. treeNode.previousSibling = this.children[this.children.length-1];
  191. treeNode.previousSibling.nextSibling = treeNode;
  192. }else{
  193. this.firstChild = treeNode;
  194. this.setOperateIcon();
  195. }
  196. treeNode.level = this.level+1;
  197. treeNode.parentNode = this;
  198. treeNode.load();
  199. treeNode.node.inject(this.childrenNode);
  200. this.children.push(treeNode);
  201. return treeNode;
  202. },
  203. expandOrCollapse: function(){
  204. this.tree.expandOrCollapseNode(this);
  205. },
  206. destroy: function(){
  207. if (this.previousSibling) this.previousSibling.nextSibling = this.nextSibling;
  208. if (this.nextSibling) this.nextSibling.previousSibling = this.previousSibling;
  209. if (this.parentNode){
  210. if (this.parentNode.firstChild==this){
  211. this.parentNode.firstChild = this.nextSibling;
  212. }
  213. this.parentNode.children.erase(this);
  214. }
  215. this.node.destroy();
  216. delete this;
  217. }
  218. });