8 var X_UI_ScrollBox_SUPPORT_ATTRS = {
\r
9 // スクロール開始するために必要な移動距離、縦か横、どちらか制限する場合、より重要
\r
10 directionLockThreshold : [ 10, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.LENGTH ],
\r
11 scrollXEnabled : [ true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],
\r
12 scrollYEnabled : [ true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],
\r
13 scrollEnabled : [ true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],
\r
14 bounceEnabled : [ true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],
\r
15 bounceTime : [ 300, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.TIME ],
\r
16 useWheel : [ true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],
\r
17 useKey : [ true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],
\r
18 hasScrollShadow : [ true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],
\r
19 scrollShadowColor : [ '#000', XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.COLOR ]
\r
22 var XUI_ScrollBox = XUI_ChromeBox.inherits(
\r
26 layout : XUI_Layout_Canvas,
\r
28 directionLockThreshold : 10,
\r
29 scrollXEnabled : true,
\r
30 scrollYEnabled : true,
\r
31 scrollEnabled : true,
\r
32 bounceEnabled : true,
\r
33 momentumEnabled : true,
\r
37 hasScrollShadow : true,
\r
38 scrollShadowColor : '#000',
\r
44 directionLocked : '',
\r
47 isInTransition : false,
\r
60 scrollXMax : 0, // px
\r
61 scrollYMax : 0, // px
\r
62 scrollXRatio : 0, // この値は scroll 不要になっても保持される。 scroll 必要時に参照される
\r
66 absStartX : 0, // px
\r
67 absStartY : 0, // px
\r
72 directionX : 0, // -1, 0, 1
\r
73 directionY : 0, // -1, 0, 1
\r
75 lastScrollWidth : 0,
\r
76 lastScrollHeight : 0,
\r
80 _containerNode : null,
\r
83 xnodeIndicatorX : null,
\r
84 xnodeIndicatorY : null,
\r
86 Constructor : function( user, layout, args ){
\r
87 this[ 'Super' ]( user, layout, args );
\r
88 this._containerNode = X_Pair_get( this.containerNode );
\r
89 this.xnodeSlider = this._containerNode.xnode[ 'className' ]( 'ScrollSlider' ).listen( X_EVENT_ANIME_END, this, X_UI_ScrollBox_onAnimeEnd );
\r
90 this.xnode[ 'className' ]( 'ScrollBox' );
\r
93 creationComplete : function(){
\r
94 XUI_Box.prototype.creationComplete.apply( this, arguments );
\r
97 calculate : function(){
\r
98 this.lastScrollWidth = this._containerNode.boxWidth;
\r
99 this.lastScrollHeight = this._containerNode.boxHeight;
\r
100 this.lastBoxWidth = this.boxWidth;
\r
101 this.lastBoxHeight = this.boxHeight;
\r
103 XUI_Box.prototype.calculate.apply( this, arguments );
\r
105 // TODO root の layout_complete 後に。
\r
106 // TODO calculate 前に scroll の解放。
\r
109 this.lastScrollWidth !== this._containerNode.boxWidth ||
\r
110 this.lastScrollHeight !== this._containerNode.boxHeight ||
\r
111 this.lastBoxWidth !== this.boxWidth || this.lastBoxHeight !== this.boxHeight
\r
113 X_UI_rootData[ 'listenOnce' ]( XUI_Event.LAYOUT_COMPLETE, this, X_UI_ScrollBox_onLayoutComplete );
\r
117 scrollTo : function( x, y, opt_time, opt_easing, opt_release ){
\r
118 //if( this.scrollX === x && this.scrollY === y ) return;
\r
120 opt_time = 0 <= opt_time ? opt_time : 0;
\r
121 opt_easing = opt_easing || 'circular';
\r
122 opt_release = 0 <= opt_release ? opt_release : 300;
\r
124 this.isInTransition = 0 < opt_time;
\r
126 X_UI_ScrollBox_translate( this, x, y, opt_time, opt_easing, opt_release );
\r
129 _remove : function(){
\r
130 XUI_AbstractUINode.prototype._remove.apply( this, arguments );
\r
131 if( this.scrolling ){
\r
139 function X_UI_ScrollBox_onLayoutBefore( e ){
\r
140 if( e[ 'cancelable' ] && this.isInTransition && X_Node_Anime_translateZ ){
\r
141 this[ 'listenOnce' ]( XUI_Event.SCROLL_END, X_UI_rootData, X_UI_rootData.calculate );
\r
142 return X_CALLBACK_PREVENT_DEFAULT;
\r
144 this.scrollXRatio = this.scrollX ? this.scrollXMax / this.scrollX : 0;
\r
145 this.scrollYRatio = this.scrollY ? this.scrollYMax / this.scrollY : 0;
\r
146 this.xnodeSlider[ 'stop' ]();
\r
147 this.isInTransition = false;
\r
148 return X_CALLBACK_NONE;
\r
151 function X_UI_ScrollBox_onLayoutComplete( e ){
\r
152 // scroll の停止、GPU の解除
\r
153 var font = this.fontSize = this.xnodeSlider[ 'call' ]( 'fontSize' );
\r
155 this.scrollXMax = ( this.boxWidth - this._containerNode.boxWidth ) * font | 0;
\r
156 this.scrollYMax = ( this.boxHeight - this._containerNode.boxHeight ) * font | 0;
\r
158 this.hasHScroll = this.scrollXEnabled && ( this.scrollXMax < -1 ); // < 0 だと
\r
159 this.hasVScroll = this.scrollYEnabled && ( this.scrollYMax < -1 );
\r
161 if( !this.hasHScroll ){
\r
162 this.scrollXMax = 0;
\r
165 if( !this.hasVScroll ){
\r
166 this.scrollYMax = 0;
\r
169 delete this.endTime;
\r
170 delete this.directionX;
\r
171 delete this.directionY;
\r
173 X_UI_ScrollBox_resetPosition( this, 0 );
\r
175 if( this.hasHScroll || this.hasVScroll ){
\r
177 if( this.scrolling ){
\r
178 X_UI_ScrollBox_translate( this, this.scrollXMax * this.scrollXRatio, this.scrollYMax * this.scrollYRatio, 100, '', 300 );
\r
181 this[ 'listen' ]( XUI_Event._POINTER_DOWN, this, X_UI_ScrollBox_onStart );
\r
182 X_UI_rootData[ 'listen' ]( XUI_Event.LAYOUT_BEFORE, this, X_UI_ScrollBox_onLayoutBefore );
\r
184 X_UI_ScrollBox_translate( this, this.scrollXMax * this.scrollXRatio, this.scrollYMax * this.scrollYRatio, 100, '', 300 );
\r
185 this.scrolling = true;
\r
189 if( this.scrolling ){
\r
191 this[ 'unlisten' ]( XUI_Event._POINTER_DOWN, this, X_UI_ScrollBox_onStart );
\r
192 X_UI_rootData[ 'unlisten' ]( XUI_Event.LAYOUT_BEFORE, this, X_UI_ScrollBox_onLayoutBefore );
\r
194 ( this.scrollX !== 0 || this.scrollY !== 0 ) && X_UI_ScrollBox_translate( this, 0, 0, 100, '', 300 );
\r
196 delete this.scrolling;
\r
197 delete this.scrollXRatio;
\r
198 delete this.scrollYRatio;
\r
202 // TODO use scrollLeft, scrollTop
\r
203 function X_UI_ScrollBox_translate( that, x, y, opt_time, opt_easing, opt_release ){
\r
205 opt_time = 0 <= opt_time ? opt_time : 0;
\r
206 opt_easing = opt_easing === '' ? '' : opt_easing || 'circular';
\r
207 opt_release = 0 <= opt_release ? opt_release : 300;
\r
209 that.xnodeSlider[ 'animate' ](
\r
218 opt_time, opt_easing, opt_release
\r
224 if( that.indicators ){
\r
225 for( i = that.indicators.length; i--; ){
\r
226 that.indicators[ i ].updatePosition();
\r
231 function X_UI_ScrollBox_onStart( e ){
\r
232 var ret = X_CALLBACK_NONE;
\r
234 // React to left mouse button only
\r
235 if( e.pointerType === 'mouse' && e.button !== 0 ){
\r
239 if( !this.scrollEnabled || ( this.initiated && e.pointerType !== this.initiated ) ){
\r
243 this.initiated = e.pointerType;
\r
244 this.moved = false;
\r
247 this.directionX = 0;
\r
248 this.directionY = 0;
\r
249 this.directionLocked = '';
\r
250 this.startTime = X_Timer_now();
\r
253 if( this.isInTransition ){
\r
254 this.isInTransition = false;
\r
255 this[ 'dispatch' ]( XUI_Event.SCROLL_END );
\r
257 this.xnodeSlider[ 'stop' ]();
\r
260 this.startX = this.scrollX;
\r
261 this.startY = this.scrollY;
\r
262 this.absStartX = this.scrollX;
\r
263 this.absStartY = this.scrollY;
\r
264 this.pointX = e.pageX;
\r
265 this.pointY = e.pageY;
\r
267 console.log( 'scrollstart ' + e.pageY );
\r
269 this[ 'listen' ]( XUI_Event._POINTER_MOVE, this, X_UI_ScrollBox_onMove );
\r
270 this[ 'listen' ]( [ XUI_Event._POINTER_UP, XUI_Event._POINTER_CANCEL ], this, X_UI_ScrollBox_onEnd );
\r
272 return ret | X_CALLBACK_PREVENT_DEFAULT;
\r
275 function X_UI_ScrollBox_onMove( e ){
\r
276 var ret = X_CALLBACK_NONE,
\r
277 deltaX, deltaY, timestamp,
\r
279 absDistX, absDistY;
\r
280 // 規定以上の move でスクロール開始
\r
282 //console.log( 'scrollmove ' + e.buttons + ' ' + e.button );
\r
284 if( !this.scrollEnabled || e.pointerType !== this.initiated ){
\r
288 if( e.buttons !== 1 ){
\r
289 return X_UI_ScrollBox_onEnd.call( this, e );
\r
293 if( !this.xnodeSlider[ '_anime' ] ){
\r
294 //console.log( 'gpuレイヤーの用意 ' + e.pageY );
\r
295 //console.log( 'mov1 x:' + this.scrollX + ' y:' + this.scrollY );
\r
296 X_UI_ScrollBox_translate( this, this.scrollX, this.scrollY, 0, '', 300 );
\r
300 deltaX = e.pageX - this.pointX;
\r
301 deltaY = e.pageY - this.pointY;
\r
302 timestamp = X_Timer_now();
\r
304 this.pointX = e.pageX;
\r
305 this.pointY = e.pageY;
\r
307 this.distX += deltaX;
\r
308 this.distY += deltaY;
\r
309 absDistX = Math.abs(this.distX);
\r
310 absDistY = Math.abs(this.distY);
\r
312 // We need to move at least 10 pixels for the scrolling to initiate
\r
313 if( 300 < timestamp - this.endTime && ( absDistX < 10 && absDistY < 10 ) ){
\r
317 // If you are scrolling in one direction lock the other
\r
318 if( !this.directionLocked ){
\r
319 if( absDistX > absDistY + this.directionLockThreshold ){
\r
320 this.directionLocked = 'h'; // lock horizontally
\r
322 if( absDistY >= absDistX + this.directionLockThreshold ){
\r
323 this.directionLocked = 'v'; // lock vertically
\r
325 this.directionLocked = 'n'; // no lock
\r
329 if( this.directionLocked === 'h' ){
\r
332 if( this.directionLocked === 'v' ){
\r
336 deltaX = this.hasHScroll ? deltaX : 0;
\r
337 deltaY = this.hasVScroll ? deltaY : 0;
\r
340 this[ 'dispatch' ]( XUI_Event.SCROLL_BEFORE_MOVE );
\r
342 this.minusX = deltaX;
\r
343 this.minusY = deltaY;
\r
345 this[ 'dispatch' ]( XUI_Event.SCROLL_MOVE );
\r
348 newX = this.scrollX + deltaX;// - this.minusX;
\r
349 newY = this.scrollY + deltaY;// - this.minusY;
\r
351 // Slow down if outside of the boundaries
\r
352 if( 0 < newX || newX < this.scrollXMax ){
\r
353 newX = this.bounceEnabled ? this.scrollX + ( deltaX ) / 3 : 0 < newX ? 0 : this.scrollXMax;
\r
356 if( 0 < newY || newY < this.scrollYMax ){
\r
357 //console.log( 'slow... ' + newY + ' ' + this.scrollYMax );
\r
358 newY = this.bounceEnabled ? this.scrollY + ( deltaY ) / 3 : 0 < newY ? 0 : this.scrollYMax;
\r
361 this.directionX = 0 < deltaX ? -1 : deltaX < 0 ? 1 : 0;
\r
362 this.directionY = 0 < deltaY ? -1 : deltaY < 0 ? 1 : 0;
\r
364 //console.log( 'mov2 x:' + newX + ' y:' + newY );
\r
365 X_UI_ScrollBox_translate( this, newX, newY, 0, '', 300 );
\r
367 if( 300 < timestamp - this.startTime ){
\r
368 this.startTime = timestamp;
\r
369 this.startX = this.scrollX;
\r
370 this.startY = this.scrollY;
\r
373 return ret | X_CALLBACK_PREVENT_DEFAULT | X_CALLBACK_CAPTURE_POINTER;
\r
376 function X_UI_ScrollBox_onEnd( e ){
\r
377 var ret = X_CALLBACK_NONE,
\r
381 momentumX, momentumY,
\r
382 duration, distanceX, distanceY;
\r
384 this[ 'unlisten' ]( XUI_Event._POINTER_MOVE, this, X_UI_ScrollBox_onMove );
\r
385 this[ 'unlisten' ]( [ XUI_Event._POINTER_UP, XUI_Event._POINTER_CANCEL ], this, X_UI_ScrollBox_onEnd );
\r
387 if( !this.scrollEnabled || e.pointerType !== this.initiated ){
\r
391 delete this.isInTransition;
\r
392 delete this.initiated;
\r
393 this.endTime = X_Timer_now();
\r
395 duration = this.endTime - this.startTime;
\r
396 newX = Math.round( this.scrollX );
\r
397 newY = Math.round( this.scrollY );
\r
398 distanceX = Math.abs(newX - this.startX);
\r
399 distanceY = Math.abs(newY - this.startY);
\r
401 // reset if we are outside of the boundaries
\r
402 if( X_UI_ScrollBox_resetPosition( this, this.bounceTime ) ){
\r
406 // we scrolled less than 10 pixels
\r
408 // this[ 'dispatch' ]( X_EVENT_CANCELED );
\r
409 console.log( 'we scrolled less than 10 pixels ' + e.pageY );
\r
413 // start momentum animation if needed
\r
414 if( this.momentumEnabled && duration < 300 ){
\r
415 momentumX = this.hasHScroll ?
\r
416 X_UI_ScrollBox_momentum( this.scrollX, this.startX, duration, this.scrollXMax, this.bounceEnabled ? this.boxWidth * this.fontSize : 0, this.deceleration ) :
\r
417 { destination: newX, duration: 0 };
\r
418 momentumY = this.hasVScroll ?
\r
419 X_UI_ScrollBox_momentum( this.scrollY, this.startY, duration, this.scrollYMax, this.bounceEnabled ? this.boxHeight * this.fontSize : 0, this.deceleration ) :
\r
420 { destination: newY, duration: 0 };
\r
421 newX = momentumX.destination;
\r
422 newY = momentumY.destination;
\r
423 time = Math.max( momentumX.duration, momentumY.duration ) | 0;
\r
424 this.isInTransition = true;
\r
426 console.log( '慣性無し' );
\r
429 if( newX != this.scrollX || newY != this.scrollY ){
\r
430 // change easing function when scroller goes out of the boundaries
\r
431 if( 0 < newX || newX < this.scrollXMax || 0 < newY || newY < this.scrollYMax ){
\r
432 easing = 'quadratic';
\r
435 console.log( 'end2 x:' + newX + ' y:' + newY + ' t:' + time );
\r
436 this.scrollTo( newX, newY, time, easing, 1000 );
\r
440 console.log( 'end1 x:' + newX + ' y:' + newY );
\r
441 this.scrollTo( newX, newY, 0, '', 1000 ); // ensures that the last position is rounded
\r
443 this[ 'dispatch' ]( XUI_Event.SCROLL_END );
\r
448 function X_UI_ScrollBox_resetPosition( that, time ){
\r
449 var x = that.scrollX,
\r
454 if( !that.hasHScroll || 0 < that.scrollX ){
\r
457 if( that.scrollX < that.scrollXMax ){
\r
458 x = that.scrollXMax;
\r
461 if( !that.hasVScroll || 0 < that.scrollY ){
\r
464 if( that.scrollY < that.scrollYMax ){
\r
465 y = that.scrollYMax;
\r
468 if( x === that.scrollX && y === that.scrollY ){
\r
469 console.log( 'no バウンド y:' + y + ' max:' + that.scrollYMax );
\r
473 console.log( ' ===> resetPosition - バウンド!' );
\r
474 console.log( ' x:' + x + ' y:' + y );
\r
475 that.scrollTo( x, y, time, that.bounceEasing, 1000 );
\r
480 function X_UI_ScrollBox_onAnimeEnd( e ){
\r
481 if( e.target !== this.xnodeSlider || !this.isInTransition ){
\r
482 return X_CALLBACK_NONE;
\r
484 if( !X_UI_ScrollBox_resetPosition( this, this.bounceTime ) ){
\r
485 this.isInTransition = false;
\r
486 this[ 'dispatch' ]( XUI_Event.SCROLL_END );
\r
488 return X_CALLBACK_NONE;
\r
491 function X_UI_ScrollBox_momentum( current, start, time, lowerMargin, wrapperSize, deceleration ){
\r
492 var distance = current - start,
\r
493 speed = Math.abs( distance ) / time,
\r
497 deceleration = deceleration === undefined ? 0.0006 : deceleration;
\r
499 destination = current + ( speed * speed ) / ( 2 * deceleration ) * ( distance < 0 ? -1 : 1 );
\r
500 duration = speed / deceleration;
\r
502 if( destination < lowerMargin ){
\r
503 destination = wrapperSize ? lowerMargin - ( wrapperSize / 2.5 * ( speed / 8 ) ) : lowerMargin;
\r
504 distance = Math.abs( destination - current );
\r
505 duration = distance / speed;
\r
507 if ( destination > 0 ) {
\r
508 destination = wrapperSize ? wrapperSize / 2.5 * ( speed / 8 ) : 0;
\r
509 distance = Math.abs( current ) + destination;
\r
510 duration = distance / speed;
\r
514 destination : Math.round( destination ),
\r
515 duration : duration
\r
519 X.UI.ScrollBox = X.UI.ChromeBox.inherits(
\r
523 Constructor : function(){
\r
524 var supports, slider;
\r
526 if( XUI_ScrollBox.prototype.usableAttrs === XUI_ChromeBox.prototype.usableAttrs ){
\r
527 XUI_ScrollBox.prototype.usableAttrs = supports = XUI_Attr_createAttrDef( XUI_Attr_Support, X_UI_ScrollBox_SUPPORT_ATTRS );
\r
529 XUI_ScrollBox.prototype.attrClass = XUI_Attr_preset( XUI_Box.prototype.attrClass, supports, { width : '100%', height : '100%', bgColor : 0x111111 } );
\r
533 XUI_Layout_Vertical,
\r
535 name : 'ScrollBox-Scroller',
\r
536 role : 'container',
\r
543 i = arguments.length,
\r
547 arg = arguments[ --i ];
\r
548 if( arg[ 'instanceOf' ] && arg[ 'instanceOf' ]( XUI_LayoutBase ) ){
\r
551 if( arg[ 'instanceOf' ] && arg[ 'instanceOf' ]( X.UI.AbstractUINode ) ){
\r
552 args[ args.length ] = arg;
\r
554 if( X_Type_isObject( arg ) ){
\r
555 args[ args.length ] = attr = arg;
\r
556 slider = attr.scrollSlider;
\r
566 slider || X.UI.VBox.apply( 0, args )
\r
571 //attr && this.attr( attr );
\r
573 scrollX : function(){
\r
576 scrollY : function(){
\r
579 scrollWidth : function(){
\r
582 scrollHeight : function(){
\r
585 scrollTo : function( nodeOrX, y ){
\r