LeftNavi.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. o2.widget = o2.widget || {};
  2. o2.require("o2.widget.Common", null, false);
  3. o2.require("o2.widget.Tree", null, false);
  4. o2.widget.LeftNavi = new Class({
  5. Extends: o2.widget.Tree,
  6. Implements: [Options, Events],
  7. options: {
  8. "style": "default",
  9. "expand": false
  10. },
  11. jsonMapping: {
  12. "expand": "expand",
  13. "title": "title",
  14. "text": "text",
  15. "action": "action",
  16. "icon": "icon",
  17. "sub": "sub"
  18. },
  19. initialize: function(container, options){
  20. this.setOptions(options);
  21. this.path = o2.session.path+"/widget/$LeftNavi/";
  22. this.cssPath = o2.session.path+"/widget/$LeftNavi/"+this.options.style+"/css.wcss";
  23. this._loadCss();
  24. this.container = $(container);
  25. this.children = [];
  26. this.treeJson = null;
  27. this.treeXML = null;
  28. },
  29. appendChild: function(obj){
  30. var treeNode = new o2.widget.LeftNavi.Menu(this, obj);
  31. if (this.children.length){
  32. treeNode.previousSibling = this.children[this.children.length-1];
  33. treeNode.previousSibling.nextSibling = treeNode;
  34. }else{
  35. this.firstChild = treeNode;
  36. }
  37. treeNode.load();
  38. treeNode.node.inject(this.node);
  39. this.children.push(treeNode);
  40. return treeNode;
  41. },
  42. expand: function(treeNode){
  43. if (this.fireEvent("queryExpand", [treeNode])){
  44. treeNode.childrenNode.setStyle("display", "block");
  45. }
  46. this.fireEvent("postExpand", [treeNode]);
  47. },
  48. collapse: function(treeNode){
  49. if (this.fireEvent("queryCollapse", [treeNode])){
  50. treeNode.childrenNode.setStyle("display", "none");
  51. }
  52. this.fireEvent("postCollapse", [treeNode]);
  53. }
  54. });
  55. o2.widget.LeftNavi.Menu = new Class({
  56. Extends: o2.widget.Tree.Node,
  57. Implements: [Options, Events],
  58. insertChild: function(obj){
  59. var treeNode = new o2.widget.LeftNavi.Node(this.tree, obj);
  60. var tmpTreeNode = this.previousSibling;
  61. this.previousSibling = treeNode;
  62. treeNode.nextSibling = this;
  63. treeNode.previousSibling = tmpTreeNode;
  64. if (tmpTreeNode){
  65. tmpTreeNode.nextSibling = treeNode;
  66. }else{
  67. this.parentNode.firstChild = treeNode;
  68. }
  69. treeNode.parentNode = this.parentNode;
  70. treeNode.level = this.level;
  71. treeNode.load();
  72. treeNode.node.inject(this.node, "before");
  73. this.parentNode.children.push(treeNode);
  74. return treeNode;
  75. },
  76. appendChild: function(obj){
  77. var treeNode = new o2.widget.LeftNavi.Node(this.tree, obj);
  78. if (this.children.length){
  79. treeNode.previousSibling = this.children[this.children.length-1];
  80. treeNode.previousSibling.nextSibling = treeNode;
  81. }else{
  82. this.firstChild = treeNode;
  83. this.setOperateIcon();
  84. }
  85. treeNode.level = this.level+1;
  86. treeNode.parentNode = this;
  87. treeNode.load();
  88. treeNode.node.inject(this.childrenNode);
  89. this.children.push(treeNode);
  90. return treeNode;
  91. },
  92. load: function(){
  93. this.nodeTable = new Element("table", {
  94. "border": "0",
  95. "cellpadding": "0",
  96. "cellspacing": "0",
  97. "styles": this.tree.css.nodeTable
  98. }).inject(this.itemNode);
  99. var tbody = new Element("tbody").inject(this.nodeTable);
  100. this.nodeArea = new Element("tr").inject(tbody);
  101. this.createLevelNode();
  102. this.createOperateNode();
  103. this.createIconNode();
  104. this.createTextNode();
  105. this.setOtherEvent();
  106. },
  107. setOtherEvent: function(){
  108. this.operateNode.removeEvents("click");
  109. this.textNode.getFirst("div").removeEvents("click");
  110. this.itemNode.addEvent("click", function(){
  111. this.clickNode();
  112. }.bind(this));
  113. },
  114. clickNode: function(){
  115. this.expand();
  116. },
  117. expand: function(){
  118. if (this.tree.currentMenu != this){
  119. this.selectNode();
  120. }
  121. },
  122. selectNode: function(){
  123. if (this.tree.currentMenu){
  124. this.tree.currentMenu.itemNode.setStyles(this.tree.css.treeItemNode);
  125. this.tree.currentMenu.collapseNode();
  126. }
  127. this.itemNode.setStyles(this.tree.css.treeItemNodeSelected);
  128. this.expandNode();
  129. this.tree.currentMenu = this;
  130. },
  131. expandNode: function(){
  132. var morph = new Fx.Morph(this.childrenNode, {duration: 100});
  133. this.childrenNode.setStyles({
  134. "display": "block",
  135. "height": "0px"
  136. });
  137. morph.start({
  138. "height": this.children.length*40
  139. }).chain(function(){}.bind(this));
  140. },
  141. collapseNode: function(){
  142. var morph = new Fx.Morph(this.childrenNode, {duration: 100});
  143. morph.start({
  144. "height": 0
  145. }).chain(function(){
  146. this.childrenNode.setStyle("display", "none");
  147. }.bind(this));
  148. }
  149. });
  150. o2.widget.LeftNavi.Node = new Class({
  151. Extends: o2.widget.Tree.Node,
  152. Implements: [Options, Events],
  153. insertChild: function(obj){
  154. var treeNode = new o2.widget.LeftNavi.Node(this.tree, obj);
  155. var tmpTreeNode = this.previousSibling;
  156. this.previousSibling = treeNode;
  157. treeNode.nextSibling = this;
  158. treeNode.previousSibling = tmpTreeNode;
  159. if (tmpTreeNode){
  160. tmpTreeNode.nextSibling = treeNode;
  161. }else{
  162. this.parentNode.firstChild = treeNode;
  163. }
  164. treeNode.parentNode = this.parentNode;
  165. treeNode.level = this.level;
  166. treeNode.load();
  167. treeNode.node.inject(this.node, "before");
  168. this.parentNode.children.push(treeNode);
  169. return treeNode;
  170. },
  171. appendChild: function(obj){
  172. var treeNode = new o2.widget.LeftNavi.Node(this.tree, obj);
  173. if (this.children.length){
  174. treeNode.previousSibling = this.children[this.children.length-1];
  175. treeNode.previousSibling.nextSibling = treeNode;
  176. }else{
  177. this.firstChild = treeNode;
  178. this.setOperateIcon();
  179. }
  180. treeNode.level = this.level+1;
  181. treeNode.parentNode = this;
  182. treeNode.load();
  183. treeNode.node.inject(this.childrenNode);
  184. this.children.push(treeNode);
  185. return treeNode;
  186. },
  187. load: function(){
  188. this.nodeTable = new Element("table", {
  189. "border": "0",
  190. "cellpadding": "0",
  191. "cellspacing": "0",
  192. "styles": this.tree.css.nodeTable
  193. }).inject(this.itemNode);
  194. var tbody = new Element("tbody").inject(this.nodeTable);
  195. this.nodeArea = new Element("tr").inject(tbody);
  196. this.createLevelNode();
  197. this.createOperateNode();
  198. this.createIconNode();
  199. this.createTextNode();
  200. this.setLevelStyle();
  201. this.setOtherEvent();
  202. },
  203. setOtherEvent: function(){
  204. if (this.level==0){
  205. this.operateNode.removeEvents("click");
  206. this.itemNode.addEvent("click", function(){
  207. this.menuClickNode();
  208. }.bind(this));
  209. }
  210. },
  211. menuClickNode: function(){
  212. this.selectNode();
  213. this.expandOrCollapse();
  214. },
  215. selectNode: function(){
  216. if (this.tree.currentNode){
  217. var itemNode = this.tree.currentNode.itemNode;
  218. if (this.level>0){
  219. itemNode.setStyles(this.tree.css.treeItemNodeSub);
  220. }else{
  221. itemNode.setStyles(this.tree.css.treeItemNode);
  222. }
  223. }
  224. var itemNode = this.itemNode;
  225. if (this.level>0){
  226. itemNode.setStyles(this.tree.css.treeItemNodeSelectedSub);
  227. }else{
  228. itemNode.setStyles(this.tree.css.treeItemNodeSelected);
  229. }
  230. this.tree.currentNode = this;
  231. },
  232. setLevelStyle: function(){
  233. if (this.tree.css.treeNodeSub) this.node.setStyles(this.tree.css.treeNodeSub);
  234. if (this.tree.css.treeItemNodeSub) this.itemNode.setStyles(this.tree.css.treeItemNodeSub);
  235. if (this.tree.css.treeChildrenNodeSub) this.childrenNode.setStyles(this.tree.css.treeChildrenNodeSub);
  236. if (this.tree.css.nodeTableSub) this.nodeTable.setStyles(this.tree.css.nodeTableSub);
  237. if (this.tree.css.textDivNodeSub) this.textNode.getFirst("div").setStyles(this.tree.css.textDivNodeSub);
  238. if (this.tree.css.iconNodeSub) this.iconNode.setStyles(this.tree.css.iconNodeSub);
  239. if (this.tree.css.blankLevelNodeSub){
  240. this.levelNode.each(function(node){
  241. node.setStyles(this.tree.css.blankLevelNodeSub);
  242. });
  243. }
  244. }
  245. });