Fix #7978 - tidy up layout classes
[roojs1] / Roo / bootstrap / layout / Basic.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11  
12 /**
13  * @class Roo.bootstrap.layout.Basic
14  * @extends Roo.util.Observable
15  * This class represents a lightweight region in a layout manager. This region does not move dom nodes
16  * and does not have a titlebar, tabs or any other features. All it does is size and position 
17  * panels. To create a BasicLayoutRegion, add lightweight:true or basic:true to your regions config.
18  * @cfg {Roo.bootstrap.layout.Manager}   mgr The manager
19  * @cfg {string}   region  the region that it inhabits..
20  * @cfg {bool}   skipConfig skip config?
21  * 
22
23  */
24 Roo.bootstrap.layout.Basic = function(config){
25     
26     this.mgr = config.mgr;
27     
28     this.position = config.region;
29     
30     var skipConfig = config.skipConfig;
31     
32     this.events = {
33         /**
34          * @scope Roo.layout.BasicRegion
35          */
36         
37         /**
38          * @event beforeremove
39          * Fires before a panel is removed (or closed). To cancel the removal set "e.cancel = true" on the event argument.
40          * @param {Roo.layout.Region} this
41          * @param {Roo.ContentPanel} panel The panel
42          * @param {Object} e The cancel event object
43          */
44         "beforeremove" : true,
45         /**
46          * @event invalidated
47          * Fires when the layout for this region is changed.
48          * @param {Roo.layout.Region} this
49          */
50         "invalidated" : true,
51         /**
52          * @event visibilitychange
53          * Fires when this region is shown or hidden 
54          * @param {Roo.layout.Region} this
55          * @param {Boolean} visibility true or false
56          */
57         "visibilitychange" : true,
58         /**
59          * @event paneladded
60          * Fires when a panel is added. 
61          * @param {Roo.layout.Region} this
62          * @param {Roo.ContentPanel} panel The panel
63          */
64         "paneladded" : true,
65         /**
66          * @event panelremoved
67          * Fires when a panel is removed. 
68          * @param {Roo.layout.Region} this
69          * @param {Roo.ContentPanel} panel The panel
70          */
71         "panelremoved" : true,
72         /**
73          * @event beforecollapse
74          * Fires when this region before collapse.
75          * @param {Roo.layout.Region} this
76          */
77         "beforecollapse" : true,
78         /**
79          * @event collapsed
80          * Fires when this region is collapsed.
81          * @param {Roo.layout.Region} this
82          */
83         "collapsed" : true,
84         /**
85          * @event expanded
86          * Fires when this region is expanded.
87          * @param {Roo.layout.Region} this
88          */
89         "expanded" : true,
90         /**
91          * @event slideshow
92          * Fires when this region is slid into view.
93          * @param {Roo.layout.Region} this
94          */
95         "slideshow" : true,
96         /**
97          * @event slidehide
98          * Fires when this region slides out of view. 
99          * @param {Roo.layout.Region} this
100          */
101         "slidehide" : true,
102         /**
103          * @event panelactivated
104          * Fires when a panel is activated. 
105          * @param {Roo.layout.Region} this
106          * @param {Roo.ContentPanel} panel The activated panel
107          */
108         "panelactivated" : true,
109         /**
110          * @event resized
111          * Fires when the user resizes this region. 
112          * @param {Roo.layout.Region} this
113          * @param {Number} newSize The new size (width for east/west, height for north/south)
114          */
115         "resized" : true
116     };
117     /** A collection of panels in this region. @type Roo.util.MixedCollection */
118     this.panels = new Roo.util.MixedCollection();
119     this.panels.getKey = this.getPanelId.createDelegate(this);
120     this.box = null;
121     this.activePanel = null;
122     // ensure listeners are added...
123     
124     if (config.listeners || config.events) {
125         Roo.bootstrap.layout.Basic.superclass.constructor.call(this, {
126             listeners : config.listeners || {},
127             events : config.events || {}
128         });
129     }
130     
131     if(skipConfig !== true){
132         this.applyConfig(config);
133     }
134 };
135
136 Roo.extend(Roo.bootstrap.layout.Basic, Roo.util.Observable,
137 {
138     getPanelId : function(p){
139         return p.getId();
140     },
141     
142     applyConfig : function(config){
143         this.margins = config.margins || this.margins || {top: 0, left: 0, right:0, bottom: 0};
144         this.config = config;
145         
146     },
147     
148     /**
149      * Resizes the region to the specified size. For vertical regions (west, east) this adjusts 
150      * the width, for horizontal (north, south) the height.
151      * @param {Number} newSize The new width or height
152      */
153     resizeTo : function(newSize){
154         var el = this.el ? this.el :
155                  (this.activePanel ? this.activePanel.getEl() : null);
156         if(el){
157             switch(this.position){
158                 case "east":
159                 case "west":
160                     el.setWidth(newSize);
161                     this.fireEvent("resized", this, newSize);
162                 break;
163                 case "north":
164                 case "south":
165                     el.setHeight(newSize);
166                     this.fireEvent("resized", this, newSize);
167                 break;                
168             }
169         }
170     },
171     
172     getBox : function(){
173         return this.activePanel ? this.activePanel.getEl().getBox(false, true) : null;
174     },
175     
176     getMargins : function(){
177         return this.margins;
178     },
179     
180     updateBox : function(box){
181         this.box = box;
182         var el = this.activePanel.getEl();
183         el.dom.style.left = box.x + "px";
184         el.dom.style.top = box.y + "px";
185         this.activePanel.setSize(box.width, box.height);
186     },
187     
188     /**
189      * Returns the container element for this region.
190      * @return {Roo.Element}
191      */
192     getEl : function(){
193         return this.activePanel;
194     },
195     
196     /**
197      * Returns true if this region is currently visible.
198      * @return {Boolean}
199      */
200     isVisible : function(){
201         return this.activePanel ? true : false;
202     },
203     
204     setActivePanel : function(panel){
205         panel = this.getPanel(panel);
206         if(this.activePanel && this.activePanel != panel){
207             this.activePanel.setActiveState(false);
208             this.activePanel.getEl().setLeftTop(-10000,-10000);
209         }
210         this.activePanel = panel;
211         panel.setActiveState(true);
212         if(this.box){
213             panel.setSize(this.box.width, this.box.height);
214         }
215         this.fireEvent("panelactivated", this, panel);
216         this.fireEvent("invalidated");
217     },
218     
219     /**
220      * Show the specified panel.
221      * @param {Number/String/ContentPanel} panelId The panels index, id or the panel itself
222      * @return {Roo.ContentPanel} The shown panel or null
223      */
224     showPanel : function(panel){
225         panel = this.getPanel(panel);
226         if(panel){
227             this.setActivePanel(panel);
228         }
229         return panel;
230     },
231     
232     /**
233      * Get the active panel for this region.
234      * @return {Roo.ContentPanel} The active panel or null
235      */
236     getActivePanel : function(){
237         return this.activePanel;
238     },
239     
240     /**
241      * Add the passed ContentPanel(s)
242      * @param {ContentPanel...} panel The ContentPanel(s) to add (you can pass more than one)
243      * @return {Roo.ContentPanel} The panel added (if only one was added)
244      */
245     add : function(panel){
246         if(arguments.length > 1){
247             for(var i = 0, len = arguments.length; i < len; i++) {
248                 this.add(arguments[i]);
249             }
250             return null;
251         }
252         if(this.hasPanel(panel)){
253             this.showPanel(panel);
254             return panel;
255         }
256         var el = panel.getEl();
257         if(el.dom.parentNode != this.mgr.el.dom){
258             this.mgr.el.dom.appendChild(el.dom);
259         }
260         if(panel.setRegion){
261             panel.setRegion(this);
262         }
263         this.panels.add(panel);
264         el.setStyle("position", "absolute");
265         if(!panel.background){
266             this.setActivePanel(panel);
267             if(this.config.initialSize && this.panels.getCount()==1){
268                 this.resizeTo(this.config.initialSize);
269             }
270         }
271         this.fireEvent("paneladded", this, panel);
272         return panel;
273     },
274     
275     /**
276      * Returns true if the panel is in this region.
277      * @param {Number/String/ContentPanel} panel The panels index, id or the panel itself
278      * @return {Boolean}
279      */
280     hasPanel : function(panel){
281         if(typeof panel == "object"){ // must be panel obj
282             panel = panel.getId();
283         }
284         return this.getPanel(panel) ? true : false;
285     },
286     
287     /**
288      * Removes the specified panel. If preservePanel is not true (either here or in the config), the panel is destroyed.
289      * @param {Number/String/ContentPanel} panel The panels index, id or the panel itself
290      * @param {Boolean} preservePanel Overrides the config preservePanel option
291      * @return {Roo.ContentPanel} The panel that was removed
292      */
293     remove : function(panel, preservePanel){
294         panel = this.getPanel(panel);
295         if(!panel){
296             return null;
297         }
298         var e = {};
299         this.fireEvent("beforeremove", this, panel, e);
300         if(e.cancel === true){
301             return null;
302         }
303         var panelId = panel.getId();
304         this.panels.removeKey(panelId);
305         return panel;
306     },
307     
308     /**
309      * Returns the panel specified or null if it's not in this region.
310      * @param {Number/String/ContentPanel} panel The panels index, id or the panel itself
311      * @return {Roo.ContentPanel}
312      */
313     getPanel : function(id){
314         if(typeof id == "object"){ // must be panel obj
315             return id;
316         }
317         return this.panels.get(id);
318     },
319     
320     /**
321      * Returns this regions position (north/south/east/west/center).
322      * @return {String} 
323      */
324     getPosition: function(){
325         return this.position;    
326     }
327 });