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 * @class Roo.LayoutRegion
14 * @extends Roo.BasicLayoutRegion
15 * This class represents a region in a layout manager.
16 * @cfg {Boolean} collapsible False to disable collapsing (defaults to true)
17 * @cfg {Boolean} collapsed True to set the initial display to collapsed (defaults to false)
18 * @cfg {Boolean} floatable False to disable floating (defaults to true)
19 * @cfg {Object} margins Margins for the element (defaults to {top: 0, left: 0, right:0, bottom: 0})
20 * @cfg {Object} cmargins Margins for the element when collapsed (defaults to: north/south {top: 2, left: 0, right:0, bottom: 2} or east/west {top: 0, left: 2, right:2, bottom: 0})
21 * @cfg {String} tabPosition (top|bottom) "top" or "bottom" (defaults to "bottom")
22 * @cfg {String} collapsedTitle Optional string message to display in the collapsed block of a north or south region
23 * @cfg {Boolean} alwaysShowTabs True to always display tabs even when there is only 1 panel (defaults to false)
24 * @cfg {Boolean} autoScroll True to enable overflow scrolling (defaults to false)
25 * @cfg {Boolean} titlebar True to display a title bar (defaults to true)
26 * @cfg {String} title The title for the region (overrides panel titles)
27 * @cfg {Boolean} animate True to animate expand/collapse (defaults to false)
28 * @cfg {Boolean} autoHide False to disable auto hiding when the mouse leaves the "floated" region (defaults to true)
29 * @cfg {Boolean} preservePanels True to preserve removed panels so they can be readded later (defaults to false)
30 * @cfg {Boolean} closeOnTab True to place the close icon on the tabs instead of the region titlebar (defaults to false)
31 * @cfg {Boolean} hideTabs True to hide the tab strip (defaults to false)
32 * @cfg {Boolean} resizeTabs True to enable automatic tab resizing. This will resize the tabs so they are all the same size and fit within
33 * the space available, similar to FireFox 1.5 tabs (defaults to false)
34 * @cfg {Number} minTabWidth The minimum tab width (defaults to 40)
35 * @cfg {Number} preferredTabWidth The preferred tab width (defaults to 150)
36 * @cfg {Boolean} showPin True to show a pin button
37 * @cfg {Boolean} hidden True to start the region hidden (defaults to false)
38 * @cfg {Boolean} hideWhenEmpty True to hide the region when it has no panels
39 * @cfg {Boolean} disableTabTips True to disable tab tooltips
40 * @cfg {Number} width For East/West panels
41 * @cfg {Number} height For North/South panels
42 * @cfg {Boolean} split To show the splitter
43 * @cfg {Boolean} toolbar xtype configuration for a toolbar - shows on right of tabbar
45 Roo.LayoutRegion = function(mgr, config, pos){
46 Roo.LayoutRegion.superclass.constructor.call(this, mgr, config, pos, true);
47 var dh = Roo.DomHelper;
48 /** This region's container element
49 * @type Roo.Element */
50 this.el = dh.append(mgr.el.dom, {tag: "div", cls: "x-layout-panel x-layout-panel-" + this.position}, true);
51 /** This region's title element
52 * @type Roo.Element */
54 this.titleEl = dh.append(this.el.dom, {tag: "div", unselectable: "on", cls: "x-unselectable x-layout-panel-hd x-layout-title-"+this.position, children:[
55 {tag: "span", cls: "x-unselectable x-layout-panel-hd-text", unselectable: "on", html: " "},
56 {tag: "div", cls: "x-unselectable x-layout-panel-hd-tools", unselectable: "on"}
58 this.titleEl.enableDisplayMode();
59 /** This region's title text element
60 * @type HTMLElement */
61 this.titleTextEl = this.titleEl.dom.firstChild;
62 this.tools = Roo.get(this.titleEl.dom.childNodes[1], true);
63 this.closeBtn = this.createTool(this.tools.dom, "x-layout-close");
64 this.closeBtn.enableDisplayMode();
65 this.closeBtn.on("click", this.closeClicked, this);
68 this.createBody(config);
70 this.collapsed = false;
72 if(config.hideWhenEmpty){
74 this.on("paneladded", this.validateVisibility, this);
75 this.on("panelremoved", this.validateVisibility, this);
77 this.applyConfig(config);
80 Roo.extend(Roo.LayoutRegion, Roo.BasicLayoutRegion, {
82 createBody : function(){
83 /** This region's body element
84 * @type Roo.Element */
85 this.bodyEl = this.el.createChild({tag: "div", cls: "x-layout-panel-body"});
88 applyConfig : function(c){
89 if(c.collapsible && this.position != "center" && !this.collapsedEl){
90 var dh = Roo.DomHelper;
91 if(c.titlebar !== false){
92 this.collapseBtn = this.createTool(this.tools.dom, "x-layout-collapse-"+this.position);
93 this.collapseBtn.on("click", this.collapse, this);
94 this.collapseBtn.enableDisplayMode();
96 if(c.showPin === true || this.showPin){
97 this.stickBtn = this.createTool(this.tools.dom, "x-layout-stick");
98 this.stickBtn.enableDisplayMode();
99 this.stickBtn.on("click", this.expand, this);
100 this.stickBtn.hide();
103 /** This region's collapsed element
104 * @type Roo.Element */
105 this.collapsedEl = dh.append(this.mgr.el.dom, {cls: "x-layout-collapsed x-layout-collapsed-"+this.position, children:[
106 {cls: "x-layout-collapsed-tools", children:[{cls: "x-layout-ctools-inner"}]}
108 if(c.floatable !== false){
109 this.collapsedEl.addClassOnOver("x-layout-collapsed-over");
110 this.collapsedEl.on("click", this.collapseClick, this);
113 if(c.collapsedTitle && (this.position == "north" || this.position== "south")) {
114 this.collapsedTitleTextEl = dh.append(this.collapsedEl.dom, {tag: "div", cls: "x-unselectable x-layout-panel-hd-text",
115 id: "message", unselectable: "on", style:{"float":"left"}});
116 this.collapsedTitleTextEl.innerHTML = c.collapsedTitle;
118 this.expandBtn = this.createTool(this.collapsedEl.dom.firstChild.firstChild, "x-layout-expand-"+this.position);
119 this.expandBtn.on("click", this.expand, this);
121 if(this.collapseBtn){
122 this.collapseBtn.setVisible(c.collapsible == true);
124 this.cmargins = c.cmargins || this.cmargins ||
125 (this.position == "west" || this.position == "east" ?
126 {top: 0, left: 2, right:2, bottom: 0} :
127 {top: 2, left: 0, right:0, bottom: 2});
128 this.margins = c.margins || this.margins || {top: 0, left: 0, right:0, bottom: 0};
129 this.bottomTabs = c.tabPosition != "top";
130 this.autoScroll = c.autoScroll || false;
132 this.bodyEl.setStyle("overflow", "auto");
134 this.bodyEl.setStyle("overflow", "hidden");
136 //if(c.titlebar !== false){
137 if((!c.titlebar && !c.title) || c.titlebar === false){
142 this.titleTextEl.innerHTML = c.title;
146 this.duration = c.duration || .30;
147 this.slideDuration = c.slideDuration || .45;
157 * Returns true if this region is currently visible.
160 isVisible : function(){
165 * Updates the title for collapsed north/south regions (used with {@link #collapsedTitle} config option)
166 * @param {String} title (optional) The title text (accepts HTML markup, defaults to the numeric character reference for a non-breaking space, "&#160;")
168 setCollapsedTitle : function(title){
169 title = title || " ";
170 if(this.collapsedTitleTextEl){
171 this.collapsedTitleTextEl.innerHTML = title;
178 b = this.el.getBox(false, true);
180 b = this.collapsedEl.getBox(false, true);
185 getMargins : function(){
186 return this.collapsed ? this.cmargins : this.margins;
189 highlight : function(){
190 this.el.addClass("x-layout-panel-dragover");
193 unhighlight : function(){
194 this.el.removeClass("x-layout-panel-dragover");
197 updateBox : function(box){
200 this.el.dom.style.left = box.x + "px";
201 this.el.dom.style.top = box.y + "px";
202 this.updateBody(box.width, box.height);
204 this.collapsedEl.dom.style.left = box.x + "px";
205 this.collapsedEl.dom.style.top = box.y + "px";
206 this.collapsedEl.setSize(box.width, box.height);
209 this.tabs.autoSizeTabs();
213 updateBody : function(w, h){
216 w -= this.el.getBorderWidth("rl");
217 if(this.config.adjustments){
218 w += this.config.adjustments[0];
222 this.el.setHeight(h);
223 h = this.titleEl && this.titleEl.isDisplayed() ? h - (this.titleEl.getHeight()||0) : h;
224 h -= this.el.getBorderWidth("tb");
225 if(this.config.adjustments){
226 h += this.config.adjustments[1];
228 this.bodyEl.setHeight(h);
230 h = this.tabs.syncHeight(h);
234 w = w !== null ? w : this.panelSize.width;
235 h = h !== null ? h : this.panelSize.height;
237 if(this.activePanel){
238 var el = this.activePanel.getEl();
239 w = w !== null ? w : el.getWidth();
240 h = h !== null ? h : el.getHeight();
241 this.panelSize = {width: w, height: h};
242 this.activePanel.setSize(w, h);
244 if(Roo.isIE && this.tabs){
245 this.tabs.el.repaint();
250 * Returns the container element for this region.
251 * @return {Roo.Element}
262 this.el.dom.style.left = "-2000px";
265 this.collapsedEl.dom.style.left = "-2000px";
266 this.collapsedEl.hide();
268 this.visible = false;
269 this.fireEvent("visibilitychange", this, false);
273 * Shows this region if it was previously hidden.
279 this.collapsedEl.show();
282 this.fireEvent("visibilitychange", this, true);
285 closeClicked : function(){
286 if(this.activePanel){
287 this.remove(this.activePanel);
291 collapseClick : function(e){
302 * Collapses this region.
303 * @param {Boolean} skipAnim (optional) true to collapse the element without animation (if animate is true)
305 collapse : function(skipAnim){
309 this.collapsed = true;
311 this.split.el.hide();
313 if(this.config.animate && skipAnim !== true){
314 this.fireEvent("invalidated", this);
315 this.animateCollapse();
317 this.el.setLocation(-20000,-20000);
319 this.collapsedEl.show();
320 this.fireEvent("collapsed", this);
321 this.fireEvent("invalidated", this);
325 animateCollapse : function(){
330 * Expands this region if it was previously collapsed.
331 * @param {Roo.EventObject} e The event that triggered the expand (or null if calling manually)
332 * @param {Boolean} skipAnim (optional) true to expand the element without animation (if animate is true)
334 expand : function(e, skipAnim){
338 if(!this.collapsed || this.el.hasActiveFx()) {
345 this.collapsed = false;
346 if(this.config.animate && skipAnim !== true){
347 this.animateExpand();
351 this.split.el.show();
353 this.collapsedEl.setLocation(-2000,-2000);
354 this.collapsedEl.hide();
355 this.fireEvent("invalidated", this);
356 this.fireEvent("expanded", this);
360 animateExpand : function(){
364 initTabs : function()
366 this.bodyEl.setStyle("overflow", "hidden");
367 var ts = new Roo.TabPanel(
370 tabPosition: this.bottomTabs ? 'bottom' : 'top',
371 disableTooltips: this.config.disableTabTips,
372 toolbar : this.config.toolbar
375 if(this.config.hideTabs){
376 ts.stripWrap.setDisplayed(false);
379 ts.resizeTabs = this.config.resizeTabs === true;
380 ts.minTabWidth = this.config.minTabWidth || 40;
381 ts.maxTabWidth = this.config.maxTabWidth || 250;
382 ts.preferredTabWidth = this.config.preferredTabWidth || 150;
383 ts.monitorResize = false;
384 ts.bodyEl.setStyle("overflow", this.config.autoScroll ? "auto" : "hidden");
385 ts.bodyEl.addClass('x-layout-tabs-body');
386 this.panels.each(this.initPanelAsTab, this);
389 initPanelAsTab : function(panel){
390 var ti = this.tabs.addTab(panel.getEl().id, panel.getTitle(), null,
391 this.config.closeOnTab && panel.isClosable());
392 if(panel.tabTip !== undefined){
393 ti.setTooltip(panel.tabTip);
395 ti.on("activate", function(){
396 this.setActivePanel(panel);
398 if(this.config.closeOnTab){
399 ti.on("beforeclose", function(t, e){
407 updatePanelTitle : function(panel, title){
408 if(this.activePanel == panel){
409 this.updateTitle(title);
412 var ti = this.tabs.getTab(panel.getEl().id);
414 if(panel.tabTip !== undefined){
415 ti.setTooltip(panel.tabTip);
420 updateTitle : function(title){
421 if(this.titleTextEl && !this.config.title){
422 this.titleTextEl.innerHTML = (typeof title != "undefined" && title.length > 0 ? title : " ");
426 setActivePanel : function(panel){
427 panel = this.getPanel(panel);
428 if(this.activePanel && this.activePanel != panel){
429 this.activePanel.setActiveState(false);
431 this.activePanel = panel;
432 panel.setActiveState(true);
434 panel.setSize(this.panelSize.width, this.panelSize.height);
437 this.closeBtn.setVisible(!this.config.closeOnTab && !this.isSlid && panel.isClosable());
439 this.updateTitle(panel.getTitle());
441 this.fireEvent("invalidated", this);
443 this.fireEvent("panelactivated", this, panel);
447 * Shows the specified panel.
448 * @param {Number/String/ContentPanel} panelId The panel's index, id or the panel itself
449 * @return {Roo.ContentPanel} The shown panel, or null if a panel could not be found from panelId
451 showPanel : function(panel)
453 panel = this.getPanel(panel);
456 var tab = this.tabs.getTab(panel.getEl().id);
458 this.tabs.unhideTab(tab.id);
462 this.setActivePanel(panel);
469 * Get the active panel for this region.
470 * @return {Roo.ContentPanel} The active panel or null
472 getActivePanel : function(){
473 return this.activePanel;
476 validateVisibility : function(){
477 if(this.panels.getCount() < 1){
478 this.updateTitle(" ");
479 this.closeBtn.hide();
482 if(!this.isVisible()){
489 * Adds the passed ContentPanel(s) to this region.
490 * @param {ContentPanel...} panel The ContentPanel(s) to add (you can pass more than one)
491 * @return {Roo.ContentPanel} The panel added (if only one was added; null otherwise)
493 add : function(panel){
494 if(arguments.length > 1){
495 for(var i = 0, len = arguments.length; i < len; i++) {
496 this.add(arguments[i]);
500 if(this.hasPanel(panel)){
501 this.showPanel(panel);
504 panel.setRegion(this);
505 this.panels.add(panel);
506 if(this.panels.getCount() == 1 && !this.config.alwaysShowTabs){
507 this.bodyEl.dom.appendChild(panel.getEl().dom);
508 if(panel.background !== true){
509 this.setActivePanel(panel);
511 this.fireEvent("paneladded", this, panel);
517 this.initPanelAsTab(panel);
519 if(panel.background !== true){
520 this.tabs.activate(panel.getEl().id);
522 this.fireEvent("paneladded", this, panel);
527 * Hides the tab for the specified panel.
528 * @param {Number/String/ContentPanel} panel The panel's index, id or the panel itself
530 hidePanel : function(panel){
531 if(this.tabs && (panel = this.getPanel(panel))){
532 this.tabs.hideTab(panel.getEl().id);
537 * Unhides the tab for a previously hidden panel.
538 * @param {Number/String/ContentPanel} panel The panel's index, id or the panel itself
540 unhidePanel : function(panel){
541 if(this.tabs && (panel = this.getPanel(panel))){
542 this.tabs.unhideTab(panel.getEl().id);
546 clearPanels : function(){
547 while(this.panels.getCount() > 0){
548 this.remove(this.panels.first());
553 * Removes the specified panel. If preservePanel is not true (either here or in the config), the panel is destroyed.
554 * @param {Number/String/ContentPanel} panel The panel's index, id or the panel itself
555 * @param {Boolean} preservePanel Overrides the config preservePanel option
556 * @return {Roo.ContentPanel} The panel that was removed
558 remove : function(panel, preservePanel){
559 panel = this.getPanel(panel);
564 this.fireEvent("beforeremove", this, panel, e);
565 if(e.cancel === true){
568 preservePanel = (typeof preservePanel != "undefined" ? preservePanel : (this.config.preservePanels === true || panel.preserve === true));
569 var panelId = panel.getId();
570 this.panels.removeKey(panelId);
572 document.body.appendChild(panel.getEl().dom);
575 this.tabs.removeTab(panel.getEl().id);
576 }else if (!preservePanel){
577 this.bodyEl.dom.removeChild(panel.getEl().dom);
579 if(this.panels.getCount() == 1 && this.tabs && !this.config.alwaysShowTabs){
580 var p = this.panels.first();
581 var tempEl = document.createElement("div"); // temp holder to keep IE from deleting the node
582 tempEl.appendChild(p.getEl().dom);
583 this.bodyEl.update("");
584 this.bodyEl.dom.appendChild(p.getEl().dom);
586 this.updateTitle(p.getTitle());
588 this.bodyEl.setStyle("overflow", this.config.autoScroll ? "auto" : "hidden");
589 this.setActivePanel(p);
591 panel.setRegion(null);
592 if(this.activePanel == panel){
593 this.activePanel = null;
595 if(this.config.autoDestroy !== false && preservePanel !== true){
596 try{panel.destroy();}catch(e){}
598 this.fireEvent("panelremoved", this, panel);
603 * Returns the TabPanel component used by this region
604 * @return {Roo.TabPanel}
606 getTabs : function(){
610 createTool : function(parentEl, className){
611 var btn = Roo.DomHelper.append(parentEl, {tag: "div", cls: "x-layout-tools-button",
612 children: [{tag: "div", cls: "x-layout-tools-button-inner " + className, html: " "}]}, true);
613 btn.addClassOnOver("x-layout-tools-button-over");