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