Tab.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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.tabNodeContainerRight,
  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 || page.tabNode.get("text"), "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.setStyle("display","");
  175. this.tabNode.set("styles", this.tab.css.tabNodeCurrent);
  176. this.textNode.set("styles", this.tab.css.tabTextNodeCurrent);
  177. if (this.closeNode) this.closeNode.set("styles", this.tab.css.tabCloseNodeCurrent);
  178. this.contentNodeArea.setStyle("display", "block");
  179. this.contentNodeArea.setStyle("opacity", 1);
  180. this.isShow = true;
  181. this.tab.showPage = this;
  182. if (callback) callback();
  183. this.fireEvent("show");
  184. this.fireEvent("postShow");
  185. },
  186. show: function(callback){
  187. this.fireEvent("queryShow");
  188. this.tabNode.setStyle("display","");
  189. this.tabNode.set("styles", this.tab.css.tabNodeCurrent);
  190. this.textNode.set("styles", this.tab.css.tabTextNodeCurrent);
  191. if (this.closeNode) this.closeNode.set("styles", this.tab.css.tabCloseNodeCurrent);
  192. this.contentNodeArea.setStyle("display", "block");
  193. this.contentNodeArea.setStyle("opacity", 1);
  194. if (!this.morph){
  195. this.morph = new Fx.Morph(this.contentNodeArea, {duration: 100});
  196. }
  197. this.morph.start({
  198. "opacity": 1
  199. }).chain(function(){
  200. this.isShow = true;
  201. this.tab.showPage = this;
  202. if (callback) callback();
  203. this.fireEvent("postShow");
  204. }.bind(this));
  205. this.fireEvent("show");
  206. },
  207. hideIm: function(){
  208. if (this.isShow){
  209. this.fireEvent("queryHide");
  210. this.tabNode.set("styles", this.tab.css.tabNode);
  211. this.textNode.set("styles", this.tab.css.tabTextNode);
  212. if (this.closeNode) this.closeNode.set("styles", this.tab.css.tabCloseNode);
  213. this.contentNodeArea.setStyle("display", "none");
  214. this.contentNodeArea.setStyle("opacity", 0);
  215. this.isShow = false;
  216. this.fireEvent("hide");
  217. this.fireEvent("postHide");
  218. }
  219. },
  220. hide: function(){
  221. if (this.isShow){
  222. this.fireEvent("queryHide");
  223. this.tabNode.set("styles", this.tab.css.tabNode);
  224. this.textNode.set("styles", this.tab.css.tabTextNode);
  225. if (this.closeNode) this.closeNode.set("styles", this.tab.css.tabCloseNode);
  226. if (!this.morph){
  227. this.morph = new Fx.Morph(this.contentNodeArea, {duration: 100});
  228. }
  229. this.morph.start({
  230. "opacity": 0
  231. }).chain(function(){
  232. this.contentNodeArea.setStyle("display", "none");
  233. this.isShow = false;
  234. this.fireEvent("postHide");
  235. }.bind(this));
  236. this.fireEvent("hide");
  237. }
  238. },
  239. enableTab : function(){
  240. this.disabled = false;
  241. this.showTab();
  242. },
  243. disableTab : function( notShowSibling ){
  244. this.disabled = true;
  245. this.hideTab( notShowSibling );
  246. },
  247. hideTab: function( notShowSibling ){
  248. this.fireEvent("queryHide");
  249. this.tabNode.hide();
  250. this.contentNodeArea.hide();
  251. var tmp = [];
  252. if( !notShowSibling ){
  253. var prevPage = this.getPrevPage();
  254. if (prevPage && !prevPage.disabled){
  255. prevPage.showTab();
  256. }else{
  257. if (this.tab.pages.length){
  258. for( var i=0; i<this.tab.pages.length; i++ ){
  259. var page = this.tab.pages[i];
  260. if( !page.disabled ){
  261. page.showTab();
  262. break;
  263. }
  264. }
  265. //this.tab.pages[this.tab.pages.length-1].showTab();
  266. }
  267. }
  268. }
  269. this.isShow = false;
  270. this.fireEvent("hide");
  271. },
  272. closeTab: function(){
  273. this.fireEvent("queryClose");
  274. var prevPage = this.getPrevPage();
  275. this.contentNodeArea.destroy();
  276. this.tabNode.destroy();
  277. var tmp = [];
  278. this.tab.pages = this.tab.pages.erase(this);
  279. // this.tab.pages.each(function(item){
  280. // if (item!=this){
  281. // tmp.push(item);
  282. // }
  283. // });
  284. // this.tab.pages = tmp;
  285. if (prevPage && !prevPage.disabled){
  286. prevPage.showTab();
  287. }else{
  288. if (this.tab.pages.length){
  289. for( var i=0; i<this.tab.pages.length; i++ ){
  290. var page = this.tab.pages[i];
  291. if( !page.disabled ){
  292. page.showTab();
  293. break;
  294. }
  295. }
  296. }
  297. //if (this.tab.pages.length) this.tab.pages[this.tab.pages.length-1].showTab();
  298. }
  299. this.fireEvent("close");
  300. },
  301. getPrevPage: function(){
  302. var idx = this.tab.pages.indexOf(this);
  303. var prevIdx = idx-1;
  304. if (prevIdx<0){
  305. return null;
  306. }else{
  307. return this.tab.pages[prevIdx];
  308. }
  309. }
  310. });