scala-news-reader

rss/atom news reader in scala

git clone https://9o.is/git/scala-news-reader.git

jquery.tinyscrollbar.js

(7813B)


      1 ( function( $ )
      2 {
      3     $.tiny = $.tiny || { };
      4 
      5     $.tiny.scrollbar = {
      6         options: {
      7                 axis         : 'y'    // vertical or horizontal scrollbar? ( x || y ).
      8             ,   wheel        : 40     // how many pixels must the mouswheel scroll at a time.
      9             ,   scroll       : true   // enable or disable the mousewheel.
     10             ,   lockscroll   : true   // return scrollwheel to browser if there is no more content.
     11             ,   size         : 'auto' // set the size of the scrollbar to auto or a fixed number.
     12             ,   sizethumb    : 'auto' // set the size of the thumb to auto or a fixed number.
     13             ,   invertscroll : false  // Enable mobile invert style scrolling
     14         }
     15     };
     16 
     17     $.fn.tinyscrollbar = function( params )
     18     {
     19         var options = $.extend( {}, $.tiny.scrollbar.options, params );
     20         
     21         this.each( function()
     22         { 
     23             $( this ).data('tsb', new Scrollbar( $( this ), options ) ); 
     24         });
     25 
     26         return this;
     27     };
     28 
     29     $.fn.tinyscrollbar_update = function(sScroll)
     30     {
     31         return $( this ).data( 'tsb' ).update( sScroll ); 
     32     };
     33 
     34     function Scrollbar( root, options )
     35     {
     36         var oSelf       = this
     37         ,   oWrapper    = root
     38         ,   oViewport   = { obj: $( '.viewport', root ) }
     39         ,   oContent    = { obj: $( '.overview', root ) }
     40         ,   oScrollbar  = { obj: $( '.scrollbar', root ) }
     41         ,   oTrack      = { obj: $( '.track', oScrollbar.obj ) }
     42         ,   oThumb      = { obj: $( '.thumb', oScrollbar.obj ) }
     43         ,   sAxis       = options.axis === 'x'
     44         ,   sDirection  = sAxis ? 'left' : 'top'
     45         ,   sSize       = sAxis ? 'Width' : 'Height'
     46         ,   iScroll     = 0
     47         ,   iPosition   = { start: 0, now: 0 }
     48         ,   iMouse      = {}
     49         ,   touchEvents = 'ontouchstart' in document.documentElement
     50         ;
     51 
     52         function initialize()
     53         {
     54             oSelf.update();
     55             setEvents();
     56 
     57             return oSelf;
     58         }
     59 
     60         this.update = function( sScroll )
     61         {
     62             oViewport[ options.axis ] = oViewport.obj[0][ 'offset'+ sSize ];
     63             oContent[ options.axis ]  = oContent.obj[0][ 'scroll'+ sSize ];
     64             oContent.ratio            = oViewport[ options.axis ] / oContent[ options.axis ];
     65 
     66             oScrollbar.obj.toggleClass( 'disable', oContent.ratio >= 1 );
     67 
     68             oTrack[ options.axis ] = options.size === 'auto' ? oViewport[ options.axis ] : options.size;
     69             oThumb[ options.axis ] = Math.min( oTrack[ options.axis ], Math.max( 0, ( options.sizethumb === 'auto' ? ( oTrack[ options.axis ] * oContent.ratio ) : options.sizethumb ) ) );
     70         
     71             oScrollbar.ratio = options.sizethumb === 'auto' ? ( oContent[ options.axis ] / oTrack[ options.axis ] ) : ( oContent[ options.axis ] - oViewport[ options.axis ] ) / ( oTrack[ options.axis ] - oThumb[ options.axis ] );
     72             
     73             iScroll = ( sScroll === 'relative' && oContent.ratio <= 1 ) ? Math.min( ( oContent[ options.axis ] - oViewport[ options.axis ] ), Math.max( 0, iScroll )) : 0;
     74             iScroll = ( sScroll === 'bottom' && oContent.ratio <= 1 ) ? ( oContent[ options.axis ] - oViewport[ options.axis ] ) : isNaN( parseInt( sScroll, 10 ) ) ? iScroll : parseInt( sScroll, 10 );
     75             
     76             setSize();
     77         };
     78 
     79         function setSize()
     80         {
     81             var sCssSize = sSize.toLowerCase();
     82 
     83             oThumb.obj.css( sDirection, iScroll / oScrollbar.ratio );
     84             oContent.obj.css( sDirection, -iScroll );
     85             iMouse.start = oThumb.obj.offset()[ sDirection ];
     86 
     87             oScrollbar.obj.css( sCssSize, oTrack[ options.axis ] );
     88             oTrack.obj.css( sCssSize, oTrack[ options.axis ] );
     89             oThumb.obj.css( sCssSize, oThumb[ options.axis ] );
     90         }
     91 
     92         function setEvents()
     93         {
     94             if( ! touchEvents )
     95             {
     96                 oThumb.obj.bind( 'mousedown', start );
     97                 oTrack.obj.bind( 'mouseup', drag );
     98             }
     99             else
    100             {
    101                 oViewport.obj[0].ontouchstart = function( event )
    102                 {   
    103                     if( 1 === event.touches.length )
    104                     {
    105                         start( event.touches[ 0 ] );
    106                         event.stopPropagation();
    107                     }
    108                 };
    109             }
    110 
    111             if( options.scroll && window.addEventListener )
    112             {
    113                 oWrapper[0].addEventListener( 'DOMMouseScroll', wheel, false );
    114                 oWrapper[0].addEventListener( 'mousewheel', wheel, false );
    115                 oWrapper[0].addEventListener( 'MozMousePixelScroll', function( event ){
    116                     event.preventDefault();
    117                 }, false);
    118             }
    119             else if( options.scroll )
    120             {
    121                 oWrapper[0].onmousewheel = wheel;
    122             }
    123         }
    124 
    125         function start( event )
    126         {
    127             $( "body" ).addClass( "noSelect" );
    128 
    129             var oThumbDir   = parseInt( oThumb.obj.css( sDirection ), 10 );
    130             iMouse.start    = sAxis ? event.pageX : event.pageY;
    131             iPosition.start = oThumbDir == 'auto' ? 0 : oThumbDir;
    132             
    133             if( ! touchEvents )
    134             {
    135                 $( document ).bind( 'mousemove', drag );
    136                 $( document ).bind( 'mouseup', end );
    137                 oThumb.obj.bind( 'mouseup', end );
    138             }
    139             else
    140             {
    141                 document.ontouchmove = function( event )
    142                 {
    143                     event.preventDefault();
    144                     drag( event.touches[ 0 ] );
    145                 };
    146                 document.ontouchend = end;        
    147             }
    148         }
    149 
    150         function wheel( event )
    151         {
    152             if( oContent.ratio < 1 )
    153             {
    154                 var oEvent = event || window.event
    155                 ,   iDelta = oEvent.wheelDelta ? oEvent.wheelDelta / 120 : -oEvent.detail / 3
    156                 ;
    157 
    158                 iScroll -= iDelta * options.wheel;
    159                 iScroll = Math.min( ( oContent[ options.axis ] - oViewport[ options.axis ] ), Math.max( 0, iScroll ));
    160 
    161                 oThumb.obj.css( sDirection, iScroll / oScrollbar.ratio );
    162                 oContent.obj.css( sDirection, -iScroll );
    163 
    164                 if( options.lockscroll || ( iScroll !== ( oContent[ options.axis ] - oViewport[ options.axis ] ) && iScroll !== 0 ) )
    165                 {
    166                     oEvent = $.event.fix( oEvent );
    167                     oEvent.preventDefault();
    168                 }
    169             }
    170         }
    171 
    172         function drag( event )
    173         {
    174             if( oContent.ratio < 1 )
    175             {
    176                 if( options.invertscroll && touchEvents )
    177                 {
    178                     iPosition.now = Math.min( ( oTrack[ options.axis ] - oThumb[ options.axis ] ), Math.max( 0, ( iPosition.start + ( iMouse.start - ( sAxis ? event.pageX : event.pageY ) ))));
    179                 }
    180                 else
    181                 {
    182                      iPosition.now = Math.min( ( oTrack[ options.axis ] - oThumb[ options.axis ] ), Math.max( 0, ( iPosition.start + ( ( sAxis ? event.pageX : event.pageY ) - iMouse.start))));
    183                 }
    184 
    185                 iScroll = iPosition.now * oScrollbar.ratio;
    186                 oContent.obj.css( sDirection, -iScroll );
    187                 oThumb.obj.css( sDirection, iPosition.now );
    188             }
    189         }
    190         
    191         function end()
    192         {
    193             $( "body" ).removeClass( "noSelect" );
    194             $( document ).unbind( 'mousemove', drag );
    195             $( document ).unbind( 'mouseup', end );
    196             oThumb.obj.unbind( 'mouseup', end );
    197             document.ontouchmove = document.ontouchend = null;
    198         }
    199 
    200         return initialize();
    201     }
    202 
    203 }(jQuery));