4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
13 * @extends Roo.util.Observable
14 * A lightweight tab container.
18 // basic tabs 1, built from existing content
19 var tabs = new Roo.TabPanel("tabs1");
20 tabs.addTab("script", "View Script");
21 tabs.addTab("markup", "View Markup");
22 tabs.activate("script");
24 // more advanced tabs, built from javascript
25 var jtabs = new Roo.TabPanel("jtabs");
26 jtabs.addTab("jtabs-1", "Normal Tab", "My content was added during construction.");
28 // set up the UpdateManager
29 var tab2 = jtabs.addTab("jtabs-2", "Ajax Tab 1");
30 var updater = tab2.getUpdateManager();
31 updater.setDefaultUrl("ajax1.htm");
32 tab2.on('activate', updater.refresh, updater, true);
34 // Use setUrl for Ajax loading
35 var tab3 = jtabs.addTab("jtabs-3", "Ajax Tab 2");
36 tab3.setUrl("ajax2.htm", null, true);
39 var tab4 = jtabs.addTab("tabs1-5", "Disabled Tab", "Can't see me cause I'm disabled");
42 jtabs.activate("jtabs-1");
45 * Create a new TabPanel.
46 * @param {String/HTMLElement/Roo.Element} container The id, DOM element or Roo.Element container where this TabPanel is to be rendered.
47 * @param {Object/Boolean} config Config object to set any properties for this TabPanel, or true to render the tabs on the bottom.
49 Roo.TabPanel = function(container, config){
51 * The container element for this TabPanel.
54 this.el = Roo.get(container, true);
56 if(typeof config == "boolean"){
57 this.tabPosition = config ? "bottom" : "top";
59 Roo.apply(this, config);
62 if(this.tabPosition == "bottom"){
63 this.bodyEl = Roo.get(this.createBody(this.el.dom));
64 this.el.addClass("x-tabs-bottom");
66 this.stripWrap = Roo.get(this.createStrip(this.el.dom), true);
67 this.stripEl = Roo.get(this.createStripList(this.stripWrap.dom), true);
68 this.stripBody = Roo.get(this.stripWrap.dom.firstChild.firstChild, true);
70 Roo.fly(this.stripWrap.dom.firstChild).setStyle("overflow-x", "hidden");
72 if(this.tabPosition != "bottom"){
73 /** The body element that contains {@link Roo.TabPanelItem} bodies. +
76 this.bodyEl = Roo.get(this.createBody(this.el.dom));
77 this.el.addClass("x-tabs-top");
81 this.bodyEl.setStyle("position", "relative");
84 this.activateDelegate = this.activate.createDelegate(this);
89 * Fires when the active tab changes
90 * @param {Roo.TabPanel} this
91 * @param {Roo.TabPanelItem} activePanel The new active tab
95 * @event beforetabchange
96 * Fires before the active tab changes, set cancel to true on the "e" parameter to cancel the change
97 * @param {Roo.TabPanel} this
98 * @param {Object} e Set cancel to true on this object to cancel the tab change
99 * @param {Roo.TabPanelItem} tab The tab being changed to
101 "beforetabchange" : true
104 Roo.EventManager.onWindowResize(this.onResize, this);
105 this.cpad = this.el.getPadding("lr");
106 this.hiddenCount = 0;
109 // toolbar on the tabbar support...
111 var tcfg = this.toolbar;
112 tcfg.container = this.stripEl.child('td.x-tab-strip-toolbar');
113 this.toolbar = new Roo.Toolbar(tcfg);
115 var tbl = tcfg.container.child('table', true);
116 tbl.setAttribute('width', '100%');
123 Roo.TabPanel.superclass.constructor.call(this);
126 Roo.extend(Roo.TabPanel, Roo.util.Observable, {
128 *@cfg {String} tabPosition "top" or "bottom" (defaults to "top")
132 *@cfg {Number} currentTabWidth The width of the current tab (defaults to 0)
136 *@cfg {Number} minTabWidth The minimum width of a tab (defaults to 40) (ignored if {@link #resizeTabs} is not true)
140 *@cfg {Number} maxTabWidth The maximum width of a tab (defaults to 250) (ignored if {@link #resizeTabs} is not true)
144 *@cfg {Number} preferredTabWidth The preferred (default) width of a tab (defaults to 175) (ignored if {@link #resizeTabs} is not true)
146 preferredTabWidth : 175,
148 *@cfg {Boolean} resizeTabs True to enable dynamic tab resizing (defaults to false)
152 *@cfg {Boolean} monitorResize Set this to true to turn on window resize monitoring (ignored if {@link #resizeTabs} is not true) (defaults to true)
154 monitorResize : true,
156 *@cfg {Object} toolbar xtype description of toolbar to show at the right of the tab bar.
161 * Creates a new {@link Roo.TabPanelItem} by looking for an existing element with the provided id -- if it's not found it creates one.
162 * @param {String} id The id of the div to use <b>or create</b>
163 * @param {String} text The text for the tab
164 * @param {String} content (optional) Content to put in the TabPanelItem body
165 * @param {Boolean} closable (optional) True to create a close icon on the tab
166 * @return {Roo.TabPanelItem} The created TabPanelItem
168 addTab : function(id, text, content, closable){
169 var item = new Roo.TabPanelItem(this, id, text, closable);
170 this.addTabItem(item);
172 item.setContent(content);
178 * Returns the {@link Roo.TabPanelItem} with the specified id/index
179 * @param {String/Number} id The id or index of the TabPanelItem to fetch.
180 * @return {Roo.TabPanelItem}
182 getTab : function(id){
183 return this.items[id];
187 * Hides the {@link Roo.TabPanelItem} with the specified id/index
188 * @param {String/Number} id The id or index of the TabPanelItem to hide.
190 hideTab : function(id){
191 var t = this.items[id];
200 * "Unhides" the {@link Roo.TabPanelItem} with the specified id/index.
201 * @param {String/Number} id The id or index of the TabPanelItem to unhide.
203 unhideTab : function(id){
204 var t = this.items[id];
213 * Adds an existing {@link Roo.TabPanelItem}.
214 * @param {Roo.TabPanelItem} item The TabPanelItem to add
216 addTabItem : function(item){
217 this.items[item.id] = item;
218 this.items.push(item);
220 item.setWidth(this.currentTabWidth || this.preferredTabWidth);
228 * Removes a {@link Roo.TabPanelItem}.
229 * @param {String/Number} id The id or index of the TabPanelItem to remove.
231 removeTab : function(id){
232 var items = this.items;
235 var index = items.indexOf(tab);
236 if(this.active == tab && items.length > 1){
237 var newTab = this.getNextAvailable(index);
242 this.stripEl.dom.removeChild(tab.pnode.dom);
243 if(tab.bodyEl.dom.parentNode == this.bodyEl.dom){ // if it was moved already prevent error
244 this.bodyEl.dom.removeChild(tab.bodyEl.dom);
246 items.splice(index, 1);
247 delete this.items[tab.id];
248 tab.fireEvent("close", tab);
249 tab.purgeListeners();
253 getNextAvailable : function(start){
254 var items = this.items;
256 // look for a next tab that will slide over to
257 // replace the one being removed
258 while(index < items.length){
259 var item = items[++index];
260 if(item && !item.isHidden()){
264 // if one isn't found select the previous tab (on the left)
267 var item = items[--index];
268 if(item && !item.isHidden()){
276 * Disables a {@link Roo.TabPanelItem}. It cannot be the active tab, if it is this call is ignored.
277 * @param {String/Number} id The id or index of the TabPanelItem to disable.
279 disableTab : function(id){
280 var tab = this.items[id];
281 if(tab && this.active != tab){
287 * Enables a {@link Roo.TabPanelItem} that is disabled.
288 * @param {String/Number} id The id or index of the TabPanelItem to enable.
290 enableTab : function(id){
291 var tab = this.items[id];
296 * Activates a {@link Roo.TabPanelItem}. The currently active one will be deactivated.
297 * @param {String/Number} id The id or index of the TabPanelItem to activate.
298 * @return {Roo.TabPanelItem} The TabPanelItem.
300 activate : function(id){
301 var tab = this.items[id];
305 if(tab == this.active || tab.disabled){
309 this.fireEvent("beforetabchange", this, e, tab);
310 if(e.cancel !== true && !tab.disabled){
314 this.active = this.items[id];
316 this.fireEvent("tabchange", this, this.active);
322 * Gets the active {@link Roo.TabPanelItem}.
323 * @return {Roo.TabPanelItem} The active TabPanelItem or null if none are active.
325 getActiveTab : function(){
330 * Updates the tab body element to fit the height of the container element
331 * for overflow scrolling
332 * @param {Number} targetHeight (optional) Override the starting height from the elements height
334 syncHeight : function(targetHeight){
335 var height = (targetHeight || this.el.getHeight())-this.el.getBorderWidth("tb")-this.el.getPadding("tb");
336 var bm = this.bodyEl.getMargins();
337 var newHeight = height-(this.stripWrap.getHeight()||0)-(bm.top+bm.bottom);
338 this.bodyEl.setHeight(newHeight);
342 onResize : function(){
343 if(this.monitorResize){
349 * Disables tab resizing while tabs are being added (if {@link #resizeTabs} is false this does nothing)
351 beginUpdate : function(){
352 this.updating = true;
356 * Stops an update and resizes the tabs (if {@link #resizeTabs} is false this does nothing)
358 endUpdate : function(){
359 this.updating = false;
364 * Manual call to resize the tabs (if {@link #resizeTabs} is false this does nothing)
366 autoSizeTabs : function(){
367 var count = this.items.length;
368 var vcount = count - this.hiddenCount;
369 if(!this.resizeTabs || count < 1 || vcount < 1 || this.updating) {
372 var w = Math.max(this.el.getWidth() - this.cpad, 10);
373 var availWidth = Math.floor(w / vcount);
374 var b = this.stripBody;
375 if(b.getWidth() > w){
376 var tabs = this.items;
377 this.setTabWidth(Math.max(availWidth, this.minTabWidth)-2);
378 if(availWidth < this.minTabWidth){
379 /*if(!this.sleft){ // incomplete scrolling code
380 this.createScrollButtons();
383 this.stripClip.setWidth(w - (this.sleft.getWidth()+this.sright.getWidth()));*/
386 if(this.currentTabWidth < this.preferredTabWidth){
387 this.setTabWidth(Math.min(availWidth, this.preferredTabWidth)-2);
393 * Returns the number of tabs in this TabPanel.
396 getCount : function(){
397 return this.items.length;
401 * Resizes all the tabs to the passed width
402 * @param {Number} The new width
404 setTabWidth : function(width){
405 this.currentTabWidth = width;
406 for(var i = 0, len = this.items.length; i < len; i++) {
407 if(!this.items[i].isHidden()) {
408 this.items[i].setWidth(width);
414 * Destroys this TabPanel
415 * @param {Boolean} removeEl (optional) True to remove the element from the DOM as well (defaults to undefined)
417 destroy : function(removeEl){
418 Roo.EventManager.removeResizeListener(this.onResize, this);
419 for(var i = 0, len = this.items.length; i < len; i++){
420 this.items[i].purgeListeners();
422 if(removeEl === true){
430 * @class Roo.TabPanelItem
431 * @extends Roo.util.Observable
432 * Represents an individual item (tab plus body) in a TabPanel.
433 * @param {Roo.TabPanel} tabPanel The {@link Roo.TabPanel} this TabPanelItem belongs to
434 * @param {String} id The id of this TabPanelItem
435 * @param {String} text The text for the tab of this TabPanelItem
436 * @param {Boolean} closable True to allow this TabPanelItem to be closable (defaults to false)
438 Roo.TabPanelItem = function(tabPanel, id, text, closable){
440 * The {@link Roo.TabPanel} this TabPanelItem belongs to
443 this.tabPanel = tabPanel;
445 * The id for this TabPanelItem
450 this.disabled = false;
455 this.closable = closable;
458 * The body element for this TabPanelItem.
461 this.bodyEl = Roo.get(tabPanel.createItemBody(tabPanel.bodyEl.dom, id));
462 this.bodyEl.setVisibilityMode(Roo.Element.VISIBILITY);
463 this.bodyEl.setStyle("display", "block");
464 this.bodyEl.setStyle("zoom", "1");
467 var els = tabPanel.createStripElements(tabPanel.stripEl.dom, text, closable);
469 this.el = Roo.get(els.el, true);
470 this.inner = Roo.get(els.inner, true);
471 this.textEl = Roo.get(this.el.dom.firstChild.firstChild.firstChild, true);
472 this.pnode = Roo.get(els.el.parentNode, true);
473 this.el.on("mousedown", this.onTabMouseDown, this);
474 this.el.on("click", this.onTabClick, this);
477 var c = Roo.get(els.close, true);
478 c.dom.title = this.closeText;
479 c.addClassOnOver("close-over");
480 c.on("click", this.closeClick, this);
486 * Fires when this tab becomes the active tab.
487 * @param {Roo.TabPanel} tabPanel The parent TabPanel
488 * @param {Roo.TabPanelItem} this
493 * Fires before this tab is closed. To cancel the close, set cancel to true on e (e.cancel = true).
494 * @param {Roo.TabPanelItem} this
495 * @param {Object} e Set cancel to true on this object to cancel the close.
500 * Fires when this tab is closed.
501 * @param {Roo.TabPanelItem} this
506 * Fires when this tab is no longer the active tab.
507 * @param {Roo.TabPanel} tabPanel The parent TabPanel
508 * @param {Roo.TabPanelItem} this
514 Roo.TabPanelItem.superclass.constructor.call(this);
517 Roo.extend(Roo.TabPanelItem, Roo.util.Observable, {
518 purgeListeners : function(){
519 Roo.util.Observable.prototype.purgeListeners.call(this);
520 this.el.removeAllListeners();
523 * Shows this TabPanelItem -- this <b>does not</b> deactivate the currently active TabPanelItem.
526 this.pnode.addClass("on");
529 this.tabPanel.stripWrap.repaint();
531 this.fireEvent("activate", this.tabPanel, this);
535 * Returns true if this tab is the active tab.
538 isActive : function(){
539 return this.tabPanel.getActiveTab() == this;
543 * Hides this TabPanelItem -- if you don't activate another TabPanelItem this could look odd.
546 this.pnode.removeClass("on");
548 this.fireEvent("deactivate", this.tabPanel, this);
551 hideAction : function(){
553 this.bodyEl.setStyle("position", "absolute");
554 this.bodyEl.setLeft("-20000px");
555 this.bodyEl.setTop("-20000px");
558 showAction : function(){
559 this.bodyEl.setStyle("position", "relative");
560 this.bodyEl.setTop("");
561 this.bodyEl.setLeft("");
566 * Set the tooltip for the tab.
567 * @param {String} tooltip The tab's tooltip
569 setTooltip : function(text){
570 if(Roo.QuickTips && Roo.QuickTips.isEnabled()){
571 this.textEl.dom.qtip = text;
572 this.textEl.dom.removeAttribute('title');
574 this.textEl.dom.title = text;
578 onTabClick : function(e){
580 this.tabPanel.activate(this.id);
583 onTabMouseDown : function(e){
585 this.tabPanel.activate(this.id);
588 getWidth : function(){
589 return this.inner.getWidth();
592 setWidth : function(width){
593 var iwidth = width - this.pnode.getPadding("lr");
594 this.inner.setWidth(iwidth);
595 this.textEl.setWidth(iwidth-this.inner.getPadding("lr"));
596 this.pnode.setWidth(width);
600 * Show or hide the tab
601 * @param {Boolean} hidden True to hide or false to show.
603 setHidden : function(hidden){
604 this.hidden = hidden;
605 this.pnode.setStyle("display", hidden ? "none" : "");
609 * Returns true if this tab is "hidden"
612 isHidden : function(){
617 * Returns the text for this tab
620 getText : function(){
624 autoSize : function(){
625 //this.el.beginMeasure();
626 this.textEl.setWidth(1);
628 * #2804 [new] Tabs in Roojs
629 * increase the width by 2-4 pixels to prevent the ellipssis showing in chrome
631 this.setWidth(this.textEl.dom.scrollWidth+this.pnode.getPadding("lr")+this.inner.getPadding("lr") + 2);
632 //this.el.endMeasure();
636 * Sets the text for the tab (Note: this also sets the tooltip text)
637 * @param {String} text The tab's text and tooltip
639 setText : function(text){
641 this.textEl.update(text);
642 this.setTooltip(text);
643 if(!this.tabPanel.resizeTabs){
648 * Activates this TabPanelItem -- this <b>does</b> deactivate the currently active TabPanelItem.
650 activate : function(){
651 this.tabPanel.activate(this.id);
655 * Disables this TabPanelItem -- this does nothing if this is the active TabPanelItem.
657 disable : function(){
658 if(this.tabPanel.active != this){
659 this.disabled = true;
660 this.pnode.addClass("disabled");
665 * Enables this TabPanelItem if it was previously disabled.
668 this.disabled = false;
669 this.pnode.removeClass("disabled");
673 * Sets the content for this TabPanelItem.
674 * @param {String} content The content
675 * @param {Boolean} loadScripts true to look for and load scripts
677 setContent : function(content, loadScripts){
678 this.bodyEl.update(content, loadScripts);
682 * Gets the {@link Roo.UpdateManager} for the body of this TabPanelItem. Enables you to perform Ajax updates.
683 * @return {Roo.UpdateManager} The UpdateManager
685 getUpdateManager : function(){
686 return this.bodyEl.getUpdateManager();
690 * Set a URL to be used to load the content for this TabPanelItem.
691 * @param {String/Function} url The URL to load the content from, or a function to call to get the URL
692 * @param {String/Object} params (optional) The string params for the update call or an object of the params. See {@link Roo.UpdateManager#update} for more details. (Defaults to null)
693 * @param {Boolean} loadOnce (optional) Whether to only load the content once. If this is false it makes the Ajax call every time this TabPanelItem is activated. (Defaults to false)
694 * @return {Roo.UpdateManager} The UpdateManager
696 setUrl : function(url, params, loadOnce){
697 if(this.refreshDelegate){
698 this.un('activate', this.refreshDelegate);
700 this.refreshDelegate = this._handleRefresh.createDelegate(this, [url, params, loadOnce]);
701 this.on("activate", this.refreshDelegate);
702 return this.bodyEl.getUpdateManager();
706 _handleRefresh : function(url, params, loadOnce){
707 if(!loadOnce || !this.loaded){
708 var updater = this.bodyEl.getUpdateManager();
709 updater.update(url, params, this._setLoaded.createDelegate(this));
714 * Forces a content refresh from the URL specified in the {@link #setUrl} method.
715 * Will fail silently if the setUrl method has not been called.
716 * This does not activate the panel, just updates its content.
718 refresh : function(){
719 if(this.refreshDelegate){
721 this.refreshDelegate();
726 _setLoaded : function(){
731 closeClick : function(e){
734 this.fireEvent("beforeclose", this, o);
735 if(o.cancel !== true){
736 this.tabPanel.removeTab(this.id);
740 * The text displayed in the tooltip for the close icon.
743 closeText : "Close this tab"
747 Roo.TabPanel.prototype.createStrip = function(container){
748 var strip = document.createElement("div");
749 strip.className = "x-tabs-wrap";
750 container.appendChild(strip);
754 Roo.TabPanel.prototype.createStripList = function(strip){
755 // div wrapper for retard IE
756 // returns the "tr" element.
757 strip.innerHTML = '<div class="x-tabs-strip-wrap">'+
758 '<table class="x-tabs-strip" cellspacing="0" cellpadding="0" border="0"><tbody><tr>'+
759 '<td class="x-tab-strip-toolbar"></td></tr></tbody></table></div>';
760 return strip.firstChild.firstChild.firstChild.firstChild;
763 Roo.TabPanel.prototype.createBody = function(container){
764 var body = document.createElement("div");
765 Roo.id(body, "tab-body");
766 Roo.fly(body).addClass("x-tabs-body");
767 container.appendChild(body);
771 Roo.TabPanel.prototype.createItemBody = function(bodyEl, id){
772 var body = Roo.getDom(id);
774 body = document.createElement("div");
777 Roo.fly(body).addClass("x-tabs-item-body");
778 bodyEl.insertBefore(body, bodyEl.firstChild);
782 Roo.TabPanel.prototype.createStripElements = function(stripEl, text, closable){
783 var td = document.createElement("td");
784 stripEl.insertBefore(td, stripEl.childNodes[stripEl.childNodes.length-1]);
785 //stripEl.appendChild(td);
787 td.className = "x-tabs-closable";
789 this.closeTpl = new Roo.Template(
790 '<a href="#" class="x-tabs-right"><span class="x-tabs-left"><em class="x-tabs-inner">' +
791 '<span unselectable="on"' + (this.disableTooltips ? '' : ' title="{text}"') +' class="x-tabs-text">{text}</span>' +
792 '<div unselectable="on" class="close-icon"> </div></em></span></a>'
795 var el = this.closeTpl.overwrite(td, {"text": text});
796 var close = el.getElementsByTagName("div")[0];
797 var inner = el.getElementsByTagName("em")[0];
798 return {"el": el, "close": close, "inner": inner};
801 this.tabTpl = new Roo.Template(
802 '<a href="#" class="x-tabs-right"><span class="x-tabs-left"><em class="x-tabs-inner">' +
803 '<span unselectable="on"' + (this.disableTooltips ? '' : ' title="{text}"') +' class="x-tabs-text">{text}</span></em></span></a>'
806 var el = this.tabTpl.overwrite(td, {"text": text});
807 var inner = el.getElementsByTagName("em")[0];
808 return {"el": el, "inner": inner};