Skip to content

Commit 08ab4cb

Browse files
authored
Merge pull request #1120 from jcb91/t2_1
[toc2] simplify ToC cell processing
2 parents 2091dc8 + 4fa87f0 commit 08ab4cb

File tree

3 files changed

+42
-43
lines changed

3 files changed

+42
-43
lines changed

src/jupyter_contrib_nbextensions/nbextensions/toc2/main.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@ define([
5959

6060
//.....................global variables....
6161
var st={}
62-
st.rendering_toc_cell = false;
6362
st.oldTocHeight = undefined
64-
st.cell_toc = undefined;
65-
st.toc_index = 0;
6663

6764
var read_config = function (cfg, callback) {
6865
IPython.notebook.config.loaded.then(function () {

src/jupyter_contrib_nbextensions/nbextensions/toc2/toc2.js

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
var liveNotebook = false;
1212
var all_headers = $("#notebook").find(":header");
1313

14+
// globally-used status variables:
15+
var rendering_toc_cell = false;
16+
1417
try {
1518
// this will work in a live notebook because nbextensions & custom.js
1619
// are loaded by/after notebook.js, which requires base/js/namespace
@@ -513,29 +516,51 @@
513516
// Optionnaly, the sections in the toc can be numbered.
514517

515518
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;
516535
// look for a possible toc cell
517536
var cells = IPython.notebook.get_cells();
518537
var lcells = cells.length;
519538
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];
523545
break;
524546
}
525547
}
526548
//if toc_cell=true, we want a cell_toc.
527549
// 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
530550
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;
535557
}
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);
539564
}
540565
} //end function process_cell_toc --------------------------
541566

@@ -575,8 +600,9 @@
575600
// Table of Contents =================================================================
576601
var table_of_contents = function(cfg, st) {
577602

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) {
580606
return
581607
}
582608

@@ -591,13 +617,6 @@
591617
// update toc element
592618
$("#toc").empty().append(ul);
593619

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";
601620
var depth = 1;
602621
var li = ul; //yes, initialize li with ul!
603622
all_headers = $("#notebook").find(":header"); // update all_headers
@@ -683,20 +702,8 @@
683702
if ($('#Navigate_menu').length > 0) $('#Navigate_sub').remove()
684703
}
685704

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);
700707

701708
// add collapse controls
702709
$('<i>')
@@ -734,8 +741,6 @@
734741
IPython.notebook.metadata.toc['toc_window_display'] = $('#toc-wrapper').css('display') == 'block';
735742
IPython.notebook.set_dirty();
736743
}
737-
// recompute:
738-
st.rendering_toc_cell = false;
739744
table_of_contents(cfg, st);
740745
}
741746
});

src/jupyter_contrib_nbextensions/templates/toc2.tpl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ $( document ).ready(function(){
3939
}
4040
4141
var st={}; // some variables used in the script
42-
st.rendering_toc_cell = false;
4342
st.oldTocHeight = undefined
44-
st.cell_toc = undefined;
45-
st.toc_index=0;
4643
4744
// fire the main function with these parameters
4845
require(['nbextensions/toc2/toc2'], function (toc2) {

0 commit comments

Comments
 (0)