Reticulum/docs/manual/_static/sidebar.js

70 lines
2.5 KiB
JavaScript
Raw Normal View History

2021-05-16 23:40:49 +02:00
/*
* sidebar.js
* ~~~~~~~~~~
*
* This script makes the Sphinx sidebar collapsible.
*
* .sphinxsidebar contains .sphinxsidebarwrapper. This script adds
* in .sphixsidebar, after .sphinxsidebarwrapper, the #sidebarbutton
* used to collapse and expand the sidebar.
*
* When the sidebar is collapsed the .sphinxsidebarwrapper is hidden
* and the width of the sidebar and the margin-left of the document
* are decreased. When the sidebar is expanded the opposite happens.
* This script saves a per-browser/per-session cookie used to
* remember the position of the sidebar among the pages.
* Once the browser is closed the cookie is deleted and the position
* reset to the default (expanded).
*
2022-09-30 00:02:15 +02:00
* :copyright: Copyright 2007-2022 by the Sphinx team, see AUTHORS.
2021-05-16 23:40:49 +02:00
* :license: BSD, see LICENSE for details.
*
*/
2022-09-30 00:02:15 +02:00
const initialiseSidebar = () => {
2021-05-16 23:40:49 +02:00
2022-09-30 00:02:15 +02:00
2021-05-16 23:40:49 +02:00
// global elements used by the functions.
2022-09-30 00:02:15 +02:00
const bodyWrapper = document.getElementsByClassName("bodywrapper")[0]
const sidebar = document.getElementsByClassName("sphinxsidebar")[0]
const sidebarWrapper = document.getElementsByClassName('sphinxsidebarwrapper')[0]
const sidebarButton = document.getElementById("sidebarbutton")
const sidebarArrow = sidebarButton.querySelector('span')
2021-05-16 23:40:49 +02:00
// for some reason, the document has no sidebar; do not run into errors
2022-09-30 00:02:15 +02:00
if (typeof sidebar === "undefined") return;
2021-05-16 23:40:49 +02:00
2022-09-30 00:02:15 +02:00
const flipArrow = element => element.innerText = (element.innerText === "»") ? "«" : "»"
2021-05-16 23:40:49 +02:00
2022-09-30 00:02:15 +02:00
const collapse_sidebar = () => {
bodyWrapper.style.marginLeft = ".8em";
sidebar.style.width = ".8em"
sidebarWrapper.style.display = "none"
flipArrow(sidebarArrow)
sidebarButton.title = _('Expand sidebar')
window.localStorage.setItem("sidebar", "collapsed")
2021-05-16 23:40:49 +02:00
}
2022-09-30 00:02:15 +02:00
const expand_sidebar = () => {
bodyWrapper.style.marginLeft = ""
sidebar.style.removeProperty("width")
sidebarWrapper.style.display = ""
flipArrow(sidebarArrow)
sidebarButton.title = _('Collapse sidebar')
window.localStorage.setItem("sidebar", "expanded")
2021-05-16 23:40:49 +02:00
}
2022-09-30 00:02:15 +02:00
sidebarButton.addEventListener("click", () => {
(sidebarWrapper.style.display === "none") ? expand_sidebar() : collapse_sidebar()
})
2021-05-16 23:40:49 +02:00
2022-09-30 00:02:15 +02:00
if (!window.localStorage.getItem("sidebar")) return
const value = window.localStorage.getItem("sidebar")
if (value === "collapsed") collapse_sidebar();
else if (value === "expanded") expand_sidebar();
}
2021-05-16 23:40:49 +02:00
2022-09-30 00:02:15 +02:00
if (document.readyState !== "loading") initialiseSidebar()
else document.addEventListener("DOMContentLoaded", initialiseSidebar)