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
208
/* Check if this is a directory listing or file contents */
315
209
var isdir = response.getResponseHeader("X-IVLE-Return") == "Dir";
210
if (!editmode && isdir)
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);
212
var listing = response.responseText;
213
/* The listing SHOULD be valid JSON text. Parse it into an object. */
216
listing = JSON.parse(listing);
220
handle_error("The server returned an invalid directory listing");
223
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);
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 = '';
227
/* Treat this as an ordinary file. Get the file type. */
228
var content_type = response.getResponseHeader("Content-Type");
229
var handler_type = get_handler_type(content_type);
230
/* If we're in "edit mode", always treat this file as text */
231
would_be_handler_type = handler_type;
232
if (editmode) handler_type = "text";
233
/* handler_type should now be set to either
234
* "text", "image", "audio" or "binary". */
235
switch (handler_type)
240
handle_text(path_join(path, "untitled"), "",
241
would_be_handler_type);
245
handle_text(path, response.responseText,
246
would_be_handler_type);
250
/* TODO: Custom image handler */
251
handle_binary(path, response.responseText);
254
/* TODO: Custom audio handler */
255
handle_binary(path, response.responseText);
444
284
dom_removechildren(document.getElementById("sidepanel"));
287
/** Sets the mode to either "file browser" or "text editor" mode.
288
* This modifies the window icon, and selected tab.
289
* \param editmode If True, editor mode. Else, file browser mode.
291
function setmode(editmode)
293
/* Find the DOM elements for the file browser and editor tabs */
294
var tabs = document.getElementById("apptabs");
295
var tab_files = null;
299
for (var i=0; i<tabs.childNodes.length; i++)
301
/* Find the href of the link within */
302
if (!tabs.childNodes[i].getElementsByTagName) continue;
303
a = tabs.childNodes[i].getElementsByTagName("a");
304
if (a.length == 0) continue;
305
href = a[0].getAttribute("href");
306
if (href == null) continue;
307
if (endswith(href, this_app))
308
tab_files = tabs.childNodes[i];
309
else if (endswith(href, edit_app))
310
tab_edit = tabs.childNodes[i];
315
tab_files.removeAttribute("class");
316
tab_edit.setAttribute("class", "thisapp");
320
tab_edit.removeAttribute("class");
321
tab_files.setAttribute("class", "thisapp");
447
325
/*** HANDLERS for different types of responses (such as dir listing, file,
451
* message may either be a string, or a DOM node, which will be placed inside
454
328
function handle_error(message)
456
331
var files = document.getElementById("filesbody");
458
if (typeof(message) == "string")
460
txt_elem = dom_make_text_elem("div", "Error: "
461
+ message.toString() + ".")
465
/* Assume message is a DOM node */
466
txt_elem = document.createElement("div");
467
txt_elem.appendChild(message);
332
var txt_elem = dom_make_text_elem("div", "Error: "
333
+ message.toString() + ".")
469
334
txt_elem.setAttribute("class", "padding error");
470
335
files.appendChild(txt_elem);
473
/** Given a path, filename and optional revision, returns a URL to open that
474
* revision of that file.
338
/** Presents a path list (address bar inside the page) for clicking.
476
function build_revision_url(path, filename, revision)
340
function presentpath(path)
478
bits = {'path': app_path(this_app, path, filename)};
479
if (current_revision)
342
var dom_path = document.getElementById("path");
343
var href_path = make_path(this_app);
347
/* Also set the document title */
348
document.title = path_basename(path) + " - IVLE";
349
/* Create all of the paths */
350
var pathlist = path.split("/");
351
for (var i=0; i<pathlist.length; i++)
481
bits['query_string'] = 'r=' + revision;
354
if (dir == "") continue;
355
/* Make an 'a' element */
356
href_path = path_join(href_path, dir);
357
nav_path = path_join(nav_path, dir);
358
var link = dom_make_link_elem("a", dir, "Navigate to " + nav_path,
359
href_path/*, "navigate(" + repr(href_path) + ")"*/);
360
dom_path.appendChild(link);
361
dom_path.appendChild(document.createTextNode("/"));
483
return build_url(bits);
363
dom_path.removeChild(dom_path.lastChild);
486
366
/** Given a mime type, returns the path to the icon.
553
427
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
430
/** Called when the page loads initially.
984
function browser_init()
432
window.onload = function()
986
434
/* Navigate (internally) to the path in the URL bar.
987
435
* This causes the page to be populated with whatever is at that address,
988
436
* 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
438
var path = parse_url(window.location.href).path;
997
439
/* Strip out root_dir + "/files" from the front of the path */
998
440
var strip = make_path(this_app);
441
var editmode = false;
999
442
if (path.substr(0, strip.length) == strip)
1000
443
path = path.substr(strip.length+1);