aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/lib/news.js
blob: 5319ed750ffa955941285dfe6713cd34ea8ae7c1 (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
import { $gettext } from '../lib/gettext.js';

/*jslint browser: true, unparam: true */
/*global jQuery, STUDIP */
const News = {
    /**
     * (Re-)initialise news-page, f.e. to stay in dialog
     */
    init (id) {
        $('.add_toolbar').addToolbar();
        STUDIP.i18n.init(`#${id}`);

        // prevent forms within dialog from reloading whole page, and reload dialog instead
        $(`#${id} form`).on('click', function (event) {
            $(this).data('clicked', $(event.target));
        }).on('submit', function (event) {
            event.preventDefault();

            var textarea, button, form_route, form_data;
            if (STUDIP.editor_enabled) {
                textarea = $('textarea.news_body');
                // wysiwyg is active, ensure HTML markers are set
                textarea.each(function () {
                    $(this).val(STUDIP.wysiwyg.markAsHtml($(this).val()));
                });
            }

            button     = $(this).data('clicked').attr('name');
            form_route = $(this).attr('action');
            form_data  = $(this).serialize() + '&' + button + '=1';

            $(this).find(`input[name=${button}]`).showAjaxNotification('left');
            News.update_dialog(id, form_route, form_data);
        });

        $(document).on('change', `#${id} form .news_date`, function () {
            // This is neccessary since datepickers are initialiszed on focus
            // which might not have occured yet
            STUDIP.UI.Datepicker.init();

            var start = $('#news_startdate').blur().datepicker('getDate'),
                duration,
                end,
                result;
            if ($(this).is('#news_duration')) {
                // datepicker assumes beginning of day (00:00), but the duration includes the end date (until 23:59)
                duration = window.parseInt(this.value, 10) - 1;
                result   = new Date(start);
                result.setDate(result.getDate() + duration);

                $('#news_enddate').datepicker('setDate', result);
            } else {
                start    = $('#news_startdate').datepicker('getDate');
                end      = $('#news_enddate').datepicker('getDate');
                // datepicker assumes beginning of day (see above) and we need to add a day to the duration
                duration = Math.round((end - start) / (24 * 60 * 60 * 1000)) + 1;
                duration = Math.max(0, duration);

                $('#news_duration').val(duration);
            }
        });
    },

    get_dialog (id, route) {
        // initialize dialog
        $('body').append(`<div id="${id}"></div>`);
        $(`#${id}`).dialog({
            modal: true,
            height: News.dialog_height,
            title: $gettext('Dialog wird geladen...'),
            width: News.dialog_width,
            close () {
                $(`#${id}`).remove();
            }
        });

        // load actual dialog content
        $.get(route, 'html').done(function (html, status, xhr) {
            $(`#${id}`).dialog('option', 'title', decodeURIComponent(xhr.getResponseHeader('X-Title')));
            $(`#${id}`).html(html);
            $(`#${id}_content`).css({
                height : (News.dialog_height - 120) + 'px',
                maxHeight: (News.dialog_height - 120) + 'px'
            });

            News.init(id);
        }).fail(function () {
            window.alert($gettext('Fehler beim Aufruf des News-Controllers'));
        });
    },

    update_dialog (id, route, form_data) {
        if (!News.pending_ajax_request) {
            News.pending_ajax_request = true;

            $.post(route, form_data, 'html').done(function (html) {
                var obj;

                News.pending_ajax_request = false;
                if (html.length > 0) {
                    $(`#${id}`).html(html);
                    $(`#${id}_content`).css({
                        height : (News.dialog_height - 120) + 'px',
                        maxHeight: (News.dialog_height - 120) + 'px'
                    });
                    // scroll to anker
                    obj = $('a[name=anker]');
                    if (obj.length > 0) {
                        $(`#${id}_content`).scrollTop(obj.position().top);
                    }
                } else {
                    $(`#${id}`).dialog('close');
                    obj = $('#admin_news_form');
                    if (obj.length > 0) {
                        $('#admin_news_form').submit();
                    } else {
                        location.replace(STUDIP.URLHelper.getURL(location.href, {nsave: 1}));
                    }
                }

                News.init(id);
            }).fail(function () {
                News.pending_ajax_request = false;
                window.alert($gettext('Fehler beim Aufruf des News-Controllers'));
            });
        }
    },

    toggle_category_view (id) {
        if ($(`input[name=${id}_js]`).val() === 'toggle') {
            $(`input[name=${id}_js]`).val('');
        } else {
            $(`input[name=${id}_js]`).val('toggle');
        }
        if ($(`#${id}_content`).is(':visible')) {
            $(`#${id}_content`).slideUp(400);
            $(`#${id} input[type=image]:first`)
                .attr('src', STUDIP.ASSETS_URL + 'images/icons/blue/arr_1right.svg');
        } else {
            $(`#${id}_content`).slideDown(400);
            $(`#${id} input[type=image]:first`)
                .attr('src', STUDIP.ASSETS_URL + 'images/icons/blue/arr_1down.svg');
        }
    }
};

export default News;