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
134
/* Check if this is a directory listing or file contents */
307
var isdir = response.getResponseHeader("X-IVLE-Return") == "Dir";
135
if (response.getResponseHeader("X-IVLE-Return") == "Dir")
311
home_listing(listing, subjects, path);
137
var listing = response.responseText;
138
/* The listing SHOULD be valid JSON text. Parse it into an object. */
141
listing = JSON.parse(listing);
145
handle_error("The server returned an invalid directory listing");
148
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);
152
/* Treat this as an ordinary file. Get the file type. */
153
var content_type = response.getResponseHeader("Content-Type");
155
if (content_type in type_handlers)
156
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
document.getElementsByName('data')[0].value = '';
158
{ /* Based on the first part of the MIME type */
159
handler_type = content_type.split('/')[0];
160
if (handler_type != "text" && handler_type != "image" &&
161
handler_type != "audio")
162
handler_type = "binary";
164
/* handler_type should now be set to either
165
* "text", "image", "audio" or "binary". */
166
switch (handler_type)
169
handle_text(path, response.responseText);
172
/* TODO: Custom image handler */
173
handle_binary(path, response.responseText);
176
/* TODO: Custom audio handler */
177
handle_binary(path, response.responseText);
421
202
/*** HANDLERS for different types of responses (such as dir listing, file,
425
* message may either be a string, or a DOM node, which will be placed inside
428
205
function handle_error(message)
430
var files = document.getElementById("filesbody");
432
if (typeof(message) == "string")
434
txt_elem = dom_make_text_elem("div", "Error: "
435
+ message.toString() + ".")
439
/* Assume message is a DOM node */
440
txt_elem = document.createElement("div");
441
txt_elem.appendChild(message);
443
txt_elem.setAttribute("class", "padding error");
444
files.appendChild(txt_elem);
447
/** Given a path, filename and optional revision, returns a URL to open that
448
* revision of that file.
450
function build_revision_url(path, filename, revision)
452
bits = {'path': app_path(this_app, path, filename)};
453
if (current_revision)
455
bits['query_string'] = 'r=' + revision;
457
return build_url(bits);
460
/** Given a mime type, returns the path to the icon.
461
* \param type String, Mime type.
462
* \param sizelarge Boolean, optional.
463
* \return Path to the icon. Has applied make_path, so it is relative to site
466
function mime_type_to_icon(type, sizelarge)
469
if (type in type_icons)
470
filename = type_icons[type];
472
filename = default_type_icon;
474
return make_path(path_join(type_icons_path_large, filename));
476
return make_path(path_join(type_icons_path, filename));
479
/** Given an svnstatus, returns the path to the icon.
480
* \param type String, svn status.
481
* \return Path to the icon. Has applied make_path, so it is relative to site
482
* root. May return null to indicate no SVN icon.
484
function svnstatus_to_icon(svnstatus)
487
if (svnstatus in svn_icons)
488
filename = svn_icons[svnstatus];
490
filename = default_svn_icon;
491
if (filename == null) return null;
492
return make_path(path_join(svn_icons_path, filename));
495
/** Given an svnstatus, returns the "nice" string.
497
function svnstatus_to_string(svnstatus)
499
if (svnstatus in svn_nice)
500
return svn_nice[svnstatus];
502
return default_svn_nice;
207
/* TODO: Find a better place to put this message. */
208
var files = document.getElementById("files");
209
var tr = document.createElement("tr");
210
var td = document.createElement("td");
212
var td = document.createElement("td");
214
var td = document.createElement("td");
216
var txt_elem = dom_make_text_elem("td", "Error: "
217
+ message.toString() + ".")
218
txt_elem.setAttribute("class", "error");
219
tr.appendChild(txt_elem);
220
var td = document.createElement("td");
222
var td = document.createElement("td");
224
files.appendChild(tr);
227
/** Presents a path list (address bar inside the page) for clicking.
229
function presentpath(path)
231
var dom_path = document.getElementById("path");
233
var href_path = make_path(this_app);
235
/* Create all of the paths */
236
for each (dir in path.split("/"))
238
if (dir == "") continue;
240
/* Make an 'a' element */
241
href_path = path_join(href_path, dir);
242
var link = dom_make_link_elem("a", dir, href_path,
243
"navigate(" + href_path + ")");
244
dom_path.appendChild(link);
245
dom_path.appendChild(document.createTextNode("/"));
249
/** Presents the directory listing.
251
function handle_dir_listing(path, listing)
254
/* Nav through the top-level of the JSON to the actual listing object. */
255
var listing = listing.listing;
257
/* Get "." out, it's special */
258
var thisdir = listing["."];
260
/* Is this dir under svn? */
261
var under_subversion = "svnstatus" in thisdir;
263
var files = document.getElementById("files");
269
/* Create all of the files */
270
for (var filename in listing)
272
file = listing[filename];
273
/* Make a 'tr' element */
274
row = document.createElement("tr");
275
/* Column 1: Selection checkbox */
276
row.setAttribute("class", "row" + row_toggle.toString())
277
row_toggle = row_toggle == 1 ? 2 : 1;
278
td = document.createElement("td");
279
checkbox = document.createElement("input");
280
checkbox.setAttribute("type", "checkbox");
281
checkbox.setAttribute("title", "Select this file");
282
td.appendChild(checkbox);
286
/* Column 2: Filetype and subversion icons. */
287
td = document.createElement("td");
288
td.setAttribute("class", "thincol");
289
td.appendChild(document.createTextNode("dir"));
291
td = document.createElement("td");
292
td.setAttribute("class", "thincol");
293
if (under_subversion)
294
td.appendChild(document.createTextNode(file.svnstatus));
296
/* Column 3: Filename */
297
row.appendChild(dom_make_link_elem("td", filename,
298
make_path(path_join(this_app, path, filename)),
299
"navigate(" + path_join(path, filename) + ")"));
303
/* Column 2: Filetype and subversion icons. */
304
td = document.createElement("td");
305
td.setAttribute("class", "thincol");
306
td.appendChild(document.createTextNode(file.type));
308
td = document.createElement("td");
309
td.setAttribute("class", "thincol");
310
if (under_subversion)
311
td.appendChild(document.createTextNode(file.svnstatus));
313
/* Column 3: Filename */
314
row.appendChild(dom_make_text_elem("td", filename));
317
row.appendChild(dom_make_text_elem("td", nice_filesize(file.size)));
319
row.appendChild(dom_make_text_elem("td", file.mtime_nice));
320
files.appendChild(row);
325
/** Presents the text editor.
327
function handle_text(path, text)
505
333
/** Displays a download link to the binary file.
507
335
function handle_binary(path)
509
var files = document.getElementById("filesbody");
510
var div = document.createElement("div");
511
files.appendChild(div);
512
div.setAttribute("class", "padding");
513
var download_link = app_path(download_app, path);
514
var par1 = dom_make_text_elem("p",
515
"The file " + path + " is a binary file. To download this file, " +
516
"click the following link:");
517
var par2 = dom_make_link_elem("p",
518
"Download " + path, "Download " + path, download_link);
519
div.appendChild(par1);
520
div.appendChild(par2);
523
/* Enable or disable actions1 moreactions actions. Takes either a single
524
* name, or an array of them.*/
525
function set_action_state(names, which, allow_on_revision)
527
if (!(names instanceof Array)) names = Array(names);
529
for (var i=0; i < names.length; i++)
531
element = document.getElementById('act_' + names[i]);
533
!(current_file.svnstatus == 'revision' && !allow_on_revision))
536
element.setAttribute("class", "choice");
537
element.removeAttribute("disabled");
542
element.setAttribute("class", "disabled");
543
element.setAttribute("disabled", "disabled");
548
function update_actions()
551
var numsel = selected_files.length;
556
/* Display information about the current directory instead */
557
filename = path_basename(current_path);
560
else if (numsel == 1)
562
filename = selected_files[0];
563
file = file_listing[filename];
566
/* Update each action node in the topbar.
567
* This includes enabling/disabling actions as appropriate, and
568
* setting href/onclick attributes. */
572
/* Available if exactly one file is selected */
573
var open = document.getElementById("act_open");
576
open.setAttribute("class", "choice");
578
open.setAttribute("title",
579
"Navigate to this directory in the file browser");
581
open.setAttribute("title",
582
"Edit or view this file");
583
open.setAttribute("href", build_revision_url(current_path, filename,
588
open.setAttribute("class", "disabled");
589
open.removeAttribute("title");
590
open.removeAttribute("href");
594
/* Available if zero or one files are selected,
595
* and only if this is a file, not a directory */
596
var serve = document.getElementById("act_serve");
597
if (numsel <= 1 && !file.isdir && current_file.svnstatus != 'revision')
599
serve.setAttribute("class", "choice");
600
serve.setAttribute("onclick",
601
"return maybe_save('The last saved version will be served.')");
603
serve.setAttribute("href",
604
app_path(serve_app, current_path));
606
serve.setAttribute("href",
607
app_path(serve_app, current_path, filename));
611
serve.setAttribute("class", "disabled");
612
serve.removeAttribute("href");
613
serve.removeAttribute("onclick");
617
/* Available if exactly one file is selected,
618
* and it is a Python file.
620
var run = document.getElementById("act_run");
622
if (!file.isdir && file.type == "text/x-python" && numsel <= 1
623
&& current_file.svnstatus != 'revision')
627
// In the edit window
628
var localpath = path_join('/home', current_path);
632
// In the browser window
633
var localpath = path_join('/home', current_path, filename);
635
run.setAttribute("class", "choice");
636
run.setAttribute("onclick", "runfile('" + localpath + "')");
640
run.setAttribute("class", "disabled");
641
run.removeAttribute("onclick");
645
/* Always available for current files.
646
* If 0 files selected, download the current file or directory as a ZIP.
647
* If 1 directory selected, download it as a ZIP.
648
* If 1 non-directory selected, download it.
649
* If >1 files selected, download them all as a ZIP.
651
var download = document.getElementById("act_download");
652
if (current_file.svnstatus == 'revision')
654
download.setAttribute("class", "disabled");
655
download.removeAttribute("onclick");
657
else if (numsel <= 1)
659
download.setAttribute("class", "choice")
662
download.setAttribute("href",
663
app_path(download_app, current_path));
665
download.setAttribute("title",
666
"Download the current directory as a ZIP file");
668
download.setAttribute("title",
669
"Download the current file");
673
download.setAttribute("href",
674
app_path(download_app, current_path, filename));
676
download.setAttribute("title",
677
"Download the selected directory as a ZIP file");
679
download.setAttribute("title",
680
"Download the selected file");
685
/* Make a query string with all the files to download */
686
var dlpath = urlencode_path(app_path(download_app, current_path)) + "?";
687
for (var i=0; i<numsel; i++)
688
dlpath += "path=" + encodeURIComponent(selected_files[i]) + "&";
689
dlpath = dlpath.substr(0, dlpath.length-1);
690
download.setAttribute("class", "choice")
691
download.setAttribute("href", dlpath);
692
download.setAttribute("title",
693
"Download the selected files as a ZIP file");
696
/* Refresh - No changes required */
698
/* Publish and Submit */
699
/* If this directory is under subversion and selected/unselected file is a
701
var publish = document.getElementById("act_publish");
702
var submit = document.getElementById("act_submit");
703
var pubcond = numsel <= 1 && file.isdir;
706
/* TODO: Work out of file is svn'd */
707
/* If this dir is already published, call it "Unpublish" */
710
publish.setAttribute("value", "unpublish");
711
publish.setAttribute("title" ,"Make it so this directory "
712
+ "can not be seen by anyone on the web");
713
publish.textContent = "Unpublish";
715
publish.setAttribute("value", "publish");
716
publish.setAttribute("title","Make it so this directory "
717
+ "can be seen by anyone on the web");
718
publish.textContent = "Publish";
721
set_action_state(["publish", "submit"], pubcond);
724
/* If exactly 1 non-directory file is selected, and its parent
725
* directory is published.
727
set_action_state("share", numsel == 1 && !file.isdir &&
728
current_file.published);
731
/* If exactly 1 file is selected */
732
set_action_state("rename", numsel == 1);
734
/* Delete, cut, copy */
735
/* If >= 1 file is selected */
736
set_action_state(["delete", "cut", "copy"], numsel >= 1);
738
/* Paste, new file, new directory, upload */
739
/* Disable if the current file is not a directory */
740
set_action_state(["paste", "newfile", "mkdir", "upload"], current_file.isdir);
742
/* Subversion actions */
743
/* These are only useful if we are in a versioned directory and have some
745
set_action_state(["svnadd", "svnremove", "svnrevert", "svncommit"], numsel >= 1 && current_file.svnstatus);
747
/* Diff, log and update only support one path at the moment, so we must
748
* have 0 or 1 versioned files selected. If 0, the directory must be
750
single_versioned_path = (
752
(numsel == 1 && (svnst = file_listing[selected_files[0]].svnstatus)) ||
753
(numsel == 0 && (svnst = current_file.svnstatus))
754
) && svnst != "unversioned");
755
set_action_state(["svndiff", "svnupdate"], single_versioned_path);
757
/* We can resolve if we have a file selected and it is conflicted. */
758
set_action_state("svnresolved", single_versioned_path && numsel == 1 && svnst == "conflicted");
760
/* Log should be available for revisions as well. */
761
set_action_state("svnlog", single_versioned_path, true);
763
/* There is currently nothing on the More Actions menu of use
764
* when the current file is not a directory. Hence, just remove
766
* (This makes some of the above decisions somewhat redundant).
767
* We also take this opportunity to show the appropriate actions2
768
* bar for this path. It should either be a save or upload widget.
770
if (current_file.isdir)
772
var actions2_directory = document.getElementById("actions2_directory");
773
actions2_directory.setAttribute("style", "display: inline;");
774
var moreactions = document.getElementById("moreactions_area");
775
moreactions.setAttribute("style", "display: inline;");
779
var actions2_file = document.getElementById("actions2_file");
780
actions2_file.setAttribute("style", "display: inline;");
786
/** Event handler for when an item of the "More actions..." dropdown box is
787
* selected. Performs the selected action. */
788
function handle_moreactions()
790
var moreactions = document.getElementById("moreactions");
791
if (moreactions.value == "top")
793
var selectedaction = moreactions.value;
794
/* Reset to "More actions..." */
795
moreactions.selectedIndex = 0;
797
/* If 0 files selected, filename is the name of the current dir.
798
* If 1 file selected, filename is that file.
800
if (selected_files.length == 0)
801
filename = path_basename(current_path);
802
else if (selected_files.length == 1)
803
filename = selected_files[0];
807
/* Now handle the selected action */
808
switch(selectedaction)
811
action_publish(selected_files);
814
action_unpublish(selected_files);
817
//alert("Not yet implemented: Sharing files");
818
window.open(public_app_path(serve_app, current_path, filename), 'share')
822
alert("Not yet implemented: Submit");
825
action_rename(filename);
828
action_delete(selected_files);
831
action_copy(selected_files);
834
action_cut(selected_files);
846
show_uploadpanel(true);
849
action_add(selected_files);
852
action_remove(selected_files);
855
action_revert(selected_files);
858
window.location = path_join(app_path('diff'), current_path, selected_files[0] || '');
861
action_update(selected_files);
864
action_resolved(selected_files);
867
action_commit(selected_files);
870
window.location = path_join(app_path('svnlog'), current_path, selected_files[0] || '');
875
/** User clicks "Run" button.
876
* Do an Ajax call and print the test output.
878
function runfile(localpath)
880
if (!maybe_save('The last saved version will be run.')) return false;
882
/* Dump the entire file to the console */
883
var callback = function()
885
console_enter_line("execfile('" + localpath + "')", "block");
887
start_server(callback)
891
341
/** Called when the page loads initially.
893
function browser_init()
343
window.onload = function()
895
345
/* Navigate (internally) to the path in the URL bar.
896
346
* This causes the page to be populated with whatever is at that address,