|
| 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 | + 'border-radius': '5px' |
| 64 | + }); |
| 65 | + sidebarbutton.find('span').text('»'); |
| 66 | + sidebarbutton.attr('title', _('Expand sidebar')); |
| 67 | + document.cookie = 'sidebar=collapsed'; |
| 68 | + } |
| 69 | + |
| 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 | + 'height': bodywrapper.height(), |
| 77 | + 'border-radius': '0 5px 5px 0' |
| 78 | + }); |
| 79 | + sidebarbutton.find('span').text('«'); |
| 80 | + sidebarbutton.attr('title', _('Collapse sidebar')); |
| 81 | + //sidebarwrapper.css({'padding-top': |
| 82 | + // Math.max(window.pageYOffset - sidebarwrapper.offset().top, 10)}); |
| 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 - 28 |
| 91 | + }); |
| 92 | + // create the button |
| 93 | + sidebar.append( |
| 94 | + '<div id="sidebarbutton"><span>«</span></div>' |
| 95 | + ); |
| 96 | + var sidebarbutton = $('#sidebarbutton'); |
| 97 | + // find the height of the viewport to center the '<<' in the page |
| 98 | + var viewport_height; |
| 99 | + if (window.innerHeight) |
| 100 | + viewport_height = window.innerHeight; |
| 101 | + else |
| 102 | + viewport_height = $(window).height(); |
| 103 | + var sidebar_offset = sidebar.offset().top; |
| 104 | + var sidebar_height = Math.max(bodywrapper.height(), sidebar.height()); |
| 105 | + sidebarbutton.find('span').css({ |
| 106 | + 'display': 'block', |
| 107 | + 'position': 'fixed', |
| 108 | + 'top': Math.min(viewport_height/2, sidebar_height/2 + sidebar_offset) - 10 |
| 109 | + }); |
| 110 | + |
| 111 | + sidebarbutton.click(toggle_sidebar); |
| 112 | + sidebarbutton.attr('title', _('Collapse sidebar')); |
| 113 | + sidebarbutton.css({ |
| 114 | + 'border-radius': '0 5px 5px 0', |
| 115 | + 'color': '#444444', |
| 116 | + 'background-color': '#CCCCCC', |
| 117 | + 'font-size': '1.2em', |
| 118 | + 'cursor': 'pointer', |
| 119 | + 'height': sidebar_height, |
| 120 | + 'padding-top': '1px', |
| 121 | + 'padding-left': '1px', |
| 122 | + 'margin-left': ssb_width_expanded - 12 |
| 123 | + }); |
| 124 | + |
| 125 | + sidebarbutton.hover( |
| 126 | + function () { |
| 127 | + $(this).css('background-color', dark_color); |
| 128 | + }, |
| 129 | + function () { |
| 130 | + $(this).css('background-color', light_color); |
| 131 | + } |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + function set_position_from_cookie() { |
| 136 | + if (!document.cookie) |
| 137 | + return; |
| 138 | + var items = document.cookie.split(';'); |
| 139 | + for(var k=0; k<items.length; k++) { |
| 140 | + var key_val = items[k].split('='); |
| 141 | + var key = key_val[0]; |
| 142 | + if (key == 'sidebar') { |
| 143 | + var value = key_val[1]; |
| 144 | + if ((value == 'collapsed') && (!sidebar_is_collapsed())) |
| 145 | + 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 | +}); |
0 commit comments