3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
15 * @class Ext.Element
\r
17 Ext.Element.addMethods({
\r
18 <div id="method-Ext.Element-getAnchorXY"></div>/**
\r
19 * Gets the x,y coordinates specified by the anchor position on the element.
\r
20 * @param {String} anchor (optional) The specified anchor position (defaults to "c"). See {@link #alignTo}
\r
21 * for details on supported anchor positions.
\r
22 * @param {Boolean} local (optional) True to get the local (element top/left-relative) anchor position instead
\r
23 * of page coordinates
\r
24 * @param {Object} size (optional) An object containing the size to use for calculating anchor position
\r
25 * {width: (target width), height: (target height)} (defaults to the element's current size)
\r
26 * @return {Array} [x, y] An array containing the element's x and y coordinates
\r
28 getAnchorXY : function(anchor, local, s){
\r
29 //Passing a different size is useful for pre-calculating anchors,
\r
30 //especially for anchored animations that change the el size.
\r
31 anchor = (anchor || "tl").toLowerCase();
\r
35 vp = me.dom == document.body || me.dom == document,
\r
36 w = s.width || vp ? Ext.lib.Dom.getViewWidth() : me.getWidth(),
\r
37 h = s.height || vp ? Ext.lib.Dom.getViewHeight() : me.getHeight(),
\r
41 scroll = me.getScroll(),
\r
42 extraX = vp ? scroll.left : !local ? o[0] : 0,
\r
43 extraY = vp ? scroll.top : !local ? o[1] : 0,
\r
45 c : [r(w * 0.5), r(h * 0.5)],
\r
46 t : [r(w * 0.5), 0],
\r
47 l : [0, r(h * 0.5)],
\r
48 r : [w, r(h * 0.5)],
\r
49 b : [r(w * 0.5), h],
\r
57 return [xy[0] + extraX, xy[1] + extraY];
\r
60 <div id="method-Ext.Element-anchorTo"></div>/**
\r
61 * Anchors an element to another element and realigns it when the window is resized.
\r
62 * @param {Mixed} element The element to align to.
\r
63 * @param {String} position The position to align to.
\r
64 * @param {Array} offsets (optional) Offset the positioning by [x, y]
\r
65 * @param {Boolean/Object} animate (optional) True for the default animation or a standard Element animation config object
\r
66 * @param {Boolean/Number} monitorScroll (optional) True to monitor body scroll and reposition. If this parameter
\r
67 * is a number, it is used as the buffer delay (defaults to 50ms).
\r
68 * @param {Function} callback The function to call after the animation finishes
\r
69 * @return {Ext.Element} this
\r
71 anchorTo : function(el, alignment, offsets, animate, monitorScroll, callback){
\r
76 Ext.fly(dom).alignTo(el, alignment, offsets, animate);
\r
77 Ext.callback(callback, Ext.fly(dom));
\r
80 Ext.EventManager.onWindowResize(action, me);
\r
82 if(!Ext.isEmpty(monitorScroll)){
\r
83 Ext.EventManager.on(window, 'scroll', action, me,
\r
84 {buffer: !isNaN(monitorScroll) ? monitorScroll : 50});
\r
86 action.call(me); // align immediately
\r
90 <div id="method-Ext.Element-getAlignToXY"></div>/**
\r
91 * Gets the x,y coordinates to align this element with another element. See {@link #alignTo} for more info on the
\r
92 * supported position values.
\r
93 * @param {Mixed} element The element to align to.
\r
94 * @param {String} position The position to align to.
\r
95 * @param {Array} offsets (optional) Offset the positioning by [x, y]
\r
96 * @return {Array} [x, y]
\r
98 getAlignToXY : function(el, p, o){
\r
101 if(!el || !el.dom){
\r
102 throw "Element.alignToXY with an element that doesn't exist";
\r
106 p = (p == "?" ? "tl-bl?" : (!/-/.test(p) && p !== "" ? "tl-" + p : p || "tl-bl")).toLowerCase();
\r
114 //constrain the aligned el to viewport if necessary
\r
118 dw = Ext.lib.Dom.getViewWidth() -10, // 10px of margin for ie
\r
119 dh = Ext.lib.Dom.getViewHeight()-10, // 10px of margin for ie
\r
127 docElement = doc.documentElement,
\r
128 docBody = doc.body,
\r
129 scrollX = (docElement.scrollLeft || docBody.scrollLeft || 0)+5,
\r
130 scrollY = (docElement.scrollTop || docBody.scrollTop || 0)+5,
\r
131 c = false, //constrain to viewport
\r
134 m = p.match(/^([a-z]+)-([a-z]+)(\?)?$/);
\r
137 throw "Element.alignTo with an invalid alignment " + p;
\r
144 //Subtract the aligned el's internal xy from the target's offset xy
\r
145 //plus custom offset to get the aligned el's new offset xy
\r
146 a1 = me.getAnchorXY(p1, true);
\r
147 a2 = el.getAnchorXY(p2, false);
\r
149 x = a2[0] - a1[0] + o[0];
\r
150 y = a2[1] - a1[1] + o[1];
\r
154 h = me.getHeight();
\r
155 r = el.getRegion();
\r
156 //If we are at a viewport boundary and the aligned el is anchored on a target border that is
\r
157 //perpendicular to the vp border, allow the aligned el to slide on that border,
\r
158 //otherwise swap the aligned el to the opposite border of the target.
\r
159 p1y = p1.charAt(0);
\r
160 p1x = p1.charAt(p1.length-1);
\r
161 p2y = p2.charAt(0);
\r
162 p2x = p2.charAt(p2.length-1);
\r
163 swapY = ((p1y=="t" && p2y=="b") || (p1y=="b" && p2y=="t"));
\r
164 swapX = ((p1x=="r" && p2x=="l") || (p1x=="l" && p2x=="r"));
\r
167 if (x + w > dw + scrollX) {
\r
168 x = swapX ? r.left-w : dw+scrollX-w;
\r
171 x = swapX ? r.right : scrollX;
\r
173 if (y + h > dh + scrollY) {
\r
174 y = swapY ? r.top-h : dh+scrollY-h;
\r
177 y = swapY ? r.bottom : scrollY;
\r
183 <div id="method-Ext.Element-alignTo"></div>/**
\r
184 * Aligns this element with another element relative to the specified anchor points. If the other element is the
\r
185 * document it aligns it to the viewport.
\r
186 * The position parameter is optional, and can be specified in any one of the following formats:
\r
188 * <li><b>Blank</b>: Defaults to aligning the element's top-left corner to the target's bottom-left corner ("tl-bl").</li>
\r
189 * <li><b>One anchor (deprecated)</b>: The passed anchor position is used as the target element's anchor point.
\r
190 * The element being aligned will position its top-left corner (tl) to that point. <i>This method has been
\r
191 * deprecated in favor of the newer two anchor syntax below</i>.</li>
\r
192 * <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
\r
193 * element's anchor point, and the second value is used as the target's anchor point.</li>
\r
195 * In addition to the anchor points, the position parameter also supports the "?" character. If "?" is passed at the end of
\r
196 * the position string, the element will attempt to align as specified, but the position will be adjusted to constrain to
\r
197 * the viewport if necessary. Note that the element being aligned might be swapped to align to a different position than
\r
198 * that specified in order to enforce the viewport constraints.
\r
199 * Following are all of the supported anchor positions:
\r
202 ----- -----------------------------
\r
203 tl The top left corner (default)
\r
204 t The center of the top edge
\r
205 tr The top right corner
\r
206 l The center of the left edge
\r
207 c In the center of the element
\r
208 r The center of the right edge
\r
209 bl The bottom left corner
\r
210 b The center of the bottom edge
\r
211 br The bottom right corner
\r
215 // align el to other-el using the default positioning ("tl-bl", non-constrained)
\r
216 el.alignTo("other-el");
\r
218 // align the top left corner of el with the top right corner of other-el (constrained to viewport)
\r
219 el.alignTo("other-el", "tr?");
\r
221 // align the bottom right corner of el with the center left edge of other-el
\r
222 el.alignTo("other-el", "br-l?");
\r
224 // align the center of el with the bottom left corner of other-el and
\r
225 // adjust the x position by -6 pixels (and the y position by 0)
\r
226 el.alignTo("other-el", "c-bl", [-6, 0]);
\r
228 * @param {Mixed} element The element to align to.
\r
229 * @param {String} position The position to align to.
\r
230 * @param {Array} offsets (optional) Offset the positioning by [x, y]
\r
231 * @param {Boolean/Object} animate (optional) true for the default animation or a standard Element animation config object
\r
232 * @return {Ext.Element} this
\r
234 alignTo : function(element, position, offsets, animate){
\r
236 return me.setXY(me.getAlignToXY(element, position, offsets),
\r
237 me.preanim && !!animate ? me.preanim(arguments, 3) : false);
\r
240 // private ==> used outside of core
\r
241 adjustForConstraints : function(xy, parent, offsets){
\r
242 return this.getConstrainToXY(parent || document, false, offsets, xy) || xy;
\r
245 // private ==> used outside of core
\r
246 getConstrainToXY : function(el, local, offsets, proposedXY){
\r
247 var os = {top:0, left:0, bottom:0, right: 0};
\r
249 return function(el, local, offsets, proposedXY){
\r
251 offsets = offsets ? Ext.applyIf(offsets, os) : os;
\r
253 var vw, vh, vx = 0, vy = 0;
\r
254 if(el.dom == document.body || el.dom == document){
\r
255 vw =Ext.lib.Dom.getViewWidth();
\r
256 vh = Ext.lib.Dom.getViewHeight();
\r
258 vw = el.dom.clientWidth;
\r
259 vh = el.dom.clientHeight;
\r
261 var vxy = el.getXY();
\r
267 var s = el.getScroll();
\r
269 vx += offsets.left + s.left;
\r
270 vy += offsets.top + s.top;
\r
272 vw -= offsets.right;
\r
273 vh -= offsets.bottom;
\r
278 var xy = proposedXY || (!local ? this.getXY() : [this.getLeft(true), this.getTop(true)]);
\r
279 var x = xy[0], y = xy[1];
\r
280 var w = this.dom.offsetWidth, h = this.dom.offsetHeight;
\r
282 // only move it if it needs it
\r
285 // first validate right/bottom
\r
294 // then make sure top/left isn't negative
\r
303 return moved ? [x, y] : false;
\r
309 // el = Ext.get(el);
\r
310 // offsets = Ext.applyIf(offsets || {}, {top : 0, left : 0, bottom : 0, right : 0});
\r
314 // s = el.getScroll(),
\r
315 // vxy = el.getXY(),
\r
316 // vx = offsets.left + s.left,
\r
317 // vy = offsets.top + s.top,
\r
318 // vw = -offsets.right,
\r
319 // vh = -offsets.bottom,
\r
322 // xy = proposedXY || (!local ? me.getXY() : [me.getLeft(true), me.getTop(true)]),
\r
325 // w = me.dom.offsetWidth, h = me.dom.offsetHeight,
\r
326 // moved = false; // only move it if it needs it
\r
329 // if(el.dom == doc.body || el.dom == doc){
\r
330 // vw += Ext.lib.Dom.getViewWidth();
\r
331 // vh += Ext.lib.Dom.getViewHeight();
\r
333 // vw += el.dom.clientWidth;
\r
334 // vh += el.dom.clientHeight;
\r
341 // // first validate right/bottom
\r
342 // if(x + w > vx + vw){
\r
343 // x = vx + vw - w;
\r
346 // if(y + h > vy + vh){
\r
347 // y = vy + vh - h;
\r
350 // // then make sure top/left isn't negative
\r
359 // return moved ? [x, y] : false;
\r
362 <div id="method-Ext.Element-getCenterXY"></div>/**
\r
363 * Calculates the x, y to center this element on the screen
\r
364 * @return {Array} The x, y values [x, y]
\r
366 getCenterXY : function(){
\r
367 return this.getAlignToXY(document, 'c-c');
\r
370 <div id="method-Ext.Element-center"></div>/**
\r
371 * Centers the Element in either the viewport, or another Element.
\r
372 * @param {Mixed} centerIn (optional) The element in which to center the element.
\r
374 center : function(centerIn){
\r
375 return this.alignTo(centerIn || document, 'c-c');
\r