Skip to content

Commit

Permalink
Fix version switcher (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
adriendupuis authored and dabrt committed May 28, 2024
1 parent 6bc4179 commit 23c1083
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
52 changes: 27 additions & 25 deletions docs/js/custom.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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(`
<div class="rst-versions switcher__selected-item" data-toggle="rst-versions" role="note" aria-label="versions">
<div class="rst-current-version switcher__label" data-toggle="rst-current-version">
Version
</div>
</div>
`);
}
$('.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($('<a href ="' + url + '" class="external">' + version + '</a>'));
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')) {
Expand Down
8 changes: 4 additions & 4 deletions docs/js/docs.switcher.js
Original file line number Diff line number Diff line change
@@ -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);
};
Expand All @@ -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);
};

0 comments on commit 23c1083

Please sign in to comment.