Forked mumble-django project from https://bitbucket.org/Svedrin/mumble-django
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1131 lines
40 KiB

  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. /**
  8. * @class Ext.TabPanel
  9. * <p>A basic tab container. TabPanels can be used exactly like a standard {@link Ext.Panel}
  10. * for layout purposes, but also have special support for containing child Components
  11. * (<tt>{@link Ext.Container#items items}</tt>) that are managed using a
  12. * {@link Ext.layout.CardLayout CardLayout layout manager}, and displayed as separate tabs.</p>
  13. *
  14. * <b>Note:</b> By default, a tab's close tool <i>destroys</i> the child tab Component
  15. * and all its descendants. This makes the child tab Component, and all its descendants <b>unusable</b>. To enable
  16. * re-use of a tab, configure the TabPanel with <b><code>{@link #autoDestroy autoDestroy: false}</code></b>.
  17. *
  18. * <p><b><u>TabPanel header/footer elements</u></b></p>
  19. * <p>TabPanels use their {@link Ext.Panel#header header} or {@link Ext.Panel#footer footer} element
  20. * (depending on the {@link #tabPosition} configuration) to accommodate the tab selector buttons.
  21. * This means that a TabPanel will not display any configured title, and will not display any
  22. * configured header {@link Ext.Panel#tools tools}.</p>
  23. * <p>To display a header, embed the TabPanel in a {@link Ext.Panel Panel} which uses
  24. * <b><tt>{@link Ext.Container#layout layout:'fit'}</tt></b>.</p>
  25. *
  26. * <p><b><u>Tab Events</u></b></p>
  27. * <p>There is no actual tab class &mdash; each tab is simply a {@link Ext.BoxComponent Component}
  28. * such as a {@link Ext.Panel Panel}. However, when rendered in a TabPanel, each child Component
  29. * can fire additional events that only exist for tabs and are not available from other Components.
  30. * These events are:</p>
  31. * <div><ul class="mdetail-params">
  32. * <li><tt><b>{@link Ext.Panel#activate activate}</b></tt> : Fires when this Component becomes
  33. * the active tab.</li>
  34. * <li><tt><b>{@link Ext.Panel#deactivate deactivate}</b></tt> : Fires when the Component that
  35. * was the active tab becomes deactivated.</li>
  36. * <li><tt><b>{@link Ext.Panel#beforeclose beforeclose}</b></tt> : Fires when the user clicks on the close tool of a closeable tab.
  37. * May be vetoed by returning <code>false</code> from a handler.</li>
  38. * <li><tt><b>{@link Ext.Panel#close close}</b></tt> : Fires a closeable tab has been closed by the user.</li>
  39. * </ul></div>
  40. * <p><b><u>Creating TabPanels from Code</u></b></p>
  41. * <p>TabPanels can be created and rendered completely in code, as in this example:</p>
  42. * <pre><code>
  43. var tabs = new Ext.TabPanel({
  44. renderTo: Ext.getBody(),
  45. activeTab: 0,
  46. items: [{
  47. title: 'Tab 1',
  48. html: 'A simple tab'
  49. },{
  50. title: 'Tab 2',
  51. html: 'Another one'
  52. }]
  53. });
  54. </code></pre>
  55. * <p><b><u>Creating TabPanels from Existing Markup</u></b></p>
  56. * <p>TabPanels can also be rendered from pre-existing markup in a couple of ways.</p>
  57. * <div><ul class="mdetail-params">
  58. *
  59. * <li>Pre-Structured Markup</li>
  60. * <div class="sub-desc">
  61. * <p>A container div with one or more nested tab divs with class <tt>'x-tab'</tt> can be rendered entirely
  62. * from existing markup (See the {@link #autoTabs} example).</p>
  63. * </div>
  64. *
  65. * <li>Un-Structured Markup</li>
  66. * <div class="sub-desc">
  67. * <p>A TabPanel can also be rendered from markup that is not strictly structured by simply specifying by id
  68. * which elements should be the container and the tabs. Using this method tab content can be pulled from different
  69. * elements within the page by id regardless of page structure. For example:</p>
  70. * <pre><code>
  71. var tabs = new Ext.TabPanel({
  72. renderTo: 'my-tabs',
  73. activeTab: 0,
  74. items:[
  75. {contentEl:'tab1', title:'Tab 1'},
  76. {contentEl:'tab2', title:'Tab 2'}
  77. ]
  78. });
  79. // Note that the tabs do not have to be nested within the container (although they can be)
  80. &lt;div id="my-tabs">&lt;/div>
  81. &lt;div id="tab1" class="x-hide-display">A simple tab&lt;/div>
  82. &lt;div id="tab2" class="x-hide-display">Another one&lt;/div>
  83. </code></pre>
  84. * Note that the tab divs in this example contain the class <tt>'x-hide-display'</tt> so that they can be rendered
  85. * deferred without displaying outside the tabs. You could alternately set <tt>{@link #deferredRender} = false </tt>
  86. * to render all content tabs on page load.
  87. * </div>
  88. *
  89. * </ul></div>
  90. *
  91. * @extends Ext.Panel
  92. * @constructor
  93. * @param {Object} config The configuration options
  94. * @xtype tabpanel
  95. */
  96. Ext.TabPanel = Ext.extend(Ext.Panel, {
  97. /**
  98. * @cfg {Boolean} layoutOnTabChange
  99. * Set to true to force a layout of the active tab when the tab is changed. Defaults to false.
  100. * See {@link Ext.layout.CardLayout}.<code>{@link Ext.layout.CardLayout#layoutOnCardChange layoutOnCardChange}</code>.
  101. */
  102. /**
  103. * @cfg {String} tabCls <b>This config option is used on <u>child Components</u> of ths TabPanel.</b> A CSS
  104. * class name applied to the tab strip item representing the child Component, allowing special
  105. * styling to be applied.
  106. */
  107. /**
  108. * @cfg {Boolean} deferredRender
  109. * <p><tt>true</tt> by default to defer the rendering of child <tt>{@link Ext.Container#items items}</tt>
  110. * to the browsers DOM until a tab is activated. <tt>false</tt> will render all contained
  111. * <tt>{@link Ext.Container#items items}</tt> as soon as the {@link Ext.layout.CardLayout layout}
  112. * is rendered. If there is a significant amount of content or a lot of heavy controls being
  113. * rendered into panels that are not displayed by default, setting this to <tt>true</tt> might
  114. * improve performance.</p>
  115. * <br><p>The <tt>deferredRender</tt> property is internally passed to the layout manager for
  116. * TabPanels ({@link Ext.layout.CardLayout}) as its {@link Ext.layout.CardLayout#deferredRender}
  117. * configuration value.</p>
  118. * <br><p><b>Note</b>: leaving <tt>deferredRender</tt> as <tt>true</tt> means that the content
  119. * within an unactivated tab will not be available. For example, this means that if the TabPanel
  120. * is within a {@link Ext.form.FormPanel form}, then until a tab is activated, any Fields within
  121. * unactivated tabs will not be rendered, and will therefore not be submitted and will not be
  122. * available to either {@link Ext.form.BasicForm#getValues getValues} or
  123. * {@link Ext.form.BasicForm#setValues setValues}.</p>
  124. */
  125. deferredRender : true,
  126. /**
  127. * @cfg {Number} tabWidth The initial width in pixels of each new tab (defaults to 120).
  128. */
  129. tabWidth : 120,
  130. /**
  131. * @cfg {Number} minTabWidth The minimum width in pixels for each tab when {@link #resizeTabs} = true (defaults to 30).
  132. */
  133. minTabWidth : 30,
  134. /**
  135. * @cfg {Boolean} resizeTabs True to automatically resize each tab so that the tabs will completely fill the
  136. * tab strip (defaults to false). Setting this to true may cause specific widths that might be set per tab to
  137. * be overridden in order to fit them all into view (although {@link #minTabWidth} will always be honored).
  138. */
  139. resizeTabs : false,
  140. /**
  141. * @cfg {Boolean} enableTabScroll True to enable scrolling to tabs that may be invisible due to overflowing the
  142. * overall TabPanel width. Only available with tabPosition:'top' (defaults to false).
  143. */
  144. enableTabScroll : false,
  145. /**
  146. * @cfg {Number} scrollIncrement The number of pixels to scroll each time a tab scroll button is pressed
  147. * (defaults to <tt>100</tt>, or if <tt>{@link #resizeTabs} = true</tt>, the calculated tab width). Only
  148. * applies when <tt>{@link #enableTabScroll} = true</tt>.
  149. */
  150. scrollIncrement : 0,
  151. /**
  152. * @cfg {Number} scrollRepeatInterval Number of milliseconds between each scroll while a tab scroll button is
  153. * continuously pressed (defaults to <tt>400</tt>).
  154. */
  155. scrollRepeatInterval : 400,
  156. /**
  157. * @cfg {Float} scrollDuration The number of milliseconds that each scroll animation should last (defaults
  158. * to <tt>.35</tt>). Only applies when <tt>{@link #animScroll} = true</tt>.
  159. */
  160. scrollDuration : 0.35,
  161. /**
  162. * @cfg {Boolean} animScroll True to animate tab scrolling so that hidden tabs slide smoothly into view (defaults
  163. * to <tt>true</tt>). Only applies when <tt>{@link #enableTabScroll} = true</tt>.
  164. */
  165. animScroll : true,
  166. /**
  167. * @cfg {String} tabPosition The position where the tab strip should be rendered (defaults to <tt>'top'</tt>).
  168. * The only other supported value is <tt>'bottom'</tt>. <b>Note</b>: tab scrolling is only supported for
  169. * <tt>tabPosition: 'top'</tt>.
  170. */
  171. tabPosition : 'top',
  172. /**
  173. * @cfg {String} baseCls The base CSS class applied to the panel (defaults to <tt>'x-tab-panel'</tt>).
  174. */
  175. baseCls : 'x-tab-panel',
  176. /**
  177. * @cfg {Boolean} autoTabs
  178. * <p><tt>true</tt> to query the DOM for any divs with a class of 'x-tab' to be automatically converted
  179. * to tabs and added to this panel (defaults to <tt>false</tt>). Note that the query will be executed within
  180. * the scope of the container element only (so that multiple tab panels from markup can be supported via this
  181. * method).</p>
  182. * <p>This method is only possible when the markup is structured correctly as a container with nested divs
  183. * containing the class <tt>'x-tab'</tt>. To create TabPanels without these limitations, or to pull tab content
  184. * from other elements on the page, see the example at the top of the class for generating tabs from markup.</p>
  185. * <p>There are a couple of things to note when using this method:<ul>
  186. * <li>When using the <tt>autoTabs</tt> config (as opposed to passing individual tab configs in the TabPanel's
  187. * {@link #items} collection), you must use <tt>{@link #applyTo}</tt> to correctly use the specified <tt>id</tt>
  188. * as the tab container. The <tt>autoTabs</tt> method <em>replaces</em> existing content with the TabPanel
  189. * components.</li>
  190. * <li>Make sure that you set <tt>{@link #deferredRender}: false</tt> so that the content elements for each
  191. * tab will be rendered into the TabPanel immediately upon page load, otherwise they will not be transformed
  192. * until each tab is activated and will be visible outside the TabPanel.</li>
  193. * </ul>Example usage:</p>
  194. * <pre><code>
  195. var tabs = new Ext.TabPanel({
  196. applyTo: 'my-tabs',
  197. activeTab: 0,
  198. deferredRender: false,
  199. autoTabs: true
  200. });
  201. // This markup will be converted to a TabPanel from the code above
  202. &lt;div id="my-tabs">
  203. &lt;div class="x-tab" title="Tab 1">A simple tab&lt;/div>
  204. &lt;div class="x-tab" title="Tab 2">Another one&lt;/div>
  205. &lt;/div>
  206. </code></pre>
  207. */
  208. autoTabs : false,
  209. /**
  210. * @cfg {String} autoTabSelector The CSS selector used to search for tabs in existing markup when
  211. * <tt>{@link #autoTabs} = true</tt> (defaults to <tt>'div.x-tab'</tt>). This can be any valid selector
  212. * supported by {@link Ext.DomQuery#select}. Note that the query will be executed within the scope of this
  213. * tab panel only (so that multiple tab panels from markup can be supported on a page).
  214. */
  215. autoTabSelector : 'div.x-tab',
  216. /**
  217. * @cfg {String/Number} activeTab A string id or the numeric index of the tab that should be initially
  218. * activated on render (defaults to undefined).
  219. */
  220. activeTab : undefined,
  221. /**
  222. * @cfg {Number} tabMargin The number of pixels of space to calculate into the sizing and scrolling of
  223. * tabs. If you change the margin in CSS, you will need to update this value so calculations are correct
  224. * with either <tt>{@link #resizeTabs}</tt> or scrolling tabs. (defaults to <tt>2</tt>)
  225. */
  226. tabMargin : 2,
  227. /**
  228. * @cfg {Boolean} plain </tt>true</tt> to render the tab strip without a background container image
  229. * (defaults to <tt>false</tt>).
  230. */
  231. plain : false,
  232. /**
  233. * @cfg {Number} wheelIncrement For scrolling tabs, the number of pixels to increment on mouse wheel
  234. * scrolling (defaults to <tt>20</tt>).
  235. */
  236. wheelIncrement : 20,
  237. /*
  238. * This is a protected property used when concatenating tab ids to the TabPanel id for internal uniqueness.
  239. * It does not generally need to be changed, but can be if external code also uses an id scheme that can
  240. * potentially clash with this one.
  241. */
  242. idDelimiter : '__',
  243. // private
  244. itemCls : 'x-tab-item',
  245. // private config overrides
  246. elements : 'body',
  247. headerAsText : false,
  248. frame : false,
  249. hideBorders :true,
  250. // private
  251. initComponent : function(){
  252. this.frame = false;
  253. Ext.TabPanel.superclass.initComponent.call(this);
  254. this.addEvents(
  255. /**
  256. * @event beforetabchange
  257. * Fires before the active tab changes. Handlers can <tt>return false</tt> to cancel the tab change.
  258. * @param {TabPanel} this
  259. * @param {Panel} newTab The tab being activated
  260. * @param {Panel} currentTab The current active tab
  261. */
  262. 'beforetabchange',
  263. /**
  264. * @event tabchange
  265. * Fires after the active tab has changed.
  266. * @param {TabPanel} this
  267. * @param {Panel} tab The new active tab
  268. */
  269. 'tabchange',
  270. /**
  271. * @event contextmenu
  272. * Relays the contextmenu event from a tab selector element in the tab strip.
  273. * @param {TabPanel} this
  274. * @param {Panel} tab The target tab
  275. * @param {EventObject} e
  276. */
  277. 'contextmenu'
  278. );
  279. /**
  280. * @cfg {Object} layoutConfig
  281. * TabPanel implicitly uses {@link Ext.layout.CardLayout} as its layout manager.
  282. * <code>layoutConfig</code> may be used to configure this layout manager.
  283. * <code>{@link #deferredRender}</code> and <code>{@link #layoutOnTabChange}</code>
  284. * configured on the TabPanel will be applied as configs to the layout manager.
  285. */
  286. this.setLayout(new Ext.layout.CardLayout(Ext.apply({
  287. layoutOnCardChange: this.layoutOnTabChange,
  288. deferredRender: this.deferredRender
  289. }, this.layoutConfig)));
  290. if(this.tabPosition == 'top'){
  291. this.elements += ',header';
  292. this.stripTarget = 'header';
  293. }else {
  294. this.elements += ',footer';
  295. this.stripTarget = 'footer';
  296. }
  297. if(!this.stack){
  298. this.stack = Ext.TabPanel.AccessStack();
  299. }
  300. this.initItems();
  301. },
  302. // private
  303. onRender : function(ct, position){
  304. Ext.TabPanel.superclass.onRender.call(this, ct, position);
  305. if(this.plain){
  306. var pos = this.tabPosition == 'top' ? 'header' : 'footer';
  307. this[pos].addClass('x-tab-panel-'+pos+'-plain');
  308. }
  309. var st = this[this.stripTarget];
  310. this.stripWrap = st.createChild({cls:'x-tab-strip-wrap', cn:{
  311. tag:'ul', cls:'x-tab-strip x-tab-strip-'+this.tabPosition}});
  312. var beforeEl = (this.tabPosition=='bottom' ? this.stripWrap : null);
  313. st.createChild({cls:'x-tab-strip-spacer'}, beforeEl);
  314. this.strip = new Ext.Element(this.stripWrap.dom.firstChild);
  315. // create an empty span with class x-tab-strip-text to force the height of the header element when there's no tabs.
  316. this.edge = this.strip.createChild({tag:'li', cls:'x-tab-edge', cn: [{tag: 'span', cls: 'x-tab-strip-text', cn: '&#160;'}]});
  317. this.strip.createChild({cls:'x-clear'});
  318. this.body.addClass('x-tab-panel-body-'+this.tabPosition);
  319. /**
  320. * @cfg {Template/XTemplate} itemTpl <p>(Optional) A {@link Ext.Template Template} or
  321. * {@link Ext.XTemplate XTemplate} which may be provided to process the data object returned from
  322. * <tt>{@link #getTemplateArgs}</tt> to produce a clickable selector element in the tab strip.</p>
  323. * <p>The main element created should be a <tt>&lt;li></tt> element. In order for a click event on
  324. * a selector element to be connected to its item, it must take its <i>id</i> from the TabPanel's
  325. * native <tt>{@link #getTemplateArgs}</tt>.</p>
  326. * <p>The child element which contains the title text must be marked by the CSS class
  327. * <tt>x-tab-strip-inner</tt>.</p>
  328. * <p>To enable closability, the created element should contain an element marked by the CSS class
  329. * <tt>x-tab-strip-close</tt>.</p>
  330. * <p>If a custom <tt>itemTpl</tt> is supplied, it is the developer's responsibility to create CSS
  331. * style rules to create the desired appearance.</p>
  332. * Below is an example of how to create customized tab selector items:<pre><code>
  333. new Ext.TabPanel({
  334. renderTo: document.body,
  335. minTabWidth: 115,
  336. tabWidth: 135,
  337. enableTabScroll: true,
  338. width: 600,
  339. height: 250,
  340. defaults: {autoScroll:true},
  341. itemTpl: new Ext.XTemplate(
  342. '&lt;li class="{cls}" id="{id}" style="overflow:hidden">',
  343. '&lt;tpl if="closable">',
  344. '&lt;a class="x-tab-strip-close">&lt;/a>',
  345. '&lt;/tpl>',
  346. '&lt;a class="x-tab-right" href="#" style="padding-left:6px">',
  347. '&lt;em class="x-tab-left">',
  348. '&lt;span class="x-tab-strip-inner">',
  349. '&lt;img src="{src}" style="float:left;margin:3px 3px 0 0">',
  350. '&lt;span style="margin-left:20px" class="x-tab-strip-text {iconCls}">{text} {extra}&lt;/span>',
  351. '&lt;/span>',
  352. '&lt;/em>',
  353. '&lt;/a>',
  354. '&lt;/li>'
  355. ),
  356. getTemplateArgs: function(item) {
  357. // Call the native method to collect the base data. Like the ID!
  358. var result = Ext.TabPanel.prototype.getTemplateArgs.call(this, item);
  359. // Add stuff used in our template
  360. return Ext.apply(result, {
  361. closable: item.closable,
  362. src: item.iconSrc,
  363. extra: item.extraText || ''
  364. });
  365. },
  366. items: [{
  367. title: 'New Tab 1',
  368. iconSrc: '../shared/icons/fam/grid.png',
  369. html: 'Tab Body 1',
  370. closable: true
  371. }, {
  372. title: 'New Tab 2',
  373. iconSrc: '../shared/icons/fam/grid.png',
  374. html: 'Tab Body 2',
  375. extraText: 'Extra stuff in the tab button'
  376. }]
  377. });
  378. </code></pre>
  379. */
  380. if(!this.itemTpl){
  381. var tt = new Ext.Template(
  382. '<li class="{cls}" id="{id}"><a class="x-tab-strip-close"></a>',
  383. '<a class="x-tab-right" href="#"><em class="x-tab-left">',
  384. '<span class="x-tab-strip-inner"><span class="x-tab-strip-text {iconCls}">{text}</span></span>',
  385. '</em></a></li>'
  386. );
  387. tt.disableFormats = true;
  388. tt.compile();
  389. Ext.TabPanel.prototype.itemTpl = tt;
  390. }
  391. this.items.each(this.initTab, this);
  392. },
  393. // private
  394. afterRender : function(){
  395. Ext.TabPanel.superclass.afterRender.call(this);
  396. if(this.autoTabs){
  397. this.readTabs(false);
  398. }
  399. if(this.activeTab !== undefined){
  400. var item = Ext.isObject(this.activeTab) ? this.activeTab : this.items.get(this.activeTab);
  401. delete this.activeTab;
  402. this.setActiveTab(item);
  403. }
  404. },
  405. // private
  406. initEvents : function(){
  407. Ext.TabPanel.superclass.initEvents.call(this);
  408. this.mon(this.strip, {
  409. scope: this,
  410. mousedown: this.onStripMouseDown,
  411. contextmenu: this.onStripContextMenu
  412. });
  413. if(this.enableTabScroll){
  414. this.mon(this.strip, 'mousewheel', this.onWheel, this);
  415. }
  416. },
  417. // private
  418. findTargets : function(e){
  419. var item = null,
  420. itemEl = e.getTarget('li:not(.x-tab-edge)', this.strip);
  421. if(itemEl){
  422. item = this.getComponent(itemEl.id.split(this.idDelimiter)[1]);
  423. if(item.disabled){
  424. return {
  425. close : null,
  426. item : null,
  427. el : null
  428. };
  429. }
  430. }
  431. return {
  432. close : e.getTarget('.x-tab-strip-close', this.strip),
  433. item : item,
  434. el : itemEl
  435. };
  436. },
  437. // private
  438. onStripMouseDown : function(e){
  439. if(e.button !== 0){
  440. return;
  441. }
  442. e.preventDefault();
  443. var t = this.findTargets(e);
  444. if(t.close){
  445. if (t.item.fireEvent('beforeclose', t.item) !== false) {
  446. t.item.fireEvent('close', t.item);
  447. this.remove(t.item);
  448. }
  449. return;
  450. }
  451. if(t.item && t.item != this.activeTab){
  452. this.setActiveTab(t.item);
  453. }
  454. },
  455. // private
  456. onStripContextMenu : function(e){
  457. e.preventDefault();
  458. var t = this.findTargets(e);
  459. if(t.item){
  460. this.fireEvent('contextmenu', this, t.item, e);
  461. }
  462. },
  463. /**
  464. * True to scan the markup in this tab panel for <tt>{@link #autoTabs}</tt> using the
  465. * <tt>{@link #autoTabSelector}</tt>
  466. * @param {Boolean} removeExisting True to remove existing tabs
  467. */
  468. readTabs : function(removeExisting){
  469. if(removeExisting === true){
  470. this.items.each(function(item){
  471. this.remove(item);
  472. }, this);
  473. }
  474. var tabs = this.el.query(this.autoTabSelector);
  475. for(var i = 0, len = tabs.length; i < len; i++){
  476. var tab = tabs[i],
  477. title = tab.getAttribute('title');
  478. tab.removeAttribute('title');
  479. this.add({
  480. title: title,
  481. contentEl: tab
  482. });
  483. }
  484. },
  485. // private
  486. initTab : function(item, index){
  487. var before = this.strip.dom.childNodes[index],
  488. p = this.getTemplateArgs(item),
  489. el = before ?
  490. this.itemTpl.insertBefore(before, p) :
  491. this.itemTpl.append(this.strip, p),
  492. cls = 'x-tab-strip-over',
  493. tabEl = Ext.get(el);
  494. tabEl.hover(function(){
  495. if(!item.disabled){
  496. tabEl.addClass(cls);
  497. }
  498. }, function(){
  499. tabEl.removeClass(cls);
  500. });
  501. if(item.tabTip){
  502. tabEl.child('span.x-tab-strip-text', true).qtip = item.tabTip;
  503. }
  504. item.tabEl = el;
  505. // Route *keyboard triggered* click events to the tab strip mouse handler.
  506. tabEl.select('a').on('click', function(e){
  507. if(!e.getPageX()){
  508. this.onStripMouseDown(e);
  509. }
  510. }, this, {preventDefault: true});
  511. item.on({
  512. scope: this,
  513. disable: this.onItemDisabled,
  514. enable: this.onItemEnabled,
  515. titlechange: this.onItemTitleChanged,
  516. iconchange: this.onItemIconChanged,
  517. beforeshow: this.onBeforeShowItem
  518. });
  519. },
  520. /**
  521. * <p>Provides template arguments for rendering a tab selector item in the tab strip.</p>
  522. * <p>This method returns an object hash containing properties used by the TabPanel's <tt>{@link #itemTpl}</tt>
  523. * to create a formatted, clickable tab selector element. The properties which must be returned
  524. * are:</p><div class="mdetail-params"><ul>
  525. * <li><b>id</b> : String<div class="sub-desc">A unique identifier which links to the item</div></li>
  526. * <li><b>text</b> : String<div class="sub-desc">The text to display</div></li>
  527. * <li><b>cls</b> : String<div class="sub-desc">The CSS class name</div></li>
  528. * <li><b>iconCls</b> : String<div class="sub-desc">A CSS class to provide appearance for an icon.</div></li>
  529. * </ul></div>
  530. * @param {Ext.BoxComponent} item The {@link Ext.BoxComponent BoxComponent} for which to create a selector element in the tab strip.
  531. * @return {Object} An object hash containing the properties required to render the selector element.
  532. */
  533. getTemplateArgs : function(item) {
  534. var cls = item.closable ? 'x-tab-strip-closable' : '';
  535. if(item.disabled){
  536. cls += ' x-item-disabled';
  537. }
  538. if(item.iconCls){
  539. cls += ' x-tab-with-icon';
  540. }
  541. if(item.tabCls){
  542. cls += ' ' + item.tabCls;
  543. }
  544. return {
  545. id: this.id + this.idDelimiter + item.getItemId(),
  546. text: item.title,
  547. cls: cls,
  548. iconCls: item.iconCls || ''
  549. };
  550. },
  551. // private
  552. onAdd : function(c){
  553. Ext.TabPanel.superclass.onAdd.call(this, c);
  554. if(this.rendered){
  555. var items = this.items;
  556. this.initTab(c, items.indexOf(c));
  557. if(items.getCount() == 1 && !this.collapsed){
  558. this.syncSize();
  559. }
  560. this.delegateUpdates();
  561. }
  562. },
  563. // private
  564. onBeforeAdd : function(item){
  565. var existing = item.events ? (this.items.containsKey(item.getItemId()) ? item : null) : this.items.get(item);
  566. if(existing){
  567. this.setActiveTab(item);
  568. return false;
  569. }
  570. Ext.TabPanel.superclass.onBeforeAdd.apply(this, arguments);
  571. var es = item.elements;
  572. item.elements = es ? es.replace(',header', '') : es;
  573. item.border = (item.border === true);
  574. },
  575. // private
  576. onRemove : function(c){
  577. var te = Ext.get(c.tabEl);
  578. // check if the tabEl exists, it won't if the tab isn't rendered
  579. if(te){
  580. te.select('a').removeAllListeners();
  581. Ext.destroy(te);
  582. }
  583. Ext.TabPanel.superclass.onRemove.call(this, c);
  584. this.stack.remove(c);
  585. delete c.tabEl;
  586. c.un('disable', this.onItemDisabled, this);
  587. c.un('enable', this.onItemEnabled, this);
  588. c.un('titlechange', this.onItemTitleChanged, this);
  589. c.un('iconchange', this.onItemIconChanged, this);
  590. c.un('beforeshow', this.onBeforeShowItem, this);
  591. if(c == this.activeTab){
  592. var next = this.stack.next();
  593. if(next){
  594. this.setActiveTab(next);
  595. }else if(this.items.getCount() > 0){
  596. this.setActiveTab(0);
  597. }else{
  598. this.setActiveTab(null);
  599. }
  600. }
  601. if(!this.destroying){
  602. this.delegateUpdates();
  603. }
  604. },
  605. // private
  606. onBeforeShowItem : function(item){
  607. if(item != this.activeTab){
  608. this.setActiveTab(item);
  609. return false;
  610. }
  611. },
  612. // private
  613. onItemDisabled : function(item){
  614. var el = this.getTabEl(item);
  615. if(el){
  616. Ext.fly(el).addClass('x-item-disabled');
  617. }
  618. this.stack.remove(item);
  619. },
  620. // private
  621. onItemEnabled : function(item){
  622. var el = this.getTabEl(item);
  623. if(el){
  624. Ext.fly(el).removeClass('x-item-disabled');
  625. }
  626. },
  627. // private
  628. onItemTitleChanged : function(item){
  629. var el = this.getTabEl(item);
  630. if(el){
  631. Ext.fly(el).child('span.x-tab-strip-text', true).innerHTML = item.title;
  632. }
  633. },
  634. //private
  635. onItemIconChanged : function(item, iconCls, oldCls){
  636. var el = this.getTabEl(item);
  637. if(el){
  638. el = Ext.get(el);
  639. el.child('span.x-tab-strip-text').replaceClass(oldCls, iconCls);
  640. el[Ext.isEmpty(iconCls) ? 'removeClass' : 'addClass']('x-tab-with-icon');
  641. }
  642. },
  643. /**
  644. * Gets the DOM element for the tab strip item which activates the child panel with the specified
  645. * ID. Access this to change the visual treatment of the item, for example by changing the CSS class name.
  646. * @param {Panel/Number/String} tab The tab component, or the tab's index, or the tabs id or itemId.
  647. * @return {HTMLElement} The DOM node
  648. */
  649. getTabEl : function(item){
  650. var c = this.getComponent(item);
  651. return c ? c.tabEl : null;
  652. },
  653. // private
  654. onResize : function(){
  655. Ext.TabPanel.superclass.onResize.apply(this, arguments);
  656. this.delegateUpdates();
  657. },
  658. /**
  659. * Suspends any internal calculations or scrolling while doing a bulk operation. See {@link #endUpdate}
  660. */
  661. beginUpdate : function(){
  662. this.suspendUpdates = true;
  663. },
  664. /**
  665. * Resumes calculations and scrolling at the end of a bulk operation. See {@link #beginUpdate}
  666. */
  667. endUpdate : function(){
  668. this.suspendUpdates = false;
  669. this.delegateUpdates();
  670. },
  671. /**
  672. * Hides the tab strip item for the passed tab
  673. * @param {Number/String/Panel} item The tab index, id or item
  674. */
  675. hideTabStripItem : function(item){
  676. item = this.getComponent(item);
  677. var el = this.getTabEl(item);
  678. if(el){
  679. el.style.display = 'none';
  680. this.delegateUpdates();
  681. }
  682. this.stack.remove(item);
  683. },
  684. /**
  685. * Unhides the tab strip item for the passed tab
  686. * @param {Number/String/Panel} item The tab index, id or item
  687. */
  688. unhideTabStripItem : function(item){
  689. item = this.getComponent(item);
  690. var el = this.getTabEl(item);
  691. if(el){
  692. el.style.display = '';
  693. this.delegateUpdates();
  694. }
  695. },
  696. // private
  697. delegateUpdates : function(){
  698. if(this.suspendUpdates){
  699. return;
  700. }
  701. if(this.resizeTabs && this.rendered){
  702. this.autoSizeTabs();
  703. }
  704. if(this.enableTabScroll && this.rendered){
  705. this.autoScrollTabs();
  706. }
  707. },
  708. // private
  709. autoSizeTabs : function(){
  710. var count = this.items.length,
  711. ce = this.tabPosition != 'bottom' ? 'header' : 'footer',
  712. ow = this[ce].dom.offsetWidth,
  713. aw = this[ce].dom.clientWidth;
  714. if(!this.resizeTabs || count < 1 || !aw){ // !aw for display:none
  715. return;
  716. }
  717. var each = Math.max(Math.min(Math.floor((aw-4) / count) - this.tabMargin, this.tabWidth), this.minTabWidth); // -4 for float errors in IE
  718. this.lastTabWidth = each;
  719. var lis = this.strip.query('li:not(.x-tab-edge)');
  720. for(var i = 0, len = lis.length; i < len; i++) {
  721. var li = lis[i],
  722. inner = Ext.fly(li).child('.x-tab-strip-inner', true),
  723. tw = li.offsetWidth,
  724. iw = inner.offsetWidth;
  725. inner.style.width = (each - (tw-iw)) + 'px';
  726. }
  727. },
  728. // private
  729. adjustBodyWidth : function(w){
  730. if(this.header){
  731. this.header.setWidth(w);
  732. }
  733. if(this.footer){
  734. this.footer.setWidth(w);
  735. }
  736. return w;
  737. },
  738. /**
  739. * Sets the specified tab as the active tab. This method fires the {@link #beforetabchange} event which
  740. * can <tt>return false</tt> to cancel the tab change.
  741. * @param {String/Number} item
  742. * The id or tab Panel to activate. This parameter may be any of the following:
  743. * <div><ul class="mdetail-params">
  744. * <li>a <b><tt>String</tt></b> : representing the <code>{@link Ext.Component#itemId itemId}</code>
  745. * or <code>{@link Ext.Component#id id}</code> of the child component </li>
  746. * <li>a <b><tt>Number</tt></b> : representing the position of the child component
  747. * within the <code>{@link Ext.Container#items items}</code> <b>property</b></li>
  748. * </ul></div>
  749. * <p>For additional information see {@link Ext.util.MixedCollection#get}.
  750. */
  751. setActiveTab : function(item){
  752. item = this.getComponent(item);
  753. if(this.fireEvent('beforetabchange', this, item, this.activeTab) === false){
  754. return;
  755. }
  756. if(!this.rendered){
  757. this.activeTab = item;
  758. return;
  759. }
  760. if(this.activeTab != item){
  761. if(this.activeTab){
  762. var oldEl = this.getTabEl(this.activeTab);
  763. if(oldEl){
  764. Ext.fly(oldEl).removeClass('x-tab-strip-active');
  765. }
  766. }
  767. if(item){
  768. var el = this.getTabEl(item);
  769. Ext.fly(el).addClass('x-tab-strip-active');
  770. this.activeTab = item;
  771. this.stack.add(item);
  772. this.layout.setActiveItem(item);
  773. if(this.scrolling){
  774. this.scrollToTab(item, this.animScroll);
  775. }
  776. }
  777. this.fireEvent('tabchange', this, item);
  778. }
  779. },
  780. /**
  781. * Returns the Component which is the currently active tab. <b>Note that before the TabPanel
  782. * first activates a child Component, this method will return whatever was configured in the
  783. * {@link #activeTab} config option.</b>
  784. * @return {BoxComponent} The currently active child Component if one <i>is</i> active, or the {@link #activeTab} config value.
  785. */
  786. getActiveTab : function(){
  787. return this.activeTab || null;
  788. },
  789. /**
  790. * Gets the specified tab by id.
  791. * @param {String} id The tab id
  792. * @return {Panel} The tab
  793. */
  794. getItem : function(item){
  795. return this.getComponent(item);
  796. },
  797. // private
  798. autoScrollTabs : function(){
  799. this.pos = this.tabPosition=='bottom' ? this.footer : this.header;
  800. var count = this.items.length,
  801. ow = this.pos.dom.offsetWidth,
  802. tw = this.pos.dom.clientWidth,
  803. wrap = this.stripWrap,
  804. wd = wrap.dom,
  805. cw = wd.offsetWidth,
  806. pos = this.getScrollPos(),
  807. l = this.edge.getOffsetsTo(this.stripWrap)[0] + pos;
  808. if(!this.enableTabScroll || count < 1 || cw < 20){ // 20 to prevent display:none issues
  809. return;
  810. }
  811. if(l <= tw){
  812. wd.scrollLeft = 0;
  813. wrap.setWidth(tw);
  814. if(this.scrolling){
  815. this.scrolling = false;
  816. this.pos.removeClass('x-tab-scrolling');
  817. this.scrollLeft.hide();
  818. this.scrollRight.hide();
  819. // See here: http://extjs.com/forum/showthread.php?t=49308&highlight=isSafari
  820. if(Ext.isAir || Ext.isWebKit){
  821. wd.style.marginLeft = '';
  822. wd.style.marginRight = '';
  823. }
  824. }
  825. }else{
  826. if(!this.scrolling){
  827. this.pos.addClass('x-tab-scrolling');
  828. // See here: http://extjs.com/forum/showthread.php?t=49308&highlight=isSafari
  829. if(Ext.isAir || Ext.isWebKit){
  830. wd.style.marginLeft = '18px';
  831. wd.style.marginRight = '18px';
  832. }
  833. }
  834. tw -= wrap.getMargins('lr');
  835. wrap.setWidth(tw > 20 ? tw : 20);
  836. if(!this.scrolling){
  837. if(!this.scrollLeft){
  838. this.createScrollers();
  839. }else{
  840. this.scrollLeft.show();
  841. this.scrollRight.show();
  842. }
  843. }
  844. this.scrolling = true;
  845. if(pos > (l-tw)){ // ensure it stays within bounds
  846. wd.scrollLeft = l-tw;
  847. }else{ // otherwise, make sure the active tab is still visible
  848. this.scrollToTab(this.activeTab, false);
  849. }
  850. this.updateScrollButtons();
  851. }
  852. },
  853. // private
  854. createScrollers : function(){
  855. this.pos.addClass('x-tab-scrolling-' + this.tabPosition);
  856. var h = this.stripWrap.dom.offsetHeight;
  857. // left
  858. var sl = this.pos.insertFirst({
  859. cls:'x-tab-scroller-left'
  860. });
  861. sl.setHeight(h);
  862. sl.addClassOnOver('x-tab-scroller-left-over');
  863. this.leftRepeater = new Ext.util.ClickRepeater(sl, {
  864. interval : this.scrollRepeatInterval,
  865. handler: this.onScrollLeft,
  866. scope: this
  867. });
  868. this.scrollLeft = sl;
  869. // right
  870. var sr = this.pos.insertFirst({
  871. cls:'x-tab-scroller-right'
  872. });
  873. sr.setHeight(h);
  874. sr.addClassOnOver('x-tab-scroller-right-over');
  875. this.rightRepeater = new Ext.util.ClickRepeater(sr, {
  876. interval : this.scrollRepeatInterval,
  877. handler: this.onScrollRight,
  878. scope: this
  879. });
  880. this.scrollRight = sr;
  881. },
  882. // private
  883. getScrollWidth : function(){
  884. return this.edge.getOffsetsTo(this.stripWrap)[0] + this.getScrollPos();
  885. },
  886. // private
  887. getScrollPos : function(){
  888. return parseInt(this.stripWrap.dom.scrollLeft, 10) || 0;
  889. },
  890. // private
  891. getScrollArea : function(){
  892. return parseInt(this.stripWrap.dom.clientWidth, 10) || 0;
  893. },
  894. // private
  895. getScrollAnim : function(){
  896. return {duration:this.scrollDuration, callback: this.updateScrollButtons, scope: this};
  897. },
  898. // private
  899. getScrollIncrement : function(){
  900. return this.scrollIncrement || (this.resizeTabs ? this.lastTabWidth+2 : 100);
  901. },
  902. /**
  903. * Scrolls to a particular tab if tab scrolling is enabled
  904. * @param {Panel} item The item to scroll to
  905. * @param {Boolean} animate True to enable animations
  906. */
  907. scrollToTab : function(item, animate){
  908. if(!item){
  909. return;
  910. }
  911. var el = this.getTabEl(item),
  912. pos = this.getScrollPos(),
  913. area = this.getScrollArea(),
  914. left = Ext.fly(el).getOffsetsTo(this.stripWrap)[0] + pos,
  915. right = left + el.offsetWidth;
  916. if(left < pos){
  917. this.scrollTo(left, animate);
  918. }else if(right > (pos + area)){
  919. this.scrollTo(right - area, animate);
  920. }
  921. },
  922. // private
  923. scrollTo : function(pos, animate){
  924. this.stripWrap.scrollTo('left', pos, animate ? this.getScrollAnim() : false);
  925. if(!animate){
  926. this.updateScrollButtons();
  927. }
  928. },
  929. onWheel : function(e){
  930. var d = e.getWheelDelta()*this.wheelIncrement*-1;
  931. e.stopEvent();
  932. var pos = this.getScrollPos(),
  933. newpos = pos + d,
  934. sw = this.getScrollWidth()-this.getScrollArea();
  935. var s = Math.max(0, Math.min(sw, newpos));
  936. if(s != pos){
  937. this.scrollTo(s, false);
  938. }
  939. },
  940. // private
  941. onScrollRight : function(){
  942. var sw = this.getScrollWidth()-this.getScrollArea(),
  943. pos = this.getScrollPos(),
  944. s = Math.min(sw, pos + this.getScrollIncrement());
  945. if(s != pos){
  946. this.scrollTo(s, this.animScroll);
  947. }
  948. },
  949. // private
  950. onScrollLeft : function(){
  951. var pos = this.getScrollPos(),
  952. s = Math.max(0, pos - this.getScrollIncrement());
  953. if(s != pos){
  954. this.scrollTo(s, this.animScroll);
  955. }
  956. },
  957. // private
  958. updateScrollButtons : function(){
  959. var pos = this.getScrollPos();
  960. this.scrollLeft[pos === 0 ? 'addClass' : 'removeClass']('x-tab-scroller-left-disabled');
  961. this.scrollRight[pos >= (this.getScrollWidth()-this.getScrollArea()) ? 'addClass' : 'removeClass']('x-tab-scroller-right-disabled');
  962. },
  963. // private
  964. beforeDestroy : function() {
  965. Ext.destroy(this.leftRepeater, this.rightRepeater);
  966. this.deleteMembers('strip', 'edge', 'scrollLeft', 'scrollRight', 'stripWrap');
  967. this.activeTab = null;
  968. Ext.TabPanel.superclass.beforeDestroy.apply(this);
  969. }
  970. /**
  971. * @cfg {Boolean} collapsible
  972. * @hide
  973. */
  974. /**
  975. * @cfg {String} header
  976. * @hide
  977. */
  978. /**
  979. * @cfg {Boolean} headerAsText
  980. * @hide
  981. */
  982. /**
  983. * @property header
  984. * @hide
  985. */
  986. /**
  987. * @cfg title
  988. * @hide
  989. */
  990. /**
  991. * @cfg {Array} tools
  992. * @hide
  993. */
  994. /**
  995. * @cfg {Array} toolTemplate
  996. * @hide
  997. */
  998. /**
  999. * @cfg {Boolean} hideCollapseTool
  1000. * @hide
  1001. */
  1002. /**
  1003. * @cfg {Boolean} titleCollapse
  1004. * @hide
  1005. */
  1006. /**
  1007. * @cfg {Boolean} collapsed
  1008. * @hide
  1009. */
  1010. /**
  1011. * @cfg {String} layout
  1012. * @hide
  1013. */
  1014. /**
  1015. * @cfg {Boolean} preventBodyReset
  1016. * @hide
  1017. */
  1018. });
  1019. Ext.reg('tabpanel', Ext.TabPanel);
  1020. /**
  1021. * See {@link #setActiveTab}. Sets the specified tab as the active tab. This method fires
  1022. * the {@link #beforetabchange} event which can <tt>return false</tt> to cancel the tab change.
  1023. * @param {String/Panel} tab The id or tab Panel to activate
  1024. * @method activate
  1025. */
  1026. Ext.TabPanel.prototype.activate = Ext.TabPanel.prototype.setActiveTab;
  1027. // private utility class used by TabPanel
  1028. Ext.TabPanel.AccessStack = function(){
  1029. var items = [];
  1030. return {
  1031. add : function(item){
  1032. items.push(item);
  1033. if(items.length > 10){
  1034. items.shift();
  1035. }
  1036. },
  1037. remove : function(item){
  1038. var s = [];
  1039. for(var i = 0, len = items.length; i < len; i++) {
  1040. if(items[i] != item){
  1041. s.push(items[i]);
  1042. }
  1043. }
  1044. items = s;
  1045. },
  1046. next : function(){
  1047. return items.pop();
  1048. }
  1049. };
  1050. };