/src/pyechonest/doc/build/html/_static/sidebar.js

http://echo-nest-remix.googlecode.com/ · JavaScript · 148 lines · 103 code · 13 blank · 32 comment · 13 complexity · 35b8f6c733405b82d8243e4638201cda MD5 · raw file

  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-2010 by the Sphinx team, see AUTHORS.
  20. * :license: BSD, see LICENSE for details.
  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. // original margin-left of the bodywrapper and width of the sidebar
  31. // with the sidebar expanded
  32. var bw_margin_expanded = bodywrapper.css('margin-left');
  33. var ssb_width_expanded = sidebar.width();
  34. // margin-left of the bodywrapper and width of the sidebar
  35. // with the sidebar collapsed
  36. var bw_margin_collapsed = '.8em';
  37. var ssb_width_collapsed = '.8em';
  38. // colors used by the current theme
  39. var dark_color = $('.related').css('background-color');
  40. var light_color = $('.document').css('background-color');
  41. function sidebar_is_collapsed() {
  42. return sidebarwrapper.is(':not(:visible)');
  43. }
  44. function toggle_sidebar() {
  45. if (sidebar_is_collapsed())
  46. expand_sidebar();
  47. else
  48. collapse_sidebar();
  49. }
  50. function collapse_sidebar() {
  51. sidebarwrapper.hide();
  52. sidebar.css('width', ssb_width_collapsed);
  53. bodywrapper.css('margin-left', bw_margin_collapsed);
  54. sidebarbutton.css({
  55. 'margin-left': '0',
  56. 'height': bodywrapper.height()
  57. });
  58. sidebarbutton.find('span').text('Â?');
  59. sidebarbutton.attr('title', _('Expand sidebar'));
  60. document.cookie = 'sidebar=collapsed';
  61. }
  62. function expand_sidebar() {
  63. bodywrapper.css('margin-left', bw_margin_expanded);
  64. sidebar.css('width', ssb_width_expanded);
  65. sidebarwrapper.show();
  66. sidebarbutton.css({
  67. 'margin-left': ssb_width_expanded-12,
  68. 'height': bodywrapper.height()
  69. });
  70. sidebarbutton.find('span').text('Â?');
  71. sidebarbutton.attr('title', _('Collapse sidebar'));
  72. document.cookie = 'sidebar=expanded';
  73. }
  74. function add_sidebar_button() {
  75. sidebarwrapper.css({
  76. 'float': 'left',
  77. 'margin-right': '0',
  78. 'width': ssb_width_expanded - 28
  79. });
  80. // create the button
  81. sidebar.append(
  82. '<div id="sidebarbutton"><span>&laquo;</span></div>'
  83. );
  84. var sidebarbutton = $('#sidebarbutton');
  85. light_color = sidebarbutton.css('background-color');
  86. // find the height of the viewport to center the '<<' in the page
  87. var viewport_height;
  88. if (window.innerHeight)
  89. viewport_height = window.innerHeight;
  90. else
  91. viewport_height = $(window).height();
  92. sidebarbutton.find('span').css({
  93. 'display': 'block',
  94. 'margin-top': (viewport_height - sidebar.position().top - 20) / 2
  95. });
  96. sidebarbutton.click(toggle_sidebar);
  97. sidebarbutton.attr('title', _('Collapse sidebar'));
  98. sidebarbutton.css({
  99. 'color': '#FFFFFF',
  100. 'border-left': '1px solid ' + dark_color,
  101. 'font-size': '1.2em',
  102. 'cursor': 'pointer',
  103. 'height': bodywrapper.height(),
  104. 'padding-top': '1px',
  105. 'margin-left': ssb_width_expanded - 12
  106. });
  107. sidebarbutton.hover(
  108. function () {
  109. $(this).css('background-color', dark_color);
  110. },
  111. function () {
  112. $(this).css('background-color', light_color);
  113. }
  114. );
  115. }
  116. function set_position_from_cookie() {
  117. if (!document.cookie)
  118. return;
  119. var items = document.cookie.split(';');
  120. for(var k=0; k<items.length; k++) {
  121. var key_val = items[k].split('=');
  122. var key = key_val[0];
  123. if (key == 'sidebar') {
  124. var value = key_val[1];
  125. if ((value == 'collapsed') && (!sidebar_is_collapsed()))
  126. collapse_sidebar();
  127. else if ((value == 'expanded') && (sidebar_is_collapsed()))
  128. expand_sidebar();
  129. }
  130. }
  131. }
  132. add_sidebar_button();
  133. var sidebarbutton = $('#sidebarbutton');
  134. set_position_from_cookie();
  135. });