Tree.js 15 KB

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