23
/* Mapping MIME types onto handlers.
24
* "text" : When navigating to a text file, the text editor is opened.
25
* "image" : When navigating to an image, the image is displayed (rather than
26
* going to the text editor).
27
* "audio" : When navigating to an audio file, a "play" button is presented.
28
* "binary" : When navigating to a binary file, offer it as a download through
31
* If a file is not on the list, its default action is determined by the first
32
* part of its content type, where "text/*", "image/*" and "audio/*" are
33
* treated as above, and other types are simply treated as binary.
36
"application/x-javascript" : "text",
37
"application/javascript" : "text",
38
"application/json" : "text",
39
"application/xml" : "text",
42
/* List of MIME types considered "executable" by the system.
43
* Executable files offer a "run" link, implying that the "serve"
44
* application can interpret them.
23
50
/** Calls the server using Ajax, performing an action on the server side.
24
51
* Receives the response from the server and performs a refresh of the page
25
52
* contents, updating it to display the returned data (such as a directory
83
110
function handle_response(path, response)
112
/* TODO: Set location bar to "path" */
113
/* Check the status, and if not 200, read the error and handle this as an
115
if (response.status != 200)
117
var error = response.getResponseHeader("X-IVLE-Return-Error");
119
error = response.statusText;
124
/* Check if this is a directory listing or file contents */
125
if (response.getResponseHeader("X-IVLE-Return") == "Dir")
127
var listing = response.responseText;
128
/* The listing SHOULD be valid JSON text. Parse it into an object. */
131
listing = JSON.parse(listing);
135
handle_error("The server returned an invalid directory listing");
138
handle_dir_listing(path, listing);
142
/* Treat this as an ordinary file. Get the file type. */
143
var content_type = response.getResponseHeader("Content-Type");
145
if (content_type in type_handlers)
146
handler_type = type_handlers[content_type];
148
{ /* Based on the first part of the MIME type */
149
handler_type = content_type.split('/')[0];
150
if (handler_type != "text" && handler_type != "image" &&
151
handler_type != "audio")
152
handler_type = "binary";
154
/* handler_type should now be set to either
155
* "text", "image", "audio" or "binary". */
156
switch (handler_type)
159
handle_text(path, response.responseText);
162
/* TODO: Custom image handler */
163
handle_binary(path, response.responseText);
166
/* TODO: Custom audio handler */
167
handle_binary(path, response.responseText);
176
/*** HANDLERS for different types of responses (such as dir listing, file,
179
function handle_error(message)
181
/* TODO: Rather than alert, rebuild the page into a page showing an error
183
alert("Error: " + message.toString() + ".");
186
/** Presents the directory listing.
188
function handle_dir_listing(path, listing)
194
/** Presents the text editor.
196
function handle_text(path, text)
202
/** Displays a download link to the binary file.
204
function handle_binary(path)
87
210
/** Called when the page loads initially.