2 * @class Ext.core.Element
4 Ext.core.Element.addMethods({
6 * Gets the x,y coordinates specified by the anchor position on the element.
7 * @param {String} anchor (optional) The specified anchor position (defaults to "c"). See {@link #alignTo}
8 * for details on supported anchor positions.
9 * @param {Boolean} local (optional) True to get the local (element top/left-relative) anchor position instead
11 * @param {Object} size (optional) An object containing the size to use for calculating anchor position
12 * {width: (target width), height: (target height)} (defaults to the element's current size)
13 * @return {Array} [x, y] An array containing the element's x and y coordinates
15 getAnchorXY : function(anchor, local, s){
16 //Passing a different size is useful for pre-calculating anchors,
17 //especially for anchored animations that change the el size.
18 anchor = (anchor || "tl").toLowerCase();
22 vp = me.dom == document.body || me.dom == document,
23 w = s.width || vp ? Ext.core.Element.getViewWidth() : me.getWidth(),
24 h = s.height || vp ? Ext.core.Element.getViewHeight() : me.getHeight(),
28 scroll = me.getScroll(),
29 extraX = vp ? scroll.left : !local ? o[0] : 0,
30 extraY = vp ? scroll.top : !local ? o[1] : 0,
32 c : [r(w * 0.5), r(h * 0.5)],
44 return [xy[0] + extraX, xy[1] + extraY];
48 * Anchors an element to another element and realigns it when the window is resized.
49 * @param {Mixed} element The element to align to.
50 * @param {String} position The position to align to.
51 * @param {Array} offsets (optional) Offset the positioning by [x, y]
52 * @param {Boolean/Object} animate (optional) True for the default animation or a standard Element animation config object
53 * @param {Boolean/Number} monitorScroll (optional) True to monitor body scroll and reposition. If this parameter
54 * is a number, it is used as the buffer delay (defaults to 50ms).
55 * @param {Function} callback The function to call after the animation finishes
56 * @return {Ext.core.Element} this
58 anchorTo : function(el, alignment, offsets, animate, monitorScroll, callback){
61 scroll = !Ext.isEmpty(monitorScroll),
63 Ext.fly(dom).alignTo(el, alignment, offsets, animate);
64 Ext.callback(callback, Ext.fly(dom));
66 anchor = this.getAnchor();
68 // previous listener anchor, remove it
75 Ext.EventManager.onWindowResize(action, null);
78 Ext.EventManager.on(window, 'scroll', action, null,
79 {buffer: !isNaN(monitorScroll) ? monitorScroll : 50});
81 action.call(me); // align immediately
86 * Remove any anchor to this element. See {@link #anchorTo}.
87 * @return {Ext.core.Element} this
89 removeAnchor : function(){
91 anchor = this.getAnchor();
93 if(anchor && anchor.fn){
94 Ext.EventManager.removeResizeListener(anchor.fn);
96 Ext.EventManager.un(window, 'scroll', anchor.fn);
104 getAnchor : function(){
105 var data = Ext.core.Element.data,
110 var anchor = data(dom, '_anchor');
113 anchor = data(dom, '_anchor', {});
118 getAlignVector: function(el, spec, offset) {
120 side = {t:"top", l:"left", r:"right", b: "bottom"},
121 thisRegion = me.getRegion(),
128 sourceClass: 'Ext.core.Element',
129 sourceMethod: 'getAlignVector',
130 msg: 'Attempted to align an element that doesn\'t exist'
135 elRegion = el.getRegion();
139 * Gets the x,y coordinates to align this element with another element. See {@link #alignTo} for more info on the
140 * supported position values.
141 * @param {Mixed} element The element to align to.
142 * @param {String} position (optional, defaults to "tl-bl?") The position to align to.
143 * @param {Array} offsets (optional) Offset the positioning by [x, y]
144 * @return {Array} [x, y]
146 getAlignToXY : function(el, p, o){
152 sourceClass: 'Ext.core.Element',
153 sourceMethod: 'getAlignToXY',
154 msg: 'Attempted to align an element that doesn\'t exist'
160 p = (!p || p == "?" ? "tl-bl?" : (!(/-/).test(p) && p !== "" ? "tl-" + p : p || "tl-bl")).toLowerCase();
168 //constrain the aligned el to viewport if necessary
172 dw = Ext.core.Element.getViewWidth() -10, // 10px of margin for ie
173 dh = Ext.core.Element.getViewHeight()-10, // 10px of margin for ie
181 docElement = doc.documentElement,
183 scrollX = (docElement.scrollLeft || docBody.scrollLeft || 0)+5,
184 scrollY = (docElement.scrollTop || docBody.scrollTop || 0)+5,
185 c = false, //constrain to viewport
188 m = p.match(/^([a-z]+)-([a-z]+)(\?)?$/);
193 sourceClass: 'Ext.core.Element',
194 sourceMethod: 'getAlignToXY',
198 msg: 'Attemmpted to align an element with an invalid position: "' + p + '"'
207 //Subtract the aligned el's internal xy from the target's offset xy
208 //plus custom offset to get the aligned el's new offset xy
209 a1 = me.getAnchorXY(p1, true);
210 a2 = el.getAnchorXY(p2, false);
212 x = a2[0] - a1[0] + o[0];
213 y = a2[1] - a1[1] + o[1];
219 //If we are at a viewport boundary and the aligned el is anchored on a target border that is
220 //perpendicular to the vp border, allow the aligned el to slide on that border,
221 //otherwise swap the aligned el to the opposite border of the target.
223 p1x = p1.charAt(p1.length-1);
225 p2x = p2.charAt(p2.length-1);
226 swapY = ((p1y=="t" && p2y=="b") || (p1y=="b" && p2y=="t"));
227 swapX = ((p1x=="r" && p2x=="l") || (p1x=="l" && p2x=="r"));
230 if (x + w > dw + scrollX) {
231 x = swapX ? r.left-w : dw+scrollX-w;
234 x = swapX ? r.right : scrollX;
236 if (y + h > dh + scrollY) {
237 y = swapY ? r.top-h : dh+scrollY-h;
240 y = swapY ? r.bottom : scrollY;
247 * Aligns this element with another element relative to the specified anchor points. If the other element is the
248 * document it aligns it to the viewport.
249 * The position parameter is optional, and can be specified in any one of the following formats:
251 * <li><b>Blank</b>: Defaults to aligning the element's top-left corner to the target's bottom-left corner ("tl-bl").</li>
252 * <li><b>One anchor (deprecated)</b>: The passed anchor position is used as the target element's anchor point.
253 * The element being aligned will position its top-left corner (tl) to that point. <i>This method has been
254 * deprecated in favor of the newer two anchor syntax below</i>.</li>
255 * <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
256 * element's anchor point, and the second value is used as the target's anchor point.</li>
258 * In addition to the anchor points, the position parameter also supports the "?" character. If "?" is passed at the end of
259 * the position string, the element will attempt to align as specified, but the position will be adjusted to constrain to
260 * the viewport if necessary. Note that the element being aligned might be swapped to align to a different position than
261 * that specified in order to enforce the viewport constraints.
262 * Following are all of the supported anchor positions:
265 ----- -----------------------------
266 tl The top left corner (default)
267 t The center of the top edge
268 tr The top right corner
269 l The center of the left edge
270 c In the center of the element
271 r The center of the right edge
272 bl The bottom left corner
273 b The center of the bottom edge
274 br The bottom right corner
278 // align el to other-el using the default positioning ("tl-bl", non-constrained)
279 el.alignTo("other-el");
281 // align the top left corner of el with the top right corner of other-el (constrained to viewport)
282 el.alignTo("other-el", "tr?");
284 // align the bottom right corner of el with the center left edge of other-el
285 el.alignTo("other-el", "br-l?");
287 // align the center of el with the bottom left corner of other-el and
288 // adjust the x position by -6 pixels (and the y position by 0)
289 el.alignTo("other-el", "c-bl", [-6, 0]);
291 * @param {Mixed} element The element to align to.
292 * @param {String} position (optional, defaults to "tl-bl?") The position to align to.
293 * @param {Array} offsets (optional) Offset the positioning by [x, y]
294 * @param {Boolean/Object} animate (optional) true for the default animation or a standard Element animation config object
295 * @return {Ext.core.Element} this
297 alignTo : function(element, position, offsets, animate){
299 return me.setXY(me.getAlignToXY(element, position, offsets),
300 me.anim && !!animate ? me.anim(animate) : false);
303 // private ==> used outside of core
304 adjustForConstraints : function(xy, parent) {
305 var vector = this.getConstrainVector(parent, xy);
314 * <p>Returns the <code>[X, Y]</code> vector by which this element must be translated to make a best attempt
315 * to constrain within the passed constraint. Returns <code>false</code> is this element does not need to be moved.</p>
316 * <p>Priority is given to constraining the top and left within the constraint.</p>
317 * <p>The constraint may either be an existing element into which this element is to be constrained, or
318 * an {@link Ext.util.Region Region} into which this element is to be constrained.</p>
319 * @param constrainTo {Mixed} The Element or {@link Ext.util.Region Region} into which this element is to be constrained.
320 * @param proposedPosition {Array} A proposed <code>[X, Y]</code> position to test for validity and to produce a vector for instead
321 * of using this Element's current position;
322 * @returns {Array} <b>If</b> this element <i>needs</i> to be translated, an <code>[X, Y]</code>
323 * vector by which this element must be translated. Otherwise, <code>false</code>.
325 getConstrainVector: function(constrainTo, proposedPosition) {
326 if (!(constrainTo instanceof Ext.util.Region)) {
327 constrainTo = Ext.get(constrainTo).getViewRegion();
329 var thisRegion = this.getRegion(),
331 shadowSize = this.shadow && this.shadow.offset,
334 // Shift this region to occupy the proposed position
335 if (proposedPosition) {
336 thisRegion.translateBy(proposedPosition[0] - thisRegion.x, proposedPosition[1] - thisRegion.y);
339 // Reduce the constrain region to allow for shadow
340 // TODO: Rewrite the Shadow class. When that's done, get the extra for each side from the Shadow.
342 constrainTo.adjust(0, -shadowSize, -shadowSize, shadowSize);
345 // Constrain the X coordinate by however much this Element overflows
346 if (thisRegion.right > constrainTo.right) {
348 vector[0] = (constrainTo.right - thisRegion.right); // overflowed the right
350 if (thisRegion.left + vector[0] < constrainTo.left) {
352 vector[0] = (constrainTo.left - thisRegion.left); // overflowed the left
355 // Constrain the Y coordinate by however much this Element overflows
356 if (thisRegion.bottom > constrainTo.bottom) {
358 vector[1] = (constrainTo.bottom - thisRegion.bottom); // overflowed the bottom
360 if (thisRegion.top + vector[1] < constrainTo.top) {
362 vector[1] = (constrainTo.top - thisRegion.top); // overflowed the top
364 return overflowed ? vector : false;
368 * Calculates the x, y to center this element on the screen
369 * @return {Array} The x, y values [x, y]
371 getCenterXY : function(){
372 return this.getAlignToXY(document, 'c-c');
376 * Centers the Element in either the viewport, or another Element.
377 * @param {Mixed} centerIn (optional) The element in which to center the element.
379 center : function(centerIn){
380 return this.alignTo(centerIn || document, 'c-c');