Skip to content

Commit 5b634d5

Browse files
adriendupuisdabrt
authored andcommitted
Fix version switcher (#291)
1 parent 3858ca3 commit 5b634d5

File tree

2 files changed

+31
-29
lines changed

2 files changed

+31
-29
lines changed

docs/js/custom.js

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// tmp fix for read-the-docs embeded versions injection
1+
// tmp fix for read-the-docs embedded versions injection
22
let jquery = jQuery;
33

44
$(document).ready(function() {
@@ -35,36 +35,38 @@ $(document).ready(function() {
3535

3636
// remove elements, leave only 'versions'
3737
var update = setInterval(function() {
38-
if ($('.injected .rst-versions').length) {
38+
let ready = false, version = '';
39+
if ($('readthedocs-flyout').length) {
40+
$('dl.versions', $('readthedocs-flyout').prop('shadowRoot')).appendTo('.version-switcher .switcher__list');
41+
$('readthedocs-flyout').remove();
42+
version = $('.switcher__list dl.versions dd strong a').text();
43+
ready = true;
44+
}
45+
if (ready) {
3946
clearInterval(update);
40-
var version = $('.rst-other-versions dd.rtd-current-item a').text();
41-
$('.rst-current-version span:first').html(' ' + (version != '' ? version : 'Change version'));
42-
$('.rst-other-versions').html($('.injected dl:first').clone());
43-
$('.injected').remove();
44-
45-
//replace url in version switcher
46-
var currentVersion = $('.rst-other-versions dd.rtd-current-item a').attr('href'),
47-
resourceUrl = document.location.href.replace(currentVersion, '');
48-
49-
$('.rst-other-versions dd a').each(function() {
50-
$(this).attr('href', $(this).attr('href') + resourceUrl);
51-
});
52-
53-
if ($('.version-warning').length) {
54-
var url,
55-
version = $('.version-warning .version').html(),
56-
parts = $('.rst-other-versions dd a')
57-
.first()
58-
.attr('href')
59-
.split('/');
6047

61-
parts[4] = version;
62-
url = parts.join('/');
48+
if (!$('.rst-versions.switcher__selected-item').length) {
49+
// add rst-current-version back (what removed it??)
50+
$('.switcher.version-switcher').prepend(`
51+
<div class="rst-versions switcher__selected-item" data-toggle="rst-versions" role="note" aria-label="versions">
52+
<div class="rst-current-version switcher__label" data-toggle="rst-current-version">
53+
Version
54+
</div>
55+
</div>
56+
`);
57+
}
58+
$('.rst-current-version.switcher__label').html(version.length ? version : 'Change version');
59+
$('.rst-other-versions.switcher__list dl.versions dd strong').parent().addClass('rtd-current-item');
6360

64-
$('.version-warning .version').html($('<a href ="' + url + '" class="external">' + version + '</a>'));
61+
if ('master' !== (vl = $('.rst-other-versions.switcher__list dl.versions')).find('dd:first').text()) {
62+
vl.find('dd').each(function() {$(this).detach().prependTo(vl)});
6563
}
6664
}
6765
}, 300);
66+
setTimeout(function() {
67+
clearInterval(update);
68+
setSwitcherEvents();
69+
}, 1200);
6870

6971
$('img').each(function() {
7072
if ($(this).attr('title')) {

docs/js/docs.switcher.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
(function (doc) {
1+
function setSwitcherEvents() {
22
const CLASS_EXPANDED_LIST = 'switcher__selected-item--expanded';
3-
const switchers = doc.querySelectorAll('.switcher');
3+
const switchers = window.document.querySelectorAll('.switcher');
44
const toggleListExpandedState = (event) => {
55
event.currentTarget.classList.toggle(CLASS_EXPANDED_LIST);
66
};
@@ -15,6 +15,6 @@
1515

1616
selectedItem.addEventListener('click', toggleListExpandedState, false);
1717

18-
doc.body.addEventListener('click', (event) => collapseList(event, switcher, selectedItem), false);
18+
window.document.body.addEventListener('click', (event) => collapseList(event, switcher, selectedItem), false);
1919
});
20-
})(window.document);
20+
};

0 commit comments

Comments
 (0)