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