Skip to content

Commit 2eb8186

Browse files
committed
[toc2] simplify config loading
1 parent a631fa3 commit 2eb8186

File tree

1 file changed

+31
-61
lines changed
  • src/jupyter_contrib_nbextensions/nbextensions/toc2

1 file changed

+31
-61
lines changed

src/jupyter_contrib_nbextensions/nbextensions/toc2/main.js

Lines changed: 31 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,9 @@ define([
2727
var toggle_toc = toc2.toggle_toc;
2828

2929
// ...........Parameters configuration......................
30-
// define default values for config parameters if they were not present in general settings (notebook.json)
30+
// default values for system-wide configurable parameters
3131
var cfg={'threshold':4,
32-
'number_sections':true,
33-
'toc_cell':false,
34-
'toc_window_display':false,
35-
"toc_section_display": "block",
36-
'sideBar':true,
37-
'navigate_menu':true,
32+
'navigate_menu':true,
3833
'moveMenuLeft': true,
3934
'widenNotebook': false,
4035
'colors': {
@@ -47,74 +42,49 @@ define([
4742
'navigate_num': '#000000',
4843
},
4944
collapse_to_match_collapsible_headings: false,
50-
skip_h1_title: false,
5145
}
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+
toc_cell: false,
53+
toc_position: {},
54+
toc_section_display: 'block',
55+
toc_window_display: false,
56+
};
57+
// add per-notebook settings into global config object
58+
$.extend(true, cfg, metadata_settings);
5259

5360
//.....................global variables....
54-
55-
var liveNotebook = !(typeof IPython == "undefined")
56-
5761
var st={}
5862
st.rendering_toc_cell = false;
59-
st.config_loaded = false;
60-
st.extension_initialized=false;
61-
62-
st.nbcontainer_marginleft = $('#notebook-container').css('margin-left')
63-
st.nbcontainer_marginright = $('#notebook-container').css('margin-right')
64-
st.nbcontainer_width = $('#notebook-container').css('width')
6563
st.oldTocHeight = undefined
66-
6764
st.cell_toc = undefined;
6865
st.toc_index=0;
6966

70-
71-
72-
function read_config(cfg, callback) { // read after nb is loaded
73-
// create config object to load parameters
74-
var base_url = utils.get_body_data("baseUrl");
75-
var initial_cfg = $.extend(true, {}, cfg);
76-
var config = new configmod.ConfigSection('notebook', { base_url: base_url });
77-
config.loaded.then(function(){
67+
var read_config = function (cfg, callback) {
68+
IPython.notebook.config.loaded.then(function () {
7869
// config may be specified at system level or at document level.
7970
// first, update defaults with config loaded from server
80-
cfg = $.extend(true, cfg, config.data.toc2);
71+
$.extend(true, cfg, IPython.notebook.config.data.toc2);
72+
// ensure notebook metadata has toc object, cache old values
73+
var md = IPython.notebook.metadata.toc || {};
74+
// reset notebook metadata to remove old values
75+
IPython.notebook.metadata.toc = {};
8176
// then update cfg with any found in current notebook metadata
8277
// and save in nb metadata (then can be modified per document)
83-
cfg = IPython.notebook.metadata.toc = $.extend(true, cfg,
84-
IPython.notebook.metadata.toc);
85-
// excepted colors that are taken globally (if defined)
86-
cfg.colors = $.extend(true, {}, initial_cfg.colors);
87-
try
88-
{cfg.colors = IPython.notebook.metadata.toc.colors = $.extend(true, cfg.colors, config.data.toc2.colors); }
89-
catch(e) {}
90-
// and moveMenuLeft, threshold, wideNotebook, collapse_to_match_collapsible_headings taken globally (if it exists, otherwise default)
91-
cfg.moveMenuLeft = IPython.notebook.metadata.toc.moveMenuLeft = initial_cfg.moveMenuLeft;
92-
cfg.threshold = IPython.notebook.metadata.toc.threshold = initial_cfg.threshold;
93-
cfg.widenNotebook = IPython.notebook.metadata.toc.widenNotebook = initial_cfg.widenNotebook;
94-
cfg.collapse_to_match_collapsible_headings = IPython.notebook.metadata.toc.collapse_to_match_collapsible_headings = initial_cfg.collapse_to_match_collapsible_headings
95-
if (config.data.toc2) {
96-
if (typeof config.data.toc2.moveMenuLeft !== "undefined") {
97-
cfg.moveMenuLeft = IPython.notebook.metadata.toc.moveMenuLeft = config.data.toc2.moveMenuLeft;
98-
}
99-
if (typeof config.data.toc2.threshold !== "undefined") {
100-
cfg.threshold = IPython.notebook.metadata.toc.threshold = config.data.toc2.threshold;
101-
}
102-
if (typeof config.data.toc2.widenNotebook !== "undefined") {
103-
cfg.widenNotebook = IPython.notebook.metadata.toc.widenNotebook = config.data.toc2.widenNotebook;
104-
}
105-
if (typeof config.data.toc2.collapse_to_match_collapsible_headings !== "undefined") {
106-
cfg.collapse_to_match_collapsible_headings = IPython.notebook.metadata.toc.collapse_to_match_collapsible_headings = config.data.toc2.collapse_to_match_collapsible_headings;
107-
}
108-
}
109-
// create highlights style section in document
110-
create_additional_css();
111-
// call callbacks
112-
callback && callback();
113-
st.config_loaded = true;
114-
})
115-
config.load();
78+
Object.keys(metadata_settings).forEach(function (key) {
79+
cfg[key] = IPython.notebook.metadata.toc[key] = (md.hasOwnProperty(key) ? md : cfg)[key];
80+
});
81+
// create highlights style section in document
82+
create_additional_css();
83+
// call callbacks
84+
callback && callback();
85+
});
11686
return cfg;
117-
}
87+
};
11888

11989
// extra download as html with toc menu (needs IPython kernel)
12090
function addSaveAsWithToc() {

0 commit comments

Comments
 (0)