Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit aa2fc81

Browse filesBrowse files
[3.12] Doc: Render version/language selector on Read the Docs (GH-116966) (#118038)
Co-authored-by: Manuel Kaufmann <humitos@gmail.com>
1 parent 5af8fca commit aa2fc81
Copy full SHA for aa2fc81

File tree

Expand file treeCollapse file tree

1 file changed

+86
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+86
-0
lines changed

‎Doc/tools/templates/layout.html

Copy file name to clipboardExpand all lines: Doc/tools/templates/layout.html
+86Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,90 @@
4141
{{ "}" }}
4242
</style>
4343
{{ super() }}
44+
45+
<meta name="readthedocs-addons-api-version" content="1">
46+
<script type="text/javascript">
47+
function onSwitch(event) {
48+
const option = event.target.selectedIndex;
49+
const item = event.target.options[option];
50+
window.location.href = item.dataset.url;
51+
}
52+
53+
document.addEventListener("readthedocs-addons-data-ready", function(event) {
54+
const config = event.detail.data()
55+
56+
// Add some mocked hardcoded versions pointing to the official
57+
// documentation while migrating to Read the Docs.
58+
// These are only for testing purposes.
59+
// TODO: remove them when managing all the versions on Read the Docs,
60+
// since all the "active, built and not hidden" versions will be shown automatically.
61+
let versions = config.versions.active.concat([
62+
{
63+
slug: "dev (3.13)",
64+
urls: {
65+
documentation: "https://docs.python.org/3.13/",
66+
}
67+
},
68+
{
69+
slug: "3.12",
70+
urls: {
71+
documentation: "https://docs.python.org/3.12/",
72+
}
73+
},
74+
{
75+
slug: "3.11",
76+
urls: {
77+
documentation: "https://docs.python.org/3.11/",
78+
}
79+
},
80+
]);
81+
82+
const versionSelect = `
83+
<select id="version_select">
84+
${ versions.map(
85+
(version) => `
86+
<option
87+
value="${ version.slug }"
88+
${ config.versions.current.slug === version.slug ? 'selected="selected"' : '' }
89+
data-url="${ version.urls.documentation }">
90+
${ version.slug }
91+
</option>`
92+
).join("\n") }
93+
</select>
94+
`;
95+
96+
// Prepend the current language to the options on the selector
97+
let languages = config.projects.translations.concat(config.projects.current);
98+
languages = languages.sort((a, b) => a.language.name.localeCompare(b.language.name));
99+
100+
const languageSelect = `
101+
<select id="language_select">
102+
${ languages.map(
103+
(translation) => `
104+
<option
105+
value="${ translation.slug }"
106+
${ config.projects.current.slug === translation.slug ? 'selected="selected"' : '' }
107+
data-url="${ translation.urls.documentation }">
108+
${ translation.language.name }
109+
</option>`
110+
).join("\n") }
111+
</select>
112+
`;
113+
114+
// Query all the placeholders because there are different ones for Desktop/Mobile
115+
const versionPlaceholders = document.querySelectorAll(".version_switcher_placeholder");
116+
for (placeholder of versionPlaceholders) {
117+
placeholder.innerHTML = versionSelect;
118+
let selectElement = placeholder.querySelector("select");
119+
selectElement.addEventListener("change", onSwitch);
120+
}
121+
122+
const languagePlaceholders = document.querySelectorAll(".language_switcher_placeholder");
123+
for (placeholder of languagePlaceholders) {
124+
placeholder.innerHTML = languageSelect;
125+
let selectElement = placeholder.querySelector("select");
126+
selectElement.addEventListener("change", onSwitch);
127+
}
128+
});
129+
</script>
44130
{% endblock %}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.