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;
108 Roo.TabPanel.superclass.constructor.call(this);
111 Roo.extend(Roo.TabPanel, Roo.util.Observable, {
113 *@cfg {String} tabPosition "top" or "bottom" (defaults to "top")
117 *@cfg {Number} currentTabWidth The width of the current tab (defaults to 0)
121 *@cfg {Number} minTabWidth The minimum width of a tab (defaults to 40) (ignored if {@link #resizeTabs} is not true)
125 *@cfg {Number} maxTabWidth The maximum width of a tab (defaults to 250) (ignored if {@link #resizeTabs} is not true)
129 *@cfg {Number} preferredTabWidth The preferred (default) width of a tab (defaults to 175) (ignored if {@link #resizeTabs} is not true)
131 preferredTabWidth : 175,
133 *@cfg {Boolean} resizeTabs True to enable dynamic tab resizing (defaults to false)
137 *@cfg {Boolean} monitorResize Set this to true to turn on window resize monitoring (ignored if {@link #resizeTabs} is not true) (defaults to true)
139 monitorResize : true,
142 * Creates a new {@link Roo.TabPanelItem} by looking for an existing element with the provided id -- if it's not found it creates one.
143 * @param {String} id The id of the div to use <b>or create</b>
144 * @param {String} text The text for the tab
145 * @param {String} content (optional) Content to put in the TabPanelItem body
146 * @param {Boolean} closable (optional) True to create a close icon on the tab
147 * @return {Roo.TabPanelItem} The created TabPanelItem
149 addTab : function(id, text, content, closable){
150 var item = new Roo.TabPanelItem(this, id, text, closable);
151 this.addTabItem(item);
153 item.setContent(content);
159 * Returns the {@link Roo.TabPanelItem} with the specified id/index
160 * @param {String/Number} id The id or index of the TabPanelItem to fetch.
161 * @return {Roo.TabPanelItem}
163 getTab : function(id){
164 return this.items[id];
168 * Hides the {@link Roo.TabPanelItem} with the specified id/index
169 * @param {String/Number} id The id or index of the TabPanelItem to hide.
171 hideTab : function(id){
172 var t = this.items[id];
181 * "Unhides" the {@link Roo.TabPanelItem} with the specified id/index.
182 * @param {String/Number} id The id or index of the TabPanelItem to unhide.
184 unhideTab : function(id){
185 var t = this.items[id];
194 * Adds an existing {@link Roo.TabPanelItem}.
195 * @param {Roo.TabPanelItem} item The TabPanelItem to add
197 addTabItem : function(item){
198 this.items[item.id] = item;
199 this.items.push(item);
201 item.setWidth(this.currentTabWidth || this.preferredTabWidth);
209 * Removes a {@link Roo.TabPanelItem}.
210 * @param {String/Number} id The id or index of the TabPanelItem to remove.
212 removeTab : function(id){
213 var items = this.items;
216 var index = items.indexOf(tab);
217 if(this.active == tab && items.length > 1){
218 var newTab = this.getNextAvailable(index);
219 if(newTab)newTab.activate();
221 this.stripEl.dom.removeChild(tab.pnode.dom);
222 if(tab.bodyEl.dom.parentNode == this.bodyEl.dom){ // if it was moved already prevent error
223 this.bodyEl.dom.removeChild(tab.bodyEl.dom);
225 items.splice(index, 1);
226 delete this.items[tab.id];
227 tab.fireEvent("close", tab);
228 tab.purgeListeners();
232 getNextAvailable : function(start){
233 var items = this.items;
235 // look for a next tab that will slide over to
236 // replace the one being removed
237 while(index < items.length){
238 var item = items[++index];
239 if(item && !item.isHidden()){
243 // if one isn't found select the previous tab (on the left)
246 var item = items[--index];
247 if(item && !item.isHidden()){
255 * Disables a {@link Roo.TabPanelItem}. It cannot be the active tab, if it is this call is ignored.
256 * @param {String/Number} id The id or index of the TabPanelItem to disable.
258 disableTab : function(id){
259 var tab = this.items[id];
260 if(tab && this.active != tab){
266 * Enables a {@link Roo.TabPanelItem} that is disabled.
267 * @param {String/Number} id The id or index of the TabPanelItem to enable.
269 enableTab : function(id){
270 var tab = this.items[id];
275 * Activates a {@link Roo.TabPanelItem}. The currently active one will be deactivated.
276 * @param {String/Number} id The id or index of the TabPanelItem to activate.
277 * @return {Roo.TabPanelItem} The TabPanelItem.
279 activate : function(id){
280 var tab = this.items[id];
284 if(tab == this.active || tab.disabled){
288 this.fireEvent("beforetabchange", this, e, tab);
289 if(e.cancel !== true && !tab.disabled){
293 this.active = this.items[id];
295 this.fireEvent("tabchange", this, this.active);
301 * Gets the active {@link Roo.TabPanelItem}.
302 * @return {Roo.TabPanelItem} The active TabPanelItem or null if none are active.
304 getActiveTab : function(){
309 * Updates the tab body element to fit the height of the container element
310 * for overflow scrolling
311 * @param {Number} targetHeight (optional) Override the starting height from the elements height
313 syncHeight : function(targetHeight){
314 var height = (targetHeight || this.el.getHeight())-this.el.getBorderWidth("tb")-this.el.getPadding("tb");
315 var bm = this.bodyEl.getMargins();
316 var newHeight = height-(this.stripWrap.getHeight()||0)-(bm.top+bm.bottom);
317 this.bodyEl.setHeight(newHeight);
321 onResize : function(){
322 if(this.monitorResize){
328 * Disables tab resizing while tabs are being added (if {@link #resizeTabs} is false this does nothing)
330 beginUpdate : function(){
331 this.updating = true;
335 * Stops an update and resizes the tabs (if {@link #resizeTabs} is false this does nothing)
337 endUpdate : function(){
338 this.updating = false;
343 * Manual call to resize the tabs (if {@link #resizeTabs} is false this does nothing)
345 autoSizeTabs : function(){
346 var count = this.items.length;
347 var vcount = count - this.hiddenCount;
348 if(!this.resizeTabs || count < 1 || vcount < 1 || this.updating) return;
349 var w = Math.max(this.el.getWidth() - this.cpad, 10);
350 var availWidth = Math.floor(w / vcount);
351 var b = this.stripBody;
352 if(b.getWidth() > w){
353 var tabs = this.items;
354 this.setTabWidth(Math.max(availWidth, this.minTabWidth)-2);
355 if(availWidth < this.minTabWidth){
356 /*if(!this.sleft){ // incomplete scrolling code
357 this.createScrollButtons();
360 this.stripClip.setWidth(w - (this.sleft.getWidth()+this.sright.getWidth()));*/
363 if(this.currentTabWidth < this.preferredTabWidth){
364 this.setTabWidth(Math.min(availWidth, this.preferredTabWidth)-2);
370 * Returns the number of tabs in this TabPanel.
373 getCount : function(){
374 return this.items.length;
378 * Resizes all the tabs to the passed width
379 * @param {Number} The new width
381 setTabWidth : function(width){
382 this.currentTabWidth = width;
383 for(var i = 0, len = this.items.length; i < len; i++) {
384 if(!this.items[i].isHidden())this.items[i].setWidth(width);
389 * Destroys this TabPanel
390 * @param {Boolean} removeEl (optional) True to remove the element from the DOM as well (defaults to undefined)
392 destroy : function(removeEl){
393 Roo.EventManager.removeResizeListener(this.onResize, this);
394 for(var i = 0, len = this.items.length; i < len; i++){
395 this.items[i].purgeListeners();
397 if(removeEl === true){
405 * @class Roo.TabPanelItem
406 * @extends Roo.util.Observable
407 * Represents an individual item (tab plus body) in a TabPanel.
408 * @param {Roo.TabPanel} tabPanel The {@link Roo.TabPanel} this TabPanelItem belongs to
409 * @param {String} id The id of this TabPanelItem
410 * @param {String} text The text for the tab of this TabPanelItem
411 * @param {Boolean} closable True to allow this TabPanelItem to be closable (defaults to false)
413 Roo.TabPanelItem = function(tabPanel, id, text, closable){
415 * The {@link Roo.TabPanel} this TabPanelItem belongs to
418 this.tabPanel = tabPanel;
420 * The id for this TabPanelItem
425 this.disabled = false;
430 this.closable = closable;
433 * The body element for this TabPanelItem.
436 this.bodyEl = Roo.get(tabPanel.createItemBody(tabPanel.bodyEl.dom, id));
437 this.bodyEl.setVisibilityMode(Roo.Element.VISIBILITY);
438 this.bodyEl.setStyle("display", "block");
439 this.bodyEl.setStyle("zoom", "1");
442 var els = tabPanel.createStripElements(tabPanel.stripEl.dom, text, closable);
444 this.el = Roo.get(els.el, true);
445 this.inner = Roo.get(els.inner, true);
446 this.textEl = Roo.get(this.el.dom.firstChild.firstChild.firstChild, true);
447 this.pnode = Roo.get(els.el.parentNode, true);
448 this.el.on("mousedown", this.onTabMouseDown, this);
449 this.el.on("click", this.onTabClick, this);
452 var c = Roo.get(els.close, true);
453 c.dom.title = this.closeText;
454 c.addClassOnOver("close-over");
455 c.on("click", this.closeClick, this);
461 * Fires when this tab becomes the active tab.
462 * @param {Roo.TabPanel} tabPanel The parent TabPanel
463 * @param {Roo.TabPanelItem} this
468 * Fires before this tab is closed. To cancel the close, set cancel to true on e (e.cancel = true).
469 * @param {Roo.TabPanelItem} this
470 * @param {Object} e Set cancel to true on this object to cancel the close.
475 * Fires when this tab is closed.
476 * @param {Roo.TabPanelItem} this
481 * Fires when this tab is no longer the active tab.
482 * @param {Roo.TabPanel} tabPanel The parent TabPanel
483 * @param {Roo.TabPanelItem} this
489 Roo.TabPanelItem.superclass.constructor.call(this);
492 Roo.extend(Roo.TabPanelItem, Roo.util.Observable, {
493 purgeListeners : function(){
494 Roo.util.Observable.prototype.purgeListeners.call(this);
495 this.el.removeAllListeners();
498 * Shows this TabPanelItem -- this <b>does not</b> deactivate the currently active TabPanelItem.
501 this.pnode.addClass("on");
504 this.tabPanel.stripWrap.repaint();
506 this.fireEvent("activate", this.tabPanel, this);
510 * Returns true if this tab is the active tab.
513 isActive : function(){
514 return this.tabPanel.getActiveTab() == this;
518 * Hides this TabPanelItem -- if you don't activate another TabPanelItem this could look odd.
521 this.pnode.removeClass("on");
523 this.fireEvent("deactivate", this.tabPanel, this);
526 hideAction : function(){
528 this.bodyEl.setStyle("position", "absolute");
529 this.bodyEl.setLeft("-20000px");
530 this.bodyEl.setTop("-20000px");
533 showAction : function(){
534 this.bodyEl.setStyle("position", "relative");
535 this.bodyEl.setTop("");
536 this.bodyEl.setLeft("");
541 * Set the tooltip for the tab.
542 * @param {String} tooltip The tab's tooltip
544 setTooltip : function(text){
545 if(Roo.QuickTips && Roo.QuickTips.isEnabled()){
546 this.textEl.dom.qtip = text;
547 this.textEl.dom.removeAttribute('title');
549 this.textEl.dom.title = text;
553 onTabClick : function(e){
555 this.tabPanel.activate(this.id);
558 onTabMouseDown : function(e){
560 this.tabPanel.activate(this.id);
563 getWidth : function(){
564 return this.inner.getWidth();
567 setWidth : function(width){
568 var iwidth = width - this.pnode.getPadding("lr");
569 this.inner.setWidth(iwidth);
570 this.textEl.setWidth(iwidth-this.inner.getPadding("lr"));
571 this.pnode.setWidth(width);
575 * Show or hide the tab
576 * @param {Boolean} hidden True to hide or false to show.
578 setHidden : function(hidden){
579 this.hidden = hidden;
580 this.pnode.setStyle("display", hidden ? "none" : "");
584 * Returns true if this tab is "hidden"
587 isHidden : function(){
592 * Returns the text for this tab
595 getText : function(){
599 autoSize : function(){
600 //this.el.beginMeasure();
601 this.textEl.setWidth(1);
602 this.setWidth(this.textEl.dom.scrollWidth+this.pnode.getPadding("lr")+this.inner.getPadding("lr"));
603 //this.el.endMeasure();
607 * Sets the text for the tab (Note: this also sets the tooltip text)
608 * @param {String} text The tab's text and tooltip
610 setText : function(text){
612 this.textEl.update(text);
613 this.setTooltip(text);
614 if(!this.tabPanel.resizeTabs){
619 * Activates this TabPanelItem -- this <b>does</b> deactivate the currently active TabPanelItem.
621 activate : function(){
622 this.tabPanel.activate(this.id);
626 * Disables this TabPanelItem -- this does nothing if this is the active TabPanelItem.
628 disable : function(){
629 if(this.tabPanel.active != this){
630 this.disabled = true;
631 this.pnode.addClass("disabled");
636 * Enables this TabPanelItem if it was previously disabled.
639 this.disabled = false;
640 this.pnode.removeClass("disabled");
644 * Sets the content for this TabPanelItem.
645 * @param {String} content The content
646 * @param {Boolean} loadScripts true to look for and load scripts
648 setContent : function(content, loadScripts){
649 this.bodyEl.update(content, loadScripts);
653 * Gets the {@link Roo.UpdateManager} for the body of this TabPanelItem. Enables you to perform Ajax updates.
654 * @return {Roo.UpdateManager} The UpdateManager
656 getUpdateManager : function(){
657 return this.bodyEl.getUpdateManager();
661 * Set a URL to be used to load the content for this TabPanelItem.
662 * @param {String/Function} url The URL to load the content from, or a function to call to get the URL
663 * @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)
664 * @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)
665 * @return {Roo.UpdateManager} The UpdateManager
667 setUrl : function(url, params, loadOnce){
668 if(this.refreshDelegate){
669 this.un('activate', this.refreshDelegate);
671 this.refreshDelegate = this._handleRefresh.createDelegate(this, [url, params, loadOnce]);
672 this.on("activate", this.refreshDelegate);
673 return this.bodyEl.getUpdateManager();
677 _handleRefresh : function(url, params, loadOnce){
678 if(!loadOnce || !this.loaded){
679 var updater = this.bodyEl.getUpdateManager();
680 updater.update(url, params, this._setLoaded.createDelegate(this));
685 * Forces a content refresh from the URL specified in the {@link #setUrl} method.
686 * Will fail silently if the setUrl method has not been called.
687 * This does not activate the panel, just updates its content.
689 refresh : function(){
690 if(this.refreshDelegate){
692 this.refreshDelegate();
697 _setLoaded : function(){
702 closeClick : function(e){
705 this.fireEvent("beforeclose", this, o);
706 if(o.cancel !== true){
707 this.tabPanel.removeTab(this.id);
711 * The text displayed in the tooltip for the close icon.
714 closeText : "Close this tab"
718 Roo.TabPanel.prototype.createStrip = function(container){
719 var strip = document.createElement("div");
720 strip.className = "x-tabs-wrap";
721 container.appendChild(strip);
725 Roo.TabPanel.prototype.createStripList = function(strip){
726 // div wrapper for retard IE
727 strip.innerHTML = '<div class="x-tabs-strip-wrap"><table class="x-tabs-strip" cellspacing="0" cellpadding="0" border="0"><tbody><tr></tr></tbody></table></div>';
728 return strip.firstChild.firstChild.firstChild.firstChild;
731 Roo.TabPanel.prototype.createBody = function(container){
732 var body = document.createElement("div");
733 Roo.id(body, "tab-body");
734 Roo.fly(body).addClass("x-tabs-body");
735 container.appendChild(body);
739 Roo.TabPanel.prototype.createItemBody = function(bodyEl, id){
740 var body = Roo.getDom(id);
742 body = document.createElement("div");
745 Roo.fly(body).addClass("x-tabs-item-body");
746 bodyEl.insertBefore(body, bodyEl.firstChild);
750 Roo.TabPanel.prototype.createStripElements = function(stripEl, text, closable){
751 var td = document.createElement("td");
752 stripEl.appendChild(td);
754 td.className = "x-tabs-closable";
756 this.closeTpl = new Roo.Template(
757 '<a href="#" class="x-tabs-right"><span class="x-tabs-left"><em class="x-tabs-inner">' +
758 '<span unselectable="on"' + (this.disableTooltips ? '' : ' title="{text}"') +' class="x-tabs-text">{text}</span>' +
759 '<div unselectable="on" class="close-icon"> </div></em></span></a>'
762 var el = this.closeTpl.overwrite(td, {"text": text});
763 var close = el.getElementsByTagName("div")[0];
764 var inner = el.getElementsByTagName("em")[0];
765 return {"el": el, "close": close, "inner": inner};
768 this.tabTpl = new Roo.Template(
769 '<a href="#" class="x-tabs-right"><span class="x-tabs-left"><em class="x-tabs-inner">' +
770 '<span unselectable="on"' + (this.disableTooltips ? '' : ' title="{text}"') +' class="x-tabs-text">{text}</span></em></span></a>'
773 var el = this.tabTpl.overwrite(td, {"text": text});
774 var inner = el.getElementsByTagName("em")[0];
775 return {"el": el, "inner": inner};