Skip to content

Commit

Permalink
[toc2] use amd structure to avoid polluting the global namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
jcb91 committed Jul 24, 2017
1 parent b0937b8 commit e374549
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
33 changes: 23 additions & 10 deletions src/jupyter_contrib_nbextensions/nbextensions/toc2/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,29 @@
// by minrk https://github.com/minrk/ipython_extensions
// See the history of contributions in README.md


//define(["require", "jquery", "base/js/namespace", 'services/config',
// 'base/js/utils', "nbextensions/toc2/toc2"], function(require, $, IPython, configmod, utils, toc2) {

define(["require", "jquery", "base/js/namespace", 'services/config',
'base/js/utils', 'notebook/js/codecell', "nbextensions/toc2/toc2"], function(require, $, IPython, configmod, utils, codecell, toc2 ) {

var Notebook = require('notebook/js/notebook').Notebook
"use strict";

define([
'require',
'jquery',
'base/js/namespace',
'services/config',
'base/js/utils',
'notebook/js/codecell',
'./toc2'
], function(
require,
$,
IPython,
configmod,
utils,
codecell,
toc2
) {
"use strict";

// imports
var highlight_toc_item = toc2.highlight_toc_item;
var table_of_contents = toc2.table_of_contents;
var toggle_toc = toc2.toggle_toc;

// ...........Parameters configuration......................
// define default values for config parameters if they were not present in general settings (notebook.json)
Expand Down
26 changes: 21 additions & 5 deletions src/jupyter_contrib_nbextensions/nbextensions/toc2/toc2.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
//---------------------------------------------------------------------

//......... utilitary functions............

var liveNotebook = !(typeof IPython == "undefined")
(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) {
"use strict";

var IPython;
var liveNotebook = false;
require(['base/js/namespace'], function (Jupyter_mod) {
liveNotebook = true;
IPython = Jupyter_mod;
});

function incr_lbl(ary, h_idx) { //increment heading label w/ h_idx (zero based)
ary[h_idx]++;
Expand Down Expand Up @@ -660,3 +669,10 @@ var table_of_contents = function (cfg,st) {
});

};

return {
highlight_toc_item: highlight_toc_item,
table_of_contents: table_of_contents,
toggle_toc: toggle_toc,
};
});
4 changes: 3 additions & 1 deletion src/jupyter_contrib_nbextensions/templates/toc2.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ $( document ).ready(function(){
st.toc_index=0;
// fire the main function with these parameters
table_of_contents(cfg,st);
require(['nbextensions/toc2/toc2'], function (toc2) {
toc2.table_of_contents(cfg, st);
});
});
</script>
Expand Down

0 comments on commit e374549

Please sign in to comment.