Skip to content

Commit abae4b9

Browse files
committed
conserve modified code
1 parent faea58b commit abae4b9

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

algorithm/category.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"name": "Uncategorized",
2020
"list": {
2121
"dp": "Dynamic Programming",
22-
"scratch_paper": "Scratch Paper"
22+
"scratch_paper": "<i class='fa fa-code'></i> Scratch Paper"
2323
}
2424
}
2525
}

js/script.js

+28-13
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ $.ajaxSetup({cache: false, dataType: "text"});
33
$(document).on('click', 'a', function (e) {
44
e.preventDefault();
55

6-
var win = window.open($(this).attr('href'), '_blank');
7-
if (!win) {
6+
if (!window.open($(this).attr('href'), '_blank')) {
87
alert('Please allow popups for this site');
98
}
109
});
@@ -20,32 +19,47 @@ var initEditor = function (id) {
2019
};
2120
var dataEditor = initEditor('data');
2221
var codeEditor = initEditor('code');
22+
var lastDir = null;
2323
dataEditor.on('change', function () {
24+
var data = dataEditor.getValue();
25+
if (lastDir) cachedFile[lastDir].data = data;
2426
try {
25-
eval(dataEditor.getValue());
27+
eval(data);
2628
lastModule = tracer && tracer.module;
2729
_tracer = tracer;
2830
} catch (err) {
2931
}
3032
_tracer.reset();
3133
});
34+
codeEditor.on('change', function () {
35+
var code = codeEditor.getValue();
36+
if (lastDir) cachedFile[lastDir].code = code;
37+
});
3238

39+
var cachedFile = {};
3340
var loadFile = function (category, algorithm, file, explanation) {
3441
lastData = null;
3542
$('#explanation').html(explanation);
36-
dataEditor.setValue('');
37-
codeEditor.setValue('');
3843

39-
var dir = './algorithm/' + category + '/' + algorithm + '/' + file + '/';
40-
$.get(dir + 'data.js', function (data) {
41-
dataEditor.setValue(data, -1);
44+
var dir = lastDir = './algorithm/' + category + '/' + algorithm + '/' + file + '/';
45+
if (cachedFile[dir] && cachedFile[dir].data !== undefined && cachedFile[dir].code !== undefined) {
46+
dataEditor.setValue(cachedFile[dir].data, -1);
47+
codeEditor.setValue(cachedFile[dir].code, -1);
48+
} else {
49+
dataEditor.setValue('');
50+
codeEditor.setValue('');
51+
$.get(dir + 'data.js', function (data) {
52+
cachedFile[dir] = {data: data};
53+
dataEditor.setValue(data, -1);
4254

43-
$.get(dir + 'code.js', function (data) {
44-
codeEditor.setValue(data, -1);
55+
$.get(dir + 'code.js', function (code) {
56+
cachedFile[dir].code = code;
57+
codeEditor.setValue(code, -1);
58+
});
59+
}).fail(function (jqXHR, textStatus, errorThrown) {
60+
alert("AJAX call failed: " + textStatus + ", " + errorThrown);
4561
});
46-
}).fail(function (jqXHR, textStatus, errorThrown) {
47-
alert("AJAX call failed: " + textStatus + ", " + errorThrown);
48-
});
62+
}
4963
};
5064
var loadAlgorithm = function (category, algorithm) {
5165
$('#list > button').removeClass('active');
@@ -57,6 +71,7 @@ var loadAlgorithm = function (category, algorithm) {
5771
$('#tab_desc > .wrapper').empty();
5872
$('.files_bar').empty();
5973
$('#explanation').html('');
74+
lastDir = null;
6075
dataEditor.setValue('');
6176
codeEditor.setValue('');
6277

0 commit comments

Comments
 (0)