OSDN Git Service

Version 0.6.188, fix X.UI.
[pettanr/clientJs.git] / 0.6.x / js / 20_ui / 15_ScrollBox.js
1 /*\r
2  * scroll 要素は常にひとつ\r
3  * ScrollManager\r
4  * indicatorX, Y は再利用\r
5  */\r
6 var XUI_ScrollBox_current,\r
7         XUI_ScrollBox_indicatorV,\r
8         XUI_ScrollBox_indicatorH;\r
9 \r
10 function XUI_ScrollBox_start( scrollBox ){\r
11         // 既存スクロールの停止\r
12         if( scrollBox.hasVScroll ){\r
13                 if( !XUI_ScrollBox_indicatorV ){\r
14                         XUI_ScrollBox_indicatorV = X_Doc_create( 'div' )[ 'className' ]( 'ScrollBox-IndicatorV' );\r
15                         \r
16                 };\r
17                 XUI_ScrollBox_indicatorV.parent !== scrollBox.xnode && scrollBox.xnode[ 'append' ]( XUI_ScrollBox_indicatorV );\r
18                 XUI_ScrollBox_indicatorV[ 'animate' ](\r
19                                 { opacity : 0 },\r
20                                 { opacity : 1 },\r
21                                 900, 'circular', 300\r
22                         );\r
23                 scrollBox\r
24                         [ 'listenOnce' ]( [ X_EVENT_CANCELED, XUI_Event.SCROLL_END ], XUI_ScrollBox_indicatorV, XUI_ScrollBox_indicatorHandleEvent );\r
25         };\r
26         if( scrollBox.hasHScroll ){\r
27                 if( !XUI_ScrollBox_indicatorH ){\r
28                         XUI_ScrollBox_indicatorH = X_Doc_create( 'div' )[ 'className' ]( 'ScrollBox-IndicatorH' );\r
29                 };\r
30                 XUI_ScrollBox_indicatorH.parent !== scrollBox.xnode && scrollBox.xnode[ 'append' ]( XUI_ScrollBox_indicatorH );\r
31                 XUI_ScrollBox_indicatorH[ 'animate' ](\r
32                                 { opacity : 0 },\r
33                                 { opacity : 1 },\r
34                                 900, 'circular', 300\r
35                         );\r
36                 scrollBox\r
37                         [ 'listenOnce' ]( [ X_EVENT_CANCELED, XUI_Event.SCROLL_END ], XUI_ScrollBox_indicatorH, XUI_ScrollBox_indicatorHandleEvent );\r
38         };\r
39 };\r
40 \r
41 function XUI_ScrollBox_indicatorHandleEvent( e ){\r
42         switch( e.type ){\r
43                 case X_EVENT_CANCELED :\r
44                 case XUI_Event.SCROLL_END :\r
45                         console.log( '-fadeout-' );\r
46                         this[ 'animate' ](\r
47                                 { opacity : 1 },\r
48                                 { opacity : 0 },\r
49                                 900, 'circular', 300\r
50                         );\r
51                         break;\r
52         };\r
53 };\r
54 \r
55 \r
56 var X_UI_ScrollBox_SUPPORT_ATTRS = {\r
57                 // スクロール開始するために必要な移動距離、縦か横、どちらか制限する場合、より重要\r
58                 directionLockThreshold : [     10, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.LENGTH ],\r
59                 scrollXEnabled         : [   true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],\r
60                 scrollYEnabled         : [   true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],\r
61                 scrollEnabled          : [   true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],\r
62                 bounceEnabled          : [   true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],\r
63                 bounceTime             : [    300, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.TIME ],\r
64                 useWheel               : [   true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],\r
65                 useKey                 : [   true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],\r
66                 hasScrollShadow        : [   true, XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.BOOLEAN ],\r
67                 scrollShadowColor      : [ '#000', XUI_Dirty.CLEAN, XUI_Attr_USER.UINODE, XUI_Attr_Type.COLOR ]\r
68         };\r
69 \r
70 var XUI_ScrollBox = XUI_ChromeBox.inherits(\r
71         '_ScrollBox',\r
72         X_Class.NONE,\r
73         {\r
74                 layout                 : XUI_Layout_Canvas,\r
75                 \r
76                 directionLockThreshold : 10,\r
77                 scrollXEnabled         : true,\r
78                 scrollYEnabled         : true,\r
79                 scrollEnabled          : true,\r
80                 bounceEnabled          : true,\r
81                 momentumEnabled        : true,\r
82                 bounceTime             : 600,\r
83                 useWheel               : true,\r
84                 useKey                 : true,\r
85                 hasScrollShadow        : true,\r
86                 scrollShadowColor      : '#000',\r
87                 \r
88                 scrolling       : false,\r
89                 \r
90                 initiated       : '',\r
91                 moved               : false,\r
92                 directionLocked : '',\r
93                 startTime       : 0,\r
94                 endTime         : 0,\r
95                 isInTransition  : false,\r
96 \r
97                 hasHScroll      : false,\r
98                 hasVScroll      : false,\r
99 \r
100                 wrapperOffset   : 0,\r
101                 wheelTimeout    : 0,\r
102                 requestFrameID  : 0,\r
103                 \r
104                 fontSize        : 0,\r
105                 \r
106                 scrollX         : 0, // px\r
107                 scrollY         : 0, // px\r
108                 scrollXMax      : 0, // px\r
109                 scrollYMax      : 0, // px\r
110                 scrollXRatio    : 0, // この値は scroll 不要になっても保持される。 scroll 必要時に参照される\r
111                 scrollYRatio    : 0,            \r
112                 startX          : 0, // px\r
113                 startY          : 0, // px\r
114                 absStartX       : 0, // px\r
115                 absStartY       : 0, // px\r
116                 pointX          : 0, // px\r
117                 pointY          : 0, // px\r
118                 distX               : 0, // px\r
119                 distY               : 0, // px\r
120                 directionX      : 0, // -1, 0, 1\r
121                 directionY      : 0, // -1, 0, 1\r
122                 \r
123                 lastScrollWidth  : 0,\r
124                 lastScrollHeight : 0,\r
125                 lastBoxWidth     : 0,\r
126                 lastBoxHeight    : 0,\r
127                 \r
128                 _containerNode   : null,\r
129                 xnodeSlider      : null,\r
130                 \r
131                 Constructor : function( user, layout, args ){\r
132                         this[ 'Super' ]( user, layout, args );\r
133                         this._containerNode = X_Pair_get( this.containerNode );\r
134                         this.xnodeSlider = this._containerNode.xnode[ 'className' ]( 'ScrollSlider' ).listen( X_EVENT_ANIME_END, this, X_UI_ScrollBox_onAnimeEnd );\r
135                         this.xnode[ 'className' ]( 'ScrollBox' );\r
136                 },\r
137                 \r
138                 creationComplete : function(){\r
139                         XUI_Box.prototype.creationComplete.apply( this, arguments );\r
140                 },\r
141                 \r
142                 calculate : function(){\r
143                         this.lastScrollWidth  = this._containerNode.boxWidth;\r
144                         this.lastScrollHeight = this._containerNode.boxHeight;\r
145                         this.lastBoxWidth     = this.boxWidth;\r
146                         this.lastBoxHeight    = this.boxHeight;\r
147                         \r
148                         XUI_Box.prototype.calculate.apply( this, arguments );\r
149                         \r
150                         // TODO root の layout_complete 後に。\r
151                         // TODO calculate 前に scroll の解放。\r
152                         \r
153                         if(\r
154                                         this.lastScrollWidth  !== this._containerNode.boxWidth ||\r
155                                         this.lastScrollHeight !== this._containerNode.boxHeight ||\r
156                                         this.lastBoxWidth    !== this.boxWidth    || this.lastBoxHeight    !== this.boxHeight\r
157                                 ){\r
158                                         console.log( 'scroll - calc' );\r
159                                         X_UI_rootData[ 'listenOnce' ]( XUI_Event.LAYOUT_COMPLETE, this, X_UI_ScrollBox_onLayoutComplete );\r
160                                 };\r
161                 },\r
162         \r
163                 scrollTo : function( x, y, opt_time, opt_easing, opt_release ){\r
164                         //if( this.scrollX === x && this.scrollY === y ) return;\r
165                         \r
166                         opt_time    = 0 <= opt_time ? opt_time : 0;\r
167                         opt_easing  = opt_easing || 'circular';\r
168                         opt_release = 0 <= opt_release ? opt_release : 300;\r
169         \r
170                         this.isInTransition = 0 < opt_time;\r
171         \r
172                         X_UI_ScrollBox_translate( this, x, y, opt_time, opt_easing, opt_release );\r
173                 },\r
174                 \r
175                 _remove : function(){\r
176                         XUI_AbstractUINode.prototype._remove.apply( this, arguments );\r
177                         if( this.scrolling ){\r
178                                 // remove scroll\r
179                         };\r
180                 }\r
181                 \r
182         }\r
183 );\r
184 \r
185 function X_UI_ScrollBox_onLayoutBefore( e ){\r
186         if( e[ 'cancelable' ] && this.isInTransition && X_Node_Anime_translateZ ){\r
187                 this[ 'listenOnce' ]( XUI_Event.SCROLL_END, X_UI_rootData, X_UI_rootData.calculate );\r
188                 return X_CALLBACK_PREVENT_DEFAULT;\r
189         };\r
190         this.scrollXRatio = this.scrollX / this.scrollXMax;\r
191         this.scrollYRatio = this.scrollY / this.scrollYMax;\r
192         this.xnodeSlider[ 'stop' ]();\r
193         this.isInTransition = false;\r
194         return X_CALLBACK_NONE;\r
195 };\r
196 \r
197 function X_UI_ScrollBox_onLayoutComplete( e ){\r
198         // scroll の停止、GPU の解除\r
199         var font = this.fontSize = this.xnodeSlider[ 'call' ]( 'fontSize' );\r
200 \r
201         this.scrollXMax = ( this.boxWidth  - this._containerNode.boxWidth )  * font | 0;\r
202         this.scrollYMax = ( this.boxHeight - this._containerNode.boxHeight ) * font | 0;\r
203 \r
204         this.hasHScroll = this.scrollXEnabled && ( this.scrollXMax < -1 ); // < 0 だと \r
205         this.hasVScroll = this.scrollYEnabled && ( this.scrollYMax < -1 );\r
206 \r
207         if( !this.hasHScroll ){\r
208                 this.scrollXMax  = 0;\r
209         };\r
210 \r
211         if( !this.hasVScroll ){\r
212                 this.scrollYMax   = 0;\r
213         };\r
214 \r
215         delete this.endTime;\r
216         delete this.directionX;\r
217         delete this.directionY;\r
218 \r
219         X_UI_ScrollBox_resetPosition( this, 0 );\r
220 \r
221         if( this.hasHScroll || this.hasVScroll ){\r
222                 // scroll が必要。\r
223                 if( this.scrolling ){\r
224                         X_UI_ScrollBox_translate( this, this.scrollXMax * this.scrollXRatio, this.scrollYMax * this.scrollYRatio, 100, '', 300 );\r
225                 } else {\r
226                         // scroller 作る\r
227                         this[ 'listen' ]( XUI_Event._POINTER_DOWN, this, X_UI_ScrollBox_onStart );\r
228                         X_UI_rootData[ 'listen' ]( XUI_Event.LAYOUT_BEFORE, this, X_UI_ScrollBox_onLayoutBefore );\r
229                         \r
230                         X_UI_ScrollBox_translate( this, this.scrollXMax * this.scrollXRatio, this.scrollYMax * this.scrollYRatio, 100, '', 300 );\r
231                         this.scrolling = true;\r
232                 };\r
233         } else\r
234         // scroll 不要\r
235         if( this.scrolling ){\r
236                 // scroller 削除\r
237                 this[ 'unlisten' ]( XUI_Event._POINTER_DOWN, this, X_UI_ScrollBox_onStart );\r
238                 X_UI_rootData[ 'unlisten' ]( XUI_Event.LAYOUT_BEFORE, this, X_UI_ScrollBox_onLayoutBefore );\r
239                 \r
240                 ( this.scrollX !== 0 || this.scrollY !== 0 ) && X_UI_ScrollBox_translate( this, 0, 0, 100, '', 300 );\r
241                 \r
242                 delete this.scrolling;\r
243                 delete this.scrollXRatio;\r
244                 delete this.scrollYRatio;\r
245         };\r
246 };\r
247 \r
248 // TODO use scrollLeft, scrollTop\r
249 function X_UI_ScrollBox_translate( that, x, y, opt_time, opt_easing, opt_release ){\r
250         var scrollBoxSize, indicatorSize;\r
251         \r
252         opt_time    = 0 <= opt_time ? opt_time : 0;\r
253         opt_easing  = opt_easing === '' ? '' : opt_easing || 'circular';\r
254         opt_release = 0 <= opt_release ? opt_release : 300;\r
255         \r
256         that.xnodeSlider[ 'animate' ](\r
257                 {\r
258                         x : that.scrollX,\r
259                         y : that.scrollY\r
260                 },\r
261                 {\r
262                         x : x,\r
263                         y : y\r
264                 },\r
265                 opt_time, opt_easing, opt_release\r
266         );\r
267 \r
268         if( that.hasVScroll && XUI_ScrollBox_indicatorV ){\r
269                 scrollBoxSize = that.fontSize * that.boxHeight;\r
270                 indicatorSize = scrollBoxSize * scrollBoxSize / ( - that.scrollYMax + scrollBoxSize );\r
271                 scrollBoxSize -= indicatorSize;\r
272                 XUI_ScrollBox_indicatorV\r
273                         [ 'css' ]({\r
274                                 height : ( indicatorSize | 0 ) + 'px'\r
275                         })\r
276                         [ 'animate' ](\r
277                         {\r
278                                 y : scrollBoxSize * that.scrollY / that.scrollYMax\r
279                         },\r
280                         {\r
281                                 y : scrollBoxSize * y / that.scrollYMax,\r
282                                 opacity : 1\r
283                         },\r
284                         opt_time, opt_easing, opt_release\r
285                 );\r
286         };\r
287         if( that.hasHScroll && XUI_ScrollBox_indicatorH ){\r
288                 scrollBoxSize = that.fontSize * that.boxWidth;\r
289                 indicatorSize = scrollBoxSize * scrollBoxSize / ( - that.scrollXMax + scrollBoxSize );\r
290                 scrollBoxSize -= indicatorSize;\r
291                 XUI_ScrollBox_indicatorH\r
292                         [ 'css' ]({\r
293                                 width : ( indicatorSize | 0 ) + 'px'\r
294                         })\r
295                         [ 'animate' ](\r
296                         {\r
297                                 x : scrollBoxSize * that.scrollX / that.scrollXMax\r
298                         },\r
299                         {\r
300                                 x : scrollBoxSize * x / that.scrollXMax,\r
301                                 opacity : 1\r
302                         },\r
303                         opt_time, opt_easing, opt_release\r
304                 );\r
305         };\r
306         \r
307         that.scrollX = x;\r
308         that.scrollY = y;\r
309 };\r
310 \r
311 function X_UI_ScrollBox_onStart( e ){\r
312         var ret = X_CALLBACK_NONE;\r
313 \r
314         // React to left mouse button only\r
315         if( e.pointerType === 'mouse' && e.button !== 0 ){\r
316                 return ret;\r
317         };\r
318         \r
319         if( !this.scrollEnabled || ( this.initiated && e.pointerType !== this.initiated ) ){\r
320                 return ret;\r
321         };\r
322 \r
323         this.initiated       = e.pointerType;\r
324         this.moved                   = false;\r
325         this.distX                   = 0;\r
326         this.distY                   = 0;\r
327         this.directionX      = 0;\r
328         this.directionY      = 0;\r
329         this.directionLocked = '';\r
330         this.startTime       = X_Timer_now();\r
331 \r
332         // スクロール中の停止\r
333         if( this.isInTransition ){\r
334                 this.isInTransition = false;\r
335                 console.log( '-1-' );\r
336                 this[ 'dispatch' ]( XUI_Event.SCROLL_END );\r
337                 // TODO current位置\r
338                 this.xnodeSlider[ 'stop' ]();\r
339         };                      \r
340 \r
341         this.startX    = this.scrollX;\r
342         this.startY    = this.scrollY;\r
343         this.absStartX = this.scrollX;\r
344         this.absStartY = this.scrollY;\r
345         this.pointX    = e.pageX;\r
346         this.pointY    = e.pageY;\r
347         \r
348         console.log( 'scrollstart ' + e.pageY );\r
349         \r
350         this[ 'listen' ]( XUI_Event._POINTER_MOVE, this, X_UI_ScrollBox_onMove );\r
351         this[ 'listen' ]( [ XUI_Event._POINTER_UP, XUI_Event.POINTER_OUT, XUI_Event._POINTER_CANCEL ], this, X_UI_ScrollBox_onEnd );\r
352 \r
353         return ret | X_CALLBACK_PREVENT_DEFAULT;\r
354 };\r
355 \r
356 function X_UI_ScrollBox_onMove( e ){\r
357         var ret = X_CALLBACK_NONE,\r
358                 deltaX, deltaY, timestamp,\r
359                 newX, newY,\r
360                 absDistX, absDistY;\r
361         // 規定以上の move でスクロール開始\r
362 \r
363 //console.log( 'scrollmove ' + e.buttons + ' ' + e.button );\r
364 \r
365         if( !this.scrollEnabled || e.pointerType !== this.initiated ){\r
366                 return ret;\r
367         };\r
368         \r
369         if( e.buttons !== 1 ){\r
370                 return X_UI_ScrollBox_onEnd.call( this, e );\r
371         };\r
372 \r
373         // gpu の用意\r
374         if( !this.xnodeSlider[ '_anime' ] ){\r
375                 //console.log( 'gpuレイヤーの用意 ' + e.pageY );\r
376                 //console.log( 'mov1 x:' + this.scrollX + ' y:' + this.scrollY );\r
377                 X_UI_ScrollBox_translate( this, this.scrollX, this.scrollY, 0, '', 300 );\r
378                 return ret;\r
379         };\r
380 \r
381         deltaX          = e.pageX - this.pointX;\r
382         deltaY          = e.pageY - this.pointY;\r
383         timestamp       = X_Timer_now();\r
384 \r
385         this.pointX     = e.pageX;\r
386         this.pointY     = e.pageY;\r
387 \r
388         this.distX      += deltaX;\r
389         this.distY      += deltaY;\r
390         absDistX        = Math.abs(this.distX);\r
391         absDistY        = Math.abs(this.distY);\r
392         \r
393         // We need to move at least 10 pixels for the scrolling to initiate\r
394         if( 300 < timestamp - this.endTime && ( absDistX < 10 && absDistY < 10 ) ){\r
395                 return ret;\r
396         };\r
397 \r
398         // If you are scrolling in one direction lock the other\r
399         if( !this.directionLocked ){\r
400                 if( absDistX > absDistY + this.directionLockThreshold ){\r
401                         this.directionLocked = 'h';             // lock horizontally\r
402                 } else\r
403                 if( absDistY >= absDistX + this.directionLockThreshold ){\r
404                         this.directionLocked = 'v';             // lock vertically\r
405                 } else {\r
406                         this.directionLocked = 'n';             // no lock\r
407                 };\r
408         };\r
409 \r
410         if( this.directionLocked === 'h' ){\r
411                 deltaY = 0;\r
412         } else\r
413         if( this.directionLocked === 'v' ){\r
414                 deltaX = 0;\r
415         };\r
416 \r
417         deltaX = this.hasHScroll ? deltaX : 0;\r
418         deltaY = this.hasVScroll ? deltaY : 0;\r
419 \r
420         if( !this.moved ){\r
421                 this[ 'dispatch' ]( XUI_Event.SCROLL_BEFORE_MOVE );\r
422                 this.moved  = true;\r
423                 this.minusX = deltaX;\r
424                 this.minusY = deltaY;\r
425                 // indicator\r
426                 XUI_ScrollBox_start( this );\r
427         } else {\r
428                 this[ 'dispatch' ]( XUI_Event.SCROLL_MOVE );\r
429         };\r
430 \r
431         newX = this.scrollX + deltaX;// - this.minusX;\r
432         newY = this.scrollY + deltaY;// - this.minusY;\r
433 \r
434         // Slow down if outside of the boundaries\r
435         if( 0 < newX || newX < this.scrollXMax ){\r
436                 newX = this.bounceEnabled ? this.scrollX + ( deltaX ) / 3 : 0 < newX ? 0 : this.scrollXMax;\r
437         };\r
438         \r
439         if( 0 < newY || newY < this.scrollYMax ){\r
440                 //console.log( 'slow... ' + newY + ' ' + this.scrollYMax );\r
441                 newY = this.bounceEnabled ? this.scrollY + ( deltaY ) / 3 : 0 < newY ? 0 : this.scrollYMax;\r
442         };\r
443 \r
444         this.directionX = 0 < deltaX ? -1 : deltaX < 0 ? 1 : 0;\r
445         this.directionY = 0 < deltaY ? -1 : deltaY < 0 ? 1 : 0;\r
446 \r
447         //console.log( 'mov2 x:' + newX + ' y:' + newY );\r
448         X_UI_ScrollBox_translate( this, newX, newY, 0, '', 300 );\r
449 \r
450         if( 300 < timestamp - this.startTime ){\r
451                 this.startTime = timestamp;\r
452                 this.startX = this.scrollX;\r
453                 this.startY = this.scrollY;\r
454         };\r
455         // イベントの拘束\r
456         return ret | X_CALLBACK_PREVENT_DEFAULT | X_CALLBACK_CAPTURE_POINTER;\r
457 };\r
458 \r
459 function X_UI_ScrollBox_onEnd( e ){\r
460         var ret    = X_CALLBACK_NONE,\r
461                 time   = 0,\r
462                 easing = '',\r
463                 newX, newY,\r
464                 momentumX, momentumY,\r
465                 duration, distanceX, distanceY;\r
466         \r
467         console.log( e.type + ' onend ' + XUI_Event.POINTER_OUT  );\r
468                                         \r
469         this[ 'unlisten' ]( XUI_Event._POINTER_MOVE, this, X_UI_ScrollBox_onMove );\r
470         this[ 'unlisten' ]( [ XUI_Event._POINTER_UP, XUI_Event.POINTER_OUT, XUI_Event._POINTER_CANCEL ], this, X_UI_ScrollBox_onEnd );\r
471         \r
472         if( !this.scrollEnabled || e.pointerType !== this.initiated ){\r
473                 console.log( e.type + ' onend 1 ' + e.pointerType + ' ' + this.initiated  );\r
474                 return ret;\r
475         };\r
476 \r
477         delete this.isInTransition;\r
478         delete this.initiated;\r
479         this.endTime = X_Timer_now();                   \r
480 \r
481         duration  = this.endTime - this.startTime;\r
482         newX      = Math.round( this.scrollX );\r
483         newY      = Math.round( this.scrollY );\r
484         distanceX = Math.abs(newX - this.startX);\r
485         distanceY = Math.abs(newY - this.startY);\r
486 \r
487         // reset if we are outside of the boundaries\r
488         if( X_UI_ScrollBox_resetPosition( this, this.bounceTime ) ){\r
489                 console.log( e.type + ' onend 2 ' + XUI_Event.POINTER_OUT  );\r
490                 return ret;\r
491         };\r
492 \r
493         // we scrolled less than 10 pixels\r
494         if( !this.moved ){\r
495                 this[ 'dispatch' ]( X_EVENT_CANCELED );\r
496                 console.log( 'we scrolled less than 10 pixels ' + e.pageY );\r
497                 return ret;\r
498         };\r
499 \r
500         // start momentum animation if needed\r
501         if( this.momentumEnabled && duration < 300 ){\r
502                 momentumX = this.hasHScroll ?\r
503                                                 X_UI_ScrollBox_momentum( this.scrollX, this.startX, duration, this.scrollXMax, this.bounceEnabled ? this.boxWidth  * this.fontSize : 0, this.deceleration ) :\r
504                                                 { destination: newX, duration: 0 };\r
505                 momentumY = this.hasVScroll   ?\r
506                                                 X_UI_ScrollBox_momentum( this.scrollY, this.startY, duration, this.scrollYMax, this.bounceEnabled ? this.boxHeight * this.fontSize : 0, this.deceleration ) :\r
507                                                 { destination: newY, duration: 0 };\r
508                 newX = momentumX.destination;\r
509                 newY = momentumY.destination;\r
510                 time = Math.max( momentumX.duration, momentumY.duration ) | 0;\r
511                 this.isInTransition = true;\r
512         } else {\r
513                 console.log( '慣性無し' );\r
514         };\r
515 \r
516         if( newX != this.scrollX || newY != this.scrollY ){\r
517                 // change easing function when scroller goes out of the boundaries\r
518                 if( 0 < newX || newX < this.scrollXMax || 0 < newY || newY < this.scrollYMax ){\r
519                         easing = 'quadratic';\r
520                 };\r
521 \r
522                 console.log( 'end2 x:' + newX + ' y:' + newY + '<-y:' + this.scrollY + ' t:' + time );\r
523                 this.scrollTo( newX, newY, time, easing, 1000 );\r
524         } else {\r
525                 console.log( 'end1 x:' + newX + ' y:' + newY );\r
526                 this.scrollTo( newX, newY, 0, '', 1000 );       // ensures that the last position is rounded\r
527                 console.log( '-3-' );\r
528                 this[ 'dispatch' ]( XUI_Event.SCROLL_END );             \r
529         };\r
530         return ret;\r
531 };\r
532 \r
533 function X_UI_ScrollBox_resetPosition( that, time ){\r
534         var x = that.scrollX,\r
535                 y = that.scrollY;\r
536 \r
537         time = time || 0;\r
538 \r
539         if( !that.hasHScroll || 0 < that.scrollX ){\r
540                 x = 0;\r
541         } else\r
542         if( that.scrollX < that.scrollXMax ){\r
543                 x = that.scrollXMax;\r
544         };\r
545 \r
546         if( !that.hasVScroll || 0 < that.scrollY ){\r
547                 y = 0;\r
548         } else\r
549         if( that.scrollY < that.scrollYMax ){\r
550                 y = that.scrollYMax;\r
551         };\r
552 \r
553         if( x === that.scrollX && y === that.scrollY ){\r
554                 console.log( 'no バウンド y:' + y + ' max:' + that.scrollYMax );\r
555                 return false;\r
556         };\r
557 \r
558         console.log( ' ===> resetPosition - バウンド!' );\r
559         console.log( '      x:' + x + ' y:' + y );\r
560         that.scrollTo( x, y, time, that.bounceEasing, 1000 );\r
561 \r
562         return true;\r
563 };\r
564 \r
565 function X_UI_ScrollBox_onAnimeEnd( e ){\r
566         if( e.target !== this.xnodeSlider ){\r
567                 console.log( '- not much -' );\r
568                 console.dir(e);\r
569         };\r
570         if( e.target === this.xnodeSlider && this.isInTransition && !X_UI_ScrollBox_resetPosition( this, this.bounceTime ) ){\r
571                 this.isInTransition = false;\r
572                 console.log( '-2-' );\r
573                 this[ 'dispatch' ]( XUI_Event.SCROLL_END );\r
574         };\r
575         console.log(' -2.1- '+this.isInTransition );\r
576         return X_CALLBACK_NONE;\r
577 };\r
578 \r
579 function X_UI_ScrollBox_momentum( current, start, time, lowerMargin, wrapperSize, deceleration ){\r
580         var distance = current - start,\r
581                 speed    = Math.abs( distance ) / time,\r
582                 destination,\r
583                 duration;\r
584 \r
585         deceleration = deceleration === undefined ? 0.0006 : deceleration;\r
586 \r
587         destination  = current + ( speed * speed ) / ( 2 * deceleration ) * ( distance < 0 ? -1 : 1 );\r
588         duration     = speed / deceleration;\r
589 \r
590         if( destination < lowerMargin ){\r
591                 destination = wrapperSize ? lowerMargin - ( wrapperSize / 2.5 * ( speed / 8 ) ) : lowerMargin;\r
592                 distance    = Math.abs( destination - current );\r
593                 duration    = distance / speed;\r
594         } else\r
595         if ( destination > 0 ) {\r
596                 destination = wrapperSize ? wrapperSize / 2.5 * ( speed / 8 ) : 0;\r
597                 distance    = Math.abs( current ) + destination;\r
598                 duration    = distance / speed;\r
599         };\r
600 \r
601         return {\r
602                 destination : Math.round( destination ),\r
603                 duration    : duration\r
604         };\r
605 };\r
606 \r
607 X.UI.ScrollBox = X.UI.ChromeBox.inherits(\r
608         'ScrollBox',\r
609         X_Class.NONE,\r
610         {\r
611                 Constructor : function(){\r
612                         var supports, slider;\r
613                         \r
614                         if( XUI_ScrollBox.prototype.usableAttrs === XUI_ChromeBox.prototype.usableAttrs ){\r
615                                 XUI_ScrollBox.prototype.usableAttrs = supports = XUI_Attr_createAttrDef( XUI_Attr_Support, X_UI_ScrollBox_SUPPORT_ATTRS );\r
616                 \r
617                                 XUI_ScrollBox.prototype.attrClass   = XUI_Attr_preset( XUI_Box.prototype.attrClass, supports, { width  : '100%', height : '100%', bgColor : 0x111111 } );\r
618                         };\r
619                         \r
620                         var args = [\r
621                                                 XUI_Layout_Vertical,                    \r
622                                                 {\r
623                                                         name      : 'ScrollBox-Scroller',\r
624                                                         role      : 'container',\r
625                                                         width     : 'auto',\r
626                                                         minWidth  : '100%',\r
627                                                         height    : 'auto',\r
628                                                         minHeight : '100%'\r
629                                                 }\r
630                                         ],\r
631                                 l    = arguments.length, i = 0, j = 1,\r
632                                 arg, attr;\r
633                         \r
634                         for( ; i < l; ++i ){\r
635                                 arg = arguments[ i ];\r
636                                 if( arg[ 'instanceOf' ] && arg[ 'instanceOf' ]( XUI_LayoutBase ) ){\r
637                                         args[ 0 ] = arg;\r
638                                 } else\r
639                                 if( arg[ 'instanceOf' ] && arg[ 'instanceOf' ]( X.UI.AbstractUINode ) ){\r
640                                         args[ ++j ] = arg;\r
641                                 } else\r
642                                 if( X_Type_isObject( arg ) ){\r
643                                         args[ ++j ] = attr = arg;\r
644                                         slider = attr.scrollSlider;\r
645                                 };\r
646                         };\r
647                         \r
648                         X_Pair_create(\r
649                                 this,\r
650                                 XUI_ScrollBox(\r
651                                         this,\r
652                                         null,\r
653                                         [\r
654                                                 slider || X.UI.VBox.apply( 0, args )\r
655                                         ]\r
656                                 )\r
657                         );\r
658                         \r
659                         //attr && this.attr( attr );\r
660                 },\r
661                 scrollX  : function(){\r
662                         \r
663                 },\r
664                 scrollY  : function(){\r
665                         \r
666                 },\r
667                 scrollWidth : function(){\r
668                         \r
669                 },\r
670                 scrollHeight : function(){\r
671                         \r
672                 },\r
673                 scrollTo : function( nodeOrX, y ){\r
674                         \r
675                 }\r
676         }\r
677 );\r
678 \r