58
61
/* Mapping SVN status to icons, just the file's basename */
60
"unversioned": "unversioned.png",
61
64
"normal": "normal.png",
66
"missing": "missing.png",
67
"deleted": "deleted.png",
62
68
"modified": "modified.png",
65
default_svn_icon = "normal.png";
71
/* Mapping SVN status to "nice" strings */
73
"unversioned": "Temporary file",
74
"normal": "Permanent file",
75
"added": "Temporary file (scheduled to be added)",
76
"missing": "Permanent file (missing)",
77
"deleted": "Permanent file (scheduled for deletion)",
78
"replaced": "Permanent file (replaced)",
79
"modified": "Permanent file (modified)",
80
"merged": "Permanent file (merged)",
81
"conflicted": "Permanent file (conflicted)",
84
default_svn_icon = null;
85
default_svn_nice = "Unknown status";
67
87
svn_icons_path = "media/images/svn";
89
published_icon = "media/images/interface/published.png";
69
91
/* List of MIME types considered "executable" by the system.
70
92
* Executable files offer a "run" link, implying that the "serve"
71
93
* application can interpret them.
114
141
* Called "navigate", can also be used for a simple refresh.
115
142
* Always makes a GET request.
116
143
* No return value.
144
* \param editmode Optional boolean. If true, then the user navigated here
145
* with an "edit" URL so we should favour using the editor.
118
function navigate(path)
147
function navigate(path, editmode)
120
149
/* Call the server and request the listing. This mutates the server. */
121
150
response = ajax_call(service_app, path, null, "GET");
122
151
/* Now read the response and set up the page accordingly */
123
handle_response(path, response);
152
handle_response(path, response, editmode);
155
/** Determines the "handler type" from a MIME type.
156
* The handler type is a string, either "text", "image", "audio" or "binary".
158
function get_handler_type(content_type)
162
if (content_type in type_handlers)
163
return type_handlers[content_type];
165
{ /* Based on the first part of the MIME type */
166
var handler_type = content_type.split('/')[0];
167
if (handler_type != "text" && handler_type != "image" &&
168
handler_type != "audio")
169
handler_type = "binary";
126
174
/** Given an HTTP response object, cleans up and rebuilds the contents of the
175
227
/* Treat this as an ordinary file. Get the file type. */
176
228
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";
229
var handler_type = get_handler_type(content_type);
230
/* If we're in "edit mode", always treat this file as text */
231
would_be_handler_type = handler_type;
232
if (editmode) handler_type = "text";
187
233
/* handler_type should now be set to either
188
234
* "text", "image", "audio" or "binary". */
189
235
switch (handler_type)
192
handle_text(path, response.responseText);
240
handle_text(path_join(path, "untitled"), "",
241
would_be_handler_type);
245
handle_text(path, response.responseText,
246
would_be_handler_type);
195
250
/* TODO: Custom image handler */
213
268
function clearpage()
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. */
270
dom_removechildren(document.getElementById("path"));
271
dom_removechildren(document.getElementById("filesbody"));
274
/** Deletes all "dynamic" content on the page necessary to navigate from
275
* one directory listing to another (does not clear as much as clearpage
277
* This is the equivalent of calling clearpage() then
278
* setup_for_dir_listing(), assuming the page is already on a dir listing.
280
function clearpage_dir()
220
282
dom_removechildren(document.getElementById("path"));
221
283
dom_removechildren(document.getElementById("files"));
222
284
dom_removechildren(document.getElementById("sidepanel"));
287
/** Sets the mode to either "file browser" or "text editor" mode.
288
* This modifies the window icon, and selected tab.
289
* \param editmode If True, editor mode. Else, file browser mode.
291
function setmode(editmode)
293
/* Find the DOM elements for the file browser and editor tabs */
294
var tabs = document.getElementById("apptabs");
295
var tab_files = null;
299
for (var i=0; i<tabs.childNodes.length; i++)
301
/* Find the href of the link within */
302
if (!tabs.childNodes[i].getElementsByTagName) continue;
303
a = tabs.childNodes[i].getElementsByTagName("a");
304
if (a.length == 0) continue;
305
href = a[0].getAttribute("href");
306
if (href == null) continue;
307
if (endswith(href, this_app))
308
tab_files = tabs.childNodes[i];
309
else if (endswith(href, edit_app))
310
tab_edit = tabs.childNodes[i];
315
tab_files.removeAttribute("class");
316
tab_edit.setAttribute("class", "thisapp");
320
tab_edit.removeAttribute("class");
321
tab_files.setAttribute("class", "thisapp");
225
325
/*** HANDLERS for different types of responses (such as dir listing, file,
228
328
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: "
331
var files = document.getElementById("filesbody");
332
var txt_elem = dom_make_text_elem("div", "Error: "
240
333
+ 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);
334
txt_elem.setAttribute("class", "padding error");
335
files.appendChild(txt_elem);
250
338
/** Presents a path list (address bar inside the page) for clicking.
252
340
function presentpath(path)
254
342
var dom_path = document.getElementById("path");
256
343
var href_path = make_path(this_app);
347
/* Also set the document title */
348
document.title = path_basename(path) + " - IVLE";
258
349
/* Create all of the paths */
259
for each (dir in path.split("/"))
350
var pathlist = path.split("/");
351
for (var i=0; i<pathlist.length; i++)
261
354
if (dir == "") continue;
263
355
/* Make an 'a' element */
264
356
href_path = path_join(href_path, dir);
265
var link = dom_make_link_elem("a", dir, href_path,
266
"navigate(" + href_path + ")");
357
nav_path = path_join(nav_path, dir);
358
var link = dom_make_link_elem("a", dir, "Navigate to " + nav_path,
359
href_path/*, "navigate(" + repr(href_path) + ")"*/);
267
360
dom_path.appendChild(link);
268
361
dom_path.appendChild(document.createTextNode("/"));
363
dom_path.removeChild(dom_path.lastChild);
272
366
/** Given a mime type, returns the path to the icon.
300
394
filename = svn_icons[svnstatus];
302
396
filename = default_svn_icon;
397
if (filename == null) return null;
303
398
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
make_path(path_join(this_app, path, filename)),
358
"navigate(" + path_join(path, filename) + ")"));
362
/* Column 2: Filetype and subversion icons. */
363
td = document.createElement("td");
364
td.setAttribute("class", "thincol");
365
td.appendChild(dom_make_img(mime_type_to_icon(file.type),
368
td = document.createElement("td");
369
td.setAttribute("class", "thincol");
370
if (under_subversion)
371
td.appendChild(dom_make_img(svnstatus_to_icon(file.svnstatus),
372
22, 22, file.svnstatus));
374
/* Column 3: Filename */
375
row.appendChild(dom_make_text_elem("td", filename));
378
row.appendChild(dom_make_text_elem("td", nice_filesize(file.size)));
380
row.appendChild(dom_make_text_elem("td", file.mtime_nice));
381
files.appendChild(row);
386
/** Presents the text editor.
388
function handle_text(path, text)
401
/** Given an svnstatus, returns the "nice" string.
403
function svnstatus_to_string(svnstatus)
405
if (svnstatus in svn_nice)
406
return svn_nice[svnstatus];
408
return default_svn_nice;
394
411
/** Displays a download link to the binary file.
396
413
function handle_binary(path)
416
var files = document.getElementById("filesbody");
417
var div = document.createElement("div");
418
files.appendChild(div);
419
div.setAttribute("class", "padding");
420
var download_link = app_path(download_app, path);
421
var par1 = dom_make_text_elem("p",
422
"The file " + path + " is a binary file. To download this file, " +
423
"click the following link:");
424
var par2 = dom_make_link_elem("p",
425
"Download " + path, "Download " + path, download_link);
426
div.appendChild(par1);
427
div.appendChild(par2);
402
430
/** Called when the page loads initially.
410
438
var path = parse_url(window.location.href).path;
411
439
/* Strip out root_dir + "/files" from the front of the path */
412
strip_chars = make_path(this_app).length + 1;
413
path = path.substr(strip_chars);
440
var strip = make_path(this_app);
441
var editmode = false;
442
if (path.substr(0, strip.length) == strip)
443
path = path.substr(strip.length+1);
446
/* See if this is an edit path */
447
strip = make_path(edit_app);
448
if (path.substr(0, strip.length) == strip)
450
path = path.substr(strip.length+1);
455
if (path.length == 0)
457
/* Navigate to the user's home directory by default */
462
navigate(path, editmode);