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 Roo.log('Running addTabItem!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
218 this.items[item.id] = item;
219 this.items.push(item);
221 item.setWidth(this.currentTabWidth || this.preferredTabWidth);
229 * Removes a {@link Roo.TabPanelItem}.
230 * @param {String/Number} id The id or index of the TabPanelItem to remove.
232 removeTab : function(id){
233 var items = this.items;
236 var index = items.indexOf(tab);
237 if(this.active == tab && items.length > 1){
238 var newTab = this.getNextAvailable(index);
243 this.stripEl.dom.removeChild(tab.pnode.dom);
244 if(tab.bodyEl.dom.parentNode == this.bodyEl.dom){ // if it was moved already prevent error
245 this.bodyEl.dom.removeChild(tab.bodyEl.dom);
247 items.splice(index, 1);
248 delete this.items[tab.id];
249 tab.fireEvent("close", tab);
250 tab.purgeListeners();
254 getNextAvailable : function(start){
255 var items = this.items;
257 // look for a next tab that will slide over to
258 // replace the one being removed
259 while(index < items.length){
260 var item = items[++index];
261 if(item && !item.isHidden()){
265 // if one isn't found select the previous tab (on the left)
268 var item = items[--index];
269 if(item && !item.isHidden()){
277 * Disables a {@link Roo.TabPanelItem}. It cannot be the active tab, if it is this call is ignored.
278 * @param {String/Number} id The id or index of the TabPanelItem to disable.
280 disableTab : function(id){
281 var tab = this.items[id];
282 if(tab && this.active != tab){
288 * Enables a {@link Roo.TabPanelItem} that is disabled.
289 * @param {String/Number} id The id or index of the TabPanelItem to enable.
291 enableTab : function(id){
292 var tab = this.items[id];
297 * Activates a {@link Roo.TabPanelItem}. The currently active one will be deactivated.
298 * @param {String/Number} id The id or index of the TabPanelItem to activate.
299 * @return {Roo.TabPanelItem} The TabPanelItem.
301 activate : function(id){
302 var tab = this.items[id];
306 if(tab == this.active || tab.disabled){
310 this.fireEvent("beforetabchange", this, e, tab);
311 if(e.cancel !== true && !tab.disabled){
315 this.active = this.items[id];
317 this.fireEvent("tabchange", this, this.active);
323 * Gets the active {@link Roo.TabPanelItem}.
324 * @return {Roo.TabPanelItem} The active TabPanelItem or null if none are active.
326 getActiveTab : function(){
331 * Updates the tab body element to fit the height of the container element
332 * for overflow scrolling
333 * @param {Number} targetHeight (optional) Override the starting height from the elements height
335 syncHeight : function(targetHeight){
336 var height = (targetHeight || this.el.getHeight())-this.el.getBorderWidth("tb")-this.el.getPadding("tb");
337 var bm = this.bodyEl.getMargins();
338 var newHeight = height-(this.stripWrap.getHeight()||0)-(bm.top+bm.bottom);
339 this.bodyEl.setHeight(newHeight);
343 onResize : function(){
344 if(this.monitorResize){
350 * Disables tab resizing while tabs are being added (if {@link #resizeTabs} is false this does nothing)
352 beginUpdate : function(){
353 this.updating = true;
357 * Stops an update and resizes the tabs (if {@link #resizeTabs} is false this does nothing)
359 endUpdate : function(){
360 this.updating = false;
365 * Manual call to resize the tabs (if {@link #resizeTabs} is false this does nothing)
367 autoSizeTabs : function(){
368 var count = this.items.length;
369 var vcount = count - this.hiddenCount;
370 if(!this.resizeTabs || count < 1 || vcount < 1 || this.updating) return;
371 var w = Math.max(this.el.getWidth() - this.cpad, 10);
372 var availWidth = Math.floor(w / vcount);
373 var b = this.stripBody;
374 if(b.getWidth() > w){
375 var tabs = this.items;
376 this.setTabWidth(Math.max(availWidth, this.minTabWidth)-2);
377 if(availWidth < this.minTabWidth){
378 /*if(!this.sleft){ // incomplete scrolling code
379 this.createScrollButtons();
382 this.stripClip.setWidth(w - (this.sleft.getWidth()+this.sright.getWidth()));*/
385 if(this.currentTabWidth < this.preferredTabWidth){
386 this.setTabWidth(Math.min(availWidth, this.preferredTabWidth)-2);
392 * Returns the number of tabs in this TabPanel.
395 getCount : function(){
396 return this.items.length;
400 * Resizes all the tabs to the passed width
401 * @param {Number} The new width
403 setTabWidth : function(width){
404 this.currentTabWidth = width;
405 for(var i = 0, len = this.items.length; i < len; i++) {
406 if(!this.items[i].isHidden())this.items[i].setWidth(width);
411 * Destroys this TabPanel
412 * @param {Boolean} removeEl (optional) True to remove the element from the DOM as well (defaults to undefined)
414 destroy : function(removeEl){
415 Roo.EventManager.removeResizeListener(this.onResize, this);
416 for(var i = 0, len = this.items.length; i < len; i++){
417 this.items[i].purgeListeners();
419 if(removeEl === true){
427 * @class Roo.TabPanelItem
428 * @extends Roo.util.Observable
429 * Represents an individual item (tab plus body) in a TabPanel.
430 * @param {Roo.TabPanel} tabPanel The {@link Roo.TabPanel} this TabPanelItem belongs to
431 * @param {String} id The id of this TabPanelItem
432 * @param {String} text The text for the tab of this TabPanelItem
433 * @param {Boolean} closable True to allow this TabPanelItem to be closable (defaults to false)
435 Roo.TabPanelItem = function(tabPanel, id, text, closable){
437 * The {@link Roo.TabPanel} this TabPanelItem belongs to
440 this.tabPanel = tabPanel;
442 * The id for this TabPanelItem
447 this.disabled = false;
452 this.closable = closable;
455 * The body element for this TabPanelItem.
458 this.bodyEl = Roo.get(tabPanel.createItemBody(tabPanel.bodyEl.dom, id));
459 this.bodyEl.setVisibilityMode(Roo.Element.VISIBILITY);
460 this.bodyEl.setStyle("display", "block");
461 this.bodyEl.setStyle("zoom", "1");
464 var els = tabPanel.createStripElements(tabPanel.stripEl.dom, text, closable);
466 this.el = Roo.get(els.el, true);
467 this.inner = Roo.get(els.inner, true);
468 this.textEl = Roo.get(this.el.dom.firstChild.firstChild.firstChild, true);
469 this.pnode = Roo.get(els.el.parentNode, true);
470 this.el.on("mousedown", this.onTabMouseDown, this);
471 this.el.on("click", this.onTabClick, this);
474 var c = Roo.get(els.close, true);
475 c.dom.title = this.closeText;
476 c.addClassOnOver("close-over");
477 c.on("click", this.closeClick, this);
483 * Fires when this tab becomes the active tab.
484 * @param {Roo.TabPanel} tabPanel The parent TabPanel
485 * @param {Roo.TabPanelItem} this
490 * Fires before this tab is closed. To cancel the close, set cancel to true on e (e.cancel = true).
491 * @param {Roo.TabPanelItem} this
492 * @param {Object} e Set cancel to true on this object to cancel the close.
497 * Fires when this tab is closed.
498 * @param {Roo.TabPanelItem} this
503 * Fires when this tab is no longer the active tab.
504 * @param {Roo.TabPanel} tabPanel The parent TabPanel
505 * @param {Roo.TabPanelItem} this
511 Roo.TabPanelItem.superclass.constructor.call(this);
514 Roo.extend(Roo.TabPanelItem, Roo.util.Observable, {
515 purgeListeners : function(){
516 Roo.util.Observable.prototype.purgeListeners.call(this);
517 this.el.removeAllListeners();
520 * Shows this TabPanelItem -- this <b>does not</b> deactivate the currently active TabPanelItem.
523 this.pnode.addClass("on");
526 this.tabPanel.stripWrap.repaint();
528 this.fireEvent("activate", this.tabPanel, this);
532 * Returns true if this tab is the active tab.
535 isActive : function(){
536 return this.tabPanel.getActiveTab() == this;
540 * Hides this TabPanelItem -- if you don't activate another TabPanelItem this could look odd.
543 this.pnode.removeClass("on");
545 this.fireEvent("deactivate", this.tabPanel, this);
548 hideAction : function(){
550 this.bodyEl.setStyle("position", "absolute");
551 this.bodyEl.setLeft("-20000px");
552 this.bodyEl.setTop("-20000px");
555 showAction : function(){
556 this.bodyEl.setStyle("position", "relative");
557 this.bodyEl.setTop("");
558 this.bodyEl.setLeft("");
563 * Set the tooltip for the tab.
564 * @param {String} tooltip The tab's tooltip
566 setTooltip : function(text){
567 if(Roo.QuickTips && Roo.QuickTips.isEnabled()){
568 this.textEl.dom.qtip = text;
569 this.textEl.dom.removeAttribute('title');
571 this.textEl.dom.title = text;
575 onTabClick : function(e){
577 this.tabPanel.activate(this.id);
580 onTabMouseDown : function(e){
582 this.tabPanel.activate(this.id);
585 getWidth : function(){
586 return this.inner.getWidth();
589 setWidth : function(width){
590 var iwidth = width - this.pnode.getPadding("lr");
591 this.inner.setWidth(iwidth);
592 this.textEl.setWidth(iwidth-this.inner.getPadding("lr"));
593 this.pnode.setWidth(width);
597 * Show or hide the tab
598 * @param {Boolean} hidden True to hide or false to show.
600 setHidden : function(hidden){
601 this.hidden = hidden;
602 this.pnode.setStyle("display", hidden ? "none" : "");
606 * Returns true if this tab is "hidden"
609 isHidden : function(){
614 * Returns the text for this tab
617 getText : function(){
621 autoSize : function(){
622 //this.el.beginMeasure();
623 this.textEl.setWidth(1);
624 this.setWidth(this.textEl.dom.scrollWidth+this.pnode.getPadding("lr")+this.inner.getPadding("lr"));
625 //this.el.endMeasure();
629 * Sets the text for the tab (Note: this also sets the tooltip text)
630 * @param {String} text The tab's text and tooltip
632 setText : function(text){
634 this.textEl.update(text);
635 this.setTooltip(text);
636 if(!this.tabPanel.resizeTabs){
641 * Activates this TabPanelItem -- this <b>does</b> deactivate the currently active TabPanelItem.
643 activate : function(){
644 this.tabPanel.activate(this.id);
648 * Disables this TabPanelItem -- this does nothing if this is the active TabPanelItem.
650 disable : function(){
651 if(this.tabPanel.active != this){
652 this.disabled = true;
653 this.pnode.addClass("disabled");
658 * Enables this TabPanelItem if it was previously disabled.
661 this.disabled = false;
662 this.pnode.removeClass("disabled");
666 * Sets the content for this TabPanelItem.
667 * @param {String} content The content
668 * @param {Boolean} loadScripts true to look for and load scripts
670 setContent : function(content, loadScripts){
671 this.bodyEl.update(content, loadScripts);
675 * Gets the {@link Roo.UpdateManager} for the body of this TabPanelItem. Enables you to perform Ajax updates.
676 * @return {Roo.UpdateManager} The UpdateManager
678 getUpdateManager : function(){
679 return this.bodyEl.getUpdateManager();
683 * Set a URL to be used to load the content for this TabPanelItem.
684 * @param {String/Function} url The URL to load the content from, or a function to call to get the URL
685 * @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)
686 * @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)
687 * @return {Roo.UpdateManager} The UpdateManager
689 setUrl : function(url, params, loadOnce){
690 if(this.refreshDelegate){
691 this.un('activate', this.refreshDelegate);
693 this.refreshDelegate = this._handleRefresh.createDelegate(this, [url, params, loadOnce]);
694 this.on("activate", this.refreshDelegate);
695 return this.bodyEl.getUpdateManager();
699 _handleRefresh : function(url, params, loadOnce){
700 if(!loadOnce || !this.loaded){
701 var updater = this.bodyEl.getUpdateManager();
702 updater.update(url, params, this._setLoaded.createDelegate(this));
707 * Forces a content refresh from the URL specified in the {@link #setUrl} method.
708 * Will fail silently if the setUrl method has not been called.
709 * This does not activate the panel, just updates its content.
711 refresh : function(){
712 if(this.refreshDelegate){
714 this.refreshDelegate();
719 _setLoaded : function(){
724 closeClick : function(e){
727 this.fireEvent("beforeclose", this, o);
728 if(o.cancel !== true){
729 this.tabPanel.removeTab(this.id);
733 * The text displayed in the tooltip for the close icon.
736 closeText : "Close this tab"
740 Roo.TabPanel.prototype.createStrip = function(container){
741 var strip = document.createElement("div");
742 strip.className = "x-tabs-wrap";
743 container.appendChild(strip);
747 Roo.TabPanel.prototype.createStripList = function(strip){
748 // div wrapper for retard IE
749 // returns the "tr" element.
750 strip.innerHTML = '<div class="x-tabs-strip-wrap">'+
751 '<table class="x-tabs-strip" cellspacing="0" cellpadding="0" border="0"><tbody><tr>'+
752 '<td class="x-tab-strip-toolbar"></td></tr></tbody></table></div>';
753 return strip.firstChild.firstChild.firstChild.firstChild;
756 Roo.TabPanel.prototype.createBody = function(container){
757 var body = document.createElement("div");
758 Roo.id(body, "tab-body");
759 Roo.fly(body).addClass("x-tabs-body");
760 container.appendChild(body);
764 Roo.TabPanel.prototype.createItemBody = function(bodyEl, id){
765 var body = Roo.getDom(id);
767 body = document.createElement("div");
770 Roo.fly(body).addClass("x-tabs-item-body");
771 bodyEl.insertBefore(body, bodyEl.firstChild);
775 Roo.TabPanel.prototype.createStripElements = function(stripEl, text, closable){
776 var td = document.createElement("td");
777 stripEl.insertBefore(td, stripEl.childNodes[stripEl.childNodes.length-1]);
778 //stripEl.appendChild(td);
780 td.className = "x-tabs-closable";
782 this.closeTpl = new Roo.Template(
783 '<a href="#" class="x-tabs-right"><span class="x-tabs-left"><em class="x-tabs-inner">' +
784 '<span unselectable="on"' + (this.disableTooltips ? '' : ' title="{text}"') +' class="x-tabs-text">{text}</span>' +
785 '<div unselectable="on" class="close-icon"> </div></em></span></a>'
788 var el = this.closeTpl.overwrite(td, {"text": text});
789 var close = el.getElementsByTagName("div")[0];
790 var inner = el.getElementsByTagName("em")[0];
791 return {"el": el, "close": close, "inner": inner};
794 this.tabTpl = new Roo.Template(
795 '<a href="#" class="x-tabs-right"><span class="x-tabs-left"><em class="x-tabs-inner">' +
796 '<span unselectable="on"' + (this.disableTooltips ? '' : ' title="{text}"') +' class="x-tabs-text">{text}</span></em></span></a>'
799 var el = this.tabTpl.overwrite(td, {"text": text});
800 var inner = el.getElementsByTagName("em")[0];
801 return {"el": el, "inner": inner};