3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
11 var D = Ext.lib.Dom,
\r
16 POSITION = "position",
\r
18 RELATIVE = "relative",
\r
22 Ext.Element.addMethods({
\r
24 * 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
25 * @return {Number} The X position of the element
\r
28 return D.getX(this.dom);
\r
32 * 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
33 * @return {Number} The Y position of the element
\r
36 return D.getY(this.dom);
\r
40 * 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
41 * @return {Array} The XY position of the element
\r
44 return D.getXY(this.dom);
\r
48 * 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
49 * @param {Mixed} element The element to get the offsets from.
\r
50 * @return {Array} The XY page offsets (e.g. [100, -200])
\r
52 getOffsetsTo : function(el){
\r
53 var o = this.getXY(),
\r
54 e = Ext.fly(el, '_internal').getXY();
\r
55 return [o[0]-e[0],o[1]-e[1]];
\r
59 * 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
60 * @param {Number} The X position of the element
\r
61 * @param {Boolean/Object} animate (optional) True for the default animation, or a standard Element animation config object
\r
62 * @return {Ext.Element} this
\r
64 setX : function(x, animate){
\r
65 return this.setXY([x, this.getY()], this.animTest(arguments, animate, 1));
\r
69 * 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
70 * @param {Number} The Y position of the element
\r
71 * @param {Boolean/Object} animate (optional) True for the default animation, or a standard Element animation config object
\r
72 * @return {Ext.Element} this
\r
74 setY : function(y, animate){
\r
75 return this.setXY([this.getX(), y], this.animTest(arguments, animate, 1));
\r
79 * Sets the element's left position directly using CSS style (instead of {@link #setX}).
\r
80 * @param {String} left The left CSS property value
\r
81 * @return {Ext.Element} this
\r
83 setLeft : function(left){
\r
84 this.setStyle(LEFT, this.addUnits(left));
\r
89 * Sets the element's top position directly using CSS style (instead of {@link #setY}).
\r
90 * @param {String} top The top CSS property value
\r
91 * @return {Ext.Element} this
\r
93 setTop : function(top){
\r
94 this.setStyle(TOP, this.addUnits(top));
\r
99 * Sets the element's CSS right style.
\r
100 * @param {String} right The right CSS property value
\r
101 * @return {Ext.Element} this
\r
103 setRight : function(right){
\r
104 this.setStyle(RIGHT, this.addUnits(right));
\r
109 * Sets the element's CSS bottom style.
\r
110 * @param {String} bottom The bottom CSS property value
\r
111 * @return {Ext.Element} this
\r
113 setBottom : function(bottom){
\r
114 this.setStyle(BOTTOM, this.addUnits(bottom));
\r
119 * Sets the position of the element in page coordinates, regardless of how the element is positioned.
\r
120 * The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
\r
121 * @param {Array} pos Contains X & Y [x, y] values for new position (coordinates are page-based)
\r
122 * @param {Boolean/Object} animate (optional) True for the default animation, or a standard Element animation config object
\r
123 * @return {Ext.Element} this
\r
125 setXY : function(pos, animate){
\r
127 if(!animate || !me.anim){
\r
128 D.setXY(me.dom, pos);
\r
130 me.anim({points: {to: pos}}, me.preanim(arguments, 1), 'motion');
\r
136 * Sets the position of the element in page coordinates, regardless of how the element is positioned.
\r
137 * The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
\r
138 * @param {Number} x X value for new position (coordinates are page-based)
\r
139 * @param {Number} y Y value for new position (coordinates are page-based)
\r
140 * @param {Boolean/Object} animate (optional) True for the default animation, or a standard Element animation config object
\r
141 * @return {Ext.Element} this
\r
143 setLocation : function(x, y, animate){
\r
144 return this.setXY([x, y], this.animTest(arguments, animate, 2));
\r
148 * Sets the position of the element in page coordinates, regardless of how the element is positioned.
\r
149 * The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
\r
150 * @param {Number} x X value for new position (coordinates are page-based)
\r
151 * @param {Number} y Y value for new position (coordinates are page-based)
\r
152 * @param {Boolean/Object} animate (optional) True for the default animation, or a standard Element animation config object
\r
153 * @return {Ext.Element} this
\r
155 moveTo : function(x, y, animate){
\r
156 return this.setXY([x, y], this.animTest(arguments, animate, 2));
\r
160 * Gets the left X coordinate
\r
161 * @param {Boolean} local True to get the local css position instead of page coordinate
\r
164 getLeft : function(local){
\r
165 return !local ? this.getX() : parseInt(this.getStyle(LEFT), 10) || 0;
\r
169 * Gets the right X coordinate of the element (element X position + element width)
\r
170 * @param {Boolean} local True to get the local css position instead of page coordinate
\r
173 getRight : function(local){
\r
175 return !local ? me.getX() + me.getWidth() : (me.getLeft(true) + me.getWidth()) || 0;
\r
179 * Gets the top Y coordinate
\r
180 * @param {Boolean} local True to get the local css position instead of page coordinate
\r
183 getTop : function(local) {
\r
184 return !local ? this.getY() : parseInt(this.getStyle(TOP), 10) || 0;
\r
188 * Gets the bottom Y coordinate of the element (element Y position + element height)
\r
189 * @param {Boolean} local True to get the local css position instead of page coordinate
\r
192 getBottom : function(local){
\r
194 return !local ? me.getY() + me.getHeight() : (me.getTop(true) + me.getHeight()) || 0;
\r
198 * Initializes positioning on this element. If a desired position is not passed, it will make the
\r
199 * the element positioned relative IF it is not already positioned.
\r
200 * @param {String} pos (optional) Positioning to use "relative", "absolute" or "fixed"
\r
201 * @param {Number} zIndex (optional) The zIndex to apply
\r
202 * @param {Number} x (optional) Set the page X position
\r
203 * @param {Number} y (optional) Set the page Y position
\r
205 position : function(pos, zIndex, x, y){
\r
208 if(!pos && me.isStyle(POSITION, STATIC)){
\r
209 me.setStyle(POSITION, RELATIVE);
\r
211 me.setStyle(POSITION, pos);
\r
214 me.setStyle(ZINDEX, zIndex);
\r
216 if(x || y) me.setXY([x || false, y || false]);
\r
220 * Clear positioning back to the default when the document was loaded
\r
221 * @param {String} value (optional) The value to use for the left,right,top,bottom, defaults to '' (empty string). You could use 'auto'.
\r
222 * @return {Ext.Element} this
\r
224 clearPositioning : function(value){
\r
225 value = value || '';
\r
238 * Gets an object with all CSS positioning properties. Useful along with setPostioning to get
\r
239 * snapshot before performing an update and then restoring the element.
\r
242 getPositioning : function(){
\r
243 var l = this.getStyle(LEFT);
\r
244 var t = this.getStyle(TOP);
\r
246 "position" : this.getStyle(POSITION),
\r
248 "right" : l ? "" : this.getStyle(RIGHT),
\r
250 "bottom" : t ? "" : this.getStyle(BOTTOM),
\r
251 "z-index" : this.getStyle(ZINDEX)
\r
256 * Set positioning with an object returned by getPositioning().
\r
257 * @param {Object} posCfg
\r
258 * @return {Ext.Element} this
\r
260 setPositioning : function(pc){
\r
262 style = me.dom.style;
\r
266 if(pc.right == AUTO){
\r
269 if(pc.bottom == AUTO){
\r
277 * Translates the passed page coordinates into left/top css values for this element
\r
278 * @param {Number/Array} x The page x or an array containing [x, y]
\r
279 * @param {Number} y (optional) The page y, required if x is not an array
\r
280 * @return {Object} An object with left and top properties. e.g. {left: (value), top: (value)}
\r
282 translatePoints : function(x, y){
\r
283 y = isNaN(x[1]) ? y : x[1];
\r
284 x = isNaN(x[0]) ? x : x[0];
\r
286 relative = me.isStyle(POSITION, RELATIVE),
\r
288 l = parseInt(me.getStyle(LEFT), 10),
\r
289 t = parseInt(me.getStyle(TOP), 10);
\r
291 l = !isNaN(l) ? l : (relative ? 0 : me.dom.offsetLeft);
\r
292 t = !isNaN(t) ? t : (relative ? 0 : me.dom.offsetTop);
\r
294 return {left: (x - o[0] + l), top: (y - o[1] + t)};
\r
297 animTest : function(args, animate, i) {
\r
298 return !!animate && this.preanim ? this.preanim(args, i) : false;
\r