@@ -288,7 +288,7 @@ var setupSearchButtons = () => {
288
288
*/
289
289
function checkPageExistsAndRedirect ( event ) {
290
290
const currentFilePath = `${ DOCUMENTATION_OPTIONS . pagename } .html` ;
291
- const tryUrl = event . target . getAttribute ( "href" ) ;
291
+ const tryUrl = event . currentTarget . getAttribute ( "href" ) ;
292
292
let otherDocsHomepage = tryUrl . replace ( currentFilePath , "" ) ;
293
293
294
294
fetch ( tryUrl , { method : "HEAD" } )
@@ -334,13 +334,15 @@ async function fetchVersionSwitcherJSON(url) {
334
334
}
335
335
336
336
// 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 ) {
339
341
const data = await fetchVersionSwitcherJSON (
340
342
DOCUMENTATION_OPTIONS . theme_switcher_json_url
341
343
) ;
342
344
const currentFilePath = `${ DOCUMENTATION_OPTIONS . pagename } .html` ;
343
- themeSwitchBtns . forEach ( ( btn ) => {
345
+ versionSwitcherBtns . forEach ( ( btn ) => {
344
346
// Set empty strings by default so that these attributes exist and can be used in CSS selectors
345
347
btn . dataset [ "activeVersionName" ] = "" ;
346
348
btn . dataset [ "activeVersion" ] = "" ;
@@ -351,42 +353,38 @@ if (themeSwitchBtns.length) {
351
353
if ( ! ( "name" in entry ) ) {
352
354
entry . name = entry . version ;
353
355
}
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.
355
380
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 ;
367
385
// on click, AJAX calls will check if the linked page exists before
368
386
// trying to redirect, and if not, will redirect to the homepage
369
387
// 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
-
390
388
menu . append ( node ) ;
391
389
} ) ;
392
390
} ) ;
0 commit comments