|
1 /* |
|
2 YUI 3.10.3 (build 2fb5187) |
|
3 Copyright 2013 Yahoo! Inc. All rights reserved. |
|
4 Licensed under the BSD License. |
|
5 http://yuilibrary.com/license/ |
|
6 */ |
|
7 |
|
8 YUI.add('node-screen', function (Y, NAME) { |
|
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 docWidth |
|
37 * @type {Int} |
|
38 */ |
|
39 'docWidth', |
|
40 |
|
41 /** |
|
42 * Document height |
|
43 * @config docHeight |
|
44 * @type {Int} |
|
45 */ |
|
46 'docHeight', |
|
47 |
|
48 /** |
|
49 * Pixel distance the page has been scrolled horizontally |
|
50 * @config docScrollX |
|
51 * @type {Int} |
|
52 */ |
|
53 'docScrollX', |
|
54 |
|
55 /** |
|
56 * Pixel distance the page has been scrolled vertically |
|
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 * Swaps the XY position of this node with another node. |
|
163 * @method swapXY |
|
164 * @param {Node | HTMLElement} otherNode The node to swap with. |
|
165 * @chainable |
|
166 */ |
|
167 'swapXY' |
|
168 ]); |
|
169 |
|
170 /** |
|
171 * @module node |
|
172 * @submodule node-screen |
|
173 */ |
|
174 |
|
175 /** |
|
176 * Returns a region object for the node |
|
177 * @config region |
|
178 * @for Node |
|
179 * @type Node |
|
180 */ |
|
181 Y.Node.ATTRS.region = { |
|
182 getter: function() { |
|
183 var node = this.getDOMNode(), |
|
184 region; |
|
185 |
|
186 if (node && !node.tagName) { |
|
187 if (node.nodeType === 9) { // document |
|
188 node = node.documentElement; |
|
189 } |
|
190 } |
|
191 if (Y.DOM.isWindow(node)) { |
|
192 region = Y.DOM.viewportRegion(node); |
|
193 } else { |
|
194 region = Y.DOM.region(node); |
|
195 } |
|
196 return region; |
|
197 } |
|
198 }; |
|
199 |
|
200 /** |
|
201 * Returns a region object for the node's viewport |
|
202 * @config viewportRegion |
|
203 * @type Node |
|
204 */ |
|
205 Y.Node.ATTRS.viewportRegion = { |
|
206 getter: function() { |
|
207 return Y.DOM.viewportRegion(Y.Node.getDOMNode(this)); |
|
208 } |
|
209 }; |
|
210 |
|
211 Y.Node.importMethod(Y.DOM, 'inViewportRegion'); |
|
212 |
|
213 // these need special treatment to extract 2nd node arg |
|
214 /** |
|
215 * Compares the intersection of the node with another node or region |
|
216 * @method intersect |
|
217 * @for Node |
|
218 * @param {Node|Object} node2 The node or region to compare with. |
|
219 * @param {Object} altRegion An alternate region to use (rather than this node's). |
|
220 * @return {Object} An object representing the intersection of the regions. |
|
221 */ |
|
222 Y.Node.prototype.intersect = function(node2, altRegion) { |
|
223 var node1 = Y.Node.getDOMNode(this); |
|
224 if (Y.instanceOf(node2, Y.Node)) { // might be a region object |
|
225 node2 = Y.Node.getDOMNode(node2); |
|
226 } |
|
227 return Y.DOM.intersect(node1, node2, altRegion); |
|
228 }; |
|
229 |
|
230 /** |
|
231 * Determines whether or not the node is within the giving region. |
|
232 * @method inRegion |
|
233 * @param {Node|Object} node2 The node or region to compare with. |
|
234 * @param {Boolean} all Whether or not all of the node must be in the region. |
|
235 * @param {Object} altRegion An alternate region to use (rather than this node's). |
|
236 * @return {Boolean} True if in region, false if not. |
|
237 */ |
|
238 Y.Node.prototype.inRegion = function(node2, all, altRegion) { |
|
239 var node1 = Y.Node.getDOMNode(this); |
|
240 if (Y.instanceOf(node2, Y.Node)) { // might be a region object |
|
241 node2 = Y.Node.getDOMNode(node2); |
|
242 } |
|
243 return Y.DOM.inRegion(node1, node2, all, altRegion); |
|
244 }; |
|
245 |
|
246 |
|
247 }, '3.10.3', {"requires": ["dom-screen", "node-base"]}); |