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.bootstrap.panel.Tabs = function(config){
51 * The container element for this TabPanel.
54 this.el = Roo.get(config.el);
57 if(typeof config == "boolean"){
58 this.tabPosition = config ? "bottom" : "top";
60 Roo.apply(this, config);
64 if(this.tabPosition == "bottom"){
65 this.bodyEl = Roo.get(this.createBody(this.el.dom));
66 this.el.addClass("roo-tabs-bottom");
68 this.stripWrap = Roo.get(this.createStrip(this.el.dom), true);
69 this.stripEl = Roo.get(this.createStripList(this.stripWrap.dom), true);
70 this.stripBody = Roo.get(this.stripWrap.dom.firstChild.firstChild, true);
72 Roo.fly(this.stripWrap.dom.firstChild).setStyle("overflow-x", "hidden");
74 if(this.tabPosition != "bottom"){
75 /** The body element that contains {@link Roo.TabPanelItem} bodies. +
78 this.bodyEl = Roo.get(this.createBody(this.el.dom));
79 this.el.addClass("roo-tabs-top");
83 this.bodyEl.setStyle("position", "relative");
86 this.activateDelegate = this.activate.createDelegate(this);
91 * Fires when the active tab changes
92 * @param {Roo.TabPanel} this
93 * @param {Roo.TabPanelItem} activePanel The new active tab
97 * @event beforetabchange
98 * Fires before the active tab changes, set cancel to true on the "e" parameter to cancel the change
99 * @param {Roo.TabPanel} this
100 * @param {Object} e Set cancel to true on this object to cancel the tab change
101 * @param {Roo.TabPanelItem} tab The tab being changed to
103 "beforetabchange" : true
106 Roo.EventManager.onWindowResize(this.onResize, this);
107 this.cpad = this.el.getPadding("lr");
108 this.hiddenCount = 0;
111 // toolbar on the tabbar support...
113 alert("no toolbar support yet");
114 this.toolbar = false;
116 var tcfg = this.toolbar;
117 tcfg.container = this.stripEl.child('td.x-tab-strip-toolbar');
118 this.toolbar = new Roo.Toolbar(tcfg);
120 var tbl = tcfg.container.child('table', true);
121 tbl.setAttribute('width', '100%');
129 Roo.bootstrap.panel.Tabs.superclass.constructor.call(this);
132 Roo.extend(Roo.bootstrap.panel.Tabs, Roo.util.Observable, {
134 *@cfg {String} tabPosition "top" or "bottom" (defaults to "top")
138 *@cfg {Number} currentTabWidth The width of the current tab (defaults to 0)
142 *@cfg {Number} minTabWidth The minimum width of a tab (defaults to 40) (ignored if {@link #resizeTabs} is not true)
146 *@cfg {Number} maxTabWidth The maximum width of a tab (defaults to 250) (ignored if {@link #resizeTabs} is not true)
150 *@cfg {Number} preferredTabWidth The preferred (default) width of a tab (defaults to 175) (ignored if {@link #resizeTabs} is not true)
152 preferredTabWidth : 175,
154 *@cfg {Boolean} resizeTabs True to enable dynamic tab resizing (defaults to false)
158 *@cfg {Boolean} monitorResize Set this to true to turn on window resize monitoring (ignored if {@link #resizeTabs} is not true) (defaults to true)
160 monitorResize : true,
162 *@cfg {Object} toolbar xtype description of toolbar to show at the right of the tab bar.
167 * Creates a new {@link Roo.TabPanelItem} by looking for an existing element with the provided id -- if it's not found it creates one.
168 * @param {String} id The id of the div to use <b>or create</b>
169 * @param {String} text The text for the tab
170 * @param {String} content (optional) Content to put in the TabPanelItem body
171 * @param {Boolean} closable (optional) True to create a close icon on the tab
172 * @return {Roo.TabPanelItem} The created TabPanelItem
174 addTab : function(id, text, content, closable, tpl)
176 var item = new Roo.bootstrap.panel.TabItem({
183 this.addTabItem(item);
185 item.setContent(content);
191 * Returns the {@link Roo.TabPanelItem} with the specified id/index
192 * @param {String/Number} id The id or index of the TabPanelItem to fetch.
193 * @return {Roo.TabPanelItem}
195 getTab : function(id){
196 return this.items[id];
200 * Hides the {@link Roo.TabPanelItem} with the specified id/index
201 * @param {String/Number} id The id or index of the TabPanelItem to hide.
203 hideTab : function(id){
204 var t = this.items[id];
213 * "Unhides" the {@link Roo.TabPanelItem} with the specified id/index.
214 * @param {String/Number} id The id or index of the TabPanelItem to unhide.
216 unhideTab : function(id){
217 var t = this.items[id];
226 * Adds an existing {@link Roo.TabPanelItem}.
227 * @param {Roo.TabPanelItem} item The TabPanelItem to add
229 addTabItem : function(item)
231 this.items[item.id] = item;
232 this.items.push(item);
234 // if(this.resizeTabs){
235 // item.setWidth(this.currentTabWidth || this.preferredTabWidth);
236 // this.autoSizeTabs();
243 * Removes a {@link Roo.TabPanelItem}.
244 * @param {String/Number} id The id or index of the TabPanelItem to remove.
246 removeTab : function(id){
247 var items = this.items;
250 var index = items.indexOf(tab);
251 if(this.active == tab && items.length > 1){
252 var newTab = this.getNextAvailable(index);
257 this.stripEl.dom.removeChild(tab.pnode.dom);
258 if(tab.bodyEl.dom.parentNode == this.bodyEl.dom){ // if it was moved already prevent error
259 this.bodyEl.dom.removeChild(tab.bodyEl.dom);
261 items.splice(index, 1);
262 delete this.items[tab.id];
263 tab.fireEvent("close", tab);
264 tab.purgeListeners();
268 getNextAvailable : function(start){
269 var items = this.items;
271 // look for a next tab that will slide over to
272 // replace the one being removed
273 while(index < items.length){
274 var item = items[++index];
275 if(item && !item.isHidden()){
279 // if one isn't found select the previous tab (on the left)
282 var item = items[--index];
283 if(item && !item.isHidden()){
291 * Disables a {@link Roo.TabPanelItem}. It cannot be the active tab, if it is this call is ignored.
292 * @param {String/Number} id The id or index of the TabPanelItem to disable.
294 disableTab : function(id){
295 var tab = this.items[id];
296 if(tab && this.active != tab){
302 * Enables a {@link Roo.TabPanelItem} that is disabled.
303 * @param {String/Number} id The id or index of the TabPanelItem to enable.
305 enableTab : function(id){
306 var tab = this.items[id];
311 * Activates a {@link Roo.TabPanelItem}. The currently active one will be deactivated.
312 * @param {String/Number} id The id or index of the TabPanelItem to activate.
313 * @return {Roo.TabPanelItem} The TabPanelItem.
315 activate : function(id)
317 var tab = this.items[id];
321 if(tab == this.active || tab.disabled){
325 this.fireEvent("beforetabchange", this, e, tab);
326 if(e.cancel !== true && !tab.disabled){
330 this.active = this.items[id];
332 this.fireEvent("tabchange", this, this.active);
338 * Gets the active {@link Roo.TabPanelItem}.
339 * @return {Roo.TabPanelItem} The active TabPanelItem or null if none are active.
341 getActiveTab : function(){
346 * Updates the tab body element to fit the height of the container element
347 * for overflow scrolling
348 * @param {Number} targetHeight (optional) Override the starting height from the elements height
350 syncHeight : function(targetHeight){
351 var height = (targetHeight || this.el.getHeight())-this.el.getBorderWidth("tb")-this.el.getPadding("tb");
352 var bm = this.bodyEl.getMargins();
353 var newHeight = height-(this.stripWrap.getHeight()||0)-(bm.top+bm.bottom);
354 this.bodyEl.setHeight(newHeight);
358 onResize : function(){
359 if(this.monitorResize){
365 * Disables tab resizing while tabs are being added (if {@link #resizeTabs} is false this does nothing)
367 beginUpdate : function(){
368 this.updating = true;
372 * Stops an update and resizes the tabs (if {@link #resizeTabs} is false this does nothing)
374 endUpdate : function(){
375 this.updating = false;
380 * Manual call to resize the tabs (if {@link #resizeTabs} is false this does nothing)
382 autoSizeTabs : function()
384 var count = this.items.length;
385 var vcount = count - this.hiddenCount;
386 if(!this.resizeTabs || count < 1 || vcount < 1 || this.updating) {
395 var w = Math.max(this.el.getWidth() - this.cpad, 10);
396 var availWidth = Math.floor(w / vcount);
397 var b = this.stripBody;
398 if(b.getWidth() > w){
399 var tabs = this.items;
400 this.setTabWidth(Math.max(availWidth, this.minTabWidth)-2);
401 if(availWidth < this.minTabWidth){
402 /*if(!this.sleft){ // incomplete scrolling code
403 this.createScrollButtons();
406 this.stripClip.setWidth(w - (this.sleft.getWidth()+this.sright.getWidth()));*/
409 if(this.currentTabWidth < this.preferredTabWidth){
410 this.setTabWidth(Math.min(availWidth, this.preferredTabWidth)-2);
416 * Returns the number of tabs in this TabPanel.
419 getCount : function(){
420 return this.items.length;
424 * Resizes all the tabs to the passed width
425 * @param {Number} The new width
427 setTabWidth : function(width){
428 this.currentTabWidth = width;
429 for(var i = 0, len = this.items.length; i < len; i++) {
430 if(!this.items[i].isHidden()) {
431 this.items[i].setWidth(width);
437 * Destroys this TabPanel
438 * @param {Boolean} removeEl (optional) True to remove the element from the DOM as well (defaults to undefined)
440 destroy : function(removeEl){
441 Roo.EventManager.removeResizeListener(this.onResize, this);
442 for(var i = 0, len = this.items.length; i < len; i++){
443 this.items[i].purgeListeners();
445 if(removeEl === true){
451 createStrip : function(container)
453 var strip = document.createElement("nav");
454 strip.className = Roo.bootstrap.version == 4 ?
455 "navbar-light bg-light" :
456 "navbar navbar-default"; //"x-tabs-wrap";
457 container.appendChild(strip);
461 createStripList : function(strip)
463 // div wrapper for retard IE
464 // returns the "tr" element.
465 strip.innerHTML = '<ul class="nav nav-tabs" role="tablist"></ul>';
466 //'<div class="x-tabs-strip-wrap">'+
467 // '<table class="x-tabs-strip" cellspacing="0" cellpadding="0" border="0"><tbody><tr>'+
468 // '<td class="x-tab-strip-toolbar"></td></tr></tbody></table></div>';
469 return strip.firstChild; //.firstChild.firstChild.firstChild;
471 createBody : function(container)
473 var body = document.createElement("div");
474 Roo.id(body, "tab-body");
475 //Roo.fly(body).addClass("x-tabs-body");
476 Roo.fly(body).addClass("tab-content");
477 container.appendChild(body);
480 createItemBody :function(bodyEl, id){
481 var body = Roo.getDom(id);
483 body = document.createElement("div");
486 //Roo.fly(body).addClass("x-tabs-item-body");
487 Roo.fly(body).addClass("tab-pane");
488 bodyEl.insertBefore(body, bodyEl.firstChild);
492 createStripElements : function(stripEl, text, closable, tpl)
494 var td = document.createElement("li"); // was td..
495 td.className = 'nav-item';
497 //stripEl.insertBefore(td, stripEl.childNodes[stripEl.childNodes.length-1]);
500 stripEl.appendChild(td);
502 td.className = "x-tabs-closable";
504 this.closeTpl = new Roo.Template(
505 '<a href="#" class="x-tabs-right"><span class="x-tabs-left"><em class="x-tabs-inner">' +
506 '<span unselectable="on"' + (this.disableTooltips ? '' : ' title="{text}"') +' class="x-tabs-text">{text}</span>' +
507 '<div unselectable="on" class="close-icon"> </div></em></span></a>'
510 var el = this.closeTpl.overwrite(td, {"text": text});
511 var close = el.getElementsByTagName("div")[0];
512 var inner = el.getElementsByTagName("em")[0];
513 return {"el": el, "close": close, "inner": inner};
516 // not sure what this is..
518 //this.tabTpl = new Roo.Template(
519 // '<a href="#" class="x-tabs-right"><span class="x-tabs-left"><em class="x-tabs-inner">' +
520 // '<span unselectable="on"' + (this.disableTooltips ? '' : ' title="{text}"') +' class="x-tabs-text">{text}</span></em></span></a>'
522 // this.tabTpl = new Roo.Template(
524 // '<span unselectable="on"' +
525 // (this.disableTooltips ? '' : ' title="{text}"') +
526 // ' >{text}</span></a>'
532 var template = tpl || this.tabTpl || false;
535 template = new Roo.Template(
536 Roo.bootstrap.version == 4 ?
538 '<a class="nav-link" href="#" unselectable="on"' +
539 (this.disableTooltips ? '' : ' title="{text}"') +
542 '<a class="nav-link" href="#">' +
543 '<span unselectable="on"' +
544 (this.disableTooltips ? '' : ' title="{text}"') +
545 ' >{text}</span></a>'
550 switch (typeof(template)) {
554 template = new Roo.Template(template);
560 var el = template.overwrite(td, {"text": text});
562 var inner = el.getElementsByTagName("span")[0];
564 return {"el": el, "inner": inner};