diff --git a/src/jupyter_contrib_nbextensions/nbextensions/toc2/toc2.js b/src/jupyter_contrib_nbextensions/nbextensions/toc2/toc2.js index bf4728d..62c6cd4 100644 --- a/src/jupyter_contrib_nbextensions/nbextensions/toc2/toc2.js +++ b/src/jupyter_contrib_nbextensions/nbextensions/toc2/toc2.js @@ -1,15 +1,15 @@ -(require.specified('base/js/namespace') ? define : function (deps, callback) { +(require.specified('base/js/namespace') ? define : function(deps, callback) { // if here, the Jupyter namespace hasn't been specified to be loaded. // This means that we're probably embedded in a page, so we need to make // our definition with a specific module name return define('nbextensions/toc2/toc2', deps, callback); -})(['jquery', 'require'], function ($, require) { +})(['jquery', 'require'], function($, require) { "use strict"; var IPython; var events; var liveNotebook = false; - var all_headers= $("#notebook").find(":header"); + var all_headers = $("#notebook").find(":header"); try { // this will work in a live notebook because nbextensions & custom.js @@ -17,38 +17,41 @@ IPython = require('base/js/namespace'); events = require('base/js/events'); liveNotebook = true; - } - catch (err) { + } catch (err) { // We *are* theoretically in a non-live notebook console.log('[toc2] working in non-live notebook'); //, err); // in non-live notebook, there's no event structure, so we make our own if (window.events === undefined) { - var Events = function () {}; + var Events = function() {}; window.events = $([new Events()]); } events = window.events; } -function incr_lbl(ary, h_idx) { //increment heading label w/ h_idx (zero based) - ary[h_idx]++; - for (var j = h_idx + 1; j < ary.length; j++) { ary[j] = 0; } - return ary.slice(0, h_idx + 1); -} + function incr_lbl(ary, h_idx) { //increment heading label w/ h_idx (zero based) + ary[h_idx]++; + for (var j = h_idx + 1; j < ary.length; j++) { + ary[j] = 0; + } + return ary.slice(0, h_idx + 1); + } -function removeMathJaxPreview(elt) { - elt.children('.anchor-link, .toc-mod-link').remove(); - elt.find("script[type='math/tex']").each( - function(i, e) { - $(e).replaceWith('$' + $(e).text() + '$') - }) - elt.find("span.MathJax_Preview").remove() - elt.find("span.MathJax").remove() - return elt -} - - var callback_toc_link_click = function (evt) { + function removeMathJaxPreview(elt) { + elt.children('.anchor-link, .toc-mod-link').remove(); + elt.find("script[type='math/tex']").each( + function(i, e) { + $(e).replaceWith('$' + $(e).text() + '$') + }) + elt.find("span.MathJax_Preview").remove() + elt.find("span.MathJax").remove() + return elt + } + + var callback_toc_link_click = function(evt) { // workaround for https://github.com/jupyter/notebook/issues/699 - setTimeout(function() { $.ajax() }, 100); + setTimeout(function() { + $.ajax() + }, 100); evt.preventDefault(); var trg_id = $(evt.currentTarget).attr('data-toc-modified-id'); // use native scrollIntoView method with semi-unique id @@ -60,485 +63,515 @@ function removeMathJaxPreview(elt) { // like . in an id var cell = $(document.getElementById(trg_id)).closest('.cell').data('cell'); Jupyter.notebook.select(Jupyter.notebook.find_cell_index(cell)); - highlight_toc_item("toc_link_click", {cell: cell}); + highlight_toc_item("toc_link_click", { + cell: cell + }); } }; -var make_link = function (h, toc_mod_id) { - var a = $('') - .attr({ - 'href': h.find('.anchor-link').attr('href'), - 'data-toc-modified-id': toc_mod_id, - }); - // get the text *excluding* the link text, whatever it may be - var hclone = h.clone(); - hclone = removeMathJaxPreview(hclone); - a.html(hclone.html()); - a.on('click', callback_toc_link_click); - return a; -}; + var make_link = function(h, toc_mod_id) { + var a = $('') + .attr({ + 'href': h.find('.anchor-link').attr('href'), + 'data-toc-modified-id': toc_mod_id, + }); + // get the text *excluding* the link text, whatever it may be + var hclone = h.clone(); + hclone = removeMathJaxPreview(hclone); + a.html(hclone.html()); + a.on('click', callback_toc_link_click); + return a; + }; - var ol_depth = function (element) { - // get depth of nested ol - var d = 0; - while (element.prop("tagName").toLowerCase() == 'ol') { - d += 1; - element = element.parent(); + var ol_depth = function(element) { + // get depth of nested ol + var d = 0; + while (element.prop("tagName").toLowerCase() == 'ol') { + d += 1; + element = element.parent(); + } + return d; + }; + + function highlight_toc_item(evt, data) { + var c = $(data.cell.element); + if (c.length < 1) { + return; + } + var trg_id = c.find('.toc-mod-link').attr('id') || + c.prevAll().find('.toc-mod-link').eq(-1).attr('id'); + var highlighted_item = $(); + if (trg_id !== undefined) { + highlighted_item = $('.toc a').filter(function(idx, elt) { + return $(elt).attr('data-toc-modified-id') === trg_id; + }); + } + if (evt.type === 'execute') { + // remove the selected class and add execute class + // if the cell is selected again, it will be highligted as selected+running + highlighted_item.removeClass('toc-item-highlight-select').addClass('toc-item-highlight-execute'); + } else { + $('.toc .toc-item-highlight-select').removeClass('toc-item-highlight-select'); + highlighted_item.addClass('toc-item-highlight-select'); + } } - return d; - }; -function highlight_toc_item(evt, data) { - var c = $(data.cell.element); - if (c.length < 1) { - return; + var create_navigate_menu = function(callback) { + $('#kernel_menu').parent().after('