diff options
Diffstat (limited to 'assets/javascript/core')
-rw-r--r-- | assets/javascript/core/exercise.js | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/assets/javascript/core/exercise.js b/assets/javascript/core/exercise.js new file mode 100644 index 0000000..153f544 --- /dev/null +++ b/assets/javascript/core/exercise.js @@ -0,0 +1,45 @@ +define([ + "jQuery", + "utils/execute", + "core/state" +], function($, execute, state){ + // Bind an exercise + var prepareExercise = function($exercise) { + var codeSolution = $exercise.find(".code-solution").html(); + var codeValidation = $exercise.find(".code-validation").html(); + + var editor = ace.edit($exercise.find(".editor").get(0)); + editor.setTheme("ace/theme/tomorrow"); + editor.getSession().setMode("ace/mode/javascript"); + + // Submit: test code + $exercise.find(".action-submit").click(function(e) { + e.preventDefault(); + + execute(editor.getValue(), codeValidation, function(err, result) { + $exercise.toggleClass("return-error", err != null); + $exercise.toggleClass("return-success", err == null); + if (err) $exercise.find(".alert-danger").text(err.message || err); + }); + }); + + // Set solution + $exercise.find(".action-solution").click(function(e) { + e.preventDefault(); + + editor.setValue(codeSolution); + }); + }; + + // Prepare all exercise + var init = function() { + state().$book.find("section.exercise").each(function() { + prepareExercise($(this)); + }); + }; + + return { + init: init, + prepare: prepareExercise + }; +});
\ No newline at end of file |