summaryrefslogtreecommitdiff
path: root/static/website/js/cloud.js
blob: 162da739d96673ce7673b05cc91275b81e1467ea (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
$(document).ready(function() {
    var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
        lineNumbers: true,
        lineWrapping: true,
        theme: "default",
        extraKeys: {
           "F11": function(cm) {
            cm.setOption("fullScreen", !cm.getOption("fullScreen"));
           },
           "Esc": function(cm) {
            if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
           }
         }
    });

    var result = CodeMirror.fromTextArea(document.getElementById("result"), {
        lineWrapping: true,
        theme: "default",
        extraKeys: {
           "F11": function(cm) {
            cm.setOption("fullScreen", !cm.getOption("fullScreen"));
           },
           "Esc": function(cm) {
            if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
           }
         }
    });

    /* Code Mirror Controls */
    $fullscreen_code = $("#fullscreen-code");
    $toggle_code = $("#toggle-code");

    $fullscreen_code.click(function(e) {
        editor.setOption("fullScreen", !editor.getOption("fullScreen"));
        editor.focus();
        e.preventDefault();
    });

    $toggle_code.click(function(e) {
        if(editor.getOption("theme") == "monokai") {
            editor.setOption("theme", "default");
        } else{
            editor.setOption("theme", "monokai");
        }
        e.preventDefault();
    });

    $fullscreen_result = $("#fullscreen-result");
    $toggle_result = $("#toggle-result");

    $fullscreen_result.click(function(e) {
        result.setOption("fullScreen", !result.getOption("fullScreen"));
        result.focus();
        e.preventDefault();
    });

    $toggle_result.click(function(e) {
        if(result.getOption("theme") == "monokai") {
            result.setOption("theme", "default");
        } else{
            result.setOption("theme", "monokai");
        }
        e.preventDefault();
    });

    /* 
     * Selectors function 
     * Write the queries using .on()
    */
    $(document).on("change", "#categories", function() {
        $("#books-wrapper").html("");
        $("#chapters-wrapper").html("");
        $("#examples-wrapper").html("");
        $("#contributor").hide();
        ajax_loader(this);
        Dajaxice.website.books(function(data) {
            Dajax.process(data);
            ajax_loader("clear");
        }, {category_id: $(this).val()});
    });

    $(document).on("change", "#books", function() {
        $("#chapters-wrapper").html("");
        $("#examples-wrapper").html("");
        $("#contributor").show();
        $("#download-book").show();
        ajax_loader(this);
        Dajaxice.website.chapters(function(data) { 
            Dajax.process(data);
            ajax_loader("clear");
        }, {book_id: $(this).val()});
    });

    $(document).on("change", "#chapters", function() {
        $("#examples-wrapper").html("");
        $("#download-chapter").show();
        ajax_loader(this);
        Dajaxice.website.examples(function(data) { 
            Dajax.process(data);
            ajax_loader("clear");
        }, {chapter_id: $(this).val()});
    });

    $(document).on("change", "#examples", function() {
        ajax_loader(this);
        $("#download-example").show();
        Dajaxice.website.code(function(data) {
            editor.setValue(data.code);
            ajax_loader("clear");
        }, {example_id: $(this).val()});
    });

    /* Execute the code */
    $plotbox_wrapper  = $("#plotbox-wrapper");
    $plotbox = $("#plotbox");
    $(document).on("click", "#execute", function() {
        $("#execute-inner").html("Executing...");
        Dajaxice.website.execute(function(data) {
            $("#execute-inner").html("Execute");
            result.setValue(data.output);
            if(data.plot_path) {
                $plot = $("<img>");
                $plot.attr({
                    src: data.plot_path,
                    width: 400
                });
                $plotbox.html($plot);
                $plotbox_wrapper.lightbox_me({centered: true});
            }
        }, {
            token: $("[name='csrfmiddlewaretoken']").val(),
            code: editor.getValue(),
            book_id: $("#books").val() || 0,
            chapter_id: $("#chapters").val() || 0,
            example_id: $("#examples").val() || 0
        });
    });

    /* Download book, chapter, example */
    $(document).on("click", "#download-book", function(e) {
        window.location = "http://scilab.in/download/book/" + $("#books").val();
        e.preventDefault();
    });

    $(document).on("click", "#download-chapter", function(e) {
        window.location = "http://scilab.in/download/chapter/" + $("#chapters").val();
        e.preventDefault();
    });

    $(document).on("click", "#download-example", function(e) {
        window.location = "http://scilab.in/download/example/" + $("#examples").val();
        e.preventDefault();
    });

    /* Ajax loader */
    function ajax_loader(key) {
        if(key == "clear") {
            $(".ajax-loader").remove();
        } else {
            $(key).after("<span class='ajax-loader'></span>");
        }
    }

    /* Contributor details */
    $(document).on("click", "#contributor", function(e) {
        Dajaxice.website.contributor(function(data) {
            Dajax.process(data);
            $("#databox-wrapper").lightbox_me({centered: true});
        }, {book_id: $("#books").val()});
        e.preventDefault();
    });
});