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" 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
44 Roo.LayoutRegion = function(mgr, config, pos){
45 Roo.LayoutRegion.superclass.constructor.call(this, mgr, config, pos, true);
46 var dh = Roo.DomHelper;
47 /** This region's container element
48 * @type Roo.Element */
49 this.el = dh.append(mgr.el.dom, {tag: "div", cls: "x-layout-panel x-layout-panel-" + this.position}, true);
50 /** This region's title element
51 * @type Roo.Element */
53 this.titleEl = dh.append(this.el.dom, {tag: "div", unselectable: "on", cls: "x-unselectable x-layout-panel-hd x-layout-title-"+this.position, children:[
54 {tag: "span", cls: "x-unselectable x-layout-panel-hd-text", unselectable: "on", html: " "},
55 {tag: "div", cls: "x-unselectable x-layout-panel-hd-tools", unselectable: "on"}
57 this.titleEl.enableDisplayMode();
58 /** This region's title text element
59 * @type HTMLElement */
60 this.titleTextEl = this.titleEl.dom.firstChild;
61 this.tools = Roo.get(this.titleEl.dom.childNodes[1], true);
62 this.closeBtn = this.createTool(this.tools.dom, "x-layout-close");
63 this.closeBtn.enableDisplayMode();
64 this.closeBtn.on("click", this.closeClicked, this);
67 this.createBody(config);
69 this.collapsed = false;
71 if(config.hideWhenEmpty){
73 this.on("paneladded", this.validateVisibility, this);
74 this.on("panelremoved", this.validateVisibility, this);
76 this.applyConfig(config);
79 Roo.extend(Roo.LayoutRegion, Roo.BasicLayoutRegion, {
81 createBody : function(){
82 /** This region's body element
83 * @type Roo.Element */
84 this.bodyEl = this.el.createChild({tag: "div", cls: "x-layout-panel-body"});
87 applyConfig : function(c){
88 if(c.collapsible && this.position != "center" && !this.collapsedEl){
89 var dh = Roo.DomHelper;
90 if(c.titlebar !== false){
91 this.collapseBtn = this.createTool(this.tools.dom, "x-layout-collapse-"+this.position);
92 this.collapseBtn.on("click", this.collapse, this);
93 this.collapseBtn.enableDisplayMode();
95 if(c.showPin === true || this.showPin){
96 this.stickBtn = this.createTool(this.tools.dom, "x-layout-stick");
97 this.stickBtn.enableDisplayMode();
98 this.stickBtn.on("click", this.expand, this);
102 /** This region's collapsed element
103 * @type Roo.Element */
104 this.collapsedEl = dh.append(this.mgr.el.dom, {cls: "x-layout-collapsed x-layout-collapsed-"+this.position, children:[
105 {cls: "x-layout-collapsed-tools", children:[{cls: "x-layout-ctools-inner"}]}
107 if(c.floatable !== false){
108 this.collapsedEl.addClassOnOver("x-layout-collapsed-over");
109 this.collapsedEl.on("click", this.collapseClick, this);
112 if(c.collapsedTitle && (this.position == "north" || this.position== "south")) {
113 this.collapsedTitleTextEl = dh.append(this.collapsedEl.dom, {tag: "div", cls: "x-unselectable x-layout-panel-hd-text",
114 id: "message", unselectable: "on", style:{"float":"left"}});
115 this.collapsedTitleTextEl.innerHTML = c.collapsedTitle;
117 this.expandBtn = this.createTool(this.collapsedEl.dom.firstChild.firstChild, "x-layout-expand-"+this.position);
118 this.expandBtn.on("click", this.expand, this);
120 if(this.collapseBtn){
121 this.collapseBtn.setVisible(c.collapsible == true);
123 this.cmargins = c.cmargins || this.cmargins ||
124 (this.position == "west" || this.position == "east" ?
125 {top: 0, left: 2, right:2, bottom: 0} :
126 {top: 2, left: 0, right:0, bottom: 2});
127 this.margins = c.margins || this.margins || {top: 0, left: 0, right:0, bottom: 0};
128 this.bottomTabs = c.tabPosition != "top";
129 this.autoScroll = c.autoScroll || false;
131 this.bodyEl.setStyle("overflow", "auto");
133 this.bodyEl.setStyle("overflow", "hidden");
135 //if(c.titlebar !== false){
136 if((!c.titlebar && !c.title) || c.titlebar === false){
141 this.titleTextEl.innerHTML = c.title;
145 this.duration = c.duration || .30;
146 this.slideDuration = c.slideDuration || .45;
156 * Returns true if this region is currently visible.
159 isVisible : function(){
164 * Updates the title for collapsed north/south regions (used with {@link #collapsedTitle} config option)
165 * @param {String} title (optional) The title text (accepts HTML markup, defaults to the numeric character reference for a non-breaking space, "&#160;")
167 setCollapsedTitle : function(title){
168 title = title || " ";
169 if(this.collapsedTitleTextEl){
170 this.collapsedTitleTextEl.innerHTML = title;
177 b = this.el.getBox(false, true);
179 b = this.collapsedEl.getBox(false, true);
184 getMargins : function(){
185 return this.collapsed ? this.cmargins : this.margins;
188 highlight : function(){
189 this.el.addClass("x-layout-panel-dragover");
192 unhighlight : function(){
193 this.el.removeClass("x-layout-panel-dragover");
196 updateBox : function(box){
199 this.el.dom.style.left = box.x + "px";
200 this.el.dom.style.top = box.y + "px";
201 this.updateBody(box.width, box.height);
203 this.collapsedEl.dom.style.left = box.x + "px";
204 this.collapsedEl.dom.style.top = box.y + "px";
205 this.collapsedEl.setSize(box.width, box.height);
208 this.tabs.autoSizeTabs();
212 updateBody : function(w, h){
215 w -= this.el.getBorderWidth("rl");
216 if(this.config.adjustments){
217 w += this.config.adjustments[0];
221 this.el.setHeight(h);
222 h = this.titleEl && this.titleEl.isDisplayed() ? h - (this.titleEl.getHeight()||0) : h;
223 h -= this.el.getBorderWidth("tb");
224 if(this.config.adjustments){
225 h += this.config.adjustments[1];
227 this.bodyEl.setHeight(h);
229 h = this.tabs.syncHeight(h);
233 w = w !== null ? w : this.panelSize.width;
234 h = h !== null ? h : this.panelSize.height;
236 if(this.activePanel){
237 var el = this.activePanel.getEl();
238 w = w !== null ? w : el.getWidth();
239 h = h !== null ? h : el.getHeight();
240 this.panelSize = {width: w, height: h};
241 this.activePanel.setSize(w, h);
243 if(Roo.isIE && this.tabs){
244 this.tabs.el.repaint();
249 * Returns the container element for this region.
250 * @return {Roo.Element}
261 this.el.dom.style.left = "-2000px";
264 this.collapsedEl.dom.style.left = "-2000px";
265 this.collapsedEl.hide();
267 this.visible = false;
268 this.fireEvent("visibilitychange", this, false);
272 * Shows this region if it was previously hidden.
278 this.collapsedEl.show();
281 this.fireEvent("visibilitychange", this, true);
284 closeClicked : function(){
285 if(this.activePanel){
286 this.remove(this.activePanel);
290 collapseClick : function(e){
301 * Collapses this region.
302 * @param {Boolean} skipAnim (optional) true to collapse the element without animation (if animate is true)
304 collapse : function(skipAnim){
305 if(this.collapsed) return;
306 this.collapsed = true;
308 this.split.el.hide();
310 if(this.config.animate && skipAnim !== true){
311 this.fireEvent("invalidated", this);
312 this.animateCollapse();
314 this.el.setLocation(-20000,-20000);
316 this.collapsedEl.show();
317 this.fireEvent("collapsed", this);
318 this.fireEvent("invalidated", this);
322 animateCollapse : function(){
327 * Expands this region if it was previously collapsed.
328 * @param {Roo.EventObject} e The event that triggered the expand (or null if calling manually)
329 * @param {Boolean} skipAnim (optional) true to expand the element without animation (if animate is true)
331 expand : function(e, skipAnim){
332 if(e) e.stopPropagation();
333 if(!this.collapsed || this.el.hasActiveFx()) return;
338 this.collapsed = false;
339 if(this.config.animate && skipAnim !== true){
340 this.animateExpand();
344 this.split.el.show();
346 this.collapsedEl.setLocation(-2000,-2000);
347 this.collapsedEl.hide();
348 this.fireEvent("invalidated", this);
349 this.fireEvent("expanded", this);
353 animateExpand : function(){
357 initTabs : function(){
358 this.bodyEl.setStyle("overflow", "hidden");
359 var ts = new Roo.TabPanel(this.bodyEl.dom, {
360 tabPosition: this.bottomTabs ? 'bottom' : 'top',
361 disableTooltips: this.config.disableTabTips
363 if(this.config.hideTabs){
364 ts.stripWrap.setDisplayed(false);
367 ts.resizeTabs = this.config.resizeTabs === true;
368 ts.minTabWidth = this.config.minTabWidth || 40;
369 ts.maxTabWidth = this.config.maxTabWidth || 250;
370 ts.preferredTabWidth = this.config.preferredTabWidth || 150;
371 ts.monitorResize = false;
372 ts.bodyEl.setStyle("overflow", this.config.autoScroll ? "auto" : "hidden");
373 ts.bodyEl.addClass('x-layout-tabs-body');
374 this.panels.each(this.initPanelAsTab, this);
377 initPanelAsTab : function(panel){
378 var ti = this.tabs.addTab(panel.getEl().id, panel.getTitle(), null,
379 this.config.closeOnTab && panel.isClosable());
380 if(panel.tabTip !== undefined){
381 ti.setTooltip(panel.tabTip);
383 ti.on("activate", function(){
384 this.setActivePanel(panel);
386 if(this.config.closeOnTab){
387 ti.on("beforeclose", function(t, e){
395 updatePanelTitle : function(panel, title){
396 if(this.activePanel == panel){
397 this.updateTitle(title);
400 var ti = this.tabs.getTab(panel.getEl().id);
402 if(panel.tabTip !== undefined){
403 ti.setTooltip(panel.tabTip);
408 updateTitle : function(title){
409 if(this.titleTextEl && !this.config.title){
410 this.titleTextEl.innerHTML = (typeof title != "undefined" && title.length > 0 ? title : " ");
414 setActivePanel : function(panel){
415 panel = this.getPanel(panel);
416 if(this.activePanel && this.activePanel != panel){
417 this.activePanel.setActiveState(false);
419 this.activePanel = panel;
420 panel.setActiveState(true);
422 panel.setSize(this.panelSize.width, this.panelSize.height);
425 this.closeBtn.setVisible(!this.config.closeOnTab && !this.isSlid && panel.isClosable());
427 this.updateTitle(panel.getTitle());
429 this.fireEvent("invalidated", this);
431 this.fireEvent("panelactivated", this, panel);
435 * Shows the specified panel.
436 * @param {Number/String/ContentPanel} panelId The panel's index, id or the panel itself
437 * @return {Roo.ContentPanel} The shown panel, or null if a panel could not be found from panelId
439 showPanel : function(panel){
440 if(panel = this.getPanel(panel)){
442 var tab = this.tabs.getTab(panel.getEl().id);
444 this.tabs.unhideTab(tab.id);
448 this.setActivePanel(panel);
455 * Get the active panel for this region.
456 * @return {Roo.ContentPanel} The active panel or null
458 getActivePanel : function(){
459 return this.activePanel;
462 validateVisibility : function(){
463 if(this.panels.getCount() < 1){
464 this.updateTitle(" ");
465 this.closeBtn.hide();
468 if(!this.isVisible()){
475 * Adds the passed ContentPanel(s) to this region.
476 * @param {ContentPanel...} panel The ContentPanel(s) to add (you can pass more than one)
477 * @return {Roo.ContentPanel} The panel added (if only one was added; null otherwise)
479 add : function(panel){
480 if(arguments.length > 1){
481 for(var i = 0, len = arguments.length; i < len; i++) {
482 this.add(arguments[i]);
486 if(this.hasPanel(panel)){
487 this.showPanel(panel);
490 panel.setRegion(this);
491 this.panels.add(panel);
492 if(this.panels.getCount() == 1 && !this.config.alwaysShowTabs){
493 this.bodyEl.dom.appendChild(panel.getEl().dom);
494 if(panel.background !== true){
495 this.setActivePanel(panel);
497 this.fireEvent("paneladded", this, panel);
503 this.initPanelAsTab(panel);
505 if(panel.background !== true){
506 this.tabs.activate(panel.getEl().id);
508 this.fireEvent("paneladded", this, panel);
513 * Hides the tab for the specified panel.
514 * @param {Number/String/ContentPanel} panel The panel's index, id or the panel itself
516 hidePanel : function(panel){
517 if(this.tabs && (panel = this.getPanel(panel))){
518 this.tabs.hideTab(panel.getEl().id);
523 * Unhides the tab for a previously hidden panel.
524 * @param {Number/String/ContentPanel} panel The panel's index, id or the panel itself
526 unhidePanel : function(panel){
527 if(this.tabs && (panel = this.getPanel(panel))){
528 this.tabs.unhideTab(panel.getEl().id);
532 clearPanels : function(){
533 while(this.panels.getCount() > 0){
534 this.remove(this.panels.first());
539 * Removes the specified panel. If preservePanel is not true (either here or in the config), the panel is destroyed.
540 * @param {Number/String/ContentPanel} panel The panel's index, id or the panel itself
541 * @param {Boolean} preservePanel Overrides the config preservePanel option
542 * @return {Roo.ContentPanel} The panel that was removed
544 remove : function(panel, preservePanel){
545 panel = this.getPanel(panel);
550 this.fireEvent("beforeremove", this, panel, e);
551 if(e.cancel === true){
554 preservePanel = (typeof preservePanel != "undefined" ? preservePanel : (this.config.preservePanels === true || panel.preserve === true));
555 var panelId = panel.getId();
556 this.panels.removeKey(panelId);
558 document.body.appendChild(panel.getEl().dom);
561 this.tabs.removeTab(panel.getEl().id);
562 }else if (!preservePanel){
563 this.bodyEl.dom.removeChild(panel.getEl().dom);
565 if(this.panels.getCount() == 1 && this.tabs && !this.config.alwaysShowTabs){
566 var p = this.panels.first();
567 var tempEl = document.createElement("div"); // temp holder to keep IE from deleting the node
568 tempEl.appendChild(p.getEl().dom);
569 this.bodyEl.update("");
570 this.bodyEl.dom.appendChild(p.getEl().dom);
572 this.updateTitle(p.getTitle());
574 this.bodyEl.setStyle("overflow", this.config.autoScroll ? "auto" : "hidden");
575 this.setActivePanel(p);
577 panel.setRegion(null);
578 if(this.activePanel == panel){
579 this.activePanel = null;
581 if(this.config.autoDestroy !== false && preservePanel !== true){
582 try{panel.destroy();}catch(e){}
584 this.fireEvent("panelremoved", this, panel);
589 * Returns the TabPanel component used by this region
590 * @return {Roo.TabPanel}
592 getTabs : function(){
596 createTool : function(parentEl, className){
597 var btn = Roo.DomHelper.append(parentEl, {tag: "div", cls: "x-layout-tools-button",
598 children: [{tag: "div", cls: "x-layout-tools-button-inner " + className, html: " "}]}, true);
599 btn.addClassOnOver("x-layout-tools-button-over");