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);
223 this.stripEl.dom.removeChild(tab.pnode.dom);
224 if(tab.bodyEl.dom.parentNode == this.bodyEl.dom){ // if it was moved already prevent error
225 this.bodyEl.dom.removeChild(tab.bodyEl.dom);
227 items.splice(index, 1);
228 delete this.items[tab.id];
229 tab.fireEvent("close", tab);
230 tab.purgeListeners();
234 getNextAvailable : function(start){
235 var items = this.items;
237 // look for a next tab that will slide over to
238 // replace the one being removed
239 while(index < items.length){
240 var item = items[++index];
241 if(item && !item.isHidden()){
245 // if one isn't found select the previous tab (on the left)
248 var item = items[--index];
249 if(item && !item.isHidden()){
257 * Disables a {@link Roo.TabPanelItem}. It cannot be the active tab, if it is this call is ignored.
258 * @param {String/Number} id The id or index of the TabPanelItem to disable.
260 disableTab : function(id){
261 var tab = this.items[id];
262 if(tab && this.active != tab){
268 * Enables a {@link Roo.TabPanelItem} that is disabled.
269 * @param {String/Number} id The id or index of the TabPanelItem to enable.
271 enableTab : function(id){
272 var tab = this.items[id];
277 * Activates a {@link Roo.TabPanelItem}. The currently active one will be deactivated.
278 * @param {String/Number} id The id or index of the TabPanelItem to activate.
279 * @return {Roo.TabPanelItem} The TabPanelItem.
281 activate : function(id){
282 var tab = this.items[id];
286 if(tab == this.active || tab.disabled){
290 this.fireEvent("beforetabchange", this, e, tab);
291 if(e.cancel !== true && !tab.disabled){
295 this.active = this.items[id];
297 this.fireEvent("tabchange", this, this.active);
303 * Gets the active {@link Roo.TabPanelItem}.
304 * @return {Roo.TabPanelItem} The active TabPanelItem or null if none are active.
306 getActiveTab : function(){
311 * Updates the tab body element to fit the height of the container element
312 * for overflow scrolling
313 * @param {Number} targetHeight (optional) Override the starting height from the elements height
315 syncHeight : function(targetHeight){
316 var height = (targetHeight || this.el.getHeight())-this.el.getBorderWidth("tb")-this.el.getPadding("tb");
317 var bm = this.bodyEl.getMargins();
318 var newHeight = height-(this.stripWrap.getHeight()||0)-(bm.top+bm.bottom);
319 this.bodyEl.setHeight(newHeight);
323 onResize : function(){
324 if(this.monitorResize){
330 * Disables tab resizing while tabs are being added (if {@link #resizeTabs} is false this does nothing)
332 beginUpdate : function(){
333 this.updating = true;
337 * Stops an update and resizes the tabs (if {@link #resizeTabs} is false this does nothing)
339 endUpdate : function(){
340 this.updating = false;
345 * Manual call to resize the tabs (if {@link #resizeTabs} is false this does nothing)
347 autoSizeTabs : function(){
348 var count = this.items.length;
349 var vcount = count - this.hiddenCount;
350 if(!this.resizeTabs || count < 1 || vcount < 1 || this.updating) return;
351 var w = Math.max(this.el.getWidth() - this.cpad, 10);
352 var availWidth = Math.floor(w / vcount);
353 var b = this.stripBody;
354 if(b.getWidth() > w){
355 var tabs = this.items;
356 this.setTabWidth(Math.max(availWidth, this.minTabWidth)-2);
357 if(availWidth < this.minTabWidth){
358 /*if(!this.sleft){ // incomplete scrolling code
359 this.createScrollButtons();
362 this.stripClip.setWidth(w - (this.sleft.getWidth()+this.sright.getWidth()));*/
365 if(this.currentTabWidth < this.preferredTabWidth){
366 this.setTabWidth(Math.min(availWidth, this.preferredTabWidth)-2);
372 * Returns the number of tabs in this TabPanel.
375 getCount : function(){
376 return this.items.length;
380 * Resizes all the tabs to the passed width
381 * @param {Number} The new width
383 setTabWidth : function(width){
384 this.currentTabWidth = width;
385 for(var i = 0, len = this.items.length; i < len; i++) {
386 if(!this.items[i].isHidden())this.items[i].setWidth(width);
391 * Destroys this TabPanel
392 * @param {Boolean} removeEl (optional) True to remove the element from the DOM as well (defaults to undefined)
394 destroy : function(removeEl){
395 Roo.EventManager.removeResizeListener(this.onResize, this);
396 for(var i = 0, len = this.items.length; i < len; i++){
397 this.items[i].purgeListeners();
399 if(removeEl === true){
407 * @class Roo.TabPanelItem
408 * @extends Roo.util.Observable
409 * Represents an individual item (tab plus body) in a TabPanel.
410 * @param {Roo.TabPanel} tabPanel The {@link Roo.TabPanel} this TabPanelItem belongs to
411 * @param {String} id The id of this TabPanelItem
412 * @param {String} text The text for the tab of this TabPanelItem
413 * @param {Boolean} closable True to allow this TabPanelItem to be closable (defaults to false)
415 Roo.TabPanelItem = function(tabPanel, id, text, closable){
417 * The {@link Roo.TabPanel} this TabPanelItem belongs to
420 this.tabPanel = tabPanel;
422 * The id for this TabPanelItem
427 this.disabled = false;
432 this.closable = closable;
435 * The body element for this TabPanelItem.
438 this.bodyEl = Roo.get(tabPanel.createItemBody(tabPanel.bodyEl.dom, id));
439 this.bodyEl.setVisibilityMode(Roo.Element.VISIBILITY);
440 this.bodyEl.setStyle("display", "block");
441 this.bodyEl.setStyle("zoom", "1");
444 var els = tabPanel.createStripElements(tabPanel.stripEl.dom, text, closable);
446 this.el = Roo.get(els.el, true);
447 this.inner = Roo.get(els.inner, true);
448 this.textEl = Roo.get(this.el.dom.firstChild.firstChild.firstChild, true);
449 this.pnode = Roo.get(els.el.parentNode, true);
450 this.el.on("mousedown", this.onTabMouseDown, this);
451 this.el.on("click", this.onTabClick, this);
454 var c = Roo.get(els.close, true);
455 c.dom.title = this.closeText;
456 c.addClassOnOver("close-over");
457 c.on("click", this.closeClick, this);
463 * Fires when this tab becomes the active tab.
464 * @param {Roo.TabPanel} tabPanel The parent TabPanel
465 * @param {Roo.TabPanelItem} this
470 * Fires before this tab is closed. To cancel the close, set cancel to true on e (e.cancel = true).
471 * @param {Roo.TabPanelItem} this
472 * @param {Object} e Set cancel to true on this object to cancel the close.
477 * Fires when this tab is closed.
478 * @param {Roo.TabPanelItem} this
483 * Fires when this tab is no longer the active tab.
484 * @param {Roo.TabPanel} tabPanel The parent TabPanel
485 * @param {Roo.TabPanelItem} this
491 Roo.TabPanelItem.superclass.constructor.call(this);
494 Roo.extend(Roo.TabPanelItem, Roo.util.Observable, {
495 purgeListeners : function(){
496 Roo.util.Observable.prototype.purgeListeners.call(this);
497 this.el.removeAllListeners();
500 * Shows this TabPanelItem -- this <b>does not</b> deactivate the currently active TabPanelItem.
503 this.pnode.addClass("on");
506 this.tabPanel.stripWrap.repaint();
508 this.fireEvent("activate", this.tabPanel, this);
512 * Returns true if this tab is the active tab.
515 isActive : function(){
516 return this.tabPanel.getActiveTab() == this;
520 * Hides this TabPanelItem -- if you don't activate another TabPanelItem this could look odd.
523 this.pnode.removeClass("on");
525 this.fireEvent("deactivate", this.tabPanel, this);
528 hideAction : function(){
530 this.bodyEl.setStyle("position", "absolute");
531 this.bodyEl.setLeft("-20000px");
532 this.bodyEl.setTop("-20000px");
535 showAction : function(){
536 this.bodyEl.setStyle("position", "relative");
537 this.bodyEl.setTop("");
538 this.bodyEl.setLeft("");
543 * Set the tooltip for the tab.
544 * @param {String} tooltip The tab's tooltip
546 setTooltip : function(text){
547 if(Roo.QuickTips && Roo.QuickTips.isEnabled()){
548 this.textEl.dom.qtip = text;
549 this.textEl.dom.removeAttribute('title');
551 this.textEl.dom.title = text;
555 onTabClick : function(e){
557 this.tabPanel.activate(this.id);
560 onTabMouseDown : function(e){
562 this.tabPanel.activate(this.id);
565 getWidth : function(){
566 return this.inner.getWidth();
569 setWidth : function(width){
570 var iwidth = width - this.pnode.getPadding("lr");
571 this.inner.setWidth(iwidth);
572 this.textEl.setWidth(iwidth-this.inner.getPadding("lr"));
573 this.pnode.setWidth(width);
577 * Show or hide the tab
578 * @param {Boolean} hidden True to hide or false to show.
580 setHidden : function(hidden){
581 this.hidden = hidden;
582 this.pnode.setStyle("display", hidden ? "none" : "");
586 * Returns true if this tab is "hidden"
589 isHidden : function(){
594 * Returns the text for this tab
597 getText : function(){
601 autoSize : function(){
602 //this.el.beginMeasure();
603 this.textEl.setWidth(1);
604 this.setWidth(this.textEl.dom.scrollWidth+this.pnode.getPadding("lr")+this.inner.getPadding("lr"));
605 //this.el.endMeasure();
609 * Sets the text for the tab (Note: this also sets the tooltip text)
610 * @param {String} text The tab's text and tooltip
612 setText : function(text){
614 this.textEl.update(text);
615 this.setTooltip(text);
616 if(!this.tabPanel.resizeTabs){
621 * Activates this TabPanelItem -- this <b>does</b> deactivate the currently active TabPanelItem.
623 activate : function(){
624 this.tabPanel.activate(this.id);
628 * Disables this TabPanelItem -- this does nothing if this is the active TabPanelItem.
630 disable : function(){
631 if(this.tabPanel.active != this){
632 this.disabled = true;
633 this.pnode.addClass("disabled");
638 * Enables this TabPanelItem if it was previously disabled.
641 this.disabled = false;
642 this.pnode.removeClass("disabled");
646 * Sets the content for this TabPanelItem.
647 * @param {String} content The content
648 * @param {Boolean} loadScripts true to look for and load scripts
650 setContent : function(content, loadScripts){
651 this.bodyEl.update(content, loadScripts);
655 * Gets the {@link Roo.UpdateManager} for the body of this TabPanelItem. Enables you to perform Ajax updates.
656 * @return {Roo.UpdateManager} The UpdateManager
658 getUpdateManager : function(){
659 return this.bodyEl.getUpdateManager();
663 * Set a URL to be used to load the content for this TabPanelItem.
664 * @param {String/Function} url The URL to load the content from, or a function to call to get the URL
665 * @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)
666 * @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)
667 * @return {Roo.UpdateManager} The UpdateManager
669 setUrl : function(url, params, loadOnce){
670 if(this.refreshDelegate){
671 this.un('activate', this.refreshDelegate);
673 this.refreshDelegate = this._handleRefresh.createDelegate(this, [url, params, loadOnce]);
674 this.on("activate", this.refreshDelegate);
675 return this.bodyEl.getUpdateManager();
679 _handleRefresh : function(url, params, loadOnce){
680 if(!loadOnce || !this.loaded){
681 var updater = this.bodyEl.getUpdateManager();
682 updater.update(url, params, this._setLoaded.createDelegate(this));
687 * Forces a content refresh from the URL specified in the {@link #setUrl} method.
688 * Will fail silently if the setUrl method has not been called.
689 * This does not activate the panel, just updates its content.
691 refresh : function(){
692 if(this.refreshDelegate){
694 this.refreshDelegate();
699 _setLoaded : function(){
704 closeClick : function(e){
707 this.fireEvent("beforeclose", this, o);
708 if(o.cancel !== true){
709 this.tabPanel.removeTab(this.id);
713 * The text displayed in the tooltip for the close icon.
716 closeText : "Close this tab"
720 Roo.TabPanel.prototype.createStrip = function(container){
721 var strip = document.createElement("div");
722 strip.className = "x-tabs-wrap";
723 container.appendChild(strip);
727 Roo.TabPanel.prototype.createStripList = function(strip){
728 // div wrapper for retard IE
729 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>';
730 return strip.firstChild.firstChild.firstChild.firstChild;
733 Roo.TabPanel.prototype.createBody = function(container){
734 var body = document.createElement("div");
735 Roo.id(body, "tab-body");
736 Roo.fly(body).addClass("x-tabs-body");
737 container.appendChild(body);
741 Roo.TabPanel.prototype.createItemBody = function(bodyEl, id){
742 var body = Roo.getDom(id);
744 body = document.createElement("div");
747 Roo.fly(body).addClass("x-tabs-item-body");
748 bodyEl.insertBefore(body, bodyEl.firstChild);
752 Roo.TabPanel.prototype.createStripElements = function(stripEl, text, closable){
753 var td = document.createElement("td");
754 stripEl.appendChild(td);
756 td.className = "x-tabs-closable";
758 this.closeTpl = new Roo.Template(
759 '<a href="#" class="x-tabs-right"><span class="x-tabs-left"><em class="x-tabs-inner">' +
760 '<span unselectable="on"' + (this.disableTooltips ? '' : ' title="{text}"') +' class="x-tabs-text">{text}</span>' +
761 '<div unselectable="on" class="close-icon"> </div></em></span></a>'
764 var el = this.closeTpl.overwrite(td, {"text": text});
765 var close = el.getElementsByTagName("div")[0];
766 var inner = el.getElementsByTagName("em")[0];
767 return {"el": el, "close": close, "inner": inner};
770 this.tabTpl = new Roo.Template(
771 '<a href="#" class="x-tabs-right"><span class="x-tabs-left"><em class="x-tabs-inner">' +
772 '<span unselectable="on"' + (this.disableTooltips ? '' : ' title="{text}"') +' class="x-tabs-text">{text}</span></em></span></a>'
775 var el = this.tabTpl.overwrite(td, {"text": text});
776 var inner = el.getElementsByTagName("em")[0];
777 return {"el": el, "inner": inner};