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,
64 _makePositioned: function ( $ele ) {
65 if ( $ele.css("position") === "static" ) {
66 $ele.css( "position", "relative" );
70 _create: function () {
71 var $page = $('.ui-page'),
75 this._$clip = $( this.element ).addClass("ui-scrollview-clip");
76 this._$view = this._$clip.wrapInner("<div></div>").children()
77 .addClass("ui-scrollview-view");
79 if ( this.options.scrollMethod === "translate" ) {
80 if ( this._$view.css("transform") === undefined ) {
81 this.options.scrollMethod = "position";
85 this._$clip.css( "overflow", "hidden" );
86 this._makePositioned( this._$clip );
88 this._makePositioned( this._$view );
89 this._$view.css( { left: 0, top: 0 } );
90 this._view_height = this._$view.height();
95 direction = this.options.direction;
97 this._hTracker = ( direction !== "y" ) ?
98 new MomentumTracker( this.options ) : null;
99 this._vTracker = ( direction !== "x" ) ?
100 new MomentumTracker( this.options ) : null;
102 this._timerInterval = 1000 / this.options.fps;
105 this._timerCB = function () {
106 self._handleMomentumScroll();
110 this._add_scrollbar();
113 _startMScroll: function ( speedX, speedY ) {
114 var keepGoing = false,
115 duration = this.options.scrollDuration,
122 this._showScrollBars();
124 this._$clip.trigger( this.options.startEventName );
127 c = this._$clip.width();
128 v = this._$view.width();
130 ht.start( this._sx, speedX,
131 duration, (v > c) ? -(v - c) : 0, 0 );
132 keepGoing = !ht.done();
136 c = this._$clip.height();
137 v = this._$view.height() +
138 parseFloat( this._$view.css("padding-top") );
140 vt.start( this._sy, speedY,
141 duration, (v > c) ? -(v - c) : 0, 0 );
142 keepGoing = keepGoing || !vt.done();
146 this._timerID = setTimeout( this._timerCB, this._timerInterval );
152 _stopMScroll: function () {
153 if ( this._timerID ) {
154 this._$clip.trigger( this.options.stopEventName );
155 clearTimeout( this._timerID );
159 if ( this._vTracker ) {
160 this._vTracker.reset();
163 if ( this._hTracker ) {
164 this._hTracker.reset();
167 this._hideScrollBars();
170 _handleMomentumScroll: function () {
171 var keepGoing = false,
177 if ( this._outerScrolling ) {
182 vt.update( this.options.overshootEnable );
183 y = vt.getPosition();
184 keepGoing = !vt.done();
188 ht.update( this.options.overshootEnable );
189 x = ht.getPosition();
190 keepGoing = keepGoing || !ht.done();
193 this._setScrollPosition( x, y );
194 this._$clip.trigger( this.options.updateEventName,
195 [ { x: x, y: y } ] );
198 this._timerID = setTimeout( this._timerCB, this._timerInterval );
204 _setElementTransform: function ( $ele, x, y, duration ) {
208 if ( !duration || duration === undefined ) {
211 transition = "-webkit-transform " + duration / 1000 + "s";
214 if ( $.support.cssTransform3d ) {
215 translate = "translate3d(" + x + "," + y + ", 0px)";
217 translate = "translate(" + x + "," + y + ")";
221 "-moz-transform": translate,
222 "-webkit-transform": translate,
223 "-ms-transform": translate,
224 "-o-transform": translate,
225 "transform": translate,
226 "-webkit-transition": transition
230 _setCalibration: function ( x, y ) {
231 if ( this.options.overshootEnable ) {
237 var $v = this._$view,
239 dirLock = this._directionLock,
243 if ( dirLock !== "y" && this._hTracker ) {
244 scroll_width = $v.width() - $c.width();
248 } else if ( x < -scroll_width ) {
249 this._sx = -scroll_width;
254 if ( scroll_width < 0 ) {
259 if ( dirLock !== "x" && this._vTracker ) {
260 scroll_height = $v.height() - $c.height() +
261 parseFloat( $c.css("padding-top") ) +
262 parseFloat( $c.css("padding-bottom") );
264 this._outerScroll( y, scroll_height );
268 } else if ( y < -scroll_height ) {
269 this._sy = -scroll_height;
274 if ( scroll_height < 0 ) {
280 _setScrollPosition: function ( x, y, duration ) {
281 var $v = this._$view,
282 sm = this.options.scrollMethod,
283 $vsb = this._$vScrollBar,
284 $hsb = this._$hScrollBar,
287 this._setCalibration( x, y );
289 if ( this._outerScrolling ) {
296 if ( sm === "translate" ) {
297 this._setElementTransform( $v, x + "px", y + "px", duration );
299 $v.css( {left: x + "px", top: y + "px"} );
303 $sbt = $vsb.find(".ui-scrollbar-thumb");
305 if ( sm === "translate" ) {
306 this._setElementTransform( $sbt, "0px",
307 -y / $v.height() * $sbt.parent().height() + "px",
310 $sbt.css( "top", -y / $v.height() * 100 + "%" );
315 $sbt = $hsb.find(".ui-scrollbar-thumb");
317 if ( sm === "translate" ) {
318 this._setElementTransform( $sbt,
319 -x / $v.width() * $sbt.parent().width() + "px", "0px",
322 $sbt.css("left", -x / $v.width() * 100 + "%");
327 _outerScroll: function ( y, scroll_height ) {
329 top = $( window ).scrollTop(),
331 duration = this.options.snapbackDuration,
332 start = getCurrentTime(),
335 if ( this._$clip.jqmData("scroll") !== "y" ) {
339 if ( this._outerScrolling ) {
345 } else if ( y < -scroll_height ) {
346 sy = -y - scroll_height;
353 tfunc = function () {
354 var elapsed = getCurrentTime() - start;
356 if ( elapsed >= duration ) {
357 window.scrollTo( 0, top + sy );
358 self._outerScrolling = undefined;
360 ec = $.easing.easeOutQuad( elapsed / duration, elapsed, 0, 1, duration );
362 window.scrollTo( 0, top + ( sy * ec ) );
363 self._outerScrolling = setTimeout( tfunc, self._timerInterval );
366 this._outerScrolling = setTimeout( tfunc, self._timerInterval );
368 /* skip the srollview dragging */
369 this._skip_dragging = true;
372 _scrollTo: function ( x, y, duration ) {
374 start = getCurrentTime(),
375 efunc = $.easing.easeOutQuad,
385 tfunc = function () {
386 var elapsed = getCurrentTime() - start,
389 if ( elapsed >= duration ) {
391 self._setScrollPosition( x, y );
393 ec = efunc( elapsed / duration, elapsed, 0, 1, duration );
395 self._setScrollPosition( sx + ( dx * ec ), sy + ( dy * ec ) );
396 self._timerID = setTimeout( tfunc, self._timerInterval );
400 this._timerID = setTimeout( tfunc, this._timerInterval );
403 scrollTo: function ( x, y, duration ) {
406 if ( !duration || this.options.scrollMethod === "translate" ) {
407 this._setScrollPosition( x, y, duration );
409 this._scrollTo( x, y, duration );
413 getScrollPosition: function () {
414 return { x: -this._sx, y: -this._sy };
417 _getScrollHierarchy: function () {
421 this._$clip.parents( ".ui-scrollview-clip").each( function () {
422 d = $( this ).jqmData("scrollview");
430 _getAncestorByDirection: function ( dir ) {
431 var svh = this._getScrollHierarchy(),
438 svdir = sv.options.direction;
440 if (!svdir || svdir === dir) {
447 _handleDragStart: function ( e, ex, ey ) {
450 this._didDrag = false;
452 var target = $( e.target ),
455 svdir = this.options.direction;
457 /* should skip the dragging when click the button */
458 this._skip_dragging = target.is( '.ui-btn-text' ) ||
459 target.is( '.ui-btn-inner' ) ||
460 target.is( '.ui-btn-inner .ui-icon' );
462 if ( this._skip_dragging ) {
467 * We need to prevent the default behavior to
468 * suppress accidental selection of text, etc.
470 this._shouldBlockEvent = !( target.is(':input') ||
471 target.parents(':input').length > 0 );
473 if ( this._shouldBlockEvent ) {
474 if ( this.options.eventType === "mouse" ) {
478 target.one( "resize.scrollview", function () {
479 if ( ey > $c.height() ) {
480 self.scrollTo( -ex, self._sy - ey + $c.height(),
481 self.options.snapbackDuration );
491 this._doSnapBackX = false;
492 this._doSnapBackY = false;
495 this._directionLock = "";
498 this._enableTracking();
500 this._set_scrollbar_size();
503 _propagateDragMove: function ( sv, e, ex, ey, dir ) {
504 this._hideScrollBars();
505 this._disableTracking();
506 sv._handleDragStart( e, ex, ey );
507 sv._directionLock = dir;
508 sv._didDrag = this._didDrag;
511 _handleDragMove: function ( e, ex, ey ) {
512 if ( this._skip_dragging ) {
516 if ( !this._dragging ) {
520 if ( this._shouldBlockEvent ) {
524 var mt = this.options.moveThreshold,
525 dx = ex - this._lastX,
526 dy = ey - this._lastY,
527 svdir = this.options.direction,
537 if ( Math.abs( this._startY - ey ) < mt && !this._didDrag ) {
541 this._lastMove = getCurrentTime();
543 if ( !this._directionLock ) {
547 if ( x < mt && y < mt ) {
551 if ( x < y && (x / y) < 0.5 ) {
553 } else if ( x > y && (y / x) < 0.5 ) {
557 if ( svdir && dir && svdir !== dir ) {
559 * This scrollview can't handle the direction the user
560 * is attempting to scroll. Find an ancestor scrollview
561 * that can handle the request.
564 sv = this._getAncestorByDirection( dir );
566 this._propagateDragMove( sv, e, ex, ey, dir );
571 this._directionLock = svdir || (dir || "none");
576 dirLock = this._directionLock;
578 if ( dirLock !== "y" && this._hTracker ) {
583 this._doSnapBackX = false;
585 scope = ( newX > 0 || newX < this._maxX );
587 if ( scope && dirLock === "x" ) {
588 sv = this._getAncestorByDirection("x");
590 this._setScrollPosition( newX > 0 ?
591 0 : this._maxX, newY );
592 this._propagateDragMove( sv, e, ex, ey, dir );
596 newX = x + ( dx / 2 );
597 this._doSnapBackX = true;
601 if ( dirLock !== "x" && this._vTracker ) {
606 this._doSnapBackY = false;
608 scope = ( newY > 0 || newY < this._maxY );
610 if ( scope && dirLock === "y" ) {
611 sv = this._getAncestorByDirection("y");
613 this._setScrollPosition( newX,
614 newY > 0 ? 0 : this._maxY );
615 this._propagateDragMove( sv, e, ex, ey, dir );
619 newY = y + ( dy / 2 );
620 this._doSnapBackY = true;
624 if ( this.options.overshootEnable === false ) {
625 this._doSnapBackX = false;
626 this._doSnapBackY = false;
629 this._didDrag = true;
633 this._setScrollPosition( newX, newY );
635 this._showScrollBars();
638 _handleDragStop: function ( e ) {
639 if ( this._skip_dragging ) {
643 var l = this._lastMove,
644 t = getCurrentTime(),
645 doScroll = (l && (t - l) <= this.options.moveIntervalThreshold),
646 sx = ( this._hTracker && this._speedX && doScroll ) ?
647 this._speedX : ( this._doSnapBackX ? 1 : 0 ),
648 sy = ( this._vTracker && this._speedY && doScroll ) ?
649 this._speedY : ( this._doSnapBackY ? 1 : 0 ),
650 svdir = this.options.direction,
655 this._startMScroll( sx, sy );
657 this._hideScrollBars();
660 this._disableTracking();
662 return !this._didDrag;
665 _enableTracking: function () {
666 this._dragging = true;
669 _disableTracking: function () {
670 this._dragging = false;
673 _showScrollBars: function () {
674 var vclass = "ui-scrollbar-visible";
676 if ( !this.options.showScrollBars ) {
679 if ( this._scrollbar_showed ) {
683 if ( this._$vScrollBar ) {
684 this._$vScrollBar.addClass( vclass );
686 if ( this._$hScrollBar ) {
687 this._$hScrollBar.addClass( vclass );
690 this._scrollbar_showed = true;
693 _hideScrollBars: function () {
694 var vclass = "ui-scrollbar-visible";
696 if ( !this.options.showScrollBars ) {
699 if ( !this._scrollbar_showed ) {
703 if ( this._$vScrollBar ) {
704 this._$vScrollBar.removeClass( vclass );
706 if ( this._$hScrollBar ) {
707 this._$hScrollBar.removeClass( vclass );
710 this._scrollbar_showed = false;
713 _add_event: function () {
718 if ( this.options.eventType === "mouse" ) {
719 this._dragEvt = "mousedown mousemove mouseup click mousewheel";
721 this._dragCB = function ( e ) {
724 return self._handleDragStart( e,
725 e.clientX, e.clientY );
728 return self._handleDragMove( e,
729 e.clientX, e.clientY );
732 return self._handleDragStop( e );
735 return !self._didDrag;
738 var old = self.getScrollPosition();
739 self.scrollTo( -old.x,
740 -(old.y - e.originalEvent.wheelDelta) );
745 this._dragEvt = "touchstart touchmove touchend click";
747 this._dragCB = function ( e ) {
752 t = e.originalEvent.targetTouches[0];
753 return self._handleDragStart( e,
757 t = e.originalEvent.targetTouches[0];
758 return self._handleDragMove( e,
762 return self._handleDragStop( e );
765 return !self._didDrag;
770 $v.bind( this._dragEvt, this._dragCB );
772 if ( $c.jqmData("scroll") !== "y" ) {
776 $c.bind( "updatelayout", function ( e ) {
777 var $page = $c.parentsUntil("ui-page"),
781 if ( !$c.height() || !$v.height() ) {
782 self.scrollTo( 0, 0, 0 );
786 sy = $c.height() - $v.height();
787 vh = $v.height() - self._view_height;
789 self._view_height = $v.height();
791 if ( vh == 0 || vh > $c.height() / 2 ) {
795 if ( self._sy - sy <= -vh ) {
796 self.scrollTo( 0, self._sy,
797 self.options.snapbackDuration );
798 } else if ( self._sy - sy <= vh + self.options.moveThreshold ) {
799 self.scrollTo( 0, sy,
800 self.options.snapbackDuration );
804 $( window ).bind( "resize", function ( e ) {
805 var $page = $c.parentsUntil("ui-page"),
808 if ( !$c.height() || !$v.height() ) {
812 focused = $c.find(".ui-focus");
815 focused.trigger("resize.scrollview");
819 if ( self._sy < $c.height() - $v.height() ) {
820 self.scrollTo( 0, self._sy,
821 self.options.snapbackDuration );
824 self._view_height = $v.height();
828 _add_scrollbar: function () {
829 var $c = this._$clip,
830 prefix = "<div class=\"ui-scrollbar ui-scrollbar-",
831 suffix = "\"><div class=\"ui-scrollbar-track\"><div class=\"ui-scrollbar-thumb\"></div></div></div>";
833 if ( !this.options.showScrollBars ) {
837 if ( this._vTracker ) {
838 $c.append( prefix + "y" + suffix );
839 this._$vScrollBar = $c.children(".ui-scrollbar-y");
841 if ( this._hTracker ) {
842 $c.append( prefix + "x" + suffix );
843 this._$hScrollBar = $c.children(".ui-scrollbar-x");
846 this._scrollbar_showed = false;
849 _set_scrollbar_size: function () {
850 var $c = this._$clip,
858 if ( !this.options.showScrollBars ) {
862 if ( this._hTracker ) {
865 this._maxX = cw - vw;
867 if ( this._maxX > 0 ) {
870 if ( this._$hScrollBar && vw ) {
871 thumb = this._$hScrollBar.find(".ui-scrollbar-thumb");
872 thumb.css( "width", (cw >= vw ? "100%" :
873 (Math.floor(cw / vw * 100) || 1) + "%") );
877 if ( this._vTracker ) {
880 this._maxY = ch - vh;
882 if ( this._maxY > 0 ) {
885 if ( this._$vScrollBar && vh ) {
886 thumb = this._$vScrollBar.find(".ui-scrollbar-thumb");
887 thumb.css( "height", (ch >= vh ? "100%" :
888 (Math.floor(ch / vh * 100) || 1) + "%") );
894 $.extend( MomentumTracker.prototype, {
895 start: function ( pos, speed, duration, minPos, maxPos ) {
896 var tstate = ( pos < minPos || pos > maxPos ) ?
897 tstates.snapback : tstates.scrolling,
900 this.state = ( speed !== 0 ) ? tstate : tstates.done;
903 this.duration = ( this.state === tstates.snapback ) ?
904 this.options.snapbackDuration : duration;
905 this.minPos = minPos;
906 this.maxPos = maxPos;
908 this.fromPos = ( this.state === tstates.snapback ) ? this.pos : 0;
909 pos_temp = ( this.pos < this.minPos ) ? this.minPos : this.maxPos;
910 this.toPos = ( this.state === tstates.snapback ) ? pos_temp : 0;
912 this.startTime = getCurrentTime();
916 this.state = tstates.done;
924 update: function ( overshootEnable ) {
925 var state = this.state,
926 cur_time = getCurrentTime(),
927 duration = this.duration,
928 elapsed = cur_time - this.startTime,
933 if ( state === tstates.done ) {
937 elapsed = elapsed > duration ? duration : elapsed;
939 if ( state === tstates.scrolling || state === tstates.overshot ) {
941 ( 1 - $.easing[this.easing]( elapsed / duration,
942 elapsed, 0, 1, duration ) );
946 didOverShoot = ( state === tstates.scrolling ) &&
947 ( x < this.minPos || x > this.maxPos );
949 if ( didOverShoot ) {
950 x = ( x < this.minPos ) ? this.minPos : this.maxPos;
955 if ( state === tstates.overshot ) {
956 if ( elapsed >= duration ) {
957 this.state = tstates.snapback;
958 this.fromPos = this.pos;
959 this.toPos = ( x < this.minPos ) ?
960 this.minPos : this.maxPos;
961 this.duration = this.options.snapbackDuration;
962 this.startTime = cur_time;
965 } else if ( state === tstates.scrolling ) {
966 if ( didOverShoot && overshootEnable ) {
967 this.state = tstates.overshot;
969 this.duration = this.options.overshootDuration;
970 this.startTime = cur_time;
971 } else if ( elapsed >= duration ) {
972 this.state = tstates.done;
975 } else if ( state === tstates.snapback ) {
976 if ( elapsed >= duration ) {
977 this.pos = this.toPos;
978 this.state = tstates.done;
980 this.pos = this.fromPos + (( this.toPos - this.fromPos ) *
981 $.easing[this.easing]( elapsed / duration,
982 elapsed, 0, 1, duration ));
990 return this.state === tstates.done;
993 getPosition: function () {
998 $( document ).bind( 'pagecreate create', function ( e ) {
999 var $page = $( e.target ),
1000 content_scroll = $page.find(".ui-content").jqmData("scroll");
1002 /* content scroll */
1003 if ( $.support.scrollview === undefined ) {
1004 $.support.scrollview = true;
1007 if ( $.support.scrollview === true && content_scroll === undefined ) {
1008 content_scroll = "y";
1011 if ( content_scroll !== "y" ) {
1012 content_scroll = "none";
1015 $page.find(".ui-content").attr( "data-scroll", content_scroll );
1017 $page.find(":jqmData(scroll):not(.ui-scrollview-clip)").each( function () {
1018 if ( $( this ).hasClass("ui-scrolllistview") ) {
1019 $( this ).scrolllistview();
1021 var st = $( this ).jqmData("scroll"),
1022 dir = st && ( st.search(/^[xy]/) !== -1 ) ? st.charAt(0) : null,
1025 if ( st === "none" ) {
1030 direction: dir || undefined,
1031 scrollMethod: $( this ).jqmData("scroll-method") || undefined
1034 $( this ).scrollview( opts );
1039 $( document ).bind( 'pageshow', function ( e ) {
1040 var $page = $( e.target ),
1041 scroll = $page.find(".ui-content").jqmData("scroll");
1043 if ( scroll === "y" ) {
1044 resizePageContentHeight( e.target );
1048 }( jQuery, window, document ) );