|
185 | 185 | nb_inner.css(inner_css);
|
186 | 186 | }
|
187 | 187 |
|
188 |
| - function setSideBarHeight(cfg, st) { |
189 |
| - if (cfg.sideBar) { |
190 |
| - var headerVisibleHeight = $('#header').is(':visible') ? $('#header').height() : 0 |
191 |
| - $('#toc-wrapper').css('top', liveNotebook ? headerVisibleHeight : 0) |
192 |
| - $('#toc-wrapper').css('height', $('#site').height()); |
193 |
| - $('#toc').css('height', $('#toc-wrapper').height() - $('#toc-header').height()) |
194 |
| - } |
195 |
| - } |
196 |
| - |
197 | 188 | var makeUnmakeMinimized = function (cfg, animate) {
|
198 | 189 | var open = cfg.sideBar || cfg.toc_section_display;
|
199 | 190 | var new_css, wrap = $('#toc-wrapper');
|
|
218 | 209 | return open;
|
219 | 210 | };
|
220 | 211 |
|
| 212 | + var makeUnmakeSidebar = function (cfg, save_size) { |
| 213 | + var make_sidebar = cfg.sideBar; |
| 214 | + var view_rect = (liveNotebook ? document.getElementById('site') : document.body).getBoundingClientRect(); |
| 215 | + var wrap = $('#toc-wrapper') |
| 216 | + .toggleClass('sidebar-wrapper', make_sidebar) |
| 217 | + .toggleClass('float-wrapper', !make_sidebar) |
| 218 | + .resizable('option', 'handles', make_sidebar ? 'e' : 'all'); |
| 219 | + wrap.children('.ui-resizable-se').toggleClass('ui-icon', !make_sidebar); |
| 220 | + wrap.children('.ui-resizable-e').toggleClass('ui-icon ui-icon-grip-dotted-vertical', make_sidebar); |
| 221 | + if (make_sidebar) { |
| 222 | + if (save_size) { |
| 223 | + oldTocSize = wrap.css(['height', 'width']); |
| 224 | + } |
| 225 | + wrap.css({top: view_rect.top, height: '', left: 0}); |
| 226 | + } |
| 227 | + else { |
| 228 | + wrap.css({height: oldTocSize.height, width: oldTocSize.width}); |
| 229 | + } |
| 230 | + setNotebookWidth(cfg); |
| 231 | + }; |
| 232 | + |
221 | 233 | var create_toc_div = function(cfg, st) {
|
| 234 | + |
| 235 | + var callbackPageResize = function (evt) { |
| 236 | + setNotebookWidth(cfg); |
| 237 | + }; |
| 238 | + |
222 | 239 | var toc_wrapper = $('<div id="toc-wrapper"/>')
|
223 | 240 | .css('display', 'none')
|
224 | 241 | .append(
|
|
246 | 263 | ).append(
|
247 | 264 | $("<div/>").attr("id", "toc").addClass('toc')
|
248 | 265 | )
|
249 |
| - |
250 |
| - $("body").append(toc_wrapper); |
251 |
| - |
252 |
| - // On header/menu/toolbar resize, resize the toc itself |
253 |
| - // (if displayed as a sidebar) |
254 |
| - if (liveNotebook) { |
255 |
| - events.on("resize-header.Page", function() { |
256 |
| - setSideBarHeight(cfg, st); |
257 |
| - }); |
258 |
| - events.on("toggle-all-headers", function() { |
259 |
| - setSideBarHeight(cfg, st); |
260 |
| - }); |
261 |
| - } |
| 266 | + .prependTo(liveNotebook ? '#site' : document.body); |
262 | 267 |
|
263 | 268 | // enable dragging and save position on stop moving
|
264 |
| - $('#toc-wrapper').draggable({ |
265 |
| - |
| 269 | + toc_wrapper.draggable({ |
266 | 270 | drag: function(event, ui) {
|
267 |
| - |
268 |
| - // If dragging to the left side, then transforms in sidebar |
269 |
| - if ((ui.position.left <= 0) && (cfg.sideBar == false)) { |
270 |
| - cfg.sideBar = true; |
271 |
| - st.oldTocHeight = $('#toc-wrapper').css('height'); |
272 |
| - if (liveNotebook) { |
273 |
| - IPython.notebook.metadata.toc['sideBar'] = true; |
274 |
| - IPython.notebook.set_dirty(); |
275 |
| - } |
276 |
| - toc_wrapper.removeClass('float-wrapper').addClass('sidebar-wrapper'); |
277 |
| - setNotebookWidth(cfg, st) |
278 |
| - var headerVisibleHeight = $('#header').is(':visible') ? $('#header').height() : 0 |
279 |
| - ui.position.top = liveNotebook ? headerVisibleHeight : 0; |
| 271 | + var make_sidebar = ui.position.left < 20; // 20 is snapTolerance |
| 272 | + if (make_sidebar) { |
| 273 | + ui.position.top = (liveNotebook ? document.getElementById('site') : document.body).getBoundingClientRect().top; |
280 | 274 | ui.position.left = 0;
|
281 |
| - if (liveNotebook) { |
282 |
| - $('#toc-wrapper').css('height', $('#site').height()); |
283 |
| - } else { |
284 |
| - $('#toc-wrapper').css('height', '96%'); |
285 |
| - } |
286 |
| - $('#toc').css('height', $('#toc-wrapper').height() - $('#toc-header').height()); |
287 | 275 | }
|
288 |
| - if (ui.position.left <= 0) { |
289 |
| - ui.position.left = 0; |
290 |
| - var headerVisibleHeight = $('#header').is(':visible') ? $('#header').height() : 0 |
291 |
| - ui.position.top = liveNotebook ? headerVisibleHeight : 0; |
292 |
| - } |
293 |
| - if ((ui.position.left > 0) && (cfg.sideBar == true)) { |
294 |
| - cfg.sideBar = false; |
295 |
| - if (liveNotebook) { |
296 |
| - IPython.notebook.metadata.toc['sideBar'] = false; |
297 |
| - IPython.notebook.set_dirty(); |
298 |
| - } |
299 |
| - if (st.oldTocHeight == undefined) st.oldTocHeight = Math.max($('#site').height() / 2, 200) |
300 |
| - $('#toc-wrapper').css('height', st.oldTocHeight); |
301 |
| - toc_wrapper.removeClass('sidebar-wrapper').addClass('float-wrapper'); |
302 |
| - setNotebookWidth(cfg, st) |
303 |
| - $('#toc').css('height', $('#toc-wrapper').height() - $('#toc-header').height()); //redraw at begin of of drag (after resizing height) |
304 |
| - |
| 276 | + if (make_sidebar !== cfg.sideBar) { |
| 277 | + var was_minimized = cfg.toc_section_display; |
| 278 | + cfg.toc_section_display = setMd('toc_section_display', true); |
| 279 | + cfg.sideBar = setMd('sideBar', make_sidebar); |
| 280 | + makeUnmakeMinimized(cfg); |
| 281 | + makeUnmakeSidebar(cfg, was_minimized); |
305 | 282 | }
|
306 | 283 | }, //end of drag function
|
307 |
| - start: function(event, ui) { |
308 |
| - $(this).width($(this).width()); |
309 |
| - }, |
310 | 284 | stop: function(event, ui) { // on save, store toc position
|
311 | 285 | if (liveNotebook) {
|
312 | 286 | IPython.notebook.metadata.toc['toc_position'] = {
|
|
321 | 295 | // Ensure position is fixed (again)
|
322 | 296 | $('#toc-wrapper').css('position', 'fixed');
|
323 | 297 | },
|
324 |
| - containment: 'body', |
| 298 | + containment: 'parent', |
325 | 299 | snap: 'body, #site',
|
| 300 | + snapTolerance: 20, |
326 | 301 | });
|
327 | 302 |
|
328 |
| - $('#toc-wrapper').resizable({ |
| 303 | + toc_wrapper.resizable({ |
329 | 304 | resize: function(event, ui) {
|
330 | 305 | if (cfg.sideBar) {
|
331 | 306 | setNotebookWidth(cfg, st)
|
332 |
| - } else { |
333 |
| - $('#toc').css('height', $('#toc-wrapper').height() - $('#toc-header').height()); |
334 | 307 | }
|
335 | 308 | },
|
336 | 309 | start: function(event, ui) {
|
337 |
| - $(this).width($(this).width()); |
338 | 310 | if (!cfg.sideBar) {
|
339 | 311 | cfg.toc_section_display = setMd('toc_section_display', true);
|
340 | 312 | makeUnmakeMinimized(cfg);
|
|
352 | 324 | $('#toc').css('height', $('#toc-wrapper').height() - $('#toc-header').height())
|
353 | 325 | IPython.notebook.set_dirty();
|
354 | 326 | }
|
355 |
| - } |
| 327 | + }, |
| 328 | + containment: 'parent', |
| 329 | + minHeight: 100, |
| 330 | + minWidth: 165, |
356 | 331 | })
|
357 | 332 |
|
358 |
| - $("body").append(toc_wrapper); |
359 |
| - |
360 | 333 | // On header/menu/toolbar resize, resize the toc itself
|
361 | 334 | // (if displayed as a sidebar)
|
| 335 | + $(window).on('resize', callbackPageResize); |
362 | 336 | if (liveNotebook) {
|
363 |
| - events.on("resize-header.Page toggle-all-headers", function() { |
364 |
| - setSideBarHeight(cfg, st); |
365 |
| - }); |
| 337 | + events.on("resize-header.Page toggle-all-headers", callbackPageResize); |
366 | 338 | }
|
367 | 339 |
|
368 | 340 | // restore toc position at load
|
|
433 | 405 | } else {
|
434 | 406 | toc_wrapper.addClass('float-wrapper');
|
435 | 407 | }
|
436 |
| - } |
| 408 | + }; |
437 | 409 |
|
438 | 410 | //----------------------------------------------------------------------------
|
439 | 411 | // on scroll - mark the toc item corresponding to the first header visible in
|
|
568 | 540 | return
|
569 | 541 | }
|
570 | 542 |
|
571 |
| - |
572 | 543 | var toc_wrapper = $("#toc-wrapper");
|
573 | 544 | if (toc_wrapper.length === 0) { // toc window doesn't exist at all
|
574 | 545 | create_toc_div(cfg, st); // create it
|
|
671 | 642 |
|
672 | 643 | events[cfg.collapse_to_match_collapsible_headings ? 'on' : 'off'](
|
673 | 644 | 'collapse.CollapsibleHeading uncollapse.CollapsibleHeading', callback_toc2_collapsible_headings);
|
674 |
| - |
675 |
| - $(window).resize(function() { |
676 |
| - $('#toc').css({ |
677 |
| - maxHeight: $(window).height() - 30 |
678 |
| - }); |
679 |
| - $('#toc-wrapper').css({ |
680 |
| - maxHeight: $(window).height() - 10 |
681 |
| - }); |
682 |
| - setSideBarHeight(cfg, st), |
683 |
| - setNotebookWidth(cfg, st); |
684 |
| - }); |
685 |
| - |
686 |
| - $(window).trigger('resize'); |
687 |
| - |
688 | 645 | };
|
689 | 646 |
|
690 | 647 | var toggle_toc = function(cfg, st) {
|
|
0 commit comments