Tree.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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": {"width": "fit-content", "table-layout": "fixed"}
  205. }).inject(this.itemNode);
  206. this.nodeTable.setStyles(this.tree.css.nodeTable);
  207. if (this.options.style){
  208. if (this.tree.css[this.options.style]){
  209. this.nodeTable.setStyles(this.tree.css[this.options.style].nodeTable);
  210. }
  211. }
  212. var tbody = new Element("tbody").inject(this.nodeTable);
  213. this.nodeArea = new Element("tr").inject(tbody);
  214. this.createLevelNode();
  215. this.createOperateNode();
  216. this.createIconNode();
  217. this.createTextNode();
  218. },
  219. createLevelNode: function(){
  220. for (var i=0; i<this.level; i++){
  221. var td = new Element("td",{
  222. "styles": this.tree.css.blankLevelNode
  223. }).inject(this.nodeArea);
  224. if (this.options.style){
  225. if (this.tree.css[this.options.style]){
  226. td.setStyles(this.tree.css[this.options.style].blankLevelNode);
  227. }
  228. }
  229. this.levelNode.push(td);
  230. }
  231. },
  232. createOperateNode: function(){
  233. this.operateNode = new Element("td",{
  234. "styles": this.tree.css.operateNode
  235. }).inject(this.nodeArea);
  236. if (this.options.style){
  237. if (this.tree.css[this.options.style]){
  238. this.operateNode.setStyles(this.tree.css[this.options.style].operateNode);
  239. }
  240. }
  241. this.operateNode.addEvent("click", function(){
  242. this.expandOrCollapse();
  243. }.bind(this));
  244. this.operateNode.setStyle("background", "url("+this.tree.path+this.tree.options.style+"/"+this.imgs.blank+") center center no-repeat");
  245. //var img = new Element("img", {;
  246. // "src": this.tree.path+this.tree.options.style+"/"+this.imgs.blank,
  247. // "width": this.operateNode.getStyle("width"),
  248. // "height": this.operateNode.getStyle("height"),
  249. // "border": "0",
  250. // "styles": {
  251. // //"margin-top": "6px"
  252. // }
  253. //}).inject(this.operateNode);
  254. },
  255. createIconNode: function(){
  256. if (this.options.icon){
  257. this.iconNode = new Element("td",{
  258. "styles": this.tree.css.iconNode
  259. }).inject(this.nodeArea);
  260. if (this.options.style){
  261. if (this.tree.css[this.options.style]){
  262. this.iconNode.setStyles(this.tree.css[this.options.style].iconNode);
  263. }
  264. }
  265. this.iconNode.setStyle("background", "url("+this.tree.path+this.tree.options.style+"/"+this.options.icon+") center center no-repeat");
  266. }
  267. },
  268. createTextNode: function(){
  269. this.textNode = new Element("td",{
  270. "styles": this.tree.css.textNode
  271. }).inject(this.nodeArea);
  272. if (this.options.style){
  273. if (this.tree.css[this.options.style]){
  274. this.textNode.setStyles(this.tree.css[this.options.style].textNode);
  275. }
  276. }
  277. // var width = this.tree.container.getSize().x - (this.level*20+40);
  278. // this.textNode.setStyle("width", ""+width+"px");
  279. var textDivNode = new Element("div", {
  280. "styles": {"display": "inline-block"},
  281. // "html": this.options.text,
  282. "title": this.options.title
  283. });
  284. textDivNode.setStyles(this.tree.css.textDivNode);
  285. if (this.options.style){
  286. if (this.tree.css[this.options.style]){
  287. textDivNode.setStyles(this.tree.css[this.options.style].textDivNode);
  288. }
  289. }
  290. if (this.tree.options.text=="html"){
  291. textDivNode.set("html", this.options.text);
  292. }else{
  293. textDivNode.set("text", this.options.text);
  294. }
  295. textDivNode.addEvent("click", function(e){
  296. this.clickNode(e);
  297. }.bind(this));
  298. textDivNode.inject(this.textNode);
  299. },
  300. clickNode: function(e){
  301. this.selectNode(e);
  302. this.doAction(e);
  303. },
  304. selectNode: function(){
  305. if (this.tree.currentNode){
  306. var textDivNode = this.tree.currentNode.textNode.getElement("div");
  307. textDivNode.setStyles(this.tree.css.textDivNode);
  308. if (this.tree.currentNode.options.style){
  309. if (this.tree.css[this.tree.currentNode.options.style]){
  310. textDivNode.setStyles(this.tree.css[this.tree.currentNode.options.style].textDivNode);
  311. }
  312. }
  313. }
  314. var textDivNode = this.textNode.getElement("div");
  315. textDivNode.setStyles(this.tree.css.textDivNodeSelected);
  316. if (this.options.style){
  317. if (this.tree.css[this.options.style]){
  318. textDivNode.setStyles(this.tree.css[this.options.style].textDivNodeSelected);
  319. }
  320. }
  321. this.tree.currentNode = this;
  322. },
  323. doAction: function(e){
  324. var t = typeOf(this.options.action);
  325. if (t=="string"){
  326. Browser.exec(this.options.action);
  327. }else if(t=="function"){
  328. this.options.action.apply(this, [this]);
  329. }else if(t=="object"){
  330. if (this.tree.form){
  331. this.tree.form.Macro.exec(this.options.action.code, this)
  332. }
  333. //this.options.action.apply(this, [this]);
  334. }
  335. },
  336. setOperateIcon: function(){
  337. var imgStr = (this.options.expand) ? this.imgs.expand : this.imgs.collapse;
  338. imgStr = this.tree.path+this.tree.options.style+"/"+imgStr;
  339. if (!this.firstChild) imgStr = this.tree.path+this.tree.options.style+"/"+this.imgs.blank;
  340. this.operateNode.setStyle("background", "url("+imgStr+") center center no-repeat");
  341. //var img = this.operateNode.getElement("img");
  342. //if (!img){
  343. // img = new Element("img", {
  344. // "src": imgStr,
  345. // "width": this.operateNode.getStyle("width"),
  346. // "height": this.operateNode.getStyle("height"),
  347. // "border": "0"
  348. // }).inject(this.operateNode);
  349. //}else{
  350. // img.set("src", imgStr);
  351. //}
  352. },
  353. insertChild: function(obj){
  354. var treeNode = new this.tree.$constructor.Node(this.tree, obj);
  355. var tmpTreeNode = this.previousSibling;
  356. this.previousSibling = treeNode;
  357. treeNode.nextSibling = this;
  358. treeNode.previousSibling = tmpTreeNode;
  359. if (tmpTreeNode){
  360. tmpTreeNode.nextSibling = treeNode;
  361. }else{
  362. this.parentNode.firstChild = treeNode;
  363. }
  364. treeNode.parentNode = this.parentNode;
  365. treeNode.level = this.level;
  366. treeNode.load();
  367. treeNode.node.inject(this.node, "before");
  368. this.parentNode.children.push(treeNode);
  369. return treeNode;
  370. },
  371. appendChild: function(obj){
  372. var treeNode = new this.tree.$constructor.Node(this.tree, obj);
  373. if (this.children.length){
  374. treeNode.previousSibling = this.children[this.children.length-1];
  375. treeNode.previousSibling.nextSibling = treeNode;
  376. }else{
  377. this.firstChild = treeNode;
  378. this.setOperateIcon();
  379. }
  380. treeNode.level = this.level+1;
  381. treeNode.parentNode = this;
  382. treeNode.load();
  383. treeNode.node.inject(this.childrenNode);
  384. this.children.push(treeNode);
  385. return treeNode;
  386. },
  387. expandOrCollapse: function(){
  388. this.tree.expandOrCollapseNode(this);
  389. },
  390. destroy: function(){
  391. if (this.previousSibling) this.previousSibling.nextSibling = this.nextSibling;
  392. if (this.nextSibling) this.nextSibling.previousSibling = this.previousSibling;
  393. if (this.parentNode){
  394. if (this.parentNode.firstChild==this){
  395. this.parentNode.firstChild = this.nextSibling;
  396. }
  397. this.parentNode.children.erase(this);
  398. }
  399. this.node.destroy();
  400. delete this;
  401. }
  402. });