Tree.js 13 KB

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