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;
25
/* Convert newlines to a single LF (mainly for IE's CRLFs) */
26
data = data.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
28
/* Do NOT refresh the page contents (causes problems for editarea and is
30
if (current_file.svnstatus != "revision" ||
31
confirm("You are currently viewing an older version of this file. " +
32
"Saving will overwrite the current version. " +
33
"Are you sure you want to continue?"))
35
do_action("putfile", filename,
36
{"path":".", "data":data, "overwrite":"true"},
37
"multipart/form-data");
38
disable_save_if_safe();
42
function save_file_as(default_filename)
44
filename = prompt("Path to save to:", default_filename);
45
if (!filename) return;
47
/* The filename will be path_joined with the app name, so needs to not
48
* be absolute, lest it clobber the app name. */
49
if (filename.charAt(0) == "/") filename = filename.substring(1);
50
ajax_call(save_file_as_callback, "fileservice", filename, {}, "POST");
53
function save_file_as_callback(response)
55
if (response.status == 404 || confirm("Are you sure you want to overwrite " + filename + "?"))
59
/* Return a warning to be used in window.onbeforeunload. */
60
function confirm_beforeunload() {
61
return 'If you continue, any unsaved changes to the current file will be lost.';
66
var savebutton = document.getElementById("save_button");
67
savebutton.disabled = false;
68
window.onbeforeunload = confirm_beforeunload;
71
/** Presents the "editor heading" inserting it into a given element at
72
* the front. Note that the save widget is handled by the Python.
74
function present_editorhead(elem, path, handler_type)
76
var div = document.getElementById("actions2");
78
/* Print a warning message if this is not actually a text file.
80
if (handler_type != "text")
82
var warn = dom_make_text_elem("p",
83
"Warning: You are editing a binary " +
84
"file, which explains any strange characters you may see. If " +
85
"you save this file, you could corrupt it.");
86
div.appendChild(warn);
90
function highlighting_changed(select)
92
editbox.edit(editbox.getCode(), select.value);
95
function initialise_codepress()
97
editbox.addChangeHandler(edit_text);
98
editbox.addSaveHandler(function() {document.getElementById("save_button").click()});
100
/* We can only safely disable the save button on the first load.
101
* Syntax highlighting changes will also get this function called.
102
* We unfortunately need the change handler added each time.
104
if (!initialise_codepress.already)
106
disable_save_if_safe();
107
initialise_codepress.already = true;
111
2
/** Presents the text editor.
115
6
/* Create a textarea with the text in it
116
7
* (The makings of a primitive editor).
118
11
var files = document.getElementById("filesbody");
119
/* Put our UI at the top */
120
present_editorhead(files, path, handler_type);
122
12
var div = document.createElement("div");
123
div.style.height = '100%';
124
13
files.appendChild(div);
125
var txt_elem = document.createElement("textarea");
126
txt_elem.value = text.toString();
14
div.setAttribute("class", "padding");
15
/* First, print a warning message if this is not actually a text file.
17
if (handler_type != "text")
19
var warn = dom_make_text_elem("p",
20
"Warning: You are editing a binary " +
21
"file, which explains any strange characters you may see. If " +
22
"you save this file, you could corrupt it.");
23
div.appendChild(warn);
25
var txt_elem = dom_make_text_elem("textarea",
127
27
div.appendChild(txt_elem);
128
28
txt_elem.setAttribute("id", "editbox");
129
language = language_from_mime(current_file.type);
131
// Assume plaintext if no type can be determined.
132
language = language ? language : "text";
133
document.getElementById("highlighting_select").value = language;
135
$(txt_elem).change(edit_text);
137
/* This isn't ideal, as Opera seems to fire it even for non-textual keys.
138
* But IE and WebKit don't, so this will behave properly in most browsers.
141
$(txt_elem).keypress(edit_text);
143
txt_elem.style.width = "100%";
144
txt_elem.style.height = "100%";
145
window.onbeforeunload = confirm_beforeunload;
147
/* XXX: Lord, please forgive me for browser sniffing.
148
CodePress only works properly in real Gecko at the moment,
149
so we must go to great and evil lengths to sniff it out.
150
It's by no means a complete check, but it has to support
151
more browsers than the previous situation.
152
This should be killed ASAP when we fix/replace CodePress.
154
using_codepress = (navigator.userAgent.match('Gecko') &&
155
!navigator.userAgent.match('WebKit') &&
156
!navigator.userAgent.match('KHTML') &&
157
!navigator.userAgent.match('Presto'))
161
/* This is probably real Gecko. Try to fire up CodePress.
162
* If it fails we'll have a horrible mess, so we'll hope.
164
txt_elem.className = "codepress autocomplete-off " + language;
167
/* And set a callback so we know that the editor iframe is loaded so
168
* we can set a callback so we know when to enable the save button.
169
* We also take this opportunity to disable the save button, if
170
* the browser is likely to reenable it as needed. */
171
editbox.onload = initialise_codepress;
175
/* Not using CodePress, so we can already disable the Save button. */
176
disable_save_if_safe();
180
function language_from_mime(mime)
182
return {'text/x-python': 'python',
183
'application/x-javascript': 'javascript',
184
'application/javascript': 'javascript',
186
'text/plain': 'text',
188
'application/xml': 'html',
189
'application/xhtml+xml': 'html'}[mime];
29
/* TODO: Make CSS height: 100% work */
30
txt_elem.setAttribute("rows", "20");