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??
269 //if (!cfg.xtype.match(/Panel$/)) {
274 if (typeof(cfg.region) == 'undefined') {
275 Roo.log("Failed to add Panel, region was not set");
279 var region = cfg.region;
292 case 'Content': // ContentPanel (el, cfg)
293 case 'Scroll': // ContentPanel (el, cfg)
295 cfg.autoCreate = true;
296 ret = new Roo[cfg.xtype](cfg); // new panel!!!!!
298 // var el = this.el.createChild();
299 // ret = new Roo[cfg.xtype](el, cfg); // new panel!!!!!
302 this.add(region, ret);
306 case 'TreePanel': // our new panel!
307 cfg.el = this.el.createChild();
308 ret = new Roo[cfg.xtype](cfg); // new panel!!!!!
309 this.add(region, ret);
313 case 'NestedLayoutPanel':
314 // create a new Layout (which is a Border Layout...
315 var el = this.el.createChild();
316 var clayout = cfg.layout;
318 clayout.items = clayout.items || [];
319 // replace this exitems with the clayout ones..
320 xitems = clayout.items;
323 if (region == 'center' && this.active && this.getRegion('center').panels.length < 1) {
324 cfg.background = false;
326 var layout = new Roo.BorderLayout(el, clayout);
328 ret = new Roo[cfg.xtype](layout, cfg); // new panel!!!!!
329 //console.log('adding nested layout panel ' + cfg.toSource());
330 this.add(region, ret);
331 nb = {}; /// find first...
336 // needs grid and region
338 //var el = this.getRegion(region).el.createChild();
339 var el = this.el.createChild();
340 // create the grid first...
342 var grid = new Roo.grid[cfg.grid.xtype](el, cfg.grid);
344 if (region == 'center' && this.active ) {
345 cfg.background = false;
347 ret = new Roo[cfg.xtype](grid, cfg); // new panel!!!!!
349 this.add(region, ret);
350 if (cfg.background) {
351 ret.on('activate', function(gp) {
352 if (!gp.grid.rendered) {
367 if (typeof(Roo[cfg.xtype]) != 'undefined') {
369 ret = new Roo[cfg.xtype](cfg); // new panel!!!!!
370 this.add(region, ret);
373 alert("Can not add '" + cfg.xtype + "' to BorderLayout");
377 // GridPanel (grid, cfg)
384 Roo.each(xitems, function(i) {
385 region = nb && i.region ? i.region : false;
387 var add = ret.addxtype(i);
390 nb[region] = nb[region] == undefined ? 0 : nb[region]+1;
392 abn[region] = nb[region] ;
399 // make the last non-background panel active..
400 //if (nb) { Roo.log(abn); }
404 region = this.getRegion(r);
406 // tried using nb[r], but it does not work..
408 region.showPanel(abn[r]);
419 factory : function(cfg)
422 var validRegions = ["north","south","east","west","center"];
424 var target = cfg.region;
427 var r = Roo.bootstrap.layout;
431 return new r.North(cfg);
433 return new r.South(cfg);
435 return new r.East(cfg);
437 return new r.West(cfg);
439 return new r.Center(cfg);
441 throw 'Layout region "'+target+'" not supported.';