Tree.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. o2.widget = o2.widget || {};
  2. o2.require("o2.widget.Common", null, false);
  3. o2.widget.Tree = new Class({
  4. Extends: o2.widget.Common,
  5. Implements: [Options, Events],
  6. children: [],
  7. options: {
  8. "style": "default",
  9. "expand": false,
  10. "text": "html"
  11. },
  12. jsonMapping: {
  13. "expand": "expand",
  14. "title": "title",
  15. "text": "text",
  16. "action": "action",
  17. "icon": "icon",
  18. "style": "",
  19. "sub": "sub",
  20. "defalut" : "defalut"
  21. },
  22. initialize: function(container, options){
  23. this.setOptions(options);
  24. this.path = o2.session.path+"/widget/$Tree/";
  25. this.cssPath = o2.session.path+"/widget/$Tree/"+this.options.style+"/css.wcss";
  26. this._loadCss();
  27. this.container = $(container);
  28. this.children = [];
  29. this.treeJson = null;
  30. this.treeXML = null;
  31. },
  32. load: function(json){
  33. if (this.fireEvent("queryLoad")){
  34. if (json) this.treeJson = json;
  35. this.node = new Element("div",{
  36. "styles": this.css.areaNode
  37. });
  38. this.loadTree();
  39. this.node.inject(this.container);
  40. this.fireEvent("postLoad");
  41. }
  42. },
  43. empty: function(){
  44. this.children.each(function(o){
  45. o2.release(o);
  46. }.bind(this));
  47. this.node.empty();
  48. },
  49. reLoad: function(json){
  50. if (json) this.treeJson = json;
  51. this.children = [];
  52. this.node.empty();
  53. this.loadTree();
  54. },
  55. loadTree: function(){
  56. if (this.treeJson){
  57. this.loadJsonTree(this.treeJson, this, this);
  58. }else if (this.treeXML){
  59. this.loadXMLTree();
  60. }
  61. if (this.container) this.node.inject(this.container);
  62. },
  63. mappingJson: function(mapping){
  64. if (mapping.expand) this.jsonMapping.expand = mapping.expand;
  65. if (mapping.title) this.jsonMapping.title = mapping.title;
  66. if (mapping.text) this.jsonMapping.text = mapping.text;
  67. if (mapping.action) this.jsonMapping.action = mapping.action;
  68. if (mapping.icon) this.jsonMapping.icon = mapping.icon;
  69. if (mapping.style) this.jsonMapping.style = mapping.style;
  70. if (mapping.sub) this.jsonMapping.sub = mapping.sub;
  71. },
  72. loadJsonTree: function(treeJson, tree, node){
  73. treeJson.each(function(item){
  74. var options = {};
  75. if (item[this.jsonMapping.expand]!=undefined) options.expand = item[this.jsonMapping.expand];
  76. if (item[this.jsonMapping.title]) options.title = item[this.jsonMapping.title];
  77. if (item[this.jsonMapping.text]) options.text = item[this.jsonMapping.text];
  78. if (item[this.jsonMapping.action]) options.action = item[this.jsonMapping.action];
  79. if (item[this.jsonMapping.style]) options.style = item[this.jsonMapping.style];
  80. if (item[this.jsonMapping.icon]) options.icon = item[this.jsonMapping.icon];
  81. var treeNode = node.appendChild(options);
  82. if (item[this.jsonMapping.sub]){
  83. this.loadJsonTree(item[this.jsonMapping.sub], this, treeNode);
  84. }
  85. }.bind(tree));
  86. },
  87. appendChild: function(obj){
  88. var treeNode = new this.$constructor.Node(this, obj);
  89. if (this.children.length){
  90. treeNode.previousSibling = this.children[this.children.length-1];
  91. treeNode.previousSibling.nextSibling = treeNode;
  92. }else{
  93. this.firstChild = treeNode;
  94. }
  95. treeNode.load();
  96. treeNode.node.inject(this.node);
  97. this.children.push(treeNode);
  98. return treeNode;
  99. },
  100. expandOrCollapseNode: function(treeNode){
  101. if (treeNode.options.expand){
  102. this.collapse(treeNode);
  103. treeNode.options.expand = false;
  104. }else{
  105. this.expand(treeNode);
  106. treeNode.options.expand = true;
  107. }
  108. treeNode.setOperateIcon();
  109. },
  110. expand: function(treeNode){
  111. if (this.fireEvent("queryExpand", [treeNode])){
  112. treeNode.childrenNode.setStyle("display", "block");
  113. }
  114. this.fireEvent("postExpand", [treeNode]);
  115. },
  116. collapse: function(treeNode){
  117. if (this.fireEvent("queryCollapse", [treeNode])){
  118. treeNode.childrenNode.setStyle("display", "none");
  119. }
  120. this.fireEvent("postCollapse", [treeNode]);
  121. },
  122. toJson: function(item){
  123. var json=null;
  124. var node = item.firstChild;
  125. json=[];
  126. while (node){
  127. json.push(this.transObj(node.options));
  128. json[json.length-1].sub = this.toJson(node);
  129. node = node.nextSibling;
  130. }
  131. return json;
  132. },
  133. transObj: function(options){
  134. var obj = {};
  135. obj[this.jsonMapping.expand] = options.expand;
  136. obj[this.jsonMapping.title] = options.title;
  137. obj[this.jsonMapping.text] = options.text;
  138. obj[this.jsonMapping.action] = options.action;
  139. obj[this.jsonMapping.style] = options.style;
  140. obj[this.jsonMapping.defalut] = options.defalut;
  141. obj[this.jsonMapping.icon] = (options.icon) ? options.icon : "none";
  142. return obj;
  143. }
  144. });
  145. o2.widget.Tree.Node = new Class({
  146. Implements: [Options, Events],
  147. options: {
  148. "expand": true,
  149. "title": "",
  150. "text": "",
  151. "action": "",
  152. "style": "",
  153. "default" : false,
  154. "icon": "folder.png"
  155. },
  156. imgs: {
  157. "expand": "expand.gif",
  158. "collapse":"collapse.gif",
  159. "blank": "blank.gif"
  160. },
  161. tree: null,
  162. level: 0,
  163. levelNode:[],
  164. initialize: function(tree, options){
  165. this.setOptions(options);
  166. if (options.icon=="none") this.options.icon = "";
  167. this.tree = tree;
  168. this.levelNode = [];
  169. this.children = [];
  170. this.parentNode = null;
  171. this.previousSibling = null;
  172. this.nextSibling = null;
  173. this.firstChild = null;
  174. this.node = new Element("div",{
  175. "styles": this.tree.css.treeNode
  176. });
  177. this.itemNode = new Element("div", {
  178. "styles": this.tree.css.treeItemNode
  179. }).inject(this.node);
  180. this.childrenNode = new Element("div", {
  181. "styles": this.tree.css.treeChildrenNode
  182. }).inject(this.node);
  183. if (this.options.style){
  184. if (this.tree.css[this.options.style]){
  185. this.node.setStyles(this.tree.css[this.options.style].treeNode);
  186. this.itemNode.setStyles(this.tree.css[this.options.style].treeItemNode);
  187. this.childrenNode.setStyles(this.tree.css[this.options.style].treeChildrenNode);
  188. }
  189. }
  190. if (!this.options.expand){
  191. this.childrenNode.setStyle("display", "none");
  192. }
  193. },
  194. setText: function(value){
  195. var textDivNode = this.textNode.getElement("div");
  196. if (textDivNode) textDivNode.set("text", value);
  197. },
  198. setTitle: function(value){
  199. var textDivNode = this.textNode.getElement("div");
  200. if (textDivNode) textDivNode.set("title", value);
  201. },
  202. load: function(){
  203. debugger;
  204. this.tree.fireEvent("beforeLoadTreeNode", [this]);
  205. this.nodeTable = new Element("table", {
  206. "border": "0",
  207. "cellpadding": "0",
  208. "cellspacing": "0",
  209. "styles": this.tree.css.nodeTable
  210. }).inject(this.itemNode);
  211. if (this.options.style){
  212. if (this.tree.css[this.options.style]){
  213. this.nodeTable.setStyles(this.tree.css[this.options.style].nodeTable);
  214. }
  215. }
  216. var tbody = new Element("tbody").inject(this.nodeTable);
  217. this.nodeArea = new Element("tr").inject(tbody);
  218. this.createLevelNode();
  219. this.createOperateNode();
  220. this.createIconNode();
  221. this.createTextNode();
  222. this.tree.fireEvent("afterLoadTreeNode", [this]);
  223. },
  224. createLevelNode: function(){
  225. for (var i=0; i<this.level; i++){
  226. var td = new Element("td",{
  227. "styles": this.tree.css.blankLevelNode
  228. }).inject(this.nodeArea);
  229. if (this.options.style){
  230. if (this.tree.css[this.options.style]){
  231. td.setStyles(this.tree.css[this.options.style].blankLevelNode);
  232. }
  233. }
  234. this.levelNode.push(td);
  235. }
  236. },
  237. createOperateNode: function(){
  238. this.operateNode = new Element("td",{
  239. "styles": this.tree.css.operateNode
  240. }).inject(this.nodeArea);
  241. if (this.options.style){
  242. if (this.tree.css[this.options.style]){
  243. this.operateNode.setStyles(this.tree.css[this.options.style].operateNode);
  244. }
  245. }
  246. this.operateNode.addEvent("click", function(){
  247. this.expandOrCollapse();
  248. }.bind(this));
  249. this.operateNode.setStyle("background", "url("+this.tree.path+this.tree.options.style+"/"+this.imgs.blank+") center center no-repeat");
  250. //var img = new Element("img", {;
  251. // "src": this.tree.path+this.tree.options.style+"/"+this.imgs.blank,
  252. // "width": this.operateNode.getStyle("width"),
  253. // "height": this.operateNode.getStyle("height"),
  254. // "border": "0",
  255. // "styles": {
  256. // //"margin-top": "6px"
  257. // }
  258. //}).inject(this.operateNode);
  259. },
  260. createIconNode: function(){
  261. if (this.options.icon){
  262. this.iconNode = new Element("td",{
  263. "styles": this.tree.css.iconNode
  264. }).inject(this.nodeArea);
  265. if (this.options.style){
  266. if (this.tree.css[this.options.style]){
  267. this.iconNode.setStyles(this.tree.css[this.options.style].iconNode);
  268. }
  269. }
  270. this.iconNode.setStyle("background", "url("+this.tree.path+this.tree.options.style+"/"+this.options.icon+") center center no-repeat");
  271. }
  272. },
  273. createTextNode: function(){
  274. this.textNode = new Element("td",{
  275. "styles": this.tree.css.textNode
  276. }).inject(this.nodeArea);
  277. if (this.options.style){
  278. if (this.tree.css[this.options.style]){
  279. this.textNode.setStyles(this.tree.css[this.options.style].textNode);
  280. }
  281. }
  282. // var width = this.tree.container.getSize().x - (this.level*20+40);
  283. // this.textNode.setStyle("width", ""+width+"px");
  284. var textDivNode = new Element("div", {
  285. "styles": this.tree.css.textDivNode,
  286. // "html": this.options.text,
  287. "title": this.options.title
  288. });
  289. if (this.options.style){
  290. if (this.tree.css[this.options.style]){
  291. textDivNode.setStyles(this.tree.css[this.options.style].textDivNode);
  292. }
  293. }
  294. if (this.tree.options.text=="html"){
  295. textDivNode.set("html", this.options.text);
  296. }else{
  297. textDivNode.set("text", this.options.text);
  298. }
  299. textDivNode.addEvent("click", function(e){
  300. this.clickNode(e);
  301. }.bind(this));
  302. textDivNode.inject(this.textNode);
  303. if( this.options.default ){
  304. textDivNode.click();
  305. }
  306. },
  307. clickNode: function(e){
  308. this.selectNode(e);
  309. this.doAction(e);
  310. },
  311. selectNode: function(){
  312. this.tree.fireEvent("beforeSelect", [this]);
  313. if (this.tree.currentNode){
  314. var textDivNode = this.tree.currentNode.textNode.getElement("div");
  315. textDivNode.setStyles(this.tree.css.textDivNode);
  316. if (this.tree.currentNode.options.style){
  317. if (this.tree.css[this.tree.currentNode.options.style]){
  318. textDivNode.setStyles(this.tree.css[this.tree.currentNode.options.style].textDivNode);
  319. }
  320. }
  321. }
  322. var textDivNode = this.textNode.getElement("div");
  323. textDivNode.setStyles(this.tree.css.textDivNodeSelected);
  324. if (this.options.style){
  325. if (this.tree.css[this.options.style]){
  326. textDivNode.setStyles(this.tree.css[this.options.style].textDivNodeSelected);
  327. }
  328. }
  329. this.tree.currentNode = this;
  330. this.tree.fireEvent("afterSelect", [this]);
  331. },
  332. doAction: function(e){
  333. var t = typeOf(this.options.action);
  334. if (t=="string"){
  335. Browser.exec(this.options.action);
  336. }else if(t=="function"){
  337. this.options.action.apply(this, [this]);
  338. }else if(t=="object"){
  339. if (this.tree.form){
  340. this.tree.form.Macro.exec(this.options.action.code, this)
  341. }
  342. //this.options.action.apply(this, [this]);
  343. }
  344. },
  345. setOperateIcon: function(){
  346. var imgStr = (this.options.expand) ? this.imgs.expand : this.imgs.collapse;
  347. imgStr = this.tree.path+this.tree.options.style+"/"+imgStr;
  348. if (!this.firstChild) imgStr = this.tree.path+this.tree.options.style+"/"+this.imgs.blank;
  349. this.operateNode.setStyle("background", "url("+imgStr+") center center no-repeat");
  350. //var img = this.operateNode.getElement("img");
  351. //if (!img){
  352. // img = new Element("img", {
  353. // "src": imgStr,
  354. // "width": this.operateNode.getStyle("width"),
  355. // "height": this.operateNode.getStyle("height"),
  356. // "border": "0"
  357. // }).inject(this.operateNode);
  358. //}else{
  359. // img.set("src", imgStr);
  360. //}
  361. },
  362. insertChild: function(obj){
  363. var treeNode = new this.tree.$constructor.Node(this.tree, obj);
  364. var tmpTreeNode = this.previousSibling;
  365. this.previousSibling = treeNode;
  366. treeNode.nextSibling = this;
  367. treeNode.previousSibling = tmpTreeNode;
  368. if (tmpTreeNode){
  369. tmpTreeNode.nextSibling = treeNode;
  370. }else{
  371. this.parentNode.firstChild = treeNode;
  372. }
  373. treeNode.parentNode = this.parentNode;
  374. treeNode.level = this.level;
  375. treeNode.load();
  376. treeNode.node.inject(this.node, "before");
  377. this.parentNode.children.push(treeNode);
  378. return treeNode;
  379. },
  380. appendChild: function(obj){
  381. var treeNode = new this.tree.$constructor.Node(this.tree, obj);
  382. if (this.children.length){
  383. treeNode.previousSibling = this.children[this.children.length-1];
  384. treeNode.previousSibling.nextSibling = treeNode;
  385. }else{
  386. this.firstChild = treeNode;
  387. this.setOperateIcon();
  388. }
  389. treeNode.level = this.level+1;
  390. treeNode.parentNode = this;
  391. treeNode.load();
  392. treeNode.node.inject(this.childrenNode);
  393. this.children.push(treeNode);
  394. return treeNode;
  395. },
  396. expandOrCollapse: function(){
  397. this.tree.expandOrCollapseNode(this);
  398. },
  399. destroy: function(){
  400. if (this.previousSibling) this.previousSibling.nextSibling = this.nextSibling;
  401. if (this.nextSibling) this.nextSibling.previousSibling = this.previousSibling;
  402. if (this.parentNode){
  403. if (this.parentNode.firstChild==this){
  404. this.parentNode.firstChild = this.nextSibling;
  405. }
  406. this.parentNode.children.erase(this);
  407. }
  408. this.node.destroy();
  409. delete this;
  410. }
  411. });