3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
7 YUI.add('node-screen', function(Y) {
10 * Extended Node interface for managing regions and screen positioning.
11 * Adds support for positioning elements and normalizes window size and scroll detection.
13 * @submodule node-screen
16 // these are all "safe" returns, no wrapping required
19 * Returns the inner width of the viewport (exludes scrollbar).
27 * Returns the inner height of the viewport (exludes scrollbar).
48 * Pixel distance the page has been scrolled horizontally
55 * Pixel distance the page has been scrolled vertically
62 Y.Node.ATTRS[name] = {
64 var args = Array.prototype.slice.call(arguments);
65 args.unshift(Y.Node.getDOMNode(this));
67 return Y.DOM[name].apply(this, args);
73 Y.Node.ATTRS.scrollLeft = {
75 var node = Y.Node.getDOMNode(this);
76 return ('scrollLeft' in node) ? node.scrollLeft : Y.DOM.docScrollX(node);
79 setter: function(val) {
80 var node = Y.Node.getDOMNode(this);
82 if ('scrollLeft' in node) {
83 node.scrollLeft = val;
84 } else if (node.document || node.nodeType === 9) {
85 Y.DOM._getWin(node).scrollTo(val, Y.DOM.docScrollY(node)); // scroll window if win or doc
92 Y.Node.ATTRS.scrollTop = {
94 var node = Y.Node.getDOMNode(this);
95 return ('scrollTop' in node) ? node.scrollTop : Y.DOM.docScrollY(node);
98 setter: function(val) {
99 var node = Y.Node.getDOMNode(this);
101 if ('scrollTop' in node) {
102 node.scrollTop = val;
103 } else if (node.document || node.nodeType === 9) {
104 Y.DOM._getWin(node).scrollTo(Y.DOM.docScrollX(node), val); // scroll window if win or doc
111 Y.Node.importMethod(Y.DOM, [
113 * Gets the current position of the node in page coordinates.
116 * @return {Array} The XY position of the node
121 * Set the position of the node in page coordinates, regardless of how the node is positioned.
123 * @param {Array} xy Contains X & Y values for new position (coordinates are page-based)
129 * Gets the current position of the node in page coordinates.
131 * @return {Int} The X position of the node
136 * Set the position of the node in page coordinates, regardless of how the node is positioned.
138 * @param {Int} x X value for new position (coordinates are page-based)
144 * Gets the current position of the node in page coordinates.
146 * @return {Int} The Y position of the node
151 * Set the position of the node in page coordinates, regardless of how the node is positioned.
153 * @param {Int} y Y value for new position (coordinates are page-based)
159 * Swaps the XY position of this node with another node.
161 * @param {Node | HTMLElement} otherNode The node to swap with.
169 * @submodule node-screen
173 * Returns a region object for the node
178 Y.Node.ATTRS.region = {
180 var node = this.getDOMNode(),
183 if (node && !node.tagName) {
184 if (node.nodeType === 9) { // document
185 node = node.documentElement;
188 if (Y.DOM.isWindow(node)) {
189 region = Y.DOM.viewportRegion(node);
191 region = Y.DOM.region(node);
198 * Returns a region object for the node's viewport
199 * @config viewportRegion
202 Y.Node.ATTRS.viewportRegion = {
204 return Y.DOM.viewportRegion(Y.Node.getDOMNode(this));
208 Y.Node.importMethod(Y.DOM, 'inViewportRegion');
210 // these need special treatment to extract 2nd node arg
212 * Compares the intersection of the node with another node or region
215 * @param {Node|Object} node2 The node or region to compare with.
216 * @param {Object} altRegion An alternate region to use (rather than this node's).
217 * @return {Object} An object representing the intersection of the regions.
219 Y.Node.prototype.intersect = function(node2, altRegion) {
220 var node1 = Y.Node.getDOMNode(this);
221 if (Y.instanceOf(node2, Y.Node)) { // might be a region object
222 node2 = Y.Node.getDOMNode(node2);
224 return Y.DOM.intersect(node1, node2, altRegion);
228 * Determines whether or not the node is within the giving region.
230 * @param {Node|Object} node2 The node or region to compare with.
231 * @param {Boolean} all Whether or not all of the node must be in the region.
232 * @param {Object} altRegion An alternate region to use (rather than this node's).
233 * @return {Object} An object representing the intersection of the regions.
235 Y.Node.prototype.inRegion = function(node2, all, altRegion) {
236 var node1 = Y.Node.getDOMNode(this);
237 if (Y.instanceOf(node2, Y.Node)) { // might be a region object
238 node2 = Y.Node.getDOMNode(node2);
240 return Y.DOM.inRegion(node1, node2, all, altRegion);
244 }, '3.5.0' ,{requires:['node-base', 'dom-screen']});