Skip to content

Commit 0a4a318

Browse files
authored
[3.13] Docs: Move inline JavaScript to own file to reduce duplication (GH-119541) (#119630)
1 parent 8117cb5 commit 0a4a318

File tree

2 files changed

+90
-85
lines changed

2 files changed

+90
-85
lines changed

Doc/tools/static/rtd_switcher.js

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
function onSwitch(event) {
2+
const option = event.target.selectedIndex;
3+
const item = event.target.options[option];
4+
window.location.href = item.dataset.url;
5+
}
6+
7+
document.addEventListener("readthedocs-addons-data-ready", function(event) {
8+
const config = event.detail.data()
9+
10+
// Add some mocked hardcoded versions pointing to the official
11+
// documentation while migrating to Read the Docs.
12+
// These are only for testing purposes.
13+
// TODO: remove them when managing all the versions on Read the Docs,
14+
// since all the "active, built and not hidden" versions will be shown automatically.
15+
let versions = config.versions.active.concat([
16+
{
17+
slug: "dev (3.14)",
18+
urls: {
19+
documentation: "https://docs.python.org/3.14/",
20+
}
21+
},
22+
{
23+
slug: "dev (3.13)",
24+
urls: {
25+
documentation: "https://docs.python.org/3.13/",
26+
}
27+
},
28+
{
29+
slug: "3.12",
30+
urls: {
31+
documentation: "https://docs.python.org/3.12/",
32+
}
33+
},
34+
{
35+
slug: "3.11",
36+
urls: {
37+
documentation: "https://docs.python.org/3.11/",
38+
}
39+
},
40+
]);
41+
42+
const versionSelect = `
43+
<select id="version_select">
44+
${ versions.map(
45+
(version) => `
46+
<option
47+
value="${ version.slug }"
48+
${ config.versions.current.slug === version.slug ? 'selected="selected"' : '' }
49+
data-url="${ version.urls.documentation }">
50+
${ version.slug }
51+
</option>`
52+
).join("\n") }
53+
</select>
54+
`;
55+
56+
// Prepend the current language to the options on the selector
57+
let languages = config.projects.translations.concat(config.projects.current);
58+
languages = languages.sort((a, b) => a.language.name.localeCompare(b.language.name));
59+
60+
const languageSelect = `
61+
<select id="language_select">
62+
${ languages.map(
63+
(translation) => `
64+
<option
65+
value="${ translation.slug }"
66+
${ config.projects.current.slug === translation.slug ? 'selected="selected"' : '' }
67+
data-url="${ translation.urls.documentation }">
68+
${ translation.language.name }
69+
</option>`
70+
).join("\n") }
71+
</select>
72+
`;
73+
74+
// Query all the placeholders because there are different ones for Desktop/Mobile
75+
const versionPlaceholders = document.querySelectorAll(".version_switcher_placeholder");
76+
for (placeholder of versionPlaceholders) {
77+
placeholder.innerHTML = versionSelect;
78+
let selectElement = placeholder.querySelector("select");
79+
selectElement.addEventListener("change", onSwitch);
80+
}
81+
82+
const languagePlaceholders = document.querySelectorAll(".language_switcher_placeholder");
83+
for (placeholder of languagePlaceholders) {
84+
placeholder.innerHTML = languageSelect;
85+
let selectElement = placeholder.querySelector("select");
86+
selectElement.addEventListener("change", onSwitch);
87+
}
88+
});

Doc/tools/templates/layout.html

+2-85
Original file line numberDiff line numberDiff line change
@@ -43,90 +43,7 @@
4343
{{ super() }}
4444

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

0 commit comments

Comments
 (0)