Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / docs / source / Element.position.html
1 <html>\r
2 <head>\r
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
6 </head>\r
7 <body  onload="prettyPrint();">\r
8     <pre class="prettyprint lang-js">/**\r
9  * @class Ext.Element\r
10  */\r
11 (function(){\r
12 var D = Ext.lib.Dom,\r
13         LEFT = "left",\r
14         RIGHT = "right",\r
15         TOP = "top",\r
16         BOTTOM = "bottom",\r
17         POSITION = "position",\r
18         STATIC = "static",\r
19         RELATIVE = "relative",\r
20         AUTO = "auto",\r
21         ZINDEX = "z-index";\r
22 \r
23 function animTest(args, animate, i) {\r
24         return this.preanim && !!animate ? this.preanim(args, i) : false        \r
25 }\r
26 \r
27 Ext.Element.addMethods({\r
28         <div id="method-Ext.Element-getX"></div>/**\r
29       * Gets the current X position of the element based on page coordinates.  Element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).\r
30       * @return {Number} The X position of the element\r
31       */\r
32     getX : function(){\r
33         return D.getX(this.dom);\r
34     },\r
35 \r
36     <div id="method-Ext.Element-getY"></div>/**\r
37       * Gets the current Y position of the element based on page coordinates.  Element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).\r
38       * @return {Number} The Y position of the element\r
39       */\r
40     getY : function(){\r
41         return D.getY(this.dom);\r
42     },\r
43 \r
44     <div id="method-Ext.Element-getXY"></div>/**\r
45       * Gets the current position of the element based on page coordinates.  Element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).\r
46       * @return {Array} The XY position of the element\r
47       */\r
48     getXY : function(){\r
49         return D.getXY(this.dom);\r
50     },\r
51 \r
52     <div id="method-Ext.Element-getOffsetsTo"></div>/**\r
53       * Returns the offsets of this element from the passed element. Both element must be part of the DOM tree and not have display:none to have page coordinates.\r
54       * @param {Mixed} element The element to get the offsets from.\r
55       * @return {Array} The XY page offsets (e.g. [100, -200])\r
56       */\r
57     getOffsetsTo : function(el){\r
58         var o = this.getXY(),\r
59                 e = Ext.fly(el, '_internal').getXY();\r
60         return [o[0]-e[0],o[1]-e[1]];\r
61     },\r
62 \r
63     <div id="method-Ext.Element-setX"></div>/**\r
64      * Sets the X position of the element based on page coordinates.  Element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).\r
65      * @param {Number} The X position of the element\r
66      * @param {Boolean/Object} animate (optional) True for the default animation, or a standard Element animation config object\r
67      * @return {Ext.Element} this\r
68      */\r
69     setX : function(x, animate){            \r
70             return this.setXY([x, this.getY()], animTest.call(this, arguments, animate, 1));\r
71     },\r
72 \r
73     <div id="method-Ext.Element-setY"></div>/**\r
74      * Sets the Y position of the element based on page coordinates.  Element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).\r
75      * @param {Number} The Y position of the element\r
76      * @param {Boolean/Object} animate (optional) True for the default animation, or a standard Element animation config object\r
77      * @return {Ext.Element} this\r
78      */\r
79     setY : function(y, animate){            \r
80             return this.setXY([this.getX(), y], animTest.call(this, arguments, animate, 1));\r
81     },\r
82 \r
83     <div id="method-Ext.Element-setLeft"></div>/**\r
84      * Sets the element's left position directly using CSS style (instead of {@link #setX}).\r
85      * @param {String} left The left CSS property value\r
86      * @return {Ext.Element} this\r
87      */\r
88     setLeft : function(left){\r
89         this.setStyle(LEFT, this.addUnits(left));\r
90         return this;\r
91     },\r
92 \r
93     <div id="method-Ext.Element-setTop"></div>/**\r
94      * Sets the element's top position directly using CSS style (instead of {@link #setY}).\r
95      * @param {String} top The top CSS property value\r
96      * @return {Ext.Element} this\r
97      */\r
98     setTop : function(top){\r
99         this.setStyle(TOP, this.addUnits(top));\r
100         return this;\r
101     },\r
102 \r
103     <div id="method-Ext.Element-setRight"></div>/**\r
104      * Sets the element's CSS right style.\r
105      * @param {String} right The right CSS property value\r
106      * @return {Ext.Element} this\r
107      */\r
108     setRight : function(right){\r
109         this.setStyle(RIGHT, this.addUnits(right));\r
110         return this;\r
111     },\r
112 \r
113     <div id="method-Ext.Element-setBottom"></div>/**\r
114      * Sets the element's CSS bottom style.\r
115      * @param {String} bottom The bottom CSS property value\r
116      * @return {Ext.Element} this\r
117      */\r
118     setBottom : function(bottom){\r
119         this.setStyle(BOTTOM, this.addUnits(bottom));\r
120         return this;\r
121     },\r
122 \r
123     <div id="method-Ext.Element-setXY"></div>/**\r
124      * Sets the position of the element in page coordinates, regardless of how the element is positioned.\r
125      * The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).\r
126      * @param {Array} pos Contains X & Y [x, y] values for new position (coordinates are page-based)\r
127      * @param {Boolean/Object} animate (optional) True for the default animation, or a standard Element animation config object\r
128      * @return {Ext.Element} this\r
129      */\r
130     setXY : function(pos, animate){\r
131             var me = this;\r
132         if(!animate || !me.anim){\r
133             D.setXY(me.dom, pos);\r
134         }else{\r
135             me.anim({points: {to: pos}}, me.preanim(arguments, 1), 'motion');\r
136         }\r
137         return me;\r
138     },\r
139 \r
140     <div id="method-Ext.Element-setLocation"></div>/**\r
141      * Sets the position of the element in page coordinates, regardless of how the element is positioned.\r
142      * The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).\r
143      * @param {Number} x X value for new position (coordinates are page-based)\r
144      * @param {Number} y Y value for new position (coordinates are page-based)\r
145      * @param {Boolean/Object} animate (optional) True for the default animation, or a standard Element animation config object\r
146      * @return {Ext.Element} this\r
147      */\r
148     setLocation : function(x, y, animate){\r
149         return this.setXY([x, y], animTest.call(this, arguments, animate, 2));\r
150     },\r
151 \r
152     <div id="method-Ext.Element-moveTo"></div>/**\r
153      * Sets the position of the element in page coordinates, regardless of how the element is positioned.\r
154      * The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).\r
155      * @param {Number} x X value for new position (coordinates are page-based)\r
156      * @param {Number} y Y value for new position (coordinates are page-based)\r
157      * @param {Boolean/Object} animate (optional) True for the default animation, or a standard Element animation config object\r
158      * @return {Ext.Element} this\r
159      */\r
160     moveTo : function(x, y, animate){\r
161         return this.setXY([x, y], animTest.call(this, arguments, animate, 2));        \r
162     },    \r
163     \r
164     <div id="method-Ext.Element-getLeft"></div>/**\r
165      * Gets the left X coordinate\r
166      * @param {Boolean} local True to get the local css position instead of page coordinate\r
167      * @return {Number}\r
168      */\r
169     getLeft : function(local){\r
170             return !local ? this.getX() : parseInt(this.getStyle(LEFT), 10) || 0;\r
171     },\r
172 \r
173     <div id="method-Ext.Element-getRight"></div>/**\r
174      * Gets the right X coordinate of the element (element X position + element width)\r
175      * @param {Boolean} local True to get the local css position instead of page coordinate\r
176      * @return {Number}\r
177      */\r
178     getRight : function(local){\r
179             var me = this;\r
180             return !local ? me.getX() + me.getWidth() : (me.getLeft(true) + me.getWidth()) || 0;\r
181     },\r
182 \r
183     <div id="method-Ext.Element-getTop"></div>/**\r
184      * Gets the top Y coordinate\r
185      * @param {Boolean} local True to get the local css position instead of page coordinate\r
186      * @return {Number}\r
187      */\r
188     getTop : function(local) {\r
189             return !local ? this.getY() : parseInt(this.getStyle(TOP), 10) || 0;\r
190     },\r
191 \r
192     <div id="method-Ext.Element-getBottom"></div>/**\r
193      * Gets the bottom Y coordinate of the element (element Y position + element height)\r
194      * @param {Boolean} local True to get the local css position instead of page coordinate\r
195      * @return {Number}\r
196      */\r
197     getBottom : function(local){\r
198             var me = this;\r
199             return !local ? me.getY() + me.getHeight() : (me.getTop(true) + me.getHeight()) || 0;\r
200     },\r
201 \r
202     <div id="method-Ext.Element-position"></div>/**\r
203     * Initializes positioning on this element. If a desired position is not passed, it will make the\r
204     * the element positioned relative IF it is not already positioned.\r
205     * @param {String} pos (optional) Positioning to use "relative", "absolute" or "fixed"\r
206     * @param {Number} zIndex (optional) The zIndex to apply\r
207     * @param {Number} x (optional) Set the page X position\r
208     * @param {Number} y (optional) Set the page Y position\r
209     */\r
210     position : function(pos, zIndex, x, y){\r
211             var me = this;\r
212             \r
213         if(!pos && me.isStyle(POSITION, STATIC)){           \r
214             me.setStyle(POSITION, RELATIVE);           \r
215         } else if(pos) {\r
216             me.setStyle(POSITION, pos);\r
217         }\r
218         if(zIndex){\r
219             me.setStyle(ZINDEX, zIndex);\r
220         }\r
221         if(x || y) me.setXY([x || false, y || false]);\r
222     },\r
223 \r
224     <div id="method-Ext.Element-clearPositioning"></div>/**\r
225     * Clear positioning back to the default when the document was loaded\r
226     * @param {String} value (optional) The value to use for the left,right,top,bottom, defaults to '' (empty string). You could use 'auto'.\r
227     * @return {Ext.Element} this\r
228      */\r
229     clearPositioning : function(value){\r
230         value = value || '';\r
231         this.setStyle({\r
232             left : value,\r
233             right : value,\r
234             top : value,\r
235             bottom : value,\r
236             "z-index" : "",\r
237             position : STATIC\r
238         });\r
239         return this;\r
240     },\r
241 \r
242     <div id="method-Ext.Element-getPositioning"></div>/**\r
243     * Gets an object with all CSS positioning properties. Useful along with setPostioning to get\r
244     * snapshot before performing an update and then restoring the element.\r
245     * @return {Object}\r
246     */\r
247     getPositioning : function(){\r
248         var l = this.getStyle(LEFT);\r
249         var t = this.getStyle(TOP);\r
250         return {\r
251             "position" : this.getStyle(POSITION),\r
252             "left" : l,\r
253             "right" : l ? "" : this.getStyle(RIGHT),\r
254             "top" : t,\r
255             "bottom" : t ? "" : this.getStyle(BOTTOM),\r
256             "z-index" : this.getStyle(ZINDEX)\r
257         };\r
258     },\r
259     \r
260     <div id="method-Ext.Element-setPositioning"></div>/**\r
261     * Set positioning with an object returned by getPositioning().\r
262     * @param {Object} posCfg\r
263     * @return {Ext.Element} this\r
264      */\r
265     setPositioning : function(pc){\r
266             var me = this,\r
267                 style = me.dom.style;\r
268                 \r
269         me.setStyle(pc);\r
270         \r
271         if(pc.right == AUTO){\r
272             style.right = "";\r
273         }\r
274         if(pc.bottom == AUTO){\r
275             style.bottom = "";\r
276         }\r
277         \r
278         return me;\r
279     },    \r
280         \r
281     <div id="method-Ext.Element-translatePoints"></div>/**\r
282      * Translates the passed page coordinates into left/top css values for this element\r
283      * @param {Number/Array} x The page x or an array containing [x, y]\r
284      * @param {Number} y (optional) The page y, required if x is not an array\r
285      * @return {Object} An object with left and top properties. e.g. {left: (value), top: (value)}\r
286      */\r
287     translatePoints : function(x, y){                \r
288             y = isNaN(x[1]) ? y : x[1];\r
289         x = isNaN(x[0]) ? x : x[0];\r
290         var me = this,\r
291                 relative = me.isStyle(POSITION, RELATIVE),\r
292                 o = me.getXY(),\r
293                 l = parseInt(me.getStyle(LEFT), 10),\r
294                 t = parseInt(me.getStyle(TOP), 10);\r
295         \r
296         l = !isNaN(l) ? l : (relative ? 0 : me.dom.offsetLeft);\r
297         t = !isNaN(t) ? t : (relative ? 0 : me.dom.offsetTop);        \r
298 \r
299         return {left: (x - o[0] + l), top: (y - o[1] + t)}; \r
300     },\r
301     \r
302     animTest : animTest\r
303 });\r
304 })();</pre>    \r
305 </body>\r
306 </html>