GroupTabPanel.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Ext JS Library 3.2.0
  3. * Copyright(c) 2006-2010 Ext JS, Inc.
  4. * licensing@extjs.com
  5. * http://www.extjs.com/license
  6. */
  7. Ext.ns('Ext.ux');
  8. Ext.ux.GroupTabPanel = Ext.extend(Ext.TabPanel, {
  9. tabPosition: 'left',
  10. alternateColor: false,
  11. alternateCls: 'x-grouptabs-panel-alt',
  12. defaultType: 'grouptab',
  13. deferredRender: false,
  14. activeGroup : null,
  15. initComponent: function(){
  16. Ext.ux.GroupTabPanel.superclass.initComponent.call(this);
  17. this.addEvents(
  18. 'beforegroupchange',
  19. 'groupchange'
  20. );
  21. this.elements = 'body,header';
  22. this.stripTarget = 'header';
  23. this.tabPosition = this.tabPosition == 'right' ? 'right' : 'left';
  24. this.addClass('x-grouptabs-panel');
  25. if (this.tabStyle && this.tabStyle != '') {
  26. this.addClass('x-grouptabs-panel-' + this.tabStyle);
  27. }
  28. if (this.alternateColor) {
  29. this.addClass(this.alternateCls);
  30. }
  31. this.on('beforeadd', function(gtp, item, index){
  32. this.initGroup(item, index);
  33. });
  34. },
  35. initEvents : function() {
  36. this.mon(this.strip, 'mousedown', this.onStripMouseDown, this);
  37. },
  38. onRender: function(ct, position){
  39. Ext.TabPanel.superclass.onRender.call(this, ct, position);
  40. if(this.plain){
  41. var pos = this.tabPosition == 'top' ? 'header' : 'footer';
  42. this[pos].addClass('x-tab-panel-'+pos+'-plain');
  43. }
  44. var st = this[this.stripTarget];
  45. this.stripWrap = st.createChild({cls:'x-tab-strip-wrap ', cn:{
  46. tag:'ul', cls:'x-grouptabs-strip x-grouptabs-tab-strip-'+this.tabPosition}});
  47. var beforeEl = (this.tabPosition=='bottom' ? this.stripWrap : null);
  48. this.strip = new Ext.Element(this.stripWrap.dom.firstChild);
  49. this.header.addClass('x-grouptabs-panel-header');
  50. this.bwrap.addClass('x-grouptabs-bwrap');
  51. this.body.addClass('x-tab-panel-body-'+this.tabPosition + ' x-grouptabs-panel-body');
  52. if (!this.groupTpl) {
  53. var tt = new Ext.Template(
  54. '<li class="{cls}" id="{id}">',
  55. '<a class="x-grouptabs-expand" onclick="return false;"></a>',
  56. '<a class="x-grouptabs-text {iconCls}" href="#" onclick="return false;">',
  57. '<span>{text}</span></a>',
  58. '</li>'
  59. );
  60. tt.disableFormats = true;
  61. tt.compile();
  62. Ext.ux.GroupTabPanel.prototype.groupTpl = tt;
  63. }
  64. this.items.each(this.initGroup, this);
  65. },
  66. afterRender: function(){
  67. Ext.ux.GroupTabPanel.superclass.afterRender.call(this);
  68. this.tabJoint = Ext.fly(this.body.dom.parentNode).createChild({
  69. cls: 'x-tab-joint'
  70. });
  71. this.addClass('x-tab-panel-' + this.tabPosition);
  72. this.header.setWidth(this.tabWidth);
  73. if (this.activeGroup !== undefined) {
  74. var group = (typeof this.activeGroup == 'object') ? this.activeGroup : this.items.get(this.activeGroup);
  75. delete this.activeGroup;
  76. this.setActiveGroup(group);
  77. group.setActiveTab(group.getMainItem());
  78. }
  79. },
  80. getGroupEl : Ext.TabPanel.prototype.getTabEl,
  81. // private
  82. findTargets: function(e){
  83. var item = null,
  84. itemEl = e.getTarget('li', this.strip);
  85. if (itemEl) {
  86. item = this.findById(itemEl.id.split(this.idDelimiter)[1]);
  87. if (item.disabled) {
  88. return {
  89. expand: null,
  90. item: null,
  91. el: null
  92. };
  93. }
  94. }
  95. return {
  96. expand: e.getTarget('.x-grouptabs-expand', this.strip),
  97. isGroup: !e.getTarget('ul.x-grouptabs-sub', this.strip),
  98. item: item,
  99. el: itemEl
  100. };
  101. },
  102. // private
  103. onStripMouseDown: function(e){
  104. if (e.button != 0) {
  105. return;
  106. }
  107. e.preventDefault();
  108. var t = this.findTargets(e);
  109. if (t.expand) {
  110. this.toggleGroup(t.el);
  111. }
  112. else if (t.item) {
  113. if(t.isGroup) {
  114. t.item.setActiveTab(t.item.getMainItem());
  115. }
  116. else {
  117. t.item.ownerCt.setActiveTab(t.item);
  118. }
  119. }
  120. },
  121. expandGroup: function(groupEl){
  122. if(groupEl.isXType) {
  123. groupEl = this.getGroupEl(groupEl);
  124. }
  125. Ext.fly(groupEl).addClass('x-grouptabs-expanded');
  126. this.syncTabJoint();
  127. },
  128. toggleGroup: function(groupEl){
  129. if(groupEl.isXType) {
  130. groupEl = this.getGroupEl(groupEl);
  131. }
  132. Ext.fly(groupEl).toggleClass('x-grouptabs-expanded');
  133. this.syncTabJoint();
  134. },
  135. collapseGroup: function(groupEl){
  136. if(groupEl.isXType) {
  137. groupEl = this.getGroupEl(groupEl);
  138. }
  139. Ext.fly(groupEl).removeClass('x-grouptabs-expanded');
  140. this.syncTabJoint();
  141. },
  142. syncTabJoint: function(groupEl){
  143. if (!this.tabJoint) {
  144. return;
  145. }
  146. groupEl = groupEl || this.getGroupEl(this.activeGroup);
  147. if(groupEl) {
  148. this.tabJoint.setHeight(Ext.fly(groupEl).getHeight() - 2);
  149. var y = Ext.isGecko2 ? 0 : 1;
  150. if (this.tabPosition == 'left'){
  151. this.tabJoint.alignTo(groupEl, 'tl-tr', [-2,y]);
  152. }
  153. else {
  154. this.tabJoint.alignTo(groupEl, 'tr-tl', [1,y]);
  155. }
  156. }
  157. else {
  158. this.tabJoint.hide();
  159. }
  160. },
  161. getActiveTab : function() {
  162. if(!this.activeGroup) return null;
  163. return this.activeGroup.getTabEl(this.activeGroup.activeTab) || null;
  164. },
  165. onResize: function(){
  166. Ext.ux.GroupTabPanel.superclass.onResize.apply(this, arguments);
  167. this.syncTabJoint();
  168. },
  169. createCorner: function(el, pos){
  170. return Ext.fly(el).createChild({
  171. cls: 'x-grouptabs-corner x-grouptabs-corner-' + pos
  172. });
  173. },
  174. initGroup: function(group, index){
  175. var before = this.strip.dom.childNodes[index],
  176. p = this.getTemplateArgs(group);
  177. if (index === 0) {
  178. p.cls += ' x-tab-first';
  179. }
  180. p.cls += ' x-grouptabs-main';
  181. p.text = group.getMainItem().title;
  182. var el = before ? this.groupTpl.insertBefore(before, p) : this.groupTpl.append(this.strip, p),
  183. tl = this.createCorner(el, 'top-' + this.tabPosition),
  184. bl = this.createCorner(el, 'bottom-' + this.tabPosition);
  185. group.tabEl = el;
  186. if (group.expanded) {
  187. this.expandGroup(el);
  188. }
  189. if (Ext.isIE6 || (Ext.isIE && !Ext.isStrict)){
  190. bl.setLeft('-10px');
  191. bl.setBottom('-5px');
  192. tl.setLeft('-10px');
  193. tl.setTop('-5px');
  194. }
  195. this.mon(group, {
  196. scope: this,
  197. changemainitem: this.onGroupChangeMainItem,
  198. beforetabchange: this.onGroupBeforeTabChange
  199. });
  200. },
  201. setActiveGroup : function(group) {
  202. group = this.getComponent(group);
  203. if(!group){
  204. return false;
  205. }
  206. if(!this.rendered){
  207. this.activeGroup = group;
  208. return true;
  209. }
  210. if(this.activeGroup != group && this.fireEvent('beforegroupchange', this, group, this.activeGroup) !== false){
  211. if(this.activeGroup){
  212. this.activeGroup.activeTab = null;
  213. var oldEl = this.getGroupEl(this.activeGroup);
  214. if(oldEl){
  215. Ext.fly(oldEl).removeClass('x-grouptabs-strip-active');
  216. }
  217. }
  218. var groupEl = this.getGroupEl(group);
  219. Ext.fly(groupEl).addClass('x-grouptabs-strip-active');
  220. this.activeGroup = group;
  221. this.stack.add(group);
  222. this.layout.setActiveItem(group);
  223. this.syncTabJoint(groupEl);
  224. this.fireEvent('groupchange', this, group);
  225. return true;
  226. }
  227. return false;
  228. },
  229. onGroupBeforeTabChange: function(group, newTab, oldTab){
  230. if(group !== this.activeGroup || newTab !== oldTab) {
  231. this.strip.select('.x-grouptabs-sub > li.x-grouptabs-strip-active', true).removeClass('x-grouptabs-strip-active');
  232. }
  233. this.expandGroup(this.getGroupEl(group));
  234. if(group !== this.activeGroup) {
  235. return this.setActiveGroup(group);
  236. }
  237. },
  238. getFrameHeight: function(){
  239. var h = this.el.getFrameWidth('tb');
  240. h += (this.tbar ? this.tbar.getHeight() : 0) +
  241. (this.bbar ? this.bbar.getHeight() : 0);
  242. return h;
  243. },
  244. adjustBodyWidth: function(w){
  245. return w - this.tabWidth;
  246. }
  247. });
  248. Ext.reg('grouptabpanel', Ext.ux.GroupTabPanel);