Skip to content

Commit a1af9fd

Browse files
committed
[toc2] simplify notebook width-setting
1 parent ef815af commit a1af9fd

File tree

1 file changed

+18
-32
lines changed
  • src/jupyter_contrib_nbextensions/nbextensions/toc2

1 file changed

+18
-32
lines changed

src/jupyter_contrib_nbextensions/nbextensions/toc2/toc2.js

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -161,41 +161,27 @@
161161
}
162162

163163
function setNotebookWidth(cfg, st) {
164-
if (cfg.sideBar) {
165-
if ($('#toc-wrapper').is(':visible')) {
166-
if (cfg.widenNotebook) {
167-
$('#notebook-container').css('margin-left', $('#toc-wrapper').width() + 30)
168-
$('#notebook-container').css('width', $('#notebook').width() - $('#toc-wrapper').width() - 30)
169-
} else {
170-
var space_needed = $('#toc-wrapper').width() + 30 +
171-
$('#notebook-container').width() - $('#notebook').width();
172-
if (space_needed > 0) {
173-
$('#notebook-container').css('margin-left', $('#toc-wrapper').width() + 30)
174-
$('#notebook-container').css('width', $('#notebook-container').width() - space_needed)
175-
}
176-
}
177-
} else {
178-
if (cfg.widenNotebook) {
179-
$('#notebook-container').css('margin-left', 30);
180-
$('#notebook-container').css('width', $('#notebook').width() - 30);
181-
} else { // original width
182-
$("#notebook-container").css({
183-
'width': '',
184-
'margin-left': ''
185-
})
164+
var margin = 20;
165+
var nb_inner = $('#notebook-container');
166+
var nb_wrap_w = $('#notebook').width();
167+
var sidebar = $('#toc-wrapper');
168+
var visible_sidebar = cfg.sideBar && sidebar.is(':visible');
169+
var sidebar_w = visible_sidebar ? sidebar.outerWidth() : 0;
170+
var available_space = nb_wrap_w - 2 * margin - sidebar_w;
171+
var inner_css = {marginLeft: '', width: ''};
172+
if (cfg.widenNotebook) {
173+
inner_css.width = available_space;
174+
}
175+
if (visible_sidebar) {
176+
var nb_inner_w = nb_inner.outerWidth();
177+
if (available_space <= nb_inner_w + sidebar_w) {
178+
inner_css.marginLeft = sidebar_w + margin; // shift notebook rightward to fit the sidebar in
179+
if (available_space <= nb_inner_w) {
180+
inner_css.width = available_space; // also slim notebook to fit sidebar
186181
}
187182
}
188-
} else {
189-
if (cfg.widenNotebook) {
190-
$('#notebook-container').css('margin-left', 30);
191-
$('#notebook-container').css('width', $('#notebook').width() - 30);
192-
} else { // original width
193-
$("#notebook-container").css({
194-
'width': '',
195-
'margin-left': ''
196-
})
197-
}
198183
}
184+
nb_inner.css(inner_css);
199185
}
200186

201187
function setSideBarHeight(cfg, st) {

0 commit comments

Comments
 (0)