|
| 1 | +var selector; |
| 2 | +var label; |
| 3 | +var isVisibleByDefault; |
| 4 | + |
| 5 | +require(["gitbook", "jQuery"], function(gitbook, $) { |
| 6 | + anchors.options = { |
| 7 | + placement: 'left' |
| 8 | + }; |
| 9 | + |
| 10 | + gitbook.events.bind('start', function(e, config) { |
| 11 | + var configuration = config['intopic-toc']; |
| 12 | + selector = configuration.selector; |
| 13 | + isVisibleByDefault = configuration.visible; |
| 14 | + label = configuration.label; |
| 15 | + |
| 16 | + // Label can be language specific and could be specified via user configuration |
| 17 | + if (typeof label === 'object') { |
| 18 | + var language = gitbook.state.innerLanguage; |
| 19 | + |
| 20 | + if (language && label.hasOwnProperty(language)) { |
| 21 | + label = label[language]; |
| 22 | + } else { |
| 23 | + label = ''; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + // Hide navigation if a search is ative |
| 28 | + $bookSearchResults = $('#book-search-results'); |
| 29 | + |
| 30 | + var observer = new MutationObserver(() => { |
| 31 | + if ($bookSearchResults.hasClass('open')) { |
| 32 | + $('.intopic-toc').hide(); |
| 33 | + } |
| 34 | + else { |
| 35 | + $('.intopic-toc').show(); |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + observer.observe($bookSearchResults[0], { attributes: true }); |
| 40 | + }); |
| 41 | + |
| 42 | + gitbook.events.bind("page.change", function() { |
| 43 | + anchors.removeAll(); |
| 44 | + anchors.add(selector); |
| 45 | + |
| 46 | + var isVisible = (isVisibleByDefault || gitbook.state.page.isTocVisible) && gitbook.state.page.isTocVisible != false; |
| 47 | + |
| 48 | + if (anchors.elements.length > 1 && isVisible) { |
| 49 | + var navigation = buildNavigation(anchors.elements); |
| 50 | + |
| 51 | + var section = document.body.querySelector('.page-wrapper'); |
| 52 | + section.appendChild(navigation, section.firstChild); |
| 53 | + |
| 54 | + $(".book-body .body-inner").scroll(onScroll); |
| 55 | + } |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +function buildNavigation(elements) { |
| 60 | + var navigation = document.createElement('nav'); |
| 61 | + navigation.className = 'intopic-toc'; |
| 62 | + |
| 63 | + var heading = document.createElement('h3'); |
| 64 | + heading.innerText = label; |
| 65 | + navigation.appendChild(heading); |
| 66 | + |
| 67 | + var container = document.createElement('ol'); |
| 68 | + navigation.appendChild(container); |
| 69 | + |
| 70 | + for (var i = 0; i < elements.length; i++) { |
| 71 | + var text = elements[i].textContent; |
| 72 | + var href = elements[i].querySelector('.anchorjs-link').getAttribute('href'); |
| 73 | + |
| 74 | + var item = document.createElement('li'); |
| 75 | + |
| 76 | + if (i === 0) { |
| 77 | + item.classList.add('selected'); |
| 78 | + } |
| 79 | + |
| 80 | + var anchor = document.createElement('a'); |
| 81 | + anchor.text = text; |
| 82 | + anchor.href = href; |
| 83 | + |
| 84 | + item.appendChild(anchor); |
| 85 | + container.appendChild(item); |
| 86 | + } |
| 87 | + |
| 88 | + return navigation; |
| 89 | +} |
| 90 | + |
| 91 | +function onScroll(e) { |
| 92 | + const currentPosition = $(e.target).scrollTop() + $('.book-header').height(); |
| 93 | + |
| 94 | + for (var i = 0; i < anchors.elements.length; i++) { |
| 95 | + const section = anchors.elements[i]; |
| 96 | + const isInView = $(section).offset().top < currentPosition; |
| 97 | + |
| 98 | + if (isInView) { |
| 99 | + const menuItemID = section.getAttribute('id'); |
| 100 | + const activeItem = $(`.intopic-toc ol li`).has(`a[href="#${menuItemID}"]`); |
| 101 | + |
| 102 | + if (!activeItem) { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + $('.intopic-toc ol li').each(function() { |
| 107 | + $(this).attr('data-active', 'false'); |
| 108 | + $(this).removeClass('selected'); |
| 109 | + }); |
| 110 | + |
| 111 | + $(activeItem).attr('data-active', 'true'); |
| 112 | + $(activeItem).addClass('selected'); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments