GroupTabPanel.js
上传用户:shuoshiled
上传日期:2018-01-28
资源大小:10124k
文件大小:9k
源码类别:

中间件编程

开发平台:

JavaScript

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