Tab.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. MWF.widget = MWF.widget || {};
  2. MWF.widget.Tab = new Class({
  3. Implements: [Options, Events],
  4. Extends: MWF.widget.Common,
  5. options: {
  6. "style": "default"
  7. },
  8. initialize: function(container, options){
  9. this.setOptions(options);
  10. this.pages = [];
  11. this.path = MWF.defaultPath+"/widget/$Tab/";
  12. this.cssPath = MWF.defaultPath+"/widget/$Tab/"+this.options.style+"/css.wcss";
  13. this._loadCss();
  14. this.css = Object.clone(this.css);
  15. this.showPage = null;
  16. this.node = $(container);
  17. },
  18. load: function(){
  19. if (this.fireEvent("queryLoad")){
  20. if (!this.tabNodeContainer) this.tabNodeContainer = new Element("div");
  21. this.tabNodeContainer.set("styles", this.css.tabNodeContainer);
  22. this.tabNodeContainer.inject(this.node);
  23. if (!this.contentNodeContainer) this.contentNodeContainer = new Element("div");
  24. this.contentNodeContainer.set("name", "MWFcontentNodeContainer");
  25. this.contentNodeContainer.set("styles", this.css.contentNodeContainer);
  26. this.contentNodeContainer.inject(this.node);
  27. this.fireEvent("postLoad");
  28. }
  29. },
  30. rebuildTab: function(contentAreaNode,contentNode, pageNode){
  31. var tabPage = new MWF.widget.TabPage(contentNode, "", this, {"isClose": false});
  32. tabPage.contentNodeArea = contentAreaNode;
  33. tabPage.tabNode = pageNode;
  34. tabPage.textNode = pageNode.getFirst();
  35. tabPage.closeNode = tabPage.textNode.getFirst();
  36. tabPage.options.title = tabPage.textNode.get("text");
  37. tabPage.load();
  38. this.pages.push(tabPage);
  39. return tabPage;
  40. },
  41. addTab: function(node, title, isclose, pageNode){
  42. var tabPage = new MWF.widget.TabPage(node, title, this, {"isClose": isclose, "tabNode": pageNode});
  43. tabPage.load();
  44. this.pages.push(tabPage);
  45. return tabPage;
  46. }
  47. });
  48. MWF.widget.TabPage = new Class({
  49. Implements: [Options, Events],
  50. options: {
  51. "isClose": true,
  52. "tabNode": null,
  53. "title": ""
  54. },
  55. initialize: function(node, title, tab, options){
  56. this.setOptions(options);
  57. this.options.title = title;
  58. this.tab = tab;
  59. this.contentNode = $(node);
  60. },
  61. load: function(){
  62. if (!this.contentNodeArea) this.contentNodeArea = new Element("div");
  63. this.contentNodeArea.set("styles", this.tab.css.contentNodeArea);
  64. if (!this.tabNode) this.tabNode = new Element("div");
  65. this.tabNode.set("styles", this.tab.css.tabNode);
  66. this.tabNode.addEvent("mousedown", function(event){event.stop();});
  67. if (!this.textNode) this.textNode = new Element("div").inject(this.tabNode);
  68. this.textNode.set({
  69. "styles": this.tab.css.tabTextNode,
  70. "text": this.options.title
  71. });
  72. this.tabNode.addEvent("click", this._showTab.bind(this));
  73. if (this.options.isClose){
  74. if (!this.closeNode) this.closeNode = new Element("div").inject(this.tabNode);
  75. this.closeNode.set({
  76. "styles": this.tab.css.tabCloseNode
  77. });
  78. this.closeNode.addEvent("click", this.closeTab.bind(this));
  79. }
  80. this.contentNodeArea.inject(this.tab.contentNodeContainer);
  81. this.tabNode.inject(this.tab.tabNodeContainer);
  82. this.contentNode.inject(this.contentNodeArea);
  83. },
  84. _showTab: function(){
  85. this.showTabIm();
  86. },
  87. showTabIm: function(callback){
  88. if (!this.isShow){
  89. this.tab.pages.each(function(page){
  90. if (page.isShow) page.hideIm();
  91. });
  92. this.showIm(callback);
  93. }
  94. },
  95. showTab: function(callback){
  96. if (!this.isShow){
  97. this.tab.pages.each(function(page){
  98. if (page.isShow) page.hide();
  99. });
  100. this.show(callback);
  101. }
  102. },
  103. showIm: function(callback){
  104. this.fireEvent("queryShow");
  105. this.tabNode.set("styles", this.tab.css.tabNodeCurrent);
  106. this.textNode.set("styles", this.tab.css.tabTextNodeCurrent);
  107. if (this.closeNode) this.closeNode.set("styles", this.tab.css.tabCloseNodeCurrent);
  108. this.contentNodeArea.setStyle("display", "block");
  109. this.contentNodeArea.setStyle("opacity", 1);
  110. this.isShow = true;
  111. this.tab.showPage = this;
  112. if (callback) callback();
  113. this.fireEvent("show");
  114. this.fireEvent("postShow");
  115. },
  116. show: function(callback){
  117. this.fireEvent("queryShow");
  118. this.tabNode.set("styles", this.tab.css.tabNodeCurrent);
  119. this.textNode.set("styles", this.tab.css.tabTextNodeCurrent);
  120. if (this.closeNode) this.closeNode.set("styles", this.tab.css.tabCloseNodeCurrent);
  121. this.contentNodeArea.setStyle("display", "block");
  122. this.contentNodeArea.setStyle("opacity", 1);
  123. if (!this.morph){
  124. this.morph = new Fx.Morph(this.contentNodeArea, {duration: 100});
  125. }
  126. this.morph.start({
  127. "opacity": 1
  128. }).chain(function(){
  129. this.isShow = true;
  130. this.tab.showPage = this;
  131. if (callback) callback();
  132. this.fireEvent("postShow");
  133. }.bind(this));
  134. this.fireEvent("show");
  135. },
  136. hideIm: function(){
  137. if (this.isShow){
  138. this.fireEvent("queryHide");
  139. this.tabNode.set("styles", this.tab.css.tabNode);
  140. this.textNode.set("styles", this.tab.css.tabTextNode);
  141. if (this.closeNode) this.closeNode.set("styles", this.tab.css.tabCloseNode);
  142. this.contentNodeArea.setStyle("display", "none");
  143. this.contentNodeArea.setStyle("opacity", 0);
  144. this.isShow = false;
  145. this.fireEvent("hide");
  146. this.fireEvent("postHide");
  147. }
  148. },
  149. hide: function(){
  150. if (this.isShow){
  151. this.fireEvent("queryHide");
  152. this.tabNode.set("styles", this.tab.css.tabNode);
  153. this.textNode.set("styles", this.tab.css.tabTextNode);
  154. if (this.closeNode) this.closeNode.set("styles", this.tab.css.tabCloseNode);
  155. if (!this.morph){
  156. this.morph = new Fx.Morph(this.contentNodeArea, {duration: 100});
  157. }
  158. this.morph.start({
  159. "opacity": 0
  160. }).chain(function(){
  161. this.contentNodeArea.setStyle("display", "none");
  162. this.isShow = false;
  163. this.fireEvent("postHide");
  164. }.bind(this));
  165. this.fireEvent("hide");
  166. }
  167. },
  168. closeTab: function(){
  169. this.fireEvent("queryClose");
  170. var prevPage = this.getPrevPage();
  171. this.contentNodeArea.destroy();
  172. this.tabNode.destroy();
  173. var tmp = [];
  174. this.tab.pages = this.tab.pages.erase(this);
  175. // this.tab.pages.each(function(item){
  176. // if (item!=this){
  177. // tmp.push(item);
  178. // }
  179. // });
  180. // this.tab.pages = tmp;
  181. if (prevPage){
  182. prevPage.showTab();
  183. }else{
  184. if (this.tab.pages.length) this.tab.pages[this.tab.pages.length-1].showTab();
  185. }
  186. this.fireEvent("close");
  187. },
  188. getPrevPage: function(){
  189. var idx = this.tab.pages.indexOf(this);
  190. var prevIdx = idx-1;
  191. if (prevIdx<0){
  192. return null;
  193. }else{
  194. return this.tab.pages[prevIdx];
  195. }
  196. }
  197. });