1
function disable_save()
3
var savebutton = document.getElementById("save_button");
4
savebutton.disabled = true;
5
window.onbeforeunload = null;
8
function save_file(filename)
11
data = codemirror.getCode();
13
data = document.getElementById("editbox").value;
15
/* Convert newlines to a single LF (mainly for IE's CRLFs) */
16
data = data.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
5
filename = document.getElementById("save_filename").value;
6
data = editAreaLoader.getValue("editbox");
18
7
/* Do NOT refresh the page contents (causes problems for editarea and is
20
if (current_file.svnstatus != "revision" ||
21
confirm("You are currently viewing an older version of this file. " +
22
"Saving will overwrite the current version. " +
23
"Are you sure you want to continue?"))
25
do_action("putfile", filename,
26
{"path":".", "data":data, "overwrite":"true"},
27
"multipart/form-data");
32
function save_file_as(default_filename)
34
filename = prompt("Path to save to:", default_filename);
35
if (!filename) return;
37
/* The filename will be path_joined with the app name, so needs to not
38
* be absolute, lest it clobber the app name. */
39
if (filename.charAt(0) == "/") filename = filename.substring(1);
40
ajax_call(save_file_as_callback, "fileservice", filename, {}, "POST");
43
function save_file_as_callback(response)
45
if (response.status == 404 || confirm("Are you sure you want to overwrite " + filename + "?"))
49
/* Return a warning to be used in window.onbeforeunload. */
50
function confirm_beforeunload() {
51
return 'If you continue, any unsaved changes to the current file will be lost.';
9
do_action("putfile", filename, {"path":".", "data":data}, null, true);
10
saved_status.data = "Saved.";
54
13
function edit_text()
56
var savebutton = document.getElementById("save_button");
57
savebutton.disabled = false;
58
window.onbeforeunload = confirm_beforeunload;
15
saved_status.data = "Not saved.";
61
function highlighting_changed(select)
18
/** Presents the "editor heading" (the part with the save box)
19
* inserting it into a given element at the front.
21
function present_editorhead(elem, path, handler_type)
63
codemirror_language(select.value);
23
var div = document.createElement("div");
24
/* Insert as the head element */
25
elem.insertBefore(div, elem.firstChild)
26
div.setAttribute("id", "editorhead");
28
/* Set up minimal interface */
29
var p = dom_make_text_elem("p", "Path: ");
30
var pathname = document.createElement("input");
31
pathname.setAttribute("type", "text");
32
pathname.setAttribute("size", "30");
33
pathname.setAttribute("id", "save_filename");
34
pathname.setAttribute("value", path);
35
p.appendChild(pathname);
36
var savebutton = document.createElement("input");
37
savebutton.setAttribute("type", "button");
38
savebutton.setAttribute("value", "Save");
39
savebutton.setAttribute("onclick", "save_file()");
40
p.appendChild(savebutton);
41
var t = document.createTextNode(" ");
43
saved_status = document.createTextNode("Saved.");
44
//p.appendChild(saved_status);
47
/* Print a warning message if this is not actually a text file.
49
if (handler_type != "text")
51
var warn = dom_make_text_elem("p",
52
"Warning: You are editing a binary " +
53
"file, which explains any strange characters you may see. If " +
54
"you save this file, you could corrupt it.");
55
div.appendChild(warn);
66
59
/** Presents the text editor.
68
function handle_text(path, content_type, url_args)
70
/* Need to make a 2nd ajax call, this time get the actual file
72
callback = function(response)
74
/* Read the response and set up the page accordingly */
75
handle_text_response(path, content_type, response.responseText);
77
/* Call the server and request the listing. */
79
args = shallow_clone_object(url_args);
82
/* This time, get the contents of the file, not its metadata */
83
args['return'] = "contents";
84
ajax_call(callback, service_app, path, args, "GET");
87
function handle_text_response(path, content_type, response_text)
61
function handle_text(path, text, handler_type)
89
63
/* Create a textarea with the text in it
90
64
* (The makings of a primitive editor).
92
68
var files = document.getElementById("filesbody");
69
/* Put our UI at the top */
70
present_editorhead(files, path, handler_type);
94
72
var div = document.createElement("div");
95
div.style.height = '100%';
96
73
files.appendChild(div);
97
var txt_elem = document.createElement("textarea");
98
txt_elem.value = response_text.toString();
74
var txt_elem = dom_make_text_elem("textarea",
99
76
div.appendChild(txt_elem);
100
77
txt_elem.setAttribute("id", "editbox");
101
language = language_from_mime(content_type);
103
// Assume plaintext if no type can be determined.
104
language = language ? language : "text";
105
document.getElementById("highlighting_select").value = language;
107
$(txt_elem).change(edit_text);
109
/* This isn't ideal, as Opera seems to fire it even for non-textual keys.
110
* But IE and WebKit don't, so this will behave properly in most browsers.
113
$(txt_elem).keypress(edit_text);
115
txt_elem.style.width = "100%";
116
txt_elem.style.height = "100%";
117
txt_elem.style.padding = "0";
118
window.onbeforeunload = confirm_beforeunload;
120
/* Always use CodeMirror (unless we find a good reason not to!) */
121
using_codemirror = true;
123
if (using_codemirror)
126
using_codemirror = true;
127
codemirror = new CodeMirror.fromTextArea(txt_elem, {
128
path: codemirrorpath,
130
codemirrorpath + "/contrib/python/css/pythoncolors.css",
131
codemirrorpath + "/css/xmlcolors.css",
132
codemirrorpath + "/css/jscolors.css",
133
codemirrorpath + "/css/csscolors.css"
135
basefiles: ["js/util.js",
136
"js/stringstream.js",
142
parserfile: ["contrib/python/js/parsepython.js",
145
"js/tokenizejavascript.js",
146
"js/parsejavascript.js",
147
"js/parsehtmlmixed.js",
152
width: "auto", // Fixes issue with > 100% width
155
initCallback: function() {codemirror_language(language);},
156
saveFunction: function() {document.getElementById("save_button").click();}
161
/* Not using CodePress, so we can already disable the Save button. */
166
function codemirror_language(lang)
168
if(lang == 'python') {
169
codemirror.setParser("PythonParser")
170
} else if(lang == 'html') {
171
codemirror.setParser("HTMLMixedParser")
172
} else if(lang == 'css') {
173
codemirror.setParser("CSSParser")
174
} else if(lang == 'javascript') {
175
codemirror.setParser("JSParser")
177
codemirror.setParser("DummyParser")
181
$("#actions2_file").show();
184
function language_from_mime(mime)
186
return {'text/x-python': 'python',
187
'application/x-javascript': 'javascript',
188
'application/javascript': 'javascript',
190
'text/plain': 'text',
192
'application/xml': 'html',
193
'application/xhtml+xml': 'html'}[mime];
78
txt_elem.setAttribute("onchange", "edit_text()");
79
/* TODO: Make CSS height: 100% work */
80
txt_elem.setAttribute("rows", "35");
82
/* Load EditArea into the editbox */
86
start_highlight: true,
89
replace_tab_by_spaces: 4,