|
11 | 11 | var liveNotebook = false;
|
12 | 12 | var all_headers = $("#notebook").find(":header");
|
13 | 13 |
|
| 14 | + // globally-used status variables: |
| 15 | + var rendering_toc_cell = false; |
| 16 | + |
14 | 17 | try {
|
15 | 18 | // this will work in a live notebook because nbextensions & custom.js
|
16 | 19 | // are loaded by/after notebook.js, which requires base/js/namespace
|
|
513 | 516 | // Optionnaly, the sections in the toc can be numbered.
|
514 | 517 |
|
515 | 518 | function process_cell_toc(cfg, st) {
|
| 519 | + var cell_toc_text = 'Table of Contents'; |
| 520 | + var new_html = '<h1>' + |
| 521 | + cell_toc_text + '<span class="tocSkip"></span></h1>\n' + |
| 522 | + '<div class="toc" style="margin-top: 1em;">' + |
| 523 | + $('#toc').html() + |
| 524 | + '</div>'; |
| 525 | + if (!liveNotebook) { |
| 526 | + if (cfg.toc_cell) { |
| 527 | + $('.cell > .toc').parent(':has(.tocSkip)') |
| 528 | + .html(new_html) |
| 529 | + .find('.toc-item li a') |
| 530 | + .on('click', callback_toc_link_click); |
| 531 | + } |
| 532 | + return; |
| 533 | + } |
| 534 | + var cell_toc; |
516 | 535 | // look for a possible toc cell
|
517 | 536 | var cells = IPython.notebook.get_cells();
|
518 | 537 | var lcells = cells.length;
|
519 | 538 | for (var i = 0; i < lcells; i++) {
|
520 |
| - if (cells[i].metadata.toc == "true") { |
521 |
| - st.cell_toc = cells[i]; |
522 |
| - st.toc_index = i; |
| 539 | + if (cells[i].metadata.toc) { |
| 540 | + // delete if we don't want it |
| 541 | + if (!cfg.toc_cell) { |
| 542 | + return IPython.notebook.delete_cell(i); |
| 543 | + } |
| 544 | + cell_toc = cells[i]; |
523 | 545 | break;
|
524 | 546 | }
|
525 | 547 | }
|
526 | 548 | //if toc_cell=true, we want a cell_toc.
|
527 | 549 | // If it does not exist, create it at the beginning of the notebook
|
528 |
| - //if toc_cell=false, we do not want a cell-toc. |
529 |
| - // If one exists, delete it |
530 | 550 | if (cfg.toc_cell) {
|
531 |
| - if (st.cell_toc == undefined) { |
532 |
| - st.rendering_toc_cell = true; |
533 |
| - st.cell_toc = IPython.notebook.select(0).insert_cell_above("markdown"); |
534 |
| - st.cell_toc.metadata.toc = "true"; |
| 551 | + if (cell_toc === undefined) { |
| 552 | + // set rendering_toc_cell flag to avoid loop on insert_cell_above |
| 553 | + rendering_toc_cell = true; |
| 554 | + cell_toc = IPython.notebook.insert_cell_above('markdown', 0); |
| 555 | + cell_toc.metadata.toc = true; |
| 556 | + rendering_toc_cell = false; |
535 | 557 | }
|
536 |
| - } else { |
537 |
| - if (st.cell_toc !== undefined) IPython.notebook.delete_cell(st.toc_index); |
538 |
| - st.rendering_toc_cell = false; |
| 558 | + // set rendering_toc_cell flag to avoid loop on render |
| 559 | + rendering_toc_cell = true; |
| 560 | + cell_toc.set_text(new_html); |
| 561 | + cell_toc.render(); |
| 562 | + rendering_toc_cell = false; |
| 563 | + cell_toc.element.find('.toc-item li a').on('click', callback_toc_link_click); |
539 | 564 | }
|
540 | 565 | } //end function process_cell_toc --------------------------
|
541 | 566 |
|
|
575 | 600 | // Table of Contents =================================================================
|
576 | 601 | var table_of_contents = function(cfg, st) {
|
577 | 602 |
|
578 |
| - if (st.rendering_toc_cell) { // if toc_cell is rendering, do not call table_of_contents, |
579 |
| - st.rendering_toc_cell = false; // otherwise it will loop |
| 603 | + // if this call is a result of toc_cell rendering, do nothing to avoid |
| 604 | + // looping, as we're already in a table_of_contents call |
| 605 | + if (rendering_toc_cell) { |
580 | 606 | return
|
581 | 607 | }
|
582 | 608 |
|
|
591 | 617 | // update toc element
|
592 | 618 | $("#toc").empty().append(ul);
|
593 | 619 |
|
594 |
| - st.cell_toc = undefined; |
595 |
| - // if cfg.toc_cell=true, add and update a toc cell in the notebook. |
596 |
| - if (liveNotebook) { |
597 |
| - process_cell_toc(cfg, st); |
598 |
| - } |
599 |
| - |
600 |
| - var cell_toc_text = " # Table of Contents\n"; |
601 | 620 | var depth = 1;
|
602 | 621 | var li = ul; //yes, initialize li with ul!
|
603 | 622 | all_headers = $("#notebook").find(":header"); // update all_headers
|
|
683 | 702 | if ($('#Navigate_menu').length > 0) $('#Navigate_sub').remove()
|
684 | 703 | }
|
685 | 704 |
|
686 |
| - |
687 |
| - // check for st.cell_toc because we may have a non-live notebook where |
688 |
| - // metadata says to use cell_toc, but the actual cell's been removed |
689 |
| - if (cfg.toc_cell && st.cell_toc) { |
690 |
| - st.rendering_toc_cell = true; |
691 |
| - st.cell_toc.set_text( |
692 |
| - cell_toc_text + |
693 |
| - '<div class="toc" style="margin-top: 1em;">' + |
694 |
| - $('#toc').html() + |
695 |
| - '</div>' |
696 |
| - ); |
697 |
| - st.cell_toc.render(); |
698 |
| - st.cell_toc.element.find('.toc-item li a').on('click', callback_toc_link_click); |
699 |
| - }; |
| 705 | + // if cfg.toc_cell=true, find/add and update a toc cell in the notebook. |
| 706 | + process_cell_toc(cfg, st); |
700 | 707 |
|
701 | 708 | // add collapse controls
|
702 | 709 | $('<i>')
|
|
734 | 741 | IPython.notebook.metadata.toc['toc_window_display'] = $('#toc-wrapper').css('display') == 'block';
|
735 | 742 | IPython.notebook.set_dirty();
|
736 | 743 | }
|
737 |
| - // recompute: |
738 |
| - st.rendering_toc_cell = false; |
739 | 744 | table_of_contents(cfg, st);
|
740 | 745 | }
|
741 | 746 | });
|
|
0 commit comments