Tree.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. MWF.widget = MWF.widget || {};
  2. MWF.require("MWF.widget.Common", null, false);
  3. MWF.widget.Tree = new Class({
  4. Extends: MWF.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 = MWF.defaultPath+"/widget/$Tree/";
  24. this.cssPath = MWF.defaultPath+"/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. MWF.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. debugger;
  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.icon] = (options.icon) ? options.icon : "none";
  141. return obj;
  142. }
  143. });
  144. MWF.widget.Tree.Node = new Class({
  145. Implements: [Options, Events],
  146. options: {
  147. "expand": true,
  148. "title": "",
  149. "text": "",
  150. "action": "",
  151. "style": "",
  152. "icon": "folder.png"
  153. },
  154. imgs: {
  155. "expand": "expand.gif",
  156. "collapse":"collapse.gif",
  157. "blank": "blank.gif"
  158. },
  159. tree: null,
  160. level: 0,
  161. levelNode:[],
  162. initialize: function(tree, options){
  163. this.setOptions(options);
  164. if (options.icon=="none") this.options.icon = "";
  165. this.tree = tree;
  166. this.levelNode = [];
  167. this.children = [];
  168. this.parentNode = null;
  169. this.previousSibling = null;
  170. this.nextSibling = null;
  171. this.firstChild = null;
  172. this.node = new Element("div",{
  173. "styles": this.tree.css.treeNode
  174. });
  175. this.itemNode = new Element("div", {
  176. "styles": this.tree.css.treeItemNode
  177. }).inject(this.node);
  178. this.childrenNode = new Element("div", {
  179. "styles": this.tree.css.treeChildrenNode
  180. }).inject(this.node);
  181. if (this.options.style){
  182. if (this.tree.css[this.options.style]){
  183. this.node.setStyles(this.tree.css[this.options.style].treeNode);
  184. this.itemNode.setStyles(this.tree.css[this.options.style].treeItemNode);
  185. this.childrenNode.setStyles(this.tree.css[this.options.style].treeChildrenNode);
  186. }
  187. }
  188. if (!this.options.expand){
  189. this.childrenNode.setStyle("display", "none");
  190. }
  191. },
  192. setText: function(value){
  193. var textDivNode = this.textNode.getElement("div");
  194. if (textDivNode) textDivNode.set("text", value);
  195. },
  196. setTitle: function(value){
  197. var textDivNode = this.textNode.getElement("div");
  198. if (textDivNode) textDivNode.set("title", value);
  199. },
  200. load: function(){
  201. this.nodeTable = new Element("table", {
  202. "border": "0",
  203. "cellpadding": "0",
  204. "cellspacing": "0",
  205. "styles": this.tree.css.nodeTable
  206. }).inject(this.itemNode);
  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": this.tree.css.textDivNode,
  281. // "html": this.options.text,
  282. "title": this.options.title
  283. });
  284. if (this.options.style){
  285. if (this.tree.css[this.options.style]){
  286. textDivNode.setStyles(this.tree.css[this.options.style].textDivNode);
  287. }
  288. }
  289. if (this.tree.options.text=="html"){
  290. textDivNode.set("html", this.options.text);
  291. }else{
  292. textDivNode.set("text", this.options.text);
  293. }
  294. textDivNode.addEvent("click", function(e){
  295. this.clickNode(e);
  296. }.bind(this));
  297. textDivNode.inject(this.textNode);
  298. },
  299. clickNode: function(e){
  300. this.selectNode(e);
  301. this.doAction(e);
  302. },
  303. selectNode: function(){
  304. if (this.tree.currentNode){
  305. var textDivNode = this.tree.currentNode.textNode.getElement("div");
  306. textDivNode.setStyles(this.tree.css.textDivNode);
  307. if (this.tree.currentNode.options.style){
  308. if (this.tree.css[this.tree.currentNode.options.style]){
  309. textDivNode.setStyles(this.tree.css[this.tree.currentNode.options.style].textDivNode);
  310. }
  311. }
  312. }
  313. var textDivNode = this.textNode.getElement("div");
  314. textDivNode.setStyles(this.tree.css.textDivNodeSelected);
  315. if (this.options.style){
  316. if (this.tree.css[this.options.style]){
  317. textDivNode.setStyles(this.tree.css[this.options.style].textDivNodeSelected);
  318. }
  319. }
  320. this.tree.currentNode = this;
  321. },
  322. doAction: function(e){
  323. if (typeOf(this.options.action)=="string"){
  324. Browser.exec(this.options.action);
  325. }else if(typeOf(this.options.action)=="function"){
  326. this.options.action.apply(this, [this]);
  327. }
  328. },
  329. setOperateIcon: function(){
  330. var imgStr = (this.options.expand) ? this.imgs.expand : this.imgs.collapse;
  331. imgStr = this.tree.path+this.tree.options.style+"/"+imgStr;
  332. if (!this.firstChild) imgStr = this.tree.path+this.tree.options.style+"/"+this.imgs.blank;
  333. this.operateNode.setStyle("background", "url("+imgStr+") center center no-repeat");
  334. //var img = this.operateNode.getElement("img");
  335. //if (!img){
  336. // img = new Element("img", {
  337. // "src": imgStr,
  338. // "width": this.operateNode.getStyle("width"),
  339. // "height": this.operateNode.getStyle("height"),
  340. // "border": "0"
  341. // }).inject(this.operateNode);
  342. //}else{
  343. // img.set("src", imgStr);
  344. //}
  345. },
  346. insertChild: function(obj){
  347. var treeNode = new this.tree.$constructor.Node(this.tree, obj);
  348. var tmpTreeNode = this.previousSibling;
  349. this.previousSibling = treeNode;
  350. treeNode.nextSibling = this;
  351. treeNode.previousSibling = tmpTreeNode;
  352. if (tmpTreeNode){
  353. tmpTreeNode.nextSibling = treeNode;
  354. }else{
  355. this.parentNode.firstChild = treeNode;
  356. }
  357. treeNode.parentNode = this.parentNode;
  358. treeNode.level = this.level;
  359. treeNode.load();
  360. treeNode.node.inject(this.node, "before");
  361. this.parentNode.children.push(treeNode);
  362. return treeNode;
  363. },
  364. appendChild: function(obj){
  365. var treeNode = new this.tree.$constructor.Node(this.tree, obj);
  366. if (this.children.length){
  367. treeNode.previousSibling = this.children[this.children.length-1];
  368. treeNode.previousSibling.nextSibling = treeNode;
  369. }else{
  370. this.firstChild = treeNode;
  371. this.setOperateIcon();
  372. }
  373. treeNode.level = this.level+1;
  374. treeNode.parentNode = this;
  375. treeNode.load();
  376. treeNode.node.inject(this.childrenNode);
  377. this.children.push(treeNode);
  378. return treeNode;
  379. },
  380. expandOrCollapse: function(){
  381. this.tree.expandOrCollapseNode(this);
  382. },
  383. destroy: function(){
  384. if (this.previousSibling) this.previousSibling.nextSibling = this.nextSibling;
  385. if (this.nextSibling) this.nextSibling.previousSibling = this.previousSibling;
  386. if (this.parentNode){
  387. if (this.parentNode.firstChild==this){
  388. this.parentNode.firstChild = this.nextSibling;
  389. }
  390. this.parentNode.children.erase(this);
  391. }
  392. this.node.destroy();
  393. delete this;
  394. }
  395. });