1
function disable_save_if_safe()
3
/* If this is defined, this engine supports change notification, so is able
4
* to enable the button again. Disable it for them. */
5
if(editbox.editor.addChangeHandler)
7
var savebutton = document.getElementById("save_button");
8
savebutton.disabled = true;
12
function save_file(filename)
14
data = editbox.getCode();
15
/* Do NOT refresh the page contents (causes problems for editarea and is
17
do_action("putfile", filename,
18
{"path":".", "data":data, "overwrite":"true"},
19
"multipart/form-data", true);
20
disable_save_if_safe();
23
function save_file_as(default_filename)
25
filename = prompt("Path to save to:", default_filename);
26
if (!filename) return;
28
/* The filename will be path_joined with the app name, so needs to not
29
* be absolute, lest it clobber the app name. */
30
if (filename.charAt(0) == "/") filename = filename.substring(1);
31
ajax_call(save_file_as_callback, "fileservice", filename, {}, "POST");
34
function save_file_as_callback(response)
36
if (response.status == 404 || confirm("Are you sure you want to overwrite " + filename + "?"))
42
var savebutton = document.getElementById("save_button");
43
savebutton.disabled = false;
46
/** Presents the "editor heading" inserting it into a given element at
47
* the front. Note that the save widget is handled by the Python.
49
function present_editorhead(elem, path, handler_type)
51
var div = document.getElementById("actions2");
53
/* Print a warning message if this is not actually a text file.
55
if (handler_type != "text")
57
var warn = dom_make_text_elem("p",
58
"Warning: You are editing a binary " +
59
"file, which explains any strange characters you may see. If " +
60
"you save this file, you could corrupt it.");
61
div.appendChild(warn);
65
function highlighting_changed(select)
67
editbox.edit(editbox.getCode(), select.value);
70
/** Presents the text editor.
72
function handle_text(path, text, handler_type)
74
/* Create a textarea with the text in it
75
* (The makings of a primitive editor).
77
var files = document.getElementById("filesbody");
78
/* Put our UI at the top */
79
present_editorhead(files, path, handler_type);
81
var div = document.createElement("div");
82
div.style.height = '100%';
83
files.appendChild(div);
84
var txt_elem = dom_make_text_elem("textarea",
86
div.appendChild(txt_elem);
87
txt_elem.setAttribute("id", "editbox");
88
language = language_from_mime(current_file.type);
90
// Assume plaintext if no type can be determined.
91
language = language ? language : "text";
92
document.getElementById("highlighting_select").value = language;
94
txt_elem.className = "codepress " + language;
95
txt_elem.setAttribute("onchange", "edit_text()");
96
/* TODO: Make CSS height: 100% work */
97
txt_elem.setAttribute("rows", "35");
100
/* And set a callback so we know that the editor iframe is loaded so we
101
* can set a callback so we know when to enable the save button.
102
* We also take this opportunity to disable the save button, if
103
* the browser is likely to reenable it as needed. */
104
editbox.onload = function() {editbox.addChangeHandler(edit_text);
105
disable_save_if_safe(); };
108
function language_from_mime(mime)
110
return {'text/x-python': 'python',
111
'application/javascript': 'javascript',
113
'text/plain': 'text',
115
'application/xhtml+xml': 'html'}[mime];