Skip to content

Commit f9b43ae

Browse files
committed
[toc2] simplify position save/load
* only store position when dragging/resizing * don't store height or position (just store width) if in sidebar mode * only use one variable - we don't need to store size separately in oldTocSize, just keep it in position var * set defaults in js, in case css hasn't loaded by the time the notebook width is set
1 parent 41d9217 commit f9b43ae

File tree

2 files changed

+13
-22
lines changed

2 files changed

+13
-22
lines changed

src/jupyter_contrib_nbextensions/nbextensions/toc2/main.css

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ padding-left: 20px;
9090
}
9191

9292
.float-wrapper {
93-
top: 120px;
94-
left: 20px;
9593
border-color: rgba(0, 0, 0, 0.38);
9694
border-radius: 5px;
9795
opacity: .8;

src/jupyter_contrib_nbextensions/nbextensions/toc2/toc2.js

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414

1515
// globally-used status variables:
1616
var rendering_toc_cell = false;
17-
// oldTocSize also becomes the default size for a floating toc in a
18-
// non-live notebook
19-
var oldTocSize = {height: 'calc(100% - 180px)', width: '200px'};
17+
// toc_position default also serves as the defaults for a non-live notebook
18+
var toc_position = {height: 'calc(100% - 180px)', width: '20%', left: '10px', top: '150px'};
2019

2120
try {
2221
// this will work in a live notebook because nbextensions & custom.js
@@ -188,7 +187,10 @@
188187
}
189188

190189
var saveTocPosition = function () {
191-
setMd('toc_position', $('#toc-wrapper').css(['left', 'top', 'height', 'width']));
190+
var toc_wrapper = $('#toc-wrapper');
191+
var new_values = toc_wrapper.hasClass('sidebar-wrapper') ? ['width'] : ['left', 'top', 'height', 'width'];
192+
$.extend(toc_position, toc_wrapper.css(new_values));
193+
setMd('toc_position', toc_position);
192194
};
193195

194196
var makeUnmakeMinimized = function (cfg, animate) {
@@ -197,10 +199,9 @@
197199
var anim_opts = {duration: animate ? 'fast' : 0};
198200
if (open) {
199201
$('#toc').show();
200-
new_css = cfg.sideBar ? {} : oldTocSize;
202+
new_css = cfg.sideBar ? {} : {height: toc_position.height, width: toc_position.width};
201203
}
202204
else {
203-
oldTocSize = wrap.css(['height', 'width']);
204205
new_css = {
205206
height: wrap.outerHeight() - wrap.find('#toc').outerHeight(),
206207
};
@@ -215,7 +216,7 @@
215216
return open;
216217
};
217218

218-
var makeUnmakeSidebar = function (cfg, save_size) {
219+
var makeUnmakeSidebar = function (cfg) {
219220
var make_sidebar = cfg.sideBar;
220221
var view_rect = (liveNotebook ? document.getElementById('site') : document.body).getBoundingClientRect();
221222
var wrap = $('#toc-wrapper')
@@ -225,13 +226,10 @@
225226
wrap.children('.ui-resizable-se').toggleClass('ui-icon', !make_sidebar);
226227
wrap.children('.ui-resizable-e').toggleClass('ui-icon ui-icon-grip-dotted-vertical', make_sidebar);
227228
if (make_sidebar) {
228-
if (save_size) {
229-
oldTocSize = wrap.css(['height', 'width']);
230-
}
231229
wrap.css({top: view_rect.top, height: '', left: 0});
232230
}
233231
else {
234-
wrap.css({height: oldTocSize.height, width: oldTocSize.width});
232+
wrap.css({height: toc_position.height});
235233
}
236234
setNotebookWidth(cfg);
237235
};
@@ -280,11 +278,10 @@
280278
ui.position.left = 0;
281279
}
282280
if (make_sidebar !== cfg.sideBar) {
283-
var was_minimized = cfg.toc_section_display;
284281
cfg.toc_section_display = setMd('toc_section_display', true);
285282
cfg.sideBar = setMd('sideBar', make_sidebar);
286283
makeUnmakeMinimized(cfg);
287-
makeUnmakeSidebar(cfg, was_minimized);
284+
makeUnmakeSidebar(cfg);
288285
}
289286
}, //end of drag function
290287
stop: saveTocPosition,
@@ -318,18 +315,14 @@
318315
$(window).on('resize', callbackPageResize);
319316
if (liveNotebook) {
320317
events.on("resize-header.Page toggle-all-headers", callbackPageResize);
321-
// restore toc position at load
322-
var toc_pos = IPython.notebook.metadata.toc.toc_position;
323-
if (toc_pos !== undefined) {
324-
toc_wrapper.css(cfg.sideBar ? {width: toc_pos.width} : toc_pos);
325-
oldTocSize.width = toc_pos.width;
326-
oldTocSize.height = toc_pos.height;
327-
}
318+
$.extend(toc_position, IPython.notebook.metadata.toc.toc_position);
328319
}
329320
else {
330321
// default to true for non-live notebook
331322
$.extend(true, cfg, {toc_window_display: true, toc_section_display: true});
332323
}
324+
// restore toc position at load
325+
toc_wrapper.css(cfg.sideBar ? {width: toc_position.width} : toc_position);
333326
// older toc2 versions stored string representations, so update those
334327
if (cfg.toc_window_display === 'none') {
335328
cfg.toc_window_display = setMd('toc_window_display', false);

0 commit comments

Comments
 (0)