@@ -66,26 +66,18 @@ function showMain() {
6666( function ( ) {
6767 window . rootPath = getVar ( "root-path" ) ;
6868 window . currentCrate = getVar ( "current-crate" ) ;
69- window . searchJS = resourcePath ( "search" , ".js" ) ;
70- window . searchIndexJS = resourcePath ( "search-index" , ".js" ) ;
71- window . settingsJS = resourcePath ( "settings" , ".js" ) ;
72- const sidebarVars = document . getElementById ( "sidebar-vars" ) ;
73- if ( sidebarVars ) {
74- window . sidebarCurrent = {
75- name : sidebarVars . attributes [ "data-name" ] . value ,
76- ty : sidebarVars . attributes [ "data-ty" ] . value ,
77- relpath : sidebarVars . attributes [ "data-relpath" ] . value ,
78- } ;
79- // FIXME: It would be nicer to generate this text content directly in HTML,
80- // but with the current code it's hard to get the right information in the right place.
81- const mobileLocationTitle = document . querySelector ( ".mobile-topbar h2.location" ) ;
82- const locationTitle = document . querySelector ( ".sidebar h2.location" ) ;
83- if ( mobileLocationTitle && locationTitle ) {
84- mobileLocationTitle . innerHTML = locationTitle . innerHTML ;
85- }
86- }
8769} ( ) ) ;
8870
71+ function setMobileTopbar ( ) {
72+ // FIXME: It would be nicer to generate this text content directly in HTML,
73+ // but with the current code it's hard to get the right information in the right place.
74+ const mobileLocationTitle = document . querySelector ( ".mobile-topbar h2.location" ) ;
75+ const locationTitle = document . querySelector ( ".sidebar h2.location" ) ;
76+ if ( mobileLocationTitle && locationTitle ) {
77+ mobileLocationTitle . innerHTML = locationTitle . innerHTML ;
78+ }
79+ }
80+
8981// Gets the human-readable string for the virtual-key code of the
9082// given KeyboardEvent, ev.
9183//
@@ -227,7 +219,7 @@ function loadCss(cssFileName) {
227219 // Sending request for the CSS and the JS files at the same time so it will
228220 // hopefully be loaded when the JS will generate the settings content.
229221 loadCss ( "settings" ) ;
230- loadScript ( window . settingsJS ) ;
222+ loadScript ( resourcePath ( "settings" , ".js" ) ) ;
231223 } ;
232224
233225 window . searchState = {
@@ -304,8 +296,8 @@ function loadCss(cssFileName) {
304296 function loadSearch ( ) {
305297 if ( ! searchLoaded ) {
306298 searchLoaded = true ;
307- loadScript ( window . searchJS ) ;
308- loadScript ( window . searchIndexJS ) ;
299+ loadScript ( resourcePath ( "search" , ".js" ) ) ;
300+ loadScript ( resourcePath ( "search-index" , ".js" ) ) ;
309301 }
310302 }
311303
@@ -485,40 +477,11 @@ function loadCss(cssFileName) {
485477 document . addEventListener ( "keypress" , handleShortcut ) ;
486478 document . addEventListener ( "keydown" , handleShortcut ) ;
487479
488- // delayed sidebar rendering.
489- window . initSidebarItems = items => {
490- const sidebar = document . getElementsByClassName ( "sidebar-elems" ) [ 0 ] ;
491- let others ;
492- const current = window . sidebarCurrent ;
493-
494- function addSidebarCrates ( crates ) {
495- if ( ! hasClass ( document . body , "crate" ) ) {
496- // We only want to list crates on the crate page.
497- return ;
498- }
499- // Draw a convenient sidebar of known crates if we have a listing
500- const div = document . createElement ( "div" ) ;
501- div . className = "block crate" ;
502- div . innerHTML = "<h3>Crates</h3>" ;
503- const ul = document . createElement ( "ul" ) ;
504- div . appendChild ( ul ) ;
505-
506- for ( const crate of crates ) {
507- let klass = "crate" ;
508- if ( window . rootPath !== "./" && crate === window . currentCrate ) {
509- klass += " current" ;
510- }
511- const link = document . createElement ( "a" ) ;
512- link . href = window . rootPath + crate + "/index.html" ;
513- link . className = klass ;
514- link . textContent = crate ;
515-
516- const li = document . createElement ( "li" ) ;
517- li . appendChild ( link ) ;
518- ul . appendChild ( li ) ;
519- }
520- others . appendChild ( div ) ;
480+ function addSidebarItems ( ) {
481+ if ( ! window . SIDEBAR_ITEMS ) {
482+ return ;
521483 }
484+ const sidebar = document . getElementsByClassName ( "sidebar-elems" ) [ 0 ] ;
522485
523486 /**
524487 * Append to the sidebar a "block" of links - a heading along with a list (`<ul>`) of items.
@@ -529,7 +492,7 @@ function loadCss(cssFileName) {
529492 * "Modules", or "Macros".
530493 */
531494 function block ( shortty , id , longty ) {
532- const filtered = items [ shortty ] ;
495+ const filtered = window . SIDEBAR_ITEMS [ shortty ] ;
533496 if ( ! filtered ) {
534497 return ;
535498 }
@@ -546,17 +509,18 @@ function loadCss(cssFileName) {
546509 const desc = item [ 1 ] ; // can be null
547510
548511 let klass = shortty ;
549- if ( name === current . name && shortty === current . ty ) {
550- klass += " current" ;
551- }
552512 let path ;
553513 if ( shortty === "mod" ) {
554514 path = name + "/index.html" ;
555515 } else {
556516 path = shortty + "." + name + ".html" ;
557517 }
518+ const current_page = document . location . href . split ( "/" ) . pop ( ) ;
519+ if ( path === current_page ) {
520+ klass += " current" ;
521+ }
558522 const link = document . createElement ( "a" ) ;
559- link . href = current . relpath + path ;
523+ link . href = path ;
560524 link . title = desc ;
561525 link . className = klass ;
562526 link . textContent = name ;
@@ -565,14 +529,10 @@ function loadCss(cssFileName) {
565529 ul . appendChild ( li ) ;
566530 }
567531 div . appendChild ( ul ) ;
568- others . appendChild ( div ) ;
532+ sidebar . appendChild ( div ) ;
569533 }
570534
571535 if ( sidebar ) {
572- others = document . createElement ( "div" ) ;
573- others . className = "others" ;
574- sidebar . appendChild ( others ) ;
575-
576536 const isModule = hasClass ( document . body , "mod" ) ;
577537 if ( ! isModule ) {
578538 block ( "primitive" , "primitives" , "Primitive Types" ) ;
@@ -590,12 +550,8 @@ function loadCss(cssFileName) {
590550 block ( "keyword" , "keywords" , "Keywords" ) ;
591551 block ( "traitalias" , "trait-aliases" , "Trait Aliases" ) ;
592552 }
593-
594- // `crates{version}.js` should always be loaded before this script, so we can use
595- // it safely.
596- addSidebarCrates ( window . ALL_CRATES ) ;
597553 }
598- } ;
554+ }
599555
600556 window . register_implementors = imp => {
601557 const implementors = document . getElementById ( "implementors-list" ) ;
@@ -680,6 +636,39 @@ function loadCss(cssFileName) {
680636 window . register_implementors ( window . pending_implementors ) ;
681637 }
682638
639+ function addSidebarCrates ( ) {
640+ if ( ! window . ALL_CRATES ) {
641+ return ;
642+ }
643+ const sidebarElems = document . getElementsByClassName ( "sidebar-elems" ) [ 0 ] ;
644+ if ( ! sidebarElems ) {
645+ return ;
646+ }
647+ // Draw a convenient sidebar of known crates if we have a listing
648+ const div = document . createElement ( "div" ) ;
649+ div . className = "block crate" ;
650+ div . innerHTML = "<h3>Crates</h3>" ;
651+ const ul = document . createElement ( "ul" ) ;
652+ div . appendChild ( ul ) ;
653+
654+ for ( const crate of window . ALL_CRATES ) {
655+ let klass = "crate" ;
656+ if ( window . rootPath !== "./" && crate === window . currentCrate ) {
657+ klass += " current" ;
658+ }
659+ const link = document . createElement ( "a" ) ;
660+ link . href = window . rootPath + crate + "/index.html" ;
661+ link . className = klass ;
662+ link . textContent = crate ;
663+
664+ const li = document . createElement ( "li" ) ;
665+ li . appendChild ( link ) ;
666+ ul . appendChild ( li ) ;
667+ }
668+ sidebarElems . appendChild ( div ) ;
669+ }
670+
671+
683672 function labelForToggleButton ( sectionIsCollapsed ) {
684673 if ( sectionIsCollapsed ) {
685674 // button will expand the section
@@ -924,6 +913,9 @@ function loadCss(cssFileName) {
924913 buildHelperPopup = ( ) => { } ;
925914 } ;
926915
916+ setMobileTopbar ( ) ;
917+ addSidebarItems ( ) ;
918+ addSidebarCrates ( ) ;
927919 onHashChange ( null ) ;
928920 window . addEventListener ( "hashchange" , onHashChange ) ;
929921 searchState . setup ( ) ;
0 commit comments