summaryrefslogtreecommitdiff
path: root/public/site.js
blob: c9c04916bf349333180a727260fb1445a2e05ec8 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
$(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(null, 640);

    $("#dlCodeButton").on("click", function() {
        var a = $("<a></a>")
            .attr("href", "data:text/x-java;charset=UTF-8," + encodeURIComponent(editor.getValue()))
            .attr("download", "Program.java")
            .css("display", "none")
            .appendTo("body");
        a.get(0).click();
        a.remove();
    });

    $("#permalink").on("click", function() {
        $("#loader").show();
        $.ajax({
            type: "POST",
            url: location.href,
            data: {
                code: editor.getValue(),
                csrf: csrf,
                permalink: "1"
            },
            dataType: "json",
            success: function(data) {
                $("#loader").hide();

                if (!data.ok) {
                    alert("Error: " + data.message);
                    if (data.csrf)
                        csrf = data.csrf;
                    return;
                }

                csrf = data.csrf;

                location.href = "?c=" + encodeURIComponent(data.data);
            }
        });
    });

    $("#toggleFullWidth").on("click", function(e) {
        e.preventDefault();
        $("body").toggleClass("full-width");
    });

    $("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);
            }
        })
    });
});