summaryrefslogtreecommitdiff
path: root/public/site.js
blob: 21d51a722a589019b46f676d46a2342e5dd3c31c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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);
            }
        })
    });
});