1
/* IVLE - Informatics Virtual Learning Environment
2
* Copyright (C) 2007-2008 The University of Melbourne
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
* Module: File Browser (client)
23
/* Url names for apps */
25
service_app = "fileservice";
27
/* Mapping MIME types onto handlers.
28
* "text" : When navigating to a text file, the text editor is opened.
29
* "image" : When navigating to an image, the image is displayed (rather than
30
* going to the text editor).
31
* "audio" : When navigating to an audio file, a "play" button is presented.
32
* "binary" : When navigating to a binary file, offer it as a download through
35
* If a file is not on the list, its default action is determined by the first
36
* part of its content type, where "text/*", "image/*" and "audio/*" are
37
* treated as above, and other types are simply treated as binary.
40
"application/x-javascript" : "text",
41
"application/javascript" : "text",
42
"application/json" : "text",
43
"application/xml" : "text",
46
/* Mapping MIME types to icons, just the file's basename */
48
"text/directory": "dir.png",
49
"text/x-python": "py.png",
52
default_type_icon = "txt.png";
54
/* Relative to IVLE root */
55
type_icons_path = "media/images/mime";
56
type_icons_path_large = "media/images/mime/large";
58
/* Mapping SVN status to icons, just the file's basename */
60
"unversioned": "unversioned.png",
61
"normal": "normal.png",
62
"modified": "modified.png",
65
default_svn_icon = "normal.png";
67
svn_icons_path = "media/images/svn";
69
/* List of MIME types considered "executable" by the system.
70
* Executable files offer a "run" link, implying that the "serve"
71
* application can interpret them.
77
/** Calls the server using Ajax, performing an action on the server side.
78
* Receives the response from the server and performs a refresh of the page
79
* contents, updating it to display the returned data (such as a directory
80
* listing, file preview, or editor pane).
81
* Always makes a POST request.
84
* \param action String. Name of the action to perform, as defined in the
86
* \param path URL path to make the request to, within the application.
87
* \param args Argument object, as described in util.parse_url and friends.
88
* This should contain the arguments to the action, but NOT the action
89
* itself. (Also a minor side-effect; the "args" object will be mutated
90
* to include the action attribute).
91
* \param content_type String, optional.
92
* May be "application/x-www-form-urlencoded" or "multipart/form-data".
93
* Defaults to "application/x-www-form-urlencoded".
94
* "multipart/form-data" is recommended for large uploads.
96
function do_action(action, path, args, content_type)
99
/* Call the server and perform the action. This mutates the server. */
100
response = ajax_call(service_app, path, args, "POST", content_type);
101
/* Check for action errors reported by the server, and report them to the
103
error = response.getResponseHeader("X-IVLE-Action-Error");
105
alert("Error: " + error.toString() + ".");
106
/* Now read the response and set up the page accordingly */
107
handle_response(path, response);
110
/** Calls the server using Ajax, requesting a directory listing. This should
111
* not modify the server in any way. Receives the response from the server and
112
* performs a refresh of the page contents, updating it to display the
113
* returned data (such as a directory listing, file preview, or editor pane).
114
* Called "navigate", can also be used for a simple refresh.
115
* Always makes a GET request.
118
function navigate(path)
120
/* Call the server and request the listing. This mutates the server. */
121
response = ajax_call(service_app, path, null, "GET");
122
/* Now read the response and set up the page accordingly */
123
handle_response(path, response);
126
/** Given an HTTP response object, cleans up and rebuilds the contents of the
127
* page using the response data. This does not navigate away from the page, it
128
* merely rebuilds most of the data.
129
* Note that depending on the type of data returned, this could result in a
130
* directory listing, an image preview, an editor pane, etc.
131
* Figures out the type and calls the appropriate function.
132
* \param path URL path which the request was made for. This can (among other
133
* things) be used to update the URL in the location bar.
134
* \param response XMLHttpRequest object returned by the server. Should
135
* contain all the response data.
137
function handle_response(path, response)
139
/* TODO: Set location bar to "path" */
141
/* Clear away the existing page contents */
143
/* Display the path at the top, for navigation */
146
/* Check the status, and if not 200, read the error and handle this as an
148
if (response.status != 200)
150
var error = response.getResponseHeader("X-IVLE-Return-Error");
152
error = response.statusText;
157
/* Check if this is a directory listing or file contents */
158
if (response.getResponseHeader("X-IVLE-Return") == "Dir")
160
var listing = response.responseText;
161
/* The listing SHOULD be valid JSON text. Parse it into an object. */
164
listing = JSON.parse(listing);
168
handle_error("The server returned an invalid directory listing");
171
handle_dir_listing(path, listing);
175
/* Treat this as an ordinary file. Get the file type. */
176
var content_type = response.getResponseHeader("Content-Type");
178
if (content_type in type_handlers)
179
handler_type = type_handlers[content_type];
181
{ /* Based on the first part of the MIME type */
182
handler_type = content_type.split('/')[0];
183
if (handler_type != "text" && handler_type != "image" &&
184
handler_type != "audio")
185
handler_type = "binary";
187
/* handler_type should now be set to either
188
* "text", "image", "audio" or "binary". */
189
switch (handler_type)
192
handle_text(path, response.responseText);
195
/* TODO: Custom image handler */
196
handle_binary(path, response.responseText);
199
/* TODO: Custom audio handler */
200
handle_binary(path, response.responseText);
209
/** Deletes all "dynamic" content on the page.
210
* This returns the page back to the state it is in when the HTML arrives to
211
* the browser, ready for another handler to populate it.
215
/* Note: For now clear just enough to repopulate with a dir listing.
216
* Later, will have to clear more to make way for other handlers.
217
* Possibly have a "full clear" for all handlers, and special
218
* less-violent clearers for each handler if the same handler is going to
219
* be used that was used last time. */
220
dom_removechildren(document.getElementById("path"));
221
dom_removechildren(document.getElementById("files"));
222
dom_removechildren(document.getElementById("sidepanel"));
225
/*** HANDLERS for different types of responses (such as dir listing, file,
228
function handle_error(message)
230
/* TODO: Find a better place to put this message. */
231
var files = document.getElementById("files");
232
var tr = document.createElement("tr");
233
var td = document.createElement("td");
235
var td = document.createElement("td");
237
var td = document.createElement("td");
239
var txt_elem = dom_make_text_elem("td", "Error: "
240
+ message.toString() + ".")
241
txt_elem.setAttribute("class", "error");
242
tr.appendChild(txt_elem);
243
var td = document.createElement("td");
245
var td = document.createElement("td");
247
files.appendChild(tr);
250
/** Presents a path list (address bar inside the page) for clicking.
252
function presentpath(path)
254
var dom_path = document.getElementById("path");
255
var href_path = make_path(this_app);
258
/* Create all of the paths */
259
for each (dir in path.split("/"))
261
if (dir == "") continue;
262
/* Make an 'a' element */
263
href_path = path_join(href_path, dir);
264
nav_path = path_join(nav_path, dir);
265
var link = dom_make_link_elem("a", dir, "Navigate to " + nav_path,
266
href_path, "navigate(" + href_path + ")");
267
dom_path.appendChild(link);
268
dom_path.appendChild(document.createTextNode("/"));
272
/** Given a mime type, returns the path to the icon.
273
* \param type String, Mime type.
274
* \param sizelarge Boolean, optional.
275
* \return Path to the icon. Has applied make_path, so it is relative to site
278
function mime_type_to_icon(type, sizelarge)
281
if (type in type_icons)
282
filename = type_icons[type];
284
filename = default_type_icon;
286
return make_path(path_join(type_icons_path_large, filename));
288
return make_path(path_join(type_icons_path, filename));
291
/** Given an svnstatus, returns the path to the icon.
292
* \param type String, svn status.
293
* \return Path to the icon. Has applied make_path, so it is relative to site
296
function svnstatus_to_icon(svnstatus)
299
if (svnstatus in svn_icons)
300
filename = svn_icons[svnstatus];
302
filename = default_svn_icon;
303
return make_path(path_join(svn_icons_path, filename));
306
/** Presents the directory listing.
308
function handle_dir_listing(path, listing)
311
/* Nav through the top-level of the JSON to the actual listing object. */
312
var listing = listing.listing;
314
/* Get "." out, it's special */
315
var thisdir = listing["."];
317
/* Is this dir under svn? */
318
var under_subversion = "svnstatus" in thisdir;
320
var files = document.getElementById("files");
326
/* Create all of the files */
327
for (var filename in listing)
329
file = listing[filename];
330
/* Make a 'tr' element */
331
row = document.createElement("tr");
332
/* Column 1: Selection checkbox */
333
row.setAttribute("class", "row" + row_toggle.toString())
334
row_toggle = row_toggle == 1 ? 2 : 1;
335
td = document.createElement("td");
336
checkbox = document.createElement("input");
337
checkbox.setAttribute("type", "checkbox");
338
checkbox.setAttribute("title", "Select this file");
339
td.appendChild(checkbox);
343
/* Column 2: Filetype and subversion icons. */
344
td = document.createElement("td");
345
td.setAttribute("class", "thincol");
346
td.appendChild(dom_make_img(mime_type_to_icon("text/directory"),
349
td = document.createElement("td");
350
td.setAttribute("class", "thincol");
351
if (under_subversion)
352
td.appendChild(dom_make_img(svnstatus_to_icon(file.svnstatus),
353
22, 22, file.svnstatus));
355
/* Column 3: Filename */
356
row.appendChild(dom_make_link_elem("td", filename,
357
"Navigate to " + path_join(path, filename),
358
make_path(path_join(this_app, path, filename)),
359
"navigate(" + path_join(path, filename) + ")"));
363
/* Column 2: Filetype and subversion icons. */
364
td = document.createElement("td");
365
td.setAttribute("class", "thincol");
366
td.appendChild(dom_make_img(mime_type_to_icon(file.type),
369
td = document.createElement("td");
370
td.setAttribute("class", "thincol");
371
if (under_subversion)
372
td.appendChild(dom_make_img(svnstatus_to_icon(file.svnstatus),
373
22, 22, file.svnstatus));
375
/* Column 3: Filename */
376
row.appendChild(dom_make_text_elem("td", filename));
379
row.appendChild(dom_make_text_elem("td", nice_filesize(file.size)));
381
row.appendChild(dom_make_text_elem("td", file.mtime_short,
383
files.appendChild(row);
388
/** Presents the text editor.
390
function handle_text(path, text)
396
/** Displays a download link to the binary file.
398
function handle_binary(path)
404
/** Called when the page loads initially.
406
window.onload = function()
408
/* Navigate (internally) to the path in the URL bar.
409
* This causes the page to be populated with whatever is at that address,
410
* whether it be a directory or a file.
412
var path = parse_url(window.location.href).path;
413
/* Strip out root_dir + "/files" from the front of the path */
414
strip_chars = make_path(this_app).length + 1;
415
path = path.substr(strip_chars);
417
if (path.length == 0)
419
/* Navigate to the user's home directory by default */