244
/* This will always return a listing, whether it is a dir or a file.
246
var listing = response.responseText;
247
/* The listing SHOULD be valid JSON text. Parse it into an object. */
250
listing = JSON.parse(listing);
251
file_listing = listing.listing; /* Global */
257
var err = document.createElement("div");
258
var p = dom_make_text_elem("p", "Error: "
259
+ "There was an unexpected server error processing "
260
+ "the selected command.");
262
p = dom_make_text_elem("p", "If the problem persists, please "
263
+ "contact the system administrator.")
265
p = document.createElement("p");
266
var refresh = document.createElement("input");
267
refresh.setAttribute("type", "button");
268
refresh.setAttribute("value", "Back to file view");
269
refresh.setAttribute("onclick", "refresh()");
270
p.appendChild(refresh);
276
var err = document.createElement("div");
277
var p = dom_make_text_elem("p", "Error: "
278
+ "There was an unexpected server error retrieving "
279
+ "the requested file or directory.");
281
p = dom_make_text_elem("p", "If the problem persists, please "
282
+ "contact the system administrator.")
288
/* Get "." out, it's special */
289
current_file = file_listing["."]; /* Global */
290
delete file_listing["."];
292
if ('revision' in listing)
294
current_revision = listing.revision;
208
297
/* Check if this is a directory listing or file contents */
209
298
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
301
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);
305
/* Need to make a 2nd ajax call, this time get the actual file
307
callback = function(response)
309
/* Read the response and set up the page accordingly */
310
handle_contents_response(path, response);
312
/* Call the server and request the listing. */
314
args = shallow_clone_object(url_args);
317
/* This time, get the contents of the file, not its metadata */
318
args['return'] = "contents";
319
ajax_call(callback, service_app, path, args, "GET");
321
update_actions(isdir);
324
function handle_contents_response(path, response)
326
/* Treat this as an ordinary file. Get the file type. */
327
var content_type = response.getResponseHeader("Content-Type");
328
var handler_type = get_handler_type(content_type);
329
would_be_handler_type = handler_type;
330
/* handler_type should now be set to either
331
* "text", "image", "audio" or "binary". */
332
switch (handler_type)
335
handle_text(path, response.responseText,
336
would_be_handler_type);
339
/* TODO: Custom image handler */
340
handle_binary(path, response.responseText);
343
/* TODO: Custom audio handler */
344
handle_binary(path, response.responseText);
352
/* Called when a form upload comes back (from an iframe).
353
* Refreshes the page.
355
function upload_callback()
357
/* This has a pretty nasty hack, which happens to work.
358
* upload_callback is set as the "onload" callback for the iframe which
359
* receives the response from the server for uploading a file.
360
* This means it gets called twice. Once when initialising the iframe, and
361
* a second time when the actual response comes back.
362
* All we want to do is call navigate to refresh the page. But we CAN'T do
363
* that on the first load or it will just go into an infinite cycle of
364
* refreshing. We need to refresh the page ONLY on the second refresh.
365
* upload_callback_count is reset to 0 just before the iframe is created.
367
upload_callback_count++;
368
if (upload_callback_count >= 2)
370
document.getElementsByName('data')[0].value = '';
284
408
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
411
/*** HANDLERS for different types of responses (such as dir listing, file,
415
* message may either be a string, or a DOM node, which will be placed inside
328
418
function handle_error(message)
331
420
var files = document.getElementById("filesbody");
332
var txt_elem = dom_make_text_elem("div", "Error: "
333
+ message.toString() + ".")
422
if (typeof(message) == "string")
424
txt_elem = dom_make_text_elem("div", "Error: "
425
+ message.toString() + ".")
429
/* Assume message is a DOM node */
430
txt_elem = document.createElement("div");
431
txt_elem.appendChild(message);
334
433
txt_elem.setAttribute("class", "padding error");
335
434
files.appendChild(txt_elem);
338
/** Presents a path list (address bar inside the page) for clicking.
437
/** Given a path, filename and optional revision, returns a URL to open that
438
* revision of that file.
340
function presentpath(path)
440
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++)
442
bits = {'path': app_path(this_app, path, filename)};
443
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("/"));
445
bits['query_string'] = 'r=' + revision;
363
dom_path.removeChild(dom_path.lastChild);
447
return build_url(bits);
366
450
/** Given a mime type, returns the path to the icon.
427
510
div.appendChild(par2);
513
/* Enable or disable actions1 moreactions actions. Takes either a single
514
* name, or an array of them.*/
515
function set_action_state(names, which, allow_on_revision)
517
if (!(names instanceof Array)) names = Array(names);
519
for (var i=0; i < names.length; i++)
521
element = document.getElementById('act_' + names[i]);
523
!(current_file.svnstatus == 'revision' && !allow_on_revision))
526
element.setAttribute("class", "choice");
527
element.removeAttribute("disabled");
532
element.setAttribute("class", "disabled");
533
element.setAttribute("disabled", "disabled");
538
function update_actions()
541
var numsel = selected_files.length;
546
/* Display information about the current directory instead */
547
filename = path_basename(current_path);
550
else if (numsel == 1)
552
filename = selected_files[0];
553
file = file_listing[filename];
556
/* Update each action node in the topbar.
557
* This includes enabling/disabling actions as appropriate, and
558
* setting href/onclick attributes. */
562
/* Available if exactly one file is selected */
563
var open = document.getElementById("act_open");
566
open.setAttribute("class", "choice");
568
open.setAttribute("title",
569
"Navigate to this directory in the file browser");
571
open.setAttribute("title",
572
"Edit or view this file");
573
open.setAttribute("href", build_revision_url(current_path, filename,
578
open.setAttribute("class", "disabled");
579
open.removeAttribute("title");
580
open.removeAttribute("href");
584
/* Available if zero or one files are selected,
585
* and only if this is a file, not a directory */
586
var serve = document.getElementById("act_serve");
587
if (numsel <= 1 && !file.isdir && current_file.svnstatus != 'revision')
589
serve.setAttribute("class", "choice");
590
serve.setAttribute("onclick",
591
"return maybe_save('The last saved version will be served.')");
593
serve.setAttribute("href",
594
app_path(serve_app, current_path));
596
serve.setAttribute("href",
597
app_path(serve_app, current_path, filename));
601
serve.setAttribute("class", "disabled");
602
serve.removeAttribute("href");
603
serve.removeAttribute("onclick");
607
/* Available if exactly one file is selected,
608
* and it is a Python file.
610
var run = document.getElementById("act_run");
612
if (!file.isdir && file.type == "text/x-python" && numsel <= 1
613
&& current_file.svnstatus != 'revision')
617
// In the edit window
618
var localpath = path_join('/home', current_path);
622
// In the browser window
623
var localpath = path_join('/home', current_path, filename);
625
run.setAttribute("class", "choice");
626
run.setAttribute("onclick", "runfile('" + localpath + "')");
630
run.setAttribute("class", "disabled");
631
run.removeAttribute("onclick");
635
/* Always available for current files.
636
* If 0 files selected, download the current file or directory as a ZIP.
637
* If 1 directory selected, download it as a ZIP.
638
* If 1 non-directory selected, download it.
639
* If >1 files selected, download them all as a ZIP.
641
var download = document.getElementById("act_download");
642
if (current_file.svnstatus == 'revision')
644
download.setAttribute("class", "disabled");
645
download.removeAttribute("onclick");
647
else if (numsel <= 1)
649
download.setAttribute("class", "choice")
652
download.setAttribute("href",
653
app_path(download_app, current_path));
655
download.setAttribute("title",
656
"Download the current directory as a ZIP file");
658
download.setAttribute("title",
659
"Download the current file");
663
download.setAttribute("href",
664
app_path(download_app, current_path, filename));
666
download.setAttribute("title",
667
"Download the selected directory as a ZIP file");
669
download.setAttribute("title",
670
"Download the selected file");
675
/* Make a query string with all the files to download */
676
var dlpath = urlencode_path(app_path(download_app, current_path)) + "?";
677
for (var i=0; i<numsel; i++)
678
dlpath += "path=" + encodeURIComponent(selected_files[i]) + "&";
679
dlpath = dlpath.substr(0, dlpath.length-1);
680
download.setAttribute("class", "choice")
681
download.setAttribute("href", dlpath);
682
download.setAttribute("title",
683
"Download the selected files as a ZIP file");
686
/* Refresh - No changes required */
688
/* Publish and Submit */
689
/* If this directory is under subversion and selected/unselected file is a
691
var publish = document.getElementById("act_publish");
692
var submit = document.getElementById("act_submit");
693
var pubcond = numsel <= 1 && file.isdir;
696
/* TODO: Work out of file is svn'd */
697
/* If this dir is already published, call it "Unpublish" */
700
publish.setAttribute("value", "unpublish");
701
publish.setAttribute("title" ,"Make it so this directory "
702
+ "can not be seen by anyone on the web");
703
publish.textContent = "Unpublish";
705
publish.setAttribute("value", "publish");
706
publish.setAttribute("title","Make it so this directory "
707
+ "can be seen by anyone on the web");
708
publish.textContent = "Publish";
711
set_action_state(["publish", "submit"], pubcond);
714
/* If exactly 1 non-directory file is selected, and its parent
715
* directory is published.
717
set_action_state("share", numsel == 1 && !file.isdir &&
718
current_file.published);
721
/* If exactly 1 file is selected */
722
set_action_state("rename", numsel == 1);
724
/* Delete, cut, copy */
725
/* If >= 1 file is selected */
726
set_action_state(["delete", "cut", "copy"], numsel >= 1);
728
/* Paste, new file, new directory, upload */
729
/* Disable if the current file is not a directory */
730
set_action_state(["paste", "newfile", "mkdir", "upload"], current_file.isdir);
732
/* Subversion actions */
733
/* These are only useful if we are in a versioned directory and have some
735
set_action_state(["svnadd", "svnremove", "svnrevert", "svncommit"], numsel >= 1 && current_file.svnstatus);
737
/* Diff, log and update only support one path at the moment, so we must
738
* have 0 or 1 versioned files selected. If 0, the directory must be
740
single_versioned_path = (
742
(numsel == 1 && (svnst = file_listing[selected_files[0]].svnstatus)) ||
743
(numsel == 0 && (svnst = current_file.svnstatus))
744
) && svnst != "unversioned");
745
set_action_state(["svndiff", "svnupdate"], single_versioned_path);
747
/* We can resolve if we have a file selected and it is conflicted. */
748
set_action_state("svnresolved", single_versioned_path && numsel == 1 && svnst == "conflicted");
750
/* Log should be available for revisions as well. */
751
set_action_state("svnlog", single_versioned_path, true);
753
/* current_path == username: We are at the top level */
754
set_action_state("svncheckout", current_path == username);
756
/* There is currently nothing on the More Actions menu of use
757
* when the current file is not a directory. Hence, just remove
759
* (This makes some of the above decisions somewhat redundant).
760
* We also take this opportunity to show the appropriate actions2
761
* bar for this path. It should either be a save or upload widget.
763
if (current_file.isdir)
765
var actions2_directory = document.getElementById("actions2_directory");
766
actions2_directory.setAttribute("style", "display: inline;");
767
var moreactions = document.getElementById("moreactions_area");
768
moreactions.setAttribute("style", "display: inline;");
772
var actions2_file = document.getElementById("actions2_file");
773
actions2_file.setAttribute("style", "display: inline;");
779
/** Event handler for when an item of the "More actions..." dropdown box is
780
* selected. Performs the selected action. */
781
function handle_moreactions()
783
var moreactions = document.getElementById("moreactions");
784
if (moreactions.value == "top")
786
var selectedaction = moreactions.value;
787
/* Reset to "More actions..." */
788
moreactions.selectedIndex = 0;
790
/* If 0 files selected, filename is the name of the current dir.
791
* If 1 file selected, filename is that file.
793
if (selected_files.length == 0)
794
filename = path_basename(current_path);
795
else if (selected_files.length == 1)
796
filename = selected_files[0];
800
/* Now handle the selected action */
801
switch(selectedaction)
804
action_publish(selected_files);
807
action_unpublish(selected_files);
810
//alert("Not yet implemented: Sharing files");
811
window.open(public_app_path(serve_app, current_path, filename), 'share')
815
alert("Not yet implemented: Submit");
818
action_rename(filename);
821
action_delete(selected_files);
824
action_copy(selected_files);
827
action_cut(selected_files);
839
show_uploadpanel(true);
842
action_add(selected_files);
845
action_remove(selected_files);
848
action_revert(selected_files);
851
window.location = path_join(app_path('diff'), current_path, selected_files[0] || '');
854
action_update(selected_files);
857
action_resolved(selected_files);
860
action_commit(selected_files);
863
window.location = path_join(app_path('svnlog'), current_path, selected_files[0] || '');
871
/** User clicks "Run" button.
872
* Do an Ajax call and print the test output.
874
function runfile(localpath)
876
if (!maybe_save('The last saved version will be run.')) return false;
878
/* Dump the entire file to the console */
879
var callback = function()
881
console_enter_line("execfile('" + localpath + "')", "block");
883
start_server(callback)
430
887
/** Called when the page loads initially.
432
window.onload = function()
889
function browser_init()
434
891
/* Navigate (internally) to the path in the URL bar.
435
892
* This causes the page to be populated with whatever is at that address,