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

    observeSidebar() {
        const options = {
            root: null,
            rootMargin: '0px 0px 35px 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);
        }
    },

    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.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;