diff options
author | Aaron O'Mullan <aaron.omullan@gmail.com> | 2014-04-12 01:51:06 -0700 |
---|---|---|
committer | Aaron O'Mullan <aaron.omullan@gmail.com> | 2014-04-12 01:51:06 -0700 |
commit | 78cb9a56ba2cb89842d9940cf3dd81f582c6270a (patch) | |
tree | b73929b036c03ce961feee64e9e1044cdffb12d6 /theme/javascript | |
parent | ab27725b9935b185a58b725e75aedeb579e87d8f (diff) | |
parent | 6173fb76ad3d9ad9357389021aa347df3b1bde92 (diff) | |
download | gitbook-78cb9a56ba2cb89842d9940cf3dd81f582c6270a.zip gitbook-78cb9a56ba2cb89842d9940cf3dd81f582c6270a.tar.gz gitbook-78cb9a56ba2cb89842d9940cf3dd81f582c6270a.tar.bz2 |
Merge pull request #85 from GitbookIO/pr/63
Pr/63
Diffstat (limited to 'theme/javascript')
-rw-r--r-- | theme/javascript/app.js | 6 | ||||
-rw-r--r-- | theme/javascript/core/quiz.js | 52 |
2 files changed, 56 insertions, 2 deletions
diff --git a/theme/javascript/app.js b/theme/javascript/app.js index 392d45b..f18ae24 100644 --- a/theme/javascript/app.js +++ b/theme/javascript/app.js @@ -7,10 +7,11 @@ require([ "core/state", "core/keyboard", "core/exercise", + "core/quiz", "core/progress", "core/sidebar", "core/search" -], function($, storage, analytic, sharing, state, keyboard, exercise, progress, sidebar, search){ +], function($, storage, analytic, sharing, state, keyboard, exercise, quiz, progress, sidebar, search){ $(document).ready(function() { var $book = state.$book; @@ -35,8 +36,9 @@ require([ }); } - // Bind exercise + // Bind exercises exercise.init(); + quiz.init(); // Bind sharing button sharing.init(); diff --git a/theme/javascript/core/quiz.js b/theme/javascript/core/quiz.js new file mode 100644 index 0000000..7043641 --- /dev/null +++ b/theme/javascript/core/quiz.js @@ -0,0 +1,52 @@ +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 + }; +}); |