1
function disable_save_if_safe()
3
/* If we are using CodePress, we can only safely disable the save button
4
* (indicating that there are no changes to save) if the engine supports
5
* change notification, so the button can be enabled again.
7
* Our non-CodePress mode just uses normal textarea events, so is always
10
if((!using_codepress) || editbox.editor.addChangeHandler)
12
var savebutton = document.getElementById("save_button");
13
savebutton.disabled = true;
14
window.onbeforeunload = null;
18
function save_file(filename)
21
data = editbox.getCode();
23
data = document.getElementById("editbox").value;
3
var savebutton = document.getElementById("save_button");
4
var filename = document.getElementById("save_filename").value;
5
data = editAreaLoader.getValue("editbox");
25
6
/* Do NOT refresh the page contents (causes problems for editarea and is
27
if (current_file.svnstatus != "revision" ||
28
confirm("You are currently viewing an older version of this file. " +
29
"Saving will overwrite the current version. " +
30
"Are you sure you want to continue?"))
32
do_action("putfile", filename,
33
{"path":".", "data":data, "overwrite":"true"},
34
"multipart/form-data");
35
disable_save_if_safe();
39
function save_file_as(default_filename)
41
filename = prompt("Path to save to:", default_filename);
42
if (!filename) return;
44
/* The filename will be path_joined with the app name, so needs to not
45
* be absolute, lest it clobber the app name. */
46
if (filename.charAt(0) == "/") filename = filename.substring(1);
47
ajax_call(save_file_as_callback, "fileservice", filename, {}, "POST");
50
function save_file_as_callback(response)
52
if (response.status == 404 || confirm("Are you sure you want to overwrite " + filename + "?"))
56
/* Return a warning to be used in window.onbeforeunload. */
57
function confirm_beforeunload() {
58
return 'If you continue, any unsaved changes to the current file will be lost.';
8
do_action("putfile", filename,
9
{"path":".", "data":data, "overwrite":"true"},
10
"multipart/form-data", true);
11
savebutton.setAttribute("value", "Saved");
12
// XXX Do not disable for now; there is a problem getting the callback
14
//savebutton.setAttribute("disabled", "disabled");
61
17
function edit_text()
63
19
var savebutton = document.getElementById("save_button");
64
savebutton.disabled = false;
65
window.onbeforeunload = confirm_beforeunload;
20
savebutton.setAttribute("value", "Save");
21
savebutton.removeAttribute("disabled");
68
/** Presents the "editor heading" inserting it into a given element at
69
* the front. Note that the save widget is handled by the Python.
24
/** Presents the "editor heading" (the part with the save box)
25
* inserting it into a given element at the front.
71
27
function present_editorhead(elem, path, handler_type)
73
29
var div = document.getElementById("actions2");
31
/* Set up minimal interface */
32
var p = dom_make_text_elem("p", "Save as: ");
33
var pathname = document.createElement("input");
34
pathname.setAttribute("type", "text");
35
pathname.setAttribute("size", "30");
36
pathname.setAttribute("id", "save_filename");
37
pathname.setAttribute("value", path);
38
p.appendChild(pathname);
39
var savebutton = document.createElement("input");
40
savebutton.setAttribute("id", "save_button");
41
savebutton.setAttribute("type", "button");
42
savebutton.setAttribute("value", "Saved");
43
// XXX Do not disable for now; there is a problem getting the callback
45
//savebutton.setAttribute("disabled", "disabled");
46
savebutton.setAttribute("onclick", "save_file()");
47
p.appendChild(savebutton);
48
var t = document.createTextNode(" ");
75
52
/* Print a warning message if this is not actually a text file.
77
54
if (handler_type != "text")
117
73
present_editorhead(files, path, handler_type);
119
75
var div = document.createElement("div");
120
div.style.height = '100%';
121
76
files.appendChild(div);
122
var txt_elem = document.createElement("textarea");
123
txt_elem.value = text.toString();
77
var txt_elem = dom_make_text_elem("textarea",
124
79
div.appendChild(txt_elem);
125
80
txt_elem.setAttribute("id", "editbox");
126
language = language_from_mime(current_file.type);
128
// Assume plaintext if no type can be determined.
129
language = language ? language : "text";
130
document.getElementById("highlighting_select").value = language;
132
$(txt_elem).change(edit_text);
134
/* This isn't ideal, as Opera seems to fire it even for non-textual keys.
135
* But IE and WebKit don't, so this will behave properly in most browsers.
138
$(txt_elem).keypress(edit_text);
140
txt_elem.style.width = "100%";
141
txt_elem.style.height = "100%";
142
window.onbeforeunload = confirm_beforeunload;
144
/* XXX: Lord, please forgive me for browser sniffing.
145
CodePress only works properly in real Gecko at the moment,
146
so we must go to great and evil lengths to sniff it out.
147
It's by no means a complete check, but it has to support
148
more browsers than the previous situation.
149
This should be killed ASAP when we fix/replace CodePress.
151
using_codepress = (navigator.userAgent.match('Gecko') &&
152
!navigator.userAgent.match('WebKit') &&
153
!navigator.userAgent.match('KHTML') &&
154
!navigator.userAgent.match('Presto'))
158
/* This is probably real Gecko. Try to fire up CodePress.
159
* If it fails we'll have a horrible mess, so we'll hope.
161
txt_elem.className = "codepress autocomplete-off " + language;
164
/* And set a callback so we know that the editor iframe is loaded so
165
* we can set a callback so we know when to enable the save button.
166
* We also take this opportunity to disable the save button, if
167
* the browser is likely to reenable it as needed. */
168
editbox.onload = initialise_codepress;
172
/* Not using CodePress, so we can already disable the Save button. */
173
disable_save_if_safe();
177
function language_from_mime(mime)
179
return {'text/x-python': 'python',
180
'application/x-javascript': 'javascript',
181
'application/javascript': 'javascript',
183
'text/plain': 'text',
185
'application/xml': 'html',
186
'application/xhtml+xml': 'html'}[mime];
81
txt_elem.setAttribute("onchange", "edit_text()");
82
/* TODO: Make CSS height: 100% work */
83
txt_elem.setAttribute("rows", "35");
85
/* Load EditArea into the editbox */
89
toolbar: "search, go_to_line, |, undo, redo, |, select_font, |, syntax_selection, |, highlight, |, help",
90
start_highlight: true,
93
replace_tab_by_spaces: 4,
94
change_callback: "edit_text"