summaryrefslogtreecommitdiff
path: root/yaksh/static/yaksh/js/lesson.js
blob: 7968c7a0b31b752bb0429aa7b7177f5205cb7870 (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
$(document).ready(function(){
    function getCookie(name) {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }

    var csrftoken = getCookie("csrftoken");
    function csrfSafeMethod(method) {
        // These HTTP methods do not require CSRF protection
        return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
    }
    $.ajaxSetup({
        beforeSend: function(xhr, settings) {
            if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
                xhr.setRequestHeader("X-CSRFToken", csrftoken);
            }
        }
    });
    $("#preview").click(function(){
        var description = $("#id_description").val();
        var preview_url = window.location.protocol + "//" +
                window.location.host + "/exam/manage/courses/lesson/preview/";
        $.ajax({
            url: preview_url,
            timeout: 15000,
            type: 'POST',
            data: JSON.stringify({'description': description}),
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            success: function(msg) {
                preview_text(msg['data']);
            },
            error: function(jqXHR, textStatus) {

            }
        });
    });

    function preview_text(data){
        var preview_div = $("#preview_text_div");
        if (!preview_div.is(":visible")){
            $("#preview_text_div").toggle();
        }
        $("#description_body").empty();
        $("#description_body").append(data);
    }
});