1
/* IVLE - Informatics Virtual Learning Environment
2
* Copyright (C) 2007-2008 The University of Melbourne
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
* Module: Console (Client-side JavaScript)
19
* Author: Tom Conway, Matt Giuca
23
digest_constant = "hello";
29
/* Begin religious debate (tabs vs spaces) here: */
30
/* (This string will be inserted in the console when the user presses the Tab
34
/* Console DOM objects */
36
console_filler = null;
38
windowpane_mode = false;
39
server_started = false;
41
/* Starts the console server, if it isn't already.
42
* This can be called any number of times - it only starts the one server.
43
* This is a separate step from console_init, as the server is only to be
44
* started once the first command is entered.
45
* Does not return a value. Writes to global variables
46
* server_host, server_port, server_magic.
48
function start_server()
50
if (server_started) return;
51
var xhr = ajax_call("consoleservice", "start", {}, "POST");
52
var json_text = xhr.responseText;
53
var server_info = JSON.parse(json_text);
54
server_host = server_info.host;
55
server_port = server_info.port;
56
server_magic = server_info.magic;
57
server_started = true;
60
/** Initialises the console. All apps which import console are required to
62
* Optional "windowpane" (bool), if true, will cause the console to go into
63
* "window pane" mode which will allow it to be opened and closed, and float
65
* (Defaults to closed).
67
function console_init(windowpane)
69
/* Set up the console as a floating pane */
70
console_body = document.getElementById("console_body");
71
console_filler = document.getElementById("console_filler");
74
windowpane_mode = true;
77
/* TEMP: Start the server now.
78
* Ultimately we want the server to start only when a line is typed, but
79
* it currently does it asynchronously and doesn't start in time for the
84
/** Hide the main console panel, so the console minimizes to just an input box
85
* at the page bottom. */
86
function console_minimize()
88
if (!windowpane_mode) return;
89
console_body.setAttribute("class", "windowpane minimal");
90
console_filler.setAttribute("class", "windowpane minimal");
93
/** Show the main console panel, so it enlarges out to its full size.
95
function console_maximize()
97
if (!windowpane_mode) return;
98
console_body.setAttribute("class", "windowpane maximal");
99
console_filler.setAttribute("class", "windowpane maximal");
102
/* Below here imported from trunk/console/console.js
110
if (this.cursor >= 0)
116
function historyDown()
118
if (this.cursor < this.items.length)
124
function historyCurr()
126
if (this.cursor < 0 || this.cursor >= this.items.length)
130
return this.items[this.cursor];
133
function historyAdd(text)
135
this.items[this.items.length] = text;
136
this.cursor = this.items.length;
139
function historyShow()
142
if (this.cursor == -1)
146
for (var i = 0; i < this.items.length; i++)
148
if (i == this.cursor)
152
res += this.items[i].toString();
153
if (i == this.cursor)
159
if (this.cursor == this.items.length)
168
this.items = new Array();
171
this.down = historyDown;
172
this.curr = historyCurr;
173
this.add = historyAdd;
174
this.show = historyShow;
177
var hist = new History();
179
/** Send a line of text to the Python server, wait for its return, and react
180
* to its response by writing to the output box.
181
* Also maximize the console window if not already.
183
function console_enter_line(inputline)
185
/* Start the server if it hasn't already been started */
187
var digest = hex_md5(inputline + magic);
188
var args = {"host": server_host, "port": server_port,
189
"digest":digest, "text":inputline};
190
var xmlhttp = ajax_call("consoleservice", "chat", args, "POST");
192
var res = JSON.parse(xmlhttp.responseText);
193
var output = document.getElementById("console_output");
195
var pre = document.createElement("pre");
196
pre.setAttribute("class", "inputMsg");
197
pre.appendChild(document.createTextNode(inputline + "\n"));
198
output.appendChild(pre);
200
if (res.hasOwnProperty('okay'))
203
// print out the output (res.okay[0])
204
var pre = document.createElement("pre");
205
pre.setAttribute("class", "outputMsg");
206
pre.appendChild(document.createTextNode(res.okay[0]));
207
output.appendChild(pre);
208
// print out the return value (res.okay[1])
211
var pre = document.createElement("pre");
212
pre.setAttribute("class", "outputMsg");
213
pre.appendChild(document.createTextNode(res.okay[1] + "\n"));
214
output.appendChild(pre);
216
// set the prompt to >>>
217
var prompt = document.getElementById("console_prompt");
218
prompt.replaceChild(document.createTextNode(">>> "), prompt.firstChild);
220
else if (res.hasOwnProperty('exc'))
223
// print out any output that came before the error
224
if (res.exc[0].length > 0)
226
var pre = document.createElement("pre");
227
pre.setAttribute("class", "outputMsg");
228
pre.appendChild(document.createTextNode(res.exc[0]));
229
output.appendChild(pre);
232
// print out the error message (res.exc)
233
var pre = document.createElement("pre");
234
pre.setAttribute("class", "errorMsg");
235
pre.appendChild(document.createTextNode(res.exc[1]));
236
output.appendChild(pre);
238
else if (res.hasOwnProperty('more'))
240
// Need more input, so set the prompt to ...
241
var prompt = document.getElementById("console_prompt");
242
prompt.replaceChild(document.createTextNode("... "), prompt.firstChild);
245
// assert res.hasOwnProperty('input')
246
var prompt = document.getElementById("console_prompt");
247
prompt.replaceChild(document.createTextNode("+++ "), prompt.firstChild);
249
/* Open up the console so we can see the output */
253
function catch_input(key)
255
var inp = document.getElementById('console_inputText');
258
case 9: /* Tab key */
259
var selstart = inp.selectionStart;
260
var selend = inp.selectionEnd;
261
if (selstart == selend)
263
/* No selection, just a carat. Insert a tab here. */
264
inp.value = inp.value.substr(0, selstart)
265
+ TAB_STRING + inp.value.substr(selstart);
269
/* Text is selected. Just indent the whole line
270
* by inserting a tab at the start */
271
inp.value = TAB_STRING + inp.value;
273
/* Update the selection so the same characters as before are selected
275
inp.selectionStart = selstart + TAB_STRING.length;
276
inp.selectionEnd = inp.selectionStart + (selend - selstart);
277
/* Cancel the event, so the TAB key doesn't move focus away from this
280
/* Note: If it happens that some browsers don't support event
281
* cancelling properly, this hack might work instead:
283
"document.getElementById('console_inputText').focus()",
287
case 13: /* Enter key */
288
/* Send the line of text to the server */
289
console_enter_line(inp.value);
291
inp.value = hist.curr();
293
case 38: /* Up arrow */
295
inp.value = hist.curr();
297
case 40: /* Down arrow */
299
inp.value = hist.curr();