diff --git a/js/_utilities.js b/js/_utilities.js index eb3648c7..06bfe4c3 100644 --- a/js/_utilities.js +++ b/js/_utilities.js @@ -98,5 +98,29 @@ window.utilities = { return window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight; + }, + + /** + * Return the offset amount to deduct from the normal scroll position. + * Modify as appropriate to allow for dynamic calculations + */ + getFixedOffset: function() { + var OFFSET_HEIGHT_PADDING = 20; + // TODO: this is a little janky. We should try to not rely on JS for this + return document.getElementById("sphinx-template-page-level-bar").offsetHeight + OFFSET_HEIGHT_PADDING; + }, + + findParent: function(item) { + return $(item).parent().parent().siblings("a.reference.internal") + }, + makeHighlight: function(item) { + if ($(item).hasClass("title-link")) { + return + } + $(item).addClass("side-scroll-highlight"); + var parent = utilities.findParent(item); + if (~parent.hasClass("title-link")) { + utilities.makeHighlight(parent) + } } } diff --git a/js/highlight-navigation.js b/js/highlight-navigation.js index 60f6b2e6..de11d6ff 100644 --- a/js/highlight-navigation.js +++ b/js/highlight-navigation.js @@ -1,7 +1,7 @@ // Modified from https://stackoverflow.com/a/32396543 window.highlightNavigation = { navigationListItems: document.querySelectorAll("#pytorch-right-menu li"), - sections: document.querySelectorAll(".pytorch-article .section"), + sections: document.querySelectorAll(".pytorch-article section section, .sig.sig-object"), sectionIdTonavigationLink: {}, bind: function() { diff --git a/js/scroll-to-anchor.js b/js/scroll-to-anchor.js index da092e5c..dff467c4 100644 --- a/js/scroll-to-anchor.js +++ b/js/scroll-to-anchor.js @@ -11,11 +11,6 @@ window.scrollToAnchor = { var anchorScrolls = { ANCHOR_REGEX: /^#[^ ]+$/, - offsetHeightPx: function() { - var OFFSET_HEIGHT_PADDING = 20; - // TODO: this is a little janky. We should try to not rely on JS for this - return utilities.headersHeight() + OFFSET_HEIGHT_PADDING; - }, /** * Establish events, and fix initial scroll position if a hash is provided. @@ -23,19 +18,11 @@ window.scrollToAnchor = { init: function() { this.scrollToCurrent(); // This interferes with clicks below it, causing a double fire - // $(window).on('hashchange', $.proxy(this, 'scrollToCurrent')); + $(window).on('hashchange', $.proxy(this, 'scrollToCurrent')); $('body').on('click', 'a', $.proxy(this, 'delegateAnchors')); $('body').on('click', '#pytorch-right-menu li span', $.proxy(this, 'delegateSpans')); }, - /** - * Return the offset amount to deduct from the normal scroll position. - * Modify as appropriate to allow for dynamic calculations - */ - getFixedOffset: function() { - return this.offsetHeightPx(); - }, - /** * If the provided href is an anchor which resolves to an element on the * page, scroll to it. @@ -52,7 +39,7 @@ window.scrollToAnchor = { match = document.getElementById(href.slice(1)); if(match) { - var anchorOffset = $(match).offset().top - this.getFixedOffset(); + var anchorOffset = $(match).offset().top - utilities.getFixedOffset(); $('html, body').scrollTop(anchorOffset); diff --git a/js/side-menus.js b/js/side-menus.js index 9e4b0a79..6dcfd753 100644 --- a/js/side-menus.js +++ b/js/side-menus.js @@ -6,8 +6,6 @@ window.sideMenus = { isFixedToBottom: false, bind: function() { - sideMenus.handleLeftMenu(); - var rightMenuLinks = document.querySelectorAll("#pytorch-right-menu li"); var rightMenuHasLinks = rightMenuLinks.length > 1; @@ -72,6 +70,7 @@ window.sideMenus = { linkWithHash.nextElementSibling.children.length > 0 ) { linkWithHash.nextElementSibling.style.display = "block"; + linkWithHash.classList.remove("not-expanded"); linkWithHash.classList.add("expanded"); } @@ -92,7 +91,11 @@ window.sideMenus = { } }); - sideMenus.handleRightMenu(); + sideMenus.handleNavBar(); + sideMenus.handleLeftMenu(); + if (sideMenus.rightMenuIsOnScreen()) { + sideMenus.handleRightMenu(); + } } $(window).on('resize scroll', function(e) { @@ -183,12 +186,14 @@ window.sideMenus = { rightMenu.style.top = utilities.scrollTop() - mainHeaderHeight + "px"; rightMenu.classList.add("scrolling-absolute"); rightMenu.classList.remove("scrolling-fixed"); + document.getElementById("sphinx-template-shortcuts-wrapper").style.display = "none"; } } else { rightMenuWrapper.style.height = articleHeight + mainHeaderHeight + "px"; rightMenu.style.top = articleBottom - mainHeaderHeight - rightMenuList.offsetHeight + "px"; rightMenu.classList.add("scrolling-absolute"); + document.getElementById("sphinx-template-shortcuts-wrapper").style.display = "none"; } if (utilities.scrollTop() < articleBottom - rightMenuList.offsetHeight) { @@ -196,6 +201,7 @@ window.sideMenus = { rightMenu.style.top = ""; rightMenu.classList.remove("scrolling-absolute"); rightMenu.classList.add("scrolling-fixed"); + document.getElementById("sphinx-template-shortcuts-wrapper").style.display = "block"; } } diff --git a/js/theme.js b/js/theme.js index 1c44e34c..cc207796 100644 --- a/js/theme.js +++ b/js/theme.js @@ -134,7 +134,7 @@ function ThemeNav () { // Find associated id element, then its closest section // in the document and try with that one. var id_elt = $('.document [id="' + anchor.substring(1) + '"]'); - var closest_section = id_elt.closest('div.section'); + var closest_section = id_elt.closest('section section'); link = vmenu.find('[href="#' + closest_section.attr("id") + '"]'); if (link.length === 0) { // still not found in the sidebar. fall back to main section @@ -391,41 +391,39 @@ $(".stars-outer > i").on("click", function() { }) $("#pytorch-side-scroll-right li a").on("click", function (e) { - var href = $(this).attr("href"); + var href = $(this).attr("href").replaceAll('.', '\\.'); $('html, body').stop().animate({ - scrollTop: $(href).offset().top - 100 + scrollTop: $(href).offset().top - utilities.getFixedOffset() }, 850); e.preventDefault; }); -var lastId, - topMenu = $("#pytorch-side-scroll-right"), - topMenuHeight = topMenu.outerHeight() + 1, - // All sidenav items - menuItems = topMenu.find("a"), - // Anchors for menu items - scrollItems = menuItems.map(function () { - var item = $(this).attr("href"); - if (item.length) { - return item; - } - }); +topMenu = $("#sphinx-template-side-scroll-right"), +// All sidenav items +menuItems = topMenu.find("a[href^='#']"), +// Anchors for menu items +scrollItems = {}; +for (var i = 0; i < menuItems.length; i++) { + var ref = menuItems[i].getAttribute("href").replaceAll('.', '\\.'); + if (ref.length > 1 && $(ref).length) { + scrollItems[ref] = menuItems[i]; + } +} -$(window).scroll(function () { - var fromTop = $(this).scrollTop() + topMenuHeight; - var article = ".section"; +highlightCurrent = function() { + var article = Object.keys(scrollItems).join(', '); - $(article).each(function (i) { - var offsetScroll = $(this).offset().top - $(window).scrollTop(); + $(article).each(function () { + var offsetScroll = $(this).offset().top - $(window).scrollTop() - utilities.getFixedOffset(); if ( - offsetScroll <= topMenuHeight + 200 && - offsetScroll >= topMenuHeight - 200 && - scrollItems[i] == "#" + $(this).attr("id") && + offsetScroll <= 50 && + offsetScroll >= -50 && $(".hidden:visible") ) { $(menuItems).removeClass("side-scroll-highlight"); - $(menuItems[i]).addClass("side-scroll-highlight"); + utilities.makeHighlight(scrollItems['#' + this.id.replaceAll('.', '\\.')]); } }); -}); - +} +$(window).scroll(highlightCurrent); +$(document).ready(highlightCurrent) diff --git a/pytorch_sphinx_theme/static/css/theme.css b/pytorch_sphinx_theme/static/css/theme.css index 43cf163a..b09e4923 100644 --- a/pytorch_sphinx_theme/static/css/theme.css +++ b/pytorch_sphinx_theme/static/css/theme.css @@ -10514,7 +10514,7 @@ article.pytorch-article p.caption { margin-top: 1.25rem; } -article.pytorch-article .section:first-of-type h1:first-of-type { +article.pytorch-article section:first-of-type h1:first-of-type { margin-top: 0; } @@ -10601,7 +10601,7 @@ article.pytorch-article .sphx-glr-thumbcontainer .figure a.reference.internal { } } -article.pytorch-article .section :not(dt) > code { +article.pytorch-article section :not(dt) > code { color: #262626; border-top: solid 2px #ffffff; background-color: #ffffff; @@ -10610,7 +10610,7 @@ article.pytorch-article .section :not(dt) > code { -webkit-box-decoration-break: clone; box-decoration-break: clone; } -article.pytorch-article .section :not(dt) > code .pre { +article.pytorch-article section :not(dt) > code .pre { outline: 0px; padding: 0px; } diff --git a/pytorch_sphinx_theme/static/js/theme.js b/pytorch_sphinx_theme/static/js/theme.js index ceee8f06..5d92459a 100644 --- a/pytorch_sphinx_theme/static/js/theme.js +++ b/pytorch_sphinx_theme/static/js/theme.js @@ -99,6 +99,30 @@ window.utilities = { return window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight; + }, + + /** + * Return the offset amount to deduct from the normal scroll position. + * Modify as appropriate to allow for dynamic calculations + */ + getFixedOffset: function() { + var OFFSET_HEIGHT_PADDING = 20; + // TODO: this is a little janky. We should try to not rely on JS for this + return document.getElementById("sphinx-template-page-level-bar").offsetHeight + OFFSET_HEIGHT_PADDING; + }, + + findParent: function(item) { + return $(item).parent().parent().siblings("a.reference.internal") + }, + makeHighlight: function(item) { + if ($(item).hasClass("title-link")) { + return + } + $(item).addClass("side-scroll-highlight"); + var parent = utilities.findParent(item); + if (~parent.hasClass("title-link")) { + utilities.makeHighlight(parent) + } } } @@ -213,7 +237,7 @@ window.filterTags = { // Modified from https://stackoverflow.com/a/32396543 window.highlightNavigation = { navigationListItems: document.querySelectorAll("#pytorch-right-menu li"), - sections: document.querySelectorAll(".pytorch-article .section"), + sections: document.querySelectorAll(".pytorch-article section section, .sig.sig-object"), sectionIdTonavigationLink: {}, bind: function() { @@ -396,11 +420,6 @@ window.scrollToAnchor = { var anchorScrolls = { ANCHOR_REGEX: /^#[^ ]+$/, - offsetHeightPx: function() { - var OFFSET_HEIGHT_PADDING = 20; - // TODO: this is a little janky. We should try to not rely on JS for this - return utilities.headersHeight() + OFFSET_HEIGHT_PADDING; - }, /** * Establish events, and fix initial scroll position if a hash is provided. @@ -408,19 +427,11 @@ window.scrollToAnchor = { init: function() { this.scrollToCurrent(); // This interferes with clicks below it, causing a double fire - // $(window).on('hashchange', $.proxy(this, 'scrollToCurrent')); + $(window).on('hashchange', $.proxy(this, 'scrollToCurrent')); $('body').on('click', 'a', $.proxy(this, 'delegateAnchors')); $('body').on('click', '#pytorch-right-menu li span', $.proxy(this, 'delegateSpans')); }, - /** - * Return the offset amount to deduct from the normal scroll position. - * Modify as appropriate to allow for dynamic calculations - */ - getFixedOffset: function() { - return this.offsetHeightPx(); - }, - /** * If the provided href is an anchor which resolves to an element on the * page, scroll to it. @@ -437,7 +448,7 @@ window.scrollToAnchor = { match = document.getElementById(href.slice(1)); if(match) { - var anchorOffset = $(match).offset().top - this.getFixedOffset(); + var anchorOffset = $(match).offset().top - utilities.getFixedOffset(); $('html, body').scrollTop(anchorOffset); @@ -492,8 +503,6 @@ window.sideMenus = { isFixedToBottom: false, bind: function() { - sideMenus.handleLeftMenu(); - var rightMenuLinks = document.querySelectorAll("#pytorch-right-menu li"); var rightMenuHasLinks = rightMenuLinks.length > 1; @@ -558,6 +567,7 @@ window.sideMenus = { linkWithHash.nextElementSibling.children.length > 0 ) { linkWithHash.nextElementSibling.style.display = "block"; + linkWithHash.classList.remove("not-expanded"); linkWithHash.classList.add("expanded"); } @@ -578,7 +588,11 @@ window.sideMenus = { } }); - sideMenus.handleRightMenu(); + sideMenus.handleNavBar(); + sideMenus.handleLeftMenu(); + if (sideMenus.rightMenuIsOnScreen()) { + sideMenus.handleRightMenu(); + } } $(window).on('resize scroll', function(e) { @@ -669,12 +683,14 @@ window.sideMenus = { rightMenu.style.top = utilities.scrollTop() - mainHeaderHeight + "px"; rightMenu.classList.add("scrolling-absolute"); rightMenu.classList.remove("scrolling-fixed"); + document.getElementById("sphinx-template-shortcuts-wrapper").style.display = "none"; } } else { rightMenuWrapper.style.height = articleHeight + mainHeaderHeight + "px"; rightMenu.style.top = articleBottom - mainHeaderHeight - rightMenuList.offsetHeight + "px"; rightMenu.classList.add("scrolling-absolute"); + document.getElementById("sphinx-template-shortcuts-wrapper").style.display = "none"; } if (utilities.scrollTop() < articleBottom - rightMenuList.offsetHeight) { @@ -682,6 +698,7 @@ window.sideMenus = { rightMenu.style.top = ""; rightMenu.classList.remove("scrolling-absolute"); rightMenu.classList.add("scrolling-fixed"); + document.getElementById("sphinx-template-shortcuts-wrapper").style.display = "block"; } } @@ -829,7 +846,7 @@ function ThemeNav () { // Find associated id element, then its closest section // in the document and try with that one. var id_elt = $('.document [id="' + anchor.substring(1) + '"]'); - var closest_section = id_elt.closest('div.section'); + var closest_section = id_elt.closest('section section'); link = vmenu.find('[href="#' + closest_section.attr("id") + '"]'); if (link.length === 0) { // still not found in the sidebar. fall back to main section @@ -1086,43 +1103,42 @@ $(".stars-outer > i").on("click", function() { }) $("#pytorch-side-scroll-right li a").on("click", function (e) { - var href = $(this).attr("href"); + var href = $(this).attr("href").replaceAll('.', '\\.'); $('html, body').stop().animate({ scrollTop: $(href).offset().top - 100 }, 850); e.preventDefault; }); -var lastId, - topMenu = $("#pytorch-side-scroll-right"), - topMenuHeight = topMenu.outerHeight() + 1, - // All sidenav items - menuItems = topMenu.find("a"), - // Anchors for menu items - scrollItems = menuItems.map(function () { - var item = $(this).attr("href"); - if (item.length) { - return item; - } - }); +topMenu = $("#sphinx-template-side-scroll-right"), +// All sidenav items +menuItems = topMenu.find("a[href^='#']"), +// Anchors for menu items +scrollItems = {}; +for (var i = 0; i < menuItems.length; i++) { + var ref = menuItems[i].getAttribute("href").replaceAll('.', '\\.'); + if (ref.length > 1 && $(ref).length) { + scrollItems[ref] = menuItems[i]; + } +} -$(window).scroll(function () { - var fromTop = $(this).scrollTop() + topMenuHeight; - var article = ".section"; +highlightCurrent = function() { + var article = Object.keys(scrollItems).join(', '); - $(article).each(function (i) { - var offsetScroll = $(this).offset().top - $(window).scrollTop(); + $(article).each(function () { + var offsetScroll = $(this).offset().top - $(window).scrollTop() - utilities.getFixedOffset(); if ( - offsetScroll <= topMenuHeight + 200 && - offsetScroll >= topMenuHeight - 200 && - scrollItems[i] == "#" + $(this).attr("id") && + offsetScroll <= 50 && + offsetScroll >= -50 && $(".hidden:visible") ) { $(menuItems).removeClass("side-scroll-highlight"); - $(menuItems[i]).addClass("side-scroll-highlight"); + utilities.makeHighlight(scrollItems['#' + this.id.replaceAll('.', '\\.')]); } }); -}); +} +$(window).scroll(highlightCurrent); +$(document).ready(highlightCurrent) },{"jquery":"jquery"}]},{},[1,2,3,4,5,6,7,8,9,10,"pytorch-sphinx-theme"]);