2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
8 YUI.add('dom-screen', function(Y) {
13 * Adds position and region management functionality to DOM.
15 * @submodule dom-screen
19 var DOCUMENT_ELEMENT = 'documentElement',
20 COMPAT_MODE = 'compatMode',
21 POSITION = 'position',
23 RELATIVE = 'relative',
26 _BACK_COMPAT = 'BackCompat',
28 BORDER_LEFT_WIDTH = 'borderLeftWidth',
29 BORDER_TOP_WIDTH = 'borderTopWidth',
30 GET_BOUNDING_CLIENT_RECT = 'getBoundingClientRect',
31 GET_COMPUTED_STYLE = 'getComputedStyle',
33 // TODO: how about thead/tbody/tfoot/tr?
34 // TODO: does caption matter?
35 RE_TABLE = /^t(?:able|d|h)$/i;
39 * Returns the inner height of the viewport (exludes scrollbar).
41 * @return {Number} The current height of the viewport.
43 winHeight: function(node) {
44 var h = Y.DOM._getWinSize(node).height;
49 * Returns the inner width of the viewport (exludes scrollbar).
51 * @return {Number} The current width of the viewport.
53 winWidth: function(node) {
54 var w = Y.DOM._getWinSize(node).width;
61 * @return {Number} The current height of the document.
63 docHeight: function(node) {
64 var h = Y.DOM._getDocSize(node).height;
65 return Math.max(h, Y.DOM._getWinSize(node).height);
71 * @return {Number} The current width of the document.
73 docWidth: function(node) {
74 var w = Y.DOM._getDocSize(node).width;
75 return Math.max(w, Y.DOM._getWinSize(node).width);
79 * Amount page has been scroll horizontally
81 * @return {Number} The current amount the screen is scrolled horizontally.
83 docScrollX: function(node) {
84 var doc = Y.DOM._getDoc(node);
85 return Math.max(doc[DOCUMENT_ELEMENT].scrollLeft, doc.body.scrollLeft);
89 * Amount page has been scroll vertically
91 * @return {Number} The current amount the screen is scrolled vertically.
93 docScrollY: function(node) {
94 var doc = Y.DOM._getDoc(node);
95 return Math.max(doc[DOCUMENT_ELEMENT].scrollTop, doc.body.scrollTop);
99 * Gets the current position of an element based on page coordinates.
100 * Element must be part of the DOM tree to have page coordinates
101 * (display:none or elements not appended return false).
103 * @param element The target element
104 * @return {Array} The XY position of the element
106 TODO: test inDocument/display?
109 if (document[DOCUMENT_ELEMENT][GET_BOUNDING_CLIENT_RECT]) {
110 return function(node) {
121 if (Y.DOM.inDoc(node)) {
122 scrollLeft = Y.DOM.docScrollX(node);
123 scrollTop = Y.DOM.docScrollY(node);
124 box = node[GET_BOUNDING_CLIENT_RECT]();
125 doc = Y.DOM._getDoc(node);
126 xy = [box.left, box.top];
131 mode = doc[COMPAT_MODE];
132 bLeft = Y.DOM[GET_COMPUTED_STYLE](doc[DOCUMENT_ELEMENT], BORDER_LEFT_WIDTH);
133 bTop = Y.DOM[GET_COMPUTED_STYLE](doc[DOCUMENT_ELEMENT], BORDER_TOP_WIDTH);
136 if (mode !== _BACK_COMPAT) {
142 if ((mode == _BACK_COMPAT)) {
143 if (bLeft !== MEDIUM) {
144 off1 = parseInt(bLeft, 10);
146 if (bTop !== MEDIUM) {
147 off2 = parseInt(bTop, 10);
156 if ((scrollTop || scrollLeft)) {
160 } else { // default to current offsets
161 xy = Y.DOM._getOffset(node);
167 return function(node) { // manually calculate by crawling up offsetParents
168 //Calculate the Top and Left border sizes (assumes pixels)
176 if (Y.DOM.inDoc(node)) {
177 xy = [node.offsetLeft, node.offsetTop];
179 // TODO: refactor with !! or just falsey
180 bCheck = ((Y.UA.gecko || Y.UA.webkit > 519) ? true : false);
182 // TODO: worth refactoring for TOP/LEFT only?
183 while ((parentNode = parentNode.offsetParent)) {
184 xy[0] += parentNode.offsetLeft;
185 xy[1] += parentNode.offsetTop;
187 xy = Y.DOM._calcBorders(parentNode, xy);
191 // account for any scrolled ancestors
192 if (Y.DOM.getStyle(node, POSITION) != FIXED) {
195 while ((parentNode = parentNode.parentNode)) {
196 scrollTop = parentNode.scrollTop;
197 scrollLeft = parentNode.scrollLeft;
199 //Firefox does something funky with borders when overflow is not visible.
200 if (Y.UA.gecko && (Y.DOM.getStyle(parentNode, 'overflow') !== 'visible')) {
201 xy = Y.DOM._calcBorders(parentNode, xy);
205 if (scrollTop || scrollLeft) {
210 xy[0] += Y.DOM.docScrollX(node);
211 xy[1] += Y.DOM.docScrollY(node);
214 //Fix FIXED position -- add scrollbars
215 xy[0] += Y.DOM.docScrollX(node);
216 xy[1] += Y.DOM.docScrollY(node);
219 xy = Y.DOM._getOffset(node);
226 }(),// NOTE: Executing for loadtime branching
228 _getOffset: function(node) {
233 pos = Y.DOM.getStyle(node, POSITION);
235 parseInt(Y.DOM[GET_COMPUTED_STYLE](node, LEFT), 10),
236 parseInt(Y.DOM[GET_COMPUTED_STYLE](node, TOP), 10)
239 if ( isNaN(xy[0]) ) { // in case of 'auto'
240 xy[0] = parseInt(Y.DOM.getStyle(node, LEFT), 10); // try inline
241 if ( isNaN(xy[0]) ) { // default to offset value
242 xy[0] = (pos === RELATIVE) ? 0 : node.offsetLeft || 0;
246 if ( isNaN(xy[1]) ) { // in case of 'auto'
247 xy[1] = parseInt(Y.DOM.getStyle(node, TOP), 10); // try inline
248 if ( isNaN(xy[1]) ) { // default to offset value
249 xy[1] = (pos === RELATIVE) ? 0 : node.offsetTop || 0;
259 * Gets the current X position of an element based on page coordinates.
260 * Element must be part of the DOM tree to have page coordinates
261 * (display:none or elements not appended return false).
263 * @param element The target element
264 * @return {Int} The X position of the element
267 getX: function(node) {
268 return Y.DOM.getXY(node)[0];
272 * Gets the current Y position of an element based on page coordinates.
273 * Element must be part of the DOM tree to have page coordinates
274 * (display:none or elements not appended return false).
276 * @param element The target element
277 * @return {Int} The Y position of the element
280 getY: function(node) {
281 return Y.DOM.getXY(node)[1];
285 * Set the position of an html element in page coordinates.
286 * The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
288 * @param element The target element
289 * @param {Array} xy Contains X & Y values for new position (coordinates are page-based)
290 * @param {Boolean} noRetry By default we try and set the position a second time if the first fails
292 setXY: function(node, xy, noRetry) {
293 var setStyle = Y.DOM.setStyle,
300 pos = Y.DOM.getStyle(node, POSITION);
302 delta = Y.DOM._getOffset(node);
304 if (pos == 'static') { // default to relative
306 setStyle(node, POSITION, pos);
309 currentXY = Y.DOM.getXY(node);
311 if (xy[0] !== null) {
312 setStyle(node, LEFT, xy[0] - currentXY[0] + delta[0] + 'px');
315 if (xy[1] !== null) {
316 setStyle(node, TOP, xy[1] - currentXY[1] + delta[1] + 'px');
320 newXY = Y.DOM.getXY(node);
321 if (newXY[0] !== xy[0] || newXY[1] !== xy[1]) {
322 Y.DOM.setXY(node, xy, true);
331 * Set the X position of an html element in page coordinates, regardless of how the element is positioned.
332 * The element(s) must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
334 * @param element The target element
335 * @param {Int} x The X values for new position (coordinates are page-based)
337 setX: function(node, x) {
338 return Y.DOM.setXY(node, [x, null]);
342 * Set the Y position of an html element in page coordinates, regardless of how the element is positioned.
343 * The element(s) must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
345 * @param element The target element
346 * @param {Int} y The Y values for new position (coordinates are page-based)
348 setY: function(node, y) {
349 return Y.DOM.setXY(node, [null, y]);
352 _calcBorders: function(node, xy2) {
353 var t = parseInt(Y.DOM[GET_COMPUTED_STYLE](node, BORDER_TOP_WIDTH), 10) || 0,
354 l = parseInt(Y.DOM[GET_COMPUTED_STYLE](node, BORDER_LEFT_WIDTH), 10) || 0;
356 if (RE_TABLE.test(node.tagName)) {
366 _getWinSize: function(node) {
367 var doc = Y.DOM._getDoc(),
368 win = doc.defaultView || doc.parentWindow,
369 mode = doc[COMPAT_MODE],
372 root = doc[DOCUMENT_ELEMENT];
374 if ( mode && !Y.UA.opera ) { // IE, Gecko
375 if (mode != 'CSS1Compat') { // Quirks
378 h = root.clientHeight;
379 w = root.clientWidth;
381 return { height: h, width: w };
384 _getDocSize: function(node) {
385 var doc = Y.DOM._getDoc(),
386 root = doc[DOCUMENT_ELEMENT];
388 if (doc[COMPAT_MODE] != 'CSS1Compat') {
392 return { height: root.scrollHeight, width: root.scrollWidth };
402 getOffsets = function(r1, r2) {
403 var t = Math.max(r1[TOP], r2[TOP]),
404 r = Math.min(r1[RIGHT], r2[RIGHT]),
405 b = Math.min(r1[BOTTOM], r2[BOTTOM]),
406 l = Math.max(r1[LEFT], r2[LEFT]),
420 * Returns an Object literal containing the following about this element: (top, right, bottom, left)
422 * @param {HTMLElement} element The DOM element.
423 @return {Object} Object literal containing the following about this element: (top, right, bottom, left)
425 region: function(node) {
426 var xy = DOM.getXY(node),
430 ret = DOM._getRegion(
432 xy[0] + node.offsetWidth, // right
433 xy[1] + node.offsetHeight, // bottom
442 * Find the intersect information for the passes nodes.
444 * @param {HTMLElement} element The first element
445 * @param {HTMLElement | Object} element2 The element or region to check the interect with
446 * @param {Object} altRegion An object literal containing the region for the first element if we already have the data (for performance i.e. DragDrop)
447 @return {Object} Object literal containing the following intersection data: (top, right, bottom, left, area, yoff, xoff, inRegion)
449 intersect: function(node, node2, altRegion) {
450 var r = altRegion || DOM.region(node), region = {},
455 region = DOM.region(n);
456 } else if (Y.Lang.isObject(node2)) {
462 off = getOffsets(region, r);
468 area: ((off[BOTTOM] - off[TOP]) * (off[RIGHT] - off[LEFT])),
469 yoff: ((off[BOTTOM] - off[TOP])),
470 xoff: (off[RIGHT] - off[LEFT]),
471 inRegion: DOM.inRegion(node, node2, false, altRegion)
476 * Check if any part of this node is in the passed region
478 * @param {Object} node2 The node to get the region from or an Object literal of the region
479 * $param {Boolean} all Should all of the node be inside the region
480 * @param {Object} altRegion An object literal containing the region for this node if we already have the data (for performance i.e. DragDrop)
481 * @return {Boolean} True if in region, false if not.
483 inRegion: function(node, node2, all, altRegion) {
485 r = altRegion || DOM.region(node),
490 region = DOM.region(n);
491 } else if (Y.Lang.isObject(node2)) {
499 r[LEFT] >= region[LEFT] &&
500 r[RIGHT] <= region[RIGHT] &&
501 r[TOP] >= region[TOP] &&
502 r[BOTTOM] <= region[BOTTOM] );
504 off = getOffsets(region, r);
505 if (off[BOTTOM] >= off[TOP] && off[RIGHT] >= off[LEFT]) {
515 * Check if any part of this element is in the viewport
516 * @method inViewportRegion
517 * @param {HTMLElement} element The DOM element.
518 * @param {Boolean} all Should all of the node be inside the region
519 * @param {Object} altRegion An object literal containing the region for this node if we already have the data (for performance i.e. DragDrop)
520 * @return {Boolean} True if in region, false if not.
522 inViewportRegion: function(node, all, altRegion) {
523 return DOM.inRegion(node, DOM.viewportRegion(node), all, altRegion);
527 _getRegion: function(t, r, b, l) {
530 region[TOP] = region[1] = t;
531 region[LEFT] = region[0] = l;
534 region.width = region[RIGHT] - region[LEFT];
535 region.height = region[BOTTOM] - region[TOP];
541 * Returns an Object literal containing the following about the visible region of viewport: (top, right, bottom, left)
542 * @method viewportRegion
543 @return {Object} Object literal containing the following about the visible region of the viewport: (top, right, bottom, left)
545 viewportRegion: function(node) {
546 node = node || Y.config.doc.documentElement;
552 scrollX = DOM.docScrollX(node);
553 scrollY = DOM.docScrollY(node);
555 ret = DOM._getRegion(scrollY, // top
556 DOM.winWidth(node) + scrollX, // right
557 scrollY + DOM.winHeight(node), // bottom
567 }, '3.0.0' ,{requires:['dom-base', 'dom-style']});