build: Module build implementation
[platform/framework/web/web-ui-fw.git] / src / js / jquery.mobile.tizen.scrollview.js
1 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
2 //>>description: Implements scroll by javascript
3 //>>label: Scrollview
4 //>>group: Tizen:Core
5
6 define( [ ], function ( ) {
7 //>>excludeEnd("jqmBuildExclude");
8
9 /*
10 * jQuery Mobile Framework : scrollview plugin
11 * Copyright (c) 2010 Adobe Systems Incorporated - Kin Blas (jblas@adobe.com)
12 * Dual licensed under the MIT (MIT-LICENSE.txt) and GPL (GPL-LICENSE.txt) licenses.
13 * Note: Code is in draft form and is subject to change
14 * Modified by Koeun Choi <koeun.choi@samsung.com>
15 * Modified by Minkyu Kang <mk7.kang@samsung.com>
16 */
17
18 (function ( $, window, document, undefined ) {
19
20         function resizePageContentHeight( page ) {
21                 var $page = $( page ),
22                         $content = $page.children(".ui-content"),
23                         hh = $page.children(".ui-header").outerHeight() || 0,
24                         fh = $page.children(".ui-footer").outerHeight() || 0,
25                         pt = parseFloat( $content.css("padding-top") ),
26                         pb = parseFloat( $content.css("padding-bottom") ),
27                         wh = $( window ).height();
28
29                 $content.height( wh - (hh + fh) - (pt + pb) );
30         }
31
32         function MomentumTracker( options ) {
33                 this.options = $.extend( {}, options );
34                 this.easing = "easeOutQuad";
35                 this.reset();
36         }
37
38         var tstates = {
39                 scrolling: 0,
40                 overshot:  1,
41                 snapback:  2,
42                 done:      3
43         };
44
45         function getCurrentTime() {
46                 return Date.now();
47         }
48
49         jQuery.widget( "tizen.scrollview", jQuery.mobile.widget, {
50                 options: {
51                         direction:         null,  // "x", "y", or null for both.
52
53                         timerInterval:     10,
54                         scrollDuration:    1000,  // Duration of the scrolling animation in msecs.
55                         overshootDuration: 250,   // Duration of the overshoot animation in msecs.
56                         snapbackDuration:  500,   // Duration of the snapback animation in msecs.
57
58                         moveThreshold:     30,   // User must move this many pixels in any direction to trigger a scroll.
59                         moveIntervalThreshold:     150,   // Time between mousemoves must not exceed this threshold.
60
61                         scrollMethod:      "translate",  // "translate", "position"
62                         startEventName:    "scrollstart",
63                         updateEventName:   "scrollupdate",
64                         stopEventName:     "scrollstop",
65
66                         eventType:         $.support.touch ? "touch" : "mouse",
67
68                         showScrollBars:    true,
69                         overshootEnable:   false,
70                         outerScrollEnable: true,
71                         overflowEnable:    true,
72                         scrollJump:        false,
73                 },
74
75                 _getViewHeight: function () {
76                         return this._$view.height();
77                 },
78
79                 _makePositioned: function ( $ele ) {
80                         if ( $ele.css("position") === "static" ) {
81                                 $ele.css( "position", "relative" );
82                         }
83                 },
84
85                 _create: function () {
86                         var direction,
87                                 self = this;
88
89                         this._$clip = $( this.element ).addClass("ui-scrollview-clip");
90
91                         if ( this._$clip.children(".ui-scrollview-view").length ) {
92                                 this._$view = this._$clip.children(".ui-scrollview-view");
93                         } else {
94                                 this._$view = this._$clip.wrapInner("<div></div>").children()
95                                                         .addClass("ui-scrollview-view");
96                         }
97
98                         if ( this.options.scrollMethod === "translate" ) {
99                                 if ( this._$view.css("transform") === undefined ) {
100                                         this.options.scrollMethod = "position";
101                                 }
102                         }
103
104                         this._$clip.css( "overflow", "hidden" );
105                         this._makePositioned( this._$clip );
106
107                         this._makePositioned( this._$view );
108                         this._$view.css( { left: 0, top: 0 } );
109
110                         this._view_height = this._getViewHeight();
111
112                         this._sx = 0;
113                         this._sy = 0;
114
115                         direction = this.options.direction;
116
117                         this._hTracker = ( direction !== "y" ) ?
118                                         new MomentumTracker( this.options ) : null;
119                         this._vTracker = ( direction !== "x" ) ?
120                                         new MomentumTracker( this.options ) : null;
121
122                         this._timerInterval = this.options.timerInterval;
123                         this._timerID = 0;
124
125                         this._timerCB = function () {
126                                 self._handleMomentumScroll();
127                         };
128
129                         this._add_event();
130                         this._add_scrollbar();
131                         this._add_scroll_jump();
132                         this._add_overflow_indicator();
133                 },
134
135                 _startMScroll: function ( speedX, speedY ) {
136                         var keepGoing = false,
137                                 duration = this.options.scrollDuration,
138                                 ht = this._hTracker,
139                                 vt = this._vTracker,
140                                 c,
141                                 v;
142
143                         this._$clip.trigger( this.options.startEventName );
144
145                         if ( ht ) {
146                                 c = this._$clip.width();
147                                 v = this._$view.width();
148
149                                 if ( (( this._sx === 0 && speedX > 0 ) ||
150                                         ( this._sx === -(v - c) && speedX < 0 )) &&
151                                                 v > c ) {
152                                         return;
153                                 }
154
155                                 ht.start( this._sx, speedX,
156                                         duration, (v > c) ? -(v - c) : 0, 0 );
157                                 keepGoing = !ht.done();
158                         }
159
160                         if ( vt ) {
161                                 c = this._$clip.height();
162                                 v = this._getViewHeight();
163
164                                 if ( (( this._sy === 0 && speedY > 0 ) ||
165                                         ( this._sy === -(v - c) && speedY < 0 )) &&
166                                                 v > c ) {
167                                         return;
168                                 }
169
170                                 vt.start( this._sy, speedY,
171                                         duration, (v > c) ? -(v - c) : 0, 0 );
172                                 keepGoing = keepGoing || !vt.done();
173                         }
174
175                         if ( keepGoing ) {
176                                 this._timerID = setTimeout( this._timerCB, this._timerInterval );
177                         } else {
178                                 this._stopMScroll();
179                         }
180                 },
181
182                 _stopMScroll: function () {
183                         if ( this._timerID ) {
184                                 this._$clip.trigger( this.options.stopEventName );
185                                 clearTimeout( this._timerID );
186                         }
187                         this._timerID = 0;
188
189                         if ( this._vTracker ) {
190                                 this._vTracker.reset();
191                         }
192
193                         if ( this._hTracker ) {
194                                 this._hTracker.reset();
195                         }
196
197                         this._hideScrollBars();
198                         this._hideOverflowIndicator();
199                 },
200
201                 _handleMomentumScroll: function () {
202                         var keepGoing = false,
203                                 x = 0,
204                                 y = 0,
205                                 scroll_height = 0,
206                                 self = this,
207                                 end_effect = function ( dir ) {
208                                         setTimeout( function () {
209                                                 self._effect_dir = dir;
210                                                 self._setEndEffect( "in" );
211                                         }, 100 );
212
213                                         setTimeout( function () {
214                                                 self._setEndEffect( "out" );
215                                         }, 350 );
216                                 },
217                                 vt = this._vTracker,
218                                 ht = this._hTracker;
219
220                         if ( this._outerScrolling ) {
221                                 return;
222                         }
223
224                         if ( vt ) {
225                                 vt.update( this.options.overshootEnable );
226                                 y = vt.getPosition();
227                                 keepGoing = !vt.done();
228
229                                 if ( vt.getRemained() > this.options.overshootDuration ) {
230                                         scroll_height = this._getViewHeight() - this._$clip.height();
231
232                                         if ( !vt.isAvail() ) {
233                                                 if ( this._speedY > 0 ) {
234                                                         this._outerScroll( vt.getRemained() / 3, scroll_height );
235                                                 } else {
236                                                         this._outerScroll( y - vt.getRemained() / 3, scroll_height );
237                                                 }
238                                         } else if ( vt.isMin() ) {
239                                                 this._outerScroll( y - vt.getRemained() / 3, scroll_height );
240
241                                                 if ( scroll_height > 0 ) {
242                                                         end_effect( 1 );
243                                                 }
244                                         } else if ( vt.isMax() ) {
245                                                 this._outerScroll( vt.getRemained() / 3, scroll_height );
246
247                                                 if ( scroll_height > 0 ) {
248                                                         end_effect( 0 );
249                                                 }
250                                         }
251                                 }
252                         }
253
254                         if ( ht ) {
255                                 ht.update( this.options.overshootEnable );
256                                 x = ht.getPosition();
257                                 keepGoing = keepGoing || !ht.done();
258                         }
259
260                         this._setScrollPosition( x, y );
261                         this._$clip.trigger( this.options.updateEventName,
262                                         [ { x: x, y: y } ] );
263
264                         if ( keepGoing ) {
265                                 this._timerID = setTimeout( this._timerCB, this._timerInterval );
266                         } else {
267                                 this._stopMScroll();
268                         }
269                 },
270
271                 _setElementTransform: function ( $ele, x, y, duration ) {
272                         var translate,
273                                 transition;
274
275                         if ( !duration || duration === undefined ) {
276                                 transition = "none";
277                         } else {
278                                 transition =  "-webkit-transform " + duration / 1000 + "s ease-out";
279                         }
280
281                         if ( $.support.cssTransform3d ) {
282                                 translate = "translate3d(" + x + "," + y + ", 0px)";
283                         } else {
284                                 translate = "translate(" + x + "," + y + ")";
285                         }
286
287                         $ele.css({
288                                 "-moz-transform": translate,
289                                 "-webkit-transform": translate,
290                                 "-ms-transform": translate,
291                                 "-o-transform": translate,
292                                 "transform": translate,
293                                 "-webkit-transition": transition
294                         });
295                 },
296
297                 _setEndEffect: function ( dir ) {
298                         var scroll_height = this._getViewHeight() - this._$clip.height();
299
300                         if ( this._softkeyboard ) {
301                                 if ( this._effect_dir ) {
302                                         this._outerScroll( -scroll_height - this._softkeyboardHeight,
303                                                         scroll_height );
304                                 } else {
305                                         this._outerScroll( this._softkeyboardHeight, scroll_height );
306                                 }
307                                 return;
308                         }
309
310                         if ( dir === "in" ) {
311                                 if ( this._endEffect ) {
312                                         return;
313                                 }
314
315                                 this._endEffect = true;
316                                 this._setOverflowIndicator( this._effect_dir );
317                                 this._showOverflowIndicator();
318                         } else if ( dir === "out" ) {
319                                 if ( !this._endEffect ) {
320                                         return;
321                                 }
322
323                                 this._endEffect = false;
324                         } else {
325                                 this._endEffect = false;
326                                 this._setOverflowIndicator();
327                                 this._showOverflowIndicator();
328                         }
329                 },
330
331                 _setCalibration: function ( x, y ) {
332                         if ( this.options.overshootEnable ) {
333                                 this._sx = x;
334                                 this._sy = y;
335                                 return;
336                         }
337
338                         var $v = this._$view,
339                                 $c = this._$clip,
340                                 dirLock = this._directionLock,
341                                 scroll_height = 0,
342                                 scroll_width = 0;
343
344                         if ( dirLock !== "y" && this._hTracker ) {
345                                 scroll_width = $v.width() - $c.width();
346
347                                 if ( x >= 0 ) {
348                                         this._sx = 0;
349                                 } else if ( x < -scroll_width ) {
350                                         this._sx = -scroll_width;
351                                 } else {
352                                         this._sx = x;
353                                 }
354
355                                 if ( scroll_width < 0 ) {
356                                         this._sx = 0;
357                                 }
358                         }
359
360                         if ( dirLock !== "x" && this._vTracker ) {
361                                 scroll_height = this._getViewHeight() - $c.height();
362
363                                 if ( y > 0 ) {
364                                         this._sy = 0;
365
366                                         if ( this._didDrag && scroll_height > 0 ) {
367                                                 this._effect_dir = 0;
368                                                 this._setEndEffect( "in" );
369                                         }
370                                 } else if ( y < -scroll_height ) {
371                                         this._sy = -scroll_height;
372
373                                         if ( this._didDrag && scroll_height > 0 ) {
374                                                 this._effect_dir = 1;
375                                                 this._setEndEffect( "in" );
376                                         }
377                                 } else {
378                                         if ( this._endEffect && this._sy !== y ) {
379                                                 this._setEndEffect();
380                                         }
381
382                                         this._sy = y;
383                                 }
384
385                                 if ( scroll_height < 0 ) {
386                                         this._sy = 0;
387                                 }
388                         }
389                 },
390
391                 _setScrollPosition: function ( x, y, duration ) {
392                         var $v = this._$view,
393                                 sm = this.options.scrollMethod,
394                                 $vsb = this._$vScrollBar,
395                                 $hsb = this._$hScrollBar,
396                                 $sbt;
397
398                         this._setCalibration( x, y );
399
400                         x = this._sx;
401                         y = this._sy;
402
403                         if ( sm === "translate" ) {
404                                 this._setElementTransform( $v, x + "px", y + "px", duration );
405                         } else {
406                                 $v.css( {left: x + "px", top: y + "px"} );
407                         }
408
409                         if ( $vsb ) {
410                                 $sbt = $vsb.find(".ui-scrollbar-thumb");
411
412                                 if ( sm === "translate" ) {
413                                         this._setElementTransform( $sbt, "0px",
414                                                 -y / this._getViewHeight() * $sbt.parent().height() + "px",
415                                                 duration );
416                                 } else {
417                                         $sbt.css( "top", -y / this._getViewHeight() * 100 + "%" );
418                                 }
419                         }
420
421                         if ( $hsb ) {
422                                 $sbt = $hsb.find(".ui-scrollbar-thumb");
423
424                                 if ( sm === "translate" ) {
425                                         this._setElementTransform( $sbt,
426                                                 -x / $v.width() * $sbt.parent().width() + "px", "0px",
427                                                 duration);
428                                 } else {
429                                         $sbt.css("left", -x / $v.width() * 100 + "%");
430                                 }
431                         }
432                 },
433
434                 _outerScroll: function ( y, scroll_height ) {
435                         var self = this,
436                                 top = $( window ).scrollTop() - window.screenTop,
437                                 sy = 0,
438                                 duration = this.options.snapbackDuration,
439                                 start = getCurrentTime(),
440                                 tfunc;
441
442                         if ( !this.options.outerScrollEnable ) {
443                                 return;
444                         }
445
446                         if ( this._$clip.jqmData("scroll") !== "y" ) {
447                                 return;
448                         }
449
450                         if ( this._outerScrolling ) {
451                                 return;
452                         }
453
454                         if ( y > 0 ) {
455                                 sy = ( window.screenTop ? window.screenTop : -y );
456                         } else if ( y < -scroll_height ) {
457                                 sy = -y - scroll_height;
458                         } else {
459                                 return;
460                         }
461
462                         tfunc = function () {
463                                 var elapsed = getCurrentTime() - start;
464
465                                 if ( elapsed >= duration ) {
466                                         window.scrollTo( 0, top + sy );
467                                         self._outerScrolling = undefined;
468
469                                         self._stopMScroll();
470                                 } else {
471                                         ec = $.easing.easeOutQuad( elapsed / duration,
472                                                         elapsed, 0, 1, duration );
473
474                                         window.scrollTo( 0, top + ( sy * ec ) );
475                                         self._outerScrolling = setTimeout( tfunc, self._timerInterval );
476                                 }
477                         };
478                         this._outerScrolling = setTimeout( tfunc, self._timerInterval );
479                 },
480
481                 _scrollTo: function ( x, y, duration ) {
482                         var self = this,
483                                 start = getCurrentTime(),
484                                 efunc = $.easing.easeOutQuad,
485                                 sx = this._sx,
486                                 sy = this._sy,
487                                 dx = x - sx,
488                                 dy = y - sy,
489                                 tfunc;
490
491                         x = -x;
492                         y = -y;
493
494                         tfunc = function () {
495                                 var elapsed = getCurrentTime() - start,
496                                     ec;
497
498                                 if ( elapsed >= duration ) {
499                                         self._timerID = 0;
500                                         self._setScrollPosition( x, y );
501                                 } else {
502                                         ec = efunc( elapsed / duration, elapsed, 0, 1, duration );
503
504                                         self._setScrollPosition( sx + ( dx * ec ), sy + ( dy * ec ) );
505                                         self._timerID = setTimeout( tfunc, self._timerInterval );
506                                 }
507                         };
508
509                         this._timerID = setTimeout( tfunc, this._timerInterval );
510                 },
511
512                 scrollTo: function ( x, y, duration ) {
513                         this._stopMScroll();
514                         this._didDrag = false;
515
516                         if ( !duration || this.options.scrollMethod === "translate" ) {
517                                 this._setScrollPosition( x, y, duration );
518                         } else {
519                                 this._scrollTo( x, y, duration );
520                         }
521                 },
522
523                 getScrollPosition: function () {
524                         return { x: -this._sx, y: -this._sy };
525                 },
526
527                 skipDragging: function ( value ) {
528                         this._skip_dragging = value;
529                 },
530
531                 _getScrollHierarchy: function () {
532                         var svh = [],
533                                 d;
534
535                         this._$clip.parents( ".ui-scrollview-clip").each( function () {
536                                 d = $( this ).jqmData("scrollview");
537                                 if ( d ) {
538                                         svh.unshift( d );
539                                 }
540                         } );
541                         return svh;
542                 },
543
544                 _getAncestorByDirection: function ( dir ) {
545                         var svh = this._getScrollHierarchy(),
546                                 n = svh.length,
547                                 sv,
548                                 svdir;
549
550                         while ( 0 < n-- ) {
551                                 sv = svh[n];
552                                 svdir = sv.options.direction;
553
554                                 if (!svdir || svdir === dir) {
555                                         return sv;
556                                 }
557                         }
558                         return null;
559                 },
560
561                 _handleDragStart: function ( e, ex, ey ) {
562                         this._stopMScroll();
563
564                         this._didDrag = false;
565                         this._skip_dragging = false;
566
567                         var target = $( e.target ),
568                                 self = this,
569                                 $c = this._$clip,
570                                 svdir = this.options.direction;
571
572                         /* should prevent the default behavior when click the button */
573                         this._is_button = target.is( '.ui-btn' ) ||
574                                         target.is( '.ui-btn-text' ) ||
575                                         target.is( '.ui-btn-inner' ) ||
576                                         target.is( '.ui-btn-inner .ui-icon' );
577
578                         /* should prevent the default behavior when click the slider */
579                         if ( target.parents('.ui-slider').length || target.is('.ui-slider') ) {
580                                 this._skip_dragging = true;
581                                 return;
582                         }
583
584                         if ( target.is('textarea') ) {
585                                 target.bind( "scroll", function () {
586                                         self._skip_dragging = true;
587                                         target.unbind("scroll");
588                                 });
589                         }
590
591                         /*
592                          * We need to prevent the default behavior to
593                          * suppress accidental selection of text, etc.
594                          */
595                         this._is_inputbox = target.is(':input') ||
596                                         target.parents(':input').length > 0;
597
598                         if ( this._is_inputbox ) {
599                                 target.one( "resize.scrollview", function () {
600                                         if ( ey > $c.height() ) {
601                                                 self.scrollTo( -ex, self._sy - ey + $c.height(),
602                                                         self.options.snapbackDuration );
603                                         }
604                                 });
605                         }
606
607                         if ( this.options.eventType === "mouse" && !this._is_inputbox && !this._is_button ) {
608                                 e.preventDefault();
609                         }
610
611                         this._lastX = ex;
612                         this._lastY = ey;
613                         this._startY = ey;
614                         this._doSnapBackX = false;
615                         this._doSnapBackY = false;
616                         this._speedX = 0;
617                         this._speedY = 0;
618                         this._directionLock = "";
619
620                         this._lastMove = 0;
621                         this._enableTracking();
622
623                         this._set_scrollbar_size();
624                 },
625
626                 _propagateDragMove: function ( sv, e, ex, ey, dir ) {
627                         this._hideScrollBars();
628                         this._hideOverflowIndicator();
629                         this._disableTracking();
630                         sv._handleDragStart( e, ex, ey );
631                         sv._directionLock = dir;
632                         sv._didDrag = this._didDrag;
633                 },
634
635                 _handleDragMove: function ( e, ex, ey ) {
636                         if ( this._skip_dragging ) {
637                                 return;
638                         }
639
640                         if ( !this._dragging ) {
641                                 return;
642                         }
643
644                         if ( !this._is_inputbox && !this._is_button ) {
645                                 e.preventDefault();
646                         }
647
648                         var mt = this.options.moveThreshold,
649                                 dx = ex - this._lastX,
650                                 dy = ey - this._lastY,
651                                 svdir = this.options.direction,
652                                 dir = null,
653                                 x,
654                                 y,
655                                 sv,
656                                 scope,
657                                 newX,
658                                 newY,
659                                 dirLock;
660
661                         this._lastMove = getCurrentTime();
662
663                         if ( !this._directionLock ) {
664                                 x = Math.abs( dx );
665                                 y = Math.abs( dy );
666
667                                 if ( x < mt && y < mt ) {
668                                         return false;
669                                 }
670
671                                 if ( x < y && (x / y) < 0.5 ) {
672                                         dir = "y";
673                                 } else if ( x > y && (y / x) < 0.5 ) {
674                                         dir = "x";
675                                 }
676
677                                 if ( svdir && dir && svdir !== dir ) {
678                                         /*
679                                          * This scrollview can't handle the direction the user
680                                          * is attempting to scroll. Find an ancestor scrollview
681                                          * that can handle the request.
682                                          */
683
684                                         sv = this._getAncestorByDirection( dir );
685                                         if ( sv ) {
686                                                 this._propagateDragMove( sv, e, ex, ey, dir );
687                                                 return false;
688                                         }
689                                 }
690
691                                 this._directionLock = svdir || (dir || "none");
692                         }
693
694                         newX = this._sx;
695                         newY = this._sy;
696                         dirLock = this._directionLock;
697
698                         if ( dirLock !== "y" && this._hTracker ) {
699                                 x = this._sx;
700                                 this._speedX = dx;
701                                 newX = x + dx;
702
703                                 this._doSnapBackX = false;
704
705                                 scope = ( newX > 0 || newX < this._maxX );
706
707                                 if ( scope && dirLock === "x" ) {
708                                         sv = this._getAncestorByDirection("x");
709                                         if ( sv ) {
710                                                 this._setScrollPosition( newX > 0 ?
711                                                                 0 : this._maxX, newY );
712                                                 this._propagateDragMove( sv, e, ex, ey, dir );
713                                                 return false;
714                                         }
715
716                                         newX = x + ( dx / 2 );
717                                         this._doSnapBackX = true;
718                                 }
719                         }
720
721                         if ( dirLock !== "x" && this._vTracker ) {
722                                 if ( Math.abs( this._startY - ey ) < mt && dirLock !== "xy" ) {
723                                         return;
724                                 }
725
726                                 y = this._sy;
727                                 this._speedY = dy;
728                                 newY = y + dy;
729
730                                 this._doSnapBackY = false;
731
732                                 scope = ( newY > 0 || newY < this._maxY );
733
734                                 if ( scope && dirLock === "y" ) {
735                                         sv = this._getAncestorByDirection("y");
736                                         if ( sv ) {
737                                                 this._setScrollPosition( newX,
738                                                                 newY > 0 ? 0 : this._maxY );
739                                                 this._propagateDragMove( sv, e, ex, ey, dir );
740                                                 return false;
741                                         }
742
743                                         newY = y + ( dy / 2 );
744                                         this._doSnapBackY = true;
745                                 }
746                         }
747
748                         if ( this.options.overshootEnable === false ) {
749                                 this._doSnapBackX = false;
750                                 this._doSnapBackY = false;
751                         }
752
753                         this._lastX = ex;
754                         this._lastY = ey;
755
756                         this._setScrollPosition( newX, newY );
757
758                         if ( this._didDrag === false ) {
759                                 this._didDrag = true;
760                                 this._showScrollBars();
761                                 this._showOverflowIndicator();
762
763                                 this._$clip.parents(".ui-scrollview-clip").each( function () {
764                                         $( this ).scrollview( "skipDragging", true );
765                                 } );
766                         }
767                 },
768
769                 _handleDragStop: function ( e ) {
770                         var self = this;
771
772                         if ( this._skip_dragging ) {
773                                 return;
774                         }
775
776                         var l = this._lastMove,
777                                 t = getCurrentTime(),
778                                 doScroll = (l && (t - l) <= this.options.moveIntervalThreshold),
779                                 sx = ( this._hTracker && this._speedX && doScroll ) ?
780                                                 this._speedX : ( this._doSnapBackX ? 1 : 0 ),
781                                 sy = ( this._vTracker && this._speedY && doScroll ) ?
782                                                 this._speedY : ( this._doSnapBackY ? 1 : 0 ),
783                                 svdir = this.options.direction,
784                                 x,
785                                 y;
786
787                         if ( sx || sy ) {
788                                 if ( !this._setGestureScroll( sx, sy ) ) {
789                                         this._startMScroll( sx, sy );
790                                 }
791                         } else {
792                                 this._hideScrollBars();
793                                 this._hideOverflowIndicator();
794                         }
795
796                         this._disableTracking();
797
798                         if ( this._endEffect ) {
799                                 setTimeout( function () {
800                                         self._setEndEffect( "out" );
801                                         self._hideScrollBars();
802                                         self._hideOverflowIndicator();
803                                 }, 300 );
804                         }
805
806                         return !this._didDrag;
807                 },
808
809                 _setGestureScroll: function ( sx, sy ) {
810                         var self = this,
811                                 reset = function () {
812                                         clearTimeout( self._gesture_timer );
813                                         self._gesture_dir = 0;
814                                         self._gesture_count = 0;
815                                         self._gesture_timer = undefined;
816                                 };
817
818                         if ( !sy ) {
819                                 return false;
820                         }
821
822                         dir = sy > 0 ? 1 : -1;
823
824                         if ( !this._gesture_timer ) {
825                                 this._gesture_count = 1;
826                                 this._gesture_dir = dir;
827
828                                 this._gesture_timer = setTimeout( function () {
829                                         reset();
830                                 }, 1000 );
831
832                                 return false;
833                         }
834
835                         if ( this._gesture_dir !== dir ) {
836                                 reset();
837                                 return false;
838                         }
839
840                         this._gesture_count++;
841
842                         if ( this._gesture_count === 3 ) {
843                                 if ( dir > 0 ) {
844                                         this.scrollTo( 0, 0, this.options.overshootDuration );
845                                 } else {
846                                         this.scrollTo( 0, -( this._getViewHeight() - this._$clip.height() ),
847                                                         this.options.overshootDuration );
848                                 }
849                                 reset();
850
851                                 return true;
852                         }
853
854                         return false;
855                 },
856
857                 _enableTracking: function () {
858                         this._dragging = true;
859                 },
860
861                 _disableTracking: function () {
862                         this._dragging = false;
863                 },
864
865                 _showScrollBars: function ( interval ) {
866                         var vclass = "ui-scrollbar-visible",
867                                 self = this;
868
869                         if ( !this.options.showScrollBars ) {
870                                 return;
871                         }
872                         if ( this._scrollbar_showed ) {
873                                 return;
874                         }
875
876                         if ( this._$vScrollBar ) {
877                                 this._$vScrollBar.addClass( vclass );
878                         }
879                         if ( this._$hScrollBar ) {
880                                 this._$hScrollBar.addClass( vclass );
881                         }
882
883                         this._scrollbar_showed = true;
884
885                         if ( interval ) {
886                                 setTimeout( function () {
887                                         self._hideScrollBars();
888                                 }, interval );
889                         }
890                 },
891
892                 _hideScrollBars: function () {
893                         var vclass = "ui-scrollbar-visible";
894
895                         if ( !this.options.showScrollBars ) {
896                                 return;
897                         }
898                         if ( !this._scrollbar_showed ) {
899                                 return;
900                         }
901
902                         if ( this._$vScrollBar ) {
903                                 this._$vScrollBar.removeClass( vclass );
904                         }
905                         if ( this._$hScrollBar ) {
906                                 this._$hScrollBar.removeClass( vclass );
907                         }
908
909                         this._scrollbar_showed = false;
910                 },
911
912                 _setOverflowIndicator: function ( dir ) {
913                         if ( dir === 1 ) {
914                                 this._opacity_top = "0";
915                                 this._opacity_bottom = "0.8";
916                         } else if ( dir === 0 ) {
917                                 this._opacity_top = "0.8";
918                                 this._opacity_bottom = "0";
919                         } else {
920                                 this._opacity_top = "0.5";
921                                 this._opacity_bottom = "0.5";
922                         }
923                 },
924
925                 _showOverflowIndicator: function () {
926                         if ( !this.options.overflowEnable || !this._overflowAvail || this._softkeyboard ) {
927                                 return;
928                         }
929
930                         this._overflow_top.animate( { opacity: this._opacity_top }, 300 );
931                         this._overflow_bottom.animate( { opacity: this._opacity_bottom }, 300 );
932
933                         this._overflow_showed = true;
934                 },
935
936                 _hideOverflowIndicator: function () {
937                         if ( !this.options.overflowEnable || !this._overflowAvail || this._softkeyboard ) {
938                                 return;
939                         }
940
941                         if ( this._overflow_showed === false ) {
942                                 return;
943                         }
944
945                         this._overflow_top.animate( { opacity: 0 }, 300 );
946                         this._overflow_bottom.animate( { opacity: 0 }, 300 );
947
948                         this._overflow_showed = false;
949                         this._setOverflowIndicator();
950                 },
951
952                 _add_event: function () {
953                         var self = this,
954                                 $c = this._$clip,
955                                 $v = this._$view;
956
957                         if ( this.options.eventType === "mouse" ) {
958                                 this._dragEvt = "mousedown mousemove mouseup click mousewheel";
959
960                                 this._dragCB = function ( e ) {
961                                         switch ( e.type ) {
962                                         case "mousedown":
963                                                 return self._handleDragStart( e,
964                                                                 e.clientX, e.clientY );
965
966                                         case "mousemove":
967                                                 return self._handleDragMove( e,
968                                                                 e.clientX, e.clientY );
969
970                                         case "mouseup":
971                                                 return self._handleDragStop( e );
972
973                                         case "click":
974                                                 return !self._didDrag;
975
976                                         case "mousewheel":
977                                                 var old = self.getScrollPosition();
978                                                 self.scrollTo( -old.x,
979                                                         -(old.y - e.originalEvent.wheelDelta) );
980                                                 break;
981                                         }
982                                 };
983                         } else {
984                                 this._dragEvt = "touchstart touchmove touchend click";
985
986                                 this._dragCB = function ( e ) {
987                                         var touches = e.originalEvent.touches;
988
989                                         switch ( e.type ) {
990                                         case "touchstart":
991                                                 if ( touches.length != 1) {
992                                                         return;
993                                                 }
994
995                                                 return self._handleDragStart( e,
996                                                                 touches[0].pageX, touches[0].pageY );
997
998                                         case "touchmove":
999                                                 if ( touches.length != 1) {
1000                                                         return;
1001                                                 }
1002
1003                                                 return self._handleDragMove( e,
1004                                                                 touches[0].pageX, touches[0].pageY );
1005
1006                                         case "touchend":
1007                                                 if ( touches.length != 0) {
1008                                                         return;
1009                                                 }
1010
1011                                                 return self._handleDragStop( e );
1012
1013                                         case "click":
1014                                                 return !self._didDrag;
1015                                         }
1016                                 };
1017                         }
1018
1019                         $v.bind( this._dragEvt, this._dragCB );
1020
1021                         $v.bind( "keydown", function ( e ) {
1022                                 var elem,
1023                                         elem_top,
1024                                         scroll_top = $( window ).scrollTop() - window.screenTop,
1025                                         screen_h;
1026
1027                                 if ( e.keyCode == 9 ) {
1028                                         return false;
1029                                 }
1030
1031                                 elem = $c.find(".ui-focus");
1032
1033                                 if ( elem === undefined ) {
1034                                         return;
1035                                 }
1036
1037                                 elem_top = elem.offset().top - scroll_top;
1038                                 screen_h = $c.offset().top + $c.height() - elem.height();
1039
1040                                 if ( self._softkeyboard ) {
1041                                         screen_h -= self._softkeyboardHeight;
1042                                 }
1043
1044                                 if ( ( elem_top < $c.offset().top ) || ( elem_top > screen_h ) ) {
1045                                         self.scrollTo( 0, self._sy -
1046                                                         ( elem_top - $c.offset().top - elem.height() ) );
1047                                 }
1048
1049                                 return;
1050                         });
1051
1052                         $v.bind( "keyup", function ( e ) {
1053                                 var input,
1054                                         elem,
1055                                         elem_top,
1056                                         scroll_top = $( window ).scrollTop() - window.screenTop,
1057                                         screen_h;
1058
1059                                 if ( e.keyCode != 9 ) {
1060                                         return;
1061                                 }
1062
1063                                 /* Tab Key */
1064
1065                                 input = $( this ).find(":input");
1066
1067                                 for ( i = 0; i < input.length; i++ ) {
1068                                         if ( !$( input[i] ).hasClass("ui-focus") ) {
1069                                                 continue;
1070                                         }
1071
1072                                         if ( i + 1 == input.length ) {
1073                                                 elem = $( input[0] );
1074                                         } else {
1075                                                 elem = $( input[i + 1] );
1076                                         }
1077
1078                                         elem_top = elem.offset().top - scroll_top;
1079                                         screen_h = $c.offset().top + $c.height() - elem.height();
1080
1081                                         if ( self._softkeyboard ) {
1082                                                 screen_h -= self._softkeyboardHeight;
1083                                         }
1084
1085                                         if ( ( elem_top < 0 ) || ( elem_top > screen_h ) ) {
1086                                                 self.scrollTo( 0, self._sy - elem_top +
1087                                                         elem.height() + $c.offset().top, 0);
1088                                         }
1089
1090                                         elem.focus();
1091
1092                                         break;
1093                                 }
1094
1095                                 return false;
1096                         });
1097
1098                         $c.bind( "updatelayout", function ( e ) {
1099                                 var sy,
1100                                         vh,
1101                                         view_h = self._getViewHeight();
1102
1103                                 if ( !$c.height() || !view_h ) {
1104                                         self.scrollTo( 0, 0, 0 );
1105                                         return;
1106                                 }
1107
1108                                 sy = $c.height() - view_h;
1109                                 vh = view_h - self._view_height;
1110
1111                                 self._view_height = view_h;
1112
1113                                 if ( vh == 0 || vh > $c.height() / 2 ) {
1114                                         return;
1115                                 }
1116
1117                                 if ( sy > 0 ) {
1118                                         self.scrollTo( 0, 0, 0 );
1119                                 } else if ( self._sy - sy <= -vh ) {
1120                                         self.scrollTo( 0, self._sy,
1121                                                 self.options.snapbackDuration );
1122                                 } else if ( self._sy - sy <= vh + self.options.moveThreshold ) {
1123                                         self.scrollTo( 0, sy,
1124                                                 self.options.snapbackDuration );
1125                                 }
1126                         });
1127
1128                         $( window ).bind( "resize", function ( e ) {
1129                                 var focused,
1130                                         view_h = self._getViewHeight();
1131
1132                                 if ( $(".ui-page-active").get(0) !== $c.closest(".ui-page").get(0) ) {
1133                                         return;
1134                                 }
1135
1136                                 if ( !$c.height() || !view_h ) {
1137                                         return;
1138                                 }
1139
1140                                 focused = $c.find(".ui-focus");
1141
1142                                 if ( focused ) {
1143                                         focused.trigger("resize.scrollview");
1144                                 }
1145
1146                                 /* calibration - after triggered throttledresize */
1147                                 setTimeout( function () {
1148                                         if ( self._sy < $c.height() - self._getViewHeight() ) {
1149                                                 self.scrollTo( 0, $c.height() - self._getViewHeight(),
1150                                                         self.options.overshootDuration );
1151                                         }
1152                                 }, 260 );
1153
1154                                 self._view_height = view_h;
1155                         });
1156
1157                         $( window ).bind( "vmouseout", function ( e ) {
1158                                 var drag_stop = false;
1159
1160                                 if ( $(".ui-page-active").get(0) !== $c.closest(".ui-page").get(0) ) {
1161                                         return;
1162                                 }
1163
1164                                 if ( !self._dragging ) {
1165                                         return;
1166                                 }
1167
1168                                 if ( e.pageX < 0 || e.pageX > $( window ).width() ) {
1169                                         drag_stop = true;
1170                                 }
1171
1172                                 if ( e.pageY < 0 || e.pageY > $( window ).height() ) {
1173                                         drag_stop = true;
1174                                 }
1175
1176                                 if ( drag_stop ) {
1177                                         self._hideScrollBars();
1178                                         self._hideOverflowIndicator();
1179                                         self._disableTracking();
1180                                 }
1181                         });
1182
1183                         this._softkeyboard = false;
1184                         this._softkeyboardHeight = 0;
1185
1186                         window.addEventListener( "softkeyboardchange", function ( e ) {
1187                                 if ( $(".ui-page-active").get(0) !== $c.closest(".ui-page").get(0) ) {
1188                                         return;
1189                                 }
1190
1191                                 self._softkeyboard = ( e.state === "on" ? true : false );
1192                                 self._softkeyboardHeight = parseInt( e.height ) *
1193                                                 ( $( window ).width() / window.screen.availWidth );
1194                         });
1195
1196                         $c.closest(".ui-page")
1197                                 .bind( "pageshow", function ( e ) {
1198                                         /* should be called after pagelayout */
1199                                         setTimeout( function () {
1200                                                 self._view_height = self._getViewHeight();
1201                                                 self._set_scrollbar_size();
1202                                                 self._setScrollPosition( self._sx, self._sy );
1203                                                 self._showScrollBars( 2000 );
1204                                         }, 0 );
1205                                 });
1206                 },
1207
1208                 _add_scrollbar: function () {
1209                         var $c = this._$clip,
1210                                 prefix = "<div class=\"ui-scrollbar ui-scrollbar-",
1211                                 suffix = "\"><div class=\"ui-scrollbar-track\"><div class=\"ui-scrollbar-thumb\"></div></div></div>";
1212
1213                         if ( !this.options.showScrollBars ) {
1214                                 return;
1215                         }
1216
1217                         if ( this._vTracker ) {
1218                                 $c.append( prefix + "y" + suffix );
1219                                 this._$vScrollBar = $c.children(".ui-scrollbar-y");
1220                         }
1221                         if ( this._hTracker ) {
1222                                 $c.append( prefix + "x" + suffix );
1223                                 this._$hScrollBar = $c.children(".ui-scrollbar-x");
1224                         }
1225
1226                         this._scrollbar_showed = false;
1227                 },
1228
1229                 _add_scroll_jump: function () {
1230                         var $c = this._$clip,
1231                                 self = this,
1232                                 top_btn,
1233                                 left_btn;
1234
1235                         if ( !this.options.scrollJump ) {
1236                                 return;
1237                         }
1238
1239                         if ( this._vTracker ) {
1240                                 top_btn = $( '<div class="ui-scroll-jump-top-bg">' +
1241                                                 '<div data-role="button" data-inline="true" data-icon="scrolltop" data-style="box"></div></div>' );
1242                                 $c.append( top_btn ).trigger("create");
1243
1244                                 top_btn.bind( "vclick", function () {
1245                                         self.scrollTo( 0, 0, self.options.overshootDuration );
1246                                 } );
1247                         }
1248
1249                         if ( this._hTracker ) {
1250                                 left_btn = $( '<div class="ui-scroll-jump-left-bg">' +
1251                                                 '<div data-role="button" data-inline="true" data-icon="scrollleft" data-style="box"></div></div>' );
1252                                 $c.append( left_btn ).trigger("create");
1253
1254                                 left_btn.bind( "vclick", function () {
1255                                         self.scrollTo( 0, 0, self.options.overshootDuration );
1256                                 } );
1257                         }
1258                 },
1259
1260                 _add_overflow_indicator: function () {
1261                         if ( !this.options.overflowEnable ) {
1262                                 return;
1263                         }
1264
1265                         this._overflow_top = $( '<div class="ui-overflow-indicator-top"></div>' );
1266                         this._overflow_bottom = $( '<div class="ui-overflow-indicator-bottom"></div>' );
1267
1268                         this._$clip.append( this._overflow_top );
1269                         this._$clip.append( this._overflow_bottom );
1270
1271                         this._opacity_top = "0.5";
1272                         this._opacity_bottom = "0.5";
1273                         this._overflow_showed = false;
1274                 },
1275
1276                 _set_scrollbar_size: function () {
1277                         var $c = this._$clip,
1278                                 $v = this._$view,
1279                                 cw = 0,
1280                                 vw = 0,
1281                                 ch = 0,
1282                                 vh = 0,
1283                                 thumb;
1284
1285                         if ( !this.options.showScrollBars ) {
1286                                 return;
1287                         }
1288
1289                         if ( this._hTracker ) {
1290                                 cw = $c.width();
1291                                 vw = $v.width();
1292                                 this._maxX = cw - vw;
1293
1294                                 if ( this._maxX > 0 ) {
1295                                         this._maxX = 0;
1296                                 }
1297                                 if ( this._$hScrollBar && vw ) {
1298                                         thumb = this._$hScrollBar.find(".ui-scrollbar-thumb");
1299                                         thumb.css( "width", (cw >= vw ? "0" :
1300                                                         (Math.floor(cw / vw * 100) || 1) + "%") );
1301                                 }
1302                         }
1303
1304                         if ( this._vTracker ) {
1305                                 ch = $c.height();
1306                                 vh = this._getViewHeight();
1307                                 this._maxY = ch - vh;
1308
1309                                 if ( this._maxY > 0 ) {
1310                                         this._maxY = 0;
1311                                 }
1312                                 if ( this._$vScrollBar && vh ) {
1313                                         thumb = this._$vScrollBar.find(".ui-scrollbar-thumb");
1314                                         thumb.css( "height", (ch >= vh ? "0" :
1315                                                         (Math.floor(ch / vh * 100) || 1) + "%") );
1316
1317                                         this._overflowAvail = !!thumb.height();
1318                                 }
1319                         }
1320                 }
1321         });
1322
1323         $.extend( MomentumTracker.prototype, {
1324                 start: function ( pos, speed, duration, minPos, maxPos ) {
1325                         var tstate = ( pos < minPos || pos > maxPos ) ?
1326                                         tstates.snapback : tstates.scrolling,
1327                                 pos_temp;
1328
1329                         this.state = ( speed !== 0 ) ? tstate : tstates.done;
1330                         this.pos = pos;
1331                         this.speed = speed;
1332                         this.duration = ( this.state === tstates.snapback ) ?
1333                                         this.options.snapbackDuration : duration;
1334                         this.minPos = minPos;
1335                         this.maxPos = maxPos;
1336
1337                         this.fromPos = ( this.state === tstates.snapback ) ? this.pos : 0;
1338                         pos_temp = ( this.pos < this.minPos ) ? this.minPos : this.maxPos;
1339                         this.toPos = ( this.state === tstates.snapback ) ? pos_temp : 0;
1340
1341                         this.startTime = getCurrentTime();
1342                 },
1343
1344                 reset: function () {
1345                         this.state = tstates.done;
1346                         this.pos = 0;
1347                         this.speed = 0;
1348                         this.minPos = 0;
1349                         this.maxPos = 0;
1350                         this.duration = 0;
1351                         this.remained = 0;
1352                 },
1353
1354                 update: function ( overshootEnable ) {
1355                         var state = this.state,
1356                                 cur_time = getCurrentTime(),
1357                                 duration = this.duration,
1358                                 elapsed =  cur_time - this.startTime,
1359                                 dx,
1360                                 x,
1361                                 didOverShoot;
1362
1363                         if ( state === tstates.done ) {
1364                                 return this.pos;
1365                         }
1366
1367                         elapsed = elapsed > duration ? duration : elapsed;
1368
1369                         this.remained = duration - elapsed;
1370
1371                         if ( state === tstates.scrolling || state === tstates.overshot ) {
1372                                 dx = this.speed *
1373                                         ( 1 - $.easing[this.easing]( elapsed / duration,
1374                                                                 elapsed, 0, 1, duration ) );
1375
1376                                 x = this.pos + dx;
1377
1378                                 didOverShoot = ( state === tstates.scrolling ) &&
1379                                         ( x < this.minPos || x > this.maxPos );
1380
1381                                 if ( didOverShoot ) {
1382                                         x = ( x < this.minPos ) ? this.minPos : this.maxPos;
1383                                 }
1384
1385                                 this.pos = x;
1386
1387                                 if ( state === tstates.overshot ) {
1388                                         if ( !overshootEnable ) {
1389                                                 this.state = tstates.done;
1390                                         }
1391                                         if ( elapsed >= duration ) {
1392                                                 this.state = tstates.snapback;
1393                                                 this.fromPos = this.pos;
1394                                                 this.toPos = ( x < this.minPos ) ?
1395                                                                 this.minPos : this.maxPos;
1396                                                 this.duration = this.options.snapbackDuration;
1397                                                 this.startTime = cur_time;
1398                                                 elapsed = 0;
1399                                         }
1400                                 } else if ( state === tstates.scrolling ) {
1401                                         if ( didOverShoot && overshootEnable ) {
1402                                                 this.state = tstates.overshot;
1403                                                 this.speed = dx / 2;
1404                                                 this.duration = this.options.overshootDuration;
1405                                                 this.startTime = cur_time;
1406                                         } else if ( elapsed >= duration ) {
1407                                                 this.state = tstates.done;
1408                                         }
1409                                 }
1410                         } else if ( state === tstates.snapback ) {
1411                                 if ( elapsed >= duration ) {
1412                                         this.pos = this.toPos;
1413                                         this.state = tstates.done;
1414                                 } else {
1415                                         this.pos = this.fromPos + (( this.toPos - this.fromPos ) *
1416                                                 $.easing[this.easing]( elapsed / duration,
1417                                                         elapsed, 0, 1, duration ));
1418                                 }
1419                         }
1420
1421                         return this.pos;
1422                 },
1423
1424                 done: function () {
1425                         return this.state === tstates.done;
1426                 },
1427
1428                 isMin: function () {
1429                         return this.pos === this.minPos;
1430                 },
1431
1432                 isMax: function () {
1433                         return this.pos === this.maxPos;
1434                 },
1435
1436                 isAvail: function () {
1437                         return !( this.minPos === this.maxPos );
1438                 },
1439
1440                 getRemained: function () {
1441                         return this.remained;
1442                 },
1443
1444                 getPosition: function () {
1445                         return this.pos;
1446                 }
1447         });
1448
1449         $( document ).bind( 'pagecreate create', function ( e ) {
1450                 var $page = $( e.target ),
1451                         content_scroll = $page.find(".ui-content").jqmData("scroll");
1452
1453                 /* content scroll */
1454                 if ( $.support.scrollview === undefined ) {
1455                         $.support.scrollview = true;
1456                 }
1457
1458                 if ( $.support.scrollview === true && content_scroll === undefined ) {
1459                         content_scroll = "y";
1460                 }
1461
1462                 if ( content_scroll !== "y" ) {
1463                         content_scroll = "none";
1464                 }
1465
1466                 $page.find(".ui-content").attr( "data-scroll", content_scroll );
1467
1468                 $page.find(":jqmData(scroll)").not(".ui-scrollview-clip").each( function () {
1469                         if ( $( this ).hasClass("ui-scrolllistview") ) {
1470                                 $( this ).scrolllistview();
1471                         } else {
1472                                 var st = $( this ).jqmData("scroll"),
1473                                         dir = st && ( st.search(/^[xy]/) !== -1 ) ? st : null,
1474                                         content = $(this).hasClass("ui-content"),
1475                                         opts;
1476
1477                                 if ( st === "none" ) {
1478                                         return;
1479                                 }
1480
1481                                 opts = {
1482                                         direction: dir || undefined,
1483                                         overflowEnable: content,
1484                                         scrollMethod: $( this ).jqmData("scroll-method") || undefined,
1485                                         scrollJump: $( this ).jqmData("scroll-jump") || undefined
1486                                 };
1487
1488                                 $( this ).scrollview( opts );
1489                         }
1490                 });
1491         });
1492
1493         $( document ).bind( 'pageshow', function ( e ) {
1494                 var $page = $( e.target ),
1495                         scroll = $page.find(".ui-content").jqmData("scroll");
1496
1497                 if ( scroll === "y" ) {
1498                         resizePageContentHeight( e.target );
1499                 }
1500         });
1501
1502 }( jQuery, window, document ) );
1503
1504 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
1505 } );
1506 //>>excludeEnd("jqmBuildExclude");