Merge branch 'MDL-32509' of git://github.com/danpoltawski/moodle
[moodle.git] / lib / yui / 3.5.0 / build / node-screen / node-screen.js
blobe307c317aa467ec5b4d7a65fd28925b99276f355
1 /*
2 YUI 3.5.0 (build 5089)
3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
7 YUI.add('node-screen', function(Y) {
9 /**
10  * Extended Node interface for managing regions and screen positioning.
11  * Adds support for positioning elements and normalizes window size and scroll detection. 
12  * @module node
13  * @submodule node-screen
14  */
16 // these are all "safe" returns, no wrapping required
17 Y.each([
18     /**
19      * Returns the inner width of the viewport (exludes scrollbar). 
20      * @config winWidth
21      * @for Node
22      * @type {Int}
23      */
24     'winWidth',
26     /**
27      * Returns the inner height of the viewport (exludes scrollbar). 
28      * @config winHeight
29      * @type {Int}
30      */
31     'winHeight',
33     /**
34      * Document width 
35      * @config winHeight
36      * @type {Int}
37      */
38     'docWidth',
40     /**
41      * Document height 
42      * @config docHeight
43      * @type {Int}
44      */
45     'docHeight',
47     /**
48      * Pixel distance the page has been scrolled horizontally 
49      * @config docScrollX
50      * @type {Int}
51      */
52     'docScrollX',
54     /**
55      * Pixel distance the page has been scrolled vertically 
56      * @config docScrollY
57      * @type {Int}
58      */
59     'docScrollY'
60     ],
61     function(name) {
62         Y.Node.ATTRS[name] = {
63             getter: function() {
64                 var args = Array.prototype.slice.call(arguments);
65                 args.unshift(Y.Node.getDOMNode(this));
67                 return Y.DOM[name].apply(this, args);
68             }
69         };
70     }
73 Y.Node.ATTRS.scrollLeft = {
74     getter: function() {
75         var node = Y.Node.getDOMNode(this);
76         return ('scrollLeft' in node) ? node.scrollLeft : Y.DOM.docScrollX(node);
77     },
79     setter: function(val) {
80         var node = Y.Node.getDOMNode(this);
81         if (node) {
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
86             }
87         } else {
88         }
89     }
92 Y.Node.ATTRS.scrollTop = {
93     getter: function() {
94         var node = Y.Node.getDOMNode(this);
95         return ('scrollTop' in node) ? node.scrollTop : Y.DOM.docScrollY(node);
96     },
98     setter: function(val) {
99         var node = Y.Node.getDOMNode(this);
100         if (node) {
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
105             }
106         } else {
107         }
108     }
111 Y.Node.importMethod(Y.DOM, [
113  * Gets the current position of the node in page coordinates. 
114  * @method getXY
115  * @for Node
116  * @return {Array} The XY position of the node
118     'getXY',
121  * Set the position of the node in page coordinates, regardless of how the node is positioned.
122  * @method setXY
123  * @param {Array} xy Contains X & Y values for new position (coordinates are page-based)
124  * @chainable
125  */
126     'setXY',
129  * Gets the current position of the node in page coordinates. 
130  * @method getX
131  * @return {Int} The X position of the node
133     'getX',
136  * Set the position of the node in page coordinates, regardless of how the node is positioned.
137  * @method setX
138  * @param {Int} x X value for new position (coordinates are page-based)
139  * @chainable
140  */
141     'setX',
144  * Gets the current position of the node in page coordinates. 
145  * @method getY
146  * @return {Int} The Y position of the node
148     'getY',
151  * Set the position of the node in page coordinates, regardless of how the node is positioned.
152  * @method setY
153  * @param {Int} y Y value for new position (coordinates are page-based)
154  * @chainable
155  */
156     'setY',
159  * Swaps the XY position of this node with another node. 
160  * @method swapXY
161  * @param {Node | HTMLElement} otherNode The node to swap with.
162  * @chainable
163  */
164     'swapXY'
168  * @module node
169  * @submodule node-screen
170  */
173  * Returns a region object for the node
174  * @config region
175  * @for Node
176  * @type Node
177  */
178 Y.Node.ATTRS.region = {
179     getter: function() {
180         var node = this.getDOMNode(),
181             region;
183         if (node && !node.tagName) {
184             if (node.nodeType === 9) { // document
185                 node = node.documentElement;
186             }
187         }
188         if (Y.DOM.isWindow(node)) {
189             region = Y.DOM.viewportRegion(node);
190         } else {
191             region = Y.DOM.region(node);
192         }
193         return region;
194     }
198  * Returns a region object for the node's viewport
199  * @config viewportRegion
200  * @type Node
201  */
202 Y.Node.ATTRS.viewportRegion = {
203     getter: function() {
204         return Y.DOM.viewportRegion(Y.Node.getDOMNode(this));
205     }
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
213  * @method intersect
214  * @for Node
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.
218  */
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);
223     }
224     return Y.DOM.intersect(node1, node2, altRegion);
228  * Determines whether or not the node is within the giving region.
229  * @method inRegion
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.
234  */
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);
239     }
240     return Y.DOM.inRegion(node1, node2, all, altRegion);
244 }, '3.5.0' ,{requires:['node-base', 'dom-screen']});