-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhandout-page.js
72 lines (63 loc) · 2.37 KB
/
handout-page.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* Handout Page Renderer
*
* Renders a Markdown handout with embedded exercises.
*
* Removes itself and rendering scripts from the DOM after execution.
*/
HANDOUT_SCRIPTDIR = document.querySelector('script[src*=handout-page]').getAttribute('src').match(/.*\//)[0];
(function() {
// load JavaScript by injecting a <script> tag
function require(url, callback) {
var deferred;
if ( ! callback) {
// if no callback function, return a Deferred that resolves when the script is loaded
deferred = $.Deferred();
callback = function(event) { deferred.resolve(event); }
}
// fix relative URLs
url = url.replace('./', HANDOUT_SCRIPTDIR);
var script = document.createElement('script');
script.type = 'text/javascript';
script.charset = 'utf-8';
script.src = url;
script.onerror = function(err) { throw err; }
script.onload = callback;
document.getElementsByTagName('body')[0].appendChild(script);
return deferred;
}
//
// main
//
// load jQuery, load other dependencies, and render
require('https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js', function () {
var stages = [
[ './course-setup.js#render',
'./jump-links.js#render',
'./handout-render.js',
'./render/Markdown.Converter.js',
'https://cdnjs.cloudflare.com/ajax/libs/pluralize/7.0.0/pluralize.min.js#render',
'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js' ],
[ './render/Markdown.Extra.js',
'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js#render' ],
];
(function next() {
var scripts = stages.shift();
// when all scripts are loaded, render and run
if ( ! scripts) {
renderPage();
var data = document.querySelector('script[src*=handout-page]').dataset;
$('script[src*=handout-page], script[src*=render]').remove();
require('./handout-run.js').done(function(event) {
Object.entries(data).forEach(function(kv) {
event.target.dataset[kv[0]] = kv[1];
});
event.target.setAttribute('data-handx-url', HANDOUT_HANDX);
});
return;
}
// otherwise, require all scripts in this stage and recurse
$.when.apply($, scripts.map(function(script) { return require(script) })).done(next);
})();
});
})();