OSDN Git Service

Version 0.6.193, bug fixes X.Node.Anime.
[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                                         XUI_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, XUI_rootData, XUI_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                         // shadow の listen には this が必要!\r
228                         // TODO AbstractUI の listen が悪い!\r
229                         this[ 'listen' ]( XUI_Event._POINTER_DOWN, this, X_UI_ScrollBox_onStart )\r
230                                 [ 'listen' ]( XUI_Event.DRAG, this, X_UI_ScrollBox_onMove );\r
231         this[ 'listen' ]( XUI_Event.DRAG_END, this, X_UI_ScrollBox_onEnd );\r
232                         XUI_rootData[ 'listen' ]( XUI_Event.LAYOUT_BEFORE, this, X_UI_ScrollBox_onLayoutBefore );\r
233                         \r
234                         X_UI_ScrollBox_translate( this, this.scrollXMax * this.scrollXRatio, this.scrollYMax * this.scrollYRatio, 100, '', 300 );\r
235                         this.scrolling = true;\r
236                 };\r
237         } else\r
238         // scroll 不要\r
239         if( this.scrolling ){\r
240                 // scroller 削除\r
241                 this[ 'unlisten' ]( XUI_Event._POINTER_DOWN, this, X_UI_ScrollBox_onStart )\r
242                         [ 'unlisten' ]( XUI_Event.DRAG, this, X_UI_ScrollBox_onMove );\r
243         this[ 'unlisten' ]( XUI_Event.DRAG_END, this, X_UI_ScrollBox_onEnd );\r
244                 XUI_rootData[ 'unlisten' ]( XUI_Event.LAYOUT_BEFORE, this, X_UI_ScrollBox_onLayoutBefore );\r
245                 \r
246                 ( this.scrollX !== 0 || this.scrollY !== 0 ) && X_UI_ScrollBox_translate( this, 0, 0, 100, '', 300 );\r
247                 \r
248                 delete this.scrolling;\r
249                 delete this.scrollXRatio;\r
250                 delete this.scrollYRatio;\r
251         };\r
252 };\r
253 \r
254 // TODO use scrollLeft, scrollTop\r
255 function X_UI_ScrollBox_translate( that, x, y, opt_time, opt_easing, opt_release ){\r
256         var scrollBoxH = that.fontSize * that.boxHeight,\r
257                 scrollBoxW = that.fontSize * that.boxWidth,\r
258                 indicatorH, indicatorW;\r
259         \r
260         opt_time    = 0 <= opt_time ? opt_time : 0;\r
261         opt_easing  = opt_easing === '' ? '' : opt_easing || 'circular';\r
262         opt_release = 0 <= opt_release ? opt_release : 300;\r
263         \r
264         that.xnodeSlider[ 'animate' ](\r
265                 {\r
266                         x : that.scrollX,\r
267                         y : that.scrollY\r
268                 },\r
269                 {\r
270                         x : x,\r
271                         y : y\r
272                 },\r
273                 opt_time, opt_easing, opt_release\r
274         );\r
275 \r
276         if( X_UA[ 'IE' ] < 6 ){\r
277                 XUI_ScrollBox_indicatorV && XUI_ScrollBox_indicatorV[ 'css' ]( 'left', ( scrollBoxW - that.fontSize * 0.6 | 0 ) + 'px' );\r
278                 XUI_ScrollBox_indicatorH && XUI_ScrollBox_indicatorH[ 'css' ]( 'top' , ( scrollBoxH - that.fontSize * 0.6 | 0 ) + 'px' );\r
279         };\r
280 \r
281         if( that.hasVScroll && XUI_ScrollBox_indicatorV ){\r
282                 indicatorH = scrollBoxH * scrollBoxH / ( - that.scrollYMax + scrollBoxH );\r
283                 scrollBoxH -= indicatorH;\r
284 \r
285                 XUI_ScrollBox_indicatorV\r
286                         [ 'css' ]({\r
287                                 height : ( indicatorH | 0 ) + 'px'\r
288                         })\r
289                         [ 'animate' ](\r
290                         {\r
291                                 y : scrollBoxH * that.scrollY / that.scrollYMax\r
292                         },\r
293                         {\r
294                                 y : scrollBoxH * y / that.scrollYMax,\r
295                                 opacity : 1\r
296                         },\r
297                         opt_time, opt_easing, opt_release\r
298                 );\r
299         };\r
300         if( that.hasHScroll && XUI_ScrollBox_indicatorH ){\r
301                 indicatorW = scrollBoxW * scrollBoxW / ( - that.scrollXMax + scrollBoxW );\r
302                 scrollBoxW -= indicatorW;\r
303                 XUI_ScrollBox_indicatorH\r
304                         [ 'css' ]({\r
305                                 width : ( indicatorW | 0 ) + 'px'\r
306                         })\r
307                         [ 'animate' ](\r
308                         {\r
309                                 x : scrollBoxW * that.scrollX / that.scrollXMax\r
310                         },\r
311                         {\r
312                                 x : scrollBoxW * x / that.scrollXMax,\r
313                                 opacity : 1\r
314                         },\r
315                         opt_time, opt_easing, opt_release\r
316                 );\r
317         };\r
318         \r
319         that.scrollX   = x;\r
320         that.scrollXEm = x / that.fontSize;\r
321         that.scrollY   = y;\r
322         that.scrollYEm = y / that.fontSize;\r
323 };\r
324 \r
325 function X_UI_ScrollBox_onStart( e ){\r
326         var ret = X_CALLBACK_NONE;\r
327         \r
328         if( !this.scrollEnabled || ( this.initiated && e.pointerType !== this.initiated ) ){\r
329                 return ret;\r
330         };\r
331 \r
332         this.initiated       = e.pointerType;\r
333         this.moved                   = false;\r
334         this.distX                   = 0;\r
335         this.distY                   = 0;\r
336         this.directionX      = 0;\r
337         this.directionY      = 0;\r
338         this.directionLocked = '';\r
339         this.startTime       = X_Timer_now();\r
340 \r
341         // スクロール中の停止\r
342         if( this.isInTransition ){\r
343                 this.isInTransition = false;\r
344                 //console.log( '-1-' );\r
345                 this[ 'dispatch' ]( XUI_Event.SCROLL_END );\r
346                 // TODO current位置\r
347                 this.xnodeSlider[ 'stop' ]();\r
348         };                      \r
349 \r
350         this.startX    = this.scrollX;\r
351         this.startY    = this.scrollY;\r
352         this.absStartX = this.scrollX;\r
353         this.absStartY = this.scrollY;\r
354         this.pointX    = e.pageX;\r
355         this.pointY    = e.pageY;\r
356         \r
357         console.log( 'scrollstart ' + e.pageY + e.target.className() );\r
358 \r
359         return ret | X_CALLBACK_PREVENT_DEFAULT;\r
360 };\r
361 \r
362 function X_UI_ScrollBox_onMove( e ){\r
363         var ret = X_CALLBACK_NONE,\r
364                 deltaX, deltaY, timestamp,\r
365                 newX, newY,\r
366                 absDistX, absDistY;\r
367         // 規定以上の move でスクロール開始\r
368 \r
369 //console.log( 'scrollmove ' + e.buttons + ' ' + e.button );\r
370 \r
371         if( !this.scrollEnabled || e.pointerType !== this.initiated ){\r
372                 return ret;\r
373         };\r
374 \r
375         // gpu の用意\r
376         if( !this.xnodeSlider[ '_anime' ] ){\r
377                 //console.log( 'gpuレイヤーの用意 ' + e.pageY );\r
378                 //console.log( 'mov1 x:' + this.scrollX + ' y:' + this.scrollY );\r
379                 X_UI_ScrollBox_translate( this, this.scrollX, this.scrollY, 0, '', 300 );\r
380                 return ret;\r
381         };\r
382 \r
383         deltaX          = e.pageX - this.pointX;\r
384         deltaY          = e.pageY - this.pointY;\r
385         timestamp       = X_Timer_now();\r
386 \r
387         this.pointX     = e.pageX;\r
388         this.pointY     = e.pageY;\r
389 \r
390         this.distX      += deltaX;\r
391         this.distY      += deltaY;\r
392         absDistX        = Math.abs(this.distX);\r
393         absDistY        = Math.abs(this.distY);\r
394         \r
395         // We need to move at least 10 pixels for the scrolling to initiate\r
396         if( 300 < timestamp - this.endTime && ( absDistX < 10 && absDistY < 10 ) ){\r
397                 return ret;\r
398         };\r
399 \r
400         // If you are scrolling in one direction lock the other\r
401         if( !this.directionLocked ){\r
402                 if( absDistX > absDistY + this.directionLockThreshold ){\r
403                         this.directionLocked = 'h';             // lock horizontally\r
404                 } else\r
405                 if( absDistY >= absDistX + this.directionLockThreshold ){\r
406                         this.directionLocked = 'v';             // lock vertically\r
407                 } else {\r
408                         this.directionLocked = 'n';             // no lock\r
409                 };\r
410         };\r
411 \r
412         if( this.directionLocked === 'h' ){\r
413                 deltaY = 0;\r
414         } else\r
415         if( this.directionLocked === 'v' ){\r
416                 deltaX = 0;\r
417         };\r
418 \r
419         deltaX = this.hasHScroll ? deltaX : 0;\r
420         deltaY = this.hasVScroll ? deltaY : 0;\r
421 \r
422         if( !this.moved ){\r
423                 this[ 'dispatch' ]( XUI_Event.SCROLL_BEFORE_MOVE );\r
424                 this.moved  = true;\r
425                 this.minusX = deltaX;\r
426                 this.minusY = deltaY;\r
427                 // indicator\r
428                 XUI_ScrollBox_start( this );\r
429         } else {\r
430                 this[ 'dispatch' ]( XUI_Event.SCROLL_MOVE );\r
431         };\r
432 \r
433         newX = this.scrollX + deltaX;// - this.minusX;\r
434         newY = this.scrollY + deltaY;// - this.minusY;\r
435 \r
436         // Slow down if outside of the boundaries\r
437         if( 0 < newX || newX < this.scrollXMax ){\r
438                 newX = this.bounceEnabled ? this.scrollX + ( deltaX ) / 3 : 0 < newX ? 0 : this.scrollXMax;\r
439         };\r
440         \r
441         if( 0 < newY || newY < this.scrollYMax ){\r
442                 //console.log( 'slow... ' + newY + ' ' + this.scrollYMax );\r
443                 newY = this.bounceEnabled ? this.scrollY + ( deltaY ) / 3 : 0 < newY ? 0 : this.scrollYMax;\r
444         };\r
445 \r
446         this.directionX = 0 < deltaX ? -1 : deltaX < 0 ? 1 : 0;\r
447         this.directionY = 0 < deltaY ? -1 : deltaY < 0 ? 1 : 0;\r
448 \r
449         //console.log( 'mov2 x:' + newX + ' y:' + newY );\r
450         X_UI_ScrollBox_translate( this, newX, newY, 0, '', 300 );\r
451 \r
452         if( 300 < timestamp - this.startTime ){\r
453                 this.startTime = timestamp;\r
454                 this.startX = this.scrollX;\r
455                 this.startY = this.scrollY;\r
456         };\r
457         // イベントの拘束\r
458         return ret | X_CALLBACK_PREVENT_DEFAULT | X_CALLBACK_CAPTURE_POINTER;\r
459 };\r
460 \r
461 function X_UI_ScrollBox_onEnd( e ){\r
462         var ret    = X_CALLBACK_NONE,\r
463                 time   = 0,\r
464                 easing = '',\r
465                 newX, newY,\r
466                 momentumX, momentumY,\r
467                 duration, distanceX, distanceY;\r
468         \r
469         //console.log( e.type + ' onend ' + XUI_Event.POINTER_OUT  );\r
470         \r
471         if( !this.scrollEnabled || e.pointerType !== this.initiated ){\r
472                 //console.log( e.type + ' onend 1 ' + e.pointerType + ' ' + this.initiated  );\r
473                 return ret;\r
474         };\r
475 \r
476         delete this.isInTransition;\r
477         delete this.initiated;\r
478         this.endTime = X_Timer_now();                   \r
479 \r
480         duration  = this.endTime - this.startTime;\r
481         newX      = Math.round( this.scrollX );\r
482         newY      = Math.round( this.scrollY );\r
483         distanceX = Math.abs(newX - this.startX);\r
484         distanceY = Math.abs(newY - this.startY);\r
485 \r
486         // reset if we are outside of the boundaries\r
487         if( X_UI_ScrollBox_resetPosition( this, this.bounceTime ) ){\r
488                 //console.log( e.type + ' onend 2 ' + XUI_Event.POINTER_OUT  );\r
489                 return ret | X_CALLBACK_PREVENT_DEFAULT | X_CALLBACK_RELEASE_POINTER;\r
490         };\r
491 \r
492         // we scrolled less than 10 pixels\r
493         if( !this.moved ){\r
494                 this[ 'dispatch' ]( X_EVENT_CANCELED );\r
495                 //console.log( 'we scrolled less than 10 pixels ' + e.pageY );\r
496                 return ret | X_CALLBACK_PREVENT_DEFAULT | X_CALLBACK_RELEASE_POINTER;\r
497         };\r
498 \r
499         // start momentum animation if needed\r
500         if( this.momentumEnabled && duration < 300 ){\r
501                 momentumX = this.hasHScroll ?\r
502                                                 X_UI_ScrollBox_momentum( this.scrollX, this.startX, duration, this.scrollXMax, this.bounceEnabled ? this.boxWidth  * this.fontSize : 0, this.deceleration ) :\r
503                                                 { destination: newX, duration: 0 };\r
504                 momentumY = this.hasVScroll   ?\r
505                                                 X_UI_ScrollBox_momentum( this.scrollY, this.startY, duration, this.scrollYMax, this.bounceEnabled ? this.boxHeight * this.fontSize : 0, this.deceleration ) :\r
506                                                 { destination: newY, duration: 0 };\r
507                 newX = momentumX.destination;\r
508                 newY = momentumY.destination;\r
509                 time = Math.max( momentumX.duration, momentumY.duration ) | 0;\r
510                 this.isInTransition = true;\r
511         } else {\r
512                 //console.log( '慣性無し' );\r
513         };\r
514 \r
515         if( newX != this.scrollX || newY != this.scrollY ){\r
516                 // change easing function when scroller goes out of the boundaries\r
517                 if( 0 < newX || newX < this.scrollXMax || 0 < newY || newY < this.scrollYMax ){\r
518                         easing = 'quadratic';\r
519                 };\r
520 \r
521                 //console.log( 'end2 x:' + newX + ' y:' + newY + '<-y:' + this.scrollY + ' t:' + time );\r
522                 this.scrollTo( newX, newY, time, easing, 1000 );\r
523         } else {\r
524                 //console.log( 'end1 x:' + newX + ' y:' + newY );\r
525                 this.scrollTo( newX, newY, 0, '', 1000 );       // ensures that the last position is rounded\r
526                 //console.log( '-3-' );\r
527                 this[ 'dispatch' ]( XUI_Event.SCROLL_END );             \r
528         };\r
529 \r
530         return ret | X_CALLBACK_PREVENT_DEFAULT | X_CALLBACK_RELEASE_POINTER;\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