michael@0: /* michael@0: * sidebar.js michael@0: * ~~~~~~~~~~ michael@0: * michael@0: * This script makes the Sphinx sidebar collapsible. michael@0: * michael@0: * .sphinxsidebar contains .sphinxsidebarwrapper. This script adds michael@0: * in .sphixsidebar, after .sphinxsidebarwrapper, the #sidebarbutton michael@0: * used to collapse and expand the sidebar. michael@0: * michael@0: * When the sidebar is collapsed the .sphinxsidebarwrapper is hidden michael@0: * and the width of the sidebar and the margin-left of the document michael@0: * are decreased. When the sidebar is expanded the opposite happens. michael@0: * This script saves a per-browser/per-session cookie used to michael@0: * remember the position of the sidebar among the pages. michael@0: * Once the browser is closed the cookie is deleted and the position michael@0: * reset to the default (expanded). michael@0: * michael@0: * :copyright: Copyright 2007-2010 by the Sphinx team, see AUTHORS. michael@0: * :license: BSD, see LICENSE for details. michael@0: * michael@0: */ michael@0: michael@0: $(function() { michael@0: // global elements used by the functions. michael@0: // the 'sidebarbutton' element is defined as global after its michael@0: // creation, in the add_sidebar_button function michael@0: var bodywrapper = $('.bodywrapper'); michael@0: var sidebar = $('.sphinxsidebar'); michael@0: var sidebarwrapper = $('.sphinxsidebarwrapper'); michael@0: michael@0: // original margin-left of the bodywrapper and width of the sidebar michael@0: // with the sidebar expanded michael@0: var bw_margin_expanded = bodywrapper.css('margin-left'); michael@0: var ssb_width_expanded = sidebar.width(); michael@0: michael@0: // margin-left of the bodywrapper and width of the sidebar michael@0: // with the sidebar collapsed michael@0: var bw_margin_collapsed = '.8em'; michael@0: var ssb_width_collapsed = '.8em'; michael@0: michael@0: // colors used by the current theme michael@0: var dark_color = $('.related').css('background-color'); michael@0: var light_color = $('.document').css('background-color'); michael@0: michael@0: function sidebar_is_collapsed() { michael@0: return sidebarwrapper.is(':not(:visible)'); michael@0: } michael@0: michael@0: function toggle_sidebar() { michael@0: if (sidebar_is_collapsed()) michael@0: expand_sidebar(); michael@0: else michael@0: collapse_sidebar(); michael@0: } michael@0: michael@0: function collapse_sidebar() { michael@0: sidebarwrapper.hide(); michael@0: sidebar.css('width', ssb_width_collapsed); michael@0: bodywrapper.css('margin-left', bw_margin_collapsed); michael@0: sidebarbutton.css({ michael@0: 'margin-left': '0', michael@0: 'height': bodywrapper.height() michael@0: }); michael@0: sidebarbutton.find('span').text('»'); michael@0: sidebarbutton.attr('title', _('Expand sidebar')); michael@0: document.cookie = 'sidebar=collapsed'; michael@0: } michael@0: michael@0: function expand_sidebar() { michael@0: bodywrapper.css('margin-left', bw_margin_expanded); michael@0: sidebar.css('width', ssb_width_expanded); michael@0: sidebarwrapper.show(); michael@0: sidebarbutton.css({ michael@0: 'margin-left': ssb_width_expanded-12, michael@0: 'height': bodywrapper.height() michael@0: }); michael@0: sidebarbutton.find('span').text('«'); michael@0: sidebarbutton.attr('title', _('Collapse sidebar')); michael@0: document.cookie = 'sidebar=expanded'; michael@0: } michael@0: michael@0: function add_sidebar_button() { michael@0: sidebarwrapper.css({ michael@0: 'float': 'left', michael@0: 'margin-right': '0', michael@0: 'width': ssb_width_expanded - 28 michael@0: }); michael@0: // create the button michael@0: sidebar.append( michael@0: '
' michael@0: ); michael@0: var sidebarbutton = $('#sidebarbutton'); michael@0: light_color = sidebarbutton.css('background-color'); michael@0: // find the height of the viewport to center the '<<' in the page michael@0: var viewport_height; michael@0: if (window.innerHeight) michael@0: viewport_height = window.innerHeight; michael@0: else michael@0: viewport_height = $(window).height(); michael@0: sidebarbutton.find('span').css({ michael@0: 'display': 'block', michael@0: 'margin-top': (viewport_height - sidebar.position().top - 20) / 2 michael@0: }); michael@0: michael@0: sidebarbutton.click(toggle_sidebar); michael@0: sidebarbutton.attr('title', _('Collapse sidebar')); michael@0: sidebarbutton.css({ michael@0: 'color': '#FFFFFF', michael@0: 'border-left': '1px solid ' + dark_color, michael@0: 'font-size': '1.2em', michael@0: 'cursor': 'pointer', michael@0: 'height': bodywrapper.height(), michael@0: 'padding-top': '1px', michael@0: 'margin-left': ssb_width_expanded - 12 michael@0: }); michael@0: michael@0: sidebarbutton.hover( michael@0: function () { michael@0: $(this).css('background-color', dark_color); michael@0: }, michael@0: function () { michael@0: $(this).css('background-color', light_color); michael@0: } michael@0: ); michael@0: } michael@0: michael@0: function set_position_from_cookie() { michael@0: if (!document.cookie) michael@0: return; michael@0: var items = document.cookie.split(';'); michael@0: for(var k=0; k