Upgrade to ExtJS 3.1.0 - Released 12/16/2009
[extjs.git] / docs / source / Element.position-more.html
1 <html>\r
2 <head>\r
3   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    \r
4   <title>The source code</title>\r
5     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />\r
6     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>\r
7 </head>\r
8 <body  onload="prettyPrint();">\r
9     <pre class="prettyprint lang-js">/**\r
10  * @class Ext.Element\r
11  */\r
12 Ext.Element.addMethods({\r
13     <div id="method-Ext.Element-setBox"></div>/**\r
14      * Sets the element's box. Use getBox() on another element to get a box obj. If animate is true then width, height, x and y will be animated concurrently.\r
15      * @param {Object} box The box to fill {x, y, width, height}\r
16      * @param {Boolean} adjust (optional) Whether to adjust for box-model issues automatically\r
17      * @param {Boolean/Object} animate (optional) true for the default animation or a standard Element animation config object\r
18      * @return {Ext.Element} this\r
19      */\r
20     setBox : function(box, adjust, animate){\r
21         var me = this,\r
22                 w = box.width, \r
23                 h = box.height;\r
24         if((adjust && !me.autoBoxAdjust) && !me.isBorderBox()){\r
25            w -= (me.getBorderWidth("lr") + me.getPadding("lr"));\r
26            h -= (me.getBorderWidth("tb") + me.getPadding("tb"));\r
27         }\r
28         me.setBounds(box.x, box.y, w, h, me.animTest.call(me, arguments, animate, 2));\r
29         return me;\r
30     },\r
31 \r
32     <div id="method-Ext.Element-getBox"></div>/**\r
33      * Return an object defining the area of this Element which can be passed to {@link #setBox} to\r
34      * set another Element's size/location to match this element.\r
35      * @param {Boolean} contentBox (optional) If true a box for the content of the element is returned.\r
36      * @param {Boolean} local (optional) If true the element's left and top are returned instead of page x/y.\r
37      * @return {Object} box An object in the format<pre><code>\r
38 {\r
39     x: &lt;Element's X position>,\r
40     y: &lt;Element's Y position>,\r
41     width: &lt;Element's width>,\r
42     height: &lt;Element's height>,\r
43     bottom: &lt;Element's lower bound>,\r
44     right: &lt;Element's rightmost bound>\r
45 }\r
46 </code></pre>\r
47      * The returned object may also be addressed as an Array where index 0 contains the X position\r
48      * and index 1 contains the Y position. So the result may also be used for {@link #setXY}\r
49      */\r
50         getBox : function(contentBox, local) {      \r
51             var me = this,\r
52                 xy,\r
53                 left,\r
54                 top,\r
55                 getBorderWidth = me.getBorderWidth,\r
56                 getPadding = me.getPadding, \r
57                 l,\r
58                 r,\r
59                 t,\r
60                 b;\r
61         if(!local){\r
62             xy = me.getXY();\r
63         }else{\r
64             left = parseInt(me.getStyle("left"), 10) || 0;\r
65             top = parseInt(me.getStyle("top"), 10) || 0;\r
66             xy = [left, top];\r
67         }\r
68         var el = me.dom, w = el.offsetWidth, h = el.offsetHeight, bx;\r
69         if(!contentBox){\r
70             bx = {x: xy[0], y: xy[1], 0: xy[0], 1: xy[1], width: w, height: h};\r
71         }else{\r
72             l = getBorderWidth.call(me, "l") + getPadding.call(me, "l");\r
73             r = getBorderWidth.call(me, "r") + getPadding.call(me, "r");\r
74             t = getBorderWidth.call(me, "t") + getPadding.call(me, "t");\r
75             b = getBorderWidth.call(me, "b") + getPadding.call(me, "b");\r
76             bx = {x: xy[0]+l, y: xy[1]+t, 0: xy[0]+l, 1: xy[1]+t, width: w-(l+r), height: h-(t+b)};\r
77         }\r
78         bx.right = bx.x + bx.width;\r
79         bx.bottom = bx.y + bx.height;\r
80         return bx;\r
81         },\r
82         \r
83     <div id="method-Ext.Element-move"></div>/**\r
84      * Move this element relative to its current position.\r
85      * @param {String} direction Possible values are: "l" (or "left"), "r" (or "right"), "t" (or "top", or "up"), "b" (or "bottom", or "down").\r
86      * @param {Number} distance How far to move the element in pixels\r
87      * @param {Boolean/Object} animate (optional) true for the default animation or a standard Element animation config object\r
88      * @return {Ext.Element} this\r
89      */\r
90      move : function(direction, distance, animate){\r
91         var me = this,          \r
92                 xy = me.getXY(),\r
93                 x = xy[0],\r
94                 y = xy[1],              \r
95                 left = [x - distance, y],\r
96                 right = [x + distance, y],\r
97                 top = [x, y - distance],\r
98                 bottom = [x, y + distance],\r
99                 hash = {\r
100                         l :     left,\r
101                         left : left,\r
102                         r : right,\r
103                         right : right,\r
104                         t : top,\r
105                         top : top,\r
106                         up : top,\r
107                         b : bottom, \r
108                         bottom : bottom,\r
109                         down : bottom                           \r
110                 };\r
111         \r
112             direction = direction.toLowerCase();    \r
113             me.moveTo(hash[direction][0], hash[direction][1], me.animTest.call(me, arguments, animate, 2));\r
114     },\r
115     \r
116     <div id="method-Ext.Element-setLeftTop"></div>/**\r
117      * Quick set left and top adding default units\r
118      * @param {String} left The left CSS property value\r
119      * @param {String} top The top CSS property value\r
120      * @return {Ext.Element} this\r
121      */\r
122      setLeftTop : function(left, top){\r
123             var me = this,\r
124                 style = me.dom.style;\r
125         style.left = me.addUnits(left);\r
126         style.top = me.addUnits(top);\r
127         return me;\r
128     },\r
129     \r
130     <div id="method-Ext.Element-getRegion"></div>/**\r
131      * Returns the region of the given element.\r
132      * The element must be part of the DOM tree to have a region (display:none or elements not appended return false).\r
133      * @return {Region} A Ext.lib.Region containing "top, left, bottom, right" member data.\r
134      */\r
135     getRegion : function(){\r
136         return Ext.lib.Dom.getRegion(this.dom);\r
137     },\r
138     \r
139     <div id="method-Ext.Element-setBounds"></div>/**\r
140      * Sets the element's position and size in one shot. If animation is true then width, height, x and y will be animated concurrently.\r
141      * @param {Number} x X value for new position (coordinates are page-based)\r
142      * @param {Number} y Y value for new position (coordinates are page-based)\r
143      * @param {Mixed} width The new width. This may be one of:<div class="mdetail-params"><ul>\r
144      * <li>A Number specifying the new width in this Element's {@link #defaultUnit}s (by default, pixels)</li>\r
145      * <li>A String used to set the CSS width style. Animation may <b>not</b> be used.\r
146      * </ul></div>\r
147      * @param {Mixed} height The new height. This may be one of:<div class="mdetail-params"><ul>\r
148      * <li>A Number specifying the new height in this Element's {@link #defaultUnit}s (by default, pixels)</li>\r
149      * <li>A String used to set the CSS height style. Animation may <b>not</b> be used.</li>\r
150      * </ul></div>\r
151      * @param {Boolean/Object} animate (optional) true for the default animation or a standard Element animation config object\r
152      * @return {Ext.Element} this\r
153      */\r
154     setBounds : function(x, y, width, height, animate){\r
155             var me = this;\r
156         if (!animate || !me.anim) {\r
157             me.setSize(width, height);\r
158             me.setLocation(x, y);\r
159         } else {\r
160             me.anim({points: {to: [x, y]}, \r
161                          width: {to: me.adjustWidth(width)}, \r
162                          height: {to: me.adjustHeight(height)}},\r
163                      me.preanim(arguments, 4), \r
164                      'motion');\r
165         }\r
166         return me;\r
167     },\r
168 \r
169     <div id="method-Ext.Element-setRegion"></div>/**\r
170      * Sets the element's position and size the specified region. If animation is true then width, height, x and y will be animated concurrently.\r
171      * @param {Ext.lib.Region} region The region to fill\r
172      * @param {Boolean/Object} animate (optional) true for the default animation or a standard Element animation config object\r
173      * @return {Ext.Element} this\r
174      */\r
175     setRegion : function(region, animate) {\r
176         return this.setBounds(region.left, region.top, region.right-region.left, region.bottom-region.top, this.animTest.call(this, arguments, animate, 1));\r
177     }\r
178 });</pre>    \r
179 </body>\r
180 </html>