summaryrefslogtreecommitdiff
path: root/yaksh/static/yaksh/js/lesson.js
blob: 38db7d2f2c3de47fa45c8fae5fd6afe00bb20988 (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
$(document).ready(function() {
    var simplemde = new SimpleMDE({
        element: document.getElementById("id_description"),
    });
    const player = new Plyr('#player');
    var timer = $("#vtimer");
    var totalSeconds;
    player.on('timeupdate', event => {
      totalSeconds = parseInt(player.currentTime)
      hours = Math.floor(totalSeconds / 3600);
      totalSeconds %= 3600;
      minutes = Math.floor(totalSeconds / 60);
      seconds = totalSeconds % 60;
      hours = hours < 10 ? "0" + hours : hours;
      minutes = minutes < 10 ? "0" + minutes : minutes;
      seconds = seconds < 10 ? "0" + seconds : seconds;
      timer.val(hours + ":" + minutes + ":" + seconds);
    });
    function csrfSafeMethod(method) {
        // these HTTP methods do not require CSRF protection
        return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
    }

    $("#vtimer").on("change keyup paste", function() {
        player.pause();
        var time = $(this).val().split(":");
        var hh = parseInt(time[0]);
        var mm = parseInt(time[1]);
        var ss = parseInt(time[2]);
        player.currentTime = hh * 3600 + mm * 60 + ss;
    });

    $('#content-type').on('change', function (e) {
        var optionSelected = $("option:selected", this);
        var valueSelected = this.value;
        if (valueSelected == "" || valueSelected == "1") {
            $("#id_type").hide();
            $("#id_type").attr("required", false);
        } else {
            $("#id_type").show();
            $("#id_type").attr("required", true);
        }
    });
    
    // Marker Form
    $("#marker-form").submit(function(e) {
        e.preventDefault();
        lock_screen();
        var csrf = document.getElementById("marker-form").elements[0].value;
        ajax_call($(this).attr("action"), $(this).attr("method"), $(this).serialize(), csrf);
    });

    function ajax_call(url, method, data, csrf) {
        $.ajax({
            url: url,
            timeout: 15000,
            method: method,
            data: data,
            beforeSend: function(xhr, settings) {
              if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
                xhr.setRequestHeader("X-CSRFToken", csrf);
              }
            },
            success: function(msg) {
                unlock_screen();
                if (msg.success) {
                    if (msg.status) $("#lesson-content").html(msg.data);
                    if (msg.content_type === '1') {
                        add_topic();
                    }
                    else {
                        add_question();   
                    }
                }
                if (msg.toc) show_toc(msg.toc);
                if (msg.message) alert(msg.message)
            },
            error: function(xhr, data) {
                switch(xhr.status) {
                    case 400: {
                        unlock_screen();
                        console.log(data.responseJSON);
                        break;
                    }
                    case 500: {
                        unlock_screen();
                        alert('500 status code! server error');
                        break;
                    }
                    case 404: {
                        unlock_screen();
                        alert('404 status code! server error');
                        break;
                    }
                }
            }
        });
    }

    function add_topic() {
        if (!$("#id_timer").val()) {
            $("#id_timer").val($("#vtimer").val());
        }
        $("#topic-form").submit(function(e) {
            e.preventDefault();
            lock_screen();
            var csrf = document.getElementById("topic-form").elements[0].value;
            ajax_call($(this).attr("action"), $(this).attr("method"), $(this).serialize(), csrf);
        });
    }

    function add_question() {
        if (!$("#id_timer").val()) {
            $("#id_timer").val($("#vtimer").val());
        }
        $("#question-form").submit(function(e) {
            e.preventDefault();
            lock_screen();
            var csrf = document.getElementById("question-form").elements[0].value;
            ajax_call($(this).attr("action"), $(this).attr("method"), $(this).serialize(), csrf);
        });
    }

    function lock_screen() {
        document.getElementById("ontop").style.display = "block";
    }

    function unlock_screen() {
        document.getElementById("ontop").style.display = "none";
    }

    function show_error() {

    }

    function show_toc(toc) {
        $("#lesson-content").empty();
        $("#toc").html(toc);
    }

    $('#id_video_file').on('change',function(){
        //get the file name
        var files = [];
        for (var i = 0; i < $(this)[0].files.length; i++) {
            files.push($(this)[0].files[i].name);
        }
        $(this).next('.custom-file-label').html(files.join(', '));
    });

    $('#id_Lesson_files').on('change',function(){
        //get the file name
        var files = [];
        for (var i = 0; i < $(this)[0].files.length; i++) {
            files.push($(this)[0].files[i].name);
        }
        $(this).next('.custom-file-label').html(files.join(', '));
    });
});