-
Notifications
You must be signed in to change notification settings - Fork 329
/
Copy pathindex.js
79 lines (65 loc) · 2.51 KB
/
index.js
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
/**
* This file should be edited in ./src/js/index.js. After bundling the resulting file in ./pydata_sphinx_theme/static/js/index.js should never be manually changed.
* Edit ./src/js/index.js and run yarn build:production.
*/
/* Sphinx injects the html output with jquery and other javascript files.
* To enable Popper.js (and other jQuery plugins) to hook into the same instancce of jQuery,
* jQuery is defined as a Webpack external, thus this import uses the externally defined jquery dependency.
*/
import 'jquery';
import 'popper.js';
import 'bootstrap';
import './../scss/index.scss';
function addTOCInteractivity() {
// TOC sidebar - add "active" class to parent list
//
// Bootstrap's scrollspy adds the active class to the <a> link,
// but for the automatic collapsing we need this on the parent list item.
//
// The event is triggered on "window" (and not the nav item as documented),
// see https://github.com/twbs/bootstrap/issues/20086
$(window).on('activate.bs.scrollspy', function () {
const navLinks = document.querySelectorAll('#bd-toc-nav a');
navLinks.forEach((navLink) => {
navLink.parentElement.classList.remove('active');
});
const activeNavLinks = document.querySelectorAll('#bd-toc-nav a.active');
activeNavLinks.forEach((navLink) => {
navLink.parentElement.classList.add('active');
});
});
}
// Navigation sidebar scrolling to active page
function scrollToActive() {
var sidebar = document.getElementById('bd-docs-nav')
// Remember the sidebar scroll position between page loads
// Inspired on source of revealjs.com
let storedScrollTop = parseInt(sessionStorage.getItem('sidebar-scroll-top'), 10);
if (!isNaN(storedScrollTop)) {
sidebar.scrollTop = storedScrollTop;
}
else {
var active_pages = sidebar.querySelectorAll(".active")
var offset = 0
var i;
for (i = active_pages.length - 1; i > 0; i--) {
var active_page = active_pages[i]
if (active_page !== undefined) {
offset += active_page.offsetTop
}
}
offset -= sidebar.offsetTop
// Only scroll the navbar if the active link is lower than 50% of the page
if (active_page !== undefined && offset > (sidebar.clientHeight * .5)) {
sidebar.scrollTop = offset - (sidebar.clientHeight * .2)
}
}
// Store the sidebar scroll position
window.addEventListener('beforeunload', () => {
sessionStorage.setItem('sidebar-scroll-top', sidebar.scrollTop);
});
}
$(document).ready(() => {
scrollToActive();
addTOCInteractivity();
});