From 23c10834f6f9b189deea9c5d2464aa2d3ffd439b Mon Sep 17 00:00:00 2001 From: Adrien Dupuis <61695653+adriendupuis@users.noreply.github.com> Date: Fri, 19 Apr 2024 16:22:23 +0200 Subject: [PATCH] Fix version switcher (#291) --- docs/js/custom.js | 52 +++++++++++++++++++++------------------- docs/js/docs.switcher.js | 8 +++---- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/docs/js/custom.js b/docs/js/custom.js index 69faa931..96c528c5 100644 --- a/docs/js/custom.js +++ b/docs/js/custom.js @@ -1,4 +1,4 @@ -// tmp fix for read-the-docs embeded versions injection +// tmp fix for read-the-docs embedded versions injection let jquery = jQuery; $(document).ready(function() { @@ -35,36 +35,38 @@ $(document).ready(function() { // remove elements, leave only 'versions' var update = setInterval(function() { - if ($('.injected .rst-versions').length) { + let ready = false, version = ''; + if ($('readthedocs-flyout').length) { + $('dl.versions', $('readthedocs-flyout').prop('shadowRoot')).appendTo('.version-switcher .switcher__list'); + $('readthedocs-flyout').remove(); + version = $('.switcher__list dl.versions dd strong a').text(); + ready = true; + } + if (ready) { clearInterval(update); - var version = $('.rst-other-versions dd.rtd-current-item a').text(); - $('.rst-current-version span:first').html(' ' + (version != '' ? version : 'Change version')); - $('.rst-other-versions').html($('.injected dl:first').clone()); - $('.injected').remove(); - - //replace url in version switcher - var currentVersion = $('.rst-other-versions dd.rtd-current-item a').attr('href'), - resourceUrl = document.location.href.replace(currentVersion, ''); - - $('.rst-other-versions dd a').each(function() { - $(this).attr('href', $(this).attr('href') + resourceUrl); - }); - - if ($('.version-warning').length) { - var url, - version = $('.version-warning .version').html(), - parts = $('.rst-other-versions dd a') - .first() - .attr('href') - .split('/'); - parts[4] = version; - url = parts.join('/'); + if (!$('.rst-versions.switcher__selected-item').length) { + // add rst-current-version back (what removed it??) + $('.switcher.version-switcher').prepend(` +
+
+ Version +
+
+ `); + } + $('.rst-current-version.switcher__label').html(version.length ? version : 'Change version'); + $('.rst-other-versions.switcher__list dl.versions dd strong').parent().addClass('rtd-current-item'); - $('.version-warning .version').html($('' + version + '')); + if ('master' !== (vl = $('.rst-other-versions.switcher__list dl.versions')).find('dd:first').text()) { + vl.find('dd').each(function() {$(this).detach().prependTo(vl)}); } } }, 300); + setTimeout(function() { + clearInterval(update); + setSwitcherEvents(); + }, 1200); $('img').each(function() { if ($(this).attr('title')) { diff --git a/docs/js/docs.switcher.js b/docs/js/docs.switcher.js index 60939f57..78bced55 100644 --- a/docs/js/docs.switcher.js +++ b/docs/js/docs.switcher.js @@ -1,6 +1,6 @@ -(function (doc) { +function setSwitcherEvents() { const CLASS_EXPANDED_LIST = 'switcher__selected-item--expanded'; - const switchers = doc.querySelectorAll('.switcher'); + const switchers = window.document.querySelectorAll('.switcher'); const toggleListExpandedState = (event) => { event.currentTarget.classList.toggle(CLASS_EXPANDED_LIST); }; @@ -15,6 +15,6 @@ selectedItem.addEventListener('click', toggleListExpandedState, false); - doc.body.addEventListener('click', (event) => collapseList(event, switcher, selectedItem), false); + window.document.body.addEventListener('click', (event) => collapseList(event, switcher, selectedItem), false); }); -})(window.document); +};