Advertisement
Guest User

page.js

a guest
Feb 21st, 2013
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Page = (function() {
  2.  
  3.     var $container = $( '#container' ),
  4.         $bookBlock = $( '#bb-bookblock' ),
  5.         $items = $bookBlock.children(),
  6.         itemsCount = $items.length,
  7.         current = 0,
  8.         bb = $( '#bb-bookblock' ).bookblock( {
  9.             speed : 400,
  10.             perspective : 2000,
  11.             shadowSides : 0.8,
  12.             shadowFlip  : 0.4,
  13.             onEndFlip : function(old, page, isLimit) {
  14.                
  15.                 current = page;
  16.                 // update TOC current
  17.                 updateTOC();
  18.                 // updateNavigation
  19.                 updateNavigation( isLimit );
  20.                 // initialize jScrollPane on the content div for the new item
  21.                 setJSP( 'init' );
  22.                 // destroy jScrollPane on the content div for the old item
  23.                 setJSP( 'destroy', old );
  24.  
  25.             }
  26.         } ),
  27.         $tourBtnP1 = $( '#tour_p1_next' ),
  28.         $tourBtnP2 = $( '#tour_p2_next' ),
  29.         $tourBtnP3 = $( '#tour_p3_next' ),
  30.         $tourBtnP4 = $( '#tour_p4_next' ),
  31.         $tourBtnP5 = $( '#tour_p5_next' ),
  32.         $tourBtnP6 = $( '#tour_p6_next' ),
  33.         $navNext = $( '#bb-nav-next' ),
  34.         $navPrev = $( '#bb-nav-prev' ).hide(),
  35.         $menuItems = $container.find( 'ul.menu-toc > li' ),
  36.         $tblcontents = $( '#tblcontents' ),
  37.         transEndEventNames = {
  38.             'WebkitTransition': 'webkitTransitionEnd',
  39.             'MozTransition': 'transitionend',
  40.             'OTransition': 'oTransitionEnd',
  41.             'msTransition': 'MSTransitionEnd',
  42.             'transition': 'transitionend'
  43.         },
  44.         transEndEventName = transEndEventNames[Modernizr.prefixed('transition')],
  45.         supportTransitions = Modernizr.csstransitions;
  46.  
  47.     function init() {
  48.  
  49.         // initialize jScrollPane on the content div of the first item
  50.         setJSP( 'init' );
  51.         initEvents();
  52.  
  53.     }
  54.    
  55.     function initEvents() {
  56.  
  57.         // add navigation events
  58.         $navNext.on( 'click', function() {
  59.             bb.next();
  60.             return false;
  61.         } );
  62.        
  63.         $tourBtnP1.on( 'click', function() {
  64.             bb.next();
  65.             return false;
  66.         } );
  67.        
  68.         $tourBtnP2.on( 'click', function() {
  69.             bb.next();
  70.             return false;
  71.         } );
  72.        
  73.         $tourBtnP3.on( 'click', function() {
  74.             bb.next();
  75.             return false;
  76.         } );
  77.        
  78.         $tourBtnP4.on( 'click', function() {
  79.             bb.next();
  80.             return false;
  81.         } );
  82.        
  83.         $tourBtnP5.on( 'click', function() {
  84.             bb.next();
  85.             return false;
  86.         } );
  87.        
  88.         $tourBtnP6.on( 'click', function() {
  89.             bb.next();
  90.             return false;
  91.         } );
  92.  
  93.         $navPrev.on( 'click', function() {
  94.             bb.prev();
  95.             return false;
  96.         } );
  97.        
  98.         // add swipe events
  99.         $items.on( {
  100.             'swipeleft'     : function( event ) {
  101.                 if( $container.data( 'opened' ) ) {
  102.                     return false;
  103.                 }
  104.                 bb.next();
  105.                 return false;
  106.             },
  107.             'swiperight'    : function( event ) {
  108.                 if( $container.data( 'opened' ) ) {
  109.                     return false;
  110.                 }
  111.                 bb.prev();
  112.                 return false;
  113.             }
  114.         } );
  115.  
  116.         // show table of contents
  117.         $tblcontents.on( 'click', toggleTOC );
  118.  
  119.         // click a menu item
  120.         $menuItems.on( 'click', function() {
  121.  
  122.             var $el = $( this ),
  123.                 idx = $el.index(),
  124.                 jump = function() {
  125.                     bb.jump( idx + 1 );
  126.                 };
  127.            
  128.             current !== idx ? closeTOC( jump ) : closeTOC();
  129.  
  130.             return false;
  131.            
  132.         } );
  133.  
  134.         // reinit jScrollPane on window resize
  135.         $( window ).on( 'debouncedresize', function() {
  136.             // reinitialise jScrollPane on the content div
  137.             setJSP( 'reinit' );
  138.         } );
  139.  
  140.     }
  141.  
  142.     function setJSP( action, idx ) {
  143.        
  144.         var idx = idx === undefined ? current : idx,
  145.             $content = $items.eq( idx ).children( 'div.content' ),
  146.             apiJSP = $content.data( 'jsp' );
  147.        
  148.         if( action === 'init' && apiJSP === undefined ) {
  149.             $content.jScrollPane({verticalGutter : 0, hideFocus : true });
  150.         }
  151.         else if( action === 'reinit' && apiJSP !== undefined ) {
  152.             apiJSP.reinitialise();
  153.         }
  154.         else if( action === 'destroy' && apiJSP !== undefined ) {
  155.             apiJSP.destroy();
  156.         }
  157.  
  158.     }
  159.  
  160.     function updateTOC() {
  161.         $menuItems.removeClass( 'menu-toc-current' ).eq( current ).addClass( 'menu-toc-current' );
  162.     }
  163.  
  164.     function updateNavigation( isLastPage ) {
  165.        
  166.         if( current === 0 ) {
  167.             $navNext.show();
  168.             $navPrev.hide();
  169.         }
  170.         else if( isLastPage ) {
  171.             $navNext.hide();
  172.             $navPrev.show();
  173.         }
  174.         else {
  175.             $navNext.show();
  176.             $navPrev.show();
  177.         }
  178.  
  179.     }
  180.  
  181.     function toggleTOC() {
  182.         var opened = $container.data( 'opened' );
  183.         opened ? closeTOC() : openTOC();
  184.     }
  185.  
  186.     function openTOC() {
  187.         $navNext.hide();
  188.         $navPrev.hide();
  189.         $container.addClass( 'slideRight' ).data( 'opened', true );
  190.     }
  191.  
  192.     function closeTOC( callback ) {
  193.  
  194.         updateNavigation( current === itemsCount - 1 );
  195.         $container.removeClass( 'slideRight' ).data( 'opened', false );
  196.         if( callback ) {
  197.             if( supportTransitions ) {
  198.                 $container.on( transEndEventName, function() {
  199.                     $( this ).off( transEndEventName );
  200.                     callback.call();
  201.                 } );
  202.             }
  203.             else {
  204.                 callback.call();
  205.             }
  206.         }
  207.  
  208.     }
  209.  
  210.     return { init : init };
  211.  
  212. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement