162
165
callback = function(response)
164
167
/* Read the response and set up the page accordingly */
165
handle_response(path, response, editmode);
168
handle_response(path, response, url.args);
167
170
/* Get any query strings */
168
171
url = parse_url(window.location.href);
170
/* Call the server and request the listing. This mutates the server. */
173
/* Call the server and request the listing. */
171
174
ajax_call(callback, service_app, path, url.args, "GET");
208
211
* things) be used to update the URL in the location bar.
209
212
* \param response XMLHttpRequest object returned by the server. Should
210
213
* contain all the response data.
211
* \param editmode Optional boolean. If true, then the user navigated here
212
* with an "edit" URL so we should favour using the editor.
214
* \param url_args Arguments dict, for the arguments passed to the URL
215
* in the browser's address bar (will be forwarded along).
214
function handle_response(path, response, editmode)
217
function handle_response(path, response, url_args)
216
219
/* TODO: Set location bar to "path" */
217
220
current_path = path;
236
/* This will always return a listing, whether it is a dir or a file.
238
var listing = response.responseText;
239
/* The listing SHOULD be valid JSON text. Parse it into an object. */
242
listing = JSON.parse(listing);
243
file_listing = listing.listing; /* Global */
247
handle_error("The server returned an invalid directory listing");
250
/* Get "." out, it's special */
251
current_file = file_listing["."]; /* Global */
252
delete file_listing["."];
233
254
/* Check if this is a directory listing or file contents */
234
255
var isdir = response.getResponseHeader("X-IVLE-Return") == "Dir";
235
if (!editmode && isdir)
237
var listing = response.responseText;
238
/* The listing SHOULD be valid JSON text. Parse it into an object. */
241
listing = JSON.parse(listing);
245
handle_error("The server returned an invalid directory listing");
248
258
handle_dir_listing(path, listing);
252
/* Treat this as an ordinary file. Get the file type. */
253
var content_type = response.getResponseHeader("Content-Type");
254
var handler_type = get_handler_type(content_type);
255
/* If we're in "edit mode", always treat this file as text */
256
would_be_handler_type = handler_type;
257
if (editmode) handler_type = "text";
258
/* handler_type should now be set to either
259
* "text", "image", "audio" or "binary". */
260
switch (handler_type)
265
handle_text(path_join(path, "untitled"), "",
266
would_be_handler_type);
270
handle_text(path, response.responseText,
271
would_be_handler_type);
275
/* TODO: Custom image handler */
276
handle_binary(path, response.responseText);
279
/* TODO: Custom audio handler */
280
handle_binary(path, response.responseText);
262
/* Need to make a 2nd ajax call, this time get the actual file
264
callback = function(response)
266
/* Read the response and set up the page accordingly */
267
handle_contents_response(path, response);
269
/* Call the server and request the listing. */
271
args = shallow_clone_object(url_args);
274
/* This time, get the contents of the file, not its metadata */
275
args['return'] = "contents";
276
ajax_call(callback, service_app, path, args, "GET");
287
278
update_actions(isdir);
281
function handle_contents_response(path, response)
283
/* Treat this as an ordinary file. Get the file type. */
284
var content_type = response.getResponseHeader("Content-Type");
285
var handler_type = get_handler_type(content_type);
286
/* If we're in "edit mode", always treat this file as text */
287
would_be_handler_type = handler_type;
288
/* handler_type should now be set to either
289
* "text", "image", "audio" or "binary". */
290
switch (handler_type)
293
handle_text(path, response.responseText,
294
would_be_handler_type);
297
/* TODO: Custom image handler */
298
handle_binary(path, response.responseText);
301
/* TODO: Custom audio handler */
302
handle_binary(path, response.responseText);
290
310
/** Deletes all "dynamic" content on the page.
291
311
* This returns the page back to the state it is in when the HTML arrives to
292
312
* the browser, ready for another handler to populate it.