diff options
Diffstat (limited to 'public/site.js')
-rw-r--r-- | public/site.js | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/public/site.js b/public/site.js new file mode 100644 index 0000000..21d51a7 --- /dev/null +++ b/public/site.js @@ -0,0 +1,80 @@ +$(function() { + var csrf = $("#csrf-token").val(); + var editor = CodeMirror.fromTextArea($("#editor")[0], { + lineNumbers: true, + mode: "text/x-java", + theme: "eclipse", + tabSize: 4, + indentWithTabs: false, + smartIndent: true, + indentUnit: 4 + }); + editor.setSize(898, 500); + + $("form").on("submit", function(e) { + e.preventDefault(); + + $("#loader").show(); + $("#output").empty().hide(); + + $.ajax({ + type: "POST", + url: location.href, + data: { + code: editor.getValue(), + csrf: csrf + }, + dataType: "json", + success: function(data) { + $("#loader").hide(); + + if (!data.ok) { + alert("Error: " + data.message); + if (data.csrf) + csrf = data.csrf; + return; + } + + csrf = data.csrf; + + if (data.compile.status == 0) { + if (data.run.status != null) { + // program was executed + $("#output").append( + $("<h4></h4>").text(data.run.status == 0 ? "Executed successfully" : "Execution failed").css("color", data.run.status == 0 ? "green" : "red"), + ); + + if (data.run.stdout) { + $("#output").append( + $("<p></p>").text("Program output:"), + $("<pre></pre>").append( + $("<code></code>").text(data.run.stdout) + ) + ); + } + + if (data.run.stderr) { + $("#output").append( + $("<p></p>").text("Error output:"), + $("<pre></pre>").append( + $("<code></code>").text(data.run.stderr) + ) + ); + } + } + } else { + // compilation failed, stderr will contain compiler errors + $("#output").append( + $("<h4></h4>").text("Compilation failed").css("color", "red"), + $("<p></p>").text("Compiler errors:"), + $("<pre></pre>").append( + $("<code></code>").text(data.compile.stderr) + ) + ); + } + + $("#output").slideDown(400); + } + }) + }); +}); |