Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / src / widgets / BoxComponent.js
1 /*!
2  * Ext JS Library 3.0.3
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8  * @class Ext.BoxComponent
9  * @extends Ext.Component
10  * <p>Base class for any {@link Ext.Component Component} that is to be sized as a box, using width and height.</p>
11  * <p>BoxComponent provides automatic box model adjustments for sizing and positioning and will work correctly
12  * within the Component rendering model.</p>
13  * <p>A BoxComponent may be created as a custom Component which encapsulates any HTML element, either a pre-existing
14  * element, or one that is created to your specifications at render time. Usually, to participate in layouts,
15  * a Component will need to be a <b>Box</b>Component in order to have its width and height managed.</p>
16  * <p>To use a pre-existing element as a BoxComponent, configure it so that you preset the <b>el</b> property to the
17  * element to reference:<pre><code>
18 var pageHeader = new Ext.BoxComponent({
19     el: 'my-header-div'
20 });</code></pre>
21  * This may then be {@link Ext.Container#add added} to a {@link Ext.Container Container} as a child item.</p>
22  * <p>To create a BoxComponent based around a HTML element to be created at render time, use the
23  * {@link Ext.Component#autoEl autoEl} config option which takes the form of a
24  * {@link Ext.DomHelper DomHelper} specification:<pre><code>
25 var myImage = new Ext.BoxComponent({
26     autoEl: {
27         tag: 'img',
28         src: '/images/my-image.jpg'
29     }
30 });</code></pre></p>
31  * @constructor
32  * @param {Ext.Element/String/Object} config The configuration options.
33  * @xtype box
34  */
35 Ext.BoxComponent = Ext.extend(Ext.Component, {
36
37     // tabTip config is used when a BoxComponent is a child of a TabPanel
38     /**
39      * @cfg {String} tabTip
40      * <p><b>Note</b>: this config is only used when this BoxComponent is a child item of a TabPanel.</p>
41      * A string to be used as innerHTML (html tags are accepted) to show in a tooltip when mousing over
42      * the associated tab selector element. {@link Ext.QuickTips}.init()
43      * must be called in order for the tips to render.
44      */
45     // Configs below are used for all Components when rendered by BorderLayout.
46     /**
47      * @cfg {String} region <p><b>Note</b>: this config is only used when this BoxComponent is rendered
48      * by a Container which has been configured to use the <b>{@link Ext.layout.BorderLayout BorderLayout}</b>
49      * layout manager (e.g. specifying <tt>layout:'border'</tt>).</p><br>
50      * <p>See {@link Ext.layout.BorderLayout} also.</p>
51      */
52     // margins config is used when a BoxComponent is rendered by BorderLayout or BoxLayout.
53     /**
54      * @cfg {Object} margins <p><b>Note</b>: this config is only used when this BoxComponent is rendered
55      * by a Container which has been configured to use the <b>{@link Ext.layout.BorderLayout BorderLayout}</b>
56      * or one of the two <b>{@link Ext.layout.BoxLayout BoxLayout} subclasses.</b></p>
57      * <p>An object containing margins to apply to this BoxComponent in the
58      * format:</p><pre><code>
59 {
60     top: (top margin),
61     right: (right margin),
62     bottom: (bottom margin),
63     left: (left margin)
64 }</code></pre>
65      * <p>May also be a string containing space-separated, numeric margin values. The order of the
66      * sides associated with each value matches the way CSS processes margin values:</p>
67      * <p><div class="mdetail-params"><ul>
68      * <li>If there is only one value, it applies to all sides.</li>
69      * <li>If there are two values, the top and bottom borders are set to the first value and the
70      * right and left are set to the second.</li>
71      * <li>If there are three values, the top is set to the first value, the left and right are set
72      * to the second, and the bottom is set to the third.</li>
73      * <li>If there are four values, they apply to the top, right, bottom, and left, respectively.</li>
74      * </ul></div></p>
75      * <p>Defaults to:</p><pre><code>
76      * {top:0, right:0, bottom:0, left:0}
77      * </code></pre>
78      */
79     /**
80      * @cfg {Number} x
81      * The local x (left) coordinate for this component if contained within a positioning container.
82      */
83     /**
84      * @cfg {Number} y
85      * The local y (top) coordinate for this component if contained within a positioning container.
86      */
87     /**
88      * @cfg {Number} pageX
89      * The page level x coordinate for this component if contained within a positioning container.
90      */
91     /**
92      * @cfg {Number} pageY
93      * The page level y coordinate for this component if contained within a positioning container.
94      */
95     /**
96      * @cfg {Number} height
97      * The height of this component in pixels (defaults to auto).
98      * <b>Note</b> to express this dimension as a percentage or offset see {@link Ext.Component#anchor}.
99      */
100     /**
101      * @cfg {Number} width
102      * The width of this component in pixels (defaults to auto).
103      * <b>Note</b> to express this dimension as a percentage or offset see {@link Ext.Component#anchor}.
104      */
105     /**
106      * @cfg {Boolean} autoHeight
107      * <p>True to use height:'auto', false to use fixed height (or allow it to be managed by its parent
108      * Container's {@link Ext.Container#layout layout manager}. Defaults to false.</p>
109      * <p><b>Note</b>: Although many components inherit this config option, not all will
110      * function as expected with a height of 'auto'. Setting autoHeight:true means that the
111      * browser will manage height based on the element's contents, and that Ext will not manage it at all.</p>
112      * <p>If the <i>browser</i> is managing the height, be aware that resizes performed by the browser in response
113      * to changes within the structure of the Component cannot be detected. Therefore changes to the height might
114      * result in elements needing to be synchronized with the new height. Example:</p><pre><code>
115 var w = new Ext.Window({
116     title: 'Window',
117     width: 600,
118     autoHeight: true,
119     items: {
120         title: 'Collapse Me',
121         height: 400,
122         collapsible: true,
123         border: false,
124         listeners: {
125             beforecollapse: function() {
126                 w.el.shadow.hide();
127             },
128             beforeexpand: function() {
129                 w.el.shadow.hide();
130             },
131             collapse: function() {
132                 w.syncShadow();
133             },
134             expand: function() {
135                 w.syncShadow();
136             }
137         }
138     }
139 }).show();
140 </code></pre>
141      */
142     /**
143      * @cfg {Boolean} autoWidth
144      * <p>True to use width:'auto', false to use fixed width (or allow it to be managed by its parent
145      * Container's {@link Ext.Container#layout layout manager}. Defaults to false.</p>
146      * <p><b>Note</b>: Although many components  inherit this config option, not all will
147      * function as expected with a width of 'auto'. Setting autoWidth:true means that the
148      * browser will manage width based on the element's contents, and that Ext will not manage it at all.</p>
149      * <p>If the <i>browser</i> is managing the width, be aware that resizes performed by the browser in response
150      * to changes within the structure of the Component cannot be detected. Therefore changes to the width might
151      * result in elements needing to be synchronized with the new width. For example, where the target element is:</p><pre><code>
152 &lt;div id='grid-container' style='margin-left:25%;width:50%'>&lt;/div>
153 </code></pre>
154      * A Panel rendered into that target element must listen for browser window resize in order to relay its
155       * child items when the browser changes its width:<pre><code>
156 var myPanel = new Ext.Panel({
157     renderTo: 'grid-container',
158     monitorResize: true, // relay on browser resize
159     title: 'Panel',
160     height: 400,
161     autoWidth: true,
162     layout: 'hbox',
163     layoutConfig: {
164         align: 'stretch'
165     },
166     defaults: {
167         flex: 1
168     },
169     items: [{
170         title: 'Box 1',
171     }, {
172         title: 'Box 2'
173     }, {
174         title: 'Box 3'
175     }],
176 });
177 </code></pre>
178      */
179
180     /* // private internal config
181      * {Boolean} deferHeight
182      * True to defer height calculations to an external component, false to allow this component to set its own
183      * height (defaults to false).
184      */
185
186     // private
187     initComponent : function(){
188         Ext.BoxComponent.superclass.initComponent.call(this);
189         this.addEvents(
190             /**
191              * @event resize
192              * Fires after the component is resized.
193              * @param {Ext.Component} this
194              * @param {Number} adjWidth The box-adjusted width that was set
195              * @param {Number} adjHeight The box-adjusted height that was set
196              * @param {Number} rawWidth The width that was originally specified
197              * @param {Number} rawHeight The height that was originally specified
198              */
199             'resize',
200             /**
201              * @event move
202              * Fires after the component is moved.
203              * @param {Ext.Component} this
204              * @param {Number} x The new x position
205              * @param {Number} y The new y position
206              */
207             'move'
208         );
209     },
210
211     // private, set in afterRender to signify that the component has been rendered
212     boxReady : false,
213     // private, used to defer height settings to subclasses
214     deferHeight: false,
215
216     /**
217      * Sets the width and height of this BoxComponent. This method fires the {@link #resize} event. This method can accept
218      * either width and height as separate arguments, or you can pass a size object like <code>{width:10, height:20}</code>.
219      * @param {Mixed} width The new width to set. This may be one of:<div class="mdetail-params"><ul>
220      * <li>A Number specifying the new width in the {@link #getEl Element}'s {@link Ext.Element#defaultUnit}s (by default, pixels).</li>
221      * <li>A String used to set the CSS width style.</li>
222      * <li>A size object in the format <code>{width: widthValue, height: heightValue}</code>.</li>
223      * <li><code>undefined</code> to leave the width unchanged.</li>
224      * </ul></div>
225      * @param {Mixed} height The new height to set (not required if a size object is passed as the first arg).
226      * This may be one of:<div class="mdetail-params"><ul>
227      * <li>A Number specifying the new height in the {@link #getEl Element}'s {@link Ext.Element#defaultUnit}s (by default, pixels).</li>
228      * <li>A String used to set the CSS height style. Animation may <b>not</b> be used.</li>
229      * <li><code>undefined</code> to leave the height unchanged.</li>
230      * </ul></div>
231      * @return {Ext.BoxComponent} this
232      */
233     setSize : function(w, h){
234         // support for standard size objects
235         if(typeof w == 'object'){
236             h = w.height;
237             w = w.width;
238         }
239         // not rendered
240         if(!this.boxReady){
241             this.width = w;
242             this.height = h;
243             return this;
244         }
245
246         // prevent recalcs when not needed
247         if(this.cacheSizes !== false && this.lastSize && this.lastSize.width == w && this.lastSize.height == h){
248             return this;
249         }
250         this.lastSize = {width: w, height: h};
251         var adj = this.adjustSize(w, h);
252         var aw = adj.width, ah = adj.height;
253         if(aw !== undefined || ah !== undefined){ // this code is nasty but performs better with floaters
254             var rz = this.getResizeEl();
255             if(!this.deferHeight && aw !== undefined && ah !== undefined){
256                 rz.setSize(aw, ah);
257             }else if(!this.deferHeight && ah !== undefined){
258                 rz.setHeight(ah);
259             }else if(aw !== undefined){
260                 rz.setWidth(aw);
261             }
262             this.onResize(aw, ah, w, h);
263             this.fireEvent('resize', this, aw, ah, w, h);
264         }
265         return this;
266     },
267
268     /**
269      * Sets the width of the component.  This method fires the {@link #resize} event.
270      * @param {Number} width The new width to setThis may be one of:<div class="mdetail-params"><ul>
271      * <li>A Number specifying the new width in the {@link #getEl Element}'s {@link Ext.Element#defaultUnit}s (by default, pixels).</li>
272      * <li>A String used to set the CSS width style.</li>
273      * </ul></div>
274      * @return {Ext.BoxComponent} this
275      */
276     setWidth : function(width){
277         return this.setSize(width);
278     },
279
280     /**
281      * Sets the height of the component.  This method fires the {@link #resize} event.
282      * @param {Number} height The new height to set. This may be one of:<div class="mdetail-params"><ul>
283      * <li>A Number specifying the new height in the {@link #getEl Element}'s {@link Ext.Element#defaultUnit}s (by default, pixels).</li>
284      * <li>A String used to set the CSS height style.</li>
285      * <li><i>undefined</i> to leave the height unchanged.</li>
286      * </ul></div>
287      * @return {Ext.BoxComponent} this
288      */
289     setHeight : function(height){
290         return this.setSize(undefined, height);
291     },
292
293     /**
294      * Gets the current size of the component's underlying element.
295      * @return {Object} An object containing the element's size {width: (element width), height: (element height)}
296      */
297     getSize : function(){
298         return this.getResizeEl().getSize();
299     },
300
301     /**
302      * Gets the current width of the component's underlying element.
303      * @return {Number}
304      */
305     getWidth : function(){
306         return this.getResizeEl().getWidth();
307     },
308
309     /**
310      * Gets the current height of the component's underlying element.
311      * @return {Number}
312      */
313     getHeight : function(){
314         return this.getResizeEl().getHeight();
315     },
316
317     /**
318      * Gets the current size of the component's underlying element, including space taken by its margins.
319      * @return {Object} An object containing the element's size {width: (element width + left/right margins), height: (element height + top/bottom margins)}
320      */
321     getOuterSize : function(){
322         var el = this.getResizeEl();
323         return {width: el.getWidth() + el.getMargins('lr'),
324                 height: el.getHeight() + el.getMargins('tb')};
325     },
326
327     /**
328      * Gets the current XY position of the component's underlying element.
329      * @param {Boolean} local (optional) If true the element's left and top are returned instead of page XY (defaults to false)
330      * @return {Array} The XY position of the element (e.g., [100, 200])
331      */
332     getPosition : function(local){
333         var el = this.getPositionEl();
334         if(local === true){
335             return [el.getLeft(true), el.getTop(true)];
336         }
337         return this.xy || el.getXY();
338     },
339
340     /**
341      * Gets the current box measurements of the component's underlying element.
342      * @param {Boolean} local (optional) If true the element's left and top are returned instead of page XY (defaults to false)
343      * @return {Object} box An object in the format {x, y, width, height}
344      */
345     getBox : function(local){
346         var pos = this.getPosition(local);
347         var s = this.getSize();
348         s.x = pos[0];
349         s.y = pos[1];
350         return s;
351     },
352
353     /**
354      * Sets the current box measurements of the component's underlying element.
355      * @param {Object} box An object in the format {x, y, width, height}
356      * @return {Ext.BoxComponent} this
357      */
358     updateBox : function(box){
359         this.setSize(box.width, box.height);
360         this.setPagePosition(box.x, box.y);
361         return this;
362     },
363
364     /**
365      * <p>Returns the outermost Element of this Component which defines the Components overall size.</p>
366      * <p><i>Usually</i> this will return the same Element as <code>{@link #getEl}</code>,
367      * but in some cases, a Component may have some more wrapping Elements around its main
368      * active Element.</p>
369      * <p>An example is a ComboBox. It is encased in a <i>wrapping</i> Element which
370      * contains both the <code>&lt;input></code> Element (which is what would be returned
371      * by its <code>{@link #getEl}</code> method, <i>and</i> the trigger button Element.
372      * This Element is returned as the <code>resizeEl</code>.
373      */
374     getResizeEl : function(){
375         return this.resizeEl || this.el;
376     },
377
378     // protected
379     getPositionEl : function(){
380         return this.positionEl || this.el;
381     },
382
383     /**
384      * Sets the left and top of the component.  To set the page XY position instead, use {@link #setPagePosition}.
385      * This method fires the {@link #move} event.
386      * @param {Number} left The new left
387      * @param {Number} top The new top
388      * @return {Ext.BoxComponent} this
389      */
390     setPosition : function(x, y){
391         if(x && typeof x[1] == 'number'){
392             y = x[1];
393             x = x[0];
394         }
395         this.x = x;
396         this.y = y;
397         if(!this.boxReady){
398             return this;
399         }
400         var adj = this.adjustPosition(x, y);
401         var ax = adj.x, ay = adj.y;
402
403         var el = this.getPositionEl();
404         if(ax !== undefined || ay !== undefined){
405             if(ax !== undefined && ay !== undefined){
406                 el.setLeftTop(ax, ay);
407             }else if(ax !== undefined){
408                 el.setLeft(ax);
409             }else if(ay !== undefined){
410                 el.setTop(ay);
411             }
412             this.onPosition(ax, ay);
413             this.fireEvent('move', this, ax, ay);
414         }
415         return this;
416     },
417
418     /**
419      * Sets the page XY position of the component.  To set the left and top instead, use {@link #setPosition}.
420      * This method fires the {@link #move} event.
421      * @param {Number} x The new x position
422      * @param {Number} y The new y position
423      * @return {Ext.BoxComponent} this
424      */
425     setPagePosition : function(x, y){
426         if(x && typeof x[1] == 'number'){
427             y = x[1];
428             x = x[0];
429         }
430         this.pageX = x;
431         this.pageY = y;
432         if(!this.boxReady){
433             return;
434         }
435         if(x === undefined || y === undefined){ // cannot translate undefined points
436             return;
437         }
438         var p = this.getPositionEl().translatePoints(x, y);
439         this.setPosition(p.left, p.top);
440         return this;
441     },
442
443     // private
444     afterRender : function(){
445         Ext.BoxComponent.superclass.afterRender.call(this);
446         if(this.resizeEl){
447             this.resizeEl = Ext.get(this.resizeEl);
448         }
449         if(this.positionEl){
450             this.positionEl = Ext.get(this.positionEl);
451         }
452         this.boxReady = true;
453         this.setSize(this.width, this.height);
454         if(this.x || this.y){
455             this.setPosition(this.x, this.y);
456         }else if(this.pageX || this.pageY){
457             this.setPagePosition(this.pageX, this.pageY);
458         }
459     },
460
461     /**
462      * Force the component's size to recalculate based on the underlying element's current height and width.
463      * @return {Ext.BoxComponent} this
464      */
465     syncSize : function(){
466         delete this.lastSize;
467         this.setSize(this.autoWidth ? undefined : this.getResizeEl().getWidth(), this.autoHeight ? undefined : this.getResizeEl().getHeight());
468         return this;
469     },
470
471     /* // protected
472      * Called after the component is resized, this method is empty by default but can be implemented by any
473      * subclass that needs to perform custom logic after a resize occurs.
474      * @param {Number} adjWidth The box-adjusted width that was set
475      * @param {Number} adjHeight The box-adjusted height that was set
476      * @param {Number} rawWidth The width that was originally specified
477      * @param {Number} rawHeight The height that was originally specified
478      */
479     onResize : function(adjWidth, adjHeight, rawWidth, rawHeight){
480
481     },
482
483     /* // protected
484      * Called after the component is moved, this method is empty by default but can be implemented by any
485      * subclass that needs to perform custom logic after a move occurs.
486      * @param {Number} x The new x position
487      * @param {Number} y The new y position
488      */
489     onPosition : function(x, y){
490
491     },
492
493     // private
494     adjustSize : function(w, h){
495         if(this.autoWidth){
496             w = 'auto';
497         }
498         if(this.autoHeight){
499             h = 'auto';
500         }
501         return {width : w, height: h};
502     },
503
504     // private
505     adjustPosition : function(x, y){
506         return {x : x, y: y};
507     }
508 });
509 Ext.reg('box', Ext.BoxComponent);
510
511
512 /**
513  * @class Ext.Spacer
514  * @extends Ext.BoxComponent
515  * <p>Used to provide a sizable space in a layout.</p>
516  * @constructor
517  * @param {Object} config
518  */
519 Ext.Spacer = Ext.extend(Ext.BoxComponent, {
520     autoEl:'div'
521 });
522 Ext.reg('spacer', Ext.Spacer);