242
/* This will always return a listing, whether it is a dir or a file.
244
var listing = response.responseText;
245
/* The listing SHOULD be valid JSON text. Parse it into an object. */
248
listing = JSON.parse(listing);
249
file_listing = listing.listing; /* Global */
255
var err = document.createElement("div");
256
var p = dom_make_text_elem("p", "Error: "
257
+ "There was an unexpected server error processing "
258
+ "the selected command.");
260
p = dom_make_text_elem("p", "If the problem persists, please "
261
+ "contact the system administrator.")
263
p = document.createElement("p");
264
var refresh = document.createElement("input");
265
refresh.setAttribute("type", "button");
266
refresh.setAttribute("value", "Back to file view");
267
refresh.setAttribute("onclick", "refresh()");
268
p.appendChild(refresh);
274
var err = document.createElement("div");
275
var p = dom_make_text_elem("p", "Error: "
276
+ "There was an unexpected server error retrieving "
277
+ "the requested file or directory.");
279
p = dom_make_text_elem("p", "If the problem persists, please "
280
+ "contact the system administrator.")
286
/* Get "." out, it's special */
287
current_file = file_listing["."]; /* Global */
288
delete file_listing["."];
290
209
/* Check if this is a directory listing or file contents */
291
210
var isdir = response.getResponseHeader("X-IVLE-Return") == "Dir";
211
if (!editmode && isdir)
213
var listing = response.responseText;
214
/* The listing SHOULD be valid JSON text. Parse it into an object. */
217
listing = JSON.parse(listing);
221
handle_error("The server returned an invalid directory listing");
294
224
handle_dir_listing(path, listing);
298
/* Need to make a 2nd ajax call, this time get the actual file
300
callback = function(response)
302
/* Read the response and set up the page accordingly */
303
handle_contents_response(path, response);
305
/* Call the server and request the listing. */
307
args = shallow_clone_object(url_args);
310
/* This time, get the contents of the file, not its metadata */
311
args['return'] = "contents";
312
ajax_call(callback, service_app, path, args, "GET");
314
update_actions(isdir);
317
function handle_contents_response(path, response)
319
/* Treat this as an ordinary file. Get the file type. */
320
var content_type = response.getResponseHeader("Content-Type");
321
var handler_type = get_handler_type(content_type);
322
would_be_handler_type = handler_type;
323
/* handler_type should now be set to either
324
* "text", "image", "audio" or "binary". */
325
switch (handler_type)
328
handle_text(path, response.responseText,
329
would_be_handler_type);
332
/* TODO: Custom image handler */
333
handle_binary(path, response.responseText);
336
/* TODO: Custom audio handler */
337
handle_binary(path, response.responseText);
345
/* Called when a form upload comes back (from an iframe).
346
* Refreshes the page.
348
function upload_callback()
350
/* This has a pretty nasty hack, which happens to work.
351
* upload_callback is set as the "onload" callback for the iframe which
352
* receives the response from the server for uploading a file.
353
* This means it gets called twice. Once when initialising the iframe, and
354
* a second time when the actual response comes back.
355
* All we want to do is call navigate to refresh the page. But we CAN'T do
356
* that on the first load or it will just go into an infinite cycle of
357
* refreshing. We need to refresh the page ONLY on the second refresh.
358
* upload_callback_count is reset to 0 just before the iframe is created.
360
upload_callback_count++;
361
if (upload_callback_count >= 2)
363
document.getElementsByName('data')[0].value = '';
228
/* Treat this as an ordinary file. Get the file type. */
229
var content_type = response.getResponseHeader("Content-Type");
230
var handler_type = get_handler_type(content_type);
231
/* If we're in "edit mode", always treat this file as text */
232
would_be_handler_type = handler_type;
233
if (editmode) handler_type = "text";
234
/* handler_type should now be set to either
235
* "text", "image", "audio" or "binary". */
236
switch (handler_type)
241
handle_text(path_join(path, "untitled"), "",
242
would_be_handler_type);
246
handle_text(path, response.responseText,
247
would_be_handler_type);
251
/* TODO: Custom image handler */
252
handle_binary(path, response.responseText);
255
/* TODO: Custom audio handler */
256
handle_binary(path, response.responseText);
401
285
dom_removechildren(document.getElementById("sidepanel"));
288
/** Sets the mode to either "file browser" or "text editor" mode.
289
* This modifies the window icon, and selected tab.
290
* \param editmode If True, editor mode. Else, file browser mode.
292
function setmode(editmode)
294
/* Find the DOM elements for the file browser and editor tabs */
295
var tabs = document.getElementById("apptabs");
296
var tab_files = null;
300
for (var i=0; i<tabs.childNodes.length; i++)
302
/* Find the href of the link within */
303
if (!tabs.childNodes[i].getElementsByTagName) continue;
304
a = tabs.childNodes[i].getElementsByTagName("a");
305
if (a.length == 0) continue;
306
href = a[0].getAttribute("href");
307
if (href == null) continue;
308
if (endswith(href, this_app))
309
tab_files = tabs.childNodes[i];
310
else if (endswith(href, edit_app))
311
tab_edit = tabs.childNodes[i];
316
tab_files.removeAttribute("class");
317
tab_edit.setAttribute("class", "thisapp");
321
tab_edit.removeAttribute("class");
322
tab_files.setAttribute("class", "thisapp");
404
326
/*** HANDLERS for different types of responses (such as dir listing, file,
408
* message may either be a string, or a DOM node, which will be placed inside
411
329
function handle_error(message)
413
332
var files = document.getElementById("filesbody");
415
if (typeof(message) == "string")
417
txt_elem = dom_make_text_elem("div", "Error: "
418
+ message.toString() + ".")
422
/* Assume message is a DOM node */
423
txt_elem = document.createElement("div");
424
txt_elem.appendChild(message);
333
var txt_elem = dom_make_text_elem("div", "Error: "
334
+ message.toString() + ".")
426
335
txt_elem.setAttribute("class", "padding error");
427
336
files.appendChild(txt_elem);
339
/** Presents a path list (address bar inside the page) for clicking.
341
function presentpath(path)
343
var dom_path = document.getElementById("path");
344
var href_path = make_path(this_app);
348
/* Also set the document title */
349
document.title = path_basename(path) + " - IVLE";
350
/* Create all of the paths */
351
var pathlist = path.split("/");
352
for (var i=0; i<pathlist.length; i++)
355
if (dir == "") continue;
356
/* Make an 'a' element */
357
href_path = path_join(href_path, dir);
358
nav_path = path_join(nav_path, dir);
359
var link = dom_make_link_elem("a", dir, "Navigate to " + nav_path,
360
href_path/*, "navigate(" + repr(href_path) + ")"*/);
361
dom_path.appendChild(link);
362
dom_path.appendChild(document.createTextNode("/"));
364
dom_path.removeChild(dom_path.lastChild);
430
367
/** Given a mime type, returns the path to the icon.
431
368
* \param type String, Mime type.
432
369
* \param sizelarge Boolean, optional.
490
428
div.appendChild(par2);
493
/* Enable or disable actions1 moreactions actions. Takes either a single
494
* name, or an array of them.*/
495
function set_action_state(names, which)
497
if (!(names instanceof Array)) names = Array(names);
499
for (var i=0; i < names.length; i++)
501
element = document.getElementById('act_' + names[i]);
505
element.setAttribute("class", "choice");
506
element.removeAttribute("disabled");
511
element.setAttribute("class", "disabled");
512
element.setAttribute("disabled", "disabled");
517
function update_actions()
520
var numsel = selected_files.length;
525
/* Display information about the current directory instead */
526
filename = path_basename(current_path);
529
else if (numsel == 1)
531
filename = selected_files[0];
532
file = file_listing[filename];
535
/* Update each action node in the topbar.
536
* This includes enabling/disabling actions as appropriate, and
537
* setting href/onclick attributes. */
541
/* Available if exactly one file is selected */
542
var open = document.getElementById("act_open");
545
open.setAttribute("class", "choice");
547
open.setAttribute("title",
548
"Navigate to this directory in the file browser");
550
open.setAttribute("title",
551
"Edit or view this file");
552
open.setAttribute("href", app_path(this_app, current_path, filename));
556
open.setAttribute("class", "disabled");
557
open.removeAttribute("title");
558
open.removeAttribute("href");
562
/* Available if zero or one files are selected,
563
* and only if this is a file, not a directory */
564
var serve = document.getElementById("act_serve");
565
if (numsel <= 1 && !file.isdir)
567
serve.setAttribute("class", "choice");
568
serve.setAttribute("onclick",
569
"return maybe_save('The last saved version will be served.')");
571
serve.setAttribute("href",
572
app_path(serve_app, current_path));
574
serve.setAttribute("href",
575
app_path(serve_app, current_path, filename));
579
serve.setAttribute("class", "disabled");
580
serve.removeAttribute("href");
581
serve.removeAttribute("onclick");
585
/* Available if exactly one file is selected,
586
* and it is a Python file.
588
var run = document.getElementById("act_run");
590
if (!file.isdir && file.type == "text/x-python" && numsel <= 1)
594
// In the edit window
595
var localpath = path_join('/home', current_path);
599
// In the browser window
600
var localpath = path_join('/home', current_path, filename);
602
run.setAttribute("class", "choice");
603
run.setAttribute("onclick", "runfile('" + localpath + "')");
607
run.setAttribute("class", "disabled");
608
run.removeAttribute("onclick");
613
* If 0 files selected, download the current file or directory as a ZIP.
614
* If 1 directory selected, download it as a ZIP.
615
* If 1 non-directory selected, download it.
616
* If >1 files selected, download them all as a ZIP.
618
var download = document.getElementById("act_download");
623
download.setAttribute("href",
624
app_path(download_app, current_path));
626
download.setAttribute("title",
627
"Download the current directory as a ZIP file");
629
download.setAttribute("title",
630
"Download the current file");
634
download.setAttribute("href",
635
app_path(download_app, current_path, filename));
637
download.setAttribute("title",
638
"Download the selected directory as a ZIP file");
640
download.setAttribute("title",
641
"Download the selected file");
646
/* Make a query string with all the files to download */
647
var dlpath = urlencode_path(app_path(download_app, current_path)) + "?";
648
for (var i=0; i<numsel; i++)
649
dlpath += "path=" + encodeURIComponent(selected_files[i]) + "&";
650
dlpath = dlpath.substr(0, dlpath.length-1);
651
download.setAttribute("href", dlpath);
652
download.setAttribute("title",
653
"Download the selected files as a ZIP file");
656
/* Refresh - No changes required */
658
/* Publish and Submit */
659
/* If this directory is under subversion and selected/unselected file is a
661
var publish = document.getElementById("act_publish");
662
var submit = document.getElementById("act_submit");
663
var pubcond = numsel <= 1 && file.isdir;
666
/* TODO: Work out of file is svn'd */
667
/* If this dir is already published, call it "Unpublish" */
670
publish.setAttribute("value", "unpublish");
671
publish.setAttribute("title" ,"Make it so this directory "
672
+ "can not be seen by anyone on the web");
673
publish.textContent = "Unpublish";
675
publish.setAttribute("value", "publish");
676
publish.setAttribute("title","Make it so this directory "
677
+ "can be seen by anyone on the web");
678
publish.textContent = "Publish";
681
set_action_state(["publish", "submit"], pubcond);
684
/* If exactly 1 non-directory file is selected, and its parent
685
* directory is published.
687
set_action_state("share", numsel == 1 && !file.isdir &&
688
current_file.published);
691
/* If exactly 1 file is selected */
692
set_action_state("rename", numsel == 1);
694
/* Delete, cut, copy */
695
/* If >= 1 file is selected */
696
set_action_state(["delete", "cut", "copy"], numsel >= 1);
698
/* Paste, new file, new directory, upload */
699
/* Disable if the current file is not a directory */
700
set_action_state(["paste", "newfile", "mkdir", "upload"], current_file.isdir);
702
/* Subversion actions */
703
/* These are only useful if we are in a versioned directory and have some
705
set_action_state(["svnadd", "svnrevert", "svncommit"], numsel >= 1 && current_file.svnstatus);
707
/* Diff and log only support one path at the moment. */
708
single_versioned_path = (numsel == 1 &&
709
(svnst = file_listing[selected_files[0]].svnstatus) &&
710
svnst != "unversioned");
711
set_action_state("svnlog", single_versioned_path);
712
set_action_state("svndiff", single_versioned_path && svnst != "normal");
714
/* current_path == username: We are at the top level */
715
set_action_state("svncheckout", current_path == username);
717
/* There is currently nothing on the More Actions menu of use
718
* when the current file is not a directory. Hence, just remove
720
* (This makes some of the above decisions somewhat redundant).
721
* We also take this opportunity to show the appropriate actions2
722
* bar for this path. It should either be a save or upload widget.
724
if (current_file.isdir)
726
var actions2_directory = document.getElementById("actions2_directory");
727
actions2_directory.setAttribute("style", "display: inline;");
731
var actions2_file = document.getElementById("actions2_file");
732
actions2_file.setAttribute("style", "display: inline;");
733
var moreactions = document.getElementById("moreactions_area");
734
moreactions.setAttribute("style", "display: none;");
740
/** Event handler for when an item of the "More actions..." dropdown box is
741
* selected. Performs the selected action. */
742
function handle_moreactions()
744
var moreactions = document.getElementById("moreactions");
745
if (moreactions.value == "top")
747
var selectedaction = moreactions.value;
748
/* Reset to "More actions..." */
749
moreactions.selectedIndex = 0;
751
/* If 0 files selected, filename is the name of the current dir.
752
* If 1 file selected, filename is that file.
754
if (selected_files.length == 0)
755
filename = path_basename(current_path);
756
else if (selected_files.length == 1)
757
filename = selected_files[0];
761
/* Now handle the selected action */
762
switch(selectedaction)
765
action_publish(selected_files);
768
action_unpublish(selected_files);
771
//alert("Not yet implemented: Sharing files");
772
window.open(public_app_path(serve_app, current_path, filename), 'share')
776
alert("Not yet implemented: Submit");
779
action_rename(filename);
782
action_remove(selected_files);
785
action_copy(selected_files);
788
action_cut(selected_files);
800
show_uploadpanel(true);
803
action_add(selected_files);
806
action_revert(selected_files);
809
window.location = path_join(app_path('diff'), current_path, selected_files[0]);
812
action_commit(selected_files);
815
window.location = path_join(app_path('svnlog'), current_path, selected_files[0]);
823
/** User clicks "Run" button.
824
* Do an Ajax call and print the test output.
826
function runfile(localpath)
828
if (!maybe_save('The last saved version will be run.')) return false;
830
/* Dump the entire file to the console */
831
var callback = function()
833
console_enter_line("execfile('" + localpath + "')", "block");
835
start_server(callback)
839
431
/** Called when the page loads initially.
841
function browser_init()
433
window.onload = function()
843
435
/* Navigate (internally) to the path in the URL bar.
844
436
* This causes the page to be populated with whatever is at that address,