Skip to content

Commit 5c077f5

Browse files
authored
Merge pull request #1167 from jcb91/t2_0
[toc2] move config into toc2.js
2 parents b9ee02d + 1972313 commit 5c077f5

File tree

2 files changed

+75
-81
lines changed

2 files changed

+75
-81
lines changed

src/jupyter_contrib_nbextensions/nbextensions/toc2/main.js

Lines changed: 16 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -24,63 +24,6 @@ define([
2424
var table_of_contents = toc2.table_of_contents;
2525
var toggle_toc = toc2.toggle_toc;
2626

27-
// ...........Parameters configuration......................
28-
// default values for system-wide configurable parameters
29-
var cfg={'threshold':4,
30-
'navigate_menu':true,
31-
'markTocItemOnScroll': true,
32-
'moveMenuLeft': true,
33-
'widenNotebook': false,
34-
'colors': {
35-
'hover_highlight': '#DAA520',
36-
'selected_highlight': '#FFD700',
37-
'running_highlight': '#FF0000',
38-
'wrapper_background': '#FFFFFF',
39-
'sidebar_border': '#EEEEEE',
40-
'navigate_text': '#333333',
41-
'navigate_num': '#000000',
42-
'on_scroll': '#2447f0',
43-
},
44-
collapse_to_match_collapsible_headings: false,
45-
}
46-
// default values for per-notebook configurable parameters
47-
var metadata_settings = {
48-
nav_menu: {},
49-
number_sections: true,
50-
sideBar: true,
51-
skip_h1_title: false,
52-
title_cell: 'Table of Contents',
53-
title_sidebar: 'Contents',
54-
toc_cell: false,
55-
toc_position: {},
56-
toc_section_display: true,
57-
toc_window_display: false,
58-
};
59-
// add per-notebook settings into global config object
60-
$.extend(true, cfg, metadata_settings);
61-
62-
var read_config = function (cfg, callback) {
63-
IPython.notebook.config.loaded.then(function () {
64-
// config may be specified at system level or at document level.
65-
// first, update defaults with config loaded from server
66-
$.extend(true, cfg, IPython.notebook.config.data.toc2);
67-
// ensure notebook metadata has toc object, cache old values
68-
var md = IPython.notebook.metadata.toc || {};
69-
// reset notebook metadata to remove old values
70-
IPython.notebook.metadata.toc = {};
71-
// then update cfg with any found in current notebook metadata
72-
// and save in nb metadata (then can be modified per document)
73-
Object.keys(metadata_settings).forEach(function (key) {
74-
cfg[key] = IPython.notebook.metadata.toc[key] = (md.hasOwnProperty(key) ? md : cfg)[key];
75-
});
76-
// create highlights style section in document
77-
create_additional_css();
78-
// call callbacks
79-
callback && callback();
80-
});
81-
return cfg;
82-
};
83-
8427
// extra download as html with toc menu (needs IPython kernel)
8528
function addSaveAsWithToc() {
8629
if (IPython.notebook.metadata.kernelspec.language == 'python') {
@@ -106,28 +49,19 @@ define([
10649
}
10750
}
10851

109-
110-
111-
// **********************************************************************
112-
113-
//***********************************************************************
114-
// ----------------------------------------------------------------------
115-
116-
function toggleToc() {
117-
toggle_toc(cfg)
118-
}
119-
120-
var toc_button = function() {
52+
var toc_button = function(cfg) {
12153
if (!IPython.toolbar) {
122-
events.on("app_initialized.NotebookApp", toc_button);
54+
events.on("app_initialized.NotebookApp", function (evt) {
55+
toc_button(cfg);
56+
});
12357
return;
12458
}
12559
if ($("#toc_button").length === 0) {
12660
$(IPython.toolbar.add_buttons_group([
12761
Jupyter.keyboard_manager.actions.register ({
12862
'help' : 'Table of Contents',
12963
'icon' : 'fa-list',
130-
'handler': toggleToc,
64+
'handler': function() { toggle_toc(cfg); },
13165
}, 'toggle-toc', 'toc2')
13266
])).find('.btn').attr('id', 'toc_button');
13367
}
@@ -141,8 +75,7 @@ define([
14175
document.getElementsByTagName("head")[0].appendChild(link);
14276
};
14377

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

208141
var toc_init = function() {
209142
// read configuration, then call toc
210-
cfg = read_config(cfg, function() {
143+
IPython.notebook.config.loaded.then(function () {
144+
var cfg = toc2.read_config();
145+
// create highlights style section in document
146+
create_additional_css(cfg);
147+
// call main function with newly loaded config
211148
table_of_contents(cfg);
212-
}); // called after config is stable
213-
// event: render toc for each markdown cell modification
214-
events.on("rendered.MarkdownCell",
215-
function(evt, data) {
149+
// event: render toc for each markdown cell modification
150+
events.on("rendered.MarkdownCell", function(evt, data) {
216151
table_of_contents(cfg); // recompute the toc
217152
rehighlight_running_cells() // re-highlight running cells
218153
highlight_toc_item(evt, data); // and of course the one currently rendered
219154
});
155+
});
156+
220157
// event: on cell selection, highlight the corresponding item
221158
events.on('select.Cell', highlight_toc_item);
222159
// event: if kernel_ready (kernel change/restart): add/remove a menu item
@@ -226,13 +163,11 @@ define([
226163

227164
// add a save as HTML with toc included
228165
addSaveAsWithToc();
229-
//
230166
// Highlight cell on execution
231167
patch_CodeCell_get_callbacks()
232-
events.on('execute.CodeCell', excute_codecell_callback);
168+
events.on('execute.CodeCell', highlight_toc_item);
233169
}
234170

235-
236171
var load_ipython_extension = function() {
237172
load_css(); //console.log("Loading css")
238173
toc_button(); //console.log("Adding toc_button")

src/jupyter_contrib_nbextensions/nbextensions/toc2/toc2.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,64 @@
1212
var liveNotebook = false;
1313
var all_headers = $("#notebook").find(":header");
1414

15+
// default values for system-wide configurable parameters
16+
var default_cfg = {
17+
colors: {
18+
hover_highlight: '#DAA520',
19+
selected_highlight: '#FFD700',
20+
running_highlight: '#FF0000',
21+
wrapper_background: '#FFFFFF',
22+
sidebar_border: '#EEEEEE',
23+
navigate_text: '#333333',
24+
navigate_num: '#000000',
25+
on_scroll: '#2447f0',
26+
},
27+
collapse_to_match_collapsible_headings: false,
28+
markTocItemOnScroll: true,
29+
moveMenuLeft: true,
30+
navigate_menu: true,
31+
threshold: 4,
32+
widenNotebook: false,
33+
};
34+
// default values for per-notebook configurable parameters
35+
var metadata_settings = {
36+
nav_menu: {},
37+
number_sections: true,
38+
sideBar: true,
39+
skip_h1_title: false,
40+
title_cell: 'Table of Contents',
41+
title_sidebar: 'Contents',
42+
toc_cell: false,
43+
toc_position: {},
44+
toc_section_display: true,
45+
toc_window_display: false,
46+
};
47+
$.extend(true, default_cfg, metadata_settings);
48+
49+
/**
50+
* Read our config from server config & notebook metadata
51+
* This function should only be called when both:
52+
* 1. the notebook (and its metadata) has fully loaded
53+
* AND
54+
* 2. Jupyter.notebook.config.loaded has resolved
55+
*/
56+
var read_config = function () {
57+
var cfg = default_cfg;
58+
// config may be specified at system level or at document level.
59+
// first, update defaults with config loaded from server
60+
$.extend(true, cfg, IPython.notebook.config.data.toc2);
61+
// ensure notebook metadata has toc object, cache old values
62+
var md = IPython.notebook.metadata.toc || {};
63+
// reset notebook metadata to remove old values
64+
IPython.notebook.metadata.toc = {};
65+
// then update cfg with any found in current notebook metadata
66+
// and save in nb metadata (then can be modified per document)
67+
Object.keys(metadata_settings).forEach(function (key) {
68+
cfg[key] = IPython.notebook.metadata.toc[key] = (md.hasOwnProperty(key) ? md : cfg)[key];
69+
});
70+
return cfg;
71+
};
72+
1573
// globally-used status variables:
1674
var rendering_toc_cell = false;
1775
// toc_position default also serves as the defaults for a non-live notebook
@@ -707,6 +765,7 @@
707765
highlight_toc_item: highlight_toc_item,
708766
table_of_contents: table_of_contents,
709767
toggle_toc: toggle_toc,
768+
read_config: read_config,
710769
};
711770
});
712771
// export table_of_contents to global namespace for backwards compatibility

0 commit comments

Comments
 (0)