OSDN Git Service

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