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">
12 * @class Roo.bootstrap.layout.Border
13 * @extends Roo.bootstrap.layout.Manager
14 * This class represents a common layout manager used in desktop applications. For screenshots and more details,
15 * please see: examples/bootstrap/nested.html<br><br>
17 <b>The container the layout is rendered into can be either the body element or any other element.
18 If it is not the body element, the container needs to either be an absolute positioned element,
19 or you will need to add "position:relative" to the css of the container. You will also need to specify
20 the container size if it is not the body element.</b>
24 * @param {Object} config Configuration options
26 Roo.bootstrap.layout.Border = function(config){
27 config = config || {};
28 Roo.bootstrap.layout.Border.superclass.constructor.call(this, config);
29 this.factory = config.factory || Roo.BorderLayout.RegionFactory;
31 Roo.each(Roo.bootstrap.layout.Border.regions, function(target) {
33 config[target].target = region;
34 this.addRegion(config[target]);
40 Roo.bootstrap.layout.Border.regions = ["north","south","east","west","center"];
42 Roo.extend(Roo.bootstrap.layout.Border, Roo.bootstrap.layout.Manager, {
44 * Creates and adds a new region if it doesn't already exist.
45 * @param {String} target The target region key (north, south, east, west or center).
46 * @param {Object} config The regions config object
47 * @return {BorderLayoutRegion} The new region
49 addRegion : function(target, config)
51 if(!this.regions[target]){
52 var r = this.factory(config);
55 return this.regions[target];
59 bindRegion : function(r){
60 this.regions[r.target] = r;
62 r.on("visibilitychange", this.layout, this);
63 r.on("paneladded", this.layout, this);
64 r.on("panelremoved", this.layout, this);
65 r.on("invalidated", this.layout, this);
66 r.on("resized", this.onRegionResized, this);
67 r.on("collapsed", this.onRegionCollapsed, this);
68 r.on("expanded", this.onRegionExpanded, this);
72 * Performs a layout update.
79 var size = this.getViewSize();
88 var rs = this.regions;
89 var north = rs["north"];
90 var south = rs["south"];
91 var west = rs["west"];
92 var east = rs["east"];
93 var center = rs["center"];
94 //if(this.hideOnLayout){ // not supported anymore
95 //c.el.setStyle("display", "none");
97 if(north && north.isVisible()){
98 var b = north.getBox();
99 var m = north.getMargins();
100 b.width = w - (m.left+m.right);
103 centerY = b.height + b.y + m.bottom;
105 north.updateBox(this.safeBox(b));
107 if(south && south.isVisible()){
108 var b = south.getBox();
109 var m = south.getMargins();
110 b.width = w - (m.left+m.right);
112 var totalHeight = (b.height + m.top + m.bottom);
113 b.y = h - totalHeight + m.top;
114 centerH -= totalHeight;
115 south.updateBox(this.safeBox(b));
117 if(west && west.isVisible()){
118 var b = west.getBox();
119 var m = west.getMargins();
120 b.height = centerH - (m.top+m.bottom);
122 b.y = centerY + m.top;
123 var totalWidth = (b.width + m.left + m.right);
124 centerX += totalWidth;
125 centerW -= totalWidth;
126 west.updateBox(this.safeBox(b));
128 if(east && east.isVisible()){
129 var b = east.getBox();
130 var m = east.getMargins();
131 b.height = centerH - (m.top+m.bottom);
132 var totalWidth = (b.width + m.left + m.right);
133 b.x = w - totalWidth + m.left;
134 b.y = centerY + m.top;
135 centerW -= totalWidth;
136 east.updateBox(this.safeBox(b));
139 var m = center.getMargins();
143 width: centerW - (m.left+m.right),
144 height: centerH - (m.top+m.bottom)
146 //if(this.hideOnLayout){
147 //center.el.setStyle("display", "block");
149 center.updateBox(this.safeBox(centerBox));
152 this.fireEvent("layout", this);
156 safeBox : function(box){
157 box.width = Math.max(0, box.width);
158 box.height = Math.max(0, box.height);
163 * Adds a ContentPanel (or subclass) to this layout.
164 * @param {String} target The target region key (north, south, east, west or center).
165 * @param {Roo.ContentPanel} panel The panel to add
166 * @return {Roo.ContentPanel} The added panel
168 add : function(target, panel){
170 target = target.toLowerCase();
171 return this.regions[target].add(panel);
175 * Remove a ContentPanel (or subclass) to this layout.
176 * @param {String} target The target region key (north, south, east, west or center).
177 * @param {Number/String/Roo.ContentPanel} panel The index, id or panel to remove
178 * @return {Roo.ContentPanel} The removed panel
180 remove : function(target, panel){
181 target = target.toLowerCase();
182 return this.regions[target].remove(panel);
186 * Searches all regions for a panel with the specified id
187 * @param {String} panelId
188 * @return {Roo.ContentPanel} The panel or null if it wasn't found
190 findPanel : function(panelId){
191 var rs = this.regions;
192 for(var target in rs){
193 if(typeof rs[target] != "function"){
194 var p = rs[target].getPanel(panelId);
204 * Searches all regions for a panel with the specified id and activates (shows) it.
205 * @param {String/ContentPanel} panelId The panels id or the panel itself
206 * @return {Roo.ContentPanel} The shown panel or null
208 showPanel : function(panelId) {
209 var rs = this.regions;
210 for(var target in rs){
212 if(typeof r != "function"){
213 if(r.hasPanel(panelId)){
214 return r.showPanel(panelId);
222 * Restores this layout's state using Roo.state.Manager or the state provided by the passed provider.
223 * @param {Roo.state.Provider} provider (optional) An alternate state provider
226 restoreState : function(provider){
228 provider = Roo.state.Manager;
230 var sm = new Roo.LayoutStateManager();
231 sm.init(this, provider);
237 * Adds a xtype elements to the layout.
241 xtype : 'ContentPanel',
248 xtype : 'NestedLayoutPanel',
254 items : [ ... list of content panels or nested layout panels.. ]
258 * @param {Object} cfg Xtype definition of item to add.
260 addxtype : function(cfg)
262 // basically accepts a pannel...
263 // can accept a layout region..!?!?
264 //Roo.log('Roo.BorderLayout add ' + cfg.xtype)
267 // theory? children can only be panels??
268 //if (!cfg.xtype.match(/Panel$/)) {
273 if (typeof(cfg.region) == 'undefined') {
274 Roo.log("Failed to add Panel, region was not set");
278 var region = cfg.region;
291 case 'ContentPanel': // ContentPanel (el, cfg)
292 case 'ScrollPanel': // ContentPanel (el, cfg)
295 ret = new Roo[cfg.xtype](cfg); // new panel!!!!!
297 var el = this.el.createChild();
298 ret = new Roo[cfg.xtype](el, cfg); // new panel!!!!!
301 this.add(region, ret);
305 case 'TreePanel': // our new panel!
306 cfg.el = this.el.createChild();
307 ret = new Roo[cfg.xtype](cfg); // new panel!!!!!
308 this.add(region, ret);
311 case 'NestedLayoutPanel':
312 // create a new Layout (which is a Border Layout...
313 var el = this.el.createChild();
314 var clayout = cfg.layout;
316 clayout.items = clayout.items || [];
317 // replace this exitems with the clayout ones..
318 xitems = clayout.items;
321 if (region == 'center' && this.active && this.getRegion('center').panels.length < 1) {
322 cfg.background = false;
324 var layout = new Roo.BorderLayout(el, clayout);
326 ret = new Roo[cfg.xtype](layout, cfg); // new panel!!!!!
327 //console.log('adding nested layout panel ' + cfg.toSource());
328 this.add(region, ret);
329 nb = {}; /// find first...
334 // needs grid and region
336 //var el = this.getRegion(region).el.createChild();
337 var el = this.el.createChild();
338 // create the grid first...
340 var grid = new Roo.grid[cfg.grid.xtype](el, cfg.grid);
342 if (region == 'center' && this.active ) {
343 cfg.background = false;
345 ret = new Roo[cfg.xtype](grid, cfg); // new panel!!!!!
347 this.add(region, ret);
348 if (cfg.background) {
349 ret.on('activate', function(gp) {
350 if (!gp.grid.rendered) {
365 if (typeof(Roo[cfg.xtype]) != 'undefined') {
367 ret = new Roo[cfg.xtype](cfg); // new panel!!!!!
368 this.add(region, ret);
371 alert("Can not add '" + cfg.xtype + "' to BorderLayout");
375 // GridPanel (grid, cfg)
382 Roo.each(xitems, function(i) {
383 region = nb && i.region ? i.region : false;
385 var add = ret.addxtype(i);
388 nb[region] = nb[region] == undefined ? 0 : nb[region]+1;
390 abn[region] = nb[region] ;
397 // make the last non-background panel active..
398 //if (nb) { Roo.log(abn); }
402 region = this.getRegion(r);
404 // tried using nb[r], but it does not work..
406 region.showPanel(abn[r]);
417 factory : function(cfg)
420 var validRegions = ["north","south","east","west","center"];
422 var target = cfg.region;
425 var r = Roo.bootstrap.layout;
429 return new r.North(cfg);
431 return new r.South(cfg);
433 return new r.East(cfg);
435 return new r.West(cfg);
437 return new r.Center(cfg);
439 throw 'Layout region "'+target+'" not supported.';