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

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);
            }
        });
    },


    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;