247
var top_level_dir = path==username;
250
var req = ajax_call(null, "userservice", "get_enrolments", null, "GET")
251
subjects = decode_response(req);
255
/* This will always return a listing, whether it is a dir or a file.
257
var listing = response.responseText;
258
/* The listing SHOULD be valid JSON text. Parse it into an object. */
261
listing = JSON.parse(listing);
262
file_listing = listing.listing; /* Global */
268
var err = document.createElement("div");
269
var p = dom_make_text_elem("p", "Error: "
270
+ "There was an unexpected server error processing "
271
+ "the selected command.");
273
p = dom_make_text_elem("p", "If the problem persists, please "
274
+ "contact the system administrator.")
276
p = document.createElement("p");
277
var refresh = document.createElement("input");
278
refresh.setAttribute("type", "button");
279
refresh.setAttribute("value", "Back to file view");
280
refresh.setAttribute("onclick", "refresh()");
281
p.appendChild(refresh);
287
var err = document.createElement("div");
288
var p = dom_make_text_elem("p", "Error: "
289
+ "There was an unexpected server error retrieving "
290
+ "the requested file or directory.");
292
p = dom_make_text_elem("p", "If the problem persists, please "
293
+ "contact the system administrator.")
299
/* Get "." out, it's special */
300
current_file = file_listing["."]; /* Global */
301
delete file_listing["."];
303
if ('revision' in listing)
305
current_revision = listing.revision;
308
157
/* Check if this is a directory listing or file contents */
309
var isdir = response.getResponseHeader("X-IVLE-Return") == "Dir";
158
if (response.getResponseHeader("X-IVLE-Return") == "Dir")
313
home_listing(listing, subjects, path);
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);
317
/* Need to make a 2nd ajax call, this time get the actual file
319
callback = function(response)
321
/* Read the response and set up the page accordingly */
322
handle_contents_response(path, response);
324
/* Call the server and request the listing. */
326
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];
329
/* This time, get the contents of the file, not its metadata */
330
args['return'] = "contents";
331
ajax_call(callback, service_app, path, args, "GET");
333
update_actions(isdir);
336
function handle_contents_response(path, response)
338
/* Treat this as an ordinary file. Get the file type. */
339
var content_type = response.getResponseHeader("Content-Type");
340
var handler_type = get_handler_type(content_type);
341
would_be_handler_type = handler_type;
342
/* handler_type should now be set to either
343
* "text", "image", "audio" or "binary". */
344
switch (handler_type)
347
handle_text(path, response.responseText,
348
would_be_handler_type);
351
/* TODO: Custom image handler */
352
handle_binary(path, response.responseText);
355
/* TODO: Custom audio handler */
356
handle_binary(path, response.responseText);
364
/* Called when a form upload comes back (from an iframe).
365
* Refreshes the page.
367
function upload_callback()
369
/* This has a pretty nasty hack, which happens to work.
370
* upload_callback is set as the "onload" callback for the iframe which
371
* receives the response from the server for uploading a file.
372
* This means it gets called twice. Once when initialising the iframe, and
373
* a second time when the actual response comes back.
374
* All we want to do is call navigate to refresh the page. But we CAN'T do
375
* that on the first load or it will just go into an infinite cycle of
376
* refreshing. We need to refresh the page ONLY on the second refresh.
377
* upload_callback_count is reset to 0 just before the iframe is created.
379
upload_callback_count++;
380
if (upload_callback_count >= 2)
382
myFrame = frames['upload_iframe'].document;
383
/* Browsers will turn the raw returned JSON into an HTML document. We
384
* need to get the <pre> from inside the <body>, and look at its text.
386
data = myFrame.firstChild.getElementsByTagName(
387
'body')[0].firstChild.firstChild.nodeValue;
388
data = JSON.parse(data);
390
alert("Error: " + decodeURIComponent(data['Error']));
391
document.getElementsByName('data')[0].value = '';
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);
499
300
filename = svn_icons[svnstatus];
501
302
filename = default_svn_icon;
502
if (filename == null) return null;
503
303
return make_path(path_join(svn_icons_path, filename));
506
/** Given an svnstatus, returns the "nice" string.
508
function svnstatus_to_string(svnstatus)
510
if (svnstatus in svn_nice)
511
return svn_nice[svnstatus];
513
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)
516
396
/** Displays a download link to the binary file.
518
398
function handle_binary(path)
520
var files = document.getElementById("filesbody");
521
var div = document.createElement("div");
522
files.appendChild(div);
523
div.setAttribute("class", "padding");
524
var download_link = app_path(download_app, path);
525
var par1 = dom_make_text_elem("p",
526
"The file " + path + " is a binary file. To download this file, " +
527
"click the following link:");
528
var par2 = dom_make_link_elem("p",
529
"Download " + path, "Download " + path, download_link);
530
div.appendChild(par1);
531
div.appendChild(par2);
534
/* Enable or disable actions1 moreactions actions. Takes either a single
535
* name, or an array of them.*/
536
function set_action_state(names, which, allow_on_revision)
538
if (!(names instanceof Array)) names = Array(names);
540
for (var i=0; i < names.length; i++)
542
element = document.getElementById('act_' + names[i]);
544
!(current_file.svnstatus == 'revision' && !allow_on_revision))
547
element.setAttribute("class", "choice");
548
element.removeAttribute("disabled");
553
element.setAttribute("class", "disabled");
554
element.setAttribute("disabled", "disabled");
559
/* Updates the list of available actions based on files selected */
560
function update_actions()
563
var numsel = selected_files.length;
564
var svn_selection = false;
568
svn_selection = true;
569
for (var i = 0; i < selected_files.length; i++){
570
if (file_listing[selected_files[i]]["svnstatus"] == "unversioned")
572
svn_selection = false;
581
/* Display information about the current directory instead */
582
filename = path_basename(current_path);
585
else if (numsel == 1)
587
filename = selected_files[0];
588
file = file_listing[filename];
591
/* Update each action node in the topbar.
592
* This includes enabling/disabling actions as appropriate, and
593
* setting href/onclick attributes. */
597
/* Available if exactly one file is selected */
598
var open = document.getElementById("act_open");
601
open.setAttribute("class", "choice");
603
open.setAttribute("title",
604
"Navigate to this directory in the file browser");
606
open.setAttribute("title",
607
"Edit or view this file");
608
open.setAttribute("href", build_revision_url(current_path, filename,
613
open.setAttribute("class", "disabled");
614
open.removeAttribute("title");
615
open.removeAttribute("href");
619
/* Available if zero or one files are selected,
620
* and only if this is a file, not a directory */
621
var serve = document.getElementById("act_serve");
622
if (numsel <= 1 && !file.isdir && current_file.svnstatus != 'revision')
624
serve.setAttribute("class", "choice");
625
serve.setAttribute("onclick",
626
"return maybe_save('The last saved version will be served.')");
628
serve.setAttribute("href",
629
app_path(serve_app, current_path));
631
serve.setAttribute("href",
632
app_path(serve_app, current_path, filename));
636
serve.setAttribute("class", "disabled");
637
serve.removeAttribute("href");
638
serve.removeAttribute("onclick");
642
/* Available if exactly one file is selected,
643
* and it is a Python file.
645
var run = document.getElementById("act_run");
647
if (numsel <= 1 && !file.isdir && file.type == "text/x-python"
648
&& current_file.svnstatus != 'revision')
652
// In the edit window
653
var localpath = path_join('/home', current_path);
657
// In the browser window
658
var localpath = path_join('/home', current_path, filename);
660
run.setAttribute("class", "choice");
661
run.setAttribute("onclick", "runfile('" + localpath + "')");
665
run.setAttribute("class", "disabled");
666
run.removeAttribute("onclick");
670
/* Always available for current files.
671
* If 0 files selected, download the current file or directory as a ZIP.
672
* If 1 directory selected, download it as a ZIP.
673
* If 1 non-directory selected, download it.
674
* If >1 files selected, download them all as a ZIP.
676
var download = document.getElementById("act_download");
677
if (current_file.svnstatus == 'revision')
679
download.setAttribute("class", "disabled");
680
download.removeAttribute("onclick");
682
else if (numsel <= 1)
684
download.setAttribute("class", "choice")
687
download.setAttribute("href",
688
app_path(download_app, current_path));
690
download.setAttribute("title",
691
"Download the current directory as a ZIP file");
693
download.setAttribute("title",
694
"Download the current file");
698
download.setAttribute("href",
699
app_path(download_app, current_path, filename));
701
download.setAttribute("title",
702
"Download the selected directory as a ZIP file");
704
download.setAttribute("title",
705
"Download the selected file");
710
/* Make a query string with all the files to download */
711
var dlpath = urlencode_path(app_path(download_app, current_path)) + "?";
712
for (var i=0; i<numsel; i++)
713
dlpath += "path=" + encodeURIComponent(selected_files[i]) + "&";
714
dlpath = dlpath.substr(0, dlpath.length-1);
715
download.setAttribute("class", "choice")
716
download.setAttribute("href", dlpath);
717
download.setAttribute("title",
718
"Download the selected files as a ZIP file");
721
/* Refresh - No changes required */
723
/* Publish and Submit */
724
/* If this directory is under subversion and selected/unselected file is a
726
var publish = document.getElementById("act_publish");
727
var submit = document.getElementById("act_submit");
728
var pubcond = numsel <= 1 && file.isdir;
731
/* If this dir is already published, call it "Unpublish" */
734
publish.setAttribute("value", "unpublish");
735
publish.setAttribute("title" ,"Make it so this directory "
736
+ "can not be seen by anyone on the web");
737
publish.firstChild.nodeValue = "Unpublish";
739
publish.setAttribute("value", "publish");
740
publish.setAttribute("title","Make it so this directory "
741
+ "can be seen by anyone on the web");
742
publish.firstChild.nodeValue = "Publish";
745
set_action_state(["publish", "submit"], pubcond);
748
/* If exactly 1 non-directory file is selected, and its parent
749
* directory is published.
751
set_action_state("share", numsel == 1 && !file.isdir &&
752
current_file.published);
755
/* If exactly 1 file is selected */
756
set_action_state("rename", numsel == 1);
758
/* Delete, cut, copy */
759
/* If >= 1 file is selected */
760
set_action_state(["delete", "cut", "copy"], numsel >= 1);
762
/* Paste, new file, new directory, upload */
763
/* Disable if the current file is not a directory */
764
set_action_state(["paste", "newfile", "mkdir", "upload"], current_file.isdir);
766
/* Subversion actions */
767
/* These are only useful if we are in a versioned directory and have some
769
set_action_state(["svnadd"], numsel >= 1 && current_file.svnstatus);
770
/* And these are only usefull is ALL the selected files are versioned */
771
set_action_state(["svnremove", "svnrevert", "svncommit", "svncopy",
772
"svncut"], numsel >= 1 && current_file.svnstatus && svn_selection);
774
/* Diff, log and update only support one path at the moment, so we must
775
* have 0 or 1 versioned files selected. If 0, the directory must be
777
single_versioned_path = (
779
(numsel == 1 && (svnst = file_listing[selected_files[0]].svnstatus)) ||
780
(numsel == 0 && (svnst = current_file.svnstatus))
781
) && svnst != "unversioned");
782
set_action_state(["svndiff", "svnupdate"], single_versioned_path);
784
/* We can resolve if we have a file selected and it is conflicted. */
785
set_action_state("svnresolved", single_versioned_path && numsel == 1 && svnst == "conflicted");
787
/* Log should be available for revisions as well. */
788
set_action_state("svnlog", single_versioned_path, true);
790
/* Cleanup should be available for revisions as well. */
791
set_action_state("svncleanup", single_versioned_path, true);
793
single_ivle_versioned_path = (
795
(numsel == 1 && (stat = file_listing[selected_files[0]])) ||
796
(numsel == 0 && (stat = current_file))
797
) && stat.svnstatus != "unversioned"
799
&& stat.svnurl.substr(0, svn_base.length) == svn_base);
800
set_action_state(["submit"], single_ivle_versioned_path);
802
/* There is currently nothing on the More Actions menu of use
803
* when the current file is not a directory. Hence, just remove
805
* (This makes some of the above decisions somewhat redundant).
806
* We also take this opportunity to show the appropriate actions2
807
* bar for this path. It should either be a save or upload widget.
809
if (current_file.isdir)
811
var actions2_directory = document.getElementById("actions2_directory");
812
actions2_directory.setAttribute("style", "display: inline;");
813
var moreactions = document.getElementById("moreactions_area");
814
moreactions.setAttribute("style", "display: inline;");
818
var actions2_file = document.getElementById("actions2_file");
819
actions2_file.setAttribute("style", "display: inline;");
825
/** Event handler for when an item of the "More actions..." dropdown box is
826
* selected. Performs the selected action. */
827
function handle_moreactions()
829
var moreactions = document.getElementById("moreactions");
830
if (moreactions.value == "top")
832
var selectedaction = moreactions.value;
833
/* Reset to "More actions..." */
834
moreactions.selectedIndex = 0;
836
/* If 0 files selected, filename is the name of the current dir.
837
* If 1 file selected, filename is that file.
839
if (selected_files.length == 0)
840
filename = path_basename(current_path);
841
else if (selected_files.length == 1)
842
filename = selected_files[0];
846
/* Now handle the selected action */
847
switch(selectedaction)
850
action_publish(selected_files);
853
action_unpublish(selected_files);
856
window.open(public_app_path("~" + current_path, filename), 'share')
859
if (selected_files.length == 1)
860
stat = file_listing[selected_files[0]];
863
path = stat.svnurl.substr(svn_base.length);
865
/* The working copy might not have an up-to-date version of the
866
* directory. While submitting like this could yield unexpected
867
* results, we should really submit the latest revision to minimise
868
* terrible mistakes - so we run off and ask fileservice for the
870
$.post(app_path(service_app, current_path),
871
{"action": "svnrepostat", "path": path},
874
window.location = path_join(app_path('+submit'), path) + '?revision=' + result.svnrevision;
880
action_rename(filename);
883
action_delete(selected_files);
886
action_copy(selected_files);
889
action_cut(selected_files);
901
show_uploadpanel(true);
904
action_add(selected_files);
907
action_remove(selected_files);
910
action_revert(selected_files);
913
window.location = path_join(app_path('diff'), current_path, selected_files[0] || '');
916
action_update(selected_files);
919
action_resolved(selected_files);
922
action_commit(selected_files);
925
window.location = path_join(app_path('svnlog'), current_path, selected_files[0] || '');
928
action_svncopy(selected_files);
931
action_svncut(selected_files);
934
action_svncleanup(".");
939
/** User clicks "Run" button.
940
* Do an Ajax call and print the test output.
942
function runfile(localpath)
944
if (!maybe_save('The last saved version will be run.')) return false;
946
/* Dump the entire file to the console */
947
var callback = function()
949
console_enter_line("execfile('" + localpath + "')", "block");
951
start_server(callback)
955
404
/** Called when the page loads initially.
957
function browser_init()
406
window.onload = function()
959
408
/* Navigate (internally) to the path in the URL bar.
960
409
* This causes the page to be populated with whatever is at that address,
961
410
* whether it be a directory or a file.
963
var path = get_path();
967
/** Gets the current path of the window */
968
function get_path() {
969
412
var path = parse_url(window.location.href).path;
970
413
/* Strip out root_dir + "/files" from the front of the path */
971
var strip = make_path(this_app);
972
if (path.substr(0, strip.length) == strip)
973
path = path.substr(strip.length+1);
976
/* See if this is an edit path */
977
strip = make_path(edit_app);
978
if (path.substr(0, strip.length) == strip)
980
path = path.substr(strip.length+1);
414
strip_chars = make_path(this_app).length + 1;
415
path = path.substr(strip_chars);
984
417
if (path.length == 0)