-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_utils.js
45 lines (42 loc) · 1.21 KB
/
file_utils.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
/*
* File loading
*/
function load_file(file) {
if (!file) {
return null;
}
if (typeof file == "string") {
$("#status").text("Loading file '" + file + "'...");
$.get(file, {}, function(data, textStatus) {
if(textStatus != "success") {
file_loaded(file, null, false);
return;
}
file_loaded(file, data, true);
});
} else { // typeof file == File
$("#status").text("Loading local file '" + file.name + "'...");
var reader = new FileReader();
reader.onload = function (event) {
file_loaded(file.name, event.target.result, true);
}
reader.onerror = function (event) {
// why not display the error here directly?
file_loaded(file.name, null, false);
}
reader.readAsText(file);
}
}
function file_loaded(fname, content, success) {
if (success) {
$("#status").text("Reading " + fname + "...");
console.log("Reading " + fname + "...");
var edges;
$("#status").text(fname + " loaded");
console.log(fname + " loaded");
return createDataset(d3.csv.parseRows(content), fname);
} else {
// why do this here and not in the callback itself?
$("#status").text("Error parsing file '" + fname + "'.");
}
}