-
Notifications
You must be signed in to change notification settings - Fork 15
/
fileIO.js
125 lines (115 loc) · 3.24 KB
/
fileIO.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
function initFileIO() {
// Check for the various File API support.
if (!(window.File && window.FileReader && window.FileList && window.Blob)) {
alert("The File APIs are not fully supported in this browser.");
return -1;
}
if (DEBUG) {
console.log("Initialize File IO complete.");
}
document
.getElementById("fileInputs")
.addEventListener("change", handleFileSelect, false);
}
var JSONdata;
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
// files is a FileList of File objects. List some properties.
var output = [];
for (var i = 0, f; (f = files[i]); i++) {
output.push(
"<li><strong>",
f.name,
"</strong> (",
f.type || "n/a",
") - ",
f.size,
" bytes, last modified: ",
f.lastModifiedDate.toLocaleDateString(),
"</li>"
);
var reader = new FileReader();
reader.onload = function (e) {
var text = e.target.result;
try {
jsData = parseInputText(text);
} catch (e) {
ERROR = e; // For console access
alert("Couldn't Parse JSON from file : ", e.name, " : ", e.message);
console.log("ERROR parsing: ", e.name, " : ", e.message);
return -1;
}
loadSysFromJSON(jsData);
if (DEBUG) {
console.log("JSON FILE LOADED: ", jsData.name);
}
};
reader.onerror = errorHandler;
reader.readAsText(f);
}
document.getElementById("fileList").innerHTML =
"<ul>" + output.join("") + "</ul>";
}
function errorHandler(evt) {
switch (evt.target.error.code) {
case evt.target.error.NOT_FOUND_ERR:
alert("File Not Found!");
break;
case evt.target.error.NOT_READABLE_ERR:
alert("File is not readable");
break;
case evt.target.error.ABORT_ERR:
break; // noop
case evt.target.error.SECURITY_ERR:
alert(
"File Security Error, If running locally (ie. 'file://' then need --allow-file-access-from-files for browser)"
);
break;
default:
alert("An error occurred reading this file.");
}
if (DEBUG) {
console.log(
"FILE LOADED UNSUCCESSFULLY: Error Code ",
evt.target.error.code
);
}
}
function parseInputText(text) {
lines = text.split("\n");
for (var i = 0; i < lines.size; i++) {
console.log(i, ": ", lines[i]);
}
return JSON.parse(text);
}
function loadSysFromJSON(jsonData) {
// Constants
MINMASS =
typeof jsData.Constants.MINMASS != "undefined"
? jsData.Constants.MINMASS
: MINMASS;
MAXMASS =
typeof jsData.Constants.MAXMASS != "undefined"
? jsData.Constants.MAXMASS
: MAXMASS;
G = typeof jsData.Constants.G != "undefined" ? jsData.Constants.G : G;
GFACTOR =
typeof jsData.Constants.GFACTOR != "undefined"
? jsData.Constants.GFACTOR
: GFACTOR;
ETA = typeof jsData.Constants.ETA != "undefined" ? jsData.Constants.ETA : ETA;
if (DEBUG) {
console.log("MINMASS: ", MINMASS);
console.log("MAXMASS: ", MAXMASS);
console.log("G: ", G);
console.log("GFACTOR: ", GFACTOR);
console.log("ETA: ", ETA);
}
// Bodies
for (i = 0; i < jsData.Bodies.N; i++) {
var b = jsData.Bodies.BodyData[i];
//addBody(x,y,vx,vy,m)
addBody(b[1], b[2], b[3], b[4], b[0]);
}
refreshGraphics();
}