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);
87
/* We can only safely disable the save button on the first load.
88
* Syntax highlighting changes will also get this function called.
89
* We unfortunately need the change handler added each time.
91
if (!initialise_codepress.already)
93
disable_save_if_safe();
94
initialise_codepress.already = true;
66
98
/** 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)
100
function handle_text(path, text, handler_type)
89
102
/* Create a textarea with the text in it
90
103
* (The makings of a primitive editor).
92
105
var files = document.getElementById("filesbody");
106
/* Put our UI at the top */
107
present_editorhead(files, path, handler_type);
94
109
var div = document.createElement("div");
95
110
div.style.height = '100%';
96
111
files.appendChild(div);
97
var txt_elem = document.createElement("textarea");
98
txt_elem.value = response_text.toString();
112
var txt_elem = dom_make_text_elem("textarea",
99
114
div.appendChild(txt_elem);
100
115
txt_elem.setAttribute("id", "editbox");
101
language = language_from_mime(content_type);
116
language = language_from_mime(current_file.type);
103
118
// Assume plaintext if no type can be determined.
104
119
language = language ? language : "text";
105
120
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";
122
txt_elem.className = "codepress " + language;
123
txt_elem.setAttribute("onchange", "edit_text()");
124
/* TODO: Make CSS height: 100% work */
125
txt_elem.setAttribute("rows", "35");
118
128
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();
130
/* And set a callback so we know that the editor iframe is loaded so we
131
* can set a callback so we know when to enable the save button.
132
* We also take this opportunity to disable the save button, if
133
* the browser is likely to reenable it as needed. */
134
editbox.onload = initialise_codepress
184
137
function language_from_mime(mime)
186
139
return {'text/x-python': 'python',
187
'application/x-javascript': 'javascript',
188
140
'application/javascript': 'javascript',
189
141
'text/css': 'css',
190
142
'text/plain': 'text',
191
143
'text/html': 'html',
192
'application/xml': 'html',
193
144
'application/xhtml+xml': 'html'}[mime];