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["."];
254
157
/* Check if this is a directory listing or file contents */
255
var isdir = response.getResponseHeader("X-IVLE-Return") == "Dir";
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");
258
171
handle_dir_listing(path, listing);
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);
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];
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");
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
would_be_handler_type = handler_type;
287
/* handler_type should now be set to either
288
* "text", "image", "audio" or "binary". */
289
switch (handler_type)
292
handle_text(path, response.responseText,
293
would_be_handler_type);
296
/* TODO: Custom image handler */
297
handle_binary(path, response.responseText);
300
/* TODO: Custom audio handler */
301
handle_binary(path, response.responseText);
309
/* Called when a form upload comes back (from an iframe).
310
* Refreshes the page.
312
function upload_callback()
314
/* This has a pretty nasty hack, which happens to work.
315
* upload_callback is set as the "onload" callback for the iframe which
316
* receives the response from the server for uploading a file.
317
* This means it gets called twice. Once when initialising the iframe, and
318
* a second time when the actual response comes back.
319
* All we want to do is call navigate to refresh the page. But we CAN'T do
320
* that on the first load or it will just go into an infinite cycle of
321
* refreshing. We need to refresh the page ONLY on the second refresh.
322
* upload_callback_count is reset to 0 just before the iframe is created.
324
upload_callback_count++;
325
if (upload_callback_count >= 2)
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);
329
209
/** Deletes all "dynamic" content on the page.
391
300
filename = svn_icons[svnstatus];
393
302
filename = default_svn_icon;
394
if (filename == null) return null;
395
303
return make_path(path_join(svn_icons_path, filename));
398
/** Given an svnstatus, returns the "nice" string.
400
function svnstatus_to_string(svnstatus)
402
if (svnstatus in svn_nice)
403
return svn_nice[svnstatus];
405
return default_svn_nice;
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)
408
396
/** Displays a download link to the binary file.
410
398
function handle_binary(path)
412
var files = document.getElementById("filesbody");
413
var div = document.createElement("div");
414
files.appendChild(div);
415
div.setAttribute("class", "padding");
416
var download_link = app_path(download_app, path);
417
var par1 = dom_make_text_elem("p",
418
"The file " + path + " is a binary file. To download this file, " +
419
"click the following link:");
420
var par2 = dom_make_link_elem("p",
421
"Download " + path, "Download " + path, download_link);
422
div.appendChild(par1);
423
div.appendChild(par2);
426
function update_actions()
429
var numsel = selected_files.length;
434
/* Display information about the current directory instead */
435
filename = path_basename(current_path);
438
else if (numsel == 1)
440
filename = selected_files[0];
441
file = file_listing[filename];
444
/* Update each action node in the topbar.
445
* This includes enabling/disabling actions as appropriate, and
446
* setting href/onclick attributes. */
450
/* Available if exactly one file is selected */
451
var open = document.getElementById("act_open");
454
open.setAttribute("class", "choice");
456
open.setAttribute("title",
457
"Navigate to this directory in the file browser");
459
open.setAttribute("title",
460
"Edit or view this file");
461
open.setAttribute("href", app_path(this_app, current_path, filename));
465
open.setAttribute("class", "disabled");
466
open.removeAttribute("title");
467
open.removeAttribute("href");
471
/* Available if exactly one file is selected,
472
* and only if this is a file, not a directory */
473
var serve = document.getElementById("act_serve");
474
if (numsel == 1 && !file.isdir)
476
serve.setAttribute("class", "choice");
477
serve.setAttribute("href",
478
app_path(serve_app, current_path, filename));
482
serve.setAttribute("class", "disabled");
483
serve.removeAttribute("href");
487
/* Available if exactly one file is selected,
488
* and it is a Python file.
494
* If 0 files selected, download the current file or directory as a ZIP.
495
* If 1 directory selected, download it as a ZIP.
496
* If 1 non-directory selected, download it.
497
* If >1 files selected, download them all as a ZIP.
499
var download = document.getElementById("act_download");
504
download.setAttribute("href",
505
app_path(download_app, current_path));
507
download.setAttribute("title",
508
"Download the current directory as a ZIP file");
510
download.setAttribute("title",
511
"Download the current file");
515
download.setAttribute("href",
516
app_path(download_app, current_path, filename));
518
download.setAttribute("title",
519
"Download the selected directory as a ZIP file");
521
download.setAttribute("title",
522
"Download the selected file");
527
/* Make a query string with all the files to download */
528
var dlpath = urlencode_path(app_path(download_app, current_path)) + "?";
529
for (var i=0; i<numsel; i++)
530
dlpath += "path=" + encodeURIComponent(selected_files[i]) + "&";
531
dlpath = dlpath.substr(0, dlpath.length-1);
532
download.setAttribute("href", dlpath);
533
download.setAttribute("title",
534
"Download the selected files as a ZIP file");
537
/* Refresh - No changes required */
539
/* Publish and Submit */
540
/* If this directory is under subversion and selected/unselected file is a
542
var publish = document.getElementById("act_publish");
543
var submit = document.getElementById("act_submit");
544
if (numsel <= 1 && file.isdir)
546
/* TODO: Work out of file is svn'd */
547
/* TODO: If this dir is already published, call it "Unpublish" */
548
publish.setAttribute("class", "choice");
549
publish.removeAttribute("disabled");
550
submit.setAttribute("class", "choice");
551
submit.removeAttribute("disabled");
555
publish.setAttribute("class", "disabled");
556
publish.setAttribute("disabled", "disabled");
557
submit.setAttribute("class", "disabled");
558
submit.setAttribute("disabled", "disabled");
562
/* If exactly 1 non-directory file is selected/opened, and its parent
563
* directory is published.
565
var share = document.getElementById("act_share");
566
if (numsel <= 1 && !file.isdir)
568
/* TODO: Work out if parent dir is published */
569
share.setAttribute("class", "choice");
570
share.removeAttribute("disabled");
574
share.setAttribute("class", "disabled");
575
share.setAttribute("disabled", "disabled");
579
/* If exactly 1 file is selected */
580
var rename = document.getElementById("act_rename");
583
rename.setAttribute("class", "choice");
584
rename.removeAttribute("disabled");
588
rename.setAttribute("class", "disabled");
589
rename.setAttribute("disabled", "disabled");
592
/* Delete, cut, copy */
593
/* If >= 1 file is selected */
594
var act_delete = document.getElementById("act_delete");
595
var cut = document.getElementById("act_cut");
596
var copy = document.getElementById("act_copy");
599
act_delete.setAttribute("class", "choice");
600
act_delete.removeAttribute("disabled");
601
cut.setAttribute("class", "choice");
602
cut.removeAttribute("disabled");
603
copy.setAttribute("class", "choice");
604
copy.removeAttribute("disabled");
608
act_delete.setAttribute("class", "disabled");
609
act_delete.setAttribute("disabled", "disabled");
610
cut.setAttribute("class", "disabled");
611
cut.setAttribute("disabled", "disabled");
612
copy.setAttribute("class", "disabled");
613
copy.setAttribute("disabled", "disabled");
616
/* Paste, new file, new directory, upload */
617
/* Always enabled (assuming this is a directory) */
619
/* Subversion actions */
620
/* TODO: Work out when these are appropriate */
621
var svnadd = document.getElementById("act_svnadd");
622
var svnrevert = document.getElementById("act_svnrevert");
623
var svncommit = document.getElementById("act_svncommit");
626
svnadd.setAttribute("class", "choice");
627
svnadd.removeAttribute("disabled");
628
svnrevert.setAttribute("class", "choice");
629
svnrevert.removeAttribute("disabled");
630
svncommit.setAttribute("class", "choice");
631
svncommit.removeAttribute("disabled");
633
var svncheckout = document.getElementById("act_svncheckout");
634
/* current_path == username: We are at the top level */
635
if (current_path == username)
637
svncheckout.setAttribute("class", "choice");
638
svncheckout.removeAttribute("disabled");
642
svncheckout.setAttribute("class", "disabled");
643
svncheckout.setAttribute("disabled", "disabled");
649
/** Event handler for when an item of the "More actions..." dropdown box is
650
* selected. Performs the selected action. */
651
function handle_moreactions()
653
var moreactions = document.getElementById("moreactions");
654
if (moreactions.value == "top")
656
var selectedaction = moreactions.value;
657
/* Reset to "More actions..." */
658
moreactions.selectedIndex = 0;
660
/* If 0 files selected, filename is the name of the current dir.
661
* If 1 file selected, filename is that file.
663
if (selected_files.length == 0)
664
filename = path_basename(current_path);
665
else if (selected_files.length == 1)
666
filename = selected_files[0];
670
/* Now handle the selected action */
671
switch(selectedaction)
674
action_publish(selected_files);
677
action_unpublish(selected_files);
681
alert("Not yet implemented: Sharing files");
685
alert("Not yet implemented: Submit");
688
action_rename(filename);
691
action_remove(selected_files);
694
action_copy(selected_files);
697
action_cut(selected_files);
709
show_uploadpanel(true);
712
action_add(selected_files);
715
action_revert(selected_files);
718
action_commit(selected_files);
726
404
/** Called when the page loads initially.