|
2 | 2 | * sidebar.js
|
3 | 3 | * ~~~~~~~~~~
|
4 | 4 | *
|
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. |
7 | 8 | *
|
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. |
11 | 10 | *
|
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. |
18 | 14 | *
|
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. |
20 | 24 | * :license: BSD, see LICENSE for details.
|
21 | 25 | *
|
22 | 26 | */
|
23 | 27 |
|
24 |
| -$(function() { |
| 28 | +const initialiseSidebar = () => { |
25 | 29 | // 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'); |
31 |
| - |
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(); |
36 |
| - |
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'; |
| 30 | + const bodyWrapper = document.getElementsByClassName("bodywrapper")[0] |
| 31 | + const sidebar = document.getElementsByClassName("sphinxsidebar")[0] |
| 32 | + const sidebarWrapper = document.getElementsByClassName('sphinxsidebarwrapper')[0] |
| 33 | + const sidebarButton = document.getElementById("sidebarbutton") |
| 34 | + const sidebarArrow = sidebarButton.querySelector('span') |
41 | 35 |
|
42 |
| - // colors used by the current theme |
43 |
| - var dark_color = '#AAAAAA'; |
44 |
| - var light_color = '#CCCCCC'; |
45 |
| - |
46 |
| - function sidebar_is_collapsed() { |
47 |
| - return sidebarwrapper.is(':not(:visible)'); |
48 |
| - } |
| 36 | + // for some reason, the document has no sidebar; do not run into errors |
| 37 | + if (typeof sidebar === "undefined") return; |
49 | 38 |
|
50 |
| - function toggle_sidebar() { |
51 |
| - if (sidebar_is_collapsed()) |
52 |
| - expand_sidebar(); |
53 |
| - else |
54 |
| - collapse_sidebar(); |
55 |
| - } |
| 39 | + const flipArrow = element => element.innerText = (element.innerText === "»") ? "«" : "»" |
56 | 40 |
|
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'; |
| 41 | + const collapse_sidebar = () => { |
| 42 | + bodyWrapper.style.marginLeft = ".8em"; |
| 43 | + sidebar.style.width = ".8em" |
| 44 | + sidebarWrapper.style.display = "none" |
| 45 | + flipArrow(sidebarArrow) |
| 46 | + sidebarButton.title = _('Expand sidebar') |
| 47 | + window.localStorage.setItem("sidebar", "collapsed") |
68 | 48 | }
|
69 | 49 |
|
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'; |
| 50 | + const expand_sidebar = () => { |
| 51 | + bodyWrapper.style.marginLeft = "" |
| 52 | + sidebar.style.removeProperty("width") |
| 53 | + sidebarWrapper.style.display = "" |
| 54 | + flipArrow(sidebarArrow) |
| 55 | + sidebarButton.title = _('Collapse sidebar') |
| 56 | + window.localStorage.setItem("sidebar", "expanded") |
81 | 57 | }
|
82 | 58 |
|
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>«</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 |
| - }); |
| 59 | + sidebarButton.addEventListener("click", () => { |
| 60 | + (sidebarWrapper.style.display === "none") ? expand_sidebar() : collapse_sidebar() |
| 61 | + }) |
112 | 62 |
|
113 |
| - sidebarbutton.hover( |
114 |
| - function () { |
115 |
| - $(this).css('background-color', dark_color); |
116 |
| - }, |
117 |
| - function () { |
118 |
| - $(this).css('background-color', light_color); |
119 |
| - } |
120 |
| - ); |
121 |
| - } |
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 |
| - } |
138 |
| - } |
| 63 | + if (!window.localStorage.getItem("sidebar")) return |
| 64 | + const value = window.localStorage.getItem("sidebar") |
| 65 | + if (value === "collapsed") collapse_sidebar(); |
| 66 | + else if (value === "expanded") expand_sidebar(); |
| 67 | +} |
139 | 68 |
|
140 |
| - add_sidebar_button(); |
141 |
| - var sidebarbutton = $('#sidebarbutton'); |
142 |
| - set_position_from_cookie(); |
143 |
| -}); |
| 69 | +if (document.readyState !== "loading") initialiseSidebar() |
| 70 | +else document.addEventListener("DOMContentLoaded", initialiseSidebar) |
0 commit comments