PageRenderTime 57ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/2.3.0/src/widgets/BoxComponent.js

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