|
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 } |
|
83 } |
|
84 }; |
|
85 |
|
86 Y.Node.ATTRS.scrollTop = { |
|
87 getter: function() { |
|
88 var node = Y.Node.getDOMNode(this); |
|
89 return ('scrollTop' in node) ? node.scrollTop : Y.DOM.docScrollY(node); |
|
90 }, |
|
91 |
|
92 setter: function(val) { |
|
93 var node = Y.Node.getDOMNode(this); |
|
94 if (node) { |
|
95 if ('scrollTop' in node) { |
|
96 node.scrollTop = val; |
|
97 } else if (node.document || node.nodeType === 9) { |
|
98 Y.DOM._getWin(node).scrollTo(Y.DOM.docScrollX(node), val); // scroll window if win or doc |
|
99 } |
|
100 } else { |
|
101 } |
|
102 } |
|
103 }; |
|
104 |
|
105 Y.Node.importMethod(Y.DOM, [ |
|
106 /** |
|
107 * Gets the current position of the node in page coordinates. |
|
108 * @method getXY |
|
109 * @for Node |
|
110 * @return {Array} The XY position of the node |
|
111 */ |
|
112 'getXY', |
|
113 |
|
114 /** |
|
115 * Set the position of the node in page coordinates, regardless of how the node is positioned. |
|
116 * @method setXY |
|
117 * @param {Array} xy Contains X & Y values for new position (coordinates are page-based) |
|
118 * @chainable |
|
119 */ |
|
120 'setXY', |
|
121 |
|
122 /** |
|
123 * Gets the current position of the node in page coordinates. |
|
124 * @method getX |
|
125 * @return {Number} The X position of the node |
|
126 */ |
|
127 'getX', |
|
128 |
|
129 /** |
|
130 * Set the position of the node in page coordinates, regardless of how the node is positioned. |
|
131 * @method setX |
|
132 * @param {Number} x X value for new position (coordinates are page-based) |
|
133 * @chainable |
|
134 */ |
|
135 'setX', |
|
136 |
|
137 /** |
|
138 * Gets the current position of the node in page coordinates. |
|
139 * @method getY |
|
140 * @return {Number} The Y position of the node |
|
141 */ |
|
142 'getY', |
|
143 |
|
144 /** |
|
145 * Set the position of the node in page coordinates, regardless of how the node is positioned. |
|
146 * @method setY |
|
147 * @param {Number} y Y value for new position (coordinates are page-based) |
|
148 * @chainable |
|
149 */ |
|
150 'setY', |
|
151 |
|
152 /** |
|
153 * Swaps the XY position of this node with another node. |
|
154 * @method swapXY |
|
155 * @param {Node | HTMLElement} otherNode The node to swap with. |
|
156 * @chainable |
|
157 */ |
|
158 'swapXY' |
|
159 ]); |
|
160 |
|
161 /** |
|
162 * @module node |
|
163 * @submodule node-screen |
|
164 */ |
|
165 |
|
166 /** |
|
167 * Returns a region object for the node |
|
168 * @config region |
|
169 * @for Node |
|
170 * @type Node |
|
171 */ |
|
172 Y.Node.ATTRS.region = { |
|
173 getter: function() { |
|
174 var node = this.getDOMNode(), |
|
175 region; |
|
176 |
|
177 if (node && !node.tagName) { |
|
178 if (node.nodeType === 9) { // document |
|
179 node = node.documentElement; |
|
180 } |
|
181 } |
|
182 if (Y.DOM.isWindow(node)) { |
|
183 region = Y.DOM.viewportRegion(node); |
|
184 } else { |
|
185 region = Y.DOM.region(node); |
|
186 } |
|
187 return region; |
|
188 } |
|
189 }; |
|
190 |
|
191 /** |
|
192 * Returns a region object for the node's viewport |
|
193 * @config viewportRegion |
|
194 * @type Node |
|
195 */ |
|
196 Y.Node.ATTRS.viewportRegion = { |
|
197 getter: function() { |
|
198 return Y.DOM.viewportRegion(Y.Node.getDOMNode(this)); |
|
199 } |
|
200 }; |
|
201 |
|
202 Y.Node.importMethod(Y.DOM, 'inViewportRegion'); |
|
203 |
|
204 // these need special treatment to extract 2nd node arg |
|
205 /** |
|
206 * Compares the intersection of the node with another node or region |
|
207 * @method intersect |
|
208 * @for Node |
|
209 * @param {Node|Object} node2 The node or region to compare with. |
|
210 * @param {Object} altRegion An alternate region to use (rather than this node's). |
|
211 * @return {Object} An object representing the intersection of the regions. |
|
212 */ |
|
213 Y.Node.prototype.intersect = function(node2, altRegion) { |
|
214 var node1 = Y.Node.getDOMNode(this); |
|
215 if (Y.instanceOf(node2, Y.Node)) { // might be a region object |
|
216 node2 = Y.Node.getDOMNode(node2); |
|
217 } |
|
218 return Y.DOM.intersect(node1, node2, altRegion); |
|
219 }; |
|
220 |
|
221 /** |
|
222 * Determines whether or not the node is within the giving region. |
|
223 * @method inRegion |
|
224 * @param {Node|Object} node2 The node or region to compare with. |
|
225 * @param {Boolean} all Whether or not all of the node must be in the region. |
|
226 * @param {Object} altRegion An alternate region to use (rather than this node's). |
|
227 * @return {Boolean} True if in region, false if not. |
|
228 */ |
|
229 Y.Node.prototype.inRegion = function(node2, all, altRegion) { |
|
230 var node1 = Y.Node.getDOMNode(this); |
|
231 if (Y.instanceOf(node2, Y.Node)) { // might be a region object |
|
232 node2 = Y.Node.getDOMNode(node2); |
|
233 } |
|
234 return Y.DOM.inRegion(node1, node2, all, altRegion); |
|
235 }; |
|
236 |
|
237 |
|
238 }, '@VERSION@', {"requires": ["dom-screen", "node-base"]}); |