forked from adamlaska/circleci-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsidebar.js
More file actions
103 lines (92 loc) · 3.43 KB
/
sidebar.js
File metadata and controls
103 lines (92 loc) · 3.43 KB
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
// making sidebar stick when touching footer
// this improves the visual experience while interacting with the docs site
(function () {
window.addEventListener('load', function () {
var footer = document.querySelector('.footer');
var sidebar = document.querySelector('.sidebar');
var defaultSectionName = 'getting-started';
var mobileSidebar = document.querySelector('.sidebar-mobile-wrapper');
var mobileSidebarDefault = mobileSidebar.querySelector(
'[data-id="' + defaultSectionName + '"]',
);
// activate default section, if nothing else is selected
var activeSection = defaultSectionName;
var activePage = sidebar.querySelector('.active');
if (activePage) {
// find section for active page
activeSection = activePage.getAttribute('data-section');
}
// fullscreen sidenav expansion
function sidenavAutoExpand(parent) {
var element = parent.querySelector(
'[data-section=' + activeSection + ']',
);
if (element && element.classList.contains('closed')) {
element.classList.remove('closed');
}
}
sidenavAutoExpand(sidebar);
scrollToActiveSidebarItem();
// for mobile sidebar, if sidebar is set, display proper item
var mobileCurrentElement = mobileSidebar.querySelector(
'[data-id=' + activeSection + ']',
);
if (
mobileCurrentElement &&
mobileCurrentElement.classList.contains('hidden')
) {
mobileCurrentElement.classList.remove('hidden');
mobileSidebarDefault.classList.add('hidden');
}
function setSidebar() {
// if footer is in frame, removed fixed style (otherwise add it, if it doesn't exist)
if (
footer?.getBoundingClientRect?.()?.top - window.innerHeight <= 0 &&
footer?.getBoundingClientRect?.()?.top >= window.innerHeight
) {
if (sidebar?.classList?.contains?.('fixed')) {
sidebar.classList.remove('fixed');
}
} else {
if (!sidebar?.classList?.contains?.('fixed')) {
sidebar.classList.add('fixed');
}
}
// prevents display problems on very large screens with little content
if (footer?.getBoundingClientRect?.()?.top <= window.innerHeight) {
sidebar.style.height = footer.getBoundingClientRect().top - 70 + 'px';
} else {
sidebar.style.height = null;
}
}
function scrollToActiveSidebarItem() {
var activeEl = $('nav.sidebar .active')[0];
var sidebarTop = $('nav.sidebar')[0].offsetTop;
var activeElTop = activeEl && activeEl.offsetTop;
var elementRelativeTop = activeElTop - sidebarTop;
$('nav.sidebar').scrollTop(elementRelativeTop);
}
window.addEventListener('load', setSidebar);
// allowing opening/closing of subnav elements
var mainNavItems = Array.from(
document.querySelectorAll('nav.sidebar .main-nav-item'),
);
var mobileNavItems = Array.from(
document.querySelectorAll('nav.mobile-sidebar .main-nav-item'),
);
function enableAccordion(array) {
array.forEach(function (item) {
var listWrap = item.querySelector('.list-wrap');
listWrap.addEventListener('click', function () {
if (item.classList.contains('closed')) {
item.classList.remove('closed');
} else {
item.classList.add('closed');
}
});
});
}
enableAccordion(mainNavItems);
enableAccordion(mobileNavItems);
});
})();