aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/bootstrap/wiki.js
blob: 25a277b2602f618eb72721dfcd38a2a9eeb5dbbd (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
/**
 * This file contains all wiki related javascript.
 *
 * For now this is the "submit and edit" functionality via ajax.
 *
 * @author    Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @copyright Stud.IP Core Group
 * @license   GPL2 or any later version
 * @since     Stud.IP 3.3
 */

$(document).on('click', '#wiki button[name="submit-and-edit"]', function(event) {
    var form = $(this).closest('form'),
        data = {},
        form_data,
        i,
        wysiwyg_editor = false;

    const textarea = $('textarea[name="body"]', form).get(0);
    if (textarea) {
        wysiwyg_editor = STUDIP.wysiwyg.getEditor(textarea);
        wysiwyg_editor.sourceElement.value = STUDIP.wysiwyg.markAsHtml(wysiwyg_editor.getData());
    }

    form_data = form.serializeArray();

    // Show ajax overlay to indicate activity (and prevent buttons to be
    // clicked again)
    STUDIP.Overlay.show(true, form.css('position', 'relative'));

    // Include this button into form's data
    form_data.push({
        name: $(this).attr('name'),
        value: true
    });

    // Transform data into an easier accessible format
    for (i = 0; i < form_data.length; i += 1) {
        data[form_data[i].name] = form_data[i].value;
    }

    // Check version
    $.getJSON(
        STUDIP.URLHelper.getURL('dispatch.php/wiki/version_check/' + data.version, {
            keyword: data.wiki
        })
    )
        .then(function(response, status, jqxhr) {
            var error = jqxhr.getResponseHeader('X-Studip-Error'),
                to_confirm = jqxhr.getResponseHeader('X-Studip-Confirm'),
                confirmed = false;
            // Unrecoverable error
            if (response === false) {
                window.alert(error);
                return;
            }
            // Saving needs confirmation (newer version available?)
            if (response === null) {
                confirmed = window.confirm(error + '\n\n' + to_confirm);
            } else {
                confirmed = true;
            }
            // Ready to save
            if (confirmed) {
                $.ajax({
                    type: (form.attr('method') || 'GET').toUpperCase(),
                    url: STUDIP.URLHelper.getURL('dispatch.php/wiki/store/' + data.version),
                    data: {
                        keyword: data.wiki,
                        body: data.body
                    },
                    dataType: 'json'
                }).then(function(response) {
                    var textarea = $('textarea[name=body]', form);

                    // Update header info containing version and author
                    $(form)
                        .closest('table')
                        .prev('table')
                        .find('td:last-child')
                        .html(response.zusatz);

                    // Update version field
                    $('input[type=hidden][name=version]', form).val(response.version);

                    if (wysiwyg_editor) {
                        wysiwyg_editor.setData(response.body);
                    } else {
                        // Store current selection/caret position
                        textarea.storeSelection();

                        // Update textarea, restore selection/caret position
                        textarea.val(response.body);
                        textarea.prop('defaultValue', textarea.val());
                        textarea.restoreSelection();
                        textarea.change();
                        textarea.focus();
                    }

                    // Remove messages (and display new messages, if any)
                    $('#content .messagebox').remove();
                    if (response.messages !== false) {
                        $(response.messages).prependTo('#content');
                    }
                });
            }
        })
        .always(function() {
            // Always hide overlay when ajax request is complete
            STUDIP.Overlay.hide();
        });

    event.preventDefault();
});

$(document).on('change', '#wiki-config .global-permissions :checkbox', function () {
    if ($(this).is(':checked')) {
        return;
    }

    $('#wiki-config .read-permissions [data-activates],[data-deactivates]').filter(':checked').change();
}).on('change', '#wiki-config .read-permissions :radio', function () {
    $('#wiki-config .edit-permissions:has(:radio[disabled]:checked) :radio:not([disabled]):first').prop('checked', true);
});

$(document).on('click', '.wiki-index-more', function (ev) {
    ev.preventDefault();
    $(this).parent().toggle();
    $(this).parent().nextAll('li').toggle();
});