aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/lib/sidebar.js
blob: a92b2a25d74474b9b354a5823e144472b4dea4d0 (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
const Sidebar = {

    place() {
        const header = document.getElementById('main-header');
        const sidebar = document.getElementById('sidebar');
        if (sidebar && !document.body.classList.contains('fixed')) {
            sidebar.style.top =
                header.offsetTop + header.offsetHeight + 'px';
        }
    },

    observeSidebar() {
        const options = {
            root: null,
            rootMargin: '0px',
            threshold: 1
        };

        /**
         * Observe if sidebar fits into viewport.
         */
        const sidebar = document.getElementById('sidebar');
        if (sidebar) {
            const sObserver = new IntersectionObserver(STUDIP.Sidebar.fits, options);
            sObserver.observe(sidebar, options);
        }
    },

    observeBody() {
        const sidebar = document.getElementById('sidebar');
        /**
         * Observe body for class changes. If "fixed" is added or removed, we are in scroll mode
         * where the top navigation is removed or visible again.
         */
        const mObserver = new MutationObserver(mutations => {
            for (const mutation of mutations) {
                if ((!mutation.oldValue || mutation.oldValue.indexOf('fixed') === -1)
                    && mutation.target.classList.contains('fixed')) {
                    sidebar.style.top = '';
                    sidebar.classList.add('fixed');
                } else if (mutation.oldValue && mutation.oldValue.indexOf('fixed') !== -1
                    && !mutation.target.classList.contains('fixed')) {
                    STUDIP.Sidebar.reset();
                }
            }
        });

        // Observe body for class changes.
        mObserver.observe(document.body, {
            attributes: true,
            attributeOldValue : true,
            attributeFilter: ['class']
        });
    },

    observeFooter() {
        const options = {
            root: null,
            rootMargin: '0px',
            threshold: 1
        };

        /**
         * Observe if the footer is visible in viewport.
         */
        const fObserver = new IntersectionObserver(STUDIP.Sidebar.footerVisible, options);
        fObserver.observe(document.getElementById('main-footer'), options);

    },

    reset() {
        const sidebar = document.getElementById('sidebar');
        if (sidebar) {
            sidebar.classList.remove('oversized', 'was-oversized', 'fixed');
            sidebar.style.top = '';
            STUDIP.Sidebar.place();
            STUDIP.Sidebar.observeSidebar();
        }
    },

    fits(entries, observer) {
        const sidebar = document.getElementById('sidebar');
        if (sidebar) {
            entries.forEach(entry => {
                // Sidebar fits onto current page.
                if (entry.isIntersecting) {
                    sidebar.classList.remove('oversized');
                } else {
                    sidebar.classList.add('oversized', 'was-oversized');
                }
            });
        }
    },

    footerVisible(entries, observer) {
        const sidebar = document.getElementById('sidebar');
        if (sidebar) {
            entries.forEach(entry => {
                // Footer is visible on current page.
                if (entry.isIntersecting) {
                    sidebar.classList.remove('no-footer');
                } else {
                    sidebar.classList.add('no-footer');
                }
            });
        }
    }
};

export default Sidebar;