summary refs log tree commit diff
path: root/v1.48/website_files/table-of-contents.js
blob: 0de5960b22b137d12c4402a4801f468a70b06daf (plain) (blame)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const getPageToc = () => document.getElementsByClassName('pagetoc')[0];

const pageToc = getPageToc();
const pageTocChildren = [...pageToc.children];
const headers = [...document.getElementsByClassName('header')];


// Select highlighted item in ToC when clicking an item
pageTocChildren.forEach(child => {
    child.addEventHandler('click', () => {
        pageTocChildren.forEach(child => {
            child.classList.remove('active');
        });
        child.classList.add('active');
    });
});


/**
 * Test whether a node is in the viewport
 */
function isInViewport(node) {
    const rect = node.getBoundingClientRect();
    return rect.top >= 0 && rect.left >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && rect.right <= (window.innerWidth || document.documentElement.clientWidth);
}


/**
 * Set a new ToC entry.
 * Clear any previously highlighted ToC items, set the new one,
 * and adjust the ToC scroll position.
 */
function setTocEntry() {
    let activeEntry;
    const pageTocChildren = [...getPageToc().children];

    // Calculate which header is the current one at the top of screen
    headers.forEach(header => {
        if (window.pageYOffset >= header.offsetTop) {
            activeEntry = header;
        }
    });

    // Update selected item in ToC when scrolling
    pageTocChildren.forEach(child => {
        if (activeEntry.href.localeCompare(child.href) === 0) {
            child.classList.add('active');
        } else {
            child.classList.remove('active');
        }
    });

    let tocEntryForLocation = document.querySelector(`nav a[href="${activeEntry.href}"]`);
    if (tocEntryForLocation) {
        const headingForLocation = document.querySelector(activeEntry.hash);
        if (headingForLocation && isInViewport(headingForLocation)) {
            // Update ToC scroll
            const nav = getPageToc();
            const content = document.querySelector('html');
            if (content.scrollTop !== 0) {
                nav.scrollTo({
                    top: tocEntryForLocation.offsetTop - 100,
                    left: 0,
                    behavior: 'smooth',
                });
            } else {
                nav.scrollTop = 0;
            }
        }
    }
}


/**
 * Populate sidebar on load
 */
window.addEventListener('load', () => {
    // Only create table of contents if there is more than one header on the page
    if (headers.length <= 1) {
        return;
    }

    // Create an entry in the page table of contents for each header in the document
    headers.forEach((header, index) => {
        const link = document.createElement('a');

        // Indent shows hierarchy
        let indent = '0px';
        switch (header.parentElement.tagName) {
            case 'H1':
                indent = '5px';
                break;
            case 'H2':
                indent = '20px';
                break;
            case 'H3':
                indent = '30px';
                break;
            case 'H4':
                indent = '40px';
                break;
            case 'H5':
                indent = '50px';
                break;
            case 'H6':
                indent = '60px';
                break;
            default:
                break;
        }

        let tocEntry;
        if (index == 0) {
            // Create a bolded title for the first element
            tocEntry = document.createElement("strong");
            tocEntry.innerHTML = header.text;
        } else {
            // All other elements are non-bold
            tocEntry = document.createTextNode(header.text);
        }
        link.appendChild(tocEntry);

        link.style.paddingLeft = indent;
        link.href = header.href;
        pageToc.appendChild(link);
    });
    setTocEntry.call();
});


// Handle active headers on scroll, if there is more than one header on the page
if (headers.length > 1) {
    window.addEventListener('scroll', setTocEntry);
}