|
1 |
| -// Modified from https://stackoverflow.com/a/27078401 |
2 |
| -window.pyTorchThrottle = function(func, wait, options) { |
3 |
| - var context, args, result; |
4 |
| - var timeout = null; |
5 |
| - var previous = 0; |
6 |
| - if (!options) options = {}; |
7 |
| - var later = function() { |
8 |
| - previous = options.leading === false ? 0 : Date.now(); |
9 |
| - timeout = null; |
10 |
| - result = func.apply(context, args); |
11 |
| - if (!timeout) context = args = null; |
12 |
| - }; |
13 |
| - return function() { |
14 |
| - var now = Date.now(); |
15 |
| - if (!previous && options.leading === false) previous = now; |
16 |
| - var remaining = wait - (now - previous); |
17 |
| - context = this; |
18 |
| - args = arguments; |
19 |
| - if (remaining <= 0 || remaining > wait) { |
20 |
| - if (timeout) { |
21 |
| - clearTimeout(timeout); |
22 |
| - timeout = null; |
23 |
| - } |
24 |
| - previous = now; |
25 |
| - result = func.apply(context, args); |
26 |
| - if (!timeout) context = args = null; |
27 |
| - } else if (!timeout && options.trailing !== false) { |
28 |
| - timeout = setTimeout(later, remaining); |
29 |
| - } |
30 |
| - return result; |
31 |
| - }; |
32 |
| -}; |
33 |
| - |
34 | 1 | // Modified from https://stackoverflow.com/a/32396543
|
35 | 2 | window.highlightNavigation = {
|
36 |
| - navigationListItems: $(".pytorch-right-menu li"), |
37 |
| - sections: $( |
38 |
| - $(".pytorch-article .section") |
39 |
| - .get() |
40 |
| - .reverse() |
41 |
| - ), |
| 3 | + navigationListItems: document.querySelectorAll("#pytorch-right-menu li"), |
| 4 | + sections: document.querySelectorAll(".pytorch-article .section"), |
42 | 5 | sectionIdTonavigationLink: {},
|
43 | 6 |
|
44 | 7 | bind: function() {
|
45 |
| - // Don't show the "Shortcuts" text unless there are menu items |
46 |
| - if (highlightNavigation.navigationListItems.length > 1) { |
47 |
| - $(".pytorch-shortcuts-wrapper").show(); |
48 |
| - } |
| 8 | + if (!sideMenus.displayRightMenu) { |
| 9 | + return; |
| 10 | + }; |
49 | 11 |
|
50 |
| - highlightNavigation.sections.each(function() { |
51 |
| - var id = $(this).attr("id"); |
52 |
| - highlightNavigation.sectionIdTonavigationLink[id] = $( |
53 |
| - ".pytorch-right-menu li a[href='#" + id + "']" |
54 |
| - ); |
55 |
| - }); |
| 12 | + for (var i = 0; i < highlightNavigation.sections.length; i++) { |
| 13 | + var id = highlightNavigation.sections[i].id; |
| 14 | + highlightNavigation.sectionIdTonavigationLink[id] = |
| 15 | + document.querySelectorAll('#pytorch-right-menu li a[href="#' + id + '"]')[0]; |
| 16 | + } |
56 | 17 |
|
57 |
| - $(window).scroll(pyTorchThrottle(highlightNavigation.highlight, 500)); |
| 18 | + $(window).scroll(utilities.throttle(highlightNavigation.highlight, 100)); |
58 | 19 | },
|
59 | 20 |
|
60 | 21 | highlight: function() {
|
61 |
| - var scrollPosition = $(window).scrollTop(); |
62 |
| - var offset = $(".header-holder").height() + $(".pytorch-page-level-bar").height() + 25; |
| 22 | + var rightMenu = document.getElementById("pytorch-right-menu"); |
63 | 23 |
|
64 |
| - highlightNavigation.sections.each(function() { |
65 |
| - var currentSection = $(this); |
66 |
| - var sectionTop = currentSection.offset().top; |
| 24 | + // If right menu is not on the screen don't bother |
| 25 | + if (rightMenu.offsetWidth === 0 && rightMenu.offsetHeight === 0) { |
| 26 | + return; |
| 27 | + } |
67 | 28 |
|
68 |
| - if (scrollPosition >= sectionTop - offset) { |
69 |
| - var id = currentSection.attr("id"); |
70 |
| - var $navigationLink = highlightNavigation.sectionIdTonavigationLink[id]; |
71 |
| - var $navigationListItem = $navigationLink.closest("li"); |
| 29 | + var scrollPosition = utilities.scrollTop(); |
| 30 | + var OFFSET_TOP_PADDING = 25; |
| 31 | + var offset = document.getElementById("header-holder").offsetHeight + |
| 32 | + document.getElementById("pytorch-page-level-bar").offsetHeight + |
| 33 | + OFFSET_TOP_PADDING; |
72 | 34 |
|
73 |
| - if (!$navigationListItem.hasClass("active")) { |
74 |
| - highlightNavigation.navigationListItems.removeClass("active"); |
75 |
| - $(".pytorch-right-menu-active-dot").remove(); |
| 35 | + var sections = highlightNavigation.sections; |
76 | 36 |
|
77 |
| - if ($navigationLink.is(":visible")) { |
78 |
| - $navigationListItem.addClass("active"); |
79 |
| - $navigationListItem.prepend("<div class=\"pytorch-right-menu-active-dot\"></div>"); |
| 37 | + for (var i = (sections.length - 1); i >= 0; i--) { |
| 38 | + var currentSection = sections[i]; |
| 39 | + var sectionTop = utilities.offset(currentSection).top; |
| 40 | + |
| 41 | + if (scrollPosition >= sectionTop - offset) { |
| 42 | + var navigationLink = highlightNavigation.sectionIdTonavigationLink[currentSection.id]; |
| 43 | + var navigationListItem = utilities.closest(navigationLink, "li"); |
| 44 | + |
| 45 | + if (navigationListItem && !navigationListItem.classList.contains("active")) { |
| 46 | + for (var i = 0; i < highlightNavigation.navigationListItems.length; i++) { |
| 47 | + var el = highlightNavigation.navigationListItems[i]; |
| 48 | + if (el.classList.contains("active")) { |
| 49 | + el.classList.remove("active"); |
| 50 | + } |
80 | 51 | }
|
| 52 | + |
| 53 | + navigationListItem.classList.add("active"); |
| 54 | + |
| 55 | + // Scroll to active item. Not a requested feature but we could revive it. Needs work. |
| 56 | + |
| 57 | + // var menuTop = $("#pytorch-right-menu").position().top; |
| 58 | + // var itemTop = navigationListItem.getBoundingClientRect().top; |
| 59 | + // var TOP_PADDING = 20 |
| 60 | + // var newActiveTop = $("#pytorch-side-scroll-right").scrollTop() + itemTop - menuTop - TOP_PADDING; |
| 61 | + |
| 62 | + // $("#pytorch-side-scroll-right").animate({ |
| 63 | + // scrollTop: newActiveTop |
| 64 | + // }, 100); |
81 | 65 | }
|
82 | 66 |
|
83 |
| - return false; |
| 67 | + break; |
84 | 68 | }
|
85 |
| - }); |
| 69 | + } |
86 | 70 | }
|
87 | 71 | };
|
0 commit comments