Debian Codegraph @

cpython27/Doc/tools/static/sidebar.js

raw symbols SCIP
1 /*
2 * sidebar.js
3 * ~~~~~~~~~~
4 *
5 * This script makes the Sphinx sidebar collapsible and implements
6 * intelligent scrolling.
7 *
8 * .sphinxsidebar contains .sphinxsidebarwrapper. This script adds
9 * in .sphixsidebar, after .sphinxsidebarwrapper, the #sidebarbutton
10 * used to collapse and expand the sidebar.
11 *
12 * When the sidebar is collapsed the .sphinxsidebarwrapper is hidden
13 * and the width of the sidebar and the margin-left of the document
14 * are decreased. When the sidebar is expanded the opposite happens.
15 * This script saves a per-browser/per-session cookie used to
16 * remember the position of the sidebar among the pages.
17 * Once the browser is closed the cookie is deleted and the position
18 * reset to the default (expanded).
19 *
20 * :copyright: Copyright 2007-2011 by the Sphinx team, see AUTHORS.
21 * :license: BSD, see LICENSE for details.
22 *
23 */
24
25 $(function() {
26 // global elements used by the functions.
27 // the 'sidebarbutton' element is defined as global after its
28 // creation, in the add_sidebar_button function
29 var jwindow = $(window);
30 var jdocument = $(document);
31 var bodywrapper = $('.bodywrapper');
32 var sidebar = $('.sphinxsidebar');
33 var sidebarwrapper = $('.sphinxsidebarwrapper');
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 = '.8em';
43 var ssb_width_collapsed = '.8em';
44
45 // colors used by the current theme
46 var dark_color = $('.related').css('background-color');
47 var light_color = $('.document').css('background-color');
48
49 // set position: sticky on sidebar
50 // (browsers that don't support this will fall-back to
51 // positioning via scroll_sidebar)
52 var supportsPositionSticky = (window.CSS && window.CSS.supports &&
53 window.CSS.supports('position', 'sticky'));
54 if (supportsPositionSticky) {
55 sidebarwrapper.css('position', 'sticky');
56 }
57
58 function get_viewport_height() {
59 if (window.innerHeight)
60 return window.innerHeight;
61 else
62 return jwindow.height();
63 }
64
65 function sidebar_is_collapsed() {
66 return sidebarwrapper.is(':not(:visible)');
67 }
68
69 function toggle_sidebar() {
70 if (sidebar_is_collapsed())
71 expand_sidebar();
72 else
73 collapse_sidebar();
74 // adjust the scrolling of the sidebar
75 scroll_sidebar();
76 }
77
78 function collapse_sidebar() {
79 sidebarwrapper.hide();
80 sidebar.css('width', ssb_width_collapsed);
81 bodywrapper.css('margin-left', bw_margin_collapsed);
82 sidebarbutton.css({
83 'margin-left': '0',
84 'height': bodywrapper.height()
85 });
86 sidebarbutton.find('span').text('»');
87 sidebarbutton.attr('title', _('Expand sidebar'));
88 document.cookie = 'sidebar=collapsed';
89 }
90
91 function expand_sidebar() {
92 bodywrapper.css('margin-left', bw_margin_expanded);
93 sidebar.css('width', ssb_width_expanded);
94 sidebarwrapper.show();
95 sidebarbutton.css({
96 'margin-left': ssb_width_expanded-12,
97 'height': bodywrapper.height()
98 });
99 sidebarbutton.find('span').text('«');
100 sidebarbutton.attr('title', _('Collapse sidebar'));
101 document.cookie = 'sidebar=expanded';
102 }
103
104 function add_sidebar_button() {
105 sidebarwrapper.css({
106 'float': 'left',
107 'margin-right': '0',
108 'width': ssb_width_expanded - 28
109 });
110 // create the button
111 sidebar.append(
112 '<div id="sidebarbutton"><span>&laquo;</span></div>'
113 );
114 var sidebarbutton = $('#sidebarbutton');
115 light_color = sidebarbutton.css('background-color');
116 // find the height of the viewport to center the '<<' in the page
117 var viewport_height = get_viewport_height();
118 sidebarbutton.find('span').css({
119 'display': 'block',
120 'margin-top': (viewport_height - sidebar.position().top - 20) / 2
121 });
122
123 sidebarbutton.click(toggle_sidebar);
124 sidebarbutton.attr('title', _('Collapse sidebar'));
125 sidebarbutton.css({
126 'color': '#FFFFFF',
127 'border-left': '1px solid ' + dark_color,
128 'font-size': '1.2em',
129 'cursor': 'pointer',
130 'height': bodywrapper.height(),
131 'padding-top': '1px',
132 'margin-left': ssb_width_expanded - 12
133 });
134
135 sidebarbutton.hover(
136 function () {
137 $(this).css('background-color', dark_color);
138 },
139 function () {
140 $(this).css('background-color', light_color);
141 }
142 );
143 }
144
145 function set_position_from_cookie() {
146 if (!document.cookie)
147 return;
148 var items = document.cookie.split(';');
149 for(var k=0; k<items.length; k++) {
150 var key_val = items[k].split('=');
151 var key = key_val[0];
152 if (key == 'sidebar') {
153 var value = key_val[1];
154 if ((value == 'collapsed') && (!sidebar_is_collapsed()))
155 collapse_sidebar();
156 else if ((value == 'expanded') && (sidebar_is_collapsed()))
157 expand_sidebar();
158 }
159 }
160 }
161
162 add_sidebar_button();
163 var sidebarbutton = $('#sidebarbutton');
164 set_position_from_cookie();
165
166
167 /* intelligent scrolling */
168 function scroll_sidebar() {
169 if (supportsPositionSticky) {
170 return;
171 }
172 var sidebar_height = sidebarwrapper.height();
173 var viewport_height = get_viewport_height();
174 var offset = sidebar.position()['top'];
175 var wintop = jwindow.scrollTop();
176 var winbot = wintop + viewport_height;
177 var curtop = sidebarwrapper.position()['top'];
178 var curbot = curtop + sidebar_height;
179 // does sidebar fit in window?
180 if (sidebar_height < viewport_height) {
181 // yes: easy case -- always keep at the top
182 sidebarwrapper.css('top', $u.min([$u.max([0, wintop - offset - 10]),
183 jdocument.height() - sidebar_height - 200]));
184 }
185 else {
186 // no: only scroll if top/bottom edge of sidebar is at
187 // top/bottom edge of window
188 if (curtop > wintop && curbot > winbot) {
189 sidebarwrapper.css('top', $u.max([wintop - offset - 10, 0]));
190 }
191 else if (curtop < wintop && curbot < winbot) {
192 sidebarwrapper.css('top', $u.min([winbot - sidebar_height - offset - 20,
193 jdocument.height() - sidebar_height - 200]));
194 }
195 }
196 }
197 jwindow.scroll(scroll_sidebar);
198 });