|
1 /* |
|
2 Copyright (c) 2009, Yahoo! Inc. All rights reserved. |
|
3 Code licensed under the BSD License: |
|
4 http://developer.yahoo.net/yui/license.txt |
|
5 version: 3.0.0b1 |
|
6 build: 1163 |
|
7 */ |
|
8 YUI.add('node-screen', function(Y) { |
|
9 |
|
10 /** |
|
11 * Extended Node interface for managing regions and screen positioning. |
|
12 * Adds support for positioning elements and normalizes window size and scroll detection. |
|
13 * @module node |
|
14 * @submodule node-screen |
|
15 */ |
|
16 |
|
17 // these are all "safe" returns, no wrapping required |
|
18 Y.each([ |
|
19 /** |
|
20 * Returns the inner width of the viewport (exludes scrollbar). |
|
21 * @config winWidth |
|
22 * @for Node |
|
23 * @type {Int} |
|
24 */ |
|
25 'winWidth', |
|
26 |
|
27 /** |
|
28 * Returns the inner height of the viewport (exludes scrollbar). |
|
29 * @config winHeight |
|
30 * @type {Int} |
|
31 */ |
|
32 'winHeight', |
|
33 |
|
34 /** |
|
35 * Document width |
|
36 * @config winHeight |
|
37 * @type {Int} |
|
38 */ |
|
39 'docWidth', |
|
40 |
|
41 /** |
|
42 * Document height |
|
43 * @config docHeight |
|
44 * @type {Int} |
|
45 */ |
|
46 'docHeight', |
|
47 |
|
48 /** |
|
49 * Amount page has been scroll vertically |
|
50 * @config docScrollX |
|
51 * @type {Int} |
|
52 */ |
|
53 'docScrollX', |
|
54 |
|
55 /** |
|
56 * Amount page has been scroll horizontally |
|
57 * @config docScrollY |
|
58 * @type {Int} |
|
59 */ |
|
60 'docScrollY' |
|
61 ], |
|
62 function(name) { |
|
63 Y.Node.ATTRS[name] = { |
|
64 getter: function() { |
|
65 var args = Array.prototype.slice.call(arguments); |
|
66 args.unshift(Y.Node.getDOMNode(this)); |
|
67 |
|
68 return Y.DOM[name].apply(this, args); |
|
69 } |
|
70 }; |
|
71 } |
|
72 ); |
|
73 |
|
74 Y.Node.ATTRS.scrollLeft = { |
|
75 getter: function() { |
|
76 var node = Y.Node.getDOMNode(this); |
|
77 return ('scrollLeft' in node) ? node.scrollLeft : Y.DOM.docScrollX(node); |
|
78 }, |
|
79 |
|
80 setter: function(val) { |
|
81 var node = Y.Node.getDOMNode(this); |
|
82 if (node) { |
|
83 if ('scrollLeft' in node) { |
|
84 node.scrollLeft = val; |
|
85 } else if (node.document || node.nodeType === 9) { |
|
86 Y.DOM._getWin(node).scrollTo(val, Y.DOM.docScrollY(node)); // scroll window if win or doc |
|
87 } |
|
88 } else { |
|
89 Y.log('unable to set scrollLeft for ' + node, 'error', 'Node'); |
|
90 } |
|
91 } |
|
92 }; |
|
93 |
|
94 Y.Node.ATTRS.scrollTop = { |
|
95 getter: function() { |
|
96 var node = Y.Node.getDOMNode(this); |
|
97 return ('scrollTop' in node) ? node.scrollTop : Y.DOM.docScrollY(node); |
|
98 }, |
|
99 |
|
100 setter: function(val) { |
|
101 var node = Y.Node.getDOMNode(this); |
|
102 if (node) { |
|
103 if ('scrollTop' in node) { |
|
104 node.scrollTop = val; |
|
105 } else if (node.document || node.nodeType === 9) { |
|
106 Y.DOM._getWin(node).scrollTo(Y.DOM.docScrollX(node), val); // scroll window if win or doc |
|
107 } |
|
108 } else { |
|
109 Y.log('unable to set scrollTop for ' + node, 'error', 'Node'); |
|
110 } |
|
111 } |
|
112 }; |
|
113 |
|
114 Y.Node.importMethod(Y.DOM, [ |
|
115 /** |
|
116 * Gets the current position of the node in page coordinates. |
|
117 * @method getXY |
|
118 * @for Node |
|
119 * @return {Array} The XY position of the node |
|
120 */ |
|
121 'getXY', |
|
122 |
|
123 /** |
|
124 * Set the position of the node in page coordinates, regardless of how the node is positioned. |
|
125 * @method setXY |
|
126 * @param {Array} xy Contains X & Y values for new position (coordinates are page-based) |
|
127 * @chainable |
|
128 */ |
|
129 'setXY', |
|
130 |
|
131 /** |
|
132 * Gets the current position of the node in page coordinates. |
|
133 * @method getX |
|
134 * @return {Int} The X position of the node |
|
135 */ |
|
136 'getX', |
|
137 |
|
138 /** |
|
139 * Set the position of the node in page coordinates, regardless of how the node is positioned. |
|
140 * @method setX |
|
141 * @param {Int} x X value for new position (coordinates are page-based) |
|
142 * @chainable |
|
143 */ |
|
144 'setX', |
|
145 |
|
146 /** |
|
147 * Gets the current position of the node in page coordinates. |
|
148 * @method getY |
|
149 * @return {Int} The Y position of the node |
|
150 */ |
|
151 'getY', |
|
152 |
|
153 /** |
|
154 * Set the position of the node in page coordinates, regardless of how the node is positioned. |
|
155 * @method setY |
|
156 * @param {Int} y Y value for new position (coordinates are page-based) |
|
157 * @chainable |
|
158 */ |
|
159 'setY' |
|
160 ]); |
|
161 |
|
162 /** |
|
163 * Returns a region object for the node |
|
164 * @config region |
|
165 * @for Node |
|
166 * @type Node |
|
167 */ |
|
168 Y.Node.ATTRS.region = { |
|
169 getter: function() { |
|
170 var node = Y.Node.getDOMNode(this); |
|
171 if (node && !node.tagName) { |
|
172 if (node.nodeType === 9) { // document |
|
173 node = node.documentElement; |
|
174 } else if (node.alert) { // window |
|
175 node = node.document.documentElement; |
|
176 } |
|
177 } |
|
178 return Y.DOM.region(node); |
|
179 } |
|
180 }; |
|
181 |
|
182 /** |
|
183 * Returns a region object for the node's viewport |
|
184 * @config viewportRegion |
|
185 * @type Node |
|
186 */ |
|
187 Y.Node.ATTRS.viewportRegion = { |
|
188 getter: function() { |
|
189 return Y.DOM.viewportRegion(Y.Node.getDOMNode(this)); |
|
190 } |
|
191 }; |
|
192 |
|
193 Y.Node.importMethod(Y.DOM, 'inViewportRegion'); |
|
194 |
|
195 // these need special treatment to extract 2nd node arg |
|
196 /** |
|
197 * Compares the intersection of the node with another node or region |
|
198 * @method intersect |
|
199 * @for Node |
|
200 * @param {Node|Object} node2 The node or region to compare with. |
|
201 * @param {Object} altRegion An alternate region to use (rather than this node's). |
|
202 * @return {Object} An object representing the intersection of the regions. |
|
203 */ |
|
204 Y.Node.prototype.intersect = function(node2, altRegion) { |
|
205 var node1 = Y.Node.getDOMNode(this); |
|
206 if (node2 instanceof Y.Node) { // might be a region object |
|
207 node2 = Y.Node.getDOMNode(node2); |
|
208 } |
|
209 return Y.DOM.intersect(node1, node2, altRegion); |
|
210 }; |
|
211 |
|
212 /** |
|
213 * Determines whether or not the node is within the giving region. |
|
214 * @method inRegion |
|
215 * @param {Node|Object} node2 The node or region to compare with. |
|
216 * @param {Boolean} all Whether or not all of the node must be in the region. |
|
217 * @param {Object} altRegion An alternate region to use (rather than this node's). |
|
218 * @return {Object} An object representing the intersection of the regions. |
|
219 */ |
|
220 Y.Node.prototype.inRegion = function(node2, all, altRegion) { |
|
221 var node1 = Y.Node.getDOMNode(this); |
|
222 if (node2 instanceof Y.Node) { // might be a region object |
|
223 node2 = Y.Node.getDOMNode(node2); |
|
224 } |
|
225 return Y.DOM.inRegion(node1, node2, all, altRegion); |
|
226 }; |
|
227 |
|
228 |
|
229 }, '3.0.0b1' ,{requires:['dom-screen']}); |