diff options
author | Samy Pessé <samypesse@gmail.com> | 2014-04-14 00:06:58 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2014-04-14 00:06:58 +0200 |
commit | 9ffa462111b1d5b4c954464416fdc8daf69c9d3f (patch) | |
tree | b38e046662d4ae31d5b611166264e4d68500736a /theme/javascript/core/navigation.js | |
parent | 1bbef540157b513d3e336325a3d636885e15357e (diff) | |
download | gitbook-9ffa462111b1d5b4c954464416fdc8daf69c9d3f.zip gitbook-9ffa462111b1d5b4c954464416fdc8daf69c9d3f.tar.gz gitbook-9ffa462111b1d5b4c954464416fdc8daf69c9d3f.tar.bz2 |
Add base from clarity theme from @Nijikokun
Diffstat (limited to 'theme/javascript/core/navigation.js')
-rwxr-xr-x[-rw-r--r--] | theme/javascript/core/navigation.js | 66 |
1 files changed, 60 insertions, 6 deletions
diff --git a/theme/javascript/core/navigation.js b/theme/javascript/core/navigation.js index bb3547c..c4a7971 100644..100755 --- a/theme/javascript/core/navigation.js +++ b/theme/javascript/core/navigation.js @@ -1,15 +1,69 @@ define([ - "jQuery" -], function($) { + "jQuery", + "core/progress" +], function($, progress) { + var prev, next; + + // Prevent cache so that using the back button works + // See: http://stackoverflow.com/a/15805399/983070 + $.ajaxSetup({ + cache: false + }); + + // Recreate first page when the page loads. + history.replaceState({ path: window.location.href }, ''); + + // Back Button Hijacking :( + window.onpopstate = function (event) { + if (event.state === null) { + return; + } + + return handleNavigation(event.state.path, false); + }; + + function updateHistory (url, title) { + history.pushState({ path: url }, title, url); + } + + function handleNavigation (url, push) { + if (typeof history.pushState === "undefined") { + // Refresh the page to the new URL if pushState not supported + location.href = url; + } + + return $.get(url).done(function (data) { + $('.book-body').html($(data).find('.book-body').html()); + $('.book-summary').html($(data).find('.book-summary').html()); + if (push) updateHistory(url, null); + progress.show(); + }).fail(function () { + location.href = url; + }); + } + + var handlePagination = function (e) { + e.stopPropagation(); + e.preventDefault(); + + var url = $(this).attr('href'); + if (url) handleNavigation(url, true); + }; + var goNext = function() { - var url = $("link[rel='next']").attr("href"); - if (url) location.href = url; + var url = $(".navigation-next").attr("href"); + if (url) handleNavigation(url, true); }; + var goPrev = function() { - var url = $("link[rel='prev']").attr("href"); - if (url) location.href = url; + var url = $(".navigation-prev").attr("href"); + if (url) handleNavigation(url, true); }; + $(document).on('click', ".navigation-prev", handlePagination); + $(document).on('click', ".navigation-next", handlePagination); + $(document).on('click', ".summary [data-path] a", handlePagination); + return { goNext: goNext, goPrev: goPrev |