aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/bootstrap/subcourses.js
blob: 2a8326ef25634938ac90e9dfbb24095fa79dffd4 (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
// Open action menu on click on the icon
$(document).on('click', '.toggle-subcourses', function(event) {
    var row = $(this).closest('tr');

    if ($(this).hasClass('open')) {
        $(this).removeClass('open');
        $(this)
            .children('.icon-shape-remove')
            .addClass('hidden-js');
        $(this)
            .children('.icon-shape-add')
            .removeClass('hidden-js');
        $(
            'tr.subcourse-' +
                $(this)
                    .closest('tr')
                    .data('course-id')
        ).addClass('hidden-js');
        row.removeClass('has-subcourses');
    } else if ($(this).hasClass('loaded')) {
        $(this).addClass('open');
        $(this)
            .children('.icon-shape-add')
            .addClass('hidden-js');
        $(this)
            .children('.icon-shape-remove')
            .removeClass('hidden-js');
        $(
            'tr.subcourse-' +
                $(this)
                    .closest('tr')
                    .data('course-id')
        ).removeClass('hidden-js');
        row.addClass('has-subcourses');
    } else {
        $.ajax($(this).data('get-subcourses-url'), {
            beforeSend() {
                $('<div class="loading" style="padding: 10px">')
                    .html(
                        $('<img>')
                            .attr('src', STUDIP.ASSETS_URL + 'images/loading-indicator.svg')
                            .css('width', '24')
                            .css('height', '24')
                    )
                    .insertAfter(row);
            },
            success(data) {
                $(row)
                    .siblings('div.loading')
                    .remove();
                $(data).insertAfter(row);
            },
            error(jqXHR, textStatus, errorThrown) {
                alert('Status: ' + textStatus + '\nError: ' + errorThrown);
            }
        });
        $(this)
            .addClass('loaded')
            .addClass('open');
        $(this)
            .children('.icon-shape-add')
            .addClass('hidden-js');
        $(this)
            .children('.icon-shape-remove')
            .removeClass('hidden-js');
        row.addClass('has-subcourses');
    }

    // Stop event so the following close event will not be fired
    event.stopPropagation();

    return false;
});