Skip to content

Commit 8e52eae

Browse files
committed
try adding onclick after clone
1 parent d7532dc commit 8e52eae

File tree

1 file changed

+34
-36
lines changed

1 file changed

+34
-36
lines changed

src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ var setupSearchButtons = () => {
288288
*/
289289
function checkPageExistsAndRedirect(event) {
290290
const currentFilePath = `${DOCUMENTATION_OPTIONS.pagename}.html`;
291-
const tryUrl = event.target.getAttribute("href");
291+
const tryUrl = event.currentTarget.getAttribute("href");
292292
let otherDocsHomepage = tryUrl.replace(currentFilePath, "");
293293

294294
fetch(tryUrl, { method: "HEAD" })
@@ -334,13 +334,15 @@ async function fetchVersionSwitcherJSON(url) {
334334
}
335335

336336
// Populate the version switcher from the JSON config file
337-
var themeSwitchBtns = document.querySelectorAll(".version-switcher__button");
338-
if (themeSwitchBtns.length) {
337+
var versionSwitcherBtns = document.querySelectorAll(
338+
".version-switcher__button"
339+
);
340+
if (versionSwitcherBtns.length) {
339341
const data = await fetchVersionSwitcherJSON(
340342
DOCUMENTATION_OPTIONS.theme_switcher_json_url
341343
);
342344
const currentFilePath = `${DOCUMENTATION_OPTIONS.pagename}.html`;
343-
themeSwitchBtns.forEach((btn) => {
345+
versionSwitcherBtns.forEach((btn) => {
344346
// Set empty strings by default so that these attributes exist and can be used in CSS selectors
345347
btn.dataset["activeVersionName"] = "";
346348
btn.dataset["activeVersion"] = "";
@@ -351,42 +353,38 @@ if (themeSwitchBtns.length) {
351353
if (!("name" in entry)) {
352354
entry.name = entry.version;
353355
}
354-
356+
// create the node
357+
const anchor = document.createElement("a");
358+
anchor.setAttribute("class", "list-group-item list-group-item-action py-1");
359+
anchor.setAttribute("href", `${entry.url}${currentFilePath}`);
360+
const span = document.createElement("span");
361+
span.textContent = `${entry.name}`;
362+
anchor.appendChild(span);
363+
// Add dataset values for the version and name in case people want
364+
// to apply CSS styling based on this information.
365+
anchor.dataset["versionName"] = entry.name;
366+
anchor.dataset["version"] = entry.version;
367+
// replace dropdown button text with the preferred display name of
368+
// this version, rather than using sphinx's {{ version }} variable.
369+
// also highlight the dropdown entry for the currently-viewed
370+
// version's entry
371+
if (entry.version == DOCUMENTATION_OPTIONS.version_switcher_version_match) {
372+
anchor.classList.add("active");
373+
versionSwitcherBtns.forEach((btn) => {
374+
btn.innerText = btn.dataset["activeVersionName"] = entry.name;
375+
btn.dataset["activeVersion"] = entry.version;
376+
});
377+
}
378+
// There may be multiple version-switcher elements, e.g. one
379+
// in a slide-over panel displayed on smaller screens.
355380
document.querySelectorAll(".version-switcher__menu").forEach((menu) => {
356-
// There may be multiple version-switcher elements, e.g. one
357-
// in a slide-over panel displayed on smaller screens.
358-
// create the node
359-
const span = document.createElement("span");
360-
span.textContent = `${entry.name}`;
361-
362-
const node = document.createElement("a");
363-
node.setAttribute("class", "list-group-item list-group-item-action py-1");
364-
node.setAttribute("href", `${entry.url}${currentFilePath}`);
365-
node.appendChild(span);
366-
381+
// we need to clone the node for each menu, but onclick attributes are not
382+
// preserved by `.cloneNode()` so we add onclick here after cloning.
383+
let node = anchor.cloneNode(true);
384+
node.onclick = checkPageExistsAndRedirect;
367385
// on click, AJAX calls will check if the linked page exists before
368386
// trying to redirect, and if not, will redirect to the homepage
369387
// for that version of the docs.
370-
node.onclick = checkPageExistsAndRedirect;
371-
// Add dataset values for the version and name in case people want
372-
// to apply CSS styling based on this information.
373-
node.dataset["versionName"] = entry.name;
374-
node.dataset["version"] = entry.version;
375-
376-
// replace dropdown button text with the preferred display name of
377-
// this version, rather than using sphinx's {{ version }} variable.
378-
// also highlight the dropdown entry for the currently-viewed
379-
// version's entry
380-
if (
381-
entry.version == DOCUMENTATION_OPTIONS.version_switcher_version_match
382-
) {
383-
node.classList.add("active");
384-
themeSwitchBtns.forEach((btn) => {
385-
btn.innerText = btn.dataset["activeVersionName"] = entry.name;
386-
btn.dataset["activeVersion"] = entry.version;
387-
});
388-
}
389-
390388
menu.append(node);
391389
});
392390
});

0 commit comments

Comments
 (0)