summaryrefslogtreecommitdiffstats
path: root/theme/javascript
diff options
context:
space:
mode:
Diffstat (limited to 'theme/javascript')
-rwxr-xr-x[-rw-r--r--]theme/javascript/app.js35
-rwxr-xr-x[-rw-r--r--]theme/javascript/core/exercise.js2
-rwxr-xr-x[-rw-r--r--]theme/javascript/core/keyboard.js0
-rwxr-xr-x[-rw-r--r--]theme/javascript/core/navigation.js133
-rwxr-xr-x[-rw-r--r--]theme/javascript/core/progress.js29
-rw-r--r--theme/javascript/core/quiz.js27
-rwxr-xr-x[-rw-r--r--]theme/javascript/core/search.js2
-rwxr-xr-x[-rw-r--r--]theme/javascript/core/sidebar.js2
-rwxr-xr-x[-rw-r--r--]theme/javascript/core/state.js10
-rwxr-xr-x[-rw-r--r--]theme/javascript/execute/javascript.js0
-rwxr-xr-x[-rw-r--r--]theme/javascript/utils/execute.js0
-rwxr-xr-x[-rw-r--r--]theme/javascript/utils/platform.js0
-rwxr-xr-x[-rw-r--r--]theme/javascript/utils/sharing.js2
-rwxr-xr-x[-rw-r--r--]theme/javascript/utils/storage.js0
-rwxr-xr-x[-rw-r--r--]theme/javascript/vendors/jquery.js0
-rwxr-xr-x[-rw-r--r--]theme/javascript/vendors/lodash.js0
-rwxr-xr-x[-rw-r--r--]theme/javascript/vendors/mixpanel.js0
-rwxr-xr-x[-rw-r--r--]theme/javascript/vendors/mousetrap.js0
-rwxr-xr-x[-rw-r--r--]theme/javascript/vendors/require.js0
19 files changed, 178 insertions, 64 deletions
diff --git a/theme/javascript/app.js b/theme/javascript/app.js
index f18ae24..38a9572 100644..100755
--- a/theme/javascript/app.js
+++ b/theme/javascript/app.js
@@ -6,15 +6,19 @@ require([
"core/state",
"core/keyboard",
- "core/exercise",
- "core/quiz",
+ "core/navigation",
"core/progress",
"core/sidebar",
"core/search"
-], function($, storage, analytic, sharing, state, keyboard, exercise, quiz, progress, sidebar, search){
+], function($, storage, analytic, sharing, state, keyboard, navigation, progress, sidebar, search){
$(document).ready(function() {
var $book = state.$book;
+ if (state.githubId) {
+ // Initialize storage
+ storage.setBaseKey(state.githubId);
+ }
+
// Init sidebar
sidebar.init();
@@ -24,29 +28,10 @@ require([
// Init keyboard
keyboard.init();
- if (state.githubId) {
- // Initialize storage
- storage.setBaseKey(state.githubId);
-
- // Star and watch count
- $.getJSON("https://api.github.com/repos/"+state.githubId)
- .done(function(repo) {
- $book.find(".count-star span").text(repo.stargazers_count);
- $book.find(".count-watch span").text(repo.subscribers_count);
- });
- }
-
- // Bind exercises
- exercise.init();
- quiz.init();
-
// Bind sharing button
sharing.init();
- // Show progress
- progress.show();
-
- // Focus on content
- $(".book-body").focus();
+ // Init navigation
+ navigation.init();
});
-});
+}); \ No newline at end of file
diff --git a/theme/javascript/core/exercise.js b/theme/javascript/core/exercise.js
index 5219546..e24ae0a 100644..100755
--- a/theme/javascript/core/exercise.js
+++ b/theme/javascript/core/exercise.js
@@ -19,7 +19,7 @@ define([
$exercise.find(".action-submit").click(function(e) {
e.preventDefault();
- analytic.track("exercise.submit");
+ analytic.track("exercise.submit", {type: "code"});
execute("javascript", editor.getValue(), codeValidation, codeContext, function(err, result) {
$exercise.toggleClass("return-error", err != null);
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..2a08dab 100644..100755
--- a/theme/javascript/core/navigation.js
+++ b/theme/javascript/core/navigation.js
@@ -1,16 +1,137 @@
define([
- "jQuery"
-], function($) {
+ "jQuery",
+ "core/state",
+ "core/progress",
+ "core/exercise",
+ "core/quiz"
+], function($, state, progress, exercises, quiz) {
+ var prev, next;
+ var githubCountStars, githubCountWatch;
+
+ var updateHistory = function(url, title) {
+ history.pushState({ path: url }, title, url);
+ };
+
+ 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
+ }
+
+ return $.get(url)
+ .done(function (html) {
+ html = html.replace( /<(\/?)(html|head|body)([^>]*)>/ig, function(a,b,c,d){
+ return '<' + b + 'div' + ( b ? '' : ' data-element="' + c + '"' ) + d + '>';
+ });
+
+ var $page = $(html);
+ var $pageHead = $page.find("[data-element=head]");
+
+ // Merge heads
+ var headContent = $pageHead.html()
+
+ $("head style").each(function() {
+ headContent = headContent + this.outerHTML
+ });
+ $("head").html(headContent);
+
+ // Update header, body and summary
+ $('.book-header').html($page.find('.book-header').html());
+ $('.book-body').html($page.find('.book-body').html());
+ $('.book-summary').html($page.find('.book-summary').html());
+
+ if (push) updateHistory(url, null);
+ preparePage();
+ })
+ .fail(function () {
+ location.href = url;
+ });
+ };
+
+ var updateGitHubCounts = function() {
+ $(".book-header .count-star span").text(githubCountStars);
+ $(".book-header .count-watch span").text(githubCountWatch);
+ }
+
+ var preparePage = function() {
+ // Bind exercises/quiz
+ exercises.init();
+ quiz.init();
+
+ // Show progress
+ progress.show();
+
+ // Reset scroll
+ $(".book-body").scrollTop(0);
+
+ // Focus on content
+ $(".book-body").focus();
+
+ // Update GitHub count
+ if (state.githubId) {
+ if (githubCountStars) {
+ updateGitHubCounts();
+ } else {
+ $.getJSON("https://api.github.com/repos/"+state.githubId)
+ .done(function(repo) {
+ githubCountStars = repo.stargazers_count;
+ githubCountWatch = repo.subscribers_count;
+ updateGitHubCounts();
+ });
+ }
+ }
+ };
+
+ 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);
+ };
+
+
+
+ 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
};
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
index 7043641..bc1a16d 100644
--- a/theme/javascript/core/quiz.js
+++ b/theme/javascript/core/quiz.js
@@ -5,22 +5,22 @@ define([
"core/state"
], function($, execute, analytic, state){
// Bind an exercise
- var prepareExercise = function($exercise) {
+ var prepareQuiz = function($quiz) {
- $exercise.find(".quiz-answers input").click(function(e) {
+ $quiz.find(".quiz-answers input").click(function(e) {
e.preventDefault();
});
// Submit: test code
- $exercise.find(".action-submit").click(function(e) {
+ $quiz.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");
+ analytic.track("exercise.submit", {type: "quiz"});
+ $quiz.find("tr.alert-danger,li.alert-danger").removeClass("alert-danger");
+ $quiz.find(".alert-success,.alert-danger").addClass("hidden");
- $exercise.find(".quiz").each(function(q) {
+ $quiz.find(".question").each(function(q) {
var result = true;
- var $answers = $exercise.find(".quiz-answers").slice(q).find("input[type=radio], input[type=checkbox]");
+ var $answers = $quiz.find(".question-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;
@@ -33,20 +33,21 @@ define([
});
- $exercise.find(".action-solution").click(function(e) {
- $exercise.find(".quiz, .quiz-answers").toggleClass("hidden");
+ $quiz.find(".action-solution").click(function(e) {
+ e.preventDefault();
+ $quiz.find(".question-content, .question-answers").toggleClass("hidden");
});
};
// Prepare all exercise
var init = function() {
state.$book.find("section.quiz").each(function() {
- prepareExercise($(this));
+ prepareQuiz($(this));
});
};
return {
init: init,
- prepare: prepareExercise
+ prepare: prepareQuiz
};
-});
+}); \ No newline at end of file
diff --git a/theme/javascript/core/search.js b/theme/javascript/core/search.js
index 4171660..d8ddc2e 100644..100755
--- a/theme/javascript/core/search.js
+++ b/theme/javascript/core/search.js
@@ -64,7 +64,7 @@ define([
loadIndex();
// Toggle search
- state.$book.find(".book-header .toggle-search").click(function(e) {
+ $(document).on("click", ".book-header .toggle-search", function(e) {
e.preventDefault();
toggleSearch();
});
diff --git a/theme/javascript/core/sidebar.js b/theme/javascript/core/sidebar.js
index d318676..0d09b58 100644..100755
--- a/theme/javascript/core/sidebar.js
+++ b/theme/javascript/core/sidebar.js
@@ -25,7 +25,7 @@ define([
// Prepare sidebar: state and toggle button
var init = function() {
// Toggle summary
- state.$book.find(".book-header .toggle-summary").click(function(e) {
+ $(document).on("click", ".book-header .toggle-summary", function(e) {
e.preventDefault();
toggleSidebar();
});
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
diff --git a/theme/javascript/execute/javascript.js b/theme/javascript/execute/javascript.js
index ddbc2c1..ddbc2c1 100644..100755
--- a/theme/javascript/execute/javascript.js
+++ b/theme/javascript/execute/javascript.js
diff --git a/theme/javascript/utils/execute.js b/theme/javascript/utils/execute.js
index eb2a19b..eb2a19b 100644..100755
--- a/theme/javascript/utils/execute.js
+++ b/theme/javascript/utils/execute.js
diff --git a/theme/javascript/utils/platform.js b/theme/javascript/utils/platform.js
index ad5f3b4..ad5f3b4 100644..100755
--- a/theme/javascript/utils/platform.js
+++ b/theme/javascript/utils/platform.js
diff --git a/theme/javascript/utils/sharing.js b/theme/javascript/utils/sharing.js
index f4354f1..caf97fb 100644..100755
--- a/theme/javascript/utils/sharing.js
+++ b/theme/javascript/utils/sharing.js
@@ -19,7 +19,7 @@ define([
// Bind all sharing button
var init = function() {
- $("a[data-sharing]").click(function(e) {
+ $(document).on("click", "a[data-sharing]", function(e) {
if (e) e.preventDefault();
var type = $(this).data("sharing");
diff --git a/theme/javascript/utils/storage.js b/theme/javascript/utils/storage.js
index 57f5878..57f5878 100644..100755
--- a/theme/javascript/utils/storage.js
+++ b/theme/javascript/utils/storage.js
diff --git a/theme/javascript/vendors/jquery.js b/theme/javascript/vendors/jquery.js
index f7f4227..f7f4227 100644..100755
--- a/theme/javascript/vendors/jquery.js
+++ b/theme/javascript/vendors/jquery.js
diff --git a/theme/javascript/vendors/lodash.js b/theme/javascript/vendors/lodash.js
index d653e5a..d653e5a 100644..100755
--- a/theme/javascript/vendors/lodash.js
+++ b/theme/javascript/vendors/lodash.js
diff --git a/theme/javascript/vendors/mixpanel.js b/theme/javascript/vendors/mixpanel.js
index 763a573..763a573 100644..100755
--- a/theme/javascript/vendors/mixpanel.js
+++ b/theme/javascript/vendors/mixpanel.js
diff --git a/theme/javascript/vendors/mousetrap.js b/theme/javascript/vendors/mousetrap.js
index 66f2d5b..66f2d5b 100644..100755
--- a/theme/javascript/vendors/mousetrap.js
+++ b/theme/javascript/vendors/mousetrap.js
diff --git a/theme/javascript/vendors/require.js b/theme/javascript/vendors/require.js
index d95c608..d95c608 100644..100755
--- a/theme/javascript/vendors/require.js
+++ b/theme/javascript/vendors/require.js