aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/bootstrap/application.js
blob: c6ffe3522808be2c8541798cc8dc30a78383d568 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import { $gettext } from '../lib/gettext.js';
import eventBus from "../lib/event-bus.ts";

/* ------------------------------------------------------------------------
 * application.js
 * This file is part of Stud.IP - http://www.studip.de
 *
 * Stud.IP is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Stud.IP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Stud.IP; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

 /* ------------------------------------------------------------------------
  * add classes to html element according to horizontal screen size
  * ------------------------------------------------------------------------ */
(function ($) {
    // These sizes must match the breakpoints defined in breakspoints.less
    // TODO: use same webpack configuration for both
    const sizes = {
        tiny: '0px',
        small: '576px',
        medium: '768px',
        large: '1200px'
    };

    const setScreensizeClasses = function () {
        for (let size in sizes) {
            if (window.matchMedia(`(min-width: ${sizes[size]})`).matches) {
                $('html').addClass(`size-${size}`);
            } else {
                $('html').removeClass(`size-${size}`);
            }
        }
    };

    // Reset screen size classes on window resizes
    $(window).resize(setScreensizeClasses);

    // Set screen size classes initially
    setScreensizeClasses();
}(jQuery));

/* ------------------------------------------------------------------------
 * messages boxes
 * ------------------------------------------------------------------------ */
jQuery(document).on('click', '.messagebox .messagebox_buttons a', function () {
    if (jQuery(this).is('.details')) {
        jQuery(this).closest('.messagebox').toggleClass('details_hidden');
    } else if (jQuery(this).is('.close')) {
        jQuery(this).closest('.messagebox').hide('blind', 'fast', function () {
            jQuery(this).remove();
        });
    }
    return false;
});

STUDIP.ready(function() {
    //Set the focus on the modal overlay dialog, if any:
    $('.modaloverlay').find(':focusable').first().focus();
});


/* ------------------------------------------------------------------------
 * application wide setup
 * ------------------------------------------------------------------------ */
STUDIP.domReady(function () {
    STUDIP.study_area_selection.initialize();

    if (document.createElement('textarea').style.resize === undefined) {
        jQuery('textarea.resizable').resizable({
            handles: 's',
            minHeight: 50,
            zIndex: 1
        });
    }

    jQuery.ajaxSetup({
        beforeSend (jqXHR, settings) {
            const requestUrl = new URL(settings.url, STUDIP.ABSOLUTE_URI_STUDIP);
            const studipUrl = new URL(STUDIP.ABSOLUTE_URI_STUDIP);
            if (requestUrl.hostname === studipUrl.hostname && requestUrl.protocol === studipUrl.protocol) {
                jqXHR.setRequestHeader('X-CSRF-TOKEN', STUDIP.CSRF_TOKEN.value);
            }
        },
    });
});

STUDIP.ready((event) => {
    jQuery('.add_toolbar', event.target).addToolbar();

    STUDIP.Forms.initialize(event.target);
    STUDIP.Markup.element(event.target);
});


/* ------------------------------------------------------------------------
 * application collapsable tablerows
 * ------------------------------------------------------------------------ */
STUDIP.domReady(function () {

    $(document).on('click', 'table.collapsable .toggler', function () {
        $(this).closest('tbody').toggleClass('collapsed')
               .filter('.collapsed').find('.action-menu').removeClass('active');
        return false;
    });

    $(document).on('click', 'a.load-in-new-row', function () {
        if ($(this).data('busy')) {
            return false;
        }

        if ($(this).closest('tr').next().hasClass('loaded-details')) {
            $(this).closest('tr').next().remove();
            $('a.load-in-new-row').attr('aria-expanded', 'false');
            return false;
        }
        $(this).showAjaxNotification().data('busy', true);

        var that = this;
        $.get($(this).attr('href'), function (response) {
            var row = $('<tr />').addClass('loaded-details');
            $('<td />')
                .attr('colspan', $(that).closest('td').siblings().length + 1)
                .html(response)
                .appendTo(row);

            $(that)
                .hideAjaxNotification()
                .closest('tr').after(row);

            $(that).data('busy', false);
            $('body').trigger('ajaxLoaded');
            $('a.load-in-new-row').attr('aria-expanded', 'true');

        });

        return false;
    });

    $(document).on('click', '.loaded-details a.cancel', function () {
        $(this).closest('.loaded-details').prev().find('a.load-in-new-row').click();
        return false;
    });

    var elements = $('.load-in-new-row-open');
    elements.click();
    if (elements.length > 0) {
        $(window).scrollTo(elements.first());
    }
});

/* ------------------------------------------------------------------------
 * Toggle dates in seminar_main
 * ------------------------------------------------------------------------ */
(function ($) {
    $(document).on('click', '.more-dates', function () {
        $('.more-dates-infos').toggle();
        $('.more-dates-digits').toggle();
        if ($('.more-dates-infos').is(':visible')) {
            $('.more-dates').text('(weniger)');
            $('.more-dates').attr('title', $gettext('Blenden Sie die restlichen Termine aus'));
        } else {
            $('.more-dates').text('(mehr)');
            $('.more-dates').attr('title', $gettext('Blenden Sie die restlichen Termine ein'));
        }
    });
}(jQuery));

/* ------------------------------------------------------------------------
 * additional jQuery (UI) settings for Stud.IP
 * ------------------------------------------------------------------------ */
jQuery.ui.accordion.prototype.options.icons = {
    header: 'arrow_right',
    activeHeader: 'arrow_down'
};
eventBus.on('studip:set-locale', () => {
    jQuery.extend(jQuery.ui.dialog.prototype.options, {
        closeText: $gettext('Schließen')
    });
});



/* ------------------------------------------------------------------------
 * jQuery timepicker
 * ------------------------------------------------------------------------ */

/* German translation for the jQuery Timepicker Addon */
/* Written by Marvin */
(function ($) {
    $.timepicker.regional.de = {
        timeOnlyTitle: 'Zeit wählen',
        timeText: 'Zeit',
        hourText: 'Stunde',
        minuteText: 'Minute',
        secondText: 'Sekunde',
        millisecText: 'Millisekunde',
        microsecText: 'Mikrosekunde',
        timezoneText: 'Zeitzone',
        currentText: 'Jetzt',
        closeText: 'Fertig',
        timeFormat: "HH:mm",
        amNames: ['vorm.', 'AM', 'A'],
        pmNames: ['nachm.', 'PM', 'P'],
        isRTL: false,
        showTimezone: false
    };
    $.timepicker.setDefaults($.timepicker.regional.de);

    $(document).on('focus', '.has-time-picker', function () {
        $(this).removeClass('has-time-picker').timepicker();
    });
    $(document).on('focus', '.has-time-picker-select', function () {
        $(this).removeClass('has-time-picker-select').timepicker({controlType: 'select'});
    });
}(jQuery));


(function ($) {
    $(document).on('focusout', '.studip-timepicker', function () {
        var time = $(this).val();
        if (time.length > 0 && time.length <= 2) {
            $(this).val(time + ":00");
        } else if (time.indexOf(':') === -1 && time.length > 2) {
            var parts = time.split('');
            parts.splice(-2, 0, ':');
            time = parts.join('');
            $(this).val(time);
        }
    });
}(jQuery))


STUDIP.domReady(function () {
    $(document).on('click', 'a.print_action', function (event) {
        var url_to_print = this.href;
        $('<iframe/>', {
            name: url_to_print,
            src: url_to_print,
            width: '1px',
            height: '1px',
            frameborder: 0
        })
            .css({top: '-99px', position: 'absolute'})
            .appendTo('body')
            .on('load', (function () {
                this.contentWindow.focus();
                this.contentWindow.print();
            }));
        return false;
    });
});

/* Copies a value from a select to another element*/
jQuery(document).on('change', 'select[data-copy-to]', function () {
    var target = jQuery(this).data().copyTo,
        value = jQuery(this).val() || jQuery(target).prop('defaultValue');
    jQuery(target).val(value);
});

STUDIP.domReady(function () {
    $('#checkAll').prop('checked', $('.sem_checkbox:checked').length !== 0);
});

// Fix horizontal scroll issue on domready, window load and window resize.
// This also makes the header and footer sticky regarding horizontal scrolling.
STUDIP.domReady(function () {
    var page_margin = ($('#current-page-structure').outerWidth(true) - $('#current-page-structure').width()) / 2,
        content_margin = $('#content').outerWidth(true) - $('#content').innerWidth(),
        sidebar_width = $('#sidebar').outerWidth(true);

    function fixScrolling() {
        $('#current-page-structure').removeClass('oversized').css({
            minWidth: '',
            marginRight: '',
            paddingRight: ''
        });

        var max_width = 0,
            fix_required = $('html').is(':not(.responsified)') && $('#content').get(0).scrollWidth > $('#content').width();

        if (fix_required) {
            $('#content').children().each(function () {
                var width = $(this).get(0).scrollWidth + ($(this).outerWidth(true) - $(this).innerWidth());
                if (width > max_width) {
                    max_width = width;
                }
            });

            $('#current-page-structure').addClass('oversized').css({
                minWidth: sidebar_width + content_margin + max_width + page_margin,
                marginRight: 0,
                paddingRight: page_margin
            });

            STUDIP.Scroll.addHandler('horizontal-scroll', (function () {
                var last_left = null;
                return function (top, left) {
                    if (last_left !== left) {
                        $('#navigation-level-1,#tabs,#main-footer,#top-bar').css({
                            transform: 'translate3d(' + left + 'px,0,0)'
                        });
                    }
                    last_left = left;
                };
            }()));
        } else {
            STUDIP.Scroll.removeHandler('horizontal-scroll');
        }
    }

    if ($('.no-touch #content').length > 0) {
        window.matchMedia('screen').addListener(function() {
            // Try to fix now
            fixScrolling();

            // and fix again on window load and resize
            $(window).on('resize load', _.debounce(fixScrolling, 100));
        });
    }
});

// Global handler:
// Toggle a table element. The url of the link will be called, an ajax
// indicator will be shown instead of the element and the whole table row
// will be replaced with the row with the same id from the response.
// Thus, in your controller you only have to execute the appropriate
// action and redraw the page with the new state.
jQuery(document).on('click', 'a[data-behaviour~="ajax-toggle"]', function (event) {
    var $that = jQuery(this),
        href  = $that.attr('href'),
        id    = $that.closest('tr').attr('id');

    $that.prop('disabled', true).addClass('ajaxing');
    jQuery.get(href).done(function (response) {
        var row = jQuery('#' + id, response);
        $that.closest('tr').replaceWith(row);
    });

    event.preventDefault();
});

/* Change open-variable on course-basicdata*/
(function ($) {
    $(document).on('click', 'form[name=course-details] fieldset legend', function () {
        $('#open_variable').attr('value', $(this).parent('fieldset').data('open'));
    });
}(jQuery));