245
var top_level_dir = path==username;
248
var req = ajax_call(null, "userservice", "get_enrolments", null, "GET")
249
subjects = decode_response(req);
253
/* This will always return a listing, whether it is a dir or a file.
255
var listing = response.responseText;
256
/* The listing SHOULD be valid JSON text. Parse it into an object. */
259
listing = JSON.parse(listing);
260
file_listing = listing.listing; /* Global */
266
var err = document.createElement("div");
267
var p = dom_make_text_elem("p", "Error: "
268
+ "There was an unexpected server error processing "
269
+ "the selected command.");
271
p = dom_make_text_elem("p", "If the problem persists, please "
272
+ "contact the system administrator.")
274
p = document.createElement("p");
275
var refresh = document.createElement("input");
276
refresh.setAttribute("type", "button");
277
refresh.setAttribute("value", "Back to file view");
278
refresh.setAttribute("onclick", "refresh()");
279
p.appendChild(refresh);
285
var err = document.createElement("div");
286
var p = dom_make_text_elem("p", "Error: "
287
+ "There was an unexpected server error retrieving "
288
+ "the requested file or directory.");
290
p = dom_make_text_elem("p", "If the problem persists, please "
291
+ "contact the system administrator.")
297
/* Get "." out, it's special */
298
current_file = file_listing["."]; /* Global */
299
delete file_listing["."];
301
if ('revision' in listing)
303
current_revision = listing.revision;
306
124
/* Check if this is a directory listing or file contents */
307
var isdir = response.getResponseHeader("X-IVLE-Return") == "Dir";
125
if (response.getResponseHeader("X-IVLE-Return") == "Dir")
311
home_listing(listing, subjects, path);
127
var listing = response.responseText;
128
/* The listing SHOULD be valid JSON text. Parse it into an object. */
131
listing = JSON.parse(listing);
135
handle_error("The server returned an invalid directory listing");
138
handle_dir_listing(path, listing);
315
/* Need to make a 2nd ajax call, this time get the actual file
317
callback = function(response)
319
/* Read the response and set up the page accordingly */
320
handle_contents_response(path, response);
322
/* Call the server and request the listing. */
324
args = shallow_clone_object(url_args);
142
/* Treat this as an ordinary file. Get the file type. */
143
var content_type = response.getResponseHeader("Content-Type");
145
if (content_type in type_handlers)
146
handler_type = type_handlers[content_type];
327
/* This time, get the contents of the file, not its metadata */
328
args['return'] = "contents";
329
ajax_call(callback, service_app, path, args, "GET");
331
update_actions(isdir);
334
function handle_contents_response(path, response)
336
/* Treat this as an ordinary file. Get the file type. */
337
var content_type = response.getResponseHeader("Content-Type");
338
var handler_type = get_handler_type(content_type);
339
would_be_handler_type = handler_type;
340
/* handler_type should now be set to either
341
* "text", "image", "audio" or "binary". */
342
switch (handler_type)
345
handle_text(path, response.responseText,
346
would_be_handler_type);
349
/* TODO: Custom image handler */
350
handle_binary(path, response.responseText);
353
/* TODO: Custom audio handler */
354
handle_binary(path, response.responseText);
362
/* Called when a form upload comes back (from an iframe).
363
* Refreshes the page.
365
function upload_callback()
367
/* This has a pretty nasty hack, which happens to work.
368
* upload_callback is set as the "onload" callback for the iframe which
369
* receives the response from the server for uploading a file.
370
* This means it gets called twice. Once when initialising the iframe, and
371
* a second time when the actual response comes back.
372
* All we want to do is call navigate to refresh the page. But we CAN'T do
373
* that on the first load or it will just go into an infinite cycle of
374
* refreshing. We need to refresh the page ONLY on the second refresh.
375
* upload_callback_count is reset to 0 just before the iframe is created.
377
upload_callback_count++;
378
if (upload_callback_count >= 2)
380
myFrame = frames['upload_iframe'].document;
381
data = myFrame.firstChild.childNodes[1].firstChild.firstChild.nodeValue;
382
data = JSON.parse(data);
384
alert("Error: " + decodeURIComponent(data['Error']));
385
document.getElementsByName('data')[0].value = '';
390
/** Deletes all "dynamic" content on the page.
391
* This returns the page back to the state it is in when the HTML arrives to
392
* the browser, ready for another handler to populate it.
396
dom_removechildren(document.getElementById("filesbody"));
399
/* Checks if a file needs to be saved. If it does, the user will be asked
400
* if they want to continue anyway. The caller must specify a warning
401
* sentence which indicates the consequences of continuing.
402
* Returns true if we should continue, and false if we should not.
404
function maybe_save(warning)
406
if (warning == null) warning = '';
407
if (current_file.isdir) return true;
408
if (document.getElementById("save_button").disabled) return true;
409
return confirm("This file has unsaved changes. " + warning +
410
"\nAre you sure you wish to continue?");
413
/** Deletes all "dynamic" content on the page necessary to navigate from
414
* one directory listing to another (does not clear as much as clearpage
416
* This is the equivalent of calling clearpage() then
417
* setup_for_dir_listing(), assuming the page is already on a dir listing.
419
function clearpage_dir()
421
dom_removechildren(document.getElementById("path"));
422
dom_removechildren(document.getElementById("files"));
423
dom_removechildren(document.getElementById("sidepanel"));
148
{ /* Based on the first part of the MIME type */
149
handler_type = content_type.split('/')[0];
150
if (handler_type != "text" && handler_type != "image" &&
151
handler_type != "audio")
152
handler_type = "binary";
154
/* handler_type should now be set to either
155
* "text", "image", "audio" or "binary". */
156
switch (handler_type)
159
handle_text(path, response.responseText);
162
/* TODO: Custom image handler */
163
handle_binary(path, response.responseText);
166
/* TODO: Custom audio handler */
167
handle_binary(path, response.responseText);
426
176
/*** HANDLERS for different types of responses (such as dir listing, file,
430
* message may either be a string, or a DOM node, which will be placed inside
433
179
function handle_error(message)
435
var files = document.getElementById("filesbody");
437
if (typeof(message) == "string")
439
txt_elem = dom_make_text_elem("div", "Error: "
440
+ message.toString() + ".")
444
/* Assume message is a DOM node */
445
txt_elem = document.createElement("div");
446
txt_elem.appendChild(message);
448
txt_elem.setAttribute("class", "padding error");
449
files.appendChild(txt_elem);
452
/** Given a path, filename and optional revision, returns a URL to open that
453
* revision of that file.
455
function build_revision_url(path, filename, revision)
457
bits = {'path': app_path(this_app, path, filename)};
458
if (current_revision)
460
bits['query_string'] = 'r=' + revision;
462
return build_url(bits);
465
/** Given a mime type, returns the path to the icon.
466
* \param type String, Mime type.
467
* \param sizelarge Boolean, optional.
468
* \return Path to the icon. Has applied make_path, so it is relative to site
471
function mime_type_to_icon(type, sizelarge)
474
if (type in type_icons)
475
filename = type_icons[type];
477
filename = default_type_icon;
479
return make_path(path_join(type_icons_path_large, filename));
481
return make_path(path_join(type_icons_path, filename));
484
/** Given an svnstatus, returns the path to the icon.
485
* \param type String, svn status.
486
* \return Path to the icon. Has applied make_path, so it is relative to site
487
* root. May return null to indicate no SVN icon.
489
function svnstatus_to_icon(svnstatus)
492
if (svnstatus in svn_icons)
493
filename = svn_icons[svnstatus];
495
filename = default_svn_icon;
496
if (filename == null) return null;
497
return make_path(path_join(svn_icons_path, filename));
500
/** Given an svnstatus, returns the "nice" string.
502
function svnstatus_to_string(svnstatus)
504
if (svnstatus in svn_nice)
505
return svn_nice[svnstatus];
507
return default_svn_nice;
181
/* TODO: Rather than alert, rebuild the page into a page showing an error
183
alert("Error: " + message.toString() + ".");
186
/** Presents the directory listing.
188
function handle_dir_listing(path, listing)
194
/** Presents the text editor.
196
function handle_text(path, text)
510
202
/** Displays a download link to the binary file.
512
204
function handle_binary(path)
514
var files = document.getElementById("filesbody");
515
var div = document.createElement("div");
516
files.appendChild(div);
517
div.setAttribute("class", "padding");
518
var download_link = app_path(download_app, path);
519
var par1 = dom_make_text_elem("p",
520
"The file " + path + " is a binary file. To download this file, " +
521
"click the following link:");
522
var par2 = dom_make_link_elem("p",
523
"Download " + path, "Download " + path, download_link);
524
div.appendChild(par1);
525
div.appendChild(par2);
528
/* Enable or disable actions1 moreactions actions. Takes either a single
529
* name, or an array of them.*/
530
function set_action_state(names, which, allow_on_revision)
532
if (!(names instanceof Array)) names = Array(names);
534
for (var i=0; i < names.length; i++)
536
element = document.getElementById('act_' + names[i]);
538
!(current_file.svnstatus == 'revision' && !allow_on_revision))
541
element.setAttribute("class", "choice");
542
element.removeAttribute("disabled");
547
element.setAttribute("class", "disabled");
548
element.setAttribute("disabled", "disabled");
553
/* Updates the list of available actions based on files selected */
554
function update_actions()
557
var numsel = selected_files.length;
558
var svn_selection = false;
562
svn_selection = true;
563
for (var i = 0; i < selected_files.length; i++){
564
if (file_listing[selected_files[i]]["svnstatus"] == "unversioned")
566
svn_selection = false;
575
/* Display information about the current directory instead */
576
filename = path_basename(current_path);
579
else if (numsel == 1)
581
filename = selected_files[0];
582
file = file_listing[filename];
585
/* Update each action node in the topbar.
586
* This includes enabling/disabling actions as appropriate, and
587
* setting href/onclick attributes. */
591
/* Available if exactly one file is selected */
592
var open = document.getElementById("act_open");
595
open.setAttribute("class", "choice");
597
open.setAttribute("title",
598
"Navigate to this directory in the file browser");
600
open.setAttribute("title",
601
"Edit or view this file");
602
open.setAttribute("href", build_revision_url(current_path, filename,
607
open.setAttribute("class", "disabled");
608
open.removeAttribute("title");
609
open.removeAttribute("href");
613
/* Available if zero or one files are selected,
614
* and only if this is a file, not a directory */
615
var serve = document.getElementById("act_serve");
616
if (numsel <= 1 && !file.isdir && current_file.svnstatus != 'revision')
618
serve.setAttribute("class", "choice");
619
serve.setAttribute("onclick",
620
"return maybe_save('The last saved version will be served.')");
622
serve.setAttribute("href",
623
app_path(serve_app, current_path));
625
serve.setAttribute("href",
626
app_path(serve_app, current_path, filename));
630
serve.setAttribute("class", "disabled");
631
serve.removeAttribute("href");
632
serve.removeAttribute("onclick");
636
/* Available if exactly one file is selected,
637
* and it is a Python file.
639
var run = document.getElementById("act_run");
641
if (numsel <= 1 && !file.isdir && file.type == "text/x-python"
642
&& current_file.svnstatus != 'revision')
646
// In the edit window
647
var localpath = path_join('/home', current_path);
651
// In the browser window
652
var localpath = path_join('/home', current_path, filename);
654
run.setAttribute("class", "choice");
655
run.setAttribute("onclick", "runfile('" + localpath + "')");
659
run.setAttribute("class", "disabled");
660
run.removeAttribute("onclick");
664
/* Always available for current files.
665
* If 0 files selected, download the current file or directory as a ZIP.
666
* If 1 directory selected, download it as a ZIP.
667
* If 1 non-directory selected, download it.
668
* If >1 files selected, download them all as a ZIP.
670
var download = document.getElementById("act_download");
671
if (current_file.svnstatus == 'revision')
673
download.setAttribute("class", "disabled");
674
download.removeAttribute("onclick");
676
else if (numsel <= 1)
678
download.setAttribute("class", "choice")
681
download.setAttribute("href",
682
app_path(download_app, current_path));
684
download.setAttribute("title",
685
"Download the current directory as a ZIP file");
687
download.setAttribute("title",
688
"Download the current file");
692
download.setAttribute("href",
693
app_path(download_app, current_path, filename));
695
download.setAttribute("title",
696
"Download the selected directory as a ZIP file");
698
download.setAttribute("title",
699
"Download the selected file");
704
/* Make a query string with all the files to download */
705
var dlpath = urlencode_path(app_path(download_app, current_path)) + "?";
706
for (var i=0; i<numsel; i++)
707
dlpath += "path=" + encodeURIComponent(selected_files[i]) + "&";
708
dlpath = dlpath.substr(0, dlpath.length-1);
709
download.setAttribute("class", "choice")
710
download.setAttribute("href", dlpath);
711
download.setAttribute("title",
712
"Download the selected files as a ZIP file");
715
/* Refresh - No changes required */
717
/* Publish and Submit */
718
/* If this directory is under subversion and selected/unselected file is a
720
var publish = document.getElementById("act_publish");
721
var submit = document.getElementById("act_submit");
722
var pubcond = numsel <= 1 && file.isdir;
725
/* If this dir is already published, call it "Unpublish" */
728
publish.setAttribute("value", "unpublish");
729
publish.setAttribute("title" ,"Make it so this directory "
730
+ "can not be seen by anyone on the web");
731
publish.firstChild.nodeValue = "Unpublish";
733
publish.setAttribute("value", "publish");
734
publish.setAttribute("title","Make it so this directory "
735
+ "can be seen by anyone on the web");
736
publish.firstChild.nodeValue = "Publish";
739
set_action_state(["publish", "submit"], pubcond);
742
/* If exactly 1 non-directory file is selected, and its parent
743
* directory is published.
745
set_action_state("share", numsel == 1 && !file.isdir &&
746
current_file.published);
749
/* If exactly 1 file is selected */
750
set_action_state("rename", numsel == 1);
752
/* Delete, cut, copy */
753
/* If >= 1 file is selected */
754
set_action_state(["delete", "cut", "copy"], numsel >= 1);
756
/* Paste, new file, new directory, upload */
757
/* Disable if the current file is not a directory */
758
set_action_state(["paste", "newfile", "mkdir", "upload"], current_file.isdir);
760
/* Subversion actions */
761
/* These are only useful if we are in a versioned directory and have some
763
set_action_state(["svnadd"], numsel >= 1 && current_file.svnstatus);
764
/* And these are only usefull is ALL the selected files are versioned */
765
set_action_state(["svnremove", "svnrevert", "svncommit", "svncopy",
766
"svncut"], numsel >= 1 && current_file.svnstatus && svn_selection);
768
/* Diff, log and update only support one path at the moment, so we must
769
* have 0 or 1 versioned files selected. If 0, the directory must be
771
single_versioned_path = (
773
(numsel == 1 && (svnst = file_listing[selected_files[0]].svnstatus)) ||
774
(numsel == 0 && (svnst = current_file.svnstatus))
775
) && svnst != "unversioned");
776
set_action_state(["svndiff", "svnupdate"], single_versioned_path);
778
/* We can resolve if we have a file selected and it is conflicted. */
779
set_action_state("svnresolved", single_versioned_path && numsel == 1 && svnst == "conflicted");
781
/* Log should be available for revisions as well. */
782
set_action_state("svnlog", single_versioned_path, true);
784
single_ivle_versioned_path = (
786
(numsel == 1 && (stat = file_listing[selected_files[0]])) ||
787
(numsel == 0 && (stat = current_file))
788
) && stat.svnstatus != "unversioned"
790
&& stat.svnurl.substr(0, svn_base.length) == svn_base);
791
set_action_state(["submit"], single_ivle_versioned_path);
793
/* There is currently nothing on the More Actions menu of use
794
* when the current file is not a directory. Hence, just remove
796
* (This makes some of the above decisions somewhat redundant).
797
* We also take this opportunity to show the appropriate actions2
798
* bar for this path. It should either be a save or upload widget.
800
if (current_file.isdir)
802
var actions2_directory = document.getElementById("actions2_directory");
803
actions2_directory.setAttribute("style", "display: inline;");
804
var moreactions = document.getElementById("moreactions_area");
805
moreactions.setAttribute("style", "display: inline;");
809
var actions2_file = document.getElementById("actions2_file");
810
actions2_file.setAttribute("style", "display: inline;");
816
/** Event handler for when an item of the "More actions..." dropdown box is
817
* selected. Performs the selected action. */
818
function handle_moreactions()
820
var moreactions = document.getElementById("moreactions");
821
if (moreactions.value == "top")
823
var selectedaction = moreactions.value;
824
/* Reset to "More actions..." */
825
moreactions.selectedIndex = 0;
827
/* If 0 files selected, filename is the name of the current dir.
828
* If 1 file selected, filename is that file.
830
if (selected_files.length == 0)
831
filename = path_basename(current_path);
832
else if (selected_files.length == 1)
833
filename = selected_files[0];
837
/* Now handle the selected action */
838
switch(selectedaction)
841
action_publish(selected_files);
844
action_unpublish(selected_files);
847
window.open(public_app_path("~" + current_path, filename), 'share')
850
if (selected_files.length == 1)
851
stat = file_listing[selected_files[0]];
854
path = stat.svnurl.substr(svn_base.length);
856
/* The working copy might not have an up-to-date version of the
857
* directory. While submitting like this could yield unexpected
858
* results, we should really submit the latest revision to minimise
859
* terrible mistakes - so we run off and ask fileservice for the
861
$.post(app_path(service_app, current_path),
862
{"action": "svnrepostat", "path": path},
865
window.location = path_join(app_path('+submit'), path) + '?revision=' + result.svnrevision;
871
action_rename(filename);
874
action_delete(selected_files);
877
action_copy(selected_files);
880
action_cut(selected_files);
892
show_uploadpanel(true);
895
action_add(selected_files);
898
action_remove(selected_files);
901
action_revert(selected_files);
904
window.location = path_join(app_path('diff'), current_path, selected_files[0] || '');
907
action_update(selected_files);
910
action_resolved(selected_files);
913
action_commit(selected_files);
916
window.location = path_join(app_path('svnlog'), current_path, selected_files[0] || '');
919
action_svncopy(selected_files);
922
action_svncut(selected_files);
927
/** User clicks "Run" button.
928
* Do an Ajax call and print the test output.
930
function runfile(localpath)
932
if (!maybe_save('The last saved version will be run.')) return false;
934
/* Dump the entire file to the console */
935
var callback = function()
937
console_enter_line("execfile('" + localpath + "')", "block");
939
start_server(callback)
943
210
/** Called when the page loads initially.
945
function browser_init()
212
window.onload = function()
947
/* Navigate (internally) to the path in the URL bar.
948
* This causes the page to be populated with whatever is at that address,
949
* whether it be a directory or a file.
951
var path = parse_url(window.location.href).path;
952
/* Strip out root_dir + "/files" from the front of the path */
953
var strip = make_path(this_app);
954
if (path.substr(0, strip.length) == strip)
955
path = path.substr(strip.length+1);
958
/* See if this is an edit path */
959
strip = make_path(edit_app);
960
if (path.substr(0, strip.length) == strip)
962
path = path.substr(strip.length+1);
966
if (path.length == 0)
968
/* Navigate to the user's home directory by default */