1
digest_constant = "hello";
7
/* Starts the console server.
8
* Returns an object with fields "host", "port", "magic" describing the
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
25
/* Begin religious debate (tabs vs spaces) here: */
26
/* (This string will be inserted in the console when the user presses the Tab
30
/* Console DOM objects */
32
console_filler = null;
34
windowpane_mode = false;
35
server_started = false;
37
/* Starts the console server, if it isn't already.
38
* This can be called any number of times - it only starts the one server.
39
* This is a separate step from console_init, as the server is only to be
40
* started once the first command is entered.
41
* Does not return a value. Writes to global variables
42
* server_host, and server_port.
11
44
function start_server()
46
if (server_started) return;
13
47
var xhr = ajax_call("consoleservice", "start", {}, "POST");
14
48
var json_text = xhr.responseText;
15
return JSON.parse(json_text);
20
/* Start the server */
21
var server_info = start_server();
22
server_host = server_info.host;
23
server_port = server_info.port;
24
server_magic = server_info.magic;
27
/* Below here imported from trunk/console/console.js
49
server_key = JSON.parse(json_text);
50
server_started = true;
53
/** Initialises the console. All apps which import console are required to
55
* Optional "windowpane" (bool), if true, will cause the console to go into
56
* "window pane" mode which will allow it to be opened and closed, and float
58
* (Defaults to closed).
60
function console_init(windowpane)
62
/* Set up the console as a floating pane */
63
console_body = document.getElementById("console_body");
64
/* If there is no console body, don't worry.
65
* (This lets us import console.js even on pages without a console box */
66
if (console_body == null) return;
67
console_filler = document.getElementById("console_filler");
70
windowpane_mode = true;
73
/* TEMP: Start the server now.
74
* Ultimately we want the server to start only when a line is typed, but
75
* it currently does it asynchronously and doesn't start in time for the
80
/** Hide the main console panel, so the console minimizes to just an input box
81
* at the page bottom. */
82
function console_minimize()
84
if (!windowpane_mode) return;
85
console_body.setAttribute("class", "windowpane minimal");
86
console_filler.setAttribute("class", "windowpane minimal");
89
/** Show the main console panel, so it enlarges out to its full size.
91
function console_maximize()
93
if (!windowpane_mode) return;
94
console_body.setAttribute("class", "windowpane maximal");
95
console_filler.setAttribute("class", "windowpane maximal");
96
/* Focus the input box by default */
97
document.getElementById("console_inputText").focus()
100
/* current_text is the string currently on the command line.
101
* If non-empty, it will be stored at the bottom of the history.
103
function historyUp(current_text)
105
/* Remember the changes made to this item */
106
this.edited[this.cursor] = current_text;
111
this.earliestCursor = this.cursor;
41
function historyDown()
114
function historyDown(current_text)
43
if (this.cursor < this.items.length)
116
/* Remember the changes made to this item */
117
this.edited[this.cursor] = current_text;
118
if (this.cursor < this.items.length - 1)
49
124
function historyCurr()
51
if (this.cursor < 0 || this.cursor >= this.items.length)
55
return this.items[this.cursor];
126
return this.edited[this.cursor];
58
function historyAdd(text)
129
function historySubmit(text)
60
this.items[this.items.length] = text;
61
this.cursor = this.items.length;
131
/* Copy the selected item's "edited" version over the permanent version of
133
this.items[this.items.length-1] = text;
134
/* Add a new blank item */
135
this.items[this.items.length] = "";
136
this.cursor = this.items.length-1;
137
/* Blow away all the edited versions, replacing them with the existing
139
* Not the whole history - just start from the earliest edited one.
140
* (This avoids slowdown over extended usage time).
142
for (var i=this.earliestCursor; i<=this.cursor; i++)
143
this.edited[i] = this.items[i];
144
this.earliestCursor = this.cursor;
64
147
function historyShow()
67
if (this.cursor == -1)
71
150
for (var i = 0; i < this.items.length; i++)
73
152
if (i == this.cursor)
171
* This is a fairly complex mechanism due to complications when editing
172
* history items. We store two arrays. "items" is the permanent history of
173
* each item. "edited" is a "volatile" version of items - the edits made to
174
* the history between now and last time you hit "enter".
175
* This is because the user can go back and edit any of the previous items,
176
* and the edits are remembered until they hit enter.
178
* When hitting enter, the "edited" version of the currently selected item
179
* replaces the "item" version of the last item in the list.
180
* Then a new blank item is created, for the new line of input.
181
* Lastly, all the "edited" versions are replaced with their stable versions.
183
* Cursor never points to an invalid location.
91
185
function History()
93
this.items = new Array();
187
this.items = new Array("");
188
this.edited = new Array("");
190
this.earliestCursor = 0;
95
191
this.up = historyUp;
96
192
this.down = historyDown;
97
193
this.curr = historyCurr;
98
this.add = historyAdd;
194
this.submit = historySubmit;
99
195
this.show = historyShow;
102
198
var hist = new History();
104
function enter_line()
200
/** Send a line of text to the Python server, wait for its return, and react
201
* to its response by writing to the output box.
202
* Also maximize the console window if not already.
204
function console_enter_line(inputline, which)
106
var inp = document.getElementById('inputText');
107
var digest = hex_md5(inp.value + magic);
108
var args = {"host": server_host, "port": server_port,
109
"digest":digest, "text":inp.value};
110
var xmlhttp = ajax_call("consoleservice", "chat", args, "POST");
206
/* Start the server if it hasn't already been started */
208
var args = {"key": server_key, "text":inputline};
209
var xmlhttp = ajax_call("consoleservice", which, args, "POST");
112
211
var res = JSON.parse(xmlhttp.responseText);
113
var output = document.getElementById("output");
212
var output = document.getElementById("console_output");
115
214
var pre = document.createElement("pre");
116
215
pre.setAttribute("class", "inputMsg");
117
pre.appendChild(document.createTextNode(inp.value + "\n"));
216
pre.appendChild(document.createTextNode(inputline + "\n"));
118
217
output.appendChild(pre);
120
219
if (res.hasOwnProperty('okay'))
134
233
output.appendChild(pre);
136
235
// set the prompt to >>>
137
var prompt = document.getElementById("prompt");
236
var prompt = document.getElementById("console_prompt");
138
237
prompt.replaceChild(document.createTextNode(">>> "), prompt.firstChild);
140
239
else if (res.hasOwnProperty('exc'))
242
// print out any output that came before the error
243
if (res.exc[0].length > 0)
245
var pre = document.createElement("pre");
246
pre.setAttribute("class", "outputMsg");
247
pre.appendChild(document.createTextNode(res.exc[0]));
248
output.appendChild(pre);
143
251
// print out the error message (res.exc)
144
252
var pre = document.createElement("pre");
145
253
pre.setAttribute("class", "errorMsg");
146
pre.appendChild(document.createTextNode(res.exc));
254
pre.appendChild(document.createTextNode(res.exc[1]));
147
255
output.appendChild(pre);
149
257
else if (res.hasOwnProperty('more'))
151
259
// Need more input, so set the prompt to ...
152
var prompt = document.getElementById("prompt");
260
var prompt = document.getElementById("console_prompt");
153
261
prompt.replaceChild(document.createTextNode("... "), prompt.firstChild);
156
264
// assert res.hasOwnProperty('input')
157
var prompt = document.getElementById("prompt");
265
var prompt = document.getElementById("console_prompt");
158
266
prompt.replaceChild(document.createTextNode("+++ "), prompt.firstChild);
268
/* Open up the console so we can see the output */
162
272
function catch_input(key)
164
var inp = document.getElementById('inputText');
169
inp.value = hist.curr();
174
inp.value = hist.curr();
179
inp.value = hist.curr();
274
var inp = document.getElementById('console_inputText');
277
case 9: /* Tab key */
278
var selstart = inp.selectionStart;
279
var selend = inp.selectionEnd;
280
if (selstart == selend)
282
/* No selection, just a carat. Insert a tab here. */
283
inp.value = inp.value.substr(0, selstart)
284
+ TAB_STRING + inp.value.substr(selstart);
288
/* Text is selected. Just indent the whole line
289
* by inserting a tab at the start */
290
inp.value = TAB_STRING + inp.value;
292
/* Update the selection so the same characters as before are selected
294
inp.selectionStart = selstart + TAB_STRING.length;
295
inp.selectionEnd = inp.selectionStart + (selend - selstart);
296
/* Cancel the event, so the TAB key doesn't move focus away from this
299
/* Note: If it happens that some browsers don't support event
300
* cancelling properly, this hack might work instead:
302
"document.getElementById('console_inputText').focus()",
306
case 13: /* Enter key */
307
/* Send the line of text to the server */
308
console_enter_line(inp.value, "chat");
309
hist.submit(inp.value);
310
inp.value = hist.curr();
312
case 38: /* Up arrow */
314
inp.value = hist.curr();
316
case 40: /* Down arrow */
317
hist.down(inp.value);
318
inp.value = hist.curr();