|
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('dd-scroll', function (Y, NAME) { |
|
9 |
|
10 |
|
11 /** |
|
12 * Base scroller class used to create the Plugin.DDNodeScroll and Plugin.DDWinScroll. |
|
13 * This class should not be called on it's own, it's designed to be a plugin. |
|
14 * @module dd |
|
15 * @submodule dd-scroll |
|
16 */ |
|
17 /** |
|
18 * Base scroller class used to create the Plugin.DDNodeScroll and Plugin.DDWinScroll. |
|
19 * This class should not be called on it's own, it's designed to be a plugin. |
|
20 * @class Scroll |
|
21 * @extends Base |
|
22 * @namespace DD |
|
23 * @constructor |
|
24 */ |
|
25 |
|
26 var S = function() { |
|
27 S.superclass.constructor.apply(this, arguments); |
|
28 |
|
29 }, |
|
30 WS, NS, |
|
31 HOST = 'host', |
|
32 BUFFER = 'buffer', |
|
33 PARENT_SCROLL = 'parentScroll', |
|
34 WINDOW_SCROLL = 'windowScroll', |
|
35 SCROLL_TOP = 'scrollTop', |
|
36 SCROLL_LEFT = 'scrollLeft', |
|
37 OFFSET_WIDTH = 'offsetWidth', |
|
38 OFFSET_HEIGHT = 'offsetHeight'; |
|
39 |
|
40 |
|
41 S.ATTRS = { |
|
42 /** |
|
43 * Internal config option to hold the node that we are scrolling. Should not be set by the developer. |
|
44 * @attribute parentScroll |
|
45 * @type Node |
|
46 */ |
|
47 parentScroll: { |
|
48 value: false, |
|
49 setter: function(node) { |
|
50 if (node) { |
|
51 return node; |
|
52 } |
|
53 return false; |
|
54 } |
|
55 }, |
|
56 /** |
|
57 * The number of pixels from the edge of the screen to turn on scrolling. Default: 30 |
|
58 * @attribute buffer |
|
59 * @type Number |
|
60 */ |
|
61 buffer: { |
|
62 value: 30, |
|
63 validator: Y.Lang.isNumber |
|
64 }, |
|
65 /** |
|
66 * The number of milliseconds delay to pass to the auto scroller. Default: 235 |
|
67 * @attribute scrollDelay |
|
68 * @type Number |
|
69 */ |
|
70 scrollDelay: { |
|
71 value: 235, |
|
72 validator: Y.Lang.isNumber |
|
73 }, |
|
74 /** |
|
75 * The host we are plugged into. |
|
76 * @attribute host |
|
77 * @type Object |
|
78 */ |
|
79 host: { |
|
80 value: null |
|
81 }, |
|
82 /** |
|
83 * Turn on window scroll support, default: false |
|
84 * @attribute windowScroll |
|
85 * @type Boolean |
|
86 */ |
|
87 windowScroll: { |
|
88 value: false, |
|
89 validator: Y.Lang.isBoolean |
|
90 }, |
|
91 /** |
|
92 * Allow vertical scrolling, default: true. |
|
93 * @attribute vertical |
|
94 * @type Boolean |
|
95 */ |
|
96 vertical: { |
|
97 value: true, |
|
98 validator: Y.Lang.isBoolean |
|
99 }, |
|
100 /** |
|
101 * Allow horizontal scrolling, default: true. |
|
102 * @attribute horizontal |
|
103 * @type Boolean |
|
104 */ |
|
105 horizontal: { |
|
106 value: true, |
|
107 validator: Y.Lang.isBoolean |
|
108 } |
|
109 }; |
|
110 |
|
111 Y.extend(S, Y.Base, { |
|
112 /** |
|
113 * Tells if we are actively scrolling or not. |
|
114 * @private |
|
115 * @property _scrolling |
|
116 * @type Boolean |
|
117 */ |
|
118 _scrolling: null, |
|
119 /** |
|
120 * Cache of the Viewport dims. |
|
121 * @private |
|
122 * @property _vpRegionCache |
|
123 * @type Object |
|
124 */ |
|
125 _vpRegionCache: null, |
|
126 /** |
|
127 * Cache of the dragNode dims. |
|
128 * @private |
|
129 * @property _dimCache |
|
130 * @type Object |
|
131 */ |
|
132 _dimCache: null, |
|
133 /** |
|
134 * Holder for the Timer object returned from Y.later. |
|
135 * @private |
|
136 * @property _scrollTimer |
|
137 * @type {Y.later} |
|
138 */ |
|
139 _scrollTimer: null, |
|
140 /** |
|
141 * Sets the _vpRegionCache property with an Object containing the dims from the viewport. |
|
142 * @private |
|
143 * @method _getVPRegion |
|
144 */ |
|
145 _getVPRegion: function() { |
|
146 var r = {}, |
|
147 n = this.get(PARENT_SCROLL), |
|
148 b = this.get(BUFFER), |
|
149 ws = this.get(WINDOW_SCROLL), |
|
150 xy = ((ws) ? [] : n.getXY()), |
|
151 w = ((ws) ? 'winWidth' : OFFSET_WIDTH), |
|
152 h = ((ws) ? 'winHeight' : OFFSET_HEIGHT), |
|
153 t = ((ws) ? n.get(SCROLL_TOP) : xy[1]), |
|
154 l = ((ws) ? n.get(SCROLL_LEFT) : xy[0]); |
|
155 |
|
156 r = { |
|
157 top: t + b, |
|
158 right: (n.get(w) + l) - b, |
|
159 bottom: (n.get(h) + t) - b, |
|
160 left: l + b |
|
161 }; |
|
162 this._vpRegionCache = r; |
|
163 return r; |
|
164 }, |
|
165 initializer: function() { |
|
166 var h = this.get(HOST); |
|
167 h.after('drag:start', Y.bind(this.start, this)); |
|
168 h.after('drag:end', Y.bind(this.end, this)); |
|
169 h.on('drag:align', Y.bind(this.align, this)); |
|
170 |
|
171 //TODO - This doesn't work yet?? |
|
172 Y.one('win').on('scroll', Y.bind(function() { |
|
173 this._vpRegionCache = null; |
|
174 }, this)); |
|
175 }, |
|
176 /** |
|
177 * Check to see if we need to fire the scroll timer. If scroll timer is running this will scroll the window. |
|
178 * @private |
|
179 * @method _checkWinScroll |
|
180 * @param {Boolean} move Should we move the window. From Y.later |
|
181 */ |
|
182 _checkWinScroll: function(move) { |
|
183 var r = this._getVPRegion(), |
|
184 ho = this.get(HOST), |
|
185 ws = this.get(WINDOW_SCROLL), |
|
186 xy = ho.lastXY, |
|
187 scroll = false, |
|
188 b = this.get(BUFFER), |
|
189 win = this.get(PARENT_SCROLL), |
|
190 sTop = win.get(SCROLL_TOP), |
|
191 sLeft = win.get(SCROLL_LEFT), |
|
192 w = this._dimCache.w, |
|
193 h = this._dimCache.h, |
|
194 bottom = xy[1] + h, |
|
195 top = xy[1], |
|
196 right = xy[0] + w, |
|
197 left = xy[0], |
|
198 nt = top, |
|
199 nl = left, |
|
200 st = sTop, |
|
201 sl = sLeft; |
|
202 |
|
203 if (this.get('horizontal')) { |
|
204 if (left <= r.left) { |
|
205 scroll = true; |
|
206 nl = xy[0] - ((ws) ? b : 0); |
|
207 sl = sLeft - b; |
|
208 } |
|
209 if (right >= r.right) { |
|
210 scroll = true; |
|
211 nl = xy[0] + ((ws) ? b : 0); |
|
212 sl = sLeft + b; |
|
213 } |
|
214 } |
|
215 if (this.get('vertical')) { |
|
216 if (bottom >= r.bottom) { |
|
217 scroll = true; |
|
218 nt = xy[1] + ((ws) ? b : 0); |
|
219 st = sTop + b; |
|
220 |
|
221 } |
|
222 if (top <= r.top) { |
|
223 scroll = true; |
|
224 nt = xy[1] - ((ws) ? b : 0); |
|
225 st = sTop - b; |
|
226 } |
|
227 } |
|
228 |
|
229 if (st < 0) { |
|
230 st = 0; |
|
231 nt = xy[1]; |
|
232 } |
|
233 |
|
234 if (sl < 0) { |
|
235 sl = 0; |
|
236 nl = xy[0]; |
|
237 } |
|
238 |
|
239 if (nt < 0) { |
|
240 nt = xy[1]; |
|
241 } |
|
242 if (nl < 0) { |
|
243 nl = xy[0]; |
|
244 } |
|
245 if (move) { |
|
246 ho.actXY = [nl, nt]; |
|
247 ho._alignNode([nl, nt], true); //We are srolling.. |
|
248 xy = ho.actXY; |
|
249 ho.actXY = [nl, nt]; |
|
250 ho._moveNode({ node: win, top: st, left: sl}); |
|
251 if (!st && !sl) { |
|
252 this._cancelScroll(); |
|
253 } |
|
254 } else { |
|
255 if (scroll) { |
|
256 this._initScroll(); |
|
257 } else { |
|
258 this._cancelScroll(); |
|
259 } |
|
260 } |
|
261 }, |
|
262 /** |
|
263 * Cancel a previous scroll timer and init a new one. |
|
264 * @private |
|
265 * @method _initScroll |
|
266 */ |
|
267 _initScroll: function() { |
|
268 this._cancelScroll(); |
|
269 this._scrollTimer = Y.Lang.later(this.get('scrollDelay'), this, this._checkWinScroll, [true], true); |
|
270 |
|
271 }, |
|
272 /** |
|
273 * Cancel a currently running scroll timer. |
|
274 * @private |
|
275 * @method _cancelScroll |
|
276 */ |
|
277 _cancelScroll: function() { |
|
278 this._scrolling = false; |
|
279 if (this._scrollTimer) { |
|
280 this._scrollTimer.cancel(); |
|
281 delete this._scrollTimer; |
|
282 } |
|
283 }, |
|
284 /** |
|
285 * Called from the drag:align event to determine if we need to scroll. |
|
286 * @method align |
|
287 */ |
|
288 align: function(e) { |
|
289 if (this._scrolling) { |
|
290 this._cancelScroll(); |
|
291 e.preventDefault(); |
|
292 } |
|
293 if (!this._scrolling) { |
|
294 this._checkWinScroll(); |
|
295 } |
|
296 }, |
|
297 /** |
|
298 * Set the cache of the dragNode dims. |
|
299 * @private |
|
300 * @method _setDimCache |
|
301 */ |
|
302 _setDimCache: function() { |
|
303 var node = this.get(HOST).get('dragNode'); |
|
304 this._dimCache = { |
|
305 h: node.get(OFFSET_HEIGHT), |
|
306 w: node.get(OFFSET_WIDTH) |
|
307 }; |
|
308 }, |
|
309 /** |
|
310 * Called from the drag:start event |
|
311 * @method start |
|
312 */ |
|
313 start: function() { |
|
314 this._setDimCache(); |
|
315 }, |
|
316 /** |
|
317 * Called from the drag:end event |
|
318 * @method end |
|
319 */ |
|
320 end: function() { |
|
321 this._dimCache = null; |
|
322 this._cancelScroll(); |
|
323 } |
|
324 }); |
|
325 |
|
326 Y.namespace('Plugin'); |
|
327 |
|
328 |
|
329 /** |
|
330 * Extends the Scroll class to make the window scroll while dragging. |
|
331 * @class DDWindowScroll |
|
332 * @extends Scroll |
|
333 * @namespace Plugin |
|
334 * @constructor |
|
335 */ |
|
336 WS = function() { |
|
337 WS.superclass.constructor.apply(this, arguments); |
|
338 }; |
|
339 WS.ATTRS = Y.merge(S.ATTRS, { |
|
340 /** |
|
341 * Turn on window scroll support, default: true |
|
342 * @attribute windowScroll |
|
343 * @type Boolean |
|
344 */ |
|
345 windowScroll: { |
|
346 value: true, |
|
347 setter: function(scroll) { |
|
348 if (scroll) { |
|
349 this.set(PARENT_SCROLL, Y.one('win')); |
|
350 } |
|
351 return scroll; |
|
352 } |
|
353 } |
|
354 }); |
|
355 Y.extend(WS, S, { |
|
356 //Shouldn't have to do this.. |
|
357 initializer: function() { |
|
358 this.set('windowScroll', this.get('windowScroll')); |
|
359 } |
|
360 }); |
|
361 /** |
|
362 * The Scroll instance will be placed on the Drag instance under the winscroll namespace. |
|
363 * @property NS |
|
364 * @default winscroll |
|
365 * @readonly |
|
366 * @protected |
|
367 * @static |
|
368 * @type {String} |
|
369 */ |
|
370 WS.NAME = WS.NS = 'winscroll'; |
|
371 Y.Plugin.DDWinScroll = WS; |
|
372 |
|
373 |
|
374 /** |
|
375 * Extends the Scroll class to make a parent node scroll while dragging. |
|
376 * @class DDNodeScroll |
|
377 * @extends Scroll |
|
378 * @namespace Plugin |
|
379 * @constructor |
|
380 */ |
|
381 NS = function() { |
|
382 NS.superclass.constructor.apply(this, arguments); |
|
383 |
|
384 }; |
|
385 NS.ATTRS = Y.merge(S.ATTRS, { |
|
386 /** |
|
387 * The node we want to scroll. Used to set the internal parentScroll attribute. |
|
388 * @attribute node |
|
389 * @type Node |
|
390 */ |
|
391 node: { |
|
392 value: false, |
|
393 setter: function(node) { |
|
394 var n = Y.one(node); |
|
395 if (!n) { |
|
396 if (node !== false) { |
|
397 Y.error('DDNodeScroll: Invalid Node Given: ' + node); |
|
398 } |
|
399 } else { |
|
400 this.set(PARENT_SCROLL, n); |
|
401 } |
|
402 return n; |
|
403 } |
|
404 } |
|
405 }); |
|
406 Y.extend(NS, S, { |
|
407 //Shouldn't have to do this.. |
|
408 initializer: function() { |
|
409 this.set('node', this.get('node')); |
|
410 } |
|
411 }); |
|
412 /** |
|
413 * The NodeScroll instance will be placed on the Drag instance under the nodescroll namespace. |
|
414 * @property NS |
|
415 * @default nodescroll |
|
416 * @readonly |
|
417 * @protected |
|
418 * @static |
|
419 * @type {String} |
|
420 */ |
|
421 NS.NAME = NS.NS = 'nodescroll'; |
|
422 Y.Plugin.DDNodeScroll = NS; |
|
423 |
|
424 Y.DD.Scroll = S; |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 }, '3.10.3', {"requires": ["dd-drag"]}); |