Tab.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. o2.widget = o2.widget || {};
  2. o2.widget.Tab = new Class({
  3. Implements: [Options, Events],
  4. Extends: o2.widget.Common,
  5. options: {
  6. "style": "default"
  7. },
  8. initialize: function(container, options){
  9. this.setOptions(options);
  10. this.pages = [];
  11. this.path = o2.session.path+"/widget/$Tab/";
  12. this.cssPath = o2.session.path+"/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.tabNodeContainerRight) this.tabNodeContainerRight = new Element("div.tabNodeContainerRight");
  24. this.tabNodeContainerRight.set("styles", this.css.tabNodeContainerRight);
  25. this.tabNodeContainerRight.inject(this.tabNodeContainer);
  26. if (!this.tabNodeContainerLeft) this.tabNodeContainerLeft = new Element("div.tabNodeContainerLeft");
  27. this.tabNodeContainerLeft.set("styles", this.css.tabNodeContainerLeft);
  28. this.tabNodeContainerLeft.inject(this.tabNodeContainer);
  29. if (!this.tabNodeContainerArea) this.tabNodeContainerArea = new Element("div.tabNodeContainerArea");
  30. this.tabNodeContainerArea.set("styles", this.css.tabNodeContainerArea);
  31. this.tabNodeContainerArea.inject(this.tabNodeContainerLeft);
  32. if (!this.contentNodeContainer) this.contentNodeContainer = new Element("div");
  33. this.contentNodeContainer.set("name", "MWFcontentNodeContainer");
  34. this.contentNodeContainer.set("styles", this.css.contentNodeContainer);
  35. this.contentNodeContainer.inject(this.node);
  36. this.tabNodeContainerRight.addEvents({
  37. "mouseover": function(){this.tabNodeContainerRight.setStyles(this.css.tabNodeContainerRight_over)}.bind(this),
  38. "mouseout": function(){this.tabNodeContainerRight.setStyles(this.css.tabNodeContainerRight)}.bind(this)
  39. });
  40. o2.require("o2.xDesktop.Menu", function(){
  41. this.tabMenu = new o2.xDesktop.Menu(this.tabNodeContainerRight, {
  42. "style": "tab",
  43. "event": "click",
  44. "container": this.node,
  45. "where": {"x": "right", "y": "bottom"},
  46. "onQueryShow": function(){
  47. this.loadOverMenu();
  48. }.bind(this)
  49. });
  50. this.tabMenu.load();
  51. }.bind(this));
  52. this.tabNodeContainerRight.hide();
  53. this.fireEvent("postLoad");
  54. }
  55. },
  56. rebuildTab: function(contentAreaNode,contentNode, pageNode){
  57. var tabPage = new o2.widget.TabPage(contentNode, "", this, {"isClose": false});
  58. tabPage.contentNodeArea = contentAreaNode;
  59. tabPage.tabNode = pageNode;
  60. tabPage.textNode = pageNode.getFirst();
  61. tabPage.closeNode = tabPage.textNode.getFirst();
  62. tabPage.options.title = tabPage.textNode.get("text");
  63. tabPage.load();
  64. this.pages.push(tabPage);
  65. return tabPage;
  66. },
  67. addTab: function(node, title, isclose, pageNode){
  68. var tabPage = new o2.widget.TabPage(node, title, this, {"isClose": isclose, "tabNode": pageNode});
  69. tabPage.load();
  70. this.pages.push(tabPage);
  71. return tabPage;
  72. },
  73. loadOverMenu: function(e){
  74. this.tabMenu.clearItems();
  75. var _self = this;
  76. for (var n=this.showTabIndex; n<this.pages.length; n++){
  77. var page = this.pages[n];
  78. var menuItem = this.tabMenu.addMenuItem(page.options.title, "click", function(){
  79. _self.showOverPage(this);
  80. });
  81. menuItem.tabPage = page;
  82. }
  83. },
  84. showOverPage: function(item){
  85. var page = item.tabPage;
  86. if (page){
  87. this.pages.erase(page);
  88. this.pages.unshift(page);
  89. page.tabNode.inject(this.tabNodeContainerArea, "top");
  90. page.showTabIm();
  91. this.resize();
  92. }
  93. },
  94. resize: function(){
  95. var size = this.tabNodeContainerLeft.getSize();
  96. var tabWidth = 0;
  97. for (var i=0; i<this.pages.length; i++){
  98. var tabSize = this.pages[i].tabNode.getSize();
  99. tabWidth += tabSize.x;
  100. if (tabWidth>size.x) break;
  101. }
  102. this.showTabIndex = i;
  103. if (tabWidth>size.x && i<this.pages.length){
  104. this.tabNodeContainerRight.show();
  105. }else{
  106. this.tabNodeContainerRight.hide();
  107. }
  108. }
  109. });
  110. o2.widget.TabPage = new Class({
  111. Implements: [Options, Events],
  112. options: {
  113. "isClose": true,
  114. "tabNode": null,
  115. "title": ""
  116. },
  117. initialize: function(node, title, tab, options){
  118. this.setOptions(options);
  119. this.options.title = title;
  120. this.tab = tab;
  121. this.contentNode = $(node);
  122. },
  123. load: function(){
  124. if (!this.contentNodeArea) this.contentNodeArea = new Element("div");
  125. this.contentNodeArea.set("styles", this.tab.css.contentNodeArea);
  126. if (!this.tabNode) this.tabNode = new Element("div");
  127. this.tabNode.set("styles", this.tab.css.tabNode);
  128. this.tabNode.addEvent("mousedown", function(event){event.stop();});
  129. if (!this.textNode) this.textNode = new Element("div").inject(this.tabNode);
  130. this.textNode.set({
  131. "styles": this.tab.css.tabTextNode,
  132. "text": this.options.title
  133. });
  134. this.tabNode.addEvent("click", this._showTab.bind(this));
  135. if (this.options.isClose){
  136. if (!this.closeNode) this.closeNode = new Element("div").inject(this.tabNode);
  137. this.closeNode.set({
  138. "styles": this.tab.css.tabCloseNode
  139. });
  140. this.closeNode.addEvent("click", this.closeTab.bind(this));
  141. }
  142. this.contentNodeArea.inject(this.tab.contentNodeContainer);
  143. this.tabNode.inject(this.tab.tabNodeContainerArea);
  144. this.contentNode.inject(this.contentNodeArea);
  145. this.tab.resize();
  146. },
  147. _showTab: function(){
  148. this.showTabIm();
  149. },
  150. showTabIm: function(callback){
  151. if (!this.isShow){
  152. // if (!this.tabNode.isIntoView()){
  153. // this.tab.pages.erase(this);
  154. // this.tab.pages.unshift(this);
  155. // this.tabNode.inject(this.tab.tabNodeContainerArea, "top");
  156. // this.tab.resize();
  157. // }
  158. this.tab.pages.each(function(page){
  159. if (page.isShow) page.hideIm();
  160. });
  161. this.showIm(callback);
  162. }
  163. },
  164. showTab: function(callback){
  165. if (!this.isShow){
  166. this.tab.pages.each(function(page){
  167. if (page.isShow) page.hide();
  168. });
  169. this.show(callback);
  170. }
  171. },
  172. showIm: function(callback){
  173. this.fireEvent("queryShow");
  174. this.tabNode.set("styles", this.tab.css.tabNodeCurrent);
  175. this.textNode.set("styles", this.tab.css.tabTextNodeCurrent);
  176. if (this.closeNode) this.closeNode.set("styles", this.tab.css.tabCloseNodeCurrent);
  177. this.contentNodeArea.setStyle("display", "block");
  178. this.contentNodeArea.setStyle("opacity", 1);
  179. this.isShow = true;
  180. this.tab.showPage = this;
  181. if (callback) callback();
  182. this.fireEvent("show");
  183. this.fireEvent("postShow");
  184. },
  185. show: function(callback){
  186. this.fireEvent("queryShow");
  187. this.tabNode.set("styles", this.tab.css.tabNodeCurrent);
  188. this.textNode.set("styles", this.tab.css.tabTextNodeCurrent);
  189. if (this.closeNode) this.closeNode.set("styles", this.tab.css.tabCloseNodeCurrent);
  190. this.contentNodeArea.setStyle("display", "block");
  191. this.contentNodeArea.setStyle("opacity", 1);
  192. if (!this.morph){
  193. this.morph = new Fx.Morph(this.contentNodeArea, {duration: 100});
  194. }
  195. this.morph.start({
  196. "opacity": 1
  197. }).chain(function(){
  198. this.isShow = true;
  199. this.tab.showPage = this;
  200. if (callback) callback();
  201. this.fireEvent("postShow");
  202. }.bind(this));
  203. this.fireEvent("show");
  204. },
  205. hideIm: function(){
  206. if (this.isShow){
  207. this.fireEvent("queryHide");
  208. this.tabNode.set("styles", this.tab.css.tabNode);
  209. this.textNode.set("styles", this.tab.css.tabTextNode);
  210. if (this.closeNode) this.closeNode.set("styles", this.tab.css.tabCloseNode);
  211. this.contentNodeArea.setStyle("display", "none");
  212. this.contentNodeArea.setStyle("opacity", 0);
  213. this.isShow = false;
  214. this.fireEvent("hide");
  215. this.fireEvent("postHide");
  216. }
  217. },
  218. hide: function(){
  219. if (this.isShow){
  220. this.fireEvent("queryHide");
  221. this.tabNode.set("styles", this.tab.css.tabNode);
  222. this.textNode.set("styles", this.tab.css.tabTextNode);
  223. if (this.closeNode) this.closeNode.set("styles", this.tab.css.tabCloseNode);
  224. if (!this.morph){
  225. this.morph = new Fx.Morph(this.contentNodeArea, {duration: 100});
  226. }
  227. this.morph.start({
  228. "opacity": 0
  229. }).chain(function(){
  230. this.contentNodeArea.setStyle("display", "none");
  231. this.isShow = false;
  232. this.fireEvent("postHide");
  233. }.bind(this));
  234. this.fireEvent("hide");
  235. }
  236. },
  237. closeTab: function(){
  238. this.fireEvent("queryClose");
  239. var prevPage = this.getPrevPage();
  240. this.contentNodeArea.destroy();
  241. this.tabNode.destroy();
  242. var tmp = [];
  243. this.tab.pages = this.tab.pages.erase(this);
  244. // this.tab.pages.each(function(item){
  245. // if (item!=this){
  246. // tmp.push(item);
  247. // }
  248. // });
  249. // this.tab.pages = tmp;
  250. if (prevPage){
  251. prevPage.showTab();
  252. }else{
  253. if (this.tab.pages.length) this.tab.pages[this.tab.pages.length-1].showTab();
  254. }
  255. this.fireEvent("close");
  256. },
  257. getPrevPage: function(){
  258. var idx = this.tab.pages.indexOf(this);
  259. var prevIdx = idx-1;
  260. if (prevIdx<0){
  261. return null;
  262. }else{
  263. return this.tab.pages[prevIdx];
  264. }
  265. }
  266. });