OSDN Git Service

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