250
var top_level_dir = path==username;
253
var req = ajax_call(null, "userservice", "get_enrolments", null, "GET")
254
subjects = decode_response(req);
258
/* This will always return a listing, whether it is a dir or a file.
260
var listing = response.responseText;
261
/* The listing SHOULD be valid JSON text. Parse it into an object. */
264
listing = JSON.parse(listing);
265
file_listing = listing.listing; /* Global */
271
var err = document.createElement("div");
272
var p = dom_make_text_elem("p", "Error: "
273
+ "There was an unexpected server error processing "
274
+ "the selected command.");
276
p = dom_make_text_elem("p", "If the problem persists, please "
277
+ "contact the system administrator.")
279
p = document.createElement("p");
280
var refresh = document.createElement("input");
281
refresh.setAttribute("type", "button");
282
refresh.setAttribute("value", "Back to file view");
283
refresh.setAttribute("onclick", "refresh()");
284
p.appendChild(refresh);
290
var err = document.createElement("div");
291
var p = dom_make_text_elem("p", "Error: "
292
+ "There was an unexpected server error retrieving "
293
+ "the requested file or directory.");
295
p = dom_make_text_elem("p", "If the problem persists, please "
296
+ "contact the system administrator.")
302
/* Get "." out, it's special */
303
current_file = file_listing["."]; /* Global */
304
delete file_listing["."];
306
if ('revision' in listing)
308
current_revision = listing.revision;
311
209
/* Check if this is a directory listing or file contents */
312
210
var isdir = response.getResponseHeader("X-IVLE-Return") == "Dir";
211
if (!editmode && isdir)
316
home_listing(listing, subjects, path);
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);
320
/* Need to make a 2nd ajax call, this time get the actual file
322
callback = function(response)
324
/* Read the response and set up the page accordingly */
325
handle_contents_response(path, response);
327
/* Call the server and request the listing. */
329
args = shallow_clone_object(url_args);
332
/* This time, get the contents of the file, not its metadata */
333
args['return'] = "contents";
334
ajax_call(callback, service_app, path, args, "GET");
336
update_actions(isdir);
339
function handle_contents_response(path, response)
341
/* Treat this as an ordinary file. Get the file type. */
342
var content_type = response.getResponseHeader("Content-Type");
343
var handler_type = get_handler_type(content_type);
344
would_be_handler_type = handler_type;
345
/* handler_type should now be set to either
346
* "text", "image", "audio" or "binary". */
347
switch (handler_type)
350
handle_text(path, response.responseText,
351
would_be_handler_type);
354
/* TODO: Custom image handler */
355
handle_binary(path, response.responseText);
358
/* TODO: Custom audio handler */
359
handle_binary(path, response.responseText);
367
/* Called when a form upload comes back (from an iframe).
368
* Refreshes the page.
370
function upload_callback()
372
/* This has a pretty nasty hack, which happens to work.
373
* upload_callback is set as the "onload" callback for the iframe which
374
* receives the response from the server for uploading a file.
375
* This means it gets called twice. Once when initialising the iframe, and
376
* a second time when the actual response comes back.
377
* All we want to do is call navigate to refresh the page. But we CAN'T do
378
* that on the first load or it will just go into an infinite cycle of
379
* refreshing. We need to refresh the page ONLY on the second refresh.
380
* upload_callback_count is reset to 0 just before the iframe is created.
382
upload_callback_count++;
383
if (upload_callback_count >= 2)
385
myFrame = frames['upload_iframe'].document;
386
/* Browsers will turn the raw returned JSON into an HTML document. We
387
* need to get the <pre> from inside the <body>, and look at its text.
389
data = myFrame.firstChild.getElementsByTagName(
390
'body')[0].firstChild.firstChild.nodeValue;
391
data = JSON.parse(data);
393
alert("Error: " + decodeURIComponent(data['Error']));
394
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);
432
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");
435
326
/*** HANDLERS for different types of responses (such as dir listing, file,
439
* message may either be a string, or a DOM node, which will be placed inside
442
329
function handle_error(message)
444
332
var files = document.getElementById("filesbody");
446
if (typeof(message) == "string")
448
txt_elem = dom_make_text_elem("div", "Error: "
449
+ message.toString() + ".")
453
/* Assume message is a DOM node */
454
txt_elem = document.createElement("div");
455
txt_elem.appendChild(message);
333
var txt_elem = dom_make_text_elem("div", "Error: "
334
+ message.toString() + ".")
457
335
txt_elem.setAttribute("class", "padding error");
458
336
files.appendChild(txt_elem);
461
/** Given a path, filename and optional revision, returns a URL to open that
462
* revision of that file.
339
/** Presents a path list (address bar inside the page) for clicking.
464
function build_revision_url(path, filename, revision)
341
function presentpath(path)
466
bits = {'path': app_path(this_app, path, filename)};
467
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++)
469
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("/"));
471
return build_url(bits);
364
dom_path.removeChild(dom_path.lastChild);
474
367
/** Given a mime type, returns the path to the icon.
541
428
div.appendChild(par2);
544
/* Enable or disable actions1 moreactions actions. Takes either a single
545
* name, or an array of them.*/
546
function set_action_state(names, which, allow_on_revision)
548
if (!(names instanceof Array)) names = Array(names);
550
for (var i=0; i < names.length; i++)
552
element = document.getElementById('act_' + names[i]);
554
!(current_file.svnstatus == 'revision' && !allow_on_revision))
557
element.setAttribute("class", "choice");
558
element.removeAttribute("disabled");
563
element.setAttribute("class", "disabled");
564
element.setAttribute("disabled", "disabled");
569
/* Updates the list of available actions based on files selected */
570
function update_actions()
573
var numsel = selected_files.length;
574
var svn_selection = false;
578
svn_selection = true;
579
for (var i = 0; i < selected_files.length; i++){
580
if (!svnstatus_versioned(file_listing[selected_files[i]].svnstatus))
582
svn_selection = false;
591
/* Display information about the current directory instead */
592
filename = path_basename(current_path);
595
else if (numsel == 1)
597
filename = selected_files[0];
598
file = file_listing[filename];
601
/* Update each action node in the topbar.
602
* This includes enabling/disabling actions as appropriate, and
603
* setting href/onclick attributes. */
607
/* Available if exactly one file is selected */
608
var open = document.getElementById("act_open");
611
open.setAttribute("class", "choice");
613
open.setAttribute("title",
614
"Navigate to this directory in the file browser");
616
open.setAttribute("title",
617
"Edit or view this file");
618
open.setAttribute("href", build_revision_url(current_path, filename,
623
open.setAttribute("class", "disabled");
624
open.removeAttribute("title");
625
open.removeAttribute("href");
629
/* Available if zero or one files are selected,
630
* and only if this is a file, not a directory */
631
var serve = document.getElementById("act_serve");
632
if (numsel <= 1 && !file.isdir && current_file.svnstatus != 'revision')
634
serve.setAttribute("class", "choice");
635
serve.setAttribute("onclick",
636
"return maybe_save('The last saved version will be served.')");
638
serve.setAttribute("href",
639
app_url(serve_app, current_path));
641
serve.setAttribute("href",
642
app_url(serve_app, current_path, filename));
646
serve.setAttribute("class", "disabled");
647
serve.removeAttribute("href");
648
serve.removeAttribute("onclick");
652
/* Available if exactly one file is selected,
653
* and it is a Python file.
655
var run = document.getElementById("act_run");
657
if (numsel <= 1 && !file.isdir && file.type == "text/x-python"
658
&& current_file.svnstatus != 'revision')
662
// In the edit window
663
var localpath = path_join('/home', current_path);
667
// In the browser window
668
var localpath = path_join('/home', current_path, filename);
670
run.setAttribute("class", "choice");
671
run.setAttribute("onclick", "runfile('" + localpath + "')");
675
run.setAttribute("class", "disabled");
676
run.removeAttribute("onclick");
680
/* Always available for current files.
681
* If 0 files selected, download the current file or directory as a ZIP.
682
* If 1 directory selected, download it as a ZIP.
683
* If 1 non-directory selected, download it.
684
* If >1 files selected, download them all as a ZIP.
686
var download = document.getElementById("act_download");
687
if (current_file.svnstatus == 'revision')
689
download.setAttribute("class", "disabled");
690
download.removeAttribute("onclick");
692
else if (numsel <= 1)
694
download.setAttribute("class", "choice")
697
download.setAttribute("href",
698
app_url(download_app, current_path));
700
download.setAttribute("title",
701
"Download the current directory as a ZIP file");
703
download.setAttribute("title",
704
"Download the current file");
708
download.setAttribute("href",
709
app_url(download_app, current_path, filename));
711
download.setAttribute("title",
712
"Download the selected directory as a ZIP file");
714
download.setAttribute("title",
715
"Download the selected file");
720
/* Make a query string with all the files to download */
721
var dlpath = app_url(download_app, current_path) + "?";
722
for (var i=0; i<numsel; i++)
723
dlpath += "path=" + encodeURIComponent(selected_files[i]) + "&";
724
dlpath = dlpath.substr(0, dlpath.length-1);
725
download.setAttribute("class", "choice")
726
download.setAttribute("href", dlpath);
727
download.setAttribute("title",
728
"Download the selected files as a ZIP file");
731
/* Refresh - No changes required */
733
/* Publish and Submit */
734
/* If this directory is under subversion and selected/unselected file is a
736
var publish = document.getElementById("act_publish");
737
var submit = document.getElementById("act_submit");
738
var pubcond = numsel <= 1 && file.isdir;
741
/* If this dir is already published, call it "Unpublish" */
744
publish.setAttribute("value", "unpublish");
745
publish.setAttribute("title" ,"Make it so this directory "
746
+ "can not be seen by anyone on the web");
747
publish.firstChild.nodeValue = "Unpublish";
749
publish.setAttribute("value", "publish");
750
publish.setAttribute("title","Make it so this directory "
751
+ "can be seen by anyone on the web");
752
publish.firstChild.nodeValue = "Publish";
755
set_action_state(["publish", "submit"], pubcond);
758
/* If exactly 1 non-directory file is selected, and its parent
759
* directory is published.
761
set_action_state("share", numsel == 1 && !file.isdir &&
762
current_file.published);
765
/* If exactly 1 file is selected */
766
set_action_state("rename", numsel == 1);
768
/* Delete, cut, copy */
769
/* If >= 1 file is selected */
770
set_action_state(["delete", "cut", "copy"], numsel >= 1);
772
/* Paste, new file, new directory, upload */
773
/* Disable if the current file is not a directory */
774
set_action_state(["paste", "newfile", "mkdir", "upload"], current_file.isdir);
776
/* Subversion actions */
777
/* These are only useful if we are in a versioned directory and have some
779
set_action_state(["svnadd"], numsel >= 1 && current_file.svnstatus);
780
/* And these are only useful is ALL the selected files are versioned */
781
set_action_state(["svnremove", "svnrevert", "svncopy", "svncut"],
782
numsel >= 1 && current_file.svnstatus && svn_selection);
783
/* Commit is useful if ALL selected files are versioned, or the current
784
* directory is versioned */
785
set_action_state(["svncommit"], current_file.svnstatus &&
786
(numsel >= 1 && svn_selection || numsel == 0));
788
/* Diff, log and update only support one path at the moment, so we must
789
* have 0 or 1 versioned files selected. If 0, the directory must be
791
single_versioned_path = (
793
(numsel == 1 && (svnst = file_listing[selected_files[0]].svnstatus)) ||
794
(numsel == 0 && (svnst = current_file.svnstatus))
795
) && svnstatus_versioned(svnst));
796
set_action_state(["svndiff", "svnupdate"], single_versioned_path);
798
/* We can resolve if we have a file selected and it is conflicted. */
799
set_action_state("svnresolved", single_versioned_path && numsel == 1 && svnst == "conflicted");
801
/* Log should be available for revisions as well. */
802
set_action_state("svnlog", single_versioned_path, true);
804
/* Cleanup should be available for revisions as well. */
805
set_action_state("svncleanup", single_versioned_path, true);
807
single_ivle_versioned_path = (
809
(numsel == 1 && (stat = file_listing[selected_files[0]])) ||
810
(numsel == 0 && (stat = current_file))
811
) && svnstatus_versioned(stat.svnstatus)
813
&& stat.svnurl.substr(0, svn_base.length) == svn_base);
814
set_action_state(["submit"], single_ivle_versioned_path);
816
/* There is currently nothing on the More Actions menu of use
817
* when the current file is not a directory. Hence, just remove
819
* (This makes some of the above decisions somewhat redundant).
820
* We also take this opportunity to show the appropriate actions2
821
* bar for this path. It should either be a save or upload widget.
823
if (current_file.isdir)
825
var actions2_directory = document.getElementById("actions2_directory");
826
actions2_directory.setAttribute("style", "display: inline;");
827
var moreactions = document.getElementById("moreactions_area");
828
moreactions.setAttribute("style", "display: inline;");
832
var actions2_file = document.getElementById("actions2_file");
833
actions2_file.setAttribute("style", "display: inline;");
839
/** Event handler for when an item of the "More actions..." dropdown box is
840
* selected. Performs the selected action. */
841
function handle_moreactions()
843
var moreactions = document.getElementById("moreactions");
844
if (moreactions.value == "top")
846
var selectedaction = moreactions.value;
847
/* Reset to "More actions..." */
848
moreactions.selectedIndex = 0;
850
/* If 0 files selected, filename is the name of the current dir.
851
* If 1 file selected, filename is that file.
853
if (selected_files.length == 0)
854
filename = path_basename(current_path);
855
else if (selected_files.length == 1)
856
filename = selected_files[0];
860
/* Now handle the selected action */
861
switch(selectedaction)
864
action_publish(selected_files);
867
action_unpublish(selected_files);
870
window.open(public_app_url("~" + current_path, filename), 'share')
873
if (selected_files.length == 1)
874
stat = file_listing[selected_files[0]];
877
url = stat.svnurl.substr(svn_base.length); // URL-encoded
878
path = decodeURIComponent(url);
880
/* The working copy might not have an up-to-date version of the
881
* directory. While submitting like this could yield unexpected
882
* results, we should really submit the latest revision to minimise
883
* terrible mistakes - so we run off and ask fileservice for the
885
$.post(app_path(service_app, current_path),
886
{"action": "svnrepostat", "path": path},
889
window.location = path_join(app_path('+submit'), url) + '?revision=' + result.svnrevision;
895
action_rename(filename);
898
action_delete(selected_files);
901
action_copy(selected_files);
904
action_cut(selected_files);
916
show_uploadpanel(true);
919
action_add(selected_files);
922
action_remove(selected_files);
925
action_revert(selected_files);
928
window.location = path_join(app_url('diff'), current_path, selected_files[0] || '');
931
action_update(selected_files);
934
action_resolved(selected_files);
937
action_commit(selected_files);
940
window.location = path_join(app_url('svnlog'), current_path, selected_files[0] || '');
943
action_svncopy(selected_files);
946
action_svncut(selected_files);
949
action_svncleanup(".");
954
/** User clicks "Run" button.
955
* Do an Ajax call and print the test output.
957
function runfile(localpath)
959
if (!maybe_save('The last saved version will be run.')) return false;
961
/* Dump the entire file to the console */
962
var callback = function()
964
console_enter_line("execfile('" + localpath + "')", "block");
966
start_server(callback)
970
431
/** Called when the page loads initially.
972
function browser_init()
433
window.onload = function()
974
435
/* Navigate (internally) to the path in the URL bar.
975
436
* This causes the page to be populated with whatever is at that address,
976
437
* whether it be a directory or a file.
978
var path = get_path();
982
/** Gets the current path of the window */
983
function get_path() {
984
439
var path = parse_url(window.location.href).path;
985
440
/* Strip out root_dir + "/files" from the front of the path */
986
441
var strip = make_path(this_app);
442
var editmode = false;
987
443
if (path.substr(0, strip.length) == strip)
988
444
path = path.substr(strip.length+1);