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;
9
window.onbeforeunload = null;
13
function save_file(filename)
15
data = editbox.getCode();
16
/* Do NOT refresh the page contents (causes problems for editarea and is
18
do_action("putfile", filename,
19
{"path":".", "data":data, "overwrite":"true"},
20
"multipart/form-data", true);
21
disable_save_if_safe();
24
function save_file_as(default_filename)
26
filename = prompt("Path to save to:", default_filename);
27
if (!filename) return;
29
/* The filename will be path_joined with the app name, so needs to not
30
* be absolute, lest it clobber the app name. */
31
if (filename.charAt(0) == "/") filename = filename.substring(1);
32
ajax_call(save_file_as_callback, "fileservice", filename, {}, "POST");
35
function save_file_as_callback(response)
37
if (response.status == 404 || confirm("Are you sure you want to overwrite " + filename + "?"))
41
/* Return a warning to be used in window.onbeforeunload. */
42
function confirm_beforeunload() {
43
return 'If you continue, any unsaved changes to the current file will be lost.';
5
filename = document.getElementById("save_filename").value;
6
data = editAreaLoader.getValue("editbox");
7
do_action("putfile", filename, {"path":".", "data":data});
8
saved_status.data = "Saved.";
46
11
function edit_text()
48
var savebutton = document.getElementById("save_button");
49
savebutton.disabled = false;
50
window.onbeforeunload = confirm_beforeunload;
13
saved_status.data = "Not saved.";
53
/** Presents the "editor heading" inserting it into a given element at
54
* the front. Note that the save widget is handled by the Python.
16
/** Presents the text editor.
56
function present_editorhead(elem, path, handler_type)
18
function handle_text(path, text, handler_type)
58
var div = document.getElementById("actions2");
20
/* Create a textarea with the text in it
21
* (The makings of a primitive editor).
25
var files = document.getElementById("filesbody");
26
var div = document.createElement("div");
27
files.appendChild(div);
28
div.setAttribute("class", "padding");
30
/* Set up minimal interface */
31
var p = dom_make_text_elem("p", "Path: ");
32
var pathname = document.createElement("input");
33
pathname.setAttribute("type", "text");
34
pathname.setAttribute("size", "30");
35
pathname.setAttribute("id", "save_filename");
36
pathname.setAttribute("value", path);
37
p.appendChild(pathname);
38
var savebutton = document.createElement("input");
39
savebutton.setAttribute("type", "button");
40
savebutton.setAttribute("value", "Save");
41
savebutton.setAttribute("onclick", "save_file()");
42
p.appendChild(savebutton);
43
var t = document.createTextNode(" ");
45
saved_status = document.createTextNode("Saved.");
46
//p.appendChild(saved_status);
60
49
/* Print a warning message if this is not actually a text file.
67
56
"you save this file, you could corrupt it.");
68
57
div.appendChild(warn);
72
function highlighting_changed(select)
74
editbox.edit(editbox.getCode(), select.value);
77
function initialise_codepress()
79
editbox.addChangeHandler(edit_text);
81
/* We can only safely disable the save button on the first load.
82
* Syntax highlighting changes will also get this function called.
83
* We unfortunately need the change handler added each time.
85
if (!initialise_codepress.already)
87
disable_save_if_safe();
88
initialise_codepress.already = true;
92
/** Presents the text editor.
94
function handle_text(path, text, handler_type)
96
/* Create a textarea with the text in it
97
* (The makings of a primitive editor).
99
var files = document.getElementById("filesbody");
100
/* Put our UI at the top */
101
present_editorhead(files, path, handler_type);
103
var div = document.createElement("div");
104
div.style.height = '100%';
105
files.appendChild(div);
106
59
var txt_elem = dom_make_text_elem("textarea",
108
61
div.appendChild(txt_elem);
109
62
txt_elem.setAttribute("id", "editbox");
110
language = language_from_mime(current_file.type);
112
// Assume plaintext if no type can be determined.
113
language = language ? language : "text";
114
document.getElementById("highlighting_select").value = language;
116
txt_elem.className = "codepress " + language;
117
63
txt_elem.setAttribute("onchange", "edit_text()");
118
64
/* TODO: Make CSS height: 100% work */
119
65
txt_elem.setAttribute("rows", "35");
122
window.onbeforeunload = confirm_beforeunload;
124
/* And set a callback so we know that the editor iframe is loaded so we
125
* can set a callback so we know when to enable the save button.
126
* We also take this opportunity to disable the save button, if
127
* the browser is likely to reenable it as needed. */
128
editbox.onload = initialise_codepress
131
function language_from_mime(mime)
133
return {'text/x-python': 'python',
134
'application/javascript': 'javascript',
136
'text/plain': 'text',
138
'application/xhtml+xml': 'html'}[mime];
67
/* Load EditArea into the editbox */