forked from python/python-docs-theme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsidebar.js_t
114 lines (106 loc) · 3.85 KB
/
sidebar.js_t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* sidebar.js
* ~~~~~~~~~~
*
* This file is functionally identical to "sidebar.js" in Sphinx 5.0.
* When support for Sphinx 4 and earlier is dropped from the theme,
* this file can be removed.
*
* This script makes the Sphinx sidebar collapsible.
*
* .sphinxsidebar contains .sphinxsidebarwrapper. This script adds
* in .sphinxsidebar, 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).
*
* :copyright: Copyright 2007-2022 by the Sphinx team, see AUTHORS.
* :license: BSD, see LICENSE for details.
*
*/
const initialiseSidebar = () => {
const ngettext = Documentation.ngettext
// global elements used by the functions.
const bodyWrapper = document.querySelector(".bodywrapper")
const sidebar = document.querySelector(".sphinxsidebar")
const sidebarWrapper = document.querySelector(".sphinxsidebarwrapper")
// exit early if the document has no sidebar for some reason
if (!sidebar) {
return
}
{# Check if we need to dynamically insert the sidebar button.
# We prefer the ``sphinx_version_tuple`` variable, and if it is undefined we
# know we are running a Sphinx version older than 4.2.
#
# See: https://www.sphinx-doc.org/en/master/development/templating.html#sphinx_version_tuple
#}
{% if sphinx_version_tuple is defined and sphinx_version_tuple[0] >= 5 %}
const sidebarButton = document.getElementById("sidebarbutton")
{% else %}
// create the sidebar button element
const sidebarButton = document.createElement("div")
sidebarButton.id = "sidebarbutton"
{% endif %}
const sidebarMinWidth = 200
const sidebarMaxWidth = Math.round(0.5 * window.innerWidth)
sidebarbutton.innerHTML = ""
sidebarbutton.tabindex = "0" // make it focusable
sidebarbutton.role = "slider"
sidebarbutton.title = _("Resize sidebar")
sidebarbutton.style.cursor = "col-resize" // Set the cursor only if JS is enabled
sidebarbutton.setAttribute("aria-label", _("Resize sidebar by dragging"))
sidebarbutton.setAttribute(
"aria-valuetext",
ngettext(
"Sidebar width {count} pixel",
"Sidebar width {count} pixels",
sidebar.offsetWidth
).replace("{count}", sidebar.offsetWidth)
)
let clientX;
function onMouseMove(e) {
e.preventDefault()
const sidebarWidth = sidebar.offsetWidth
const newWidth = Math.max(
sidebarMinWidth,
Math.min(sidebarMaxWidth, sidebarWidth + e.clientX - clientX)
)
clientX = e.clientX
sidebar.style.width = `${newWidth}px`
bodyWrapper.style.marginLeft = `${newWidth}px`
window.localStorage.setItem("sidebar-width", newWidth)
}
sidebarButton.addEventListener("mousedown", e => {
e.preventDefault()
clientX = e.clientX
document.addEventListener("mousemove", onMouseMove)
document.addEventListener("mouseup", () => {
document.removeEventListener("mousemove", onMouseMove)
sidebarbutton.setAttribute(
"aria-valuetext",
ngettext(
"Sidebar width {count} pixel",
"Sidebar width {count} pixels",
sidebar.offsetWidth
).replace("{count}", sidebar.offsetWidth)
)
})
})
const sidebarWidth = parseInt(window.localStorage.getItem("sidebar-width"), 10)
if(Number.isFinite(sidebarWidth)) {
sidebar.style.width = `${sidebarWidth}px`
bodyWrapper.style.marginLeft = `${sidebarWidth}px`
}
}
if (document.readyState !== "loading") {
initialiseSidebar()
}
else {
document.addEventListener("DOMContentLoaded", initialiseSidebar)
}