summaryrefslogtreecommitdiffstats
path: root/theme/javascript/core/navigation.js
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2014-04-14 12:43:45 +0200
committerSamy Pessé <samypesse@gmail.com>2014-04-14 12:43:45 +0200
commit8c02e949fcbeb7b0200a4ffc67a809b66c39038e (patch)
tree6c4cb04ca5f58a7051fc104080285b1ea8fc380e /theme/javascript/core/navigation.js
parent56e1803b3414be5b27905d6480e127989cb73ead (diff)
downloadgitbook-8c02e949fcbeb7b0200a4ffc67a809b66c39038e.zip
gitbook-8c02e949fcbeb7b0200a4ffc67a809b66c39038e.tar.gz
gitbook-8c02e949fcbeb7b0200a4ffc67a809b66c39038e.tar.bz2
Fix exercises binding when page changed
Add base binding for quiz
Diffstat (limited to 'theme/javascript/core/navigation.js')
-rwxr-xr-xtheme/javascript/core/navigation.js76
1 files changed, 48 insertions, 28 deletions
diff --git a/theme/javascript/core/navigation.js b/theme/javascript/core/navigation.js
index 853950d..747bd03 100755
--- a/theme/javascript/core/navigation.js
+++ b/theme/javascript/core/navigation.js
@@ -1,38 +1,23 @@
define([
"jQuery",
- "core/progress"
-], function($, progress) {
+ "core/progress",
+ "core/exercise",
+ "core/quiz"
+], function($, progress, exercises, quiz) {
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) {
+ var updateHistory = function(url, title) {
history.pushState({ path: url }, title, url);
- }
+ };
- function handleNavigation (url, push) {
+ var handleNavigation = function(url, push) {
if (typeof history.pushState === "undefined") {
// Refresh the page to the new URL if pushState not supported
location.href = url;
return
}
+ console.log("load url", url);
return $.get(url)
.done(function (data) {
var $newPage = $(data);
@@ -43,12 +28,21 @@ define([
$('.book-summary').html($newPage.find('.book-summary').html());
if (push) updateHistory(url, null);
- progress.show();
+ preparePage();
})
.fail(function () {
location.href = url;
});
- }
+ };
+
+ var preparePage = function() {
+ // Bind exercises/quiz
+ exercises.init();
+ quiz.init();
+
+ // Show progress
+ progress.show();
+ };
var handlePagination = function (e) {
e.stopPropagation();
@@ -68,11 +62,37 @@ define([
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);
+
+
+ var init = function() {
+ // 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);
+ };
+
+ $(document).on('click', ".navigation-prev", handlePagination);
+ $(document).on('click', ".navigation-next", handlePagination);
+ $(document).on('click', ".summary [data-path] a", handlePagination);
+
+ // Prepare current page
+ preparePage();
+ };
return {
+ init: init,
goNext: goNext,
goPrev: goPrev
};