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 se = this.stripEl;
112 var tbh = se.child('td.x-tab-strip-toolbar'); //createChild( { tag: 'td', style: 'width:100%;' });
115 var lotb = new Roo.Toolbar(tbh);
120 Roo.TabPanel.superclass.constructor.call(this);
123 Roo.extend(Roo.TabPanel, Roo.util.Observable, {
125 *@cfg {String} tabPosition "top" or "bottom" (defaults to "top")
129 *@cfg {Number} currentTabWidth The width of the current tab (defaults to 0)
133 *@cfg {Number} minTabWidth The minimum width of a tab (defaults to 40) (ignored if {@link #resizeTabs} is not true)
137 *@cfg {Number} maxTabWidth The maximum width of a tab (defaults to 250) (ignored if {@link #resizeTabs} is not true)
141 *@cfg {Number} preferredTabWidth The preferred (default) width of a tab (defaults to 175) (ignored if {@link #resizeTabs} is not true)
143 preferredTabWidth : 175,
145 *@cfg {Boolean} resizeTabs True to enable dynamic tab resizing (defaults to false)
149 *@cfg {Boolean} monitorResize Set this to true to turn on window resize monitoring (ignored if {@link #resizeTabs} is not true) (defaults to true)
151 monitorResize : true,
153 *@cfg {Object} toolbar xtype description of toolbar to show at the right of the tab bar.
158 * Creates a new {@link Roo.TabPanelItem} by looking for an existing element with the provided id -- if it's not found it creates one.
159 * @param {String} id The id of the div to use <b>or create</b>
160 * @param {String} text The text for the tab
161 * @param {String} content (optional) Content to put in the TabPanelItem body
162 * @param {Boolean} closable (optional) True to create a close icon on the tab
163 * @return {Roo.TabPanelItem} The created TabPanelItem
165 addTab : function(id, text, content, closable){
166 var item = new Roo.TabPanelItem(this, id, text, closable);
167 this.addTabItem(item);
169 item.setContent(content);
175 * Returns the {@link Roo.TabPanelItem} with the specified id/index
176 * @param {String/Number} id The id or index of the TabPanelItem to fetch.
177 * @return {Roo.TabPanelItem}
179 getTab : function(id){
180 return this.items[id];
184 * Hides the {@link Roo.TabPanelItem} with the specified id/index
185 * @param {String/Number} id The id or index of the TabPanelItem to hide.
187 hideTab : function(id){
188 var t = this.items[id];
197 * "Unhides" the {@link Roo.TabPanelItem} with the specified id/index.
198 * @param {String/Number} id The id or index of the TabPanelItem to unhide.
200 unhideTab : function(id){
201 var t = this.items[id];
210 * Adds an existing {@link Roo.TabPanelItem}.
211 * @param {Roo.TabPanelItem} item The TabPanelItem to add
213 addTabItem : function(item){
214 this.items[item.id] = item;
215 this.items.push(item);
217 item.setWidth(this.currentTabWidth || this.preferredTabWidth);
225 * Removes a {@link Roo.TabPanelItem}.
226 * @param {String/Number} id The id or index of the TabPanelItem to remove.
228 removeTab : function(id){
229 var items = this.items;
232 var index = items.indexOf(tab);
233 if(this.active == tab && items.length > 1){
234 var newTab = this.getNextAvailable(index);
239 this.stripEl.dom.removeChild(tab.pnode.dom);
240 if(tab.bodyEl.dom.parentNode == this.bodyEl.dom){ // if it was moved already prevent error
241 this.bodyEl.dom.removeChild(tab.bodyEl.dom);
243 items.splice(index, 1);
244 delete this.items[tab.id];
245 tab.fireEvent("close", tab);
246 tab.purgeListeners();
250 getNextAvailable : function(start){
251 var items = this.items;
253 // look for a next tab that will slide over to
254 // replace the one being removed
255 while(index < items.length){
256 var item = items[++index];
257 if(item && !item.isHidden()){
261 // if one isn't found select the previous tab (on the left)
264 var item = items[--index];
265 if(item && !item.isHidden()){
273 * Disables a {@link Roo.TabPanelItem}. It cannot be the active tab, if it is this call is ignored.
274 * @param {String/Number} id The id or index of the TabPanelItem to disable.
276 disableTab : function(id){
277 var tab = this.items[id];
278 if(tab && this.active != tab){
284 * Enables a {@link Roo.TabPanelItem} that is disabled.
285 * @param {String/Number} id The id or index of the TabPanelItem to enable.
287 enableTab : function(id){
288 var tab = this.items[id];
293 * Activates a {@link Roo.TabPanelItem}. The currently active one will be deactivated.
294 * @param {String/Number} id The id or index of the TabPanelItem to activate.
295 * @return {Roo.TabPanelItem} The TabPanelItem.
297 activate : function(id){
298 var tab = this.items[id];
302 if(tab == this.active || tab.disabled){
306 this.fireEvent("beforetabchange", this, e, tab);
307 if(e.cancel !== true && !tab.disabled){
311 this.active = this.items[id];
313 this.fireEvent("tabchange", this, this.active);
319 * Gets the active {@link Roo.TabPanelItem}.
320 * @return {Roo.TabPanelItem} The active TabPanelItem or null if none are active.
322 getActiveTab : function(){
327 * Updates the tab body element to fit the height of the container element
328 * for overflow scrolling
329 * @param {Number} targetHeight (optional) Override the starting height from the elements height
331 syncHeight : function(targetHeight){
332 var height = (targetHeight || this.el.getHeight())-this.el.getBorderWidth("tb")-this.el.getPadding("tb");
333 var bm = this.bodyEl.getMargins();
334 var newHeight = height-(this.stripWrap.getHeight()||0)-(bm.top+bm.bottom);
335 this.bodyEl.setHeight(newHeight);
339 onResize : function(){
340 if(this.monitorResize){
346 * Disables tab resizing while tabs are being added (if {@link #resizeTabs} is false this does nothing)
348 beginUpdate : function(){
349 this.updating = true;
353 * Stops an update and resizes the tabs (if {@link #resizeTabs} is false this does nothing)
355 endUpdate : function(){
356 this.updating = false;
361 * Manual call to resize the tabs (if {@link #resizeTabs} is false this does nothing)
363 autoSizeTabs : function(){
364 var count = this.items.length;
365 var vcount = count - this.hiddenCount;
366 if(!this.resizeTabs || count < 1 || vcount < 1 || this.updating) return;
367 var w = Math.max(this.el.getWidth() - this.cpad, 10);
368 var availWidth = Math.floor(w / vcount);
369 var b = this.stripBody;
370 if(b.getWidth() > w){
371 var tabs = this.items;
372 this.setTabWidth(Math.max(availWidth, this.minTabWidth)-2);
373 if(availWidth < this.minTabWidth){
374 /*if(!this.sleft){ // incomplete scrolling code
375 this.createScrollButtons();
378 this.stripClip.setWidth(w - (this.sleft.getWidth()+this.sright.getWidth()));*/
381 if(this.currentTabWidth < this.preferredTabWidth){
382 this.setTabWidth(Math.min(availWidth, this.preferredTabWidth)-2);
388 * Returns the number of tabs in this TabPanel.
391 getCount : function(){
392 return this.items.length;
396 * Resizes all the tabs to the passed width
397 * @param {Number} The new width
399 setTabWidth : function(width){
400 this.currentTabWidth = width;
401 for(var i = 0, len = this.items.length; i < len; i++) {
402 if(!this.items[i].isHidden())this.items[i].setWidth(width);
407 * Destroys this TabPanel
408 * @param {Boolean} removeEl (optional) True to remove the element from the DOM as well (defaults to undefined)
410 destroy : function(removeEl){
411 Roo.EventManager.removeResizeListener(this.onResize, this);
412 for(var i = 0, len = this.items.length; i < len; i++){
413 this.items[i].purgeListeners();
415 if(removeEl === true){
423 * @class Roo.TabPanelItem
424 * @extends Roo.util.Observable
425 * Represents an individual item (tab plus body) in a TabPanel.
426 * @param {Roo.TabPanel} tabPanel The {@link Roo.TabPanel} this TabPanelItem belongs to
427 * @param {String} id The id of this TabPanelItem
428 * @param {String} text The text for the tab of this TabPanelItem
429 * @param {Boolean} closable True to allow this TabPanelItem to be closable (defaults to false)
431 Roo.TabPanelItem = function(tabPanel, id, text, closable){
433 * The {@link Roo.TabPanel} this TabPanelItem belongs to
436 this.tabPanel = tabPanel;
438 * The id for this TabPanelItem
443 this.disabled = false;
448 this.closable = closable;
451 * The body element for this TabPanelItem.
454 this.bodyEl = Roo.get(tabPanel.createItemBody(tabPanel.bodyEl.dom, id));
455 this.bodyEl.setVisibilityMode(Roo.Element.VISIBILITY);
456 this.bodyEl.setStyle("display", "block");
457 this.bodyEl.setStyle("zoom", "1");
460 var els = tabPanel.createStripElements(tabPanel.stripEl.dom, text, closable);
462 this.el = Roo.get(els.el, true);
463 this.inner = Roo.get(els.inner, true);
464 this.textEl = Roo.get(this.el.dom.firstChild.firstChild.firstChild, true);
465 this.pnode = Roo.get(els.el.parentNode, true);
466 this.el.on("mousedown", this.onTabMouseDown, this);
467 this.el.on("click", this.onTabClick, this);
470 var c = Roo.get(els.close, true);
471 c.dom.title = this.closeText;
472 c.addClassOnOver("close-over");
473 c.on("click", this.closeClick, this);
479 * Fires when this tab becomes the active tab.
480 * @param {Roo.TabPanel} tabPanel The parent TabPanel
481 * @param {Roo.TabPanelItem} this
486 * Fires before this tab is closed. To cancel the close, set cancel to true on e (e.cancel = true).
487 * @param {Roo.TabPanelItem} this
488 * @param {Object} e Set cancel to true on this object to cancel the close.
493 * Fires when this tab is closed.
494 * @param {Roo.TabPanelItem} this
499 * Fires when this tab is no longer the active tab.
500 * @param {Roo.TabPanel} tabPanel The parent TabPanel
501 * @param {Roo.TabPanelItem} this
507 Roo.TabPanelItem.superclass.constructor.call(this);
510 Roo.extend(Roo.TabPanelItem, Roo.util.Observable, {
511 purgeListeners : function(){
512 Roo.util.Observable.prototype.purgeListeners.call(this);
513 this.el.removeAllListeners();
516 * Shows this TabPanelItem -- this <b>does not</b> deactivate the currently active TabPanelItem.
519 this.pnode.addClass("on");
522 this.tabPanel.stripWrap.repaint();
524 this.fireEvent("activate", this.tabPanel, this);
528 * Returns true if this tab is the active tab.
531 isActive : function(){
532 return this.tabPanel.getActiveTab() == this;
536 * Hides this TabPanelItem -- if you don't activate another TabPanelItem this could look odd.
539 this.pnode.removeClass("on");
541 this.fireEvent("deactivate", this.tabPanel, this);
544 hideAction : function(){
546 this.bodyEl.setStyle("position", "absolute");
547 this.bodyEl.setLeft("-20000px");
548 this.bodyEl.setTop("-20000px");
551 showAction : function(){
552 this.bodyEl.setStyle("position", "relative");
553 this.bodyEl.setTop("");
554 this.bodyEl.setLeft("");
559 * Set the tooltip for the tab.
560 * @param {String} tooltip The tab's tooltip
562 setTooltip : function(text){
563 if(Roo.QuickTips && Roo.QuickTips.isEnabled()){
564 this.textEl.dom.qtip = text;
565 this.textEl.dom.removeAttribute('title');
567 this.textEl.dom.title = text;
571 onTabClick : function(e){
573 this.tabPanel.activate(this.id);
576 onTabMouseDown : function(e){
578 this.tabPanel.activate(this.id);
581 getWidth : function(){
582 return this.inner.getWidth();
585 setWidth : function(width){
586 var iwidth = width - this.pnode.getPadding("lr");
587 this.inner.setWidth(iwidth);
588 this.textEl.setWidth(iwidth-this.inner.getPadding("lr"));
589 this.pnode.setWidth(width);
593 * Show or hide the tab
594 * @param {Boolean} hidden True to hide or false to show.
596 setHidden : function(hidden){
597 this.hidden = hidden;
598 this.pnode.setStyle("display", hidden ? "none" : "");
602 * Returns true if this tab is "hidden"
605 isHidden : function(){
610 * Returns the text for this tab
613 getText : function(){
617 autoSize : function(){
618 //this.el.beginMeasure();
619 this.textEl.setWidth(1);
620 this.setWidth(this.textEl.dom.scrollWidth+this.pnode.getPadding("lr")+this.inner.getPadding("lr"));
621 //this.el.endMeasure();
625 * Sets the text for the tab (Note: this also sets the tooltip text)
626 * @param {String} text The tab's text and tooltip
628 setText : function(text){
630 this.textEl.update(text);
631 this.setTooltip(text);
632 if(!this.tabPanel.resizeTabs){
637 * Activates this TabPanelItem -- this <b>does</b> deactivate the currently active TabPanelItem.
639 activate : function(){
640 this.tabPanel.activate(this.id);
644 * Disables this TabPanelItem -- this does nothing if this is the active TabPanelItem.
646 disable : function(){
647 if(this.tabPanel.active != this){
648 this.disabled = true;
649 this.pnode.addClass("disabled");
654 * Enables this TabPanelItem if it was previously disabled.
657 this.disabled = false;
658 this.pnode.removeClass("disabled");
662 * Sets the content for this TabPanelItem.
663 * @param {String} content The content
664 * @param {Boolean} loadScripts true to look for and load scripts
666 setContent : function(content, loadScripts){
667 this.bodyEl.update(content, loadScripts);
671 * Gets the {@link Roo.UpdateManager} for the body of this TabPanelItem. Enables you to perform Ajax updates.
672 * @return {Roo.UpdateManager} The UpdateManager
674 getUpdateManager : function(){
675 return this.bodyEl.getUpdateManager();
679 * Set a URL to be used to load the content for this TabPanelItem.
680 * @param {String/Function} url The URL to load the content from, or a function to call to get the URL
681 * @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)
682 * @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)
683 * @return {Roo.UpdateManager} The UpdateManager
685 setUrl : function(url, params, loadOnce){
686 if(this.refreshDelegate){
687 this.un('activate', this.refreshDelegate);
689 this.refreshDelegate = this._handleRefresh.createDelegate(this, [url, params, loadOnce]);
690 this.on("activate", this.refreshDelegate);
691 return this.bodyEl.getUpdateManager();
695 _handleRefresh : function(url, params, loadOnce){
696 if(!loadOnce || !this.loaded){
697 var updater = this.bodyEl.getUpdateManager();
698 updater.update(url, params, this._setLoaded.createDelegate(this));
703 * Forces a content refresh from the URL specified in the {@link #setUrl} method.
704 * Will fail silently if the setUrl method has not been called.
705 * This does not activate the panel, just updates its content.
707 refresh : function(){
708 if(this.refreshDelegate){
710 this.refreshDelegate();
715 _setLoaded : function(){
720 closeClick : function(e){
723 this.fireEvent("beforeclose", this, o);
724 if(o.cancel !== true){
725 this.tabPanel.removeTab(this.id);
729 * The text displayed in the tooltip for the close icon.
732 closeText : "Close this tab"
736 Roo.TabPanel.prototype.createStrip = function(container){
737 var strip = document.createElement("div");
738 strip.className = "x-tabs-wrap";
739 container.appendChild(strip);
743 Roo.TabPanel.prototype.createStripList = function(strip){
744 // div wrapper for retard IE
745 // returns the "tr" element.
746 strip.innerHTML = '<div class="x-tabs-strip-wrap">'+
747 '<table class="x-tabs-strip" cellspacing="0" cellpadding="0" border="0"><tbody><tr>'+
748 '<td class="x-tab-strip-toolbar"></td></tr></tbody></table></div>';
749 return strip.firstChild.firstChild.firstChild.firstChild;
752 Roo.TabPanel.prototype.createBody = function(container){
753 var body = document.createElement("div");
754 Roo.id(body, "tab-body");
755 Roo.fly(body).addClass("x-tabs-body");
756 container.appendChild(body);
760 Roo.TabPanel.prototype.createItemBody = function(bodyEl, id){
761 var body = Roo.getDom(id);
763 body = document.createElement("div");
766 Roo.fly(body).addClass("x-tabs-item-body");
767 bodyEl.insertBefore(body, bodyEl.firstChild);
771 Roo.TabPanel.prototype.createStripElements = function(stripEl, text, closable){
772 var td = document.createElement("td");
773 stripEl.insertBefore(td, stripEl.childNodes[stripEl.childNodes.length-1]);
774 //stripEl.appendChild(td);
776 td.className = "x-tabs-closable";
778 this.closeTpl = new Roo.Template(
779 '<a href="#" class="x-tabs-right"><span class="x-tabs-left"><em class="x-tabs-inner">' +
780 '<span unselectable="on"' + (this.disableTooltips ? '' : ' title="{text}"') +' class="x-tabs-text">{text}</span>' +
781 '<div unselectable="on" class="close-icon"> </div></em></span></a>'
784 var el = this.closeTpl.overwrite(td, {"text": text});
785 var close = el.getElementsByTagName("div")[0];
786 var inner = el.getElementsByTagName("em")[0];
787 return {"el": el, "close": close, "inner": inner};
790 this.tabTpl = new Roo.Template(
791 '<a href="#" class="x-tabs-right"><span class="x-tabs-left"><em class="x-tabs-inner">' +
792 '<span unselectable="on"' + (this.disableTooltips ? '' : ' title="{text}"') +' class="x-tabs-text">{text}</span></em></span></a>'
795 var el = this.tabTpl.overwrite(td, {"text": text});
796 var inner = el.getElementsByTagName("em")[0];
797 return {"el": el, "inner": inner};