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);
118 Roo.TabPanel.superclass.constructor.call(this);
121 Roo.extend(Roo.TabPanel, Roo.util.Observable, {
123 *@cfg {String} tabPosition "top" or "bottom" (defaults to "top")
127 *@cfg {Number} currentTabWidth The width of the current tab (defaults to 0)
131 *@cfg {Number} minTabWidth The minimum width of a tab (defaults to 40) (ignored if {@link #resizeTabs} is not true)
135 *@cfg {Number} maxTabWidth The maximum width of a tab (defaults to 250) (ignored if {@link #resizeTabs} is not true)
139 *@cfg {Number} preferredTabWidth The preferred (default) width of a tab (defaults to 175) (ignored if {@link #resizeTabs} is not true)
141 preferredTabWidth : 175,
143 *@cfg {Boolean} resizeTabs True to enable dynamic tab resizing (defaults to false)
147 *@cfg {Boolean} monitorResize Set this to true to turn on window resize monitoring (ignored if {@link #resizeTabs} is not true) (defaults to true)
149 monitorResize : true,
151 *@cfg {Object} toolbar xtype description of toolbar to show at the right of the tab bar.
156 * Creates a new {@link Roo.TabPanelItem} by looking for an existing element with the provided id -- if it's not found it creates one.
157 * @param {String} id The id of the div to use <b>or create</b>
158 * @param {String} text The text for the tab
159 * @param {String} content (optional) Content to put in the TabPanelItem body
160 * @param {Boolean} closable (optional) True to create a close icon on the tab
161 * @return {Roo.TabPanelItem} The created TabPanelItem
163 addTab : function(id, text, content, closable){
164 var item = new Roo.TabPanelItem(this, id, text, closable);
165 this.addTabItem(item);
167 item.setContent(content);
173 * Returns the {@link Roo.TabPanelItem} with the specified id/index
174 * @param {String/Number} id The id or index of the TabPanelItem to fetch.
175 * @return {Roo.TabPanelItem}
177 getTab : function(id){
178 return this.items[id];
182 * Hides the {@link Roo.TabPanelItem} with the specified id/index
183 * @param {String/Number} id The id or index of the TabPanelItem to hide.
185 hideTab : function(id){
186 var t = this.items[id];
195 * "Unhides" the {@link Roo.TabPanelItem} with the specified id/index.
196 * @param {String/Number} id The id or index of the TabPanelItem to unhide.
198 unhideTab : function(id){
199 var t = this.items[id];
208 * Adds an existing {@link Roo.TabPanelItem}.
209 * @param {Roo.TabPanelItem} item The TabPanelItem to add
211 addTabItem : function(item){
212 this.items[item.id] = item;
213 this.items.push(item);
215 item.setWidth(this.currentTabWidth || this.preferredTabWidth);
223 * Removes a {@link Roo.TabPanelItem}.
224 * @param {String/Number} id The id or index of the TabPanelItem to remove.
226 removeTab : function(id){
227 var items = this.items;
230 var index = items.indexOf(tab);
231 if(this.active == tab && items.length > 1){
232 var newTab = this.getNextAvailable(index);
237 this.stripEl.dom.removeChild(tab.pnode.dom);
238 if(tab.bodyEl.dom.parentNode == this.bodyEl.dom){ // if it was moved already prevent error
239 this.bodyEl.dom.removeChild(tab.bodyEl.dom);
241 items.splice(index, 1);
242 delete this.items[tab.id];
243 tab.fireEvent("close", tab);
244 tab.purgeListeners();
248 getNextAvailable : function(start){
249 var items = this.items;
251 // look for a next tab that will slide over to
252 // replace the one being removed
253 while(index < items.length){
254 var item = items[++index];
255 if(item && !item.isHidden()){
259 // if one isn't found select the previous tab (on the left)
262 var item = items[--index];
263 if(item && !item.isHidden()){
271 * Disables a {@link Roo.TabPanelItem}. It cannot be the active tab, if it is this call is ignored.
272 * @param {String/Number} id The id or index of the TabPanelItem to disable.
274 disableTab : function(id){
275 var tab = this.items[id];
276 if(tab && this.active != tab){
282 * Enables a {@link Roo.TabPanelItem} that is disabled.
283 * @param {String/Number} id The id or index of the TabPanelItem to enable.
285 enableTab : function(id){
286 var tab = this.items[id];
291 * Activates a {@link Roo.TabPanelItem}. The currently active one will be deactivated.
292 * @param {String/Number} id The id or index of the TabPanelItem to activate.
293 * @return {Roo.TabPanelItem} The TabPanelItem.
295 activate : function(id){
296 var tab = this.items[id];
300 if(tab == this.active || tab.disabled){
304 this.fireEvent("beforetabchange", this, e, tab);
305 if(e.cancel !== true && !tab.disabled){
309 this.active = this.items[id];
311 this.fireEvent("tabchange", this, this.active);
317 * Gets the active {@link Roo.TabPanelItem}.
318 * @return {Roo.TabPanelItem} The active TabPanelItem or null if none are active.
320 getActiveTab : function(){
325 * Updates the tab body element to fit the height of the container element
326 * for overflow scrolling
327 * @param {Number} targetHeight (optional) Override the starting height from the elements height
329 syncHeight : function(targetHeight){
330 var height = (targetHeight || this.el.getHeight())-this.el.getBorderWidth("tb")-this.el.getPadding("tb");
331 var bm = this.bodyEl.getMargins();
332 var newHeight = height-(this.stripWrap.getHeight()||0)-(bm.top+bm.bottom);
333 this.bodyEl.setHeight(newHeight);
337 onResize : function(){
338 if(this.monitorResize){
344 * Disables tab resizing while tabs are being added (if {@link #resizeTabs} is false this does nothing)
346 beginUpdate : function(){
347 this.updating = true;
351 * Stops an update and resizes the tabs (if {@link #resizeTabs} is false this does nothing)
353 endUpdate : function(){
354 this.updating = false;
359 * Manual call to resize the tabs (if {@link #resizeTabs} is false this does nothing)
361 autoSizeTabs : function(){
362 var count = this.items.length;
363 var vcount = count - this.hiddenCount;
364 if(!this.resizeTabs || count < 1 || vcount < 1 || this.updating) return;
365 var w = Math.max(this.el.getWidth() - this.cpad, 10);
366 var availWidth = Math.floor(w / vcount);
367 var b = this.stripBody;
368 if(b.getWidth() > w){
369 var tabs = this.items;
370 this.setTabWidth(Math.max(availWidth, this.minTabWidth)-2);
371 if(availWidth < this.minTabWidth){
372 /*if(!this.sleft){ // incomplete scrolling code
373 this.createScrollButtons();
376 this.stripClip.setWidth(w - (this.sleft.getWidth()+this.sright.getWidth()));*/
379 if(this.currentTabWidth < this.preferredTabWidth){
380 this.setTabWidth(Math.min(availWidth, this.preferredTabWidth)-2);
386 * Returns the number of tabs in this TabPanel.
389 getCount : function(){
390 return this.items.length;
394 * Resizes all the tabs to the passed width
395 * @param {Number} The new width
397 setTabWidth : function(width){
398 this.currentTabWidth = width;
399 for(var i = 0, len = this.items.length; i < len; i++) {
400 if(!this.items[i].isHidden())this.items[i].setWidth(width);
405 * Destroys this TabPanel
406 * @param {Boolean} removeEl (optional) True to remove the element from the DOM as well (defaults to undefined)
408 destroy : function(removeEl){
409 Roo.EventManager.removeResizeListener(this.onResize, this);
410 for(var i = 0, len = this.items.length; i < len; i++){
411 this.items[i].purgeListeners();
413 if(removeEl === true){
421 * @class Roo.TabPanelItem
422 * @extends Roo.util.Observable
423 * Represents an individual item (tab plus body) in a TabPanel.
424 * @param {Roo.TabPanel} tabPanel The {@link Roo.TabPanel} this TabPanelItem belongs to
425 * @param {String} id The id of this TabPanelItem
426 * @param {String} text The text for the tab of this TabPanelItem
427 * @param {Boolean} closable True to allow this TabPanelItem to be closable (defaults to false)
429 Roo.TabPanelItem = function(tabPanel, id, text, closable){
431 * The {@link Roo.TabPanel} this TabPanelItem belongs to
434 this.tabPanel = tabPanel;
436 * The id for this TabPanelItem
441 this.disabled = false;
446 this.closable = closable;
449 * The body element for this TabPanelItem.
452 this.bodyEl = Roo.get(tabPanel.createItemBody(tabPanel.bodyEl.dom, id));
453 this.bodyEl.setVisibilityMode(Roo.Element.VISIBILITY);
454 this.bodyEl.setStyle("display", "block");
455 this.bodyEl.setStyle("zoom", "1");
458 var els = tabPanel.createStripElements(tabPanel.stripEl.dom, text, closable);
460 this.el = Roo.get(els.el, true);
461 this.inner = Roo.get(els.inner, true);
462 this.textEl = Roo.get(this.el.dom.firstChild.firstChild.firstChild, true);
463 this.pnode = Roo.get(els.el.parentNode, true);
464 this.el.on("mousedown", this.onTabMouseDown, this);
465 this.el.on("click", this.onTabClick, this);
468 var c = Roo.get(els.close, true);
469 c.dom.title = this.closeText;
470 c.addClassOnOver("close-over");
471 c.on("click", this.closeClick, this);
477 * Fires when this tab becomes the active tab.
478 * @param {Roo.TabPanel} tabPanel The parent TabPanel
479 * @param {Roo.TabPanelItem} this
484 * Fires before this tab is closed. To cancel the close, set cancel to true on e (e.cancel = true).
485 * @param {Roo.TabPanelItem} this
486 * @param {Object} e Set cancel to true on this object to cancel the close.
491 * Fires when this tab is closed.
492 * @param {Roo.TabPanelItem} this
497 * Fires when this tab is no longer the active tab.
498 * @param {Roo.TabPanel} tabPanel The parent TabPanel
499 * @param {Roo.TabPanelItem} this
505 Roo.TabPanelItem.superclass.constructor.call(this);
508 Roo.extend(Roo.TabPanelItem, Roo.util.Observable, {
509 purgeListeners : function(){
510 Roo.util.Observable.prototype.purgeListeners.call(this);
511 this.el.removeAllListeners();
514 * Shows this TabPanelItem -- this <b>does not</b> deactivate the currently active TabPanelItem.
517 this.pnode.addClass("on");
520 this.tabPanel.stripWrap.repaint();
522 this.fireEvent("activate", this.tabPanel, this);
526 * Returns true if this tab is the active tab.
529 isActive : function(){
530 return this.tabPanel.getActiveTab() == this;
534 * Hides this TabPanelItem -- if you don't activate another TabPanelItem this could look odd.
537 this.pnode.removeClass("on");
539 this.fireEvent("deactivate", this.tabPanel, this);
542 hideAction : function(){
544 this.bodyEl.setStyle("position", "absolute");
545 this.bodyEl.setLeft("-20000px");
546 this.bodyEl.setTop("-20000px");
549 showAction : function(){
550 this.bodyEl.setStyle("position", "relative");
551 this.bodyEl.setTop("");
552 this.bodyEl.setLeft("");
557 * Set the tooltip for the tab.
558 * @param {String} tooltip The tab's tooltip
560 setTooltip : function(text){
561 if(Roo.QuickTips && Roo.QuickTips.isEnabled()){
562 this.textEl.dom.qtip = text;
563 this.textEl.dom.removeAttribute('title');
565 this.textEl.dom.title = text;
569 onTabClick : function(e){
571 this.tabPanel.activate(this.id);
574 onTabMouseDown : function(e){
576 this.tabPanel.activate(this.id);
579 getWidth : function(){
580 return this.inner.getWidth();
583 setWidth : function(width){
584 var iwidth = width - this.pnode.getPadding("lr");
585 this.inner.setWidth(iwidth);
586 this.textEl.setWidth(iwidth-this.inner.getPadding("lr"));
587 this.pnode.setWidth(width);
591 * Show or hide the tab
592 * @param {Boolean} hidden True to hide or false to show.
594 setHidden : function(hidden){
595 this.hidden = hidden;
596 this.pnode.setStyle("display", hidden ? "none" : "");
600 * Returns true if this tab is "hidden"
603 isHidden : function(){
608 * Returns the text for this tab
611 getText : function(){
615 autoSize : function(){
616 //this.el.beginMeasure();
617 this.textEl.setWidth(1);
618 this.setWidth(this.textEl.dom.scrollWidth+this.pnode.getPadding("lr")+this.inner.getPadding("lr"));
619 //this.el.endMeasure();
623 * Sets the text for the tab (Note: this also sets the tooltip text)
624 * @param {String} text The tab's text and tooltip
626 setText : function(text){
628 this.textEl.update(text);
629 this.setTooltip(text);
630 if(!this.tabPanel.resizeTabs){
635 * Activates this TabPanelItem -- this <b>does</b> deactivate the currently active TabPanelItem.
637 activate : function(){
638 this.tabPanel.activate(this.id);
642 * Disables this TabPanelItem -- this does nothing if this is the active TabPanelItem.
644 disable : function(){
645 if(this.tabPanel.active != this){
646 this.disabled = true;
647 this.pnode.addClass("disabled");
652 * Enables this TabPanelItem if it was previously disabled.
655 this.disabled = false;
656 this.pnode.removeClass("disabled");
660 * Sets the content for this TabPanelItem.
661 * @param {String} content The content
662 * @param {Boolean} loadScripts true to look for and load scripts
664 setContent : function(content, loadScripts){
665 this.bodyEl.update(content, loadScripts);
669 * Gets the {@link Roo.UpdateManager} for the body of this TabPanelItem. Enables you to perform Ajax updates.
670 * @return {Roo.UpdateManager} The UpdateManager
672 getUpdateManager : function(){
673 return this.bodyEl.getUpdateManager();
677 * Set a URL to be used to load the content for this TabPanelItem.
678 * @param {String/Function} url The URL to load the content from, or a function to call to get the URL
679 * @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)
680 * @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)
681 * @return {Roo.UpdateManager} The UpdateManager
683 setUrl : function(url, params, loadOnce){
684 if(this.refreshDelegate){
685 this.un('activate', this.refreshDelegate);
687 this.refreshDelegate = this._handleRefresh.createDelegate(this, [url, params, loadOnce]);
688 this.on("activate", this.refreshDelegate);
689 return this.bodyEl.getUpdateManager();
693 _handleRefresh : function(url, params, loadOnce){
694 if(!loadOnce || !this.loaded){
695 var updater = this.bodyEl.getUpdateManager();
696 updater.update(url, params, this._setLoaded.createDelegate(this));
701 * Forces a content refresh from the URL specified in the {@link #setUrl} method.
702 * Will fail silently if the setUrl method has not been called.
703 * This does not activate the panel, just updates its content.
705 refresh : function(){
706 if(this.refreshDelegate){
708 this.refreshDelegate();
713 _setLoaded : function(){
718 closeClick : function(e){
721 this.fireEvent("beforeclose", this, o);
722 if(o.cancel !== true){
723 this.tabPanel.removeTab(this.id);
727 * The text displayed in the tooltip for the close icon.
730 closeText : "Close this tab"
734 Roo.TabPanel.prototype.createStrip = function(container){
735 var strip = document.createElement("div");
736 strip.className = "x-tabs-wrap";
737 container.appendChild(strip);
741 Roo.TabPanel.prototype.createStripList = function(strip){
742 // div wrapper for retard IE
743 // returns the "tr" element.
744 strip.innerHTML = '<div class="x-tabs-strip-wrap">'+
745 '<table class="x-tabs-strip" cellspacing="0" cellpadding="0" border="0"><tbody><tr>'+
746 '<td class="x-tab-strip-toolbar"></td></tr></tbody></table></div>';
747 return strip.firstChild.firstChild.firstChild.firstChild;
750 Roo.TabPanel.prototype.createBody = function(container){
751 var body = document.createElement("div");
752 Roo.id(body, "tab-body");
753 Roo.fly(body).addClass("x-tabs-body");
754 container.appendChild(body);
758 Roo.TabPanel.prototype.createItemBody = function(bodyEl, id){
759 var body = Roo.getDom(id);
761 body = document.createElement("div");
764 Roo.fly(body).addClass("x-tabs-item-body");
765 bodyEl.insertBefore(body, bodyEl.firstChild);
769 Roo.TabPanel.prototype.createStripElements = function(stripEl, text, closable){
770 var td = document.createElement("td");
771 stripEl.insertBefore(td, stripEl.childNodes[stripEl.childNodes.length-1]);
772 //stripEl.appendChild(td);
774 td.className = "x-tabs-closable";
776 this.closeTpl = new Roo.Template(
777 '<a href="#" class="x-tabs-right"><span class="x-tabs-left"><em class="x-tabs-inner">' +
778 '<span unselectable="on"' + (this.disableTooltips ? '' : ' title="{text}"') +' class="x-tabs-text">{text}</span>' +
779 '<div unselectable="on" class="close-icon"> </div></em></span></a>'
782 var el = this.closeTpl.overwrite(td, {"text": text});
783 var close = el.getElementsByTagName("div")[0];
784 var inner = el.getElementsByTagName("em")[0];
785 return {"el": el, "close": close, "inner": inner};
788 this.tabTpl = new Roo.Template(
789 '<a href="#" class="x-tabs-right"><span class="x-tabs-left"><em class="x-tabs-inner">' +
790 '<span unselectable="on"' + (this.disableTooltips ? '' : ' title="{text}"') +' class="x-tabs-text">{text}</span></em></span></a>'
793 var el = this.tabTpl.overwrite(td, {"text": text});
794 var inner = el.getElementsByTagName("em")[0];
795 return {"el": el, "inner": inner};