4 var X_UI_ScrollBox_SUPPORT_ATTRS = {
\r
5 // スクロール開始するために必要な移動距離、縦か横、どちらか制限する場合、より重要
\r
6 directionLockThreshold : [ 10, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.LENGTH ],
\r
7 scrollXEnabled : [ true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],
\r
8 scrollYEnabled : [ true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],
\r
9 scrollEnabled : [ true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],
\r
10 bounceEnabled : [ true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],
\r
11 bounceTime : [ 300, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.TIME ],
\r
12 useWheel : [ true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],
\r
13 useKey : [ true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],
\r
14 hasScrollShadow : [ true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],
\r
15 scrollShadowColor : [ '#000', XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.COLOR ]
\r
18 var XUI_ScrollBox = XUI_ChromeBox.inherits(
\r
22 layout : XUI_Layout_Canvas,
\r
24 directionLockThreshold : 10,
\r
25 scrollXEnabled : true,
\r
26 scrollYEnabled : true,
\r
27 scrollEnabled : true,
\r
28 bounceEnabled : true,
\r
29 momentumEnabled : true,
\r
33 hasScrollShadow : true,
\r
34 scrollShadowColor : '#000',
\r
40 directionLocked : '',
\r
43 isAnimating : false,
\r
44 isInTransition : false,
\r
57 scrollXMax : 0, // px
\r
58 scrollYMax : 0, // px
\r
59 scrollXRatio : 0, // この値は scroll 不要になっても保持される。 scroll 必要時に参照される
\r
63 absStartX : 0, // px
\r
64 absStartY : 0, // px
\r
69 directionX : 0, // -1, 0, 1
\r
70 directionY : 0, // -1, 0, 1
\r
72 lastScrollWidth : 0,
\r
73 lastScrollHeight : 0,
\r
77 _containerNode : null,
\r
80 Constructor : function( user, layout, args ){
\r
81 this[ 'Super' ]( user, layout, args );
\r
82 this._containerNode = X_Pair_get( this.containerNode );
\r
83 this.xnodeSlider = this._containerNode.xnode[ 'className' ]( 'ScrollSlider' ).listen( X_EVENT_ANIME_END, this, X_UI_ScrollBox_onAnimeEnd );
\r
84 this.xnode[ 'className' ]( 'ScrollBox' );
\r
87 creationComplete : function(){
\r
88 XUI_Box.prototype.creationComplete.apply( this, arguments );
\r
91 calculate : function(){
\r
92 this.lastScrollWidth = this._containerNode.boxWidth;
\r
93 this.lastScrollHeight = this._containerNode.boxHeight;
\r
94 this.lastBoxWidth = this.boxWidth;
\r
95 this.lastBoxHeight = this.boxHeight;
\r
97 XUI_Box.prototype.calculate.apply( this, arguments );
\r
99 // TODO root の layout_complete 後に。
\r
100 // TODO calculate 前に scroll の解放。
\r
103 this.lastScrollWidth !== this._containerNode.boxWidth ||
\r
104 this.lastScrollHeight !== this._containerNode.boxHeight ||
\r
105 this.lastBoxWidth !== this.boxWidth || this.lastBoxHeight !== this.boxHeight
\r
107 X_UI_rootData[ 'listenOnce' ]( XUI_Event.LAYOUT_COMPLETE, this, X_UI_ScrollBox_onLayoutComplete );
\r
111 scrollTo : function( x, y, opt_time, opt_easing, opt_release ){
\r
112 //if( this.scrollX === x && this.scrollY === y ) return;
\r
114 opt_time = 0 <= opt_time ? opt_time : 0;
\r
115 opt_easing = opt_easing || 'circular';
\r
116 opt_release = 0 <= opt_release ? opt_release : 300;
\r
118 this.isInTransition = 0 < opt_time;
\r
120 X_UI_ScrollBox_translate( this, x, y, opt_time, opt_easing, opt_release );
\r
123 _remove : function(){
\r
124 XUI_AbstractUINode.prototype._remove.apply( this, arguments );
\r
125 if( this.scrolling ){
\r
133 function X_UI_ScrollBox_onLayoutBefore( e ){
\r
134 if( e[ 'cancelable' ] && this.isInTransition && X_Node_Anime_translateZ ){
\r
135 this[ 'listenOnce' ]( XUI_Event.SCROLL_END, X_UI_rootData, X_UI_rootData.calculate );
\r
136 return X_Callback_PREVENT_DEFAULT;
\r
138 this.scrollXRatio = this.scrollX ? this.scrollXMax / this.scrollX : 0;
\r
139 this.scrollYRatio = this.scrollY ? this.scrollYMax / this.scrollY : 0;
\r
140 this.xnodeSlider.stop();
\r
141 this.isInTransition = false;
\r
142 return X_Callback_NONE;
\r
145 function X_UI_ScrollBox_onLayoutComplete( e ){
\r
146 // scroll の停止、GPU の解除
\r
147 var font = this.fontSize = this.xnodeSlider.call( 'fontSize' );
\r
149 this.scrollXMax = ( this.boxWidth - this._containerNode.boxWidth ) * font | 0;
\r
150 this.scrollYMax = ( this.boxHeight - this._containerNode.boxHeight ) * font | 0;
\r
152 this.hasHScroll = this.scrollXEnabled && ( this.scrollXMax < -1 ); // < 0 だと
\r
153 this.hasVScroll = this.scrollYEnabled && ( this.scrollYMax < -1 );
\r
155 if( !this.hasHScroll ){
\r
156 this.scrollXMax = 0;
\r
159 if( !this.hasVScroll ){
\r
160 this.scrollYMax = 0;
\r
163 delete this.endTime;
\r
164 delete this.directionX;
\r
165 delete this.directionY;
\r
167 X_UI_ScrollBox_resetPosition( this, 0 );
\r
169 if( this.hasHScroll || this.hasVScroll ){
\r
171 if( this.scrolling ){
\r
172 X_UI_ScrollBox_translate( this, this.scrollXMax * this.scrollXRatio, this.scrollYMax * this.scrollYRatio, 100, '', 300 );
\r
175 this[ 'listen' ]( XUI_Event._POINTER_DOWN, this, X_UI_ScrollBox_onStart );
\r
176 X_UI_rootData[ 'listen' ]( XUI_Event.LAYOUT_BEFORE, this, X_UI_ScrollBox_onLayoutBefore );
\r
178 X_UI_ScrollBox_translate( this, this.scrollXMax * this.scrollXRatio, this.scrollYMax * this.scrollYRatio, 100, '', 300 );
\r
179 this.scrolling = true;
\r
183 if( this.scrolling ){
\r
185 this[ 'unlisten' ]( XUI_Event._POINTER_DOWN, this, X_UI_ScrollBox_onStart );
\r
186 X_UI_rootData[ 'unlisten' ]( XUI_Event.LAYOUT_BEFORE, this, X_UI_ScrollBox_onLayoutBefore );
\r
188 ( this.scrollX !== 0 || this.scrollY !== 0 ) && X_UI_ScrollBox_translate( this, 0, 0, 100, '', 300 );
\r
190 delete this.scrolling;
\r
191 delete this.scrollXRatio;
\r
192 delete this.scrollYRatio;
\r
196 // TODO use scrollLeft, scrollTop
\r
197 function X_UI_ScrollBox_translate( that, x, y, opt_time, opt_easing, opt_release ){
\r
199 opt_time = 0 <= opt_time ? opt_time : 0;
\r
200 opt_easing = opt_easing === '' ? '' : opt_easing || 'circular';
\r
201 opt_release = 0 <= opt_release ? opt_release : 300;
\r
203 that.xnodeSlider.animate(
\r
212 opt_time, opt_easing, opt_release
\r
218 if( that.indicators ){
\r
219 for( i = that.indicators.length; i--; ){
\r
220 that.indicators[ i ].updatePosition();
\r
225 function X_UI_ScrollBox_onStart( e ){
\r
226 var ret = X_Callback_NONE;
\r
228 // React to left mouse button only
\r
229 if( e.pointerType === 'mouse' && e.button !== 0 ){
\r
233 if( !this.scrollEnabled || ( this.initiated && e.pointerType !== this.initiated ) ){
\r
237 this.initiated = e.pointerType;
\r
238 this.moved = false;
\r
241 this.directionX = 0;
\r
242 this.directionY = 0;
\r
243 this.directionLocked = '';
\r
244 this.startTime = X_Timer_now();
\r
247 if( this.isInTransition || this.isAnimating ){
\r
248 this.isInTransition = this.isAnimating = false;
\r
249 this[ 'dispatch' ]( XUI_Event.SCROLL_END );
\r
250 this.xnodeSlider.stop();
\r
253 this.startX = this.scrollX;
\r
254 this.startY = this.scrollY;
\r
255 this.absStartX = this.scrollX;
\r
256 this.absStartY = this.scrollY;
\r
257 this.pointX = e.pageX;
\r
258 this.pointY = e.pageY;
\r
260 console.log( 'scrollstart ' + e.pageY );
\r
262 this[ 'listen' ]( XUI_Event._POINTER_MOVE, this, X_UI_ScrollBox_onMove );
\r
263 this[ 'listen' ]( [ XUI_Event._POINTER_UP, XUI_Event._POINTER_CANCEL ], this, X_UI_ScrollBox_onEnd );
\r
265 return ret | X_Callback_PREVENT_DEFAULT;
\r
268 function X_UI_ScrollBox_onMove( e ){
\r
269 var ret = X_Callback_NONE,
\r
270 deltaX, deltaY, timestamp,
\r
272 absDistX, absDistY;
\r
273 // 規定以上の move でスクロール開始
\r
275 console.log( 'scrollmove ' + e.buttons + ' ' + e.button );
\r
277 if( !this.scrollEnabled || e.pointerType !== this.initiated ){
\r
281 if( e.buttons !== 1 ){
\r
282 return X_UI_ScrollBox_onEnd.call( this, e );
\r
286 if( !this.xnodeSlider[ '_anime' ] ){
\r
287 //console.log( 'gpuレイヤーの用意 ' + e.pageY );
\r
288 //console.log( 'mov1 x:' + this.scrollX + ' y:' + this.scrollY );
\r
289 X_UI_ScrollBox_translate( this, this.scrollX, this.scrollY, 0, '', 300 );
\r
293 deltaX = e.pageX - this.pointX;
\r
294 deltaY = e.pageY - this.pointY;
\r
295 timestamp = X_Timer_now();
\r
297 this.pointX = e.pageX;
\r
298 this.pointY = e.pageY;
\r
300 this.distX += deltaX;
\r
301 this.distY += deltaY;
\r
302 absDistX = Math.abs(this.distX);
\r
303 absDistY = Math.abs(this.distY);
\r
305 // We need to move at least 10 pixels for the scrolling to initiate
\r
306 if( 300 < timestamp - this.endTime && ( absDistX < 10 && absDistY < 10 ) ){
\r
310 // If you are scrolling in one direction lock the other
\r
311 if( !this.directionLocked ){
\r
312 if( absDistX > absDistY + this.directionLockThreshold ){
\r
313 this.directionLocked = 'h'; // lock horizontally
\r
315 if( absDistY >= absDistX + this.directionLockThreshold ){
\r
316 this.directionLocked = 'v'; // lock vertically
\r
318 this.directionLocked = 'n'; // no lock
\r
322 if( this.directionLocked === 'h' ){
\r
325 if( this.directionLocked === 'v' ){
\r
329 deltaX = this.hasHScroll ? deltaX : 0;
\r
330 deltaY = this.hasVScroll ? deltaY : 0;
\r
333 this[ 'dispatch' ]( XUI_Event.SCROLL_BEFORE_MOVE );
\r
335 this.minusX = deltaX;
\r
336 this.minusY = deltaY;
\r
338 this[ 'dispatch' ]( XUI_Event.SCROLL_MOVE );
\r
341 newX = this.scrollX + deltaX;// - this.minusX;
\r
342 newY = this.scrollY + deltaY;// - this.minusY;
\r
344 // Slow down if outside of the boundaries
\r
345 if( 0 < newX || newX < this.scrollXMax ){
\r
346 newX = this.bounceEnabled ? this.scrollX + ( deltaX ) / 3 : 0 < newX ? 0 : this.scrollXMax;
\r
349 if( 0 < newY || newY < this.scrollYMax ){
\r
350 //console.log( 'slow... ' + newY + ' ' + this.scrollYMax );
\r
351 newY = this.bounceEnabled ? this.scrollY + ( deltaY ) / 3 : 0 < newY ? 0 : this.scrollYMax;
\r
354 this.directionX = 0 < deltaX ? -1 : deltaX < 0 ? 1 : 0;
\r
355 this.directionY = 0 < deltaY ? -1 : deltaY < 0 ? 1 : 0;
\r
357 //console.log( 'mov2 x:' + newX + ' y:' + newY );
\r
358 X_UI_ScrollBox_translate( this, newX, newY, 0, '', 300 );
\r
360 if( 300 < timestamp - this.startTime ){
\r
361 this.startTime = timestamp;
\r
362 this.startX = this.scrollX;
\r
363 this.startY = this.scrollY;
\r
366 return ret | X_Callback_PREVENT_DEFAULT | X_Callback_CAPTURE_POINTER;
\r
369 function X_UI_ScrollBox_onEnd( e ){
\r
370 var ret = X_Callback_NONE,
\r
374 momentumX, momentumY,
\r
375 duration, distanceX, distanceY;
\r
377 this[ 'unlisten' ]( XUI_Event._POINTER_MOVE, this, X_UI_ScrollBox_onMove );
\r
378 this[ 'unlisten' ]( [ XUI_Event._POINTER_UP, XUI_Event._POINTER_CANCEL ], this, X_UI_ScrollBox_onEnd );
\r
380 if( !this.scrollEnabled || e.pointerType !== this.initiated ){
\r
384 delete this.isInTransition;
\r
385 delete this.initiated;
\r
386 this.endTime = X_Timer_now();
\r
388 duration = this.endTime - this.startTime;
\r
389 newX = Math.round( this.scrollX );
\r
390 newY = Math.round( this.scrollY );
\r
391 distanceX = Math.abs(newX - this.startX);
\r
392 distanceY = Math.abs(newY - this.startY);
\r
394 // reset if we are outside of the boundaries
\r
395 if( X_UI_ScrollBox_resetPosition( this, this.bounceTime ) ){
\r
399 // we scrolled less than 10 pixels
\r
401 // this[ 'dispatch' ]( X_EVENT_CANCELED );
\r
402 console.log( 'we scrolled less than 10 pixels ' + e.pageY );
\r
406 // start momentum animation if needed
\r
407 if( this.momentumEnabled && duration < 300 ){
\r
408 momentumX = this.hasHScroll ?
\r
409 X_UI_ScrollBox_momentum( this.scrollX, this.startX, duration, this.scrollXMax, this.bounceEnabled ? this.boxWidth * this.fontSize : 0, this.deceleration ) :
\r
410 { destination: newX, duration: 0 };
\r
411 momentumY = this.hasVScroll ?
\r
412 X_UI_ScrollBox_momentum( this.scrollY, this.startY, duration, this.scrollYMax, this.bounceEnabled ? this.boxHeight * this.fontSize : 0, this.deceleration ) :
\r
413 { destination: newY, duration: 0 };
\r
414 newX = momentumX.destination;
\r
415 newY = momentumY.destination;
\r
416 time = Math.max( momentumX.duration, momentumY.duration ) | 0;
\r
417 this.isInTransition = true;
\r
419 console.log( '慣性無し' );
\r
422 if( newX != this.scrollX || newY != this.scrollY ){
\r
423 // change easing function when scroller goes out of the boundaries
\r
424 if( 0 < newX || newX < this.scrollXMax || 0 < newY || newY < this.scrollYMax ){
\r
425 easing = 'quadratic';
\r
428 console.log( 'end2 x:' + newX + ' y:' + newY + ' t:' + time );
\r
429 this.scrollTo( newX, newY, time, easing, 1000 );
\r
433 console.log( 'end1 x:' + newX + ' y:' + newY );
\r
434 this.scrollTo( newX, newY, 0, '', 1000 ); // ensures that the last position is rounded
\r
436 this[ 'dispatch' ]( XUI_Event.SCROLL_END );
\r
441 function X_UI_ScrollBox_resetPosition( that, time ){
\r
442 var x = that.scrollX,
\r
447 if( !that.hasHScroll || 0 < that.scrollX ){
\r
450 if( that.scrollX < that.scrollXMax ){
\r
451 x = that.scrollXMax;
\r
454 if( !that.hasVScroll || 0 < that.scrollY ){
\r
457 if( that.scrollY < that.scrollYMax ){
\r
458 y = that.scrollYMax;
\r
461 if( x === that.scrollX && y === that.scrollY ){
\r
462 console.log( 'no バウンド y:' + y + ' max:' + that.scrollYMax );
\r
466 //console.log( 'バウンド!' );
\r
467 //console.log( 'rese x:' + x + ' y:' + y );
\r
468 that.scrollTo( x, y, time, that.bounceEasing, 1000 );
\r
473 function X_UI_ScrollBox_onAnimeEnd( e ){
\r
474 if( e.target !== this.xnodeSlider || !this.isInTransition ){
\r
475 return X_Callback_NONE;
\r
477 if( !X_UI_ScrollBox_resetPosition( this, this.bounceTime ) ){
\r
478 this.isInTransition = false;
\r
479 this[ 'dispatch' ]( XUI_Event.SCROLL_END );
\r
481 return X_Callback_NONE;
\r
484 function X_UI_ScrollBox_momentum( current, start, time, lowerMargin, wrapperSize, deceleration ){
\r
485 var distance = current - start,
\r
486 speed = Math.abs( distance ) / time,
\r
490 deceleration = deceleration === undefined ? 0.0006 : deceleration;
\r
492 destination = current + ( speed * speed ) / ( 2 * deceleration ) * ( distance < 0 ? -1 : 1 );
\r
493 duration = speed / deceleration;
\r
495 if( destination < lowerMargin ){
\r
496 destination = wrapperSize ? lowerMargin - ( wrapperSize / 2.5 * ( speed / 8 ) ) : lowerMargin;
\r
497 distance = Math.abs( destination - current );
\r
498 duration = distance / speed;
\r
500 if ( destination > 0 ) {
\r
501 destination = wrapperSize ? wrapperSize / 2.5 * ( speed / 8 ) : 0;
\r
502 distance = Math.abs( current ) + destination;
\r
503 duration = distance / speed;
\r
507 destination : Math.round( destination ),
\r
508 duration : duration
\r
513 X.UI.ScrollBox = X.UI.ChromeBox.inherits(
\r
517 Constructor : function(){
\r
518 var supports, slider;
\r
520 if( XUI_ScrollBox.prototype.usableAttrs === XUI_ChromeBox.prototype.usableAttrs ){
\r
521 XUI_ScrollBox.prototype.usableAttrs = supports = XUI_Attr_createAttrDef( XUI_Attr_Support, X_UI_ScrollBox_SUPPORT_ATTRS );
\r
523 XUI_ScrollBox.prototype.attrClass = XUI_Attr_preset( XUI_Box.prototype.attrClass, supports, { width : '100%', height : '100%', bgColor : 0x111111 } );
\r
527 XUI_Layout_Vertical,
\r
529 name : 'ScrollBox-Scroller',
\r
530 role : 'container',
\r
537 i = arguments.length,
\r
541 arg = arguments[ --i ];
\r
542 if( arg[ 'instanceOf' ] && arg[ 'instanceOf' ]( XUI_LayoutBase ) ){
\r
545 if( arg[ 'instanceOf' ] && arg[ 'instanceOf' ]( X.UI.AbstractUINode ) ){
\r
546 args[ args.length ] = arg;
\r
548 if( X_Type_isObject( arg ) ){
\r
549 args[ args.length ] = attr = arg;
\r
550 slider = attr.scrollSlider;
\r
560 slider || X.UI.VBox.apply( 0, args )
\r
565 //attr && this.attr( attr );
\r
567 scrollX : function(){
\r
570 scrollY : function(){
\r
573 scrollWidth : function(){
\r
576 scrollHeight : function(){
\r
579 scrollTo : function( nodeOrX, y ){
\r