[619] | 1 | /* |
---|
| 2 | * Ext JS Library 2.2.1 |
---|
| 3 | * Copyright(c) 2006-2009, Ext JS, LLC. |
---|
| 4 | * licensing@extjs.com |
---|
| 5 | * |
---|
| 6 | * http://extjs.com/license |
---|
| 7 | */ |
---|
| 8 | |
---|
| 9 | /** |
---|
| 10 | * @class Ext.BoxComponent |
---|
| 11 | * @extends Ext.Component |
---|
| 12 | * <p>Base class for any visual {@link Ext.Component} that uses a box container. BoxComponent provides automatic box |
---|
| 13 | * model adjustments for sizing and positioning and will work correctly withnin the Component rendering model. All |
---|
| 14 | * container classes should subclass BoxComponent so that they will work consistently when nested within other Ext |
---|
| 15 | * layout containers.</p> |
---|
| 16 | * <p>A BoxComponent may be created as a custom Component which encapsulates any HTML element, either a pre-existing |
---|
| 17 | * element, or one that is created to your specifications at render time. Usually, to participate in layouts, |
---|
| 18 | * a Component will need to be a <b>Box</b>Component in order to have its width and height managed.</p> |
---|
| 19 | * <p>To use a pre-existing element as a BoxComponent, configure it so that you preset the <b>el</b> property to the |
---|
| 20 | * element to reference:<pre><code> |
---|
| 21 | var pageHeader = new Ext.BoxComponent({ |
---|
| 22 | el: 'my-header-div' |
---|
| 23 | });</code></pre> |
---|
| 24 | * This may then be {@link Ext.Container#add added} to a {@link Ext.Container Container} as a child item.</p> |
---|
| 25 | * <p>To create a BoxComponent based around a HTML element to be created at render time, use the |
---|
| 26 | * {@link Ext.Component#autoEl autoEl} config option which takes the form of a |
---|
| 27 | * {@link Ext.DomHelper DomHelper} specification:<pre><code> |
---|
| 28 | var myImage = new Ext.BoxComponent({ |
---|
| 29 | autoEl: { |
---|
| 30 | tag: 'img', |
---|
| 31 | src: '/images/my-image.jpg' |
---|
| 32 | } |
---|
| 33 | });</code></pre></p> |
---|
| 34 | * @constructor |
---|
| 35 | * @param {Ext.Element/String/Object} config The configuration options. |
---|
| 36 | */ |
---|
| 37 | Ext.BoxComponent = Ext.extend(Ext.Component, { |
---|
| 38 | /** |
---|
| 39 | * @cfg {Number} x |
---|
| 40 | * The local x (left) coordinate for this component if contained within a positioning container. |
---|
| 41 | */ |
---|
| 42 | /** |
---|
| 43 | * @cfg {Number} y |
---|
| 44 | * The local y (top) coordinate for this component if contained within a positioning container. |
---|
| 45 | */ |
---|
| 46 | /** |
---|
| 47 | * @cfg {Number} pageX |
---|
| 48 | * The page level x coordinate for this component if contained within a positioning container. |
---|
| 49 | */ |
---|
| 50 | /** |
---|
| 51 | * @cfg {Number} pageY |
---|
| 52 | * The page level y coordinate for this component if contained within a positioning container. |
---|
| 53 | */ |
---|
| 54 | /** |
---|
| 55 | * @cfg {Number} height |
---|
| 56 | * The height of this component in pixels (defaults to auto). |
---|
| 57 | */ |
---|
| 58 | /** |
---|
| 59 | * @cfg {Number} width |
---|
| 60 | * The width of this component in pixels (defaults to auto). |
---|
| 61 | */ |
---|
| 62 | /** |
---|
| 63 | * @cfg {Boolean} autoHeight |
---|
| 64 | * True to use height:'auto', false to use fixed height (defaults to false). <b>Note</b>: Although many components |
---|
| 65 | * inherit this config option, not all will function as expected with a height of 'auto'. Setting autoHeight:true |
---|
| 66 | * means that the browser will manage height based on the element's contents, and that Ext will not manage it at all. |
---|
| 67 | */ |
---|
| 68 | /** |
---|
| 69 | * @cfg {Boolean} autoWidth |
---|
| 70 | * True to use width:'auto', false to use fixed width (defaults to false). <b>Note</b>: Although many components |
---|
| 71 | * inherit this config option, not all will function as expected with a width of 'auto'. Setting autoWidth:true |
---|
| 72 | * means that the browser will manage width based on the element's contents, and that Ext will not manage it at all. |
---|
| 73 | */ |
---|
| 74 | |
---|
| 75 | /* // private internal config |
---|
| 76 | * {Boolean} deferHeight |
---|
| 77 | * True to defer height calculations to an external component, false to allow this component to set its own |
---|
| 78 | * height (defaults to false). |
---|
| 79 | */ |
---|
| 80 | |
---|
| 81 | // private |
---|
| 82 | initComponent : function(){ |
---|
| 83 | Ext.BoxComponent.superclass.initComponent.call(this); |
---|
| 84 | this.addEvents( |
---|
| 85 | /** |
---|
| 86 | * @event resize |
---|
| 87 | * Fires after the component is resized. |
---|
| 88 | * @param {Ext.Component} this |
---|
| 89 | * @param {Number} adjWidth The box-adjusted width that was set |
---|
| 90 | * @param {Number} adjHeight The box-adjusted height that was set |
---|
| 91 | * @param {Number} rawWidth The width that was originally specified |
---|
| 92 | * @param {Number} rawHeight The height that was originally specified |
---|
| 93 | */ |
---|
| 94 | 'resize', |
---|
| 95 | /** |
---|
| 96 | * @event move |
---|
| 97 | * Fires after the component is moved. |
---|
| 98 | * @param {Ext.Component} this |
---|
| 99 | * @param {Number} x The new x position |
---|
| 100 | * @param {Number} y The new y position |
---|
| 101 | */ |
---|
| 102 | 'move' |
---|
| 103 | ); |
---|
| 104 | }, |
---|
| 105 | |
---|
| 106 | // private, set in afterRender to signify that the component has been rendered |
---|
| 107 | boxReady : false, |
---|
| 108 | // private, used to defer height settings to subclasses |
---|
| 109 | deferHeight: false, |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | * Sets the width and height of the component. This method fires the {@link #resize} event. This method can accept |
---|
| 113 | * either width and height as separate numeric arguments, or you can pass a size object like {width:10, height:20}. |
---|
| 114 | * @param {Number/Object} width The new width to set, or a size object in the format {width, height} |
---|
| 115 | * @param {Number} height The new height to set (not required if a size object is passed as the first arg) |
---|
| 116 | * @return {Ext.BoxComponent} this |
---|
| 117 | */ |
---|
| 118 | setSize : function(w, h){ |
---|
| 119 | // support for standard size objects |
---|
| 120 | if(typeof w == 'object'){ |
---|
| 121 | h = w.height; |
---|
| 122 | w = w.width; |
---|
| 123 | } |
---|
| 124 | // not rendered |
---|
| 125 | if(!this.boxReady){ |
---|
| 126 | this.width = w; |
---|
| 127 | this.height = h; |
---|
| 128 | return this; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | // prevent recalcs when not needed |
---|
| 132 | if(this.lastSize && this.lastSize.width == w && this.lastSize.height == h){ |
---|
| 133 | return this; |
---|
| 134 | } |
---|
| 135 | this.lastSize = {width: w, height: h}; |
---|
| 136 | var adj = this.adjustSize(w, h); |
---|
| 137 | var aw = adj.width, ah = adj.height; |
---|
| 138 | if(aw !== undefined || ah !== undefined){ // this code is nasty but performs better with floaters |
---|
| 139 | var rz = this.getResizeEl(); |
---|
| 140 | if(!this.deferHeight && aw !== undefined && ah !== undefined){ |
---|
| 141 | rz.setSize(aw, ah); |
---|
| 142 | }else if(!this.deferHeight && ah !== undefined){ |
---|
| 143 | rz.setHeight(ah); |
---|
| 144 | }else if(aw !== undefined){ |
---|
| 145 | rz.setWidth(aw); |
---|
| 146 | } |
---|
| 147 | this.onResize(aw, ah, w, h); |
---|
| 148 | this.fireEvent('resize', this, aw, ah, w, h); |
---|
| 149 | } |
---|
| 150 | return this; |
---|
| 151 | }, |
---|
| 152 | |
---|
| 153 | /** |
---|
| 154 | * Sets the width of the component. This method fires the {@link #resize} event. |
---|
| 155 | * @param {Number} width The new width to set |
---|
| 156 | * @return {Ext.BoxComponent} this |
---|
| 157 | */ |
---|
| 158 | setWidth : function(width){ |
---|
| 159 | return this.setSize(width); |
---|
| 160 | }, |
---|
| 161 | |
---|
| 162 | /** |
---|
| 163 | * Sets the height of the component. This method fires the {@link #resize} event. |
---|
| 164 | * @param {Number} height The new height to set |
---|
| 165 | * @return {Ext.BoxComponent} this |
---|
| 166 | */ |
---|
| 167 | setHeight : function(height){ |
---|
| 168 | return this.setSize(undefined, height); |
---|
| 169 | }, |
---|
| 170 | |
---|
| 171 | /** |
---|
| 172 | * Gets the current size of the component's underlying element. |
---|
| 173 | * @return {Object} An object containing the element's size {width: (element width), height: (element height)} |
---|
| 174 | */ |
---|
| 175 | getSize : function(){ |
---|
| 176 | return this.el.getSize(); |
---|
| 177 | }, |
---|
| 178 | |
---|
| 179 | /** |
---|
| 180 | * Gets the current XY position of the component's underlying element. |
---|
| 181 | * @param {Boolean} local (optional) If true the element's left and top are returned instead of page XY (defaults to false) |
---|
| 182 | * @return {Array} The XY position of the element (e.g., [100, 200]) |
---|
| 183 | */ |
---|
| 184 | getPosition : function(local){ |
---|
| 185 | if(local === true){ |
---|
| 186 | return [this.el.getLeft(true), this.el.getTop(true)]; |
---|
| 187 | } |
---|
| 188 | return this.xy || this.el.getXY(); |
---|
| 189 | }, |
---|
| 190 | |
---|
| 191 | /** |
---|
| 192 | * Gets the current box measurements of the component's underlying element. |
---|
| 193 | * @param {Boolean} local (optional) If true the element's left and top are returned instead of page XY (defaults to false) |
---|
| 194 | * @return {Object} box An object in the format {x, y, width, height} |
---|
| 195 | */ |
---|
| 196 | getBox : function(local){ |
---|
| 197 | var s = this.el.getSize(); |
---|
| 198 | if(local === true){ |
---|
| 199 | s.x = this.el.getLeft(true); |
---|
| 200 | s.y = this.el.getTop(true); |
---|
| 201 | }else{ |
---|
| 202 | var xy = this.xy || this.el.getXY(); |
---|
| 203 | s.x = xy[0]; |
---|
| 204 | s.y = xy[1]; |
---|
| 205 | } |
---|
| 206 | return s; |
---|
| 207 | }, |
---|
| 208 | |
---|
| 209 | /** |
---|
| 210 | * Sets the current box measurements of the component's underlying element. |
---|
| 211 | * @param {Object} box An object in the format {x, y, width, height} |
---|
| 212 | * @return {Ext.BoxComponent} this |
---|
| 213 | */ |
---|
| 214 | updateBox : function(box){ |
---|
| 215 | this.setSize(box.width, box.height); |
---|
| 216 | this.setPagePosition(box.x, box.y); |
---|
| 217 | return this; |
---|
| 218 | }, |
---|
| 219 | |
---|
| 220 | // protected |
---|
| 221 | getResizeEl : function(){ |
---|
| 222 | return this.resizeEl || this.el; |
---|
| 223 | }, |
---|
| 224 | |
---|
| 225 | // protected |
---|
| 226 | getPositionEl : function(){ |
---|
| 227 | return this.positionEl || this.el; |
---|
| 228 | }, |
---|
| 229 | |
---|
| 230 | /** |
---|
| 231 | * Sets the left and top of the component. To set the page XY position instead, use {@link #setPagePosition}. |
---|
| 232 | * This method fires the {@link #move} event. |
---|
| 233 | * @param {Number} left The new left |
---|
| 234 | * @param {Number} top The new top |
---|
| 235 | * @return {Ext.BoxComponent} this |
---|
| 236 | */ |
---|
| 237 | setPosition : function(x, y){ |
---|
| 238 | if(x && typeof x[1] == 'number'){ |
---|
| 239 | y = x[1]; |
---|
| 240 | x = x[0]; |
---|
| 241 | } |
---|
| 242 | this.x = x; |
---|
| 243 | this.y = y; |
---|
| 244 | if(!this.boxReady){ |
---|
| 245 | return this; |
---|
| 246 | } |
---|
| 247 | var adj = this.adjustPosition(x, y); |
---|
| 248 | var ax = adj.x, ay = adj.y; |
---|
| 249 | |
---|
| 250 | var el = this.getPositionEl(); |
---|
| 251 | if(ax !== undefined || ay !== undefined){ |
---|
| 252 | if(ax !== undefined && ay !== undefined){ |
---|
| 253 | el.setLeftTop(ax, ay); |
---|
| 254 | }else if(ax !== undefined){ |
---|
| 255 | el.setLeft(ax); |
---|
| 256 | }else if(ay !== undefined){ |
---|
| 257 | el.setTop(ay); |
---|
| 258 | } |
---|
| 259 | this.onPosition(ax, ay); |
---|
| 260 | this.fireEvent('move', this, ax, ay); |
---|
| 261 | } |
---|
| 262 | return this; |
---|
| 263 | }, |
---|
| 264 | |
---|
| 265 | /** |
---|
| 266 | * Sets the page XY position of the component. To set the left and top instead, use {@link #setPosition}. |
---|
| 267 | * This method fires the {@link #move} event. |
---|
| 268 | * @param {Number} x The new x position |
---|
| 269 | * @param {Number} y The new y position |
---|
| 270 | * @return {Ext.BoxComponent} this |
---|
| 271 | */ |
---|
| 272 | setPagePosition : function(x, y){ |
---|
| 273 | if(x && typeof x[1] == 'number'){ |
---|
| 274 | y = x[1]; |
---|
| 275 | x = x[0]; |
---|
| 276 | } |
---|
| 277 | this.pageX = x; |
---|
| 278 | this.pageY = y; |
---|
| 279 | if(!this.boxReady){ |
---|
| 280 | return; |
---|
| 281 | } |
---|
| 282 | if(x === undefined || y === undefined){ // cannot translate undefined points |
---|
| 283 | return; |
---|
| 284 | } |
---|
| 285 | var p = this.el.translatePoints(x, y); |
---|
| 286 | this.setPosition(p.left, p.top); |
---|
| 287 | return this; |
---|
| 288 | }, |
---|
| 289 | |
---|
| 290 | // private |
---|
| 291 | onRender : function(ct, position){ |
---|
| 292 | Ext.BoxComponent.superclass.onRender.call(this, ct, position); |
---|
| 293 | if(this.resizeEl){ |
---|
| 294 | this.resizeEl = Ext.get(this.resizeEl); |
---|
| 295 | } |
---|
| 296 | if(this.positionEl){ |
---|
| 297 | this.positionEl = Ext.get(this.positionEl); |
---|
| 298 | } |
---|
| 299 | }, |
---|
| 300 | |
---|
| 301 | // private |
---|
| 302 | afterRender : function(){ |
---|
| 303 | Ext.BoxComponent.superclass.afterRender.call(this); |
---|
| 304 | this.boxReady = true; |
---|
| 305 | this.setSize(this.width, this.height); |
---|
| 306 | if(this.x || this.y){ |
---|
| 307 | this.setPosition(this.x, this.y); |
---|
| 308 | }else if(this.pageX || this.pageY){ |
---|
| 309 | this.setPagePosition(this.pageX, this.pageY); |
---|
| 310 | } |
---|
| 311 | }, |
---|
| 312 | |
---|
| 313 | /** |
---|
| 314 | * Force the component's size to recalculate based on the underlying element's current height and width. |
---|
| 315 | * @return {Ext.BoxComponent} this |
---|
| 316 | */ |
---|
| 317 | syncSize : function(){ |
---|
| 318 | delete this.lastSize; |
---|
| 319 | this.setSize(this.autoWidth ? undefined : this.el.getWidth(), this.autoHeight ? undefined : this.el.getHeight()); |
---|
| 320 | return this; |
---|
| 321 | }, |
---|
| 322 | |
---|
| 323 | /* // protected |
---|
| 324 | * Called after the component is resized, this method is empty by default but can be implemented by any |
---|
| 325 | * subclass that needs to perform custom logic after a resize occurs. |
---|
| 326 | * @param {Number} adjWidth The box-adjusted width that was set |
---|
| 327 | * @param {Number} adjHeight The box-adjusted height that was set |
---|
| 328 | * @param {Number} rawWidth The width that was originally specified |
---|
| 329 | * @param {Number} rawHeight The height that was originally specified |
---|
| 330 | */ |
---|
| 331 | onResize : function(adjWidth, adjHeight, rawWidth, rawHeight){ |
---|
| 332 | |
---|
| 333 | }, |
---|
| 334 | |
---|
| 335 | /* // protected |
---|
| 336 | * Called after the component is moved, this method is empty by default but can be implemented by any |
---|
| 337 | * subclass that needs to perform custom logic after a move occurs. |
---|
| 338 | * @param {Number} x The new x position |
---|
| 339 | * @param {Number} y The new y position |
---|
| 340 | */ |
---|
| 341 | onPosition : function(x, y){ |
---|
| 342 | |
---|
| 343 | }, |
---|
| 344 | |
---|
| 345 | // private |
---|
| 346 | adjustSize : function(w, h){ |
---|
| 347 | if(this.autoWidth){ |
---|
| 348 | w = 'auto'; |
---|
| 349 | } |
---|
| 350 | if(this.autoHeight){ |
---|
| 351 | h = 'auto'; |
---|
| 352 | } |
---|
| 353 | return {width : w, height: h}; |
---|
| 354 | }, |
---|
| 355 | |
---|
| 356 | // private |
---|
| 357 | adjustPosition : function(x, y){ |
---|
| 358 | return {x : x, y: y}; |
---|
| 359 | } |
---|
| 360 | }); |
---|
| 361 | Ext.reg('box', Ext.BoxComponent); |
---|