254
/* Remove trailing slash (or path==username won't compare properly) */
255
if (path[path.length-1] == "/")
256
path = path.substr(0, path.length-1);
257
var top_level_dir = path==username;
260
var req = ajax_call(null, "userservice", "get_enrolments", null, "GET")
261
subjects = decode_response(req);
265
/* This will always return a listing, whether it is a dir or a file.
267
var listing = response.responseText;
268
/* The listing SHOULD be valid JSON text. Parse it into an object. */
271
listing = JSON.parse(listing);
272
file_listing = listing.listing; /* Global */
278
var err = document.createElement("div");
279
var p = dom_make_text_elem("p", "Error: "
280
+ "There was an unexpected server error processing "
281
+ "the selected command.");
283
p = dom_make_text_elem("p", "If the problem persists, please "
284
+ "contact the system administrator.")
286
p = document.createElement("p");
287
var refresh = document.createElement("input");
288
refresh.setAttribute("type", "button");
289
refresh.setAttribute("value", "Back to file view");
290
refresh.setAttribute("onclick", "refresh()");
291
p.appendChild(refresh);
297
var err = document.createElement("div");
298
var p = dom_make_text_elem("p", "Error: "
299
+ "There was an unexpected server error retrieving "
300
+ "the requested file or directory.");
302
p = dom_make_text_elem("p", "If the problem persists, please "
303
+ "contact the system administrator.")
309
/* Get "." out, it's special */
310
current_file = file_listing["."]; /* Global */
311
delete file_listing["."];
313
if ('revision' in listing)
315
current_revision = listing.revision;
318
209
/* Check if this is a directory listing or file contents */
319
210
var isdir = response.getResponseHeader("X-IVLE-Return") == "Dir";
211
if (!editmode && isdir)
325
/* Top-level dir, with subjects */
326
special_home_listing(listing, subjects, path);
330
/* Not the top-level dir. Do a normal dir listing. */
331
handle_dir_listing(path, listing.listing);
213
var listing = response.responseText;
214
/* The listing SHOULD be valid JSON text. Parse it into an object. */
217
listing = JSON.parse(listing);
221
handle_error("The server returned an invalid directory listing");
224
handle_dir_listing(path, listing);
336
/* Read the response and set up the page accordingly */
337
var content_type = current_file.type;
338
handle_contents_response(path, content_type, url_args);
341
update_actions(isdir);
344
function handle_contents_response(path, content_type)
346
/* Treat this as an ordinary file. Get the file type. */
347
//var content_type = response.getResponseHeader("Content-Type");
348
var handler_type = get_handler_type(content_type);
349
/* handler_type should now be set to either
350
* "text", "image", "video", "audio" or "binary". */
351
switch (handler_type)
354
handle_text(path, content_type);
360
handle_html5_media(path, content_type, "video");
363
handle_html5_media(path, content_type, "audio");
366
handle_object(path, content_type);
374
/* Called when a form upload comes back (from an iframe).
375
* Refreshes the page.
377
function upload_callback()
379
/* This has a pretty nasty hack, which happens to work.
380
* upload_callback is set as the "onload" callback for the iframe which
381
* receives the response from the server for uploading a file.
382
* This means it gets called twice. Once when initialising the iframe, and
383
* a second time when the actual response comes back.
384
* All we want to do is call navigate to refresh the page. But we CAN'T do
385
* that on the first load or it will just go into an infinite cycle of
386
* refreshing. We need to refresh the page ONLY on the second refresh.
387
* upload_callback_count is reset to 0 just before the iframe is created.
389
upload_callback_count++;
390
if (upload_callback_count >= 2)
392
myFrame = frames['upload_iframe'].document;
393
/* Browsers will turn the raw returned JSON into an HTML document. We
394
* need to get the <pre> from inside the <body>, and look at its text.
396
var pre = myFrame.firstChild.getElementsByTagName(
397
'body')[0].firstChild;
398
var data = pre.innerText || pre.textContent;
399
data = JSON.parse(data);
401
alert("Error: " + decodeURIComponent(data['Error']));
402
document.getElementsByName('data')[0].value = '';
228
/* Treat this as an ordinary file. Get the file type. */
229
var content_type = response.getResponseHeader("Content-Type");
230
var handler_type = get_handler_type(content_type);
231
/* If we're in "edit mode", always treat this file as text */
232
would_be_handler_type = handler_type;
233
if (editmode) handler_type = "text";
234
/* handler_type should now be set to either
235
* "text", "image", "audio" or "binary". */
236
switch (handler_type)
241
handle_text(path_join(path, "untitled"), "",
242
would_be_handler_type);
246
handle_text(path, response.responseText,
247
would_be_handler_type);
251
/* TODO: Custom image handler */
252
handle_binary(path, response.responseText);
255
/* TODO: Custom audio handler */
256
handle_binary(path, response.responseText);
440
285
dom_removechildren(document.getElementById("sidepanel"));
288
/** Sets the mode to either "file browser" or "text editor" mode.
289
* This modifies the window icon, and selected tab.
290
* \param editmode If True, editor mode. Else, file browser mode.
292
function setmode(editmode)
294
/* Find the DOM elements for the file browser and editor tabs */
295
var tabs = document.getElementById("apptabs");
296
var tab_files = null;
300
for (var i=0; i<tabs.childNodes.length; i++)
302
/* Find the href of the link within */
303
if (!tabs.childNodes[i].getElementsByTagName) continue;
304
a = tabs.childNodes[i].getElementsByTagName("a");
305
if (a.length == 0) continue;
306
href = a[0].getAttribute("href");
307
if (href == null) continue;
308
if (endswith(href, this_app))
309
tab_files = tabs.childNodes[i];
310
else if (endswith(href, edit_app))
311
tab_edit = tabs.childNodes[i];
316
tab_files.removeAttribute("class");
317
tab_edit.setAttribute("class", "thisapp");
321
tab_edit.removeAttribute("class");
322
tab_files.setAttribute("class", "thisapp");
443
326
/*** HANDLERS for different types of responses (such as dir listing, file,
447
* message may either be a string, or a DOM node, which will be placed inside
450
329
function handle_error(message)
452
332
var files = document.getElementById("filesbody");
454
if (typeof(message) == "string")
456
txt_elem = dom_make_text_elem("div", "Error: "
457
+ message.toString() + ".")
461
/* Assume message is a DOM node */
462
txt_elem = document.createElement("div");
463
txt_elem.appendChild(message);
333
var txt_elem = dom_make_text_elem("div", "Error: "
334
+ message.toString() + ".")
465
335
txt_elem.setAttribute("class", "padding error");
466
336
files.appendChild(txt_elem);
469
/** Given a path, filename and optional revision, returns a URL to open that
470
* revision of that file.
339
/** Presents a path list (address bar inside the page) for clicking.
472
function build_revision_url(path, filename, revision)
341
function presentpath(path)
474
bits = {'path': app_path(this_app, path, filename)};
475
if (current_revision)
343
var dom_path = document.getElementById("path");
344
var href_path = make_path(this_app);
348
/* Also set the document title */
349
document.title = path_basename(path) + " - IVLE";
350
/* Create all of the paths */
351
var pathlist = path.split("/");
352
for (var i=0; i<pathlist.length; i++)
477
bits['query_string'] = 'r=' + revision;
355
if (dir == "") continue;
356
/* Make an 'a' element */
357
href_path = path_join(href_path, dir);
358
nav_path = path_join(nav_path, dir);
359
var link = dom_make_link_elem("a", dir, "Navigate to " + nav_path,
360
href_path/*, "navigate(" + repr(href_path) + ")"*/);
361
dom_path.appendChild(link);
362
dom_path.appendChild(document.createTextNode("/"));
479
return build_url(bits);
364
dom_path.removeChild(dom_path.lastChild);
482
367
/** Given a mime type, returns the path to the icon.
554
428
div.appendChild(par2);
557
/** Displays an image file.
559
function handle_image(path)
561
/* Disable save button */
562
using_codepress = false;
566
var url = app_url(service_app, path) + "?return=contents";
569
var img = $("<img />");
570
img.attr("alt", path);
571
img.attr("src", url);
574
var div = $('<div class="padding" />');
575
div.append('<h1>Image Preview</h1>');
577
$("#filesbody").append(div);
580
/* Displays a media file using an HTML5 <audio> or <video> tag.
581
* Falls back to <object> if the format is unsupported.
583
function handle_html5_media(path, type, tag_name)
585
/* Disable save button and hide the save panel */
586
using_codepress = false;
590
var url = app_url(service_app, path) + "?return=contents";
591
var download_url = app_url(download_app, path);
593
/* Fallback download link */
595
'<p>Could not play ' + tag_name + ' file. ' +
596
'Try <a>downloading it</a> instead.</p>');
597
link.find('a').attr("href", download_url);
599
/* HTML 5 media element */
600
var html5_element = $(
601
'<' + tag_name + ' controls="true" autoplay="true" />');
602
html5_element.attr("src", url);
603
var support = (html5_element[0].canPlayType &&
604
html5_element[0].canPlayType(type));
606
/* If the browser thinks it might be able to play it, use the HTML5
607
* element. Otherwise, fall back to an <object>, which might work.
609
if (support == "probably" || support == "maybe") {
610
var element = html5_element;
612
var element = $('<object />');
613
element.attr("type", type);
614
element.attr("data", url);
616
element.append(link);
619
var div = $('<div class="padding" />');
620
div.append('<h1>File preview</h1>');
622
$("#filesbody").append(div);
625
/** Display generic object content
627
function handle_object(path, content_type)
629
/* Disable save button and hide the save panel */
630
using_codepress = false;
634
var url = app_url(service_app, path) + "?return=contents";
635
var download_url = app_url(download_app, path);
637
/* Fallback Download Link */
638
var link = $('<p><a /></p>');
639
var a = link.find('a');
640
a.attr("href", download_url);
641
a.text("Download " + path);
644
var obj = $('<object width="100%" height="500px" />');
645
obj.attr("type", content_type);
646
obj.attr("data", url);
647
obj.append('Could not load object');
650
var div = $('<div class="padding" />');
651
div.append('<h1>Preview</h1>');
654
$("#filesbody").append(div);
657
/* Present an element for the given path.
658
* Gives it a title and download link.
660
function present_custom_handler(path, type, element)
662
/* Disable save button and hide the save panel */
663
using_codepress = false;
667
var url = app_url(service_app, path) + "?return=contents";
668
var download_url = app_url(download_app, path);
670
/* Fallback download link */
672
'<p>Could not play ' + tag_name + ' file. ' +
673
'Try <a>downloading it</a> instead.</p>');
674
link.find('a').attr("href", download_url);
676
/* HTML 5 media element */
677
var html5_element = $(
678
'<' + tag_name + ' controls="true" autoplay="true" />');
679
html5_element.attr("src", url);
680
var support = (html5_element[0].canPlayType &&
681
html5_element[0].canPlayType(type));
683
/* If the browser thinks it might be able to play it, use the HTML5
684
* element. Otherwise, fall back to an <object>, which might work.
686
if (support == "probably" || support == "maybe") {
687
var element = html5_element;
689
var element = $('<object />');
690
element.attr("type", type);
691
element.attr("data", url);
693
element.append(link);
696
var div = $('<div class="padding" />');
697
div.append('<h1>File preview</h1>');
699
$("#filesbody").append(div);
702
/* Enable or disable actions1 moreactions actions. Takes either a single
703
* name, or an array of them.*/
704
function set_action_state(names, which, allow_on_revision)
706
if (!(names instanceof Array)) names = Array(names);
708
for (var i=0; i < names.length; i++)
710
element = document.getElementById('act_' + names[i]);
712
!(current_file.svnstatus == 'revision' && !allow_on_revision))
715
element.setAttribute("class", "choice");
716
element.removeAttribute("disabled");
721
element.setAttribute("class", "disabled");
722
element.setAttribute("disabled", "disabled");
727
/* Updates the list of available actions based on files selected */
728
function update_actions()
731
var numsel = selected_files.length;
732
var svn_selection = false;
736
svn_selection = true;
737
for (var i = 0; i < selected_files.length; i++){
738
if (!svnstatus_versioned(file_listing[selected_files[i]].svnstatus))
740
svn_selection = false;
749
/* Display information about the current directory instead */
750
filename = path_basename(current_path);
753
else if (numsel == 1)
755
filename = selected_files[0];
756
file = file_listing[filename];
759
/* Update each action node in the topbar.
760
* This includes enabling/disabling actions as appropriate, and
761
* setting href/onclick attributes. */
765
/* Available if exactly one file is selected */
766
var open = document.getElementById("act_open");
769
open.setAttribute("class", "choice");
771
open.setAttribute("title",
772
"Navigate to this directory in the file browser");
774
open.setAttribute("title",
775
"Edit or view this file");
776
open.setAttribute("href", build_revision_url(current_path, filename,
781
open.setAttribute("class", "disabled");
782
open.removeAttribute("title");
783
open.removeAttribute("href");
787
/* Available if zero or one files are selected,
788
* and only if this is a file, not a directory */
789
var serve = document.getElementById("act_serve");
790
if (numsel <= 1 && !file.isdir && current_file.svnstatus != 'revision')
792
serve.setAttribute("class", "choice");
793
serve.setAttribute("onclick",
794
"return maybe_save('The last saved version will be served.')");
796
serve.setAttribute("href",
797
app_url(serve_app, current_path));
799
serve.setAttribute("href",
800
app_url(serve_app, current_path, filename));
804
serve.setAttribute("class", "disabled");
805
serve.removeAttribute("href");
806
serve.removeAttribute("onclick");
810
/* Available if exactly one file is selected,
811
* and it is a Python file.
813
var run = document.getElementById("act_run");
815
if (numsel <= 1 && !file.isdir && file.type == "text/x-python"
816
&& current_file.svnstatus != 'revision')
820
// In the edit window
821
var localpath = path_join('/home', current_path);
825
// In the browser window
826
var localpath = path_join('/home', current_path, filename);
828
run.setAttribute("class", "choice");
829
run.setAttribute("onclick", "runfile('" + localpath + "')");
833
run.setAttribute("class", "disabled");
834
run.removeAttribute("onclick");
838
/* Always available for current files.
839
* If 0 files selected, download the current file or directory as a ZIP.
840
* If 1 directory selected, download it as a ZIP.
841
* If 1 non-directory selected, download it.
842
* If >1 files selected, download them all as a ZIP.
844
var download = document.getElementById("act_download");
845
if (current_file.svnstatus == 'revision')
847
download.setAttribute("class", "disabled");
848
download.removeAttribute("onclick");
850
else if (numsel <= 1)
852
download.setAttribute("class", "choice")
855
download.setAttribute("href",
856
app_url(download_app, current_path));
858
download.setAttribute("title",
859
"Download the current directory as a ZIP file");
861
download.setAttribute("title",
862
"Download the current file");
866
download.setAttribute("href",
867
app_url(download_app, current_path, filename));
869
download.setAttribute("title",
870
"Download the selected directory as a ZIP file");
872
download.setAttribute("title",
873
"Download the selected file");
878
/* Make a query string with all the files to download */
879
var dlpath = app_url(download_app, current_path) + "?";
880
for (var i=0; i<numsel; i++)
881
dlpath += "path=" + encodeURIComponent(selected_files[i]) + "&";
882
dlpath = dlpath.substr(0, dlpath.length-1);
883
download.setAttribute("class", "choice")
884
download.setAttribute("href", dlpath);
885
download.setAttribute("title",
886
"Download the selected files as a ZIP file");
889
/* Refresh - No changes required */
891
/* Publish and Submit */
892
/* If this directory is under subversion and selected/unselected file is a
894
var publish = document.getElementById("act_publish");
895
var submit = document.getElementById("act_submit");
896
var pubcond = numsel <= 1 && file.isdir;
899
/* If this dir is already published, call it "Unpublish" */
902
publish.setAttribute("value", "unpublish");
903
publish.setAttribute("title" ,"Make it so this directory "
904
+ "can not be seen by anyone on the web");
905
publish.firstChild.nodeValue = "Unpublish";
907
publish.setAttribute("value", "publish");
908
publish.setAttribute("title","Make it so this directory "
909
+ "can be seen by anyone on the web");
910
publish.firstChild.nodeValue = "Publish";
913
set_action_state(["publish", "submit"], pubcond);
916
/* If exactly 1 non-directory file is selected, and its parent
917
* directory is published.
919
set_action_state("share", numsel == 1 && !file.isdir &&
920
current_file.published);
923
/* If exactly 1 file is selected */
924
set_action_state("rename", numsel == 1);
926
/* Delete, cut, copy */
927
/* If >= 1 file is selected */
928
set_action_state(["delete", "cut", "copy"], numsel >= 1);
930
/* Paste, new file, new directory, upload */
931
/* Disable if the current file is not a directory */
932
set_action_state(["paste", "newfile", "mkdir", "upload"], current_file.isdir);
934
/* Subversion actions */
935
/* These are only useful if we are in a versioned directory and have some
937
set_action_state(["svnrename"], numsel == 1 && current_file.svnstatus);
938
set_action_state(["svnadd"], numsel >= 1 && current_file.svnstatus);
939
/* And these are only useful is ALL the selected files are versioned */
940
set_action_state(["svnremove", "svnrevert", "svncopy", "svncut"],
941
numsel >= 1 && current_file.svnstatus && svn_selection);
942
/* Commit is useful if ALL selected files are versioned, or the current
943
* directory is versioned */
944
set_action_state(["svncommit"], current_file.svnstatus &&
945
(numsel >= 1 && svn_selection || numsel == 0));
947
/* Diff, log and update only support one path at the moment, so we must
948
* have 0 or 1 versioned files selected. If 0, the directory must be
950
single_versioned_path = (
952
(numsel == 1 && (svnst = file_listing[selected_files[0]].svnstatus)) ||
953
(numsel == 0 && (svnst = current_file.svnstatus))
954
) && svnstatus_versioned(svnst));
955
set_action_state(["svndiff", "svnupdate"], single_versioned_path);
957
/* We can resolve if we have a file selected and it is conflicted. */
958
set_action_state("svnresolved", single_versioned_path && numsel == 1 && svnst == "conflicted");
960
/* Log should be available for revisions as well. */
961
set_action_state("svnlog", single_versioned_path, true);
963
/* Cleanup should be available for revisions as well. */
964
set_action_state("svncleanup", single_versioned_path, true);
966
single_ivle_versioned_path = (
968
(numsel == 1 && (stat = file_listing[selected_files[0]])) ||
969
(numsel == 0 && (stat = current_file))
970
) && svnstatus_versioned(stat.svnstatus)
972
&& stat.svnurl.substr(0, svn_base.length) == svn_base);
973
set_action_state(["submit"], single_ivle_versioned_path);
975
/* There is currently nothing on the More Actions menu of use
976
* when the current file is not a directory. Hence, just remove
978
* (This makes some of the above decisions somewhat redundant).
979
* We also take this opportunity to show the appropriate actions2
980
* bar for this path. It should either be a save or upload widget.
982
if (current_file.isdir)
984
var actions2_directory = document.getElementById("actions2_directory");
985
actions2_directory.setAttribute("style", "display: inline;");
986
var moreactions = document.getElementById("moreactions_area");
987
moreactions.setAttribute("style", "display: inline;");
993
/** Event handler for when an item of the "More actions..." dropdown box is
994
* selected. Performs the selected action. */
995
function handle_moreactions()
997
var moreactions = document.getElementById("moreactions");
998
if (moreactions.value == "top")
1000
var selectedaction = moreactions.value;
1001
/* Reset to "More actions..." */
1002
moreactions.selectedIndex = 0;
1004
/* If 0 files selected, filename is the name of the current dir.
1005
* If 1 file selected, filename is that file.
1007
if (selected_files.length == 0)
1008
filename = path_basename(current_path);
1009
else if (selected_files.length == 1)
1010
filename = selected_files[0];
1014
/* Now handle the selected action */
1015
switch(selectedaction)
1018
action_publish(selected_files);
1021
action_unpublish(selected_files);
1024
window.open(public_app_url("~" + current_path, filename), 'share')
1027
if (selected_files.length == 1)
1028
stat = file_listing[selected_files[0]];
1030
stat = current_file;
1031
url = stat.svnurl.substr(svn_base.length); // URL-encoded
1032
path = decodeURIComponent(url);
1034
/* The working copy might not have an up-to-date version of the
1035
* directory. While submitting like this could yield unexpected
1036
* results, we should really submit the latest revision to minimise
1037
* terrible mistakes - so we run off and ask fileservice for the
1038
* latest revision.*/
1039
$.post(app_path(service_app, current_path),
1040
{"action": "svnrepostat", "path": path},
1043
window.location = path_join(app_path('+submit'), url) + '?revision=' + result.svnrevision;
1049
action_rename(filename);
1052
action_delete(selected_files);
1055
action_copy(selected_files);
1058
action_cut(selected_files);
1070
show_uploadpanel(true);
1073
action_add(selected_files);
1076
action_svnremove(selected_files);
1079
action_svnrename(selected_files);
1082
action_revert(selected_files);
1085
window.location = path_join(app_url('diff'), current_path, selected_files[0] || '');
1088
action_update(selected_files);
1091
action_resolved(selected_files);
1094
action_commit(selected_files);
1097
window.location = path_join(app_url('svnlog'), current_path, selected_files[0] || '');
1100
action_svncopy(selected_files);
1103
action_svncut(selected_files);
1106
action_svncleanup(".");
1111
/** User clicks "Run" button.
1112
* Do an Ajax call and print the test output.
1114
function runfile(localpath)
1116
if (!maybe_save('The last saved version will be run.')) return false;
1118
/* Dump the entire file to the console */
1119
var callback = function()
1121
console_enter_line("execfile('" + localpath + "')", "block");
1123
start_server(callback)
1127
431
/** Called when the page loads initially.
1129
function browser_init()
433
window.onload = function()
1131
435
/* Navigate (internally) to the path in the URL bar.
1132
436
* This causes the page to be populated with whatever is at that address,
1133
437
* whether it be a directory or a file.
1135
var path = get_path();
1139
/** Gets the current path of the window */
1140
function get_path() {
1141
439
var path = parse_url(window.location.href).path;
1142
440
/* Strip out root_dir + "/files" from the front of the path */
1143
441
var strip = make_path(this_app);
442
var editmode = false;
1144
443
if (path.substr(0, strip.length) == strip)
1145
444
path = path.substr(strip.length+1);