Advertisement
Guest User

Untitled

a guest
Oct 19th, 2012
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function (window, undefined) {
  2.  
  3.     var Page = (function () {
  4.  
  5.         var $container = $('#hs-container'),
  6.             // the scroll container that wraps the articles
  7.             $scroller = $container.find('div.hs-content-scroller'),
  8.             $menu = $container.find('aside'),
  9.             // menu links
  10.             $links = $menu.find('nav > a'),
  11.             $articles = $container.find('div.hs-content-wrapper > article'),
  12.             // button to scroll to the top of the page
  13.             // only shown when screen size < 715
  14.             $toTop = $container.find('a.hs-totop-link'),
  15.             // the browser nhistory object
  16.             History = window.History,
  17.             // animation options
  18.             animation = {
  19.                 speed: 800,
  20.                 easing: 'easeInOutExpo'
  21.             },
  22.             // jScrollPane options
  23.             scrollOptions = {
  24.                 verticalGutter: 0,
  25.                 hideFocus: true
  26.             },
  27.             // init function
  28.             init = function () {
  29.  
  30.                 // initialize the jScrollPane on both the menu and articles
  31.                 _initCustomScroll();
  32.                 // initialize some events
  33.                 _initEvents();
  34.                 // sets some css properties
  35.                 _layout();
  36.                 // jumps to the respective chapter
  37.                 // according to the url
  38.                 _goto();
  39.  
  40.             },
  41.             _initCustomScroll = function () {
  42.  
  43.                 // Only add custom scroll to articles if screen size > 715.
  44.                 // If not the articles will be expanded
  45.                 if ($(window).width() > 715) {
  46.  
  47.                     $articles.jScrollPane(scrollOptions);
  48.  
  49.                 }
  50.                 // add custom scroll to menu
  51.                 $menu.children('nav').jScrollPane(scrollOptions);
  52.  
  53.             },
  54.             _goto = function (chapter) {
  55.  
  56.                 // get the url from history state (e.g. chapter=3) and extract the chapter number
  57.  
  58.                 var chapter = chapter || History.getState().hash.replace(/^\/|\/$/g, ''),
  59.                     isHome = (chapter === undefined),
  60.                     // we will jump to the introduction chapter if theres no chapter
  61.  
  62.                     $article = $(chapter.length ? '#' + chapter : '#' + 'introduction');
  63.  
  64.                 if ($article.length) {
  65.  
  66.  
  67.                     // left / top of the element
  68.                     var left = $article.position().left,
  69.                         top = $article.position().top,
  70.                         // check if we are scrolling down or left
  71.                         // is_v will be true when the screen size < 715
  72.                         is_v = ($(document).height() - $(window).height() > 0),
  73.                         // animation parameters:
  74.                         // if vertically scrolling then the body will animate the scrollTop,
  75.                         // otherwise the scroller (div.hs-content-scroller) will animate the scrollLeft
  76.                         param = (is_v) ? {
  77.                             scrollTop: (isHome) ? top : top + $menu.outerHeight(true)
  78.                         } : {
  79.                             scrollLeft: left
  80.                         },
  81.                         $elScroller = (is_v) ? $('html, body') : $scroller;
  82.  
  83.                     $elScroller.stop().animate(param, animation.speed, animation.easing, function () {
  84.  
  85.                         // active class for selected chapter
  86.                         $articles.removeClass('hs-content-active');
  87.                         $article.addClass('hs-content-active');
  88.  
  89.                     });
  90.  
  91.                 }
  92.  
  93.             },
  94.             _saveState = function (chapter) {
  95.  
  96.                 // adds a new state to the history object
  97.                 // this will trigger the statechange on the window
  98.                 if (History.getState().hash.replace(/^\/|\/$/g, '') !== chapter) {
  99.  
  100.                     History.pushState(null, null, chapter);
  101.  
  102.                 }
  103.  
  104.             },
  105.             _layout = function () {
  106.  
  107.                 // control the overflow property of the scroller (div.hs-content-scroller)
  108.                 var windowWidth = $(window).width();
  109.                 switch (true) {
  110.  
  111.                     case (windowWidth <= 715):
  112.                         $scroller.scrollLeft(0).css('overflow', 'visible');
  113.                         break;
  114.                     case (windowWidth <= 1024):
  115.                         $scroller.css('overflow-x', 'scroll');
  116.                         break;
  117.                     case (windowWidth > 1024):
  118.                         $scroller.css('overflow', 'hidden');
  119.                         break;
  120.  
  121.                 };
  122.  
  123.             },
  124.             _initEvents = function () {
  125.  
  126.                 _initWindowEvents();
  127.                 _initMenuEvents();
  128.                 _initArticleEvents();
  129.  
  130.             },
  131.             _initWindowEvents = function () {
  132.  
  133.                 $(window).on({
  134.                     // when resizing the window we need to reinitialize or destroy the jScrollPanes
  135.                     // depending on the screen size
  136.                     'smartresize': function (event) {
  137.  
  138.                         _layout();
  139.  
  140.                         $('article.hs-content').each(function () {
  141.  
  142.                             var $article = $(this),
  143.                                 aJSP = $article.data('jsp');
  144.  
  145.                             if ($(window).width() > 715) {
  146.  
  147.                                 (aJSP === undefined) ? $article.jScrollPane(scrollOptions) : aJSP.reinitialise();
  148.  
  149.                                 _initArticleEvents();
  150.  
  151.                             } else {
  152.  
  153.                                 // destroy article's custom scroll if screen size <= 715px
  154.                                 if (aJSP !== undefined) aJSP.destroy();
  155.  
  156.                                 $container.off('click', 'article.hs-content');
  157.  
  158.                             }
  159.  
  160.                         });
  161.  
  162.                         var nJSP = $menu.children('nav').data('jsp');
  163.                         nJSP.reinitialise();
  164.  
  165.                         // jumps to the current chapter
  166.                         _goto();
  167.  
  168.                     },
  169.                     // triggered when the history state changes - jumps to the respective chapter
  170.                     'statechange': function (event) {
  171.  
  172.                         _goto();
  173.  
  174.                     }
  175.                 });
  176.  
  177.             },
  178.             _initMenuEvents = function () {
  179.  
  180.                 // when we click a menu link we check which chapter the link refers to,
  181.                 // and we save the state on the history obj.
  182.                 // the statechange of the window is then triggered and the page/scroller scrolls to the
  183.                 // respective chapter's position
  184.                 $links.on('click', function (event) {
  185.  
  186.                     var href = $(this).attr('href'),
  187.                         chapter = href.replace('#', '');
  188.  
  189.                     _saveState(chapter);
  190.  
  191.                     return false;
  192.  
  193.                 });
  194.  
  195.                 // scrolls to the top of the page.
  196.                 // this button will only be visible for screen size < 715
  197.                 $toTop.on('click', function (event) {
  198.  
  199.                     $('html, body').stop().animate({
  200.                         scrollTop: 0
  201.                     }, animation.speed, animation.easing);
  202.  
  203.                     return false;
  204.  
  205.                 });
  206.  
  207.             },
  208.             _initArticleEvents = function () {
  209.  
  210.                 // when we click on an article we check which chapter the article refers to,
  211.                 // and we save the state on the history obj.
  212.                 // the statechange of the window is then triggered and the page/scroller scrolls to the
  213.                 // respective chapter's position
  214.                 $container.on('click', 'article.hs-content', function (event) {
  215.  
  216.                     var id = $(this).attr('id'),
  217.                         chapter = href.replace('#', '');
  218.  
  219.                     _saveState(chapter);
  220.  
  221.                     return false;
  222.  
  223.                 });
  224.  
  225.             };
  226.  
  227.         return {
  228.             init: init
  229.         };
  230.  
  231.     })();
  232.  
  233.     Page.init();
  234.  
  235. })(window);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement