Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / src / core / Element.position-more.js
1 /*!
2  * Ext JS Library 3.0.3
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 a box {x, y, width, height} that can be used to set another elements\r
32      * 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 {x, y, width, height}\r
36      */\r
37         getBox : function(contentBox, local) {      \r
38             var me = this,\r
39                 xy,\r
40                 left,\r
41                 top,\r
42                 getBorderWidth = me.getBorderWidth,\r
43                 getPadding = me.getPadding, \r
44                 l,\r
45                 r,\r
46                 t,\r
47                 b;\r
48         if(!local){\r
49             xy = me.getXY();\r
50         }else{\r
51             left = parseInt(me.getStyle("left"), 10) || 0;\r
52             top = parseInt(me.getStyle("top"), 10) || 0;\r
53             xy = [left, top];\r
54         }\r
55         var el = me.dom, w = el.offsetWidth, h = el.offsetHeight, bx;\r
56         if(!contentBox){\r
57             bx = {x: xy[0], y: xy[1], 0: xy[0], 1: xy[1], width: w, height: h};\r
58         }else{\r
59             l = getBorderWidth.call(me, "l") + getPadding.call(me, "l");\r
60             r = getBorderWidth.call(me, "r") + getPadding.call(me, "r");\r
61             t = getBorderWidth.call(me, "t") + getPadding.call(me, "t");\r
62             b = getBorderWidth.call(me, "b") + getPadding.call(me, "b");\r
63             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
64         }\r
65         bx.right = bx.x + bx.width;\r
66         bx.bottom = bx.y + bx.height;\r
67         return bx;\r
68         },\r
69         \r
70     /**\r
71      * Move this element relative to its current position.\r
72      * @param {String} direction Possible values are: "l" (or "left"), "r" (or "right"), "t" (or "top", or "up"), "b" (or "bottom", or "down").\r
73      * @param {Number} distance How far to move the element in pixels\r
74      * @param {Boolean/Object} animate (optional) true for the default animation or a standard Element animation config object\r
75      * @return {Ext.Element} this\r
76      */\r
77      move : function(direction, distance, animate){\r
78         var me = this,          \r
79                 xy = me.getXY(),\r
80                 x = xy[0],\r
81                 y = xy[1],              \r
82                 left = [x - distance, y],\r
83                 right = [x + distance, y],\r
84                 top = [x, y - distance],\r
85                 bottom = [x, y + distance],\r
86                 hash = {\r
87                         l :     left,\r
88                         left : left,\r
89                         r : right,\r
90                         right : right,\r
91                         t : top,\r
92                         top : top,\r
93                         up : top,\r
94                         b : bottom, \r
95                         bottom : bottom,\r
96                         down : bottom                           \r
97                 };\r
98         \r
99             direction = direction.toLowerCase();    \r
100             me.moveTo(hash[direction][0], hash[direction][1], me.animTest.call(me, arguments, animate, 2));\r
101     },\r
102     \r
103     /**\r
104      * Quick set left and top adding default units\r
105      * @param {String} left The left CSS property value\r
106      * @param {String} top The top CSS property value\r
107      * @return {Ext.Element} this\r
108      */\r
109      setLeftTop : function(left, top){\r
110             var me = this,\r
111                 style = me.dom.style;\r
112         style.left = me.addUnits(left);\r
113         style.top = me.addUnits(top);\r
114         return me;\r
115     },\r
116     \r
117     /**\r
118      * Returns the region of the given element.\r
119      * The element must be part of the DOM tree to have a region (display:none or elements not appended return false).\r
120      * @return {Region} A Ext.lib.Region containing "top, left, bottom, right" member data.\r
121      */\r
122     getRegion : function(){\r
123         return Ext.lib.Dom.getRegion(this.dom);\r
124     },\r
125     \r
126     /**\r
127      * 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
128      * @param {Number} x X value for new position (coordinates are page-based)\r
129      * @param {Number} y Y value for new position (coordinates are page-based)\r
130      * @param {Mixed} width The new width. This may be one of:<div class="mdetail-params"><ul>\r
131      * <li>A Number specifying the new width in this Element's {@link #defaultUnit}s (by default, pixels)</li>\r
132      * <li>A String used to set the CSS width style. Animation may <b>not</b> be used.\r
133      * </ul></div>\r
134      * @param {Mixed} height The new height. This may be one of:<div class="mdetail-params"><ul>\r
135      * <li>A Number specifying the new height in this Element's {@link #defaultUnit}s (by default, pixels)</li>\r
136      * <li>A String used to set the CSS height style. Animation may <b>not</b> be used.</li>\r
137      * </ul></div>\r
138      * @param {Boolean/Object} animate (optional) true for the default animation or a standard Element animation config object\r
139      * @return {Ext.Element} this\r
140      */\r
141     setBounds : function(x, y, width, height, animate){\r
142             var me = this;\r
143         if (!animate || !me.anim) {\r
144             me.setSize(width, height);\r
145             me.setLocation(x, y);\r
146         } else {\r
147             me.anim({points: {to: [x, y]}, \r
148                          width: {to: me.adjustWidth(width)}, \r
149                          height: {to: me.adjustHeight(height)}},\r
150                      me.preanim(arguments, 4), \r
151                      'motion');\r
152         }\r
153         return me;\r
154     },\r
155 \r
156     /**\r
157      * 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
158      * @param {Ext.lib.Region} region The region to fill\r
159      * @param {Boolean/Object} animate (optional) true for the default animation or a standard Element animation config object\r
160      * @return {Ext.Element} this\r
161      */\r
162     setRegion : function(region, animate) {\r
163         return this.setBounds(region.left, region.top, region.right-region.left, region.bottom-region.top, this.animTest.call(this, arguments, animate, 1));\r
164     }\r
165 });