aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/bootstrap/opengraph.js
blob: a72e2c231b9d0451b9025160cc937cf320894bd3 (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
function handleOpenGraphSections() {
    $('.opengraph-area:not(.handled)').each(function() {
        var items = $('.opengraph', this),
            switcher;
        if (items.length > 1) {
            items.filter(':gt(0)').addClass('hidden');

            switcher = $('<ul class="switcher">');
            $('<li><button class="switch-left" disabled>&lt;</button></li>').appendTo(switcher);
            $('<li><button class="switch-right">&gt;</button></li>').appendTo(switcher);
            switcher.prependTo(this);
        }

        $(this).addClass('handled');
    });
}

STUDIP.ready(handleOpenGraphSections);
$(document).on('ajaxComplete', handleOpenGraphSections);

$(document).on('click', '.opengraph-area .switcher button', function (event) {
    var direction = $(this).is('.switch-left') ? 'left' : 'right',
        current = $(this)
            .closest('.opengraph-area')
            .find('.opengraph:visible'),
        switcher = $(this).closest('.switcher'),
        buttons = {
            left: $('.switch-left', switcher),
            right: $('.switch-right', switcher)
        };

    if (direction === 'left') {
        current = current
            .addClass('hidden')
            .prev()
            .removeClass('hidden');
        buttons.left.attr('disabled', current.prev('.opengraph').length === 0);
        buttons.right.attr('disabled', false);
    } else {
        current = current
            .addClass('hidden')
            .next()
            .removeClass('hidden');
        buttons.left.attr('disabled', false);
        buttons.right.attr('disabled', current.next('.opengraph').length === 0);
    }

    event.preventDefault();
});