|
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'); |
| 30 | + const bodyWrapper = document.getElementsByClassName("bodywrapper")[0] |
| 31 | + const sidebar = document.getElementsByClassName("sphinxsidebar")[0] |
| 32 | + const sidebarWrapper = document.getElementsByClassName("sphinxsidebarwrapper")[0] |
31 | 33 |
|
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 | + } |
36 | 38 |
|
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) |
41 | 47 |
|
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 === "»") ? "«" : "»" |
45 | 49 |
|
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") |
48 | 57 | }
|
49 | 58 |
|
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") |
55 | 66 | }
|
56 | 67 |
|
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 | + }) |
69 | 71 |
|
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 |
81 | 74 | }
|
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>«</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() |
121 | 78 | }
|
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() |
138 | 81 | }
|
| 82 | +} |
139 | 83 |
|
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 | +} |
0 commit comments