2 * jQuery Mobile Framework : scrollview plugin
3 * Copyright (c) 2010 Adobe Systems Incorporated - Kin Blas (jblas@adobe.com)
4 * Dual licensed under the MIT (MIT-LICENSE.txt) and GPL (GPL-LICENSE.txt) licenses.
5 * Note: Code is in draft form and is subject to change
6 * Modified by Koeun Choi <koeun.choi@samsung.com>
7 * Modified by Minkyu Kang <mk7.kang@samsung.com>
10 (function ( $, window, document, undefined ) {
12 function resizePageContentHeight( page ) {
13 var $page = $( page ),
14 $content = $page.children(".ui-content"),
15 hh = $page.children(".ui-header").outerHeight() || 0,
16 fh = $page.children(".ui-footer").outerHeight() || 0,
17 pt = parseFloat( $content.css("padding-top") ),
18 pb = parseFloat( $content.css("padding-bottom") ),
19 wh = $( window ).height();
21 $content.height( wh - (hh + fh) - (pt + pb) );
24 function MomentumTracker( options ) {
25 this.options = $.extend( {}, options );
26 this.easing = "easeOutQuad";
37 function getCurrentTime() {
38 return ( new Date() ).getTime();
41 jQuery.widget( "tizen.scrollview", jQuery.mobile.widget, {
43 fps: 60, // Frames per second in msecs.
44 direction: null, // "x", "y", or null for both.
46 scrollDuration: 2000, // Duration of the scrolling animation in msecs.
47 overshootDuration: 250, // Duration of the overshoot animation in msecs.
48 snapbackDuration: 500, // Duration of the snapback animation in msecs.
50 moveThreshold: 30, // User must move this many pixels in any direction to trigger a scroll.
51 moveIntervalThreshold: 150, // Time between mousemoves must not exceed this threshold.
53 scrollMethod: "translate", // "translate", "position"
54 startEventName: "scrollstart",
55 updateEventName: "scrollupdate",
56 stopEventName: "scrollstop",
58 eventType: $.support.touch ? "touch" : "mouse",
61 overshootEnable: false,
65 _makePositioned: function ( $ele ) {
66 if ( $ele.css("position") === "static" ) {
67 $ele.css( "position", "relative" );
71 _create: function () {
72 var $page = $('.ui-page'),
76 this._$clip = $( this.element ).addClass("ui-scrollview-clip");
77 this._$view = this._$clip.wrapInner("<div></div>").children()
78 .addClass("ui-scrollview-view");
80 if ( this.options.scrollMethod === "translate" ) {
81 if ( this._$view.css("transform") === undefined ) {
82 this.options.scrollMethod = "position";
86 this._$clip.css( "overflow", "hidden" );
87 this._makePositioned( this._$clip );
89 this._makePositioned( this._$view );
90 this._$view.css( { left: 0, top: 0 } );
91 this._view_height = this._$view.height();
96 direction = this.options.direction;
98 this._hTracker = ( direction !== "y" ) ?
99 new MomentumTracker( this.options ) : null;
100 this._vTracker = ( direction !== "x" ) ?
101 new MomentumTracker( this.options ) : null;
103 this._timerInterval = 1000 / this.options.fps;
106 this._timerCB = function () {
107 self._handleMomentumScroll();
111 this._add_scrollbar();
112 this._add_scroll_jump();
115 _startMScroll: function ( speedX, speedY ) {
116 var keepGoing = false,
117 duration = this.options.scrollDuration,
124 this._showScrollBars();
126 this._$clip.trigger( this.options.startEventName );
129 c = this._$clip.width();
130 v = this._$view.width();
132 ht.start( this._sx, speedX,
133 duration, (v > c) ? -(v - c) : 0, 0 );
134 keepGoing = !ht.done();
138 c = this._$clip.height();
139 v = this._$view.height() +
140 parseFloat( this._$view.css("padding-top") );
142 vt.start( this._sy, speedY,
143 duration, (v > c) ? -(v - c) : 0, 0 );
144 keepGoing = keepGoing || !vt.done();
148 this._timerID = setTimeout( this._timerCB, this._timerInterval );
154 _stopMScroll: function () {
155 if ( this._timerID ) {
156 this._$clip.trigger( this.options.stopEventName );
157 clearTimeout( this._timerID );
161 if ( this._vTracker ) {
162 this._vTracker.reset();
165 if ( this._hTracker ) {
166 this._hTracker.reset();
169 this._hideScrollBars();
172 _handleMomentumScroll: function () {
173 var keepGoing = false,
179 if ( this._outerScrolling ) {
184 vt.update( this.options.overshootEnable );
185 y = vt.getPosition();
186 keepGoing = !vt.done();
190 ht.update( this.options.overshootEnable );
191 x = ht.getPosition();
192 keepGoing = keepGoing || !ht.done();
195 this._setScrollPosition( x, y );
196 this._$clip.trigger( this.options.updateEventName,
197 [ { x: x, y: y } ] );
200 this._timerID = setTimeout( this._timerCB, this._timerInterval );
206 _setElementTransform: function ( $ele, x, y, duration ) {
210 if ( !duration || duration === undefined ) {
213 transition = "-webkit-transform " + duration / 1000 + "s";
216 if ( $.support.cssTransform3d ) {
217 translate = "translate3d(" + x + "," + y + ", 0px)";
219 translate = "translate(" + x + "," + y + ")";
223 "-moz-transform": translate,
224 "-webkit-transform": translate,
225 "-ms-transform": translate,
226 "-o-transform": translate,
227 "transform": translate,
228 "-webkit-transition": transition
232 _setCalibration: function ( x, y ) {
233 if ( this.options.overshootEnable ) {
239 var $v = this._$view,
241 dirLock = this._directionLock,
245 if ( dirLock !== "y" && this._hTracker ) {
246 scroll_width = $v.width() - $c.width();
250 } else if ( x < -scroll_width ) {
251 this._sx = -scroll_width;
256 if ( scroll_width < 0 ) {
261 if ( dirLock !== "x" && this._vTracker ) {
262 scroll_height = $v.height() - $c.height() +
263 parseFloat( $c.css("padding-top") ) +
264 parseFloat( $c.css("padding-bottom") );
266 this._outerScroll( y, scroll_height );
270 } else if ( y < -scroll_height ) {
271 this._sy = -scroll_height;
276 if ( scroll_height < 0 ) {
282 _setScrollPosition: function ( x, y, duration ) {
283 var $v = this._$view,
284 sm = this.options.scrollMethod,
285 $vsb = this._$vScrollBar,
286 $hsb = this._$hScrollBar,
289 this._setCalibration( x, y );
291 if ( this._outerScrolling ) {
298 if ( sm === "translate" ) {
299 this._setElementTransform( $v, x + "px", y + "px", duration );
301 $v.css( {left: x + "px", top: y + "px"} );
305 $sbt = $vsb.find(".ui-scrollbar-thumb");
307 if ( sm === "translate" ) {
308 this._setElementTransform( $sbt, "0px",
309 -y / $v.height() * $sbt.parent().height() + "px",
312 $sbt.css( "top", -y / $v.height() * 100 + "%" );
317 $sbt = $hsb.find(".ui-scrollbar-thumb");
319 if ( sm === "translate" ) {
320 this._setElementTransform( $sbt,
321 -x / $v.width() * $sbt.parent().width() + "px", "0px",
324 $sbt.css("left", -x / $v.width() * 100 + "%");
329 _outerScroll: function ( y, scroll_height ) {
331 top = $( window ).scrollTop(),
333 duration = this.options.snapbackDuration,
334 start = getCurrentTime(),
337 if ( this._$clip.jqmData("scroll") !== "y" ) {
341 if ( this._outerScrolling ) {
345 if ( !this._dragging ) {
349 if ( scroll_height < 0 ) {
355 } else if ( y < -scroll_height ) {
356 sy = -y - scroll_height;
363 tfunc = function () {
364 var elapsed = getCurrentTime() - start;
366 if ( elapsed >= duration ) {
367 window.scrollTo( 0, top + sy );
368 self._outerScrolling = undefined;
370 ec = $.easing.easeOutQuad( elapsed / duration, elapsed, 0, 1, duration );
372 window.scrollTo( 0, top + ( sy * ec ) );
373 self._outerScrolling = setTimeout( tfunc, self._timerInterval );
376 this._outerScrolling = setTimeout( tfunc, self._timerInterval );
378 /* skip the srollview dragging */
379 this._skip_dragging = true;
382 _scrollTo: function ( x, y, duration ) {
384 start = getCurrentTime(),
385 efunc = $.easing.easeOutQuad,
395 tfunc = function () {
396 var elapsed = getCurrentTime() - start,
399 if ( elapsed >= duration ) {
401 self._setScrollPosition( x, y );
403 ec = efunc( elapsed / duration, elapsed, 0, 1, duration );
405 self._setScrollPosition( sx + ( dx * ec ), sy + ( dy * ec ) );
406 self._timerID = setTimeout( tfunc, self._timerInterval );
410 this._timerID = setTimeout( tfunc, this._timerInterval );
413 scrollTo: function ( x, y, duration ) {
416 if ( !duration || this.options.scrollMethod === "translate" ) {
417 this._setScrollPosition( x, y, duration );
419 this._scrollTo( x, y, duration );
423 getScrollPosition: function () {
424 return { x: -this._sx, y: -this._sy };
427 _getScrollHierarchy: function () {
431 this._$clip.parents( ".ui-scrollview-clip").each( function () {
432 d = $( this ).jqmData("scrollview");
440 _getAncestorByDirection: function ( dir ) {
441 var svh = this._getScrollHierarchy(),
448 svdir = sv.options.direction;
450 if (!svdir || svdir === dir) {
457 _handleDragStart: function ( e, ex, ey ) {
460 this._didDrag = false;
461 this._skip_dragging = false;
463 var target = $( e.target ),
466 svdir = this.options.direction;
468 /* should prevent the default behavior when click the button */
469 this._is_button = target.is( '.ui-btn-text' ) ||
470 target.is( '.ui-btn-inner' ) ||
471 target.is( '.ui-btn-inner .ui-icon' );
474 * We need to prevent the default behavior to
475 * suppress accidental selection of text, etc.
477 this._is_inputbox = target.is(':input') ||
478 target.parents(':input').length > 0;
480 if ( this._is_inputbox ) {
481 target.one( "resize.scrollview", function () {
482 if ( ey > $c.height() ) {
483 self.scrollTo( -ex, self._sy - ey + $c.height(),
484 self.options.snapbackDuration );
492 this._doSnapBackX = false;
493 this._doSnapBackY = false;
496 this._directionLock = "";
499 this._enableTracking();
501 this._set_scrollbar_size();
504 _propagateDragMove: function ( sv, e, ex, ey, dir ) {
505 this._hideScrollBars();
506 this._disableTracking();
507 sv._handleDragStart( e, ex, ey );
508 sv._directionLock = dir;
509 sv._didDrag = this._didDrag;
512 _handleDragMove: function ( e, ex, ey ) {
513 if ( this._skip_dragging ) {
517 if ( !this._dragging ) {
521 if ( !this._is_inputbox && !this._is_button ) {
525 var mt = this.options.moveThreshold,
526 dx = ex - this._lastX,
527 dy = ey - this._lastY,
528 svdir = this.options.direction,
538 if ( Math.abs( this._startY - ey ) < mt && !this._didDrag ) {
542 this._lastMove = getCurrentTime();
544 if ( !this._directionLock ) {
548 if ( x < mt && y < mt ) {
552 if ( x < y && (x / y) < 0.5 ) {
554 } else if ( x > y && (y / x) < 0.5 ) {
558 if ( svdir && dir && svdir !== dir ) {
560 * This scrollview can't handle the direction the user
561 * is attempting to scroll. Find an ancestor scrollview
562 * that can handle the request.
565 sv = this._getAncestorByDirection( dir );
567 this._propagateDragMove( sv, e, ex, ey, dir );
572 this._directionLock = svdir || (dir || "none");
577 dirLock = this._directionLock;
579 if ( dirLock !== "y" && this._hTracker ) {
584 this._doSnapBackX = false;
586 scope = ( newX > 0 || newX < this._maxX );
588 if ( scope && dirLock === "x" ) {
589 sv = this._getAncestorByDirection("x");
591 this._setScrollPosition( newX > 0 ?
592 0 : this._maxX, newY );
593 this._propagateDragMove( sv, e, ex, ey, dir );
597 newX = x + ( dx / 2 );
598 this._doSnapBackX = true;
602 if ( dirLock !== "x" && this._vTracker ) {
607 this._doSnapBackY = false;
609 scope = ( newY > 0 || newY < this._maxY );
611 if ( scope && dirLock === "y" ) {
612 sv = this._getAncestorByDirection("y");
614 this._setScrollPosition( newX,
615 newY > 0 ? 0 : this._maxY );
616 this._propagateDragMove( sv, e, ex, ey, dir );
620 newY = y + ( dy / 2 );
621 this._doSnapBackY = true;
625 if ( this.options.overshootEnable === false ) {
626 this._doSnapBackX = false;
627 this._doSnapBackY = false;
630 this._didDrag = true;
634 this._setScrollPosition( newX, newY );
636 this._showScrollBars();
639 _handleDragStop: function ( e ) {
640 if ( this._skip_dragging ) {
644 var l = this._lastMove,
645 t = getCurrentTime(),
646 doScroll = (l && (t - l) <= this.options.moveIntervalThreshold),
647 sx = ( this._hTracker && this._speedX && doScroll ) ?
648 this._speedX : ( this._doSnapBackX ? 1 : 0 ),
649 sy = ( this._vTracker && this._speedY && doScroll ) ?
650 this._speedY : ( this._doSnapBackY ? 1 : 0 ),
651 svdir = this.options.direction,
656 this._startMScroll( sx, sy );
658 this._hideScrollBars();
661 this._disableTracking();
663 return !this._didDrag;
666 _enableTracking: function () {
667 this._dragging = true;
670 _disableTracking: function () {
671 this._dragging = false;
674 _showScrollBars: function () {
675 var vclass = "ui-scrollbar-visible";
677 if ( !this.options.showScrollBars ) {
680 if ( this._scrollbar_showed ) {
684 if ( this._$vScrollBar ) {
685 this._$vScrollBar.addClass( vclass );
687 if ( this._$hScrollBar ) {
688 this._$hScrollBar.addClass( vclass );
691 this._scrollbar_showed = true;
694 _hideScrollBars: function () {
695 var vclass = "ui-scrollbar-visible";
697 if ( !this.options.showScrollBars ) {
700 if ( !this._scrollbar_showed ) {
704 if ( this._$vScrollBar ) {
705 this._$vScrollBar.removeClass( vclass );
707 if ( this._$hScrollBar ) {
708 this._$hScrollBar.removeClass( vclass );
711 this._scrollbar_showed = false;
714 _add_event: function () {
719 if ( this.options.eventType === "mouse" ) {
720 this._dragEvt = "mousedown mousemove mouseup click mousewheel";
722 this._dragCB = function ( e ) {
725 return self._handleDragStart( e,
726 e.clientX, e.clientY );
729 return self._handleDragMove( e,
730 e.clientX, e.clientY );
733 return self._handleDragStop( e );
736 return !self._didDrag;
739 var old = self.getScrollPosition();
740 self.scrollTo( -old.x,
741 -(old.y - e.originalEvent.wheelDelta) );
746 this._dragEvt = "touchstart touchmove touchend click";
748 this._dragCB = function ( e ) {
753 t = e.originalEvent.targetTouches[0];
754 return self._handleDragStart( e,
758 t = e.originalEvent.targetTouches[0];
759 return self._handleDragMove( e,
763 return self._handleDragStop( e );
766 return !self._didDrag;
771 $v.bind( this._dragEvt, this._dragCB );
773 if ( $c.jqmData("scroll") !== "y" ) {
777 $c.bind( "updatelayout", function ( e ) {
778 var $page = $c.parentsUntil("ui-page"),
782 if ( !$c.height() || !$v.height() ) {
783 self.scrollTo( 0, 0, 0 );
787 sy = $c.height() - $v.height();
788 vh = $v.height() - self._view_height;
790 self._view_height = $v.height();
792 if ( vh == 0 || vh > $c.height() / 2 ) {
796 if ( self._sy - sy <= -vh ) {
797 self.scrollTo( 0, self._sy,
798 self.options.snapbackDuration );
799 } else if ( self._sy - sy <= vh + self.options.moveThreshold ) {
800 self.scrollTo( 0, sy,
801 self.options.snapbackDuration );
805 $( window ).bind( "resize", function ( e ) {
806 var $page = $c.parentsUntil("ui-page"),
809 if ( !$c.height() || !$v.height() ) {
813 focused = $c.find(".ui-focus");
816 focused.trigger("resize.scrollview");
820 if ( self._sy < $c.height() - $v.height() ) {
821 self.scrollTo( 0, self._sy,
822 self.options.snapbackDuration );
825 self._view_height = $v.height();
829 _add_scrollbar: function () {
830 var $c = this._$clip,
831 prefix = "<div class=\"ui-scrollbar ui-scrollbar-",
832 suffix = "\"><div class=\"ui-scrollbar-track\"><div class=\"ui-scrollbar-thumb\"></div></div></div>";
834 if ( !this.options.showScrollBars ) {
838 if ( this._vTracker ) {
839 $c.append( prefix + "y" + suffix );
840 this._$vScrollBar = $c.children(".ui-scrollbar-y");
842 if ( this._hTracker ) {
843 $c.append( prefix + "x" + suffix );
844 this._$hScrollBar = $c.children(".ui-scrollbar-x");
847 this._scrollbar_showed = false;
850 _add_scroll_jump: function () {
851 var $c = this._$clip,
856 if ( !this.options.scrollJump ) {
860 if ( this._vTracker ) {
861 top_btn = $( '<div class="ui-scroll-jump-top-bg ui-btn" data-theme="s">' +
862 '<div class="ui-scroll-jump-top"></div></div>' );
863 $c.append( top_btn );
865 top_btn.bind( "vclick", function () {
866 self.scrollTo( 0, 0, self.options.overshootDuration );
870 if ( this._hTracker ) {
871 left_btn = $( '<div class="ui-scroll-jump-left-bg ui-btn" data-theme="s">' +
872 '<div class="ui-scroll-jump-left"></div></div>' );
873 $c.append( left_btn );
875 left_btn.bind( "vclick", function () {
876 self.scrollTo( 0, 0, self.options.overshootDuration );
881 _set_scrollbar_size: function () {
882 var $c = this._$clip,
890 if ( !this.options.showScrollBars ) {
894 if ( this._hTracker ) {
897 this._maxX = cw - vw;
899 if ( this._maxX > 0 ) {
902 if ( this._$hScrollBar && vw ) {
903 thumb = this._$hScrollBar.find(".ui-scrollbar-thumb");
904 thumb.css( "width", (cw >= vw ? "100%" :
905 (Math.floor(cw / vw * 100) || 1) + "%") );
909 if ( this._vTracker ) {
912 this._maxY = ch - vh;
914 if ( this._maxY > 0 ) {
917 if ( this._$vScrollBar && vh ) {
918 thumb = this._$vScrollBar.find(".ui-scrollbar-thumb");
919 thumb.css( "height", (ch >= vh ? "100%" :
920 (Math.floor(ch / vh * 100) || 1) + "%") );
926 $.extend( MomentumTracker.prototype, {
927 start: function ( pos, speed, duration, minPos, maxPos ) {
928 var tstate = ( pos < minPos || pos > maxPos ) ?
929 tstates.snapback : tstates.scrolling,
932 this.state = ( speed !== 0 ) ? tstate : tstates.done;
935 this.duration = ( this.state === tstates.snapback ) ?
936 this.options.snapbackDuration : duration;
937 this.minPos = minPos;
938 this.maxPos = maxPos;
940 this.fromPos = ( this.state === tstates.snapback ) ? this.pos : 0;
941 pos_temp = ( this.pos < this.minPos ) ? this.minPos : this.maxPos;
942 this.toPos = ( this.state === tstates.snapback ) ? pos_temp : 0;
944 this.startTime = getCurrentTime();
948 this.state = tstates.done;
956 update: function ( overshootEnable ) {
957 var state = this.state,
958 cur_time = getCurrentTime(),
959 duration = this.duration,
960 elapsed = cur_time - this.startTime,
965 if ( state === tstates.done ) {
969 elapsed = elapsed > duration ? duration : elapsed;
971 if ( state === tstates.scrolling || state === tstates.overshot ) {
973 ( 1 - $.easing[this.easing]( elapsed / duration,
974 elapsed, 0, 1, duration ) );
978 didOverShoot = ( state === tstates.scrolling ) &&
979 ( x < this.minPos || x > this.maxPos );
981 if ( didOverShoot ) {
982 x = ( x < this.minPos ) ? this.minPos : this.maxPos;
987 if ( state === tstates.overshot ) {
988 if ( elapsed >= duration ) {
989 this.state = tstates.snapback;
990 this.fromPos = this.pos;
991 this.toPos = ( x < this.minPos ) ?
992 this.minPos : this.maxPos;
993 this.duration = this.options.snapbackDuration;
994 this.startTime = cur_time;
997 } else if ( state === tstates.scrolling ) {
998 if ( didOverShoot && overshootEnable ) {
999 this.state = tstates.overshot;
1000 this.speed = dx / 2;
1001 this.duration = this.options.overshootDuration;
1002 this.startTime = cur_time;
1003 } else if ( elapsed >= duration ) {
1004 this.state = tstates.done;
1007 } else if ( state === tstates.snapback ) {
1008 if ( elapsed >= duration ) {
1009 this.pos = this.toPos;
1010 this.state = tstates.done;
1012 this.pos = this.fromPos + (( this.toPos - this.fromPos ) *
1013 $.easing[this.easing]( elapsed / duration,
1014 elapsed, 0, 1, duration ));
1022 return this.state === tstates.done;
1025 getPosition: function () {
1030 $( document ).bind( 'pagecreate create', function ( e ) {
1031 var $page = $( e.target ),
1032 content_scroll = $page.find(".ui-content").jqmData("scroll");
1034 /* content scroll */
1035 if ( $.support.scrollview === undefined ) {
1036 $.support.scrollview = true;
1039 if ( $.support.scrollview === true && content_scroll === undefined ) {
1040 content_scroll = "y";
1043 if ( content_scroll !== "y" ) {
1044 content_scroll = "none";
1047 $page.find(".ui-content").attr( "data-scroll", content_scroll );
1049 $page.find(":jqmData(scroll):not(.ui-scrollview-clip)").each( function () {
1050 if ( $( this ).hasClass("ui-scrolllistview") ) {
1051 $( this ).scrolllistview();
1053 var st = $( this ).jqmData("scroll"),
1054 dir = st && ( st.search(/^[xy]/) !== -1 ) ? st.charAt(0) : null,
1057 if ( st === "none" ) {
1062 direction: dir || undefined,
1063 scrollMethod: $( this ).jqmData("scroll-method") || undefined,
1064 scrollJump: $( this ).jqmData("scroll-jump") || undefined
1067 $( this ).scrollview( opts );
1072 $( document ).bind( 'pageshow', function ( e ) {
1073 var $page = $( e.target ),
1074 scroll = $page.find(".ui-content").jqmData("scroll");
1076 if ( scroll === "y" ) {
1077 resizePageContentHeight( e.target );
1081 }( jQuery, window, document ) );