Skip to content

Commit

Permalink
[toc2] move config into toc2.js
Browse files Browse the repository at this point in the history
place default config & config loading into toc2.js, where they're used,
to prevent duplicating stuff in both main.js and toc2.tpl, and avoid
issues like that in
    #1034 (comment)
  • Loading branch information
jcb91 committed Dec 4, 2017
1 parent b9ee02d commit 1972313
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 81 deletions.
97 changes: 16 additions & 81 deletions src/jupyter_contrib_nbextensions/nbextensions/toc2/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,63 +24,6 @@ define([
var table_of_contents = toc2.table_of_contents;
var toggle_toc = toc2.toggle_toc;

// ...........Parameters configuration......................
// default values for system-wide configurable parameters
var cfg={'threshold':4,
'navigate_menu':true,
'markTocItemOnScroll': true,
'moveMenuLeft': true,
'widenNotebook': false,
'colors': {
'hover_highlight': '#DAA520',
'selected_highlight': '#FFD700',
'running_highlight': '#FF0000',
'wrapper_background': '#FFFFFF',
'sidebar_border': '#EEEEEE',
'navigate_text': '#333333',
'navigate_num': '#000000',
'on_scroll': '#2447f0',
},
collapse_to_match_collapsible_headings: false,
}
// default values for per-notebook configurable parameters
var metadata_settings = {
nav_menu: {},
number_sections: true,
sideBar: true,
skip_h1_title: false,
title_cell: 'Table of Contents',
title_sidebar: 'Contents',
toc_cell: false,
toc_position: {},
toc_section_display: true,
toc_window_display: false,
};
// add per-notebook settings into global config object
$.extend(true, cfg, metadata_settings);

var read_config = function (cfg, callback) {
IPython.notebook.config.loaded.then(function () {
// config may be specified at system level or at document level.
// first, update defaults with config loaded from server
$.extend(true, cfg, IPython.notebook.config.data.toc2);
// ensure notebook metadata has toc object, cache old values
var md = IPython.notebook.metadata.toc || {};
// reset notebook metadata to remove old values
IPython.notebook.metadata.toc = {};
// then update cfg with any found in current notebook metadata
// and save in nb metadata (then can be modified per document)
Object.keys(metadata_settings).forEach(function (key) {
cfg[key] = IPython.notebook.metadata.toc[key] = (md.hasOwnProperty(key) ? md : cfg)[key];
});
// create highlights style section in document
create_additional_css();
// call callbacks
callback && callback();
});
return cfg;
};

// extra download as html with toc menu (needs IPython kernel)
function addSaveAsWithToc() {
if (IPython.notebook.metadata.kernelspec.language == 'python') {
Expand All @@ -106,28 +49,19 @@ define([
}
}



// **********************************************************************

//***********************************************************************
// ----------------------------------------------------------------------

function toggleToc() {
toggle_toc(cfg)
}

var toc_button = function() {
var toc_button = function(cfg) {
if (!IPython.toolbar) {
events.on("app_initialized.NotebookApp", toc_button);
events.on("app_initialized.NotebookApp", function (evt) {
toc_button(cfg);
});
return;
}
if ($("#toc_button").length === 0) {
$(IPython.toolbar.add_buttons_group([
Jupyter.keyboard_manager.actions.register ({
'help' : 'Table of Contents',
'icon' : 'fa-list',
'handler': toggleToc,
'handler': function() { toggle_toc(cfg); },
}, 'toggle-toc', 'toc2')
])).find('.btn').attr('id', 'toc_button');
}
Expand All @@ -141,8 +75,7 @@ define([
document.getElementsByTagName("head")[0].appendChild(link);
};


function create_additional_css() {
function create_additional_css(cfg) {
var sheet = document.createElement('style')
sheet.innerHTML = "#toc li > span:hover { background-color: " + cfg.colors.hover_highlight + " }\n" +
".toc-item-highlight-select {background-color: " + cfg.colors.selected_highlight + "}\n" +
Expand Down Expand Up @@ -207,16 +140,20 @@ define([

var toc_init = function() {
// read configuration, then call toc
cfg = read_config(cfg, function() {
IPython.notebook.config.loaded.then(function () {
var cfg = toc2.read_config();
// create highlights style section in document
create_additional_css(cfg);
// call main function with newly loaded config
table_of_contents(cfg);
}); // called after config is stable
// event: render toc for each markdown cell modification
events.on("rendered.MarkdownCell",
function(evt, data) {
// event: render toc for each markdown cell modification
events.on("rendered.MarkdownCell", function(evt, data) {
table_of_contents(cfg); // recompute the toc
rehighlight_running_cells() // re-highlight running cells
highlight_toc_item(evt, data); // and of course the one currently rendered
});
});

// event: on cell selection, highlight the corresponding item
events.on('select.Cell', highlight_toc_item);
// event: if kernel_ready (kernel change/restart): add/remove a menu item
Expand All @@ -226,13 +163,11 @@ define([

// add a save as HTML with toc included
addSaveAsWithToc();
//
// Highlight cell on execution
patch_CodeCell_get_callbacks()
events.on('execute.CodeCell', excute_codecell_callback);
events.on('execute.CodeCell', highlight_toc_item);
}


var load_ipython_extension = function() {
load_css(); //console.log("Loading css")
toc_button(); //console.log("Adding toc_button")
Expand Down
59 changes: 59 additions & 0 deletions src/jupyter_contrib_nbextensions/nbextensions/toc2/toc2.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,64 @@
var liveNotebook = false;
var all_headers = $("#notebook").find(":header");

// default values for system-wide configurable parameters
var default_cfg = {
colors: {
hover_highlight: '#DAA520',
selected_highlight: '#FFD700',
running_highlight: '#FF0000',
wrapper_background: '#FFFFFF',
sidebar_border: '#EEEEEE',
navigate_text: '#333333',
navigate_num: '#000000',
on_scroll: '#2447f0',
},
collapse_to_match_collapsible_headings: false,
markTocItemOnScroll: true,
moveMenuLeft: true,
navigate_menu: true,
threshold: 4,
widenNotebook: false,
};
// default values for per-notebook configurable parameters
var metadata_settings = {
nav_menu: {},
number_sections: true,
sideBar: true,
skip_h1_title: false,
title_cell: 'Table of Contents',
title_sidebar: 'Contents',
toc_cell: false,
toc_position: {},
toc_section_display: true,
toc_window_display: false,
};
$.extend(true, default_cfg, metadata_settings);

/**
* Read our config from server config & notebook metadata
* This function should only be called when both:
* 1. the notebook (and its metadata) has fully loaded
* AND
* 2. Jupyter.notebook.config.loaded has resolved
*/
var read_config = function () {
var cfg = default_cfg;
// config may be specified at system level or at document level.
// first, update defaults with config loaded from server
$.extend(true, cfg, IPython.notebook.config.data.toc2);
// ensure notebook metadata has toc object, cache old values
var md = IPython.notebook.metadata.toc || {};
// reset notebook metadata to remove old values
IPython.notebook.metadata.toc = {};
// then update cfg with any found in current notebook metadata
// and save in nb metadata (then can be modified per document)
Object.keys(metadata_settings).forEach(function (key) {
cfg[key] = IPython.notebook.metadata.toc[key] = (md.hasOwnProperty(key) ? md : cfg)[key];
});
return cfg;
};

// globally-used status variables:
var rendering_toc_cell = false;
// toc_position default also serves as the defaults for a non-live notebook
Expand Down Expand Up @@ -707,6 +765,7 @@
highlight_toc_item: highlight_toc_item,
table_of_contents: table_of_contents,
toggle_toc: toggle_toc,
read_config: read_config,
};
});
// export table_of_contents to global namespace for backwards compatibility
Expand Down

0 comments on commit 1972313

Please sign in to comment.