-
-
Notifications
You must be signed in to change notification settings - Fork 32k
Docs: Move inline JavaScript to own file to reduce duplication #119541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
534e117
Move inline JavaScript to own file to reduce duplication
hugovk 622fdc7
Update indent to match existing
hugovk b35dd09
Merge remote-tracking branch 'upstream/main' into rtd-switcher
hugovk 6ad01db
Fix indent
hugovk f37acb7
Merge branch 'main' into rtd-switcher
hugovk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
function onSwitch(event) { | ||
const option = event.target.selectedIndex; | ||
const item = event.target.options[option]; | ||
window.location.href = item.dataset.url; | ||
} | ||
|
||
document.addEventListener("readthedocs-addons-data-ready", function(event) { | ||
const config = event.detail.data() | ||
|
||
// Add some mocked hardcoded versions pointing to the official | ||
// documentation while migrating to Read the Docs. | ||
// These are only for testing purposes. | ||
// TODO: remove them when managing all the versions on Read the Docs, | ||
// since all the "active, built and not hidden" versions will be shown automatically. | ||
let versions = config.versions.active.concat([ | ||
{ | ||
slug: "dev (3.14)", | ||
urls: { | ||
documentation: "https://docs.python.org/3.14/", | ||
} | ||
}, | ||
{ | ||
slug: "dev (3.13)", | ||
urls: { | ||
documentation: "https://docs.python.org/3.13/", | ||
} | ||
}, | ||
{ | ||
slug: "3.12", | ||
urls: { | ||
documentation: "https://docs.python.org/3.12/", | ||
} | ||
}, | ||
{ | ||
slug: "3.11", | ||
urls: { | ||
documentation: "https://docs.python.org/3.11/", | ||
} | ||
}, | ||
]); | ||
|
||
const versionSelect = ` | ||
<select id="version_select"> | ||
${ versions.map( | ||
(version) => ` | ||
<option | ||
value="${ version.slug }" | ||
${ config.versions.current.slug === version.slug ? 'selected="selected"' : '' } | ||
data-url="${ version.urls.documentation }"> | ||
${ version.slug } | ||
</option>` | ||
).join("\n") } | ||
</select> | ||
`; | ||
|
||
// Prepend the current language to the options on the selector | ||
let languages = config.projects.translations.concat(config.projects.current); | ||
languages = languages.sort((a, b) => a.language.name.localeCompare(b.language.name)); | ||
|
||
const languageSelect = ` | ||
<select id="language_select"> | ||
${ languages.map( | ||
(translation) => ` | ||
<option | ||
value="${ translation.slug }" | ||
${ config.projects.current.slug === translation.slug ? 'selected="selected"' : '' } | ||
data-url="${ translation.urls.documentation }"> | ||
${ translation.language.name } | ||
</option>` | ||
).join("\n") } | ||
</select> | ||
`; | ||
|
||
// Query all the placeholders because there are different ones for Desktop/Mobile | ||
const versionPlaceholders = document.querySelectorAll(".version_switcher_placeholder"); | ||
for (placeholder of versionPlaceholders) { | ||
placeholder.innerHTML = versionSelect; | ||
let selectElement = placeholder.querySelector("select"); | ||
selectElement.addEventListener("change", onSwitch); | ||
} | ||
|
||
const languagePlaceholders = document.querySelectorAll(".language_switcher_placeholder"); | ||
for (placeholder of languagePlaceholders) { | ||
placeholder.innerHTML = languageSelect; | ||
let selectElement = placeholder.querySelector("select"); | ||
selectElement.addEventListener("change", onSwitch); | ||
} | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.