3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.BoxComponent"></div>/**
9 * @class Ext.BoxComponent
10 * @extends Ext.Component
11 * <p>Base class for any {@link Ext.Component Component} that is to be sized as a box, using width and height.</p>
12 * <p>BoxComponent provides automatic box model adjustments for sizing and positioning and will work correctly
13 * within the Component rendering model.</p>
14 * <p>A BoxComponent may be created as a custom Component which encapsulates any HTML element, either a pre-existing
15 * element, or one that is created to your specifications at render time. Usually, to participate in layouts,
16 * a Component will need to be a <b>Box</b>Component in order to have its width and height managed.</p>
17 * <p>To use a pre-existing element as a BoxComponent, configure it so that you preset the <b>el</b> property to the
18 * element to reference:<pre><code>
19 var pageHeader = new Ext.BoxComponent({
22 * This may then be {@link Ext.Container#add added} to a {@link Ext.Container Container} as a child item.</p>
23 * <p>To create a BoxComponent based around a HTML element to be created at render time, use the
24 * {@link Ext.Component#autoEl autoEl} config option which takes the form of a
25 * {@link Ext.DomHelper DomHelper} specification:<pre><code>
26 var myImage = new Ext.BoxComponent({
29 src: '/images/my-image.jpg'
33 * @param {Ext.Element/String/Object} config The configuration options.
36 Ext.BoxComponent = Ext.extend(Ext.Component, {
38 // Configs below are used for all Components when rendered by BorderLayout.
39 <div id="cfg-Ext.BoxComponent-region"></div>/**
40 * @cfg {String} region <p><b>Note</b>: this config is only used when this BoxComponent is rendered
41 * by a Container which has been configured to use the <b>{@link Ext.layout.BorderLayout BorderLayout}</b>
42 * layout manager (e.g. specifying <tt>layout:'border'</tt>).</p><br>
43 * <p>See {@link Ext.layout.BorderLayout} also.</p>
45 // margins config is used when a BoxComponent is rendered by BorderLayout or BoxLayout.
46 <div id="cfg-Ext.BoxComponent-margins"></div>/**
47 * @cfg {Object} margins <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 * or one of the two <b>{@link Ext.layout.BoxLayout BoxLayout} subclasses.</b></p>
50 * <p>An object containing margins to apply to this BoxComponent in the
51 * format:</p><pre><code>
54 right: (right margin),
55 bottom: (bottom margin),
58 * <p>May also be a string containing space-separated, numeric margin values. The order of the
59 * sides associated with each value matches the way CSS processes margin values:</p>
60 * <p><div class="mdetail-params"><ul>
61 * <li>If there is only one value, it applies to all sides.</li>
62 * <li>If there are two values, the top and bottom borders are set to the first value and the
63 * right and left are set to the second.</li>
64 * <li>If there are three values, the top is set to the first value, the left and right are set
65 * to the second, and the bottom is set to the third.</li>
66 * <li>If there are four values, they apply to the top, right, bottom, and left, respectively.</li>
68 * <p>Defaults to:</p><pre><code>
69 * {top:0, right:0, bottom:0, left:0}
72 <div id="cfg-Ext.BoxComponent-x"></div>/**
74 * The local x (left) coordinate for this component if contained within a positioning container.
76 <div id="cfg-Ext.BoxComponent-y"></div>/**
78 * The local y (top) coordinate for this component if contained within a positioning container.
80 <div id="cfg-Ext.BoxComponent-pageX"></div>/**
82 * The page level x coordinate for this component if contained within a positioning container.
84 <div id="cfg-Ext.BoxComponent-pageY"></div>/**
86 * The page level y coordinate for this component if contained within a positioning container.
88 <div id="cfg-Ext.BoxComponent-height"></div>/**
89 * @cfg {Number} height
90 * The height of this component in pixels (defaults to auto).
91 * <b>Note</b> to express this dimension as a percentage or offset see {@link Ext.Component#anchor}.
93 <div id="cfg-Ext.BoxComponent-width"></div>/**
95 * The width of this component in pixels (defaults to auto).
96 * <b>Note</b> to express this dimension as a percentage or offset see {@link Ext.Component#anchor}.
98 <div id="cfg-Ext.BoxComponent-autoHeight"></div>/**
99 * @cfg {Boolean} autoHeight
100 * <p>True to use height:'auto', false to use fixed height (or allow it to be managed by its parent
101 * Container's {@link Ext.Container#layout layout manager}. Defaults to false.</p>
102 * <p><b>Note</b>: Although many components inherit this config option, not all will
103 * function as expected with a height of 'auto'. Setting autoHeight:true means that the
104 * browser will manage height based on the element's contents, and that Ext will not manage it at all.</p>
105 * <p>If the <i>browser</i> is managing the height, be aware that resizes performed by the browser in response
106 * to changes within the structure of the Component cannot be detected. Therefore changes to the height might
107 * result in elements needing to be synchronized with the new height. Example:</p><pre><code>
108 var w = new Ext.Window({
113 title: 'Collapse Me',
118 beforecollapse: function() {
121 beforeexpand: function() {
124 collapse: function() {
135 <div id="cfg-Ext.BoxComponent-autoWidth"></div>/**
136 * @cfg {Boolean} autoWidth
137 * <p>True to use width:'auto', false to use fixed width (or allow it to be managed by its parent
138 * Container's {@link Ext.Container#layout layout manager}. Defaults to false.</p>
139 * <p><b>Note</b>: Although many components inherit this config option, not all will
140 * function as expected with a width of 'auto'. Setting autoWidth:true means that the
141 * browser will manage width based on the element's contents, and that Ext will not manage it at all.</p>
142 * <p>If the <i>browser</i> is managing the width, be aware that resizes performed by the browser in response
143 * to changes within the structure of the Component cannot be detected. Therefore changes to the width might
144 * result in elements needing to be synchronized with the new width. For example, where the target element is:</p><pre><code>
145 <div id='grid-container' style='margin-left:25%;width:50%'></div>
147 * A Panel rendered into that target element must listen for browser window resize in order to relay its
148 * child items when the browser changes its width:<pre><code>
149 var myPanel = new Ext.Panel({
150 renderTo: 'grid-container',
151 monitorResize: true, // relay on browser resize
173 /* // private internal config
174 * {Boolean} deferHeight
175 * True to defer height calculations to an external component, false to allow this component to set its own
176 * height (defaults to false).
180 initComponent : function(){
181 Ext.BoxComponent.superclass.initComponent.call(this);
183 <div id="event-Ext.BoxComponent-resize"></div>/**
185 * Fires after the component is resized.
186 * @param {Ext.Component} this
187 * @param {Number} adjWidth The box-adjusted width that was set
188 * @param {Number} adjHeight The box-adjusted height that was set
189 * @param {Number} rawWidth The width that was originally specified
190 * @param {Number} rawHeight The height that was originally specified
193 <div id="event-Ext.BoxComponent-move"></div>/**
195 * Fires after the component is moved.
196 * @param {Ext.Component} this
197 * @param {Number} x The new x position
198 * @param {Number} y The new y position
204 // private, set in afterRender to signify that the component has been rendered
206 // private, used to defer height settings to subclasses
209 <div id="method-Ext.BoxComponent-setSize"></div>/**
210 * Sets the width and height of this BoxComponent. This method fires the {@link #resize} event. This method can accept
211 * either width and height as separate arguments, or you can pass a size object like <code>{width:10, height:20}</code>.
212 * @param {Mixed} width The new width to set. This may be one of:<div class="mdetail-params"><ul>
213 * <li>A Number specifying the new width in the {@link #getEl Element}'s {@link Ext.Element#defaultUnit}s (by default, pixels).</li>
214 * <li>A String used to set the CSS width style.</li>
215 * <li>A size object in the format <code>{width: widthValue, height: heightValue}</code>.</li>
216 * <li><code>undefined</code> to leave the width unchanged.</li>
218 * @param {Mixed} height The new height to set (not required if a size object is passed as the first arg).
219 * This may be one of:<div class="mdetail-params"><ul>
220 * <li>A Number specifying the new height in the {@link #getEl Element}'s {@link Ext.Element#defaultUnit}s (by default, pixels).</li>
221 * <li>A String used to set the CSS height style. Animation may <b>not</b> be used.</li>
222 * <li><code>undefined</code> to leave the height unchanged.</li>
224 * @return {Ext.BoxComponent} this
226 setSize : function(w, h){
227 // support for standard size objects
228 if(typeof w == 'object'){
239 // prevent recalcs when not needed
240 if(this.cacheSizes !== false && this.lastSize && this.lastSize.width == w && this.lastSize.height == h){
243 this.lastSize = {width: w, height: h};
244 var adj = this.adjustSize(w, h);
245 var aw = adj.width, ah = adj.height;
246 if(aw !== undefined || ah !== undefined){ // this code is nasty but performs better with floaters
247 var rz = this.getResizeEl();
248 if(!this.deferHeight && aw !== undefined && ah !== undefined){
250 }else if(!this.deferHeight && ah !== undefined){
252 }else if(aw !== undefined){
255 this.onResize(aw, ah, w, h);
256 this.fireEvent('resize', this, aw, ah, w, h);
261 <div id="method-Ext.BoxComponent-setWidth"></div>/**
262 * Sets the width of the component. This method fires the {@link #resize} event.
263 * @param {Number} width The new width to setThis may be one of:<div class="mdetail-params"><ul>
264 * <li>A Number specifying the new width in the {@link #getEl Element}'s {@link Ext.Element#defaultUnit}s (by default, pixels).</li>
265 * <li>A String used to set the CSS width style.</li>
267 * @return {Ext.BoxComponent} this
269 setWidth : function(width){
270 return this.setSize(width);
273 <div id="method-Ext.BoxComponent-setHeight"></div>/**
274 * Sets the height of the component. This method fires the {@link #resize} event.
275 * @param {Number} height The new height to set. This may be one of:<div class="mdetail-params"><ul>
276 * <li>A Number specifying the new height in the {@link #getEl Element}'s {@link Ext.Element#defaultUnit}s (by default, pixels).</li>
277 * <li>A String used to set the CSS height style.</li>
278 * <li><i>undefined</i> to leave the height unchanged.</li>
280 * @return {Ext.BoxComponent} this
282 setHeight : function(height){
283 return this.setSize(undefined, height);
286 <div id="method-Ext.BoxComponent-getSize"></div>/**
287 * Gets the current size of the component's underlying element.
288 * @return {Object} An object containing the element's size {width: (element width), height: (element height)}
290 getSize : function(){
291 return this.getResizeEl().getSize();
294 <div id="method-Ext.BoxComponent-getWidth"></div>/**
295 * Gets the current width of the component's underlying element.
298 getWidth : function(){
299 return this.getResizeEl().getWidth();
302 <div id="method-Ext.BoxComponent-getHeight"></div>/**
303 * Gets the current height of the component's underlying element.
306 getHeight : function(){
307 return this.getResizeEl().getHeight();
310 <div id="method-Ext.BoxComponent-getOuterSize"></div>/**
311 * Gets the current size of the component's underlying element, including space taken by its margins.
312 * @return {Object} An object containing the element's size {width: (element width + left/right margins), height: (element height + top/bottom margins)}
314 getOuterSize : function(){
315 var el = this.getResizeEl();
316 return {width: el.getWidth() + el.getMargins('lr'),
317 height: el.getHeight() + el.getMargins('tb')};
320 <div id="method-Ext.BoxComponent-getPosition"></div>/**
321 * Gets the current XY position of the component's underlying element.
322 * @param {Boolean} local (optional) If true the element's left and top are returned instead of page XY (defaults to false)
323 * @return {Array} The XY position of the element (e.g., [100, 200])
325 getPosition : function(local){
326 var el = this.getPositionEl();
328 return [el.getLeft(true), el.getTop(true)];
330 return this.xy || el.getXY();
333 <div id="method-Ext.BoxComponent-getBox"></div>/**
334 * Gets the current box measurements of the component's underlying element.
335 * @param {Boolean} local (optional) If true the element's left and top are returned instead of page XY (defaults to false)
336 * @return {Object} box An object in the format {x, y, width, height}
338 getBox : function(local){
339 var pos = this.getPosition(local);
340 var s = this.getSize();
346 <div id="method-Ext.BoxComponent-updateBox"></div>/**
347 * Sets the current box measurements of the component's underlying element.
348 * @param {Object} box An object in the format {x, y, width, height}
349 * @return {Ext.BoxComponent} this
351 updateBox : function(box){
352 this.setSize(box.width, box.height);
353 this.setPagePosition(box.x, box.y);
357 <div id="method-Ext.BoxComponent-getResizeEl"></div>/**
358 * <p>Returns the outermost Element of this Component which defines the Components overall size.</p>
359 * <p><i>Usually</i> this will return the same Element as <code>{@link #getEl}</code>,
360 * but in some cases, a Component may have some more wrapping Elements around its main
361 * active Element.</p>
362 * <p>An example is a ComboBox. It is encased in a <i>wrapping</i> Element which
363 * contains both the <code><input></code> Element (which is what would be returned
364 * by its <code>{@link #getEl}</code> method, <i>and</i> the trigger button Element.
365 * This Element is returned as the <code>resizeEl</code>.
367 getResizeEl : function(){
368 return this.resizeEl || this.el;
372 getPositionEl : function(){
373 return this.positionEl || this.el;
376 <div id="method-Ext.BoxComponent-setPosition"></div>/**
377 * Sets the left and top of the component. To set the page XY position instead, use {@link #setPagePosition}.
378 * This method fires the {@link #move} event.
379 * @param {Number} left The new left
380 * @param {Number} top The new top
381 * @return {Ext.BoxComponent} this
383 setPosition : function(x, y){
384 if(x && typeof x[1] == 'number'){
393 var adj = this.adjustPosition(x, y);
394 var ax = adj.x, ay = adj.y;
396 var el = this.getPositionEl();
397 if(ax !== undefined || ay !== undefined){
398 if(ax !== undefined && ay !== undefined){
399 el.setLeftTop(ax, ay);
400 }else if(ax !== undefined){
402 }else if(ay !== undefined){
405 this.onPosition(ax, ay);
406 this.fireEvent('move', this, ax, ay);
411 <div id="method-Ext.BoxComponent-setPagePosition"></div>/**
412 * Sets the page XY position of the component. To set the left and top instead, use {@link #setPosition}.
413 * This method fires the {@link #move} event.
414 * @param {Number} x The new x position
415 * @param {Number} y The new y position
416 * @return {Ext.BoxComponent} this
418 setPagePosition : function(x, y){
419 if(x && typeof x[1] == 'number'){
428 if(x === undefined || y === undefined){ // cannot translate undefined points
431 var p = this.getPositionEl().translatePoints(x, y);
432 this.setPosition(p.left, p.top);
437 onRender : function(ct, position){
438 Ext.BoxComponent.superclass.onRender.call(this, ct, position);
440 this.resizeEl = Ext.get(this.resizeEl);
443 this.positionEl = Ext.get(this.positionEl);
448 afterRender : function(){
449 Ext.BoxComponent.superclass.afterRender.call(this);
450 this.boxReady = true;
451 this.setSize(this.width, this.height);
452 if(this.x || this.y){
453 this.setPosition(this.x, this.y);
454 }else if(this.pageX || this.pageY){
455 this.setPagePosition(this.pageX, this.pageY);
459 <div id="method-Ext.BoxComponent-syncSize"></div>/**
460 * Force the component's size to recalculate based on the underlying element's current height and width.
461 * @return {Ext.BoxComponent} this
463 syncSize : function(){
464 delete this.lastSize;
465 this.setSize(this.autoWidth ? undefined : this.getResizeEl().getWidth(), this.autoHeight ? undefined : this.getResizeEl().getHeight());
470 * Called after the component is resized, this method is empty by default but can be implemented by any
471 * subclass that needs to perform custom logic after a resize occurs.
472 * @param {Number} adjWidth The box-adjusted width that was set
473 * @param {Number} adjHeight The box-adjusted height that was set
474 * @param {Number} rawWidth The width that was originally specified
475 * @param {Number} rawHeight The height that was originally specified
477 onResize : function(adjWidth, adjHeight, rawWidth, rawHeight){
482 * Called after the component is moved, this method is empty by default but can be implemented by any
483 * subclass that needs to perform custom logic after a move occurs.
484 * @param {Number} x The new x position
485 * @param {Number} y The new y position
487 onPosition : function(x, y){
492 adjustSize : function(w, h){
499 return {width : w, height: h};
503 adjustPosition : function(x, y){
504 return {x : x, y: y};
507 Ext.reg('box', Ext.BoxComponent);
510 <div id="cls-Ext.Spacer"></div>/**
512 * @extends Ext.BoxComponent
513 * <p>Used to provide a sizable space in a layout.</p>
515 * @param {Object} config
517 Ext.Spacer = Ext.extend(Ext.BoxComponent, {
520 Ext.reg('spacer', Ext.Spacer);</pre>
\r