summaryrefslogtreecommitdiffstats
path: root/theme/javascript/core
diff options
context:
space:
mode:
Diffstat (limited to 'theme/javascript/core')
-rwxr-xr-x[-rw-r--r--]theme/javascript/core/exercise.js0
-rwxr-xr-x[-rw-r--r--]theme/javascript/core/keyboard.js0
-rwxr-xr-x[-rw-r--r--]theme/javascript/core/navigation.js66
-rwxr-xr-x[-rw-r--r--]theme/javascript/core/progress.js29
-rw-r--r--theme/javascript/core/quiz.js52
-rwxr-xr-x[-rw-r--r--]theme/javascript/core/search.js0
-rwxr-xr-x[-rw-r--r--]theme/javascript/core/sidebar.js0
-rwxr-xr-x[-rw-r--r--]theme/javascript/core/state.js10
8 files changed, 83 insertions, 74 deletions
diff --git a/theme/javascript/core/exercise.js b/theme/javascript/core/exercise.js
index 5219546..5219546 100644..100755
--- a/theme/javascript/core/exercise.js
+++ b/theme/javascript/core/exercise.js
diff --git a/theme/javascript/core/keyboard.js b/theme/javascript/core/keyboard.js
index 5bbdcf2..5bbdcf2 100644..100755
--- a/theme/javascript/core/keyboard.js
+++ b/theme/javascript/core/keyboard.js
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
diff --git a/theme/javascript/core/progress.js b/theme/javascript/core/progress.js
index d074cae..a409a11 100644..100755
--- a/theme/javascript/core/progress.js
+++ b/theme/javascript/core/progress.js
@@ -10,15 +10,16 @@ define([
};
// Return all levels
- var getLevels = function() {
+ var getLevels = function () {
var levels = $(".book-summary li[data-level]");
+
return _.map(levels, function(level) {
return $(level).data("level").toString();
});
};
// Return a map chapter -> number (timestamp)
- var getProgress = function() {
+ var getProgress = function () {
// Current level
var progress = storage.get("progress", {});
@@ -33,27 +34,33 @@ define([
};
// Change value of progress for a level
- var markProgress = function(level, state) {
- if (state == null) state = true;
-
+ var markProgress = function (level, state) {
var progress = getProgress();
- progress[level] = state? Date.now() : 0;
+
+ if (state == null) {
+ state = true;
+ }
+
+ progress[level] = state
+ ? Date.now()
+ : 0;
storage.set("progress", progress);
};
// Show progress
- var showProgress = function() {
- // Update progress
+ var showProgress = function () {
var progress = getProgress();
var $summary = $(".book-summary");
- _.each(progress, function(value, level) {
+ _.each(progress, function (value, level) {
$summary.find("li[data-level='"+level+"']").toggleClass("done", value > 0);
});
- // Mark current progress
- markProgress(getCurrentLevel(), true);
+ // Mark current progress if we have not already
+ if (!progress[getCurrentLevel()]) {
+ markProgress(getCurrentLevel(), true);
+ }
};
return {
diff --git a/theme/javascript/core/quiz.js b/theme/javascript/core/quiz.js
deleted file mode 100644
index 7043641..0000000
--- a/theme/javascript/core/quiz.js
+++ /dev/null
@@ -1,52 +0,0 @@
-define([
- "jQuery",
- "utils/execute",
- "utils/analytic",
- "core/state"
-], function($, execute, analytic, state){
- // Bind an exercise
- var prepareExercise = function($exercise) {
-
- $exercise.find(".quiz-answers input").click(function(e) {
- e.preventDefault();
- });
-
- // Submit: test code
- $exercise.find(".action-submit").click(function(e) {
- e.preventDefault();
- analytic.track("exercise.submit");
- $exercise.find("tr.alert-danger,li.alert-danger").removeClass("alert-danger");
- $exercise.find(".alert-success,.alert-danger").addClass("hidden");
-
- $exercise.find(".quiz").each(function(q) {
- var result = true;
- var $answers = $exercise.find(".quiz-answers").slice(q).find("input[type=radio], input[type=checkbox]");
- $(this).find("input[type=radio],input[type=checkbox]").each(function(i) {
- var correct = $(this).is(":checked") === $answers.slice(i).first().is(":checked");
- result = result && correct;
- if (!correct) {
- $(this).closest("tr, li").addClass("alert-danger");
- }
- });
- $(this).find(result ? "div.alert-success" : "div.alert-danger").toggleClass("hidden");
- });
-
- });
-
- $exercise.find(".action-solution").click(function(e) {
- $exercise.find(".quiz, .quiz-answers").toggleClass("hidden");
- });
- };
-
- // Prepare all exercise
- var init = function() {
- state.$book.find("section.quiz").each(function() {
- prepareExercise($(this));
- });
- };
-
- return {
- init: init,
- prepare: prepareExercise
- };
-});
diff --git a/theme/javascript/core/search.js b/theme/javascript/core/search.js
index 4171660..4171660 100644..100755
--- a/theme/javascript/core/search.js
+++ b/theme/javascript/core/search.js
diff --git a/theme/javascript/core/sidebar.js b/theme/javascript/core/sidebar.js
index d318676..d318676 100644..100755
--- a/theme/javascript/core/sidebar.js
+++ b/theme/javascript/core/sidebar.js
diff --git a/theme/javascript/core/state.js b/theme/javascript/core/state.js
index 79876b9..34851c1 100644..100755
--- a/theme/javascript/core/state.js
+++ b/theme/javascript/core/state.js
@@ -4,11 +4,11 @@ define([
var $book = $(".book");
return {
- '$book': $book,
+ '$book': $book,
- 'githubId': $book.data("github"),
- 'level': $book.data("level"),
- 'basePath': $book.data("basepath"),
- 'revision': $book.data("revision")
+ 'githubId': $book.data("github"),
+ 'level': $book.data("level"),
+ 'basePath': $book.data("basepath"),
+ 'revision': $book.data("revision")
};
}); \ No newline at end of file