3 function X_UI_ScrollBox_momentum( current, start, time, lowerMargin, wrapperSize, deceleration ){
\r
4 var distance = current - start,
\r
5 speed = Math.abs( distance ) / time,
\r
9 deceleration = deceleration === undefined ? 0.0006 : deceleration;
\r
11 destination = current + ( speed * speed ) / ( 2 * deceleration ) * ( distance < 0 ? -1 : 1 );
\r
12 duration = speed / deceleration;
\r
14 if( destination < lowerMargin ){
\r
15 destination = wrapperSize ? lowerMargin - ( wrapperSize / 2.5 * ( speed / 8 ) ) : lowerMargin;
\r
16 distance = Math.abs( destination - current );
\r
17 duration = distance / speed;
\r
19 if ( destination > 0 ) {
\r
20 destination = wrapperSize ? wrapperSize / 2.5 * ( speed / 8 ) : 0;
\r
21 distance = Math.abs( current ) + destination;
\r
22 duration = distance / speed;
\r
26 destination : Math.round( destination ),
\r
31 var X_UI_ScrollBox_SUPPORT_ATTRS = {
\r
32 // スクロール開始するために必要な移動距離、縦か横、どちらか制限する場合、より重要
\r
33 directionLockThreshold : [ 10, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.LENGTH ],
\r
34 scrollXEnabled : [ true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],
\r
35 scrollYEnabled : [ true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],
\r
36 scrollEnabled : [ true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],
\r
37 bounceEnabled : [ true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],
\r
38 bounceTime : [ 600, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.TIME ],
\r
39 useWheel : [ true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],
\r
40 useKey : [ true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],
\r
41 hasScrollShadow : [ true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],
\r
42 scrollShadowColor : [ '#000', XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.COLOR ]
\r
45 var XUI_ScrollBox = XUI_ChromeBox.inherits(
\r
49 directionLockThreshold : 10,
\r
50 scrollXEnabled : true,
\r
51 scrollYEnabled : true,
\r
52 scrollEnabled : true,
\r
53 bounceEnabled : true,
\r
54 momentumEnabled : true,
\r
58 hasScrollShadow : true,
\r
59 scrollShadowColor : '#000',
\r
61 supportAttrs : XUI_Attr_createAttrDef( XUI_Attr_Support, X_UI_ScrollBox_SUPPORT_ATTRS ),
\r
67 directionLocked : '',
\r
70 isAnimating : false,
\r
71 isInTransition : false,
\r
84 scrollXMax : 0, // px
\r
85 scrollYMax : 0, // px
\r
86 scrollXRatio : 0, // この値は scroll 不要になっても保持される。 scroll 必要時に参照される
\r
90 absStartX : 0, // px
\r
91 absStartY : 0, // px
\r
96 directionX : 0, // -1, 0, 1
\r
97 directionY : 0, // -1, 0, 1
\r
99 lastScrollWidth : 0,
\r
100 lastScrollHeight : 0,
\r
104 _containerNode : null,
\r
105 xnodeSlider : null,
\r
107 Constructor : function( user, layout, args ){
\r
108 this[ 'Super' ]( user, layout, args );
\r
109 this._containerNode = X_Pair_get( this.containerNode );
\r
110 this.xnodeSlider = this._containerNode.xnode[ 'className' ]( 'ScrollSlider' ).listen( X_EVENT_ANIME_END, this, X_UI_ScrollBox_onAnimeEnd );
\r
111 this.xnode[ 'className' ]( 'ScrollBox' );
\r
114 creationComplete : function(){
\r
115 XUI_Box.prototype.creationComplete.apply( this, arguments );
\r
118 calculate : function(){
\r
119 this.lastScrollWidth = this._containerNode.boxWidth;
\r
120 this.lastScrollHeight = this._containerNode.boxHeight;
\r
121 this.lastBoxWidth = this.boxWidth;
\r
122 this.lastBoxHeight = this.boxHeight;
\r
124 XUI_Box.prototype.calculate.apply( this, arguments );
\r
126 // TODO root の layout_complete 後に。
\r
127 // TODO calculate 前に scroll の解放。
\r
130 this.lastScrollWidth !== this._containerNode.boxWidth ||
\r
131 this.lastScrollHeight !== this._containerNode.boxHeight ||
\r
132 this.lastBoxWidth !== this.boxWidth || this.lastBoxHeight !== this.boxHeight
\r
134 X_UI_rootData[ 'listenOnce' ]( XUI_Event.LAYOUT_COMPLETE, this, X_UI_ScrollBox_onLayoutComplete );
\r
138 scrollTo : function( x, y, opt_time, opt_easing, opt_release ){
\r
139 //if( this.scrollX === x && this.scrollY === y ) return;
\r
141 opt_time = 0 <= opt_time ? opt_time : 0;
\r
142 opt_easing = opt_easing || 'circular';
\r
143 opt_release = 0 <= opt_release ? opt_release : 300;
\r
145 this.isInTransition = 0 < opt_time;
\r
147 X_UI_ScrollBox_translate( this, x, y, opt_time, opt_easing, opt_release );
\r
150 _remove : function(){
\r
151 XUI_AbstractUINode.prototype._remove.apply( this, arguments );
\r
152 if( this.scrolling ){
\r
160 function X_UI_ScrollBox_onLayoutBefore( e ){
\r
161 if( e[ 'cancelable' ] && this.isInTransition && X_Node_Anime_translateZ ){
\r
162 this[ 'listenOnce' ]( XUI_Event.SCROLL_END, X_UI_rootData, X_UI_rootData.calculate );
\r
163 return X_Callback_PREVENT_DEFAULT;
\r
165 this.scrollXRatio = this.scrollX ? this.scrollXMax / this.scrollX : 0;
\r
166 this.scrollYRatio = this.scrollY ? this.scrollYMax / this.scrollY : 0;
\r
167 this.xnodeSlider.stop();
\r
168 this.isInTransition = false;
\r
169 return X_Callback_NONE;
\r
172 function X_UI_ScrollBox_onLayoutComplete( e ){
\r
173 // scroll の停止、GPU の解除
\r
174 var font = this.fontSize = this.xnodeSlider.call( 'fontSize' );
\r
176 this.scrollXMax = ( this.boxWidth - this._containerNode.boxWidth ) * font;
\r
177 this.scrollYMax = ( this.boxHeight - this._containerNode.boxHeight ) * font;
\r
179 this.hasHScroll = this.scrollXEnabled && this.scrollXMax < 0;
\r
180 this.hasVScroll = this.scrollYEnabled && this.scrollYMax < 0;
\r
182 if( !this.hasHScroll ){
\r
183 this.scrollXMax = 0;
\r
186 if( !this.hasVScroll ){
\r
187 this.scrollYMax = 0;
\r
190 delete this.endTime;
\r
191 delete this.directionX;
\r
192 delete this.directionY;
\r
194 X_UI_ScrollBox_resetPosition( this, 0 );
\r
196 if( this.hasHScroll || this.hasVScroll ){
\r
198 if( this.scrolling ){
\r
199 X_UI_ScrollBox_translate( this, this.scrollXMax * this.scrollXRatio, this.scrollYMax * this.scrollYRatio, 100, '', 300 );
\r
202 this[ 'listen' ]( XUI_Event._POINTER_DOWN, this, X_UI_ScrollBox_onStart );
\r
203 X_UI_rootData[ 'listen' ]( XUI_Event.LAYOUT_BEFORE, this, X_UI_ScrollBox_onLayoutBefore );
\r
205 X_UI_ScrollBox_translate( this, this.scrollXMax * this.scrollXRatio, this.scrollYMax * this.scrollYRatio, 100, '', 300 );
\r
206 this.scrolling = true;
\r
210 if( this.scrolling ){
\r
212 this[ 'unlisten' ]( XUI_Event._POINTER_DOWN, this, X_UI_ScrollBox_onStart );
\r
213 X_UI_rootData[ 'unlisten' ]( XUI_Event.LAYOUT_BEFORE, this, X_UI_ScrollBox_onLayoutBefore );
\r
215 ( this.scrollX !== 0 || this.scrollY !== 0 ) && X_UI_ScrollBox_translate( this, 0, 0, 100, '', 300 );
\r
217 delete this.scrolling;
\r
218 delete this.scrollXRatio;
\r
219 delete this.scrollYRatio;
\r
223 // TODO use scrollLeft, scrollTop
\r
224 function X_UI_ScrollBox_translate( that, x, y, opt_time, opt_easing, opt_release ){
\r
226 opt_time = 0 <= opt_time ? opt_time : 0;
\r
227 opt_easing = opt_easing === '' ? '' : opt_easing || 'circular';
\r
228 opt_release = 0 <= opt_release ? opt_release : 300;
\r
230 that.xnodeSlider.animate(
\r
239 opt_time, opt_easing, opt_release
\r
245 if( that.indicators ){
\r
246 for( i = that.indicators.length; i--; ){
\r
247 that.indicators[ i ].updatePosition();
\r
252 function X_UI_ScrollBox_onStart( e ){
\r
253 var ret = X_Callback_NONE;
\r
255 // React to left mouse button only
\r
256 if( e.pointerType === 'mouse' && e.button !== 0 ){
\r
260 if( !this.scrollEnabled || ( this.initiated && e.pointerType !== this.initiated ) ){
\r
264 this.initiated = e.pointerType;
\r
265 this.moved = false;
\r
268 this.directionX = 0;
\r
269 this.directionY = 0;
\r
270 this.directionLocked = '';
\r
271 this.startTime = X_Timer_now();
\r
274 if( this.isInTransition || this.isAnimating ){
\r
275 this.isInTransition = this.isAnimating = false;
\r
276 this[ 'dispatch' ]( XUI_Event.SCROLL_END );
\r
277 this.xnodeSlider.stop();
\r
280 this.startX = this.scrollX;
\r
281 this.startY = this.scrollY;
\r
282 this.absStartX = this.scrollX;
\r
283 this.absStartY = this.scrollY;
\r
284 this.pointX = e.pageX;
\r
285 this.pointY = e.pageY;
\r
287 console.log( 'scrollstart ' + e.pageY );
\r
289 this[ 'listen' ]( XUI_Event._POINTER_MOVE, this, X_UI_ScrollBox_onMove );
\r
290 this[ 'listen' ]( [ XUI_Event._POINTER_UP, XUI_Event._POINTER_CANCEL ], this, X_UI_ScrollBox_onEnd );
\r
292 return ret | X_Callback_PREVENT_DEFAULT;
\r
295 function X_UI_ScrollBox_onMove( e ){
\r
296 var ret = X_Callback_NONE,
\r
297 deltaX, deltaY, timestamp,
\r
299 absDistX, absDistY;
\r
300 // 規定以上の move でスクロール開始
\r
302 console.log( 'scrollmove ' + e.buttons + ' ' + e.button );
\r
304 if( !this.scrollEnabled || e.pointerType !== this.initiated ){
\r
308 if( e.buttons !== 1 ){
\r
309 return X_UI_ScrollBox_onEnd.call( this, e );
\r
313 if( !this.xnodeSlider[ '_anime' ] ){
\r
314 //console.log( 'gpuレイヤーの用意 ' + e.pageY );
\r
315 //console.log( 'mov1 x:' + this.scrollX + ' y:' + this.scrollY );
\r
316 X_UI_ScrollBox_translate( this, this.scrollX, this.scrollY, 0, '', 300 );
\r
320 deltaX = e.pageX - this.pointX;
\r
321 deltaY = e.pageY - this.pointY;
\r
322 timestamp = X_Timer_now();
\r
324 this.pointX = e.pageX;
\r
325 this.pointY = e.pageY;
\r
327 this.distX += deltaX;
\r
328 this.distY += deltaY;
\r
329 absDistX = Math.abs(this.distX);
\r
330 absDistY = Math.abs(this.distY);
\r
332 // We need to move at least 10 pixels for the scrolling to initiate
\r
333 if( 300 < timestamp - this.endTime && ( absDistX < 10 && absDistY < 10 ) ){
\r
337 // If you are scrolling in one direction lock the other
\r
338 if( !this.directionLocked ){
\r
339 if( absDistX > absDistY + this.directionLockThreshold ){
\r
340 this.directionLocked = 'h'; // lock horizontally
\r
342 if( absDistY >= absDistX + this.directionLockThreshold ){
\r
343 this.directionLocked = 'v'; // lock vertically
\r
345 this.directionLocked = 'n'; // no lock
\r
349 if( this.directionLocked === 'h' ){
\r
352 if( this.directionLocked === 'v' ){
\r
356 deltaX = this.hasHScroll ? deltaX : 0;
\r
357 deltaY = this.hasVScroll ? deltaY : 0;
\r
360 this[ 'dispatch' ]( XUI_Event.SCROLL_BEFORE_MOVE );
\r
362 this.minusX = deltaX;
\r
363 this.minusY = deltaY;
\r
365 this[ 'dispatch' ]( XUI_Event.SCROLL_MOVE );
\r
368 newX = this.scrollX + deltaX;// - this.minusX;
\r
369 newY = this.scrollY + deltaY;// - this.minusY;
\r
371 // Slow down if outside of the boundaries
\r
372 if( 0 < newX || newX < this.scrollXMax ){
\r
373 newX = this.bounceEnabled ? this.scrollX + ( deltaX ) / 3 : 0 < newX ? 0 : this.scrollXMax;
\r
376 if( 0 < newY || newY < this.scrollYMax ){
\r
377 //console.log( 'slow... ' + newY + ' ' + this.scrollYMax );
\r
378 newY = this.bounceEnabled ? this.scrollY + ( deltaY ) / 3 : 0 < newY ? 0 : this.scrollYMax;
\r
381 this.directionX = 0 < deltaX ? -1 : deltaX < 0 ? 1 : 0;
\r
382 this.directionY = 0 < deltaY ? -1 : deltaY < 0 ? 1 : 0;
\r
384 //console.log( 'mov2 x:' + newX + ' y:' + newY );
\r
385 X_UI_ScrollBox_translate( this, newX, newY, 0, '', 300 );
\r
387 if( 300 < timestamp - this.startTime ){
\r
388 this.startTime = timestamp;
\r
389 this.startX = this.scrollX;
\r
390 this.startY = this.scrollY;
\r
393 return ret | X_Callback_PREVENT_DEFAULT | X_Callback_CAPTURE_POINTER;
\r
396 function X_UI_ScrollBox_onEnd( e ){
\r
397 var ret = X_Callback_NONE,
\r
401 momentumX, momentumY,
\r
402 duration, distanceX, distanceY;
\r
404 this[ 'unlisten' ]( XUI_Event._POINTER_MOVE, this, X_UI_ScrollBox_onMove );
\r
405 this[ 'unlisten' ]( [ XUI_Event._POINTER_UP, XUI_Event._POINTER_CANCEL ], this, X_UI_ScrollBox_onEnd );
\r
407 if( !this.scrollEnabled || e.pointerType !== this.initiated ){
\r
411 delete this.isInTransition;
\r
412 delete this.initiated;
\r
413 this.endTime = X_Timer_now();
\r
415 duration = this.endTime - this.startTime;
\r
416 newX = Math.round( this.scrollX );
\r
417 newY = Math.round( this.scrollY );
\r
418 distanceX = Math.abs(newX - this.startX);
\r
419 distanceY = Math.abs(newY - this.startY);
\r
421 // reset if we are outside of the boundaries
\r
422 if( X_UI_ScrollBox_resetPosition( this, this.bounceTime ) ){
\r
426 // we scrolled less than 10 pixels
\r
428 // this[ 'dispatch' ]( X_EVENT_CANCELED );
\r
429 console.log( 'we scrolled less than 10 pixels ' + e.pageY );
\r
433 // start momentum animation if needed
\r
434 if( this.momentumEnabled && duration < 300 ){
\r
435 momentumX = this.hasHScroll ?
\r
436 X_UI_ScrollBox_momentum( this.scrollX, this.startX, duration, this.scrollXMax, this.bounceEnabled ? this.boxWidth * this.fontSize : 0, this.deceleration ) :
\r
437 { destination: newX, duration: 0 };
\r
438 momentumY = this.hasVScroll ?
\r
439 X_UI_ScrollBox_momentum( this.scrollY, this.startY, duration, this.scrollYMax, this.bounceEnabled ? this.boxHeight * this.fontSize : 0, this.deceleration ) :
\r
440 { destination: newY, duration: 0 };
\r
441 newX = momentumX.destination;
\r
442 newY = momentumY.destination;
\r
443 time = Math.max( momentumX.duration, momentumY.duration ) | 0;
\r
444 this.isInTransition = true;
\r
446 console.log( '慣性無し' );
\r
449 if( newX != this.scrollX || newY != this.scrollY ){
\r
450 // change easing function when scroller goes out of the boundaries
\r
451 if( 0 < newX || newX < this.scrollXMax || 0 < newY || newY < this.scrollYMax ){
\r
452 easing = 'quadratic';
\r
455 console.log( 'end2 x:' + newX + ' y:' + newY + ' t:' + time );
\r
456 this.scrollTo( newX, newY, time, easing, 1000 );
\r
460 console.log( 'end1 x:' + newX + ' y:' + newY );
\r
461 this.scrollTo( newX, newY, 0, '', 1000 ); // ensures that the last position is rounded
\r
463 this[ 'dispatch' ]( XUI_Event.SCROLL_END );
\r
468 function X_UI_ScrollBox_resetPosition( that, time ){
\r
469 var x = that.scrollX,
\r
474 if( !that.hasHScroll || 0 < that.scrollX ){
\r
477 if( that.scrollX < that.scrollXMax ){
\r
478 x = that.scrollXMax;
\r
481 if( !that.hasVScroll || 0 < that.scrollY ){
\r
484 if( that.scrollY < that.scrollYMax ){
\r
485 y = that.scrollYMax;
\r
488 if( x === that.scrollX && y === that.scrollY ){
\r
489 console.log( 'no バウンド y:' + y + ' max:' + that.scrollYMax );
\r
493 //console.log( 'バウンド!' );
\r
494 //console.log( 'rese x:' + x + ' y:' + y );
\r
495 that.scrollTo( x, y, time, that.bounceEasing, 1000 );
\r
500 function X_UI_ScrollBox_onAnimeEnd( e ){
\r
501 if( e.target !== this.xnodeSlider || !this.isInTransition ){
\r
502 return X_Callback_NONE;
\r
504 if( !X_UI_ScrollBox_resetPosition( this, this.bounceTime ) ){
\r
505 this.isInTransition = false;
\r
506 this[ 'dispatch' ]( XUI_Event.SCROLL_END );
\r
508 return X_Callback_NONE;
\r
512 X.UI.ScrollBox = X.UI.ChromeBox.inherits(
\r
516 Constructor : function(){
\r
518 XUI_Layout_Canvas,
\r
520 name : 'ScrollBox-Scroller',
\r
521 role : 'container',
\r
528 i = arguments.length,
\r
532 arg = arguments[ --i ];
\r
533 if( arg[ 'instanceOf' ] && arg[ 'instanceOf' ]( XUI_LayoutBase ) ){
\r
536 if( arg[ 'instanceOf' ] && arg[ 'instanceOf' ]( X.UI.AbstractUINode ) ){
\r
537 args[ args.length ] = arg;
\r
539 if( X_Type_isObject( arg ) ){
\r
554 X.UI.VBox.apply( 0, args )
\r
559 attr && this.attr( attr );
\r
561 scrollX : function(){
\r
564 scrollY : function(){
\r
567 scrollWidth : function(){
\r
570 scrollHeight : function(){
\r
573 scrollTo : function( nodeOrX, y ){
\r