Skip to content
Snippets Groups Projects
Commit 30e34a5b authored by Thomas Wood's avatar Thomas Wood
Browse files

Add ability to select/load source files

parent 41d17969
No related branches found
No related tags found
No related merge requests found
...@@ -94,7 +94,9 @@ ...@@ -94,7 +94,9 @@
<div class='source_div'> <div class='source_div'>
JavaScript code to run: <br />
<select id='select_source_code'><option disabled selected>Examples</option></select><br />
<input type='file' accept='.js' id='select_file' />
<table id='main_table'><tr> <table id='main_table'><tr>
<td> <td>
<textarea id='source_code' class='source' rows='6' cols='60'>source code here</textarea> <textarea id='source_code' class='source' rows='6' cols='60'>source code here</textarea>
...@@ -208,4 +210,4 @@ var t = []; for (var i = 0; i < 3; i++) { t[i] = function() { return i; } }; ...@@ -208,4 +210,4 @@ var t = []; for (var i = 0; i < 3; i++) { t[i] = function() { return i; } };
t[0](); t[0]();
3 3
--> -->
\ No newline at end of file
...@@ -69,27 +69,49 @@ var tracer_length = 0; ...@@ -69,27 +69,49 @@ var tracer_length = 0;
var tracer_pos = 0; var tracer_pos = 0;
// Core Mirror objects // Core Mirror objects
var source = ""; var source = null;
var interpreter = null; var interpreter = null;
// Initial source code // Initial source code
var source_file = 'var x = 2;\nx'; var source_files = [
'var x = 2;\nx',
var source_file = ' var t = {}; for (var i = 0; i < 3; i++) { t[i] = function() { return i; } }; t[0](); '; ' var t = {}; for (var i = 0; i < 3; i++) { t[i] = function() { return i; } }; t[0](); ',
var source_file = '{}'; '{}',
var source_file = '{} + {}'; '{} + {}',
var source_file = 'var x = { a : 1, b : 2 }; '; 'var x = { a : 1, b : 2 }; ',
var source_file = 'x = 1;\nx = 2;\nx = 3'; 'x = 1;\nx = 2;\nx = 3',
var source_file = 'var x = { a : 1 };\n x.b = 2;\nx'; 'var x = { a : 1 };\n x.b = 2;\nx',
var source_file = 'var x = { a : { c: 1 } };\n x.a.b = 2;\nx'; 'var x = { a : { c: 1 } };\n x.a.b = 2;\nx',
var source_file = '(function (x) {\nreturn 1;\n})()'; '(function (x) {return 1;})()',
];
source_files.reduce((select, file_content) => {
let option = document.createElement('option');
option.textContent = file_content;
select.append(option);
return select;
}, $('#select_source_code'));
function setSourceCode(text) {
$("#source_code").val(text);
if (source !== null) {
source.setValue(text);
}
}
$('#select_source_code').change(e => { setSourceCode(e.target.value)});
$('#select_file').change(e => {
let fr = new FileReader();
fr.onload = function (e) { setSourceCode(e.target.result) };
fr.readAsText(e.target.files[0]);
});
// --------------- Initialization ---------------- // --------------- Initialization ----------------
// WARNING: do not move this initialization further down in the file // WARNING: do not move this initialization further down in the file
// source code displayed initially // source code displayed initially
$("#source_code").val(source_file);
setSourceCode(source_files[source_files.length - 1]);
// --------------- Methods ---------------- // --------------- Methods ----------------
...@@ -1091,4 +1113,4 @@ function findToken(token) { ...@@ -1091,4 +1113,4 @@ function findToken(token) {
} }
} }
return -1; return -1;
}; };
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment