diff options
Diffstat (limited to 'theme/javascript')
-rw-r--r-- | theme/javascript/app.js | 6 | ||||
-rw-r--r-- | theme/javascript/core/quiz.js | 50 |
2 files changed, 54 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..2e900e8 --- /dev/null +++ b/theme/javascript/core/quiz.js @@ -0,0 +1,50 @@ +define([ + "jQuery", + "utils/execute", + "utils/analytic", + "core/state" +], function($, execute, analytic, state){ + // Bind an exercise + var prepareExercise = function($exercise) { + var $answers = $exercise.find(".quiz-answers").find("input[type=radio], input[type=checkbox]"); + + $answers.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").removeClass("alert-danger"); + $exercise.find(".alert-success,.alert-danger").addClass("hidden"); + + var result = true; + $exercise.find(".quiz input[type=radio],.quiz 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").addClass("alert-danger"); + } + }); + + $exercise.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 + }; +}); |