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