1
function disable_save()
1
function disable_save_if_safe()
3
var savebutton = document.getElementById("save_button");
4
savebutton.disabled = true;
5
window.onbeforeunload = null;
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;
8
13
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');
15
data = editbox.getCode();
18
16
/* Do NOT refresh the page contents (causes problems for editarea and is
20
18
if (current_file.svnstatus != "revision" ||
58
56
window.onbeforeunload = confirm_beforeunload;
59
/** Presents the "editor heading" inserting it into a given element at
60
* the front. Note that the save widget is handled by the Python.
62
function present_editorhead(elem, path, handler_type)
64
var div = document.getElementById("actions2");
66
/* Print a warning message if this is not actually a text file.
68
if (handler_type != "text")
70
var warn = dom_make_text_elem("p",
71
"Warning: You are editing a binary " +
72
"file, which explains any strange characters you may see. If " +
73
"you save this file, you could corrupt it.");
74
div.appendChild(warn);
61
78
function highlighting_changed(select)
63
codemirror_language(select.value);
80
editbox.edit(editbox.getCode(), select.value);
83
function initialise_codepress()
85
editbox.addChangeHandler(edit_text);
86
editbox.addSaveHandler(function() {document.getElementById("save_button").click()});
88
/* We can only safely disable the save button on the first load.
89
* Syntax highlighting changes will also get this function called.
90
* We unfortunately need the change handler added each time.
92
if (!initialise_codepress.already)
94
disable_save_if_safe();
95
initialise_codepress.already = true;
66
99
/** 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)
101
function handle_text(path, text, handler_type)
89
103
/* Create a textarea with the text in it
90
104
* (The makings of a primitive editor).
92
106
var files = document.getElementById("filesbody");
107
/* Put our UI at the top */
108
present_editorhead(files, path, handler_type);
94
110
var div = document.createElement("div");
95
111
div.style.height = '100%';
96
112
files.appendChild(div);
97
var txt_elem = document.createElement("textarea");
98
txt_elem.value = response_text.toString();
113
var txt_elem = dom_make_text_elem("textarea",
99
115
div.appendChild(txt_elem);
100
116
txt_elem.setAttribute("id", "editbox");
101
language = language_from_mime(content_type);
117
language = language_from_mime(current_file.type);
103
119
// Assume plaintext if no type can be determined.
104
120
language = language ? language : "text";
105
121
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";
123
txt_elem.className = "codepress autocomplete-off " + language;
124
txt_elem.setAttribute("onchange", "edit_text()");
125
/* TODO: Make CSS height: 100% work */
126
txt_elem.setAttribute("rows", "35");
118
129
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();
131
/* And set a callback so we know that the editor iframe is loaded so we
132
* can set a callback so we know when to enable the save button.
133
* We also take this opportunity to disable the save button, if
134
* the browser is likely to reenable it as needed. */
135
editbox.onload = initialise_codepress
184
138
function language_from_mime(mime)
186
140
return {'text/x-python': 'python',
187
'application/x-javascript': 'javascript',
188
141
'application/javascript': 'javascript',
189
142
'text/css': 'css',
190
143
'text/plain': 'text',