Skip to content

Commit

Permalink
HTML layout rework
Browse files Browse the repository at this point in the history
  • Loading branch information
humitos committed Sep 17, 2024
1 parent 6ced13d commit ca7f615
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 48 deletions.
8 changes: 2 additions & 6 deletions sphinx_rtd_theme/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,8 @@
{%- endif %}

{%- if READTHEDOCS %}
<div class="dropdown-selectors">
{%- if theme_version_selector %}
<select id="readthedocs-version-selector"></select>
{%- endif %}
{%- if theme_language_selector %}
<select id="readthedocs-language-selector"></select>
{%- if theme_version_selector or theme_language_selector %}
<div class="dropdown-selectors"></div>
{%- endif %}
</div>
{%- endif %}
Expand Down
79 changes: 37 additions & 42 deletions sphinx_rtd_theme/static/js/versions.js_t
Original file line number Diff line number Diff line change
Expand Up @@ -137,36 +137,28 @@ function onSelectorSwitch(event) {
document.addEventListener("readthedocs-addons-data-ready", function(event) {
const config = event.detail.data();

{%- if theme_version_selector %}
const versionOptions = `
${ config.versions.active.map(
(version) => `
<option
value="${ version.slug }"
${ config.versions.current.slug === version.slug ? 'selected="selected"' : '' }
data-url="${ version.urls.documentation }">
${ version.slug }
</option>`
).join("\n") }
`;

// Prepend the current version to the options if it's hidden since it won't
// appear in the "config.versions.active" list
let currentHiddenVersion = "";
if (config.versions.current.hidden) {
currentHiddenVersion = `
<option
value="${ config.versions.current.slug }"
selected="selected"
data-url="${ config.versions.current.urls.documentation }">
${ config.versions.current.slug }
</option>
`;
}
const dropdownSelectors = document.querySelector("div.dropdown-selectors");

const versionSelect = document.querySelector("#readthedocs-version-selector");
versionSelect.addEventListener("change", onSelectorSwitch);
versionSelect.innerHTML = currentHiddenVersion + versionOptions;
{%- if theme_version_selector %}
const versionSelect = `
<select id="readthedocs-version-selector">
${ config.versions.active.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>
`;

dropdownSelectors.insertAdjacentHTML("beforeend", versionSelect);

const versionSelector = document.querySelector("#readthedocs-version-selector");
versionSelector.addEventListener("change", onSelectorSwitch);
versionSelector.innerHTML = versionSelect;
{%- endif %}


Expand All @@ -176,21 +168,24 @@ document.addEventListener("readthedocs-addons-data-ready", function(event) {
let languages = config.projects.translations.concat(config.projects.current);
languages = languages.sort((a, b) => a.language.name.localeCompare(b.language.name));

const languageOptions = `
${ languages.map(
(language) => `
<option
value="${ language.language.code }"
${ config.projects.current.slug === language.slug ? 'selected="selected"' : '' }
data-url="${ language.urls.documentation }">
${ language.language.name }
</option>`
).join("\n") }
const languageSelect= `
<select id="readthedocs-version-selector">
${ languages.map(
(language) => `
<option
value="${ language.language.code }"
${ config.projects.current.slug === language.slug ? 'selected="selected"' : '' }
data-url="${ language.urls.documentation }">
${ language.language.name }
</option>`
).join("\n") }
</select>
`;

const languageSelect = document.querySelector("#readthedocs-language-selector");
languageSelect.addEventListener("change", onSelectorSwitch);
languageSelect.innerHTML = languageOptions;
dropdownSelectors.insertAdjacentHTML("beforeend", languageSelect);
const languageSelector = document.querySelector("#readthedocs-language-selector");
languageSelector.addEventListener("change", onSelectorSwitch);
languageSelector.innerHTML = languageSelect;
}
{%- endif %}
});
Expand Down

0 comments on commit ca7f615

Please sign in to comment.