~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/webapp/console/media/console.js

  • Committer: Nick Chadwick
  • Date: 2009-02-24 05:22:21 UTC
  • mto: (1099.1.227 exercise-ui)
  • mto: This revision was merged to the branch mainline in revision 1162.
  • Revision ID: chadnickbok@gmail.com-20090224052221-xws3fir8gg46ovvz
Modifief exercise view, so that exercises can now be viewed in an
expandable javascript style.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* IVLE - Informatics Virtual Learning Environment
2
 
 * Copyright (C) 2007-2008 The University of Melbourne
3
 
 *
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.
8
 
 *
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.
13
 
 *
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
17
 
 *
18
 
 * Module: Console (Client-side JavaScript)
19
 
 * Author: Tom Conway, Matt Giuca
20
 
 * Date: 30/1/2008
21
 
 */
22
 
 
23
 
var server_key;
24
 
 
25
 
/* Begin religious debate (tabs vs spaces) here: */
26
 
/* (This string will be inserted in the console when the user presses the Tab
27
 
 * key) */
28
 
TAB_STRING = "    ";
29
 
 
30
 
/* Console DOM objects */
31
 
console_body = null;
32
 
console_filler = null;
33
 
 
34
 
windowpane_mode = false;
35
 
server_started = false;
36
 
 
37
 
interrupted = false;
38
 
 
39
 
 
40
 
function get_console_start_directory()
41
 
{
42
 
    if((typeof(current_path) != 'undefined') && current_file)
43
 
    {
44
 
        // We have a current_path - give a suggestion to the server
45
 
        var path;
46
 
        if (current_file.isdir)
47
 
        {
48
 
            // Browser
49
 
            return path_join("/home", current_path);
50
 
        }
51
 
        else
52
 
        {
53
 
            // Editor - need to chop off filename
54
 
            var tmp_path = current_path.split('/');
55
 
            tmp_path.pop();
56
 
            return path_join("/home", tmp_path.join('/'));
57
 
        }
58
 
    }
59
 
    else
60
 
    {
61
 
        // No current_path - let the server decide
62
 
        return '';
63
 
    }
64
 
}
65
 
 
66
 
/* Starts the console server, if it isn't already.
67
 
 * This can be called any number of times - it only starts the one server.
68
 
 * Note that this is asynchronous. It will return after signalling to start
69
 
 * the server, but there is no guarantee that it has been started yet.
70
 
 * This is a separate step from console_init, as the server is only to be
71
 
 * started once the first command is entered.
72
 
 * Does not return a value. Writes to global variables
73
 
 * server_host, and server_port.
74
 
 *
75
 
 * \param callback Function which will be called after the server has been
76
 
 * started. No parameters are passed. May be null.
77
 
 */
78
 
function start_server(callback)
79
 
{
80
 
    if (server_started)
81
 
    {
82
 
        callback();
83
 
        return;
84
 
    }
85
 
    var callback1 = function(xhr)
86
 
        {
87
 
            var json_text = xhr.responseText;
88
 
            try
89
 
            {
90
 
                server_key = JSON.parse(json_text).key;
91
 
                server_started = true;
92
 
                if (callback != null)
93
 
                    callback();
94
 
            }
95
 
            catch (e)
96
 
            {
97
 
                alert("An error occured when starting the IVLE console. " +
98
 
                    "Please refresh the page and try again.\n" +
99
 
                    "Details have been logged for further examination.")
100
 
            }
101
 
        }
102
 
 
103
 
    ajax_call(
104
 
        callback1, "console", "service",
105
 
        {"ivle.op": "start", "cwd": get_console_start_directory()}, 
106
 
        "POST");
107
 
}
108
 
 
109
 
/** Initialises the console. All apps which import console are required to
110
 
 * call this function.
111
 
 * Optional "windowpane" (bool), if true, will cause the console to go into
112
 
 * "window pane" mode which will allow it to be opened and closed, and float
113
 
 * over the page.
114
 
 * (Defaults to closed).
115
 
 */
116
 
function console_init(windowpane)
117
 
{
118
 
    /* Set up the console as a floating pane */
119
 
    console_body = document.getElementById("console_body");
120
 
    /* If there is no console body, don't worry.
121
 
     * (This lets us import console.js even on pages without a console box */
122
 
    if (console_body == null) return;
123
 
    console_filler = document.getElementById("console_filler");
124
 
    if (windowpane)
125
 
    {
126
 
        windowpane_mode = true;
127
 
        console_minimize();
128
 
    }
129
 
}
130
 
 
131
 
/** Hide the main console panel, so the console minimizes to just an input box
132
 
 *  at the page bottom. */
133
 
function console_minimize()
134
 
{
135
 
    if (!windowpane_mode) return;
136
 
    console_body.setAttribute("class", "windowpane minimal");
137
 
    console_filler.setAttribute("class", "windowpane minimal");
138
 
}
139
 
 
140
 
/** Show the main console panel, so it enlarges out to its full size.
141
 
 */
142
 
function console_maximize()
143
 
{
144
 
    if (!windowpane_mode) return;
145
 
    console_body.setAttribute("class", "windowpane maximal");
146
 
    console_filler.setAttribute("class", "windowpane maximal");
147
 
}
148
 
 
149
 
/* current_text is the string currently on the command line.
150
 
 * If non-empty, it will be stored at the bottom of the history.
151
 
 */
152
 
function historyUp(current_text)
153
 
{
154
 
    /* Remember the changes made to this item */
155
 
    this.edited[this.cursor] = current_text;
156
 
    if (this.cursor > 0)
157
 
    {
158
 
        this.cursor--;
159
 
    }
160
 
    this.earliestCursor = this.cursor;
161
 
}
162
 
 
163
 
function historyDown(current_text)
164
 
{
165
 
    /* Remember the changes made to this item */
166
 
    this.edited[this.cursor] = current_text;
167
 
    if (this.cursor < this.items.length - 1)
168
 
    {
169
 
        this.cursor++;
170
 
    }
171
 
}
172
 
 
173
 
function historyCurr()
174
 
{
175
 
    return this.edited[this.cursor];
176
 
}
177
 
 
178
 
function historySubmit(text)
179
 
{
180
 
    /* Copy the selected item's "edited" version over the permanent version of
181
 
     * the last item. */
182
 
    this.items[this.items.length-1] = text;
183
 
    /* Add a new blank item */
184
 
    this.items[this.items.length] = "";
185
 
    this.cursor = this.items.length-1;
186
 
    /* Blow away all the edited versions, replacing them with the existing
187
 
     * items set.
188
 
     * Not the whole history - just start from the earliest edited one.
189
 
     * (This avoids slowdown over extended usage time).
190
 
     */
191
 
    for (var i=this.earliestCursor; i<=this.cursor; i++)
192
 
        this.edited[i] = this.items[i];
193
 
    this.earliestCursor = this.cursor;
194
 
}
195
 
 
196
 
function historyShow()
197
 
{
198
 
    var res = "";
199
 
    for (var i = 0; i < this.items.length; i++)
200
 
    {
201
 
        if (i == this.cursor)
202
 
        {
203
 
            res += "["
204
 
        }
205
 
        res += this.items[i].toString();
206
 
        if (i == this.cursor)
207
 
        {
208
 
            res += "]"
209
 
        }
210
 
        res += " "
211
 
    }
212
 
    if (this.cursor == this.items.length)
213
 
    {
214
 
        res += "[]";
215
 
    }
216
 
    return res;
217
 
}
218
 
 
219
 
/* How history works
220
 
 * This is a fairly complex mechanism due to complications when editing
221
 
 * history items. We store two arrays. "items" is the permanent history of
222
 
 * each item. "edited" is a "volatile" version of items - the edits made to
223
 
 * the history between now and last time you hit "enter".
224
 
 * This is because the user can go back and edit any of the previous items,
225
 
 * and the edits are remembered until they hit enter.
226
 
 *
227
 
 * When hitting enter, the "edited" version of the currently selected item
228
 
 * replaces the "item" version of the last item in the list.
229
 
 * Then a new blank item is created, for the new line of input.
230
 
 * Lastly, all the "edited" versions are replaced with their stable versions.
231
 
 *
232
 
 * Cursor never points to an invalid location.
233
 
 */
234
 
function History()
235
 
{
236
 
    this.items = new Array("");
237
 
    this.edited = new Array("");
238
 
    this.cursor = 0;
239
 
    this.earliestCursor = 0;
240
 
    this.up = historyUp;
241
 
    this.down = historyDown;
242
 
    this.curr = historyCurr;
243
 
    this.submit = historySubmit;
244
 
    this.show = historyShow;
245
 
}
246
 
 
247
 
var hist = new History();
248
 
 
249
 
function set_interrupt()
250
 
{
251
 
    interrupted = true;
252
 
}
253
 
 
254
 
function clear_output()
255
 
{
256
 
    var output = document.getElementById("console_output");
257
 
    while (output.firstChild)
258
 
    {
259
 
        output.removeChild(output.firstChild);
260
 
    }
261
 
}
262
 
 
263
 
/** Send a line of text to the Python server, wait for its return, and react
264
 
 * to its response by writing to the output box.
265
 
 * Also maximize the console window if not already.
266
 
 */
267
 
function console_enter_line(inputbox, which)
268
 
{
269
 
    interrupted = false;
270
 
 
271
 
    if (typeof(inputbox) == "string")
272
 
    {
273
 
        var inputline = inputbox + "\n";
274
 
        inputbox = null;
275
 
    }
276
 
    else
277
 
    {
278
 
        /* Disable the text box */
279
 
        inputbox.setAttribute("disabled", "disabled");
280
 
 
281
 
        var inputline = inputbox.value + "\n";
282
 
    }
283
 
    var output = document.getElementById("console_output");
284
 
    {
285
 
        // Print ">>>" span
286
 
        var span = document.createElement("span");
287
 
        span.setAttribute("class", "inputPrompt");
288
 
        span.appendChild(document.createTextNode(
289
 
              document.getElementById("console_prompt").firstChild.nodeValue)
290
 
                        );
291
 
        output.appendChild(span);
292
 
        // Print input line itself in a span
293
 
        var span = document.createElement("span");
294
 
        span.setAttribute("class", "inputMsg");
295
 
        span.appendChild(document.createTextNode(inputline));
296
 
        output.appendChild(span);
297
 
    }
298
 
    var args = {
299
 
        "ivle.op": "chat", "kind": which, "key": server_key,
300
 
        "text": inputline, "cwd": get_console_start_directory()
301
 
        };
302
 
    var callback = function(xhr)
303
 
        {
304
 
            console_response(inputbox, inputline, xhr.responseText);
305
 
        }
306
 
    ajax_call(callback, "console", "service", args, "POST");
307
 
}
308
 
 
309
 
function console_response(inputbox, inputline, responseText)
310
 
{
311
 
    try
312
 
    {
313
 
        var res = JSON.parse(responseText);
314
 
    }
315
 
    catch (e)
316
 
    {
317
 
        alert("An internal error occurred in the python console.");
318
 
        return;
319
 
    }
320
 
    var output = document.getElementById("console_output");
321
 
    if (res.hasOwnProperty('okay'))
322
 
    {
323
 
        // Success!
324
 
        if (res.okay)
325
 
        {
326
 
            output.appendChild(document.createTextNode(res.okay + "\n"));
327
 
            output.appendChild(span);
328
 
        }
329
 
        // set the prompt to >>>
330
 
        set_prompt(">>>");
331
 
    }
332
 
    else if (res.hasOwnProperty('exc'))
333
 
    {
334
 
        // Failure!
335
 
        // print out the error message (res.exc)
336
 
        print_error(res.exc);
337
 
        
338
 
        // set the prompt to >>>
339
 
        set_prompt(">>>");
340
 
    }
341
 
    else if (res.hasOwnProperty('restart') && res.hasOwnProperty('key'))
342
 
    {
343
 
        // Server has indicated that the console should be restarted
344
 
        
345
 
        // Get the new key (host, port, magic)
346
 
        server_key = res.key;
347
 
 
348
 
        // Print a reason to explain why we'd do such a horrible thing
349
 
        // (console timeout, server error etc.)
350
 
        print_error("Console Restart: " + res.restart);
351
 
        
352
 
        // set the prompt to >>>
353
 
        set_prompt(">>>");
354
 
    }
355
 
    else if (res.hasOwnProperty('more'))
356
 
    {
357
 
        // Need more input, so set the prompt to ...
358
 
        set_prompt("...");
359
 
    }
360
 
    else if (res.hasOwnProperty('output'))
361
 
    {
362
 
        if (res.output.length > 0)
363
 
        {
364
 
            output.appendChild(document.createTextNode(res.output));
365
 
        }
366
 
        var callback = function(xhr)
367
 
            {
368
 
                console_response(inputbox, null, xhr.responseText);
369
 
            }
370
 
        if (interrupted)
371
 
        {
372
 
            var kind = "interrupt";
373
 
        }
374
 
        else
375
 
        {
376
 
            var kind = "chat";
377
 
        }
378
 
        var args = {
379
 
            "ivle.op": "chat", "kind": kind, "key": server_key,
380
 
            "text": '', "cwd": get_console_start_directory()
381
 
            };
382
 
        ajax_call(callback, "console", "service", args, "POST");
383
 
 
384
 
        // Open up the console so we can see the output
385
 
        // FIXME: do we need to maximize here?
386
 
        console_maximize();
387
 
 
388
 
        /* Auto-scrolling */
389
 
        divScroll.activeScroll();
390
 
 
391
 
        // Return early, so we don't re-enable the input box.
392
 
        return;
393
 
    }
394
 
    else
395
 
    {
396
 
        // assert res.hasOwnProperty('input')
397
 
        set_prompt("...");
398
 
    }
399
 
 
400
 
    if (inputbox != null)
401
 
    {
402
 
        /* Re-enable the text box */
403
 
        inputbox.removeAttribute("disabled");
404
 
        interrupted = false;
405
 
    }
406
 
 
407
 
    /* Open up the console so we can see the output */
408
 
    console_maximize();
409
 
    /* Auto-scrolling */
410
 
    divScroll.activeScroll();
411
 
 
412
 
    // Focus the input box by default
413
 
    document.getElementById("console_output").focus();
414
 
    document.getElementById("console_inputText").focus();
415
 
}
416
 
 
417
 
function catch_input(key)
418
 
{
419
 
    var inp = document.getElementById('console_inputText');
420
 
    switch (key)
421
 
    {
422
 
    case 9:                 /* Tab key */
423
 
        var selstart = inp.selectionStart;
424
 
        var selend = inp.selectionEnd;
425
 
        if (selstart == selend)
426
 
        {
427
 
            /* No selection, just a carat. Insert a tab here. */
428
 
            inp.value = inp.value.substr(0, selstart)
429
 
                + TAB_STRING + inp.value.substr(selstart);
430
 
        }
431
 
        else
432
 
        {
433
 
            /* Text is selected. Just indent the whole line
434
 
             * by inserting a tab at the start */
435
 
            inp.value = TAB_STRING + inp.value;
436
 
        }
437
 
        /* Update the selection so the same characters as before are selected
438
 
         */
439
 
        inp.selectionStart = selstart + TAB_STRING.length;
440
 
        inp.selectionEnd = inp.selectionStart + (selend - selstart);
441
 
        /* Cancel the event, so the TAB key doesn't move focus away from this
442
 
         * box */
443
 
        return false;
444
 
        /* Note: If it happens that some browsers don't support event
445
 
         * cancelling properly, this hack might work instead:
446
 
        setTimeout(
447
 
            "document.getElementById('console_inputText').focus()",
448
 
            0);
449
 
         */
450
 
        break;
451
 
    case 13:                /* Enter key */
452
 
        var callback = function()
453
 
        {
454
 
            /* Send the line of text to the server */
455
 
            console_enter_line(inp, "chat");
456
 
            hist.submit(inp.value);
457
 
            inp.value = hist.curr();
458
 
        }
459
 
 
460
 
        /* Disable the text box. This will be redone by
461
 
         * console_enter_line, but we do it here too in case start_server
462
 
         * takes a while.
463
 
         */
464
 
        inp.setAttribute("disabled", "disabled");
465
 
        /* Start the server if it hasn't already been started */
466
 
        start_server(callback);
467
 
        break;
468
 
    case 38:                /* Up arrow */
469
 
        hist.up(inp.value);
470
 
        inp.value = hist.curr();
471
 
        /* Inhibit further responses to this event, or WebKit moves the
472
 
         * cursor to the start. */
473
 
        return false;
474
 
        break;
475
 
    case 40:                /* Down arrow */
476
 
        hist.down(inp.value);
477
 
        inp.value = hist.curr();
478
 
        return false;
479
 
        break;
480
 
    }
481
 
}
482
 
 
483
 
/** Resets the console by signalling the old console to expire and starting a 
484
 
 * new one.
485
 
 */
486
 
function console_reset()
487
 
{
488
 
    // FIXME: We show some feedback here - either disable input or at very 
489
 
    // least the reset button.
490
 
 
491
 
    // Restart the console
492
 
    if(!server_started)
493
 
    {
494
 
        start_server(null);
495
 
    }
496
 
    else
497
 
    {
498
 
        xhr = ajax_call(null, "console", "service", {"ivle.op": "chat", "kind": "terminate", "key": server_key, "cwd": get_console_start_directory()}, "POST");
499
 
        console_response(null, null, xhr.responseText);
500
 
    }
501
 
}
502
 
 
503
 
/** Prints an error line in the console **/
504
 
function print_error(error)
505
 
506
 
    var output = document.getElementById("console_output");
507
 
  
508
 
    // Create text block
509
 
    var span = document.createElement("span");
510
 
    span.setAttribute("class", "errorMsg");
511
 
    span.appendChild(document.createTextNode(error + "\n"));
512
 
    output.appendChild(span);
513
 
 
514
 
    // Autoscroll
515
 
    divScroll.activeScroll();
516
 
}
517
 
 
518
 
/** Sets the prompt text **/
519
 
function set_prompt(prompt_text)
520
 
{
521
 
    var prompt = document.getElementById("console_prompt");
522
 
    prompt.replaceChild(document.createTextNode(prompt_text + " "), prompt.firstChild);
523
 
}
524
 
 
525
 
/**** Following Code modified from ******************************************/
526
 
/**** http://radio.javaranch.com/pascarello/2006/08/17/1155837038219.html ***/
527
 
/****************************************************************************/
528
 
var chatscroll = new Object();
529
 
 
530
 
chatscroll.Pane = function(scrollContainerId)
531
 
{
532
 
    this.scrollContainerId = scrollContainerId;
533
 
}
534
 
 
535
 
chatscroll.Pane.prototype.activeScroll = function()
536
 
{
537
 
    var scrollDiv = document.getElementById(this.scrollContainerId);
538
 
    var currentHeight = 0;
539
 
        
540
 
    if (scrollDiv.scrollHeight > 0)
541
 
        currentHeight = scrollDiv.scrollHeight;
542
 
    else if (scrollDiv.offsetHeight > 0)
543
 
        currentHeight = scrollDiv.offsetHeight;
544
 
 
545
 
    scrollDiv.scrollTop = currentHeight;
546
 
 
547
 
    scrollDiv = null;
548
 
}
549
 
 
550
 
var divScroll = new chatscroll.Pane('console_output');