Skip to content

Commit 37e1099

Browse files
committed
[toc2] use amd structure to avoid polluting the global namespace
We use a synchronous method for detecting the live notebook, otherwise we run into various timing issues
1 parent c200197 commit 37e1099

File tree

3 files changed

+60
-28
lines changed

3 files changed

+60
-28
lines changed

src/jupyter_contrib_nbextensions/nbextensions/toc2/main.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,29 @@
22
// by minrk https://github.com/minrk/ipython_extensions
33
// See the history of contributions in README.md
44

5-
6-
//define(["require", "jquery", "base/js/namespace", 'services/config',
7-
// 'base/js/utils', "nbextensions/toc2/toc2"], function(require, $, IPython, configmod, utils, toc2) {
8-
9-
define(["require", "jquery", "base/js/namespace", 'services/config',
10-
'base/js/utils', 'notebook/js/codecell', "nbextensions/toc2/toc2"], function(require, $, IPython, configmod, utils, codecell, toc2 ) {
11-
12-
var Notebook = require('notebook/js/notebook').Notebook
13-
"use strict";
14-
5+
define([
6+
'require',
7+
'jquery',
8+
'base/js/namespace',
9+
'services/config',
10+
'base/js/utils',
11+
'notebook/js/codecell',
12+
'./toc2'
13+
], function(
14+
require,
15+
$,
16+
IPython,
17+
configmod,
18+
utils,
19+
codecell,
20+
toc2
21+
) {
22+
"use strict";
23+
24+
// imports
25+
var highlight_toc_item = toc2.highlight_toc_item;
26+
var table_of_contents = toc2.table_of_contents;
27+
var toggle_toc = toc2.toggle_toc;
1528

1629
// ...........Parameters configuration......................
1730
// define default values for config parameters if they were not present in general settings (notebook.json)

src/jupyter_contrib_nbextensions/nbextensions/toc2/toc2.js

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
1-
//---------------------------------------------------------------------
2-
3-
//......... utilitary functions............
4-
5-
var liveNotebook = !(typeof IPython == "undefined")
6-
var events;
7-
if (liveNotebook) {
8-
events = require('base/js/events');
9-
}
10-
else {
11-
// in non-live notebook, there's no event structure, so we make our own
12-
if (window.events === undefined) {
13-
var Events = function () {};
14-
window.events = $([new Events()]);
1+
(require.specified('base/js/namespace') ? define : function (deps, callback) {
2+
// if here, the Jupyter namespace hasn't been specified to be loaded.
3+
// This means that we're probably embedded in a page, so we need to make
4+
// our definition with a specific module name
5+
return define('nbextensions/toc2/toc2', deps, callback);
6+
})(['jquery', 'require'], function ($, require) {
7+
"use strict";
8+
9+
var IPython;
10+
var events;
11+
var liveNotebook = false;
12+
try {
13+
// this will work in a live notebook because nbextensions & custom.js
14+
// are loaded by/after notebook.js, which requires base/js/namespace
15+
IPython = require('base/js/namespace');
16+
events = require('base/js/events');
17+
liveNotebook = true;
18+
}
19+
catch (err) {
20+
// log the error, just in case we *are* in a live notebook
21+
console.log('[toc2] working in non-live notebook:', err);
22+
// in non-live notebook, there's no event structure, so we make our own
23+
if (window.events === undefined) {
24+
var Events = function () {};
25+
window.events = $([new Events()]);
26+
}
27+
events = window.events;
1528
}
16-
events = window.events;
17-
}
18-
1929

2030
function incr_lbl(ary, h_idx) { //increment heading label w/ h_idx (zero based)
2131
ary[h_idx]++;
@@ -695,3 +705,10 @@ var table_of_contents = function (cfg,st) {
695705
});
696706

697707
};
708+
709+
return {
710+
highlight_toc_item: highlight_toc_item,
711+
table_of_contents: table_of_contents,
712+
toggle_toc: toggle_toc,
713+
};
714+
});

src/jupyter_contrib_nbextensions/templates/toc2.tpl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ $( document ).ready(function(){
5050
st.toc_index=0;
5151
5252
// fire the main function with these parameters
53-
table_of_contents(cfg,st);
53+
require(['nbextensions/toc2/toc2'], function (toc2) {
54+
toc2.table_of_contents(cfg, st);
55+
});
5456
});
5557
</script>
5658

0 commit comments

Comments
 (0)