forked from algorithm-visualizer/algorithm-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_file.js
More file actions
65 lines (53 loc) · 1.41 KB
/
load_file.js
File metadata and controls
65 lines (53 loc) · 1.41 KB
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
'use strict';
const RSVP = require('rsvp');
const app = require('../app');
const {
getFileDir,
isScratchPaper
} = require('../utils');
const {
checkLoading,
setPath
} = require('./helpers');
const get = require('./ajax/get');
const loadDataAndCode = (dir) => {
return RSVP.hash({
data: get(`${dir}data.js`),
code: get(`${dir}code.js`)
});
};
const loadFileAndUpdateContent = (dir) => {
app.getEditor().clearContent();
return loadDataAndCode(dir).then((content) => {
app.updateCachedFile(dir, content);
app.getEditor().setContent(content);
});
};
const cachedContentExists = (cachedFile) => {
return cachedFile &&
cachedFile.data !== undefined &&
cachedFile.code !== undefined;
};
module.exports = (category, algorithm, file, explanation) => {
return new RSVP.Promise((resolve, reject) => {
if (checkLoading()) {
reject();
} else {
if (isScratchPaper(category)) {
setPath(category, app.getLoadedScratch());
} else {
setPath(category, algorithm, file);
}
$('#explanation').html(explanation);
let dir = getFileDir(category, algorithm, file);
app.setLastFileUsed(dir);
const cachedFile = app.getCachedFile(dir);
if (cachedContentExists(cachedFile)) {
app.getEditor().setContent(cachedFile);
resolve();
} else {
loadFileAndUpdateContent(dir).then(resolve, reject);
}
}
});
};