-
Notifications
You must be signed in to change notification settings - Fork 0
/
testpage.js
111 lines (90 loc) · 3.34 KB
/
testpage.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
var scriptTagContents = $('script').last().text();
$(function()
{
var filesToLoad = JSON.parse(scriptTagContents);
console.log("Loading script files: ", filesToLoad);
var section = console.group || console.log;
var endSection = console.groupEnd || function(){};
var scriptDisplayTemplate = $('#scriptDisplayTemplate');
function runScript(runButton, filename, body)
{
runButton.button('running');
section(filename);
eval('(function(){' + body + '\n})();');
endSection();
runButton.button('idle');
}
function addScriptToPage(filename, body)
{
var scriptDisplayElem = scriptDisplayTemplate.clone();
scriptDisplayElem.attr('id', filename.replace('.', '_'));
// Set the accordion title.
var titleLink = scriptDisplayElem.find('div.accordion-heading a.accordion-toggle');
titleLink.text(filename);
/*
// Set up collapse toggling.
var collapsedElem = scriptDisplayElem.find('div.accordion-body.collapse');
collapsedElem.collapse();
// On click, toggle the accordion:
titleLink.click(collapsedElem.collapse.bind(collapsedElem, 'toggle'));
*/
// Add the new script widget to the page.
scriptDisplayElem.appendTo('article');
// Populate the script display tag.
var scriptEdit = scriptDisplayElem.find('div.accordion-inner');
var codeMirror = CodeMirror(scriptEdit[0], {
mode: "application/json",
value: body
});
var runButton = scriptDisplayElem.find('div.accordion-heading button');
runButton.click(function onRunClicked(runButton, filename, codeMirror)
{
runScript(runButton, filename, codeMirror.getValue());
}.bind(this, runButton, filename, codeMirror));
}
function scriptLoaded(filename, body)
{
console.log('Script "' + filename + '" loaded; adding to page.');
addScriptToPage(filename, body);
// Load next script.
loadNext();
}
function onScriptLoadError(filename, jqXHR, textStatus, errorThrown)
{
console.log('onScriptLoadError', filename, jqXHR, textStatus, errorThrown);
var errorElem = $('<div class="alert"><strong></strong><span></span></div>');
errorElem.find('strong').text('Error while loading script "' + filename + '":');
errorElem.find('span').text(errorThrown);
errorElem.appendTo('article');
// Load next script.
loadNext();
}
function loadNext()
{
// Get and remove the first filename from the list.
var filename = filesToLoad.shift();
if(!filename)
{
console.log("Finished loading.");
setTimeout(console.clear, 100);
return;
}
var url = filename;
if(url.indexOf('://') == -1)
{
var loc = window.location + '';
url = loc.slice(0, loc.lastIndexOf('/') + 1) + url;
}
// Start loading this script.
console.log("Loading script: ", filename);
$.ajax({
url: url,
success: scriptLoaded.bind(this, filename),
dataType: 'text',
isLocal: true,
error: onScriptLoadError.bind(this, filename)
});
}
// Start loading the JS files.
loadNext();
});