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
206
/* Check if this is a directory listing or file contents */
315
207
var isdir = response.getResponseHeader("X-IVLE-Return") == "Dir";
208
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);
210
var listing = response.responseText;
211
/* The listing SHOULD be valid JSON text. Parse it into an object. */
214
listing = JSON.parse(listing);
218
handle_error("The server returned an invalid directory listing");
221
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
var pre = myFrame.firstChild.getElementsByTagName(
402
'body')[0].firstChild;
403
var data = pre.innerText || pre.textContent;
404
data = JSON.parse(data);
406
alert("Error: " + decodeURIComponent(data['Error']));
407
document.getElementsByName('data')[0].value = '';
225
/* Treat this as an ordinary file. Get the file type. */
226
var content_type = response.getResponseHeader("Content-Type");
227
var handler_type = get_handler_type(content_type);
228
/* If we're in "edit mode", always treat this file as text */
229
would_be_handler_type = handler_type;
230
if (editmode) handler_type = "text";
231
/* handler_type should now be set to either
232
* "text", "image", "audio" or "binary". */
233
switch (handler_type)
238
handle_text(path_join(path, "untitled"), "",
239
would_be_handler_type);
243
handle_text(path, response.responseText,
244
would_be_handler_type);
248
/* TODO: Custom image handler */
249
handle_binary(path, response.responseText);
252
/* TODO: Custom audio handler */
253
handle_binary(path, response.responseText);
445
282
dom_removechildren(document.getElementById("sidepanel"));
285
/** Sets the mode to either "file browser" or "text editor" mode.
286
* This modifies the window icon, and selected tab.
287
* \param editmode If True, editor mode. Else, file browser mode.
289
function setmode(editmode)
291
/* Find the DOM elements for the file browser and editor tabs */
292
var tabs = document.getElementById("apptabs");
293
var tab_files = null;
297
for (var i=0; i<tabs.childNodes.length; i++)
299
/* Find the href of the link within */
300
if (!tabs.childNodes[i].getElementsByTagName) continue;
301
a = tabs.childNodes[i].getElementsByTagName("a");
302
if (a.length == 0) continue;
303
href = a[0].getAttribute("href");
304
if (href == null) continue;
305
if (endswith(href, this_app))
306
tab_files = tabs.childNodes[i];
307
else if (endswith(href, edit_app))
308
tab_edit = tabs.childNodes[i];
313
tab_files.removeAttribute("class");
314
tab_edit.setAttribute("class", "thisapp");
318
tab_edit.removeAttribute("class");
319
tab_files.setAttribute("class", "thisapp");
448
323
/*** HANDLERS for different types of responses (such as dir listing, file,
452
* message may either be a string, or a DOM node, which will be placed inside
455
326
function handle_error(message)
457
329
var files = document.getElementById("filesbody");
459
if (typeof(message) == "string")
461
txt_elem = dom_make_text_elem("div", "Error: "
462
+ message.toString() + ".")
466
/* Assume message is a DOM node */
467
txt_elem = document.createElement("div");
468
txt_elem.appendChild(message);
330
var txt_elem = dom_make_text_elem("div", "Error: "
331
+ message.toString() + ".")
470
332
txt_elem.setAttribute("class", "padding error");
471
333
files.appendChild(txt_elem);
474
/** Given a path, filename and optional revision, returns a URL to open that
475
* revision of that file.
336
/** Presents a path list (address bar inside the page) for clicking.
477
function build_revision_url(path, filename, revision)
338
function presentpath(path)
479
bits = {'path': app_path(this_app, path, filename)};
480
if (current_revision)
340
var dom_path = document.getElementById("path");
341
var href_path = make_path(this_app);
345
/* Also set the document title */
346
document.title = path_basename(path) + " - IVLE";
347
/* Create all of the paths */
348
var pathlist = path.split("/");
349
for (var i=0; i<pathlist.length; i++)
482
bits['query_string'] = 'r=' + revision;
352
if (dir == "") continue;
353
/* Make an 'a' element */
354
href_path = path_join(href_path, dir);
355
nav_path = path_join(nav_path, dir);
356
var link = dom_make_link_elem("a", dir, "Navigate to " + nav_path,
357
href_path/*, "navigate(" + repr(href_path) + ")"*/);
358
dom_path.appendChild(link);
359
dom_path.appendChild(document.createTextNode("/"));
484
return build_url(bits);
361
dom_path.removeChild(dom_path.lastChild);
487
364
/** Given a mime type, returns the path to the icon.
554
425
div.appendChild(par2);
557
/* Enable or disable actions1 moreactions actions. Takes either a single
558
* name, or an array of them.*/
559
function set_action_state(names, which, allow_on_revision)
561
if (!(names instanceof Array)) names = Array(names);
563
for (var i=0; i < names.length; i++)
565
element = document.getElementById('act_' + names[i]);
567
!(current_file.svnstatus == 'revision' && !allow_on_revision))
570
element.setAttribute("class", "choice");
571
element.removeAttribute("disabled");
576
element.setAttribute("class", "disabled");
577
element.setAttribute("disabled", "disabled");
582
/* Updates the list of available actions based on files selected */
583
function update_actions()
586
var numsel = selected_files.length;
587
var svn_selection = false;
591
svn_selection = true;
592
for (var i = 0; i < selected_files.length; i++){
593
if (!svnstatus_versioned(file_listing[selected_files[i]].svnstatus))
595
svn_selection = false;
604
/* Display information about the current directory instead */
605
filename = path_basename(current_path);
608
else if (numsel == 1)
610
filename = selected_files[0];
611
file = file_listing[filename];
614
/* Update each action node in the topbar.
615
* This includes enabling/disabling actions as appropriate, and
616
* setting href/onclick attributes. */
620
/* Available if exactly one file is selected */
621
var open = document.getElementById("act_open");
624
open.setAttribute("class", "choice");
626
open.setAttribute("title",
627
"Navigate to this directory in the file browser");
629
open.setAttribute("title",
630
"Edit or view this file");
631
open.setAttribute("href", build_revision_url(current_path, filename,
636
open.setAttribute("class", "disabled");
637
open.removeAttribute("title");
638
open.removeAttribute("href");
642
/* Available if zero or one files are selected,
643
* and only if this is a file, not a directory */
644
var serve = document.getElementById("act_serve");
645
if (numsel <= 1 && !file.isdir && current_file.svnstatus != 'revision')
647
serve.setAttribute("class", "choice");
648
serve.setAttribute("onclick",
649
"return maybe_save('The last saved version will be served.')");
651
serve.setAttribute("href",
652
app_url(serve_app, current_path));
654
serve.setAttribute("href",
655
app_url(serve_app, current_path, filename));
659
serve.setAttribute("class", "disabled");
660
serve.removeAttribute("href");
661
serve.removeAttribute("onclick");
665
/* Available if exactly one file is selected,
666
* and it is a Python file.
668
var run = document.getElementById("act_run");
670
if (numsel <= 1 && !file.isdir && file.type == "text/x-python"
671
&& current_file.svnstatus != 'revision')
675
// In the edit window
676
var localpath = path_join('/home', current_path);
680
// In the browser window
681
var localpath = path_join('/home', current_path, filename);
683
run.setAttribute("class", "choice");
684
run.setAttribute("onclick", "runfile('" + localpath + "')");
688
run.setAttribute("class", "disabled");
689
run.removeAttribute("onclick");
693
/* Always available for current files.
694
* If 0 files selected, download the current file or directory as a ZIP.
695
* If 1 directory selected, download it as a ZIP.
696
* If 1 non-directory selected, download it.
697
* If >1 files selected, download them all as a ZIP.
699
var download = document.getElementById("act_download");
700
if (current_file.svnstatus == 'revision')
702
download.setAttribute("class", "disabled");
703
download.removeAttribute("onclick");
705
else if (numsel <= 1)
707
download.setAttribute("class", "choice")
710
download.setAttribute("href",
711
app_url(download_app, current_path));
713
download.setAttribute("title",
714
"Download the current directory as a ZIP file");
716
download.setAttribute("title",
717
"Download the current file");
721
download.setAttribute("href",
722
app_url(download_app, current_path, filename));
724
download.setAttribute("title",
725
"Download the selected directory as a ZIP file");
727
download.setAttribute("title",
728
"Download the selected file");
733
/* Make a query string with all the files to download */
734
var dlpath = app_url(download_app, current_path) + "?";
735
for (var i=0; i<numsel; i++)
736
dlpath += "path=" + encodeURIComponent(selected_files[i]) + "&";
737
dlpath = dlpath.substr(0, dlpath.length-1);
738
download.setAttribute("class", "choice")
739
download.setAttribute("href", dlpath);
740
download.setAttribute("title",
741
"Download the selected files as a ZIP file");
744
/* Refresh - No changes required */
746
/* Publish and Submit */
747
/* If this directory is under subversion and selected/unselected file is a
749
var publish = document.getElementById("act_publish");
750
var submit = document.getElementById("act_submit");
751
var pubcond = numsel <= 1 && file.isdir;
754
/* If this dir is already published, call it "Unpublish" */
757
publish.setAttribute("value", "unpublish");
758
publish.setAttribute("title" ,"Make it so this directory "
759
+ "can not be seen by anyone on the web");
760
publish.firstChild.nodeValue = "Unpublish";
762
publish.setAttribute("value", "publish");
763
publish.setAttribute("title","Make it so this directory "
764
+ "can be seen by anyone on the web");
765
publish.firstChild.nodeValue = "Publish";
768
set_action_state(["publish", "submit"], pubcond);
771
/* If exactly 1 non-directory file is selected, and its parent
772
* directory is published.
774
set_action_state("share", numsel == 1 && !file.isdir &&
775
current_file.published);
778
/* If exactly 1 file is selected */
779
set_action_state("rename", numsel == 1);
781
/* Delete, cut, copy */
782
/* If >= 1 file is selected */
783
set_action_state(["delete", "cut", "copy"], numsel >= 1);
785
/* Paste, new file, new directory, upload */
786
/* Disable if the current file is not a directory */
787
set_action_state(["paste", "newfile", "mkdir", "upload"], current_file.isdir);
789
/* Subversion actions */
790
/* These are only useful if we are in a versioned directory and have some
792
set_action_state(["svnadd"], numsel >= 1 && current_file.svnstatus);
793
/* And these are only useful is ALL the selected files are versioned */
794
set_action_state(["svnremove", "svnrevert", "svncopy", "svncut"],
795
numsel >= 1 && current_file.svnstatus && svn_selection);
796
/* Commit is useful if ALL selected files are versioned, or the current
797
* directory is versioned */
798
set_action_state(["svncommit"], current_file.svnstatus &&
799
(numsel >= 1 && svn_selection || numsel == 0));
801
/* Diff, log and update only support one path at the moment, so we must
802
* have 0 or 1 versioned files selected. If 0, the directory must be
804
single_versioned_path = (
806
(numsel == 1 && (svnst = file_listing[selected_files[0]].svnstatus)) ||
807
(numsel == 0 && (svnst = current_file.svnstatus))
808
) && svnstatus_versioned(svnst));
809
set_action_state(["svndiff", "svnupdate"], single_versioned_path);
811
/* We can resolve if we have a file selected and it is conflicted. */
812
set_action_state("svnresolved", single_versioned_path && numsel == 1 && svnst == "conflicted");
814
/* Log should be available for revisions as well. */
815
set_action_state("svnlog", single_versioned_path, true);
817
/* Cleanup should be available for revisions as well. */
818
set_action_state("svncleanup", single_versioned_path, true);
820
single_ivle_versioned_path = (
822
(numsel == 1 && (stat = file_listing[selected_files[0]])) ||
823
(numsel == 0 && (stat = current_file))
824
) && svnstatus_versioned(stat.svnstatus)
826
&& stat.svnurl.substr(0, svn_base.length) == svn_base);
827
set_action_state(["submit"], single_ivle_versioned_path);
829
/* There is currently nothing on the More Actions menu of use
830
* when the current file is not a directory. Hence, just remove
832
* (This makes some of the above decisions somewhat redundant).
833
* We also take this opportunity to show the appropriate actions2
834
* bar for this path. It should either be a save or upload widget.
836
if (current_file.isdir)
838
var actions2_directory = document.getElementById("actions2_directory");
839
actions2_directory.setAttribute("style", "display: inline;");
840
var moreactions = document.getElementById("moreactions_area");
841
moreactions.setAttribute("style", "display: inline;");
845
var actions2_file = document.getElementById("actions2_file");
846
actions2_file.setAttribute("style", "display: inline;");
852
/** Event handler for when an item of the "More actions..." dropdown box is
853
* selected. Performs the selected action. */
854
function handle_moreactions()
856
var moreactions = document.getElementById("moreactions");
857
if (moreactions.value == "top")
859
var selectedaction = moreactions.value;
860
/* Reset to "More actions..." */
861
moreactions.selectedIndex = 0;
863
/* If 0 files selected, filename is the name of the current dir.
864
* If 1 file selected, filename is that file.
866
if (selected_files.length == 0)
867
filename = path_basename(current_path);
868
else if (selected_files.length == 1)
869
filename = selected_files[0];
873
/* Now handle the selected action */
874
switch(selectedaction)
877
action_publish(selected_files);
880
action_unpublish(selected_files);
883
window.open(public_app_url("~" + current_path, filename), 'share')
886
if (selected_files.length == 1)
887
stat = file_listing[selected_files[0]];
890
url = stat.svnurl.substr(svn_base.length); // URL-encoded
891
path = decodeURIComponent(url);
893
/* The working copy might not have an up-to-date version of the
894
* directory. While submitting like this could yield unexpected
895
* results, we should really submit the latest revision to minimise
896
* terrible mistakes - so we run off and ask fileservice for the
898
$.post(app_path(service_app, current_path),
899
{"action": "svnrepostat", "path": path},
902
window.location = path_join(app_path('+submit'), url) + '?revision=' + result.svnrevision;
908
action_rename(filename);
911
action_delete(selected_files);
914
action_copy(selected_files);
917
action_cut(selected_files);
929
show_uploadpanel(true);
932
action_add(selected_files);
935
action_remove(selected_files);
938
action_revert(selected_files);
941
window.location = path_join(app_url('diff'), current_path, selected_files[0] || '');
944
action_update(selected_files);
947
action_resolved(selected_files);
950
action_commit(selected_files);
953
window.location = path_join(app_url('svnlog'), current_path, selected_files[0] || '');
956
action_svncopy(selected_files);
959
action_svncut(selected_files);
962
action_svncleanup(".");
967
/** User clicks "Run" button.
968
* Do an Ajax call and print the test output.
970
function runfile(localpath)
972
if (!maybe_save('The last saved version will be run.')) return false;
974
/* Dump the entire file to the console */
975
var callback = function()
977
console_enter_line("execfile('" + localpath + "')", "block");
979
start_server(callback)
983
428
/** Called when the page loads initially.
985
function browser_init()
430
window.onload = function()
987
432
/* Navigate (internally) to the path in the URL bar.
988
433
* This causes the page to be populated with whatever is at that address,
989
434
* whether it be a directory or a file.
991
var path = get_path();
995
/** Gets the current path of the window */
996
function get_path() {
997
436
var path = parse_url(window.location.href).path;
998
437
/* Strip out root_dir + "/files" from the front of the path */
999
438
var strip = make_path(this_app);
439
var editmode = false;
1000
440
if (path.substr(0, strip.length) == strip)
1001
441
path = path.substr(strip.length+1);