diff options
author | Samy Pessé <samypesse@gmail.com> | 2014-04-04 14:10:56 -0700 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2014-04-04 14:10:56 -0700 |
commit | 446d486a0fb725f3df76d9b6e8d2bca02042805b (patch) | |
tree | cfd5405f607fcca196ef5bad6b0b88662c30ccb9 /theme/javascript/core/exercise.js | |
parent | 07101deaefb8848d40231eea12a3bf0710e6a78e (diff) | |
download | gitbook-446d486a0fb725f3df76d9b6e8d2bca02042805b.zip gitbook-446d486a0fb725f3df76d9b6e8d2bca02042805b.tar.gz gitbook-446d486a0fb725f3df76d9b6e8d2bca02042805b.tar.bz2 |
Add base for theming
Diffstat (limited to 'theme/javascript/core/exercise.js')
-rw-r--r-- | theme/javascript/core/exercise.js | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/theme/javascript/core/exercise.js b/theme/javascript/core/exercise.js new file mode 100644 index 0000000..4163730 --- /dev/null +++ b/theme/javascript/core/exercise.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 codeSolution = $exercise.find(".code-solution").text(); + var codeValidation = $exercise.find(".code-validation").text(); + + var editor = ace.edit($exercise.find(".editor").get(0)); + editor.setTheme("ace/theme/tomorrow"); + editor.getSession().setUseWorker(false); + editor.getSession().setMode("ace/mode/javascript"); + + // Submit: test code + $exercise.find(".action-submit").click(function(e) { + e.preventDefault(); + + analytic.track("exercise.submit"); + + 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); + editor.gotoLine(0); + }); + }; + + // 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 |