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