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.';
5
filename = document.getElementById("save_filename").value;
6
data = document.getElementById("editbox").value;
7
do_action("putfile", filename, {"path":".", "data":data});
8
saved_status.data = "Saved.";
64
11
function edit_text()
66
var savebutton = document.getElementById("save_button");
67
savebutton.disabled = false;
68
window.onbeforeunload = confirm_beforeunload;
13
saved_status.data = "Not saved.";
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.
16
/** Presents the text editor.
74
function present_editorhead(elem, path, handler_type)
18
function handle_text(path, text, handler_type)
76
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);
78
49
/* Print a warning message if this is not actually a text file.
85
56
"you save this file, you could corrupt it.");
86
57
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
/** Presents the text editor.
113
function handle_text(path, text, handler_type)
115
/* Create a textarea with the text in it
116
* (The makings of a primitive editor).
118
var files = document.getElementById("filesbody");
119
/* Put our UI at the top */
120
present_editorhead(files, path, handler_type);
122
var div = document.createElement("div");
123
div.style.height = '100%';
124
files.appendChild(div);
125
var txt_elem = document.createElement("textarea");
126
txt_elem.value = text.toString();
59
var txt_elem = dom_make_text_elem("textarea",
127
61
div.appendChild(txt_elem);
128
62
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];
63
txt_elem.setAttribute("onchange", "edit_text()");
64
/* TODO: Make CSS height: 100% work */
65
txt_elem.setAttribute("rows", "20");