Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 40a3de3

Browse files
committed
Drop jQuery in sidebars.js
1 parent b0c184d commit 40a3de3

File tree

3 files changed

+94
-118
lines changed

3 files changed

+94
-118
lines changed

python_docs_theme/static/pydoctheme.css

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,12 @@ div.sphinxsidebar h4 {
104104
}
105105

106106
div.sphinxsidebarwrapper {
107+
width: 217px;
107108
box-sizing: border-box;
108109
height: 100%;
109110
overflow-x: hidden;
110111
overflow-y: auto;
112+
float: left;
111113
}
112114

113115
div.sphinxsidebarwrapper > h3:first-child {
@@ -135,6 +137,33 @@ div.sphinxsidebar input[type='text'] {
135137
max-width: 150px;
136138
}
137139

140+
#sidebarbutton {
141+
/* Sphinx 4.x and earlier compat */
142+
height: 100%;
143+
background-color: #CCCCCC;
144+
margin-left: 0;
145+
color: #444444;
146+
font-size: 1.2em;
147+
cursor: pointer;
148+
padding-top: 1px;
149+
float: right;
150+
display: table;
151+
/* after Sphinx 4.x and earlier is dropped, only the below is needed */
152+
width: 12px;
153+
border-radius: 0 5px 5px 0;
154+
border-left: none;
155+
}
156+
157+
#sidebarbutton span {
158+
/* Sphinx 4.x and earlier compat */
159+
display: table-cell;
160+
vertical-align: middle;
161+
}
162+
163+
#sidebarbutton:hover {
164+
background-color: #AAAAAA;
165+
}
166+
138167
div.body {
139168
padding: 0 0 0 1.2em;
140169
}

python_docs_theme/static/sidebar.js

Lines changed: 64 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -2,142 +2,88 @@
22
* sidebar.js
33
* ~~~~~~~~~~
44
*
5-
* This script makes the Sphinx sidebar collapsible. This is a slightly
6-
* modified version of Sphinx's own sidebar.js.
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.
78
*
8-
* .sphinxsidebar contains .sphinxsidebarwrapper. This script adds in
9-
* .sphixsidebar, after .sphinxsidebarwrapper, the #sidebarbutton used to
10-
* collapse and expand the sidebar.
9+
* This script makes the Sphinx sidebar collapsible.
1110
*
12-
* When the sidebar is collapsed the .sphinxsidebarwrapper is hidden and the
13-
* width of the sidebar and the margin-left of the document are decreased.
14-
* When the sidebar is expanded the opposite happens. This script saves a
15-
* per-browser/per-session cookie used to remember the position of the sidebar
16-
* among the pages. Once the browser is closed the cookie is deleted and the
17-
* position reset to the default (expanded).
11+
* .sphinxsidebar contains .sphinxsidebarwrapper. This script adds
12+
* in .sphinxsidebar, after .sphinxsidebarwrapper, the #sidebarbutton
13+
* used to collapse and expand the sidebar.
1814
*
19-
* :copyright: Copyright 2007-2011 by the Sphinx team, see AUTHORS.
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.
2024
* :license: BSD, see LICENSE for details.
2125
*
2226
*/
2327

24-
$(function() {
28+
const initialiseSidebar = () => {
2529
// global elements used by the functions.
26-
// the 'sidebarbutton' element is defined as global after its
27-
// creation, in the add_sidebar_button function
28-
var bodywrapper = $('.bodywrapper');
29-
var sidebar = $('.sphinxsidebar');
30-
var sidebarwrapper = $('.sphinxsidebarwrapper');
30+
const bodyWrapper = document.getElementsByClassName("bodywrapper")[0]
31+
const sidebar = document.getElementsByClassName("sphinxsidebar")[0]
32+
const sidebarWrapper = document.getElementsByClassName("sphinxsidebarwrapper")[0]
3133

32-
// original margin-left of the bodywrapper and width of the sidebar
33-
// with the sidebar expanded
34-
var bw_margin_expanded = bodywrapper.css('margin-left');
35-
var ssb_width_expanded = sidebar.width();
34+
// exit early if the document has no sidebar for some reason
35+
if (typeof sidebar === "undefined") {
36+
return
37+
}
3638

37-
// margin-left of the bodywrapper and width of the sidebar
38-
// with the sidebar collapsed
39-
var bw_margin_collapsed = '.8em';
40-
var ssb_width_collapsed = '.8em';
39+
// create the sidebar button element
40+
const sidebarButton = document.createElement("div")
41+
sidebarButton.id = "sidebarbutton"
42+
// create the sidebar button arrow element
43+
const sidebarArrow = document.createElement("span")
44+
sidebarArrow.innerText = "«"
45+
sidebarButton.appendChild(sidebarArrow)
46+
sidebar.appendChild(sidebarButton)
4147

42-
// colors used by the current theme
43-
var dark_color = '#AAAAAA';
44-
var light_color = '#CCCCCC';
48+
const flipArrow = element => element.innerText = (element.innerText === "»") ? "«" : "»"
4549

46-
function sidebar_is_collapsed() {
47-
return sidebarwrapper.is(':not(:visible)');
50+
const collapse_sidebar = () => {
51+
bodyWrapper.style.marginLeft = ".8em"
52+
sidebar.style.width = ".8em"
53+
sidebarWrapper.style.display = "none"
54+
flipArrow(sidebarArrow)
55+
sidebarButton.title = _("Expand sidebar")
56+
window.localStorage.setItem("sidebar", "collapsed")
4857
}
4958

50-
function toggle_sidebar() {
51-
if (sidebar_is_collapsed())
52-
expand_sidebar();
53-
else
54-
collapse_sidebar();
59+
const expand_sidebar = () => {
60+
bodyWrapper.style.marginLeft = ""
61+
sidebar.style.removeProperty("width")
62+
sidebarWrapper.style.display = ""
63+
flipArrow(sidebarArrow)
64+
sidebarButton.title = _("Collapse sidebar")
65+
window.localStorage.setItem("sidebar", "expanded")
5566
}
5667

57-
function collapse_sidebar() {
58-
sidebarwrapper.hide();
59-
sidebar.css('width', ssb_width_collapsed);
60-
bodywrapper.css('margin-left', bw_margin_collapsed);
61-
sidebarbutton.css({
62-
'margin-left': '0',
63-
'border-radius': '5px'
64-
});
65-
sidebarbutton.find('span').text('»');
66-
sidebarbutton.attr('title', _('Expand sidebar'));
67-
document.cookie = 'sidebar=collapsed';
68-
}
68+
sidebarButton.addEventListener("click", () => {
69+
(sidebarWrapper.style.display === "none") ? expand_sidebar() : collapse_sidebar()
70+
})
6971

70-
function expand_sidebar() {
71-
bodywrapper.css('margin-left', bw_margin_expanded);
72-
sidebar.css('width', ssb_width_expanded);
73-
sidebarwrapper.show();
74-
sidebarbutton.css({
75-
'margin-left': ssb_width_expanded-12,
76-
'border-radius': '0 5px 5px 0'
77-
});
78-
sidebarbutton.find('span').text('«');
79-
sidebarbutton.attr('title', _('Collapse sidebar'));
80-
document.cookie = 'sidebar=expanded';
72+
if (!window.localStorage.getItem("sidebar")) {
73+
return
8174
}
82-
83-
function add_sidebar_button() {
84-
sidebarwrapper.css({
85-
'float': 'left',
86-
'margin-right': '0',
87-
'width': ssb_width_expanded - 13
88-
});
89-
// create the button
90-
sidebar.append(
91-
'<div id="sidebarbutton"><span>&laquo;</span></div>'
92-
);
93-
var sidebarbutton = $('#sidebarbutton');
94-
sidebarbutton.find('span').css({
95-
'display': 'block',
96-
'position': 'fixed',
97-
'top': '50%'
98-
});
99-
100-
sidebarbutton.click(toggle_sidebar);
101-
sidebarbutton.attr('title', _('Collapse sidebar'));
102-
sidebarbutton.css({
103-
'border-radius': '0 5px 5px 0',
104-
'color': '#444444',
105-
'background-color': '#CCCCCC',
106-
'font-size': '1.2em',
107-
'cursor': 'pointer',
108-
'height': '100%',
109-
'padding-left': '1px',
110-
'margin-left': ssb_width_expanded - 12
111-
});
112-
113-
sidebarbutton.hover(
114-
function () {
115-
$(this).css('background-color', dark_color);
116-
},
117-
function () {
118-
$(this).css('background-color', light_color);
119-
}
120-
);
75+
const value = window.localStorage.getItem("sidebar")
76+
if (value === "collapsed") {
77+
collapse_sidebar()
12178
}
122-
123-
function set_position_from_cookie() {
124-
if (!document.cookie)
125-
return;
126-
var items = document.cookie.split(';');
127-
for(var k=0; k<items.length; k++) {
128-
var key_val = items[k].split('=');
129-
var key = key_val[0];
130-
if (key == 'sidebar') {
131-
var value = key_val[1];
132-
if ((value == 'collapsed') && (!sidebar_is_collapsed()))
133-
collapse_sidebar();
134-
else if ((value == 'expanded') && (sidebar_is_collapsed()))
135-
expand_sidebar();
136-
}
137-
}
79+
else if (value === "expanded") {
80+
expand_sidebar()
13881
}
82+
}
13983

140-
add_sidebar_button();
141-
var sidebarbutton = $('#sidebarbutton');
142-
set_position_from_cookie();
143-
});
84+
if (document.readyState !== "loading") {
85+
initialiseSidebar()
86+
}
87+
else {
88+
document.addEventListener("DOMContentLoaded", initialiseSidebar)
89+
}

python_docs_theme/theme.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ relbarlinkcolor = #444444
1515
sidebarbgcolor = white
1616
sidebartextcolor = #444444
1717
sidebarlinkcolor = #444444
18+
sidebarbtncolor = #cccccc
1819
bgcolor = white
1920
textcolor = #222222
2021
linkcolor = #0090c0

0 commit comments

Comments
 (0)