aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/lib/startpage.js
blob: 1d7de31fa82871afc6757f7a63cd7048df783e1b (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
const startpage = {
    init() {
        $('.start-widgetcontainer .portal-widget-list').sortable({
            handle: '.widget-header',
            connectWith: 'ul.portal-widget-list',
            start() {
                $(this)
                    .closest('.start-widgetcontainer')
                    .find('.portal-widget-list')
                    .addClass('move');
            },
            update(event, ui) {
                let lanes = [];
                $(this)
                    .closest('.start-widgetcontainer')
                    .children('.portal-widget-list')
                    .each((index, element) => {
                        lanes[index] = $('.studip-widget-wrapper', element)
                            .map((i, el) => el.getAttribute('id'))
                            .get(); // Ensure we have an array
                    });

                $.post(
                    STUDIP.URLHelper.getURL('dispatch.php/start/storeNewOrder'),
                    {lanes}
                );
            },
            stop() {
                $(this)
                    .closest('.start-widgetcontainer')
                    .find('.portal-widget-list')
                    .removeClass('move');
            }
        });
    },

    init_edit(perm) {
        $('.edit-widgetcontainer .portal-widget-list').sortable({
            handle: '.widget-header',
            connectWith: '.edit-widgetcontainer .portal-widget-list',
            start: function() {
                $(this)
                    .closest('.edit-widgetcontainer')
                    .find('.portal-widget-list')
                    .addClass('ui-sortable move');
            },
            stop: function() {
                // store the whole widget constellation
                var widgets = {
                    left: {},
                    right: {}
                };

                $('.edit-widgetcontainer .start-widgetcontainer .portal-widget-list:first-child > li').each(function() {
                    widgets.left[$(this).attr('id')] = $(this).index();
                });

                $('.edit-widgetcontainer .start-widgetcontainer .portal-widget-list:last-child > li').each(function() {
                    widgets.right[$(this).attr('id')] = $(this).index();
                });

                $.post(STUDIP.ABSOLUTE_URI_STUDIP + 'dispatch.php/start/update_defaults/' + perm, widgets);

                $(this)
                    .closest('.edit-widgetcontainer')
                    .find('.portal-widget-list')
                    .removeClass('move');
            }
        });
    }
};

export default startpage;