OSDN Git Service

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