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 eea26c4

Browse filesBrowse files
authored
Docs: Move inline JavaScript to own file to reduce duplication (#119541)
1 parent 3dfa364 commit eea26c4
Copy full SHA for eea26c4

File tree

2 files changed

+90
-91
lines changed
Filter options

2 files changed

+90
-91
lines changed

‎Doc/tools/static/rtd_switcher.js

Copy file name to clipboard
+88Lines changed: 88 additions & 0 deletions
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

Copy file name to clipboardExpand all lines: Doc/tools/templates/layout.html
+2-91Lines changed: 2 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -43,96 +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.14)",
65-
urls: {
66-
documentation: "https://docs.python.org/3.14/",
67-
}
68-
},
69-
{
70-
slug: "pre (3.13)",
71-
urls: {
72-
documentation: "https://docs.python.org/3.13/",
73-
}
74-
},
75-
{
76-
slug: "3.12",
77-
urls: {
78-
documentation: "https://docs.python.org/3.12/",
79-
}
80-
},
81-
{
82-
slug: "3.11",
83-
urls: {
84-
documentation: "https://docs.python.org/3.11/",
85-
}
86-
},
87-
]);
88-
89-
const versionSelect = `
90-
<select id="version_select">
91-
${ versions.map(
92-
(version) => `
93-
<option
94-
value="${ version.slug }"
95-
${ config.versions.current.slug === version.slug ? 'selected="selected"' : '' }
96-
data-url="${ version.urls.documentation }">
97-
${ version.slug }
98-
</option>`
99-
).join("\n") }
100-
</select>
101-
`;
102-
103-
// Prepend the current language to the options on the selector
104-
let languages = config.projects.translations.concat(config.projects.current);
105-
languages = languages.sort((a, b) => a.language.name.localeCompare(b.language.name));
106-
107-
const languageSelect = `
108-
<select id="language_select">
109-
${ languages.map(
110-
(translation) => `
111-
<option
112-
value="${ translation.slug }"
113-
${ config.projects.current.slug === translation.slug ? 'selected="selected"' : '' }
114-
data-url="${ translation.urls.documentation }">
115-
${ translation.language.name }
116-
</option>`
117-
).join("\n") }
118-
</select>
119-
`;
120-
121-
// Query all the placeholders because there are different ones for Desktop/Mobile
122-
const versionPlaceholders = document.querySelectorAll(".version_switcher_placeholder");
123-
for (placeholder of versionPlaceholders) {
124-
placeholder.innerHTML = versionSelect;
125-
let selectElement = placeholder.querySelector("select");
126-
selectElement.addEventListener("change", onSwitch);
127-
}
128-
129-
const languagePlaceholders = document.querySelectorAll(".language_switcher_placeholder");
130-
for (placeholder of languagePlaceholders) {
131-
placeholder.innerHTML = languageSelect;
132-
let selectElement = placeholder.querySelector("select");
133-
selectElement.addEventListener("change", onSwitch);
134-
}
135-
});
136-
</script>
46+
<script type="text/javascript" src="{{ pathto('_static/rtd_switcher.js', 1) }}"></script>
47+
<meta name="readthedocs-addons-api-version" content="1">
13748
{%- endif %}
13849
{% endblock %}

0 commit comments

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