243
/* This will always return a listing, whether it is a dir or a file.
245
var listing = response.responseText;
246
/* The listing SHOULD be valid JSON text. Parse it into an object. */
249
listing = JSON.parse(listing);
250
file_listing = listing.listing; /* Global */
256
var err = document.createElement("div");
257
var p = dom_make_text_elem("p", "Error: "
258
+ "There was an unexpected server error processing "
259
+ "the selected command.");
261
p = dom_make_text_elem("p", "If the problem persists, please "
262
+ "contact the system administrator.")
264
p = document.createElement("p");
265
var refresh = document.createElement("input");
266
refresh.setAttribute("type", "button");
267
refresh.setAttribute("value", "Back to file view");
268
refresh.setAttribute("onclick", "refresh()");
269
p.appendChild(refresh);
275
var err = document.createElement("div");
276
var p = dom_make_text_elem("p", "Error: "
277
+ "There was an unexpected server error retrieving "
278
+ "the requested file or directory.");
280
p = dom_make_text_elem("p", "If the problem persists, please "
281
+ "contact the system administrator.")
287
/* Get "." out, it's special */
288
current_file = file_listing["."]; /* Global */
289
delete file_listing["."];
291
if ('revision' in listing)
293
current_revision = listing.revision;
208
296
/* Check if this is a directory listing or file contents */
209
297
var isdir = response.getResponseHeader("X-IVLE-Return") == "Dir";
210
if (!editmode && isdir)
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
300
handle_dir_listing(path, listing);
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);
304
/* Need to make a 2nd ajax call, this time get the actual file
306
callback = function(response)
308
/* Read the response and set up the page accordingly */
309
handle_contents_response(path, response);
311
/* Call the server and request the listing. */
313
args = shallow_clone_object(url_args);
316
/* This time, get the contents of the file, not its metadata */
317
args['return'] = "contents";
318
ajax_call(callback, service_app, path, args, "GET");
320
update_actions(isdir);
323
function handle_contents_response(path, response)
325
/* Treat this as an ordinary file. Get the file type. */
326
var content_type = response.getResponseHeader("Content-Type");
327
var handler_type = get_handler_type(content_type);
328
would_be_handler_type = handler_type;
329
/* handler_type should now be set to either
330
* "text", "image", "audio" or "binary". */
331
switch (handler_type)
334
handle_text(path, response.responseText,
335
would_be_handler_type);
338
/* TODO: Custom image handler */
339
handle_binary(path, response.responseText);
342
/* TODO: Custom audio handler */
343
handle_binary(path, response.responseText);
351
/* Called when a form upload comes back (from an iframe).
352
* Refreshes the page.
354
function upload_callback()
356
/* This has a pretty nasty hack, which happens to work.
357
* upload_callback is set as the "onload" callback for the iframe which
358
* receives the response from the server for uploading a file.
359
* This means it gets called twice. Once when initialising the iframe, and
360
* a second time when the actual response comes back.
361
* All we want to do is call navigate to refresh the page. But we CAN'T do
362
* that on the first load or it will just go into an infinite cycle of
363
* refreshing. We need to refresh the page ONLY on the second refresh.
364
* upload_callback_count is reset to 0 just before the iframe is created.
366
upload_callback_count++;
367
if (upload_callback_count >= 2)
369
document.getElementsByName('data')[0].value = '';
284
407
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");
325
410
/*** HANDLERS for different types of responses (such as dir listing, file,
414
* message may either be a string, or a DOM node, which will be placed inside
328
417
function handle_error(message)
331
419
var files = document.getElementById("filesbody");
332
var txt_elem = dom_make_text_elem("div", "Error: "
333
+ message.toString() + ".")
421
if (typeof(message) == "string")
423
txt_elem = dom_make_text_elem("div", "Error: "
424
+ message.toString() + ".")
428
/* Assume message is a DOM node */
429
txt_elem = document.createElement("div");
430
txt_elem.appendChild(message);
334
432
txt_elem.setAttribute("class", "padding error");
335
433
files.appendChild(txt_elem);
338
/** Presents a path list (address bar inside the page) for clicking.
436
/** Given a path, filename and optional revision, returns a URL to open that
437
* revision of that file.
340
function presentpath(path)
439
function build_revision_url(path, filename, 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++)
441
bits = {'path': app_path(this_app, path, filename)};
442
if (current_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("/"));
444
bits['query_string'] = 'r=' + revision;
363
dom_path.removeChild(dom_path.lastChild);
446
return build_url(bits);
366
449
/** Given a mime type, returns the path to the icon.
427
509
div.appendChild(par2);
512
/* Enable or disable actions1 moreactions actions. Takes either a single
513
* name, or an array of them.*/
514
function set_action_state(names, which, allow_on_revision)
516
if (!(names instanceof Array)) names = Array(names);
518
for (var i=0; i < names.length; i++)
520
element = document.getElementById('act_' + names[i]);
522
!(current_file.svnstatus == 'revision' && !allow_on_revision))
525
element.setAttribute("class", "choice");
526
element.removeAttribute("disabled");
531
element.setAttribute("class", "disabled");
532
element.setAttribute("disabled", "disabled");
537
function update_actions()
540
var numsel = selected_files.length;
545
/* Display information about the current directory instead */
546
filename = path_basename(current_path);
549
else if (numsel == 1)
551
filename = selected_files[0];
552
file = file_listing[filename];
555
/* Update each action node in the topbar.
556
* This includes enabling/disabling actions as appropriate, and
557
* setting href/onclick attributes. */
561
/* Available if exactly one file is selected */
562
var open = document.getElementById("act_open");
565
open.setAttribute("class", "choice");
567
open.setAttribute("title",
568
"Navigate to this directory in the file browser");
570
open.setAttribute("title",
571
"Edit or view this file");
572
open.setAttribute("href", build_revision_url(current_path, filename,
577
open.setAttribute("class", "disabled");
578
open.removeAttribute("title");
579
open.removeAttribute("href");
583
/* Available if zero or one files are selected,
584
* and only if this is a file, not a directory */
585
var serve = document.getElementById("act_serve");
586
if (numsel <= 1 && !file.isdir && current_file.svnstatus != 'revision')
588
serve.setAttribute("class", "choice");
589
serve.setAttribute("onclick",
590
"return maybe_save('The last saved version will be served.')");
592
serve.setAttribute("href",
593
app_path(serve_app, current_path));
595
serve.setAttribute("href",
596
app_path(serve_app, current_path, filename));
600
serve.setAttribute("class", "disabled");
601
serve.removeAttribute("href");
602
serve.removeAttribute("onclick");
606
/* Available if exactly one file is selected,
607
* and it is a Python file.
609
var run = document.getElementById("act_run");
611
if (!file.isdir && file.type == "text/x-python" && numsel <= 1
612
&& current_file.svnstatus != 'revision')
616
// In the edit window
617
var localpath = path_join('/home', current_path);
621
// In the browser window
622
var localpath = path_join('/home', current_path, filename);
624
run.setAttribute("class", "choice");
625
run.setAttribute("onclick", "runfile('" + localpath + "')");
629
run.setAttribute("class", "disabled");
630
run.removeAttribute("onclick");
634
/* Always available for current files.
635
* If 0 files selected, download the current file or directory as a ZIP.
636
* If 1 directory selected, download it as a ZIP.
637
* If 1 non-directory selected, download it.
638
* If >1 files selected, download them all as a ZIP.
640
var download = document.getElementById("act_download");
641
if (current_file.svnstatus == 'revision')
643
download.setAttribute("class", "disabled");
644
download.removeAttribute("onclick");
646
else if (numsel <= 1)
648
download.setAttribute("class", "choice")
651
download.setAttribute("href",
652
app_path(download_app, current_path));
654
download.setAttribute("title",
655
"Download the current directory as a ZIP file");
657
download.setAttribute("title",
658
"Download the current file");
662
download.setAttribute("href",
663
app_path(download_app, current_path, filename));
665
download.setAttribute("title",
666
"Download the selected directory as a ZIP file");
668
download.setAttribute("title",
669
"Download the selected file");
674
/* Make a query string with all the files to download */
675
var dlpath = urlencode_path(app_path(download_app, current_path)) + "?";
676
for (var i=0; i<numsel; i++)
677
dlpath += "path=" + encodeURIComponent(selected_files[i]) + "&";
678
dlpath = dlpath.substr(0, dlpath.length-1);
679
download.setAttribute("class", "choice")
680
download.setAttribute("href", dlpath);
681
download.setAttribute("title",
682
"Download the selected files as a ZIP file");
685
/* Refresh - No changes required */
687
/* Publish and Submit */
688
/* If this directory is under subversion and selected/unselected file is a
690
var publish = document.getElementById("act_publish");
691
var submit = document.getElementById("act_submit");
692
var pubcond = numsel <= 1 && file.isdir;
695
/* TODO: Work out of file is svn'd */
696
/* If this dir is already published, call it "Unpublish" */
699
publish.setAttribute("value", "unpublish");
700
publish.setAttribute("title" ,"Make it so this directory "
701
+ "can not be seen by anyone on the web");
702
publish.textContent = "Unpublish";
704
publish.setAttribute("value", "publish");
705
publish.setAttribute("title","Make it so this directory "
706
+ "can be seen by anyone on the web");
707
publish.textContent = "Publish";
710
set_action_state(["publish", "submit"], pubcond);
713
/* If exactly 1 non-directory file is selected, and its parent
714
* directory is published.
716
set_action_state("share", numsel == 1 && !file.isdir &&
717
current_file.published);
720
/* If exactly 1 file is selected */
721
set_action_state("rename", numsel == 1);
723
/* Delete, cut, copy */
724
/* If >= 1 file is selected */
725
set_action_state(["delete", "cut", "copy"], numsel >= 1);
727
/* Paste, new file, new directory, upload */
728
/* Disable if the current file is not a directory */
729
set_action_state(["paste", "newfile", "mkdir", "upload"], current_file.isdir);
731
/* Subversion actions */
732
/* These are only useful if we are in a versioned directory and have some
734
set_action_state(["svnadd", "svnrevert", "svncommit"], numsel >= 1 && current_file.svnstatus);
736
/* Diff and log only support one path at the moment, so we must have 0 or 1
737
* versioned files selected. If 0, the directory must be versioned. */
738
single_versioned_path = (
740
(numsel == 1 && (svnst = file_listing[selected_files[0]].svnstatus)) ||
741
(numsel == 0 && (svnst = current_file.svnstatus))
742
) && svnst != "unversioned");
743
set_action_state("svndiff", single_versioned_path);
745
/* Log should be available for revisions as well. */
746
set_action_state("svnlog", single_versioned_path, true);
748
/* current_path == username: We are at the top level */
749
set_action_state("svncheckout", current_path == username);
751
/* There is currently nothing on the More Actions menu of use
752
* when the current file is not a directory. Hence, just remove
754
* (This makes some of the above decisions somewhat redundant).
755
* We also take this opportunity to show the appropriate actions2
756
* bar for this path. It should either be a save or upload widget.
758
if (current_file.isdir)
760
var actions2_directory = document.getElementById("actions2_directory");
761
actions2_directory.setAttribute("style", "display: inline;");
762
var moreactions = document.getElementById("moreactions_area");
763
moreactions.setAttribute("style", "display: inline;");
767
var actions2_file = document.getElementById("actions2_file");
768
actions2_file.setAttribute("style", "display: inline;");
774
/** Event handler for when an item of the "More actions..." dropdown box is
775
* selected. Performs the selected action. */
776
function handle_moreactions()
778
var moreactions = document.getElementById("moreactions");
779
if (moreactions.value == "top")
781
var selectedaction = moreactions.value;
782
/* Reset to "More actions..." */
783
moreactions.selectedIndex = 0;
785
/* If 0 files selected, filename is the name of the current dir.
786
* If 1 file selected, filename is that file.
788
if (selected_files.length == 0)
789
filename = path_basename(current_path);
790
else if (selected_files.length == 1)
791
filename = selected_files[0];
795
/* Now handle the selected action */
796
switch(selectedaction)
799
action_publish(selected_files);
802
action_unpublish(selected_files);
805
//alert("Not yet implemented: Sharing files");
806
window.open(public_app_path(serve_app, current_path, filename), 'share')
810
alert("Not yet implemented: Submit");
813
action_rename(filename);
816
action_remove(selected_files);
819
action_copy(selected_files);
822
action_cut(selected_files);
834
show_uploadpanel(true);
837
action_add(selected_files);
840
action_revert(selected_files);
843
window.location = path_join(app_path('diff'), current_path, selected_files[0] || '');
846
action_commit(selected_files);
849
window.location = path_join(app_path('svnlog'), current_path, selected_files[0] || '');
857
/** User clicks "Run" button.
858
* Do an Ajax call and print the test output.
860
function runfile(localpath)
862
if (!maybe_save('The last saved version will be run.')) return false;
864
/* Dump the entire file to the console */
865
var callback = function()
867
console_enter_line("execfile('" + localpath + "')", "block");
869
start_server(callback)
430
873
/** Called when the page loads initially.
432
window.onload = function()
875
function browser_init()
434
877
/* Navigate (internally) to the path in the URL bar.
435
878
* This causes the page to be populated with whatever is at that address,