aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/bootstrap/files.js
blob: ae47af4415713f9b10c95d1ce8b4f56c1cef79aa (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
/*jslint esversion: 6 */
function searchMoreFiles (button) {
    var table = $(button).closest('table');
    var loading = $('<div class="loading" style="padding: 10px">').html(
        $('<img>')
            .attr('src', STUDIP.ASSETS_URL + 'images/ajax-indicator-black.svg')
            .css('width', '24')
            .css('height', '24')
    );

    $(button).replaceWith(loading);

    $.get(button.href).done((output) => {
        table.find('tbody').append($('tbody tr', output));
        table.find('tfoot').replaceWith($('tfoot', output));
    });

    return false;
}

STUDIP.domReady(() => {

    STUDIP.Files.init();

    $('form.drag-and-drop.files').on('dragover dragleave', (event) => {
        $(event.target).toggleClass('hovered', event.type === 'dragover');

        event.preventDefault();
    }).on('drop', (event) => {
        var filelist = event.originalEvent.dataTransfer.files || {};
        STUDIP.Files.upload(filelist);

        event.preventDefault();
    }).on('click', function() {
        $('.file_selector input[type=file]').first().click();
    });

    $('table.documents.flat.filter').each(function () {
        var ignored = [];
        $('colgroup col', this).each((index, col) => {
            if ($(col).is('[data-filter-ignore]')) {
                ignored.push(index);
            }
        });
        $(this).filterTable({
            highlightClass: 'filter-match',
            ignoreColumns: ignored,
            inputSelector: '.sidebar .tablesorterfilter',
            minChars: 1,
            minRows: 1
        });
    });

    $(document).trigger('refresh-handlers');

    $(document).on(
        'click',
        '#file_license_chooser_1 > input[type=radio]',
        STUDIP.Files.updateTermsOfUseDescription
    );

    $(document).on('click', '.files-search-more', (event) => {
        searchMoreFiles(event.target);

        event.preventDefault();
    });
});


jQuery(document).on('ajaxComplete', (event, xhr) => {
    if (!xhr.getResponseHeader('X-Filesystem-Changes')) {
        return;
    }

    var changes = JSON.parse(xhr.getResponseHeader('X-Filesystem-Changes'));
    var payload = false;

    function process(key, handler) {
        if (!changes.hasOwnProperty(key)) {
            return;
        }

        var values = changes[key];
        if (values === null && xhr.getResponseHeader('Content-Type').match(/json/)) {
            try {
                if (payload === false) {
                    payload = JSON.parse(xhr.responseText);
                }
                if (payload.hasOwnProperty(key)) {
                    values = payload[key];
                }
            } catch (e) {
            }
        }

        handler(values);
    }

    process('added_files', STUDIP.Files.addFileDisplay);
    process('added_folders', STUDIP.Files.addFolderDisplay);
    process('removed_files', STUDIP.Files.removeFileDisplay);
    process('redirect', STUDIP.Dialog.fromURL);
    process('message', (html) => {
        $('.file_upload_window .uploadbar').hide().parent().append(html);
    });
    process('close_dialog', STUDIP.Dialog.close);

});