250
/* Remove trailing slash (or path==username won't compare properly) */
251
if (path[path.length-1] == "/")
252
path = path.substr(0, path.length-1);
253
var top_level_dir = path==username;
256
var req = ajax_call(null, "userservice", "get_enrolments", null, "GET")
257
subjects = decode_response(req);
261
/* This will always return a listing, whether it is a dir or a file.
263
var listing = response.responseText;
264
/* The listing SHOULD be valid JSON text. Parse it into an object. */
267
listing = JSON.parse(listing);
268
file_listing = listing.listing; /* Global */
274
var err = document.createElement("div");
275
var p = dom_make_text_elem("p", "Error: "
276
+ "There was an unexpected server error processing "
277
+ "the selected command.");
279
p = dom_make_text_elem("p", "If the problem persists, please "
280
+ "contact the system administrator.")
282
p = document.createElement("p");
283
var refresh = document.createElement("input");
284
refresh.setAttribute("type", "button");
285
refresh.setAttribute("value", "Back to file view");
286
refresh.setAttribute("onclick", "refresh()");
287
p.appendChild(refresh);
293
var err = document.createElement("div");
294
var p = dom_make_text_elem("p", "Error: "
295
+ "There was an unexpected server error retrieving "
296
+ "the requested file or directory.");
298
p = dom_make_text_elem("p", "If the problem persists, please "
299
+ "contact the system administrator.")
305
/* Get "." out, it's special */
306
current_file = file_listing["."]; /* Global */
307
delete file_listing["."];
309
if ('revision' in listing)
311
current_revision = listing.revision;
314
157
/* Check if this is a directory listing or file contents */
315
var isdir = response.getResponseHeader("X-IVLE-Return") == "Dir";
158
if (response.getResponseHeader("X-IVLE-Return") == "Dir")
321
/* Top-level dir, with subjects */
322
special_home_listing(listing, subjects, path);
326
/* Not the top-level dir. Do a normal dir listing. */
327
handle_dir_listing(path, listing.listing);
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);
332
/* Need to make a 2nd ajax call, this time get the actual file
334
callback = function(response)
336
/* Read the response and set up the page accordingly */
337
handle_contents_response(path, response);
339
/* Call the server and request the listing. */
341
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];
344
/* This time, get the contents of the file, not its metadata */
345
args['return'] = "contents";
346
ajax_call(callback, service_app, path, args, "GET");
348
update_actions(isdir);
351
function handle_contents_response(path, response)
353
/* Treat this as an ordinary file. Get the file type. */
354
var content_type = response.getResponseHeader("Content-Type");
355
var handler_type = get_handler_type(content_type);
356
would_be_handler_type = handler_type;
357
/* handler_type should now be set to either
358
* "text", "image", "audio" or "binary". */
359
switch (handler_type)
362
handle_text(path, response.responseText,
363
would_be_handler_type);
366
/* TODO: Custom image handler */
367
handle_binary(path, response.responseText);
370
/* TODO: Custom audio handler */
371
handle_binary(path, response.responseText);
379
/* Called when a form upload comes back (from an iframe).
380
* Refreshes the page.
382
function upload_callback()
384
/* This has a pretty nasty hack, which happens to work.
385
* upload_callback is set as the "onload" callback for the iframe which
386
* receives the response from the server for uploading a file.
387
* This means it gets called twice. Once when initialising the iframe, and
388
* a second time when the actual response comes back.
389
* All we want to do is call navigate to refresh the page. But we CAN'T do
390
* that on the first load or it will just go into an infinite cycle of
391
* refreshing. We need to refresh the page ONLY on the second refresh.
392
* upload_callback_count is reset to 0 just before the iframe is created.
394
upload_callback_count++;
395
if (upload_callback_count >= 2)
397
myFrame = frames['upload_iframe'].document;
398
/* Browsers will turn the raw returned JSON into an HTML document. We
399
* need to get the <pre> from inside the <body>, and look at its text.
401
data = myFrame.firstChild.getElementsByTagName(
402
'body')[0].firstChild.firstChild.nodeValue;
403
data = JSON.parse(data);
405
alert("Error: " + decodeURIComponent(data['Error']));
406
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);
514
307
filename = svn_icons[svnstatus];
516
309
filename = default_svn_icon;
517
if (filename == null) return null;
518
310
return make_path(path_join(svn_icons_path, filename));
521
/** Given an svnstatus, returns the "nice" string.
523
function svnstatus_to_string(svnstatus)
525
if (svnstatus in svn_nice)
526
return svn_nice[svnstatus];
528
return default_svn_nice;
531
/** Returns true if a file is versioned (not unversioned or ignored).
533
function svnstatus_versioned(svnstatus)
535
return svnstatus != "unversioned" && svnstatus != "ignored";
313
/** Initialises the DOM elements required to present a dir listing,
314
* assuming that clear_page has just been called or the page just
315
* loaded for the first time.
317
function setup_for_dir_listing()
319
var filesbody = document.getElementById("filesbody");
321
/* Using a table-based layout, for reasons of sanity */
322
/* One row, 2 columns */
323
var middle = document.createElement("table");
324
filesbody.appendChild(middle);
325
middle.setAttribute("id", "middle");
326
var middle_tbody = document.createElement("tbody");
327
middle.appendChild(middle_tbody);
328
var middle_tr = document.createElement("tr");
329
middle_tbody.appendChild(middle_tr);
331
/* Column 1: File table */
332
var filetable = document.createElement("td");
333
middle_tr.appendChild(filetable);
334
filetable.setAttribute("id", "filetable");
335
var filetablediv = document.createElement("div");
336
filetable.appendChild(filetablediv);
337
filetablediv.setAttribute("id", "filetablediv");
338
/* A nested table within this div - the actual files listing */
339
var filetabletable = document.createElement("table");
340
filetablediv.appendChild(filetabletable);
341
filetabletable.setAttribute("width", "100%");
342
var filetablethead = document.createElement("thead");
343
filetabletable.appendChild(filetablethead);
344
var filetablethead_tr = document.createElement("tr");
345
filetablethead.appendChild(filetablethead_tr);
346
filetablethead_tr.setAttribute("class", "rowhead");
348
var filetablethead_th = document.createElement("th");
349
filetablethead_tr.appendChild(filetablethead_th);
350
filetablethead_th.setAttribute("class", "col-check");
351
filetablethead_th = dom_make_link_elem("th", "Filename",
352
"Sort by filename", "")
353
filetablethead_tr.appendChild(filetablethead_th);
354
filetablethead_th.setAttribute("class", "col-filename");
355
filetablethead_th.setAttribute("colspan", 3);
356
filetablethead_th = dom_make_link_elem("th", "Size",
357
"Sort by file size", "")
358
filetablethead_tr.appendChild(filetablethead_th);
359
filetablethead_th.setAttribute("class", "col-size");
360
filetablethead_th = dom_make_link_elem("th", "Modified",
361
"Sort by date modified", "")
362
filetablethead_tr.appendChild(filetablethead_th);
363
filetablethead_th.setAttribute("class", "col-date");
365
var filetabletbody = document.createElement("tbody");
366
filetabletable.appendChild(filetabletbody);
367
filetabletbody.setAttribute("id", "files");
369
/* Column 2: Side-panel */
370
var sidepanel = document.createElement("td");
371
middle_tr.appendChild(sidepanel);
372
sidepanel.setAttribute("id", "sidepanel");
375
/* Now after the table "middle", there is a status bar */
376
var statusbar = dom_make_text_elem("div", "5 files, 14 kB");
377
filesbody.appendChild(statusbar);
378
statusbar.setAttribute("id", "statusbar");
381
/** Presents the directory listing.
383
function handle_dir_listing(path, listing)
385
setup_for_dir_listing();
387
/* Nav through the top-level of the JSON to the actual listing object. */
388
var listing = listing.listing;
390
/* Get "." out, it's special */
391
var thisdir = listing["."];
393
/* Is this dir under svn? */
394
var under_subversion = "svnstatus" in thisdir;
396
var files = document.getElementById("files");
402
/* Create all of the files */
403
for (var filename in listing)
405
file = listing[filename];
406
/* Make a 'tr' element */
407
row = document.createElement("tr");
408
/* Column 1: Selection checkbox */
409
row.setAttribute("class", "row" + row_toggle.toString())
410
row_toggle = row_toggle == 1 ? 2 : 1;
411
td = document.createElement("td");
412
checkbox = document.createElement("input");
413
checkbox.setAttribute("type", "checkbox");
414
checkbox.setAttribute("title", "Select this file");
415
td.appendChild(checkbox);
419
/* Column 2: Filetype and subversion icons. */
420
td = document.createElement("td");
421
td.setAttribute("class", "thincol");
422
td.appendChild(dom_make_img(mime_type_to_icon("text/directory"),
425
td = document.createElement("td");
426
td.setAttribute("class", "thincol");
427
if (under_subversion)
428
td.appendChild(dom_make_img(svnstatus_to_icon(file.svnstatus),
429
22, 22, file.svnstatus));
431
/* Column 3: Filename */
432
row.appendChild(dom_make_link_elem("td", filename,
433
"Navigate to " + path_join(path, filename),
434
make_path(path_join(this_app, path, filename)),
435
"navigate(" + path_join(path, filename) + ")"));
439
/* Column 2: Filetype and subversion icons. */
440
td = document.createElement("td");
441
td.setAttribute("class", "thincol");
442
td.appendChild(dom_make_img(mime_type_to_icon(file.type),
445
td = document.createElement("td");
446
td.setAttribute("class", "thincol");
447
if (under_subversion)
448
td.appendChild(dom_make_img(svnstatus_to_icon(file.svnstatus),
449
22, 22, file.svnstatus));
451
/* Column 3: Filename */
452
row.appendChild(dom_make_text_elem("td", filename));
455
row.appendChild(dom_make_text_elem("td", nice_filesize(file.size)));
457
row.appendChild(dom_make_text_elem("td", file.mtime_short,
459
files.appendChild(row);
464
/** Presents the text editor.
466
function handle_text(path, text)
538
472
/** Displays a download link to the binary file.
540
474
function handle_binary(path)
542
var files = document.getElementById("filesbody");
543
var div = document.createElement("div");
544
files.appendChild(div);
545
div.setAttribute("class", "padding");
546
var download_link = app_url(download_app, path);
547
var par1 = dom_make_text_elem("p",
548
"The file " + path + " is a binary file. To download this file, " +
549
"click the following link:");
550
var par2 = dom_make_link_elem("p",
551
"Download " + path, "Download " + path, download_link);
552
div.appendChild(par1);
553
div.appendChild(par2);
556
/* Enable or disable actions1 moreactions actions. Takes either a single
557
* name, or an array of them.*/
558
function set_action_state(names, which, allow_on_revision)
560
if (!(names instanceof Array)) names = Array(names);
562
for (var i=0; i < names.length; i++)
564
element = document.getElementById('act_' + names[i]);
566
!(current_file.svnstatus == 'revision' && !allow_on_revision))
569
element.setAttribute("class", "choice");
570
element.removeAttribute("disabled");
575
element.setAttribute("class", "disabled");
576
element.setAttribute("disabled", "disabled");
581
/* Updates the list of available actions based on files selected */
582
function update_actions()
585
var numsel = selected_files.length;
586
var svn_selection = false;
590
svn_selection = true;
591
for (var i = 0; i < selected_files.length; i++){
592
if (!svnstatus_versioned(file_listing[selected_files[i]].svnstatus))
594
svn_selection = false;
603
/* Display information about the current directory instead */
604
filename = path_basename(current_path);
607
else if (numsel == 1)
609
filename = selected_files[0];
610
file = file_listing[filename];
613
/* Update each action node in the topbar.
614
* This includes enabling/disabling actions as appropriate, and
615
* setting href/onclick attributes. */
619
/* Available if exactly one file is selected */
620
var open = document.getElementById("act_open");
623
open.setAttribute("class", "choice");
625
open.setAttribute("title",
626
"Navigate to this directory in the file browser");
628
open.setAttribute("title",
629
"Edit or view this file");
630
open.setAttribute("href", build_revision_url(current_path, filename,
635
open.setAttribute("class", "disabled");
636
open.removeAttribute("title");
637
open.removeAttribute("href");
641
/* Available if zero or one files are selected,
642
* and only if this is a file, not a directory */
643
var serve = document.getElementById("act_serve");
644
if (numsel <= 1 && !file.isdir && current_file.svnstatus != 'revision')
646
serve.setAttribute("class", "choice");
647
serve.setAttribute("onclick",
648
"return maybe_save('The last saved version will be served.')");
650
serve.setAttribute("href",
651
app_url(serve_app, current_path));
653
serve.setAttribute("href",
654
app_url(serve_app, current_path, filename));
658
serve.setAttribute("class", "disabled");
659
serve.removeAttribute("href");
660
serve.removeAttribute("onclick");
664
/* Available if exactly one file is selected,
665
* and it is a Python file.
667
var run = document.getElementById("act_run");
669
if (numsel <= 1 && !file.isdir && file.type == "text/x-python"
670
&& current_file.svnstatus != 'revision')
674
// In the edit window
675
var localpath = path_join('/home', current_path);
679
// In the browser window
680
var localpath = path_join('/home', current_path, filename);
682
run.setAttribute("class", "choice");
683
run.setAttribute("onclick", "runfile('" + localpath + "')");
687
run.setAttribute("class", "disabled");
688
run.removeAttribute("onclick");
692
/* Always available for current files.
693
* If 0 files selected, download the current file or directory as a ZIP.
694
* If 1 directory selected, download it as a ZIP.
695
* If 1 non-directory selected, download it.
696
* If >1 files selected, download them all as a ZIP.
698
var download = document.getElementById("act_download");
699
if (current_file.svnstatus == 'revision')
701
download.setAttribute("class", "disabled");
702
download.removeAttribute("onclick");
704
else if (numsel <= 1)
706
download.setAttribute("class", "choice")
709
download.setAttribute("href",
710
app_url(download_app, current_path));
712
download.setAttribute("title",
713
"Download the current directory as a ZIP file");
715
download.setAttribute("title",
716
"Download the current file");
720
download.setAttribute("href",
721
app_url(download_app, current_path, filename));
723
download.setAttribute("title",
724
"Download the selected directory as a ZIP file");
726
download.setAttribute("title",
727
"Download the selected file");
732
/* Make a query string with all the files to download */
733
var dlpath = app_url(download_app, current_path) + "?";
734
for (var i=0; i<numsel; i++)
735
dlpath += "path=" + encodeURIComponent(selected_files[i]) + "&";
736
dlpath = dlpath.substr(0, dlpath.length-1);
737
download.setAttribute("class", "choice")
738
download.setAttribute("href", dlpath);
739
download.setAttribute("title",
740
"Download the selected files as a ZIP file");
743
/* Refresh - No changes required */
745
/* Publish and Submit */
746
/* If this directory is under subversion and selected/unselected file is a
748
var publish = document.getElementById("act_publish");
749
var submit = document.getElementById("act_submit");
750
var pubcond = numsel <= 1 && file.isdir;
753
/* If this dir is already published, call it "Unpublish" */
756
publish.setAttribute("value", "unpublish");
757
publish.setAttribute("title" ,"Make it so this directory "
758
+ "can not be seen by anyone on the web");
759
publish.firstChild.nodeValue = "Unpublish";
761
publish.setAttribute("value", "publish");
762
publish.setAttribute("title","Make it so this directory "
763
+ "can be seen by anyone on the web");
764
publish.firstChild.nodeValue = "Publish";
767
set_action_state(["publish", "submit"], pubcond);
770
/* If exactly 1 non-directory file is selected, and its parent
771
* directory is published.
773
set_action_state("share", numsel == 1 && !file.isdir &&
774
current_file.published);
777
/* If exactly 1 file is selected */
778
set_action_state("rename", numsel == 1);
780
/* Delete, cut, copy */
781
/* If >= 1 file is selected */
782
set_action_state(["delete", "cut", "copy"], numsel >= 1);
784
/* Paste, new file, new directory, upload */
785
/* Disable if the current file is not a directory */
786
set_action_state(["paste", "newfile", "mkdir", "upload"], current_file.isdir);
788
/* Subversion actions */
789
/* These are only useful if we are in a versioned directory and have some
791
set_action_state(["svnadd"], numsel >= 1 && current_file.svnstatus);
792
/* And these are only useful is ALL the selected files are versioned */
793
set_action_state(["svnremove", "svnrevert", "svncopy", "svncut"],
794
numsel >= 1 && current_file.svnstatus && svn_selection);
795
/* Commit is useful if ALL selected files are versioned, or the current
796
* directory is versioned */
797
set_action_state(["svncommit"], current_file.svnstatus &&
798
(numsel >= 1 && svn_selection || numsel == 0));
800
/* Diff, log and update only support one path at the moment, so we must
801
* have 0 or 1 versioned files selected. If 0, the directory must be
803
single_versioned_path = (
805
(numsel == 1 && (svnst = file_listing[selected_files[0]].svnstatus)) ||
806
(numsel == 0 && (svnst = current_file.svnstatus))
807
) && svnstatus_versioned(svnst));
808
set_action_state(["svndiff", "svnupdate"], single_versioned_path);
810
/* We can resolve if we have a file selected and it is conflicted. */
811
set_action_state("svnresolved", single_versioned_path && numsel == 1 && svnst == "conflicted");
813
/* Log should be available for revisions as well. */
814
set_action_state("svnlog", single_versioned_path, true);
816
/* Cleanup should be available for revisions as well. */
817
set_action_state("svncleanup", single_versioned_path, true);
819
single_ivle_versioned_path = (
821
(numsel == 1 && (stat = file_listing[selected_files[0]])) ||
822
(numsel == 0 && (stat = current_file))
823
) && svnstatus_versioned(stat.svnstatus)
825
&& stat.svnurl.substr(0, svn_base.length) == svn_base);
826
set_action_state(["submit"], single_ivle_versioned_path);
828
/* There is currently nothing on the More Actions menu of use
829
* when the current file is not a directory. Hence, just remove
831
* (This makes some of the above decisions somewhat redundant).
832
* We also take this opportunity to show the appropriate actions2
833
* bar for this path. It should either be a save or upload widget.
835
if (current_file.isdir)
837
var actions2_directory = document.getElementById("actions2_directory");
838
actions2_directory.setAttribute("style", "display: inline;");
839
var moreactions = document.getElementById("moreactions_area");
840
moreactions.setAttribute("style", "display: inline;");
844
var actions2_file = document.getElementById("actions2_file");
845
actions2_file.setAttribute("style", "display: inline;");
851
/** Event handler for when an item of the "More actions..." dropdown box is
852
* selected. Performs the selected action. */
853
function handle_moreactions()
855
var moreactions = document.getElementById("moreactions");
856
if (moreactions.value == "top")
858
var selectedaction = moreactions.value;
859
/* Reset to "More actions..." */
860
moreactions.selectedIndex = 0;
862
/* If 0 files selected, filename is the name of the current dir.
863
* If 1 file selected, filename is that file.
865
if (selected_files.length == 0)
866
filename = path_basename(current_path);
867
else if (selected_files.length == 1)
868
filename = selected_files[0];
872
/* Now handle the selected action */
873
switch(selectedaction)
876
action_publish(selected_files);
879
action_unpublish(selected_files);
882
window.open(public_app_url("~" + current_path, filename), 'share')
885
if (selected_files.length == 1)
886
stat = file_listing[selected_files[0]];
889
url = stat.svnurl.substr(svn_base.length); // URL-encoded
890
path = decodeURIComponent(url);
892
/* The working copy might not have an up-to-date version of the
893
* directory. While submitting like this could yield unexpected
894
* results, we should really submit the latest revision to minimise
895
* terrible mistakes - so we run off and ask fileservice for the
897
$.post(app_path(service_app, current_path),
898
{"action": "svnrepostat", "path": path},
901
window.location = path_join(app_path('+submit'), url) + '?revision=' + result.svnrevision;
907
action_rename(filename);
910
action_delete(selected_files);
913
action_copy(selected_files);
916
action_cut(selected_files);
928
show_uploadpanel(true);
931
action_add(selected_files);
934
action_remove(selected_files);
937
action_revert(selected_files);
940
window.location = path_join(app_url('diff'), current_path, selected_files[0] || '');
943
action_update(selected_files);
946
action_resolved(selected_files);
949
action_commit(selected_files);
952
window.location = path_join(app_url('svnlog'), current_path, selected_files[0] || '');
955
action_svncopy(selected_files);
958
action_svncut(selected_files);
961
action_svncleanup(".");
966
/** User clicks "Run" button.
967
* Do an Ajax call and print the test output.
969
function runfile(localpath)
971
if (!maybe_save('The last saved version will be run.')) return false;
973
/* Dump the entire file to the console */
974
var callback = function()
976
console_enter_line("execfile('" + localpath + "')", "block");
978
start_server(callback)
982
480
/** Called when the page loads initially.
984
function browser_init()
482
window.onload = function()
986
484
/* Navigate (internally) to the path in the URL bar.
987
485
* This causes the page to be populated with whatever is at that address,
988
486
* whether it be a directory or a file.
990
var path = get_path();
994
/** Gets the current path of the window */
995
function get_path() {
996
488
var path = parse_url(window.location.href).path;
997
489
/* Strip out root_dir + "/files" from the front of the path */
998
var strip = make_path(this_app);
999
if (path.substr(0, strip.length) == strip)
1000
path = path.substr(strip.length+1);
1003
/* See if this is an edit path */
1004
strip = make_path(edit_app);
1005
if (path.substr(0, strip.length) == strip)
1007
path = path.substr(strip.length+1);
490
strip_chars = make_path(this_app).length + 1;
491
path = path.substr(strip_chars);
1011
493
if (path.length == 0)