| 1 |
/* |
| 2 |
* sidebar.js |
| 3 |
* ~~~~~~~~~~ |
| 4 |
* |
| 5 |
* This script makes the Sphinx sidebar collapsible. |
| 6 |
* |
| 7 |
* .sphinxsidebar contains .sphinxsidebarwrapper. This script adds |
| 8 |
* in .sphixsidebar, after .sphinxsidebarwrapper, the #sidebarbutton |
| 9 |
* used to collapse and expand the sidebar. |
| 10 |
* |
| 11 |
* When the sidebar is collapsed the .sphinxsidebarwrapper is hidden |
| 12 |
* and the width of the sidebar and the margin-left of the document |
| 13 |
* are decreased. When the sidebar is expanded the opposite happens. |
| 14 |
* This script saves a per-browser/per-session cookie used to |
| 15 |
* remember the position of the sidebar among the pages. |
| 16 |
* Once the browser is closed the cookie is deleted and the position |
| 17 |
* reset to the default (expanded). |
| 18 |
* |
| 19 |
* :copyright: Copyright 2007-2011 by the Sphinx team, see AUTHORS. |
| 20 |
* :license: BSD, see LICENSE for details. |
| 21 |
* |
| 22 |
*/ |
| 23 |
|
| 24 |
$(function () { |
| 25 |
// 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 |
// for some reason, the document has no sidebar; do not run into errors |
| 33 |
if (!sidebar.length) return; |
| 34 |
|
| 35 |
// original margin-left of the bodywrapper and width of the sidebar |
| 36 |
// with the sidebar expanded |
| 37 |
var bw_margin_expanded = bodywrapper.css("margin-left"); |
| 38 |
var ssb_width_expanded = sidebar.width(); |
| 39 |
|
| 40 |
// margin-left of the bodywrapper and width of the sidebar |
| 41 |
// with the sidebar collapsed |
| 42 |
var bw_margin_collapsed = 12; |
| 43 |
var ssb_width_collapsed = 12; |
| 44 |
|
| 45 |
// custom colors |
| 46 |
var dark_color = "#404040"; |
| 47 |
var light_color = "#505050"; |
| 48 |
|
| 49 |
function sidebar_is_collapsed() { |
| 50 |
return sidebarwrapper.is(":not(:visible)"); |
| 51 |
} |
| 52 |
|
| 53 |
function toggle_sidebar() { |
| 54 |
if (sidebar_is_collapsed()) expand_sidebar(); |
| 55 |
else collapse_sidebar(); |
| 56 |
} |
| 57 |
|
| 58 |
function collapse_sidebar() { |
| 59 |
sidebarwrapper.hide(); |
| 60 |
sidebar.css("width", ssb_width_collapsed); |
| 61 |
bodywrapper.css("margin-left", bw_margin_collapsed); |
| 62 |
sidebarbutton.css({ |
| 63 |
"margin-left": "-1px", |
| 64 |
height: bodywrapper.height(), |
| 65 |
"border-radius": "3px", |
| 66 |
}); |
| 67 |
sidebarbutton.find("span").text("»"); |
| 68 |
sidebarbutton.attr("title", _("Expand sidebar")); |
| 69 |
document.cookie = "sidebar=collapsed"; |
| 70 |
} |
| 71 |
|
| 72 |
function expand_sidebar() { |
| 73 |
bodywrapper.css("margin-left", bw_margin_expanded); |
| 74 |
sidebar.css("width", ssb_width_expanded); |
| 75 |
sidebarwrapper.show(); |
| 76 |
sidebarbutton.css({ |
| 77 |
"margin-left": ssb_width_expanded - 12, |
| 78 |
height: bodywrapper.height(), |
| 79 |
"border-radius": "0px 3px 3px 0px", |
| 80 |
}); |
| 81 |
sidebarbutton.find("span").text("«"); |
| 82 |
sidebarbutton.attr("title", _("Collapse sidebar")); |
| 83 |
document.cookie = "sidebar=expanded"; |
| 84 |
} |
| 85 |
|
| 86 |
function add_sidebar_button() { |
| 87 |
sidebarwrapper.css({ |
| 88 |
float: "left", |
| 89 |
"margin-right": "0", |
| 90 |
width: ssb_width_expanded - 18, |
| 91 |
}); |
| 92 |
// create the button |
| 93 |
sidebar.append('<div id="sidebarbutton"><span>«</span></div>'); |
| 94 |
var sidebarbutton = $("#sidebarbutton"); |
| 95 |
|
| 96 |
// find the height of the viewport to center the '<<' in the page |
| 97 |
var viewport_height; |
| 98 |
if (window.innerHeight) viewport_height = window.innerHeight; |
| 99 |
else viewport_height = $(window).height(); |
| 100 |
var sidebar_offset = sidebar.offset().top; |
| 101 |
var sidebar_height = Math.max(bodywrapper.height(), sidebar.height()); |
| 102 |
sidebarbutton.find("span").css({ |
| 103 |
"font-family": '"Lucida Grande",Arial,sans-serif', |
| 104 |
display: "block", |
| 105 |
top: |
| 106 |
Math.min(viewport_height / 2, sidebar_height / 2 + sidebar_offset) - 10, |
| 107 |
width: 12, |
| 108 |
position: "fixed", |
| 109 |
"text-align": "center", |
| 110 |
}); |
| 111 |
|
| 112 |
sidebarbutton.click(toggle_sidebar); |
| 113 |
sidebarbutton.attr("title", _("Collapse sidebar")); |
| 114 |
sidebarbutton.css({ |
| 115 |
color: "#FFFFFF", |
| 116 |
"background-color": light_color, |
| 117 |
border: "1px solid " + light_color, |
| 118 |
"border-radius": "0px 3px 3px 0px", |
| 119 |
"font-size": "1.2em", |
| 120 |
cursor: "pointer", |
| 121 |
height: sidebar_height, |
| 122 |
"padding-top": "1px", |
| 123 |
margin: "-1px", |
| 124 |
"margin-left": ssb_width_expanded - 12, |
| 125 |
}); |
| 126 |
|
| 127 |
sidebarbutton.hover( |
| 128 |
function () { |
| 129 |
$(this).css("background-color", dark_color); |
| 130 |
}, |
| 131 |
function () { |
| 132 |
$(this).css("background-color", light_color); |
| 133 |
}, |
| 134 |
); |
| 135 |
} |
| 136 |
|
| 137 |
function set_position_from_cookie() { |
| 138 |
if (!document.cookie) return; |
| 139 |
var items = document.cookie.split(";"); |
| 140 |
for (var k = 0; k < items.length; k++) { |
| 141 |
var key_val = items[k].split("="); |
| 142 |
var key = key_val[0]; |
| 143 |
if (key == "sidebar") { |
| 144 |
var value = key_val[1]; |
| 145 |
if (value == "collapsed" && !sidebar_is_collapsed()) collapse_sidebar(); |
| 146 |
else if (value == "expanded" && sidebar_is_collapsed()) |
| 147 |
expand_sidebar(); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
add_sidebar_button(); |
| 153 |
var sidebarbutton = $("#sidebarbutton"); |
| 154 |
set_position_from_cookie(); |
| 155 |
}); |