Python Library Caching
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sidebar.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * sidebar.js
  3. * ~~~~~~~~~~
  4. *
  5. * This file is functionally identical to "sidebar.js" in Sphinx 5.0.
  6. * When support for Sphinx 4 and earlier is dropped from the theme,
  7. * this file can be removed.
  8. *
  9. * This script makes the Sphinx sidebar collapsible.
  10. *
  11. * .sphinxsidebar contains .sphinxsidebarwrapper. This script adds
  12. * in .sphinxsidebar, after .sphinxsidebarwrapper, the #sidebarbutton
  13. * used to collapse and expand the sidebar.
  14. *
  15. * When the sidebar is collapsed the .sphinxsidebarwrapper is hidden
  16. * and the width of the sidebar and the margin-left of the document
  17. * are decreased. When the sidebar is expanded the opposite happens.
  18. * This script saves a per-browser/per-session cookie used to
  19. * remember the position of the sidebar among the pages.
  20. * Once the browser is closed the cookie is deleted and the position
  21. * reset to the default (expanded).
  22. *
  23. * :copyright: Copyright 2007-2022 by the Sphinx team, see AUTHORS.
  24. * :license: BSD, see LICENSE for details.
  25. *
  26. */
  27. const initialiseSidebar = () => {
  28. // global elements used by the functions.
  29. const bodyWrapper = document.getElementsByClassName("bodywrapper")[0]
  30. const sidebar = document.getElementsByClassName("sphinxsidebar")[0]
  31. const sidebarWrapper = document.getElementsByClassName("sphinxsidebarwrapper")[0]
  32. // exit early if the document has no sidebar for some reason
  33. if (typeof sidebar === "undefined") {
  34. return
  35. }
  36. const sidebarButton = document.getElementById("sidebarbutton")
  37. const sidebarArrow = sidebarButton.querySelector('span')
  38. const collapse_sidebar = () => {
  39. bodyWrapper.style.marginLeft = ".8em"
  40. sidebar.style.width = ".8em"
  41. sidebarWrapper.style.display = "none"
  42. sidebarArrow.innerText = "»"
  43. sidebarButton.title = _("Expand sidebar")
  44. window.localStorage.setItem("sidebar", "collapsed")
  45. }
  46. const expand_sidebar = () => {
  47. bodyWrapper.style.marginLeft = ""
  48. sidebar.style.removeProperty("width")
  49. sidebarWrapper.style.display = ""
  50. sidebarArrow.innerText = "«"
  51. sidebarButton.title = _("Collapse sidebar")
  52. window.localStorage.setItem("sidebar", "expanded")
  53. }
  54. sidebarButton.addEventListener("click", () => {
  55. (sidebarWrapper.style.display === "none") ? expand_sidebar() : collapse_sidebar()
  56. })
  57. const sidebar_state = window.localStorage.getItem("sidebar")
  58. if (sidebar_state === "collapsed") {
  59. collapse_sidebar()
  60. }
  61. else if (sidebar_state === "expanded") {
  62. expand_sidebar()
  63. }
  64. }
  65. if (document.readyState !== "loading") {
  66. initialiseSidebar()
  67. }
  68. else {
  69. document.addEventListener("DOMContentLoaded", initialiseSidebar)
  70. }