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
46 var XUI_ScrollBox = XUI_ChromeBox.inherits(
\r
48 X_Class.PRIVATE_DATA,
\r
50 directionLockThreshold : 10,
\r
51 scrollXEnabled : true,
\r
52 scrollYEnabled : true,
\r
53 scrollEnabled : true,
\r
54 bounceEnabled : true,
\r
55 momentumEnabled : true,
\r
59 hasScrollShadow : true,
\r
60 scrollShadowColor : '#000',
\r
62 supportAttrs : XUI_Attr_createAttrDef( XUI_Attr_Support, X_UI_ScrollBox_SUPPORT_ATTRS ),
\r
68 directionLocked : '',
\r
71 isAnimating : false,
\r
72 isInTransition : false,
\r
85 scrollXMax : 0, // px
\r
86 scrollYMax : 0, // px
\r
87 scrollXRatio : 0, // この値は scroll 不要になっても保持される。 scroll 必要時に参照される
\r
91 absStartX : 0, // px
\r
92 absStartY : 0, // px
\r
97 directionX : 0, // -1, 0, 1
\r
98 directionY : 0, // -1, 0, 1
\r
100 lastScrollWidth : 0,
\r
101 lastScrollHeight : 0,
\r
105 _containerNode : null,
\r
106 xnodeSlider : null,
\r
108 Constructor : function( layout, args ){
\r
109 this[ 'Super' ]( layout, args );
\r
110 this._containerNode = X_Class_getPrivate( this.containerNode );
\r
111 this.xnodeSlider = this._containerNode.xnode[ 'className' ]( 'ScrollSlider' ).listen( X_EVENT_ANIME_END, this, X_UI_ScrollBox_onAnimeEnd );
\r
112 this.xnode[ 'className' ]( 'ScrollBox' );
\r
115 creationComplete : function(){
\r
116 XUI_Box.prototype.creationComplete.apply( this, arguments );
\r
119 calculate : function(){
\r
120 this.lastScrollWidth = this._containerNode.boxWidth;
\r
121 this.lastScrollHeight = this._containerNode.boxHeight;
\r
122 this.lastBoxWidth = this.boxWidth;
\r
123 this.lastBoxHeight = this.boxHeight;
\r
125 XUI_Box.prototype.calculate.apply( this, arguments );
\r
127 // TODO root の layout_complete 後に。
\r
128 // TODO calculate 前に scroll の解放。
\r
131 this.lastScrollWidth !== this._containerNode.boxWidth ||
\r
132 this.lastScrollHeight !== this._containerNode.boxHeight ||
\r
133 this.lastBoxWidth !== this.boxWidth || this.lastBoxHeight !== this.boxHeight
\r
135 X_UI_rootData[ 'listenOnce' ]( XUI_Event.LAYOUT_COMPLETE, this, X_UI_ScrollBox_onLayoutComplete );
\r
139 scrollTo : function( x, y, opt_time, opt_easing, opt_release ){
\r
140 //if( this.scrollX === x && this.scrollY === y ) return;
\r
142 opt_time = 0 <= opt_time ? opt_time : 0;
\r
143 opt_easing = opt_easing || 'circular';
\r
144 opt_release = 0 <= opt_release ? opt_release : 300;
\r
146 this.isInTransition = 0 < opt_time;
\r
148 X_UI_ScrollBox_translate( this, x, y, opt_time, opt_easing, opt_release );
\r
151 _remove : function(){
\r
152 XUI_AbstractUINode.prototype._remove.apply( this, arguments );
\r
153 if( this.scrolling ){
\r
161 function X_UI_ScrollBox_onLayoutBefore( e ){
\r
162 this.scrollXRatio = this.scrollX ? this.scrollXMax / this.scrollX : 0;
\r
163 this.scrollYRatio = this.scrollY ? this.scrollYMax / this.scrollY : 0;
\r
164 this.xnodeSlider.stop();
\r
167 function X_UI_ScrollBox_onLayoutComplete( e ){
\r
168 // scroll の停止、GPU の解除
\r
169 var font = this.fontSize = this.xnodeSlider.call( 'fontSize' );
\r
171 this.scrollXMax = ( this.boxWidth - this._containerNode.boxWidth ) * font;
\r
172 this.scrollYMax = ( this.boxHeight - this._containerNode.boxHeight ) * font;
\r
174 this.hasHScroll = this.scrollXEnabled && this.scrollXMax < 0;
\r
175 this.hasVScroll = this.scrollYEnabled && this.scrollYMax < 0;
\r
177 if( !this.hasHScroll ){
\r
178 this.scrollXMax = 0;
\r
179 //this.scrollWidth = this.boxWidth;
\r
182 if( !this.hasVScroll ){
\r
183 this.scrollYMax = 0;
\r
184 //this.scrollHeight = this.boxHeight;
\r
187 delete this.endTime;
\r
188 delete this.directionX;
\r
189 delete this.directionY;
\r
191 //this.wrapperOffset = this.xnodeWrapper[ 'offset' ]();
\r
193 //this[ 'dispatch' ]('refresh');
\r
195 X_UI_ScrollBox_resetPosition( this, 0 );
\r
197 if( this.hasHScroll || this.hasVScroll ){
\r
199 if( this.scrolling ){
\r
200 X_UI_ScrollBox_translate( this, this.scrollXMax * this.scrollXRatio, this.scrollYMax * this.scrollYRatio, 0, '', 0 );
\r
203 this[ 'listen' ]( XUI_Event._POINTER_DOWN, this, X_UI_ScrollBox_onStart );
\r
204 X_UI_rootData[ 'listen' ]( XUI_Event.LAYOUT_BEFORE, this, X_UI_ScrollBox_onLayoutBefore );
\r
206 X_UI_ScrollBox_translate( this, this.scrollXMax * this.scrollXRatio, this.scrollYMax * this.scrollYRatio, 0, '', 0 );
\r
207 this.scrolling = true;
\r
211 if( this.scrolling ){
\r
213 this[ 'unlisten' ]( XUI_Event._POINTER_DOWN, this, X_UI_ScrollBox_onStart );
\r
214 X_UI_rootData[ 'unlisten' ]( XUI_Event.LAYOUT_BEFORE, this, X_UI_ScrollBox_onLayoutBefore );
\r
216 ( this.scrollX !== 0 || this.scrollY !== 0 ) && X_UI_ScrollBox_translate( this, 0, 0, 0, '', 0 );
\r
218 delete this.scrolling;
\r
219 delete this.scrollXRatio;
\r
220 delete this.scrollYRatio;
\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
279 this.startX = this.scrollX;
\r
280 this.startY = this.scrollY;
\r
281 this.absStartX = this.scrollX;
\r
282 this.absStartY = this.scrollY;
\r
283 this.pointX = e.pageX;
\r
284 this.pointY = e.pageY;
\r
286 this[ 'listen' ]( XUI_Event._POINTER_MOVE, this, X_UI_ScrollBox_onMove );
\r
287 this[ 'listen' ]( [ XUI_Event._POINTER_UP, XUI_Event._POINTER_CANCEL ], this, X_UI_ScrollBox_onEnd );
\r
289 return ret | X_Callback_PREVENT_DEFAULT;
\r
292 function X_UI_ScrollBox_onMove( e ){
\r
293 var ret = X_Callback_NONE,
\r
294 deltaX, deltaY, timestamp,
\r
296 absDistX, absDistY;
\r
297 // 規定以上の move でスクロール開始
\r
299 if( !this.scrollEnabled || e.pointerType !== this.initiated ){
\r
303 if( e.buttons !== 1 ){
\r
304 return X_UI_ScrollBox_onEnd.call( this, e );
\r
308 if( !this.xnodeSlider[ '_anime' ] ){
\r
309 //console.log( 'gpuレイヤーの用意 ' + e.pageY );
\r
310 //console.log( 'mov1 x:' + this.scrollX + ' y:' + this.scrollY );
\r
311 X_UI_ScrollBox_translate( this, this.scrollX, this.scrollY, 0, '', 300 );
\r
315 deltaX = e.pageX - this.pointX;
\r
316 deltaY = e.pageY - this.pointY;
\r
317 timestamp = X_Timer_now();
\r
319 this.pointX = e.pageX;
\r
320 this.pointY = e.pageY;
\r
322 this.distX += deltaX;
\r
323 this.distY += deltaY;
\r
324 absDistX = Math.abs(this.distX);
\r
325 absDistY = Math.abs(this.distY);
\r
327 // We need to move at least 10 pixels for the scrolling to initiate
\r
328 if( 300 < timestamp - this.endTime && ( absDistX < 10 && absDistY < 10 ) ){
\r
332 // If you are scrolling in one direction lock the other
\r
333 if( !this.directionLocked ){
\r
334 if( absDistX > absDistY + this.directionLockThreshold ){
\r
335 this.directionLocked = 'h'; // lock horizontally
\r
337 if( absDistY >= absDistX + this.directionLockThreshold ){
\r
338 this.directionLocked = 'v'; // lock vertically
\r
340 this.directionLocked = 'n'; // no lock
\r
344 if( this.directionLocked === 'h' ){
\r
347 if( this.directionLocked === 'v' ){
\r
351 deltaX = this.hasHScroll ? deltaX : 0;
\r
352 deltaY = this.hasVScroll ? deltaY : 0;
\r
355 this[ 'dispatch' ]( XUI_Event.SCROLL_BEFORE_MOVE );
\r
357 this.minusX = deltaX;
\r
358 this.minusY = deltaY;
\r
360 this[ 'dispatch' ]( XUI_Event.SCROLL_MOVE );
\r
363 newX = this.scrollX + deltaX;// - this.minusX;
\r
364 newY = this.scrollY + deltaY;// - this.minusY;
\r
366 // Slow down if outside of the boundaries
\r
367 if( 0 < newX || newX < this.scrollXMax ){
\r
368 newX = this.bounceEnabled ? this.scrollX + ( deltaX ) / 3 : 0 < newX ? 0 : this.scrollXMax;
\r
371 if( 0 < newY || newY < this.scrollYMax ){
\r
372 //console.log( 'slow... ' + newY + ' ' + this.scrollYMax );
\r
373 newY = this.bounceEnabled ? this.scrollY + ( deltaY ) / 3 : 0 < newY ? 0 : this.scrollYMax;
\r
376 this.directionX = 0 < deltaX ? -1 : deltaX < 0 ? 1 : 0;
\r
377 this.directionY = 0 < deltaY ? -1 : deltaY < 0 ? 1 : 0;
\r
379 //console.log( 'mov2 x:' + newX + ' y:' + newY );
\r
380 X_UI_ScrollBox_translate( this, newX, newY, 0, '', 300 );
\r
382 if( 300 < timestamp - this.startTime ){
\r
383 this.startTime = timestamp;
\r
384 this.startX = this.scrollX;
\r
385 this.startY = this.scrollY;
\r
388 return ret | X_Callback_PREVENT_DEFAULT | X_Callback_CAPTURE_POINTER;
\r
391 function X_UI_ScrollBox_onEnd( e ){
\r
392 var ret = X_Callback_NONE,
\r
396 momentumX, momentumY,
\r
397 duration, distanceX, distanceY;
\r
399 this[ 'unlisten' ]( XUI_Event._POINTER_MOVE, this, X_UI_ScrollBox_onMove );
\r
400 this[ 'unlisten' ]( [ XUI_Event._POINTER_UP, XUI_Event._POINTER_CANCEL ], this, X_UI_ScrollBox_onEnd );
\r
402 if( !this.scrollEnabled || e.pointerType !== this.initiated ){
\r
406 delete this.isInTransition;
\r
407 delete this.initiated;
\r
408 this.endTime = X_Timer_now();
\r
410 duration = this.endTime - this.startTime;
\r
411 newX = Math.round( this.scrollX );
\r
412 newY = Math.round( this.scrollY );
\r
413 distanceX = Math.abs(newX - this.startX);
\r
414 distanceY = Math.abs(newY - this.startY);
\r
416 // reset if we are outside of the boundaries
\r
417 if( X_UI_ScrollBox_resetPosition( this, this.bounceTime ) ){
\r
421 // we scrolled less than 10 pixels
\r
423 // this[ 'dispatch' ]( X_EVENT_CANCELED );
\r
424 console.log( 'we scrolled less than 10 pixels' );
\r
428 // start momentum animation if needed
\r
429 if( this.momentumEnabled && duration < 300 ){
\r
430 momentumX = this.hasHScroll ?
\r
431 X_UI_ScrollBox_momentum( this.scrollX, this.startX, duration, this.scrollXMax, this.bounceEnabled ? this.boxWidth * this.fontSize : 0, this.deceleration ) :
\r
432 { destination: newX, duration: 0 };
\r
433 momentumY = this.hasVScroll ?
\r
434 X_UI_ScrollBox_momentum( this.scrollY, this.startY, duration, this.scrollYMax, this.bounceEnabled ? this.boxHeight * this.fontSize : 0, this.deceleration ) :
\r
435 { destination: newY, duration: 0 };
\r
436 newX = momentumX.destination;
\r
437 newY = momentumY.destination;
\r
438 time = Math.max( momentumX.duration, momentumY.duration ) | 0;
\r
439 this.isInTransition = true;
\r
441 console.log( '慣性無し' );
\r
444 if( newX != this.scrollX || newY != this.scrollY ){
\r
445 // change easing function when scroller goes out of the boundaries
\r
446 if( 0 < newX || newX < this.scrollXMax || 0 < newY || newY < this.scrollYMax ){
\r
447 easing = 'quadratic';
\r
450 console.log( 'end2 x:' + newX + ' y:' + newY + ' t:' + time );
\r
451 this.scrollTo( newX, newY, time, easing, 1000 );
\r
455 console.log( 'end1 x:' + newX + ' y:' + newY );
\r
456 this.scrollTo( newX, newY, 0, '', 1000 ); // ensures that the last position is rounded
\r
458 this[ 'dispatch' ]( XUI_Event.SCROLL_END );
\r
463 function X_UI_ScrollBox_resetPosition( that, time ){
\r
464 var x = that.scrollX,
\r
469 if( !that.hasHScroll || 0 < that.scrollX ){
\r
472 if( that.scrollX < that.scrollXMax ){
\r
473 x = that.scrollXMax;
\r
476 if( !that.hasVScroll || 0 < that.scrollY ){
\r
479 if( that.scrollY < that.scrollYMax ){
\r
480 y = that.scrollYMax;
\r
483 if( x === that.scrollX && y === that.scrollY ){
\r
484 console.log( 'no バウンド y:' + y + ' max:' + that.scrollYMax );
\r
488 //console.log( 'バウンド!' );
\r
489 //console.log( 'rese x:' + x + ' y:' + y );
\r
490 that.scrollTo( x, y, time, that.bounceEasing, 1000 );
\r
495 function X_UI_ScrollBox_onAnimeEnd( e ){
\r
496 if( e.target !== this.xnodeSlider || !this.isInTransition ){
\r
497 return X_Callback_NONE;
\r
499 if( !X_UI_ScrollBox_resetPosition( this, this.bounceTime ) ){
\r
500 this.isInTransition = false;
\r
501 this.dispatch( XUI_Event.SCROLL_END );
\r
503 return X_Callback_NONE;
\r
506 X.UI.ScrollBox = X.UI.ChromeBox.inherits(
\r
511 Constructor : function(){
\r
513 XUI_Layout_Vertical,
\r
515 name : 'ScrollBox-Scroller',
\r
516 role : 'container',
\r
523 i = arguments.length,
\r
527 arg = arguments[ --i ];
\r
528 if( arg[ 'instanceOf' ] && arg[ 'instanceOf' ]( XUI_LayoutBase ) ){
\r
531 if( arg[ 'instanceOf' ] && arg[ 'instanceOf' ]( X.UI.AbstractUINode ) ){
\r
532 args[ args.length ] = arg;
\r
534 if( X_Type_isObject( arg ) ){
\r
539 X_Class_newPrivate(
\r
547 X.UI.VBox.apply( 0, args )
\r
551 attr && this.attr( attr );
\r
553 scrollX : function(){
\r
556 scrollY : function(){
\r
559 scrollWidth : function(){
\r
562 scrollHeight : function(){
\r
565 scrollTo : function( nodeOrX, y ){
\r