3 * Copyright(c) 2006-2010 Ext JS, LLC
5 * http://www.extjs.com/license
10 Ext.Element.addMethods({
12 * Gets the x,y coordinates specified by the anchor position on the element.
13 * @param {String} anchor (optional) The specified anchor position (defaults to "c"). See {@link #alignTo}
14 * for details on supported anchor positions.
15 * @param {Boolean} local (optional) True to get the local (element top/left-relative) anchor position instead
17 * @param {Object} size (optional) An object containing the size to use for calculating anchor position
18 * {width: (target width), height: (target height)} (defaults to the element's current size)
19 * @return {Array} [x, y] An array containing the element's x and y coordinates
21 getAnchorXY : function(anchor, local, s){
22 //Passing a different size is useful for pre-calculating anchors,
23 //especially for anchored animations that change the el size.
24 anchor = (anchor || "tl").toLowerCase();
28 vp = me.dom == document.body || me.dom == document,
29 w = s.width || vp ? Ext.lib.Dom.getViewWidth() : me.getWidth(),
30 h = s.height || vp ? Ext.lib.Dom.getViewHeight() : me.getHeight(),
34 scroll = me.getScroll(),
35 extraX = vp ? scroll.left : !local ? o[0] : 0,
36 extraY = vp ? scroll.top : !local ? o[1] : 0,
38 c : [r(w * 0.5), r(h * 0.5)],
50 return [xy[0] + extraX, xy[1] + extraY];
54 * Anchors an element to another element and realigns it when the window is resized.
55 * @param {Mixed} element The element to align to.
56 * @param {String} position The position to align to.
57 * @param {Array} offsets (optional) Offset the positioning by [x, y]
58 * @param {Boolean/Object} animate (optional) True for the default animation or a standard Element animation config object
59 * @param {Boolean/Number} monitorScroll (optional) True to monitor body scroll and reposition. If this parameter
60 * is a number, it is used as the buffer delay (defaults to 50ms).
61 * @param {Function} callback The function to call after the animation finishes
62 * @return {Ext.Element} this
64 anchorTo : function(el, alignment, offsets, animate, monitorScroll, callback){
67 scroll = !Ext.isEmpty(monitorScroll),
69 Ext.fly(dom).alignTo(el, alignment, offsets, animate);
70 Ext.callback(callback, Ext.fly(dom));
72 anchor = this.getAnchor();
74 // previous listener anchor, remove it
81 Ext.EventManager.onWindowResize(action, null);
84 Ext.EventManager.on(window, 'scroll', action, null,
85 {buffer: !isNaN(monitorScroll) ? monitorScroll : 50});
87 action.call(me); // align immediately
92 * Remove any anchor to this element. See {@link #anchorTo}.
93 * @return {Ext.Element} this
95 removeAnchor : function(){
97 anchor = this.getAnchor();
99 if(anchor && anchor.fn){
100 Ext.EventManager.removeResizeListener(anchor.fn);
102 Ext.EventManager.un(window, 'scroll', anchor.fn);
110 getAnchor : function(){
111 var data = Ext.Element.data,
116 var anchor = data(dom, '_anchor');
119 anchor = data(dom, '_anchor', {});
125 * Gets the x,y coordinates to align this element with another element. See {@link #alignTo} for more info on the
126 * supported position values.
127 * @param {Mixed} element The element to align to.
128 * @param {String} position (optional, defaults to "tl-bl?") The position to align to.
129 * @param {Array} offsets (optional) Offset the positioning by [x, y]
130 * @return {Array} [x, y]
132 getAlignToXY : function(el, p, o){
136 throw "Element.alignToXY with an element that doesn't exist";
140 p = (!p || p == "?" ? "tl-bl?" : (!/-/.test(p) && p !== "" ? "tl-" + p : p || "tl-bl")).toLowerCase();
148 //constrain the aligned el to viewport if necessary
152 dw = Ext.lib.Dom.getViewWidth() -10, // 10px of margin for ie
153 dh = Ext.lib.Dom.getViewHeight()-10, // 10px of margin for ie
161 docElement = doc.documentElement,
163 scrollX = (docElement.scrollLeft || docBody.scrollLeft || 0)+5,
164 scrollY = (docElement.scrollTop || docBody.scrollTop || 0)+5,
165 c = false, //constrain to viewport
168 m = p.match(/^([a-z]+)-([a-z]+)(\?)?$/);
171 throw "Element.alignTo with an invalid alignment " + p;
178 //Subtract the aligned el's internal xy from the target's offset xy
179 //plus custom offset to get the aligned el's new offset xy
180 a1 = me.getAnchorXY(p1, true);
181 a2 = el.getAnchorXY(p2, false);
183 x = a2[0] - a1[0] + o[0];
184 y = a2[1] - a1[1] + o[1];
190 //If we are at a viewport boundary and the aligned el is anchored on a target border that is
191 //perpendicular to the vp border, allow the aligned el to slide on that border,
192 //otherwise swap the aligned el to the opposite border of the target.
194 p1x = p1.charAt(p1.length-1);
196 p2x = p2.charAt(p2.length-1);
197 swapY = ((p1y=="t" && p2y=="b") || (p1y=="b" && p2y=="t"));
198 swapX = ((p1x=="r" && p2x=="l") || (p1x=="l" && p2x=="r"));
201 if (x + w > dw + scrollX) {
202 x = swapX ? r.left-w : dw+scrollX-w;
205 x = swapX ? r.right : scrollX;
207 if (y + h > dh + scrollY) {
208 y = swapY ? r.top-h : dh+scrollY-h;
211 y = swapY ? r.bottom : scrollY;
218 * Aligns this element with another element relative to the specified anchor points. If the other element is the
219 * document it aligns it to the viewport.
220 * The position parameter is optional, and can be specified in any one of the following formats:
222 * <li><b>Blank</b>: Defaults to aligning the element's top-left corner to the target's bottom-left corner ("tl-bl").</li>
223 * <li><b>One anchor (deprecated)</b>: The passed anchor position is used as the target element's anchor point.
224 * The element being aligned will position its top-left corner (tl) to that point. <i>This method has been
225 * deprecated in favor of the newer two anchor syntax below</i>.</li>
226 * <li><b>Two anchors</b>: If two values from the table below are passed separated by a dash, the first value is used as the
227 * element's anchor point, and the second value is used as the target's anchor point.</li>
229 * In addition to the anchor points, the position parameter also supports the "?" character. If "?" is passed at the end of
230 * the position string, the element will attempt to align as specified, but the position will be adjusted to constrain to
231 * the viewport if necessary. Note that the element being aligned might be swapped to align to a different position than
232 * that specified in order to enforce the viewport constraints.
233 * Following are all of the supported anchor positions:
236 ----- -----------------------------
237 tl The top left corner (default)
238 t The center of the top edge
239 tr The top right corner
240 l The center of the left edge
241 c In the center of the element
242 r The center of the right edge
243 bl The bottom left corner
244 b The center of the bottom edge
245 br The bottom right corner
249 // align el to other-el using the default positioning ("tl-bl", non-constrained)
250 el.alignTo("other-el");
252 // align the top left corner of el with the top right corner of other-el (constrained to viewport)
253 el.alignTo("other-el", "tr?");
255 // align the bottom right corner of el with the center left edge of other-el
256 el.alignTo("other-el", "br-l?");
258 // align the center of el with the bottom left corner of other-el and
259 // adjust the x position by -6 pixels (and the y position by 0)
260 el.alignTo("other-el", "c-bl", [-6, 0]);
262 * @param {Mixed} element The element to align to.
263 * @param {String} position (optional, defaults to "tl-bl?") The position to align to.
264 * @param {Array} offsets (optional) Offset the positioning by [x, y]
265 * @param {Boolean/Object} animate (optional) true for the default animation or a standard Element animation config object
266 * @return {Ext.Element} this
268 alignTo : function(element, position, offsets, animate){
270 return me.setXY(me.getAlignToXY(element, position, offsets),
271 me.preanim && !!animate ? me.preanim(arguments, 3) : false);
274 // private ==> used outside of core
275 adjustForConstraints : function(xy, parent, offsets){
276 return this.getConstrainToXY(parent || document, false, offsets, xy) || xy;
279 // private ==> used outside of core
280 getConstrainToXY : function(el, local, offsets, proposedXY){
281 var os = {top:0, left:0, bottom:0, right: 0};
283 return function(el, local, offsets, proposedXY){
285 offsets = offsets ? Ext.applyIf(offsets, os) : os;
287 var vw, vh, vx = 0, vy = 0;
288 if(el.dom == document.body || el.dom == document){
289 vw =Ext.lib.Dom.getViewWidth();
290 vh = Ext.lib.Dom.getViewHeight();
292 vw = el.dom.clientWidth;
293 vh = el.dom.clientHeight;
295 var vxy = el.getXY();
301 var s = el.getScroll();
303 vx += offsets.left + s.left;
304 vy += offsets.top + s.top;
307 vh -= offsets.bottom;
312 var xy = proposedXY || (!local ? this.getXY() : [this.getLeft(true), this.getTop(true)]);
313 var x = xy[0], y = xy[1];
314 var w = this.dom.offsetWidth, h = this.dom.offsetHeight;
316 // only move it if it needs it
319 // first validate right/bottom
328 // then make sure top/left isn't negative
337 return moved ? [x, y] : false;
344 // offsets = Ext.applyIf(offsets || {}, {top : 0, left : 0, bottom : 0, right : 0});
348 // s = el.getScroll(),
350 // vx = offsets.left + s.left,
351 // vy = offsets.top + s.top,
352 // vw = -offsets.right,
353 // vh = -offsets.bottom,
356 // xy = proposedXY || (!local ? me.getXY() : [me.getLeft(true), me.getTop(true)]),
359 // w = me.dom.offsetWidth, h = me.dom.offsetHeight,
360 // moved = false; // only move it if it needs it
363 // if(el.dom == doc.body || el.dom == doc){
364 // vw += Ext.lib.Dom.getViewWidth();
365 // vh += Ext.lib.Dom.getViewHeight();
367 // vw += el.dom.clientWidth;
368 // vh += el.dom.clientHeight;
375 // // first validate right/bottom
376 // if(x + w > vx + vw){
380 // if(y + h > vy + vh){
384 // // then make sure top/left isn't negative
393 // return moved ? [x, y] : false;
397 * Calculates the x, y to center this element on the screen
398 * @return {Array} The x, y values [x, y]
400 getCenterXY : function(){
401 return this.getAlignToXY(document, 'c-c');
405 * Centers the Element in either the viewport, or another Element.
406 * @param {Mixed} centerIn (optional) The element in which to center the element.
408 center : function(centerIn){
409 return this.alignTo(centerIn || document, 'c-c');