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
|
const Sidebar = {
place() {
const header = document.getElementById('main-header');
const sidebar = document.getElementById('sidebar');
if (sidebar) {
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();
}
},
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.classList.add('fixed');
sidebar.style.top = '';
} else if (mutation.oldValue && mutation.oldValue.indexOf('fixed') !== -1
&& !mutation.target.classList.contains('fixed')) {
sidebar.classList.remove('fixed');
}
}
});
// 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'));
},
reset() {
const sidebar = document.getElementById('sidebar');
if (sidebar) {
sidebar.classList.remove('oversized', 'adjusted', 'fixed');
sidebar.style.top = '';
}
STUDIP.Sidebar.observe();
},
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', 'adjusted');
}
});
}
},
footerVisible(entries, observer) {
const sidebar = document.getElementById('sidebar');
if (sidebar) {
entries.forEach(entry => {
// Footer is visible on current page.
if (entry.isIntersecting) {
if (sidebar.classList.contains('no-footer')) {
sidebar.classList.remove('no-footer');
}
} else {
if (!sidebar.classList.contains('no-footer')) {
sidebar.classList.add('no-footer');
}
}
});
}
}
};
export default Sidebar;
|