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

« back to all changes in this revision

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

  • Committer: David Coles
  • Date: 2010-08-30 03:26:13 UTC
  • Revision ID: coles.david@gmail.com-20100830032613-d14vng0jkelniu3l
python-console: Fix globals broken with new JSON library.

simplejson always returns unicode strings. cJSON would return ordinary strings 
if possible. cPickle.loads() only accepts strings. At present we use pickle 
version 0 so they should all works as ASCII strings. Higher versions of pickle 
are not plain ASCII and are likely to break this and so this should be fixed 
at some point.

Also replaced unconditional exception with one that catches Pickle errors. Not 
sure the best way to report failures of these functions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
 
37
37
interrupted = false;
38
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
 
39
66
/* Starts the console server, if it isn't already.
40
67
 * This can be called any number of times - it only starts the one server.
41
68
 * Note that this is asynchronous. It will return after signalling to start
58
85
    var callback1 = function(xhr)
59
86
        {
60
87
            var json_text = xhr.responseText;
61
 
            server_key = JSON.parse(json_text);
62
 
            server_started = true;
63
 
            if (callback != null)
64
 
                callback();
65
 
        }
66
 
 
67
 
    //var current_path;
68
 
    if(typeof(current_path) != 'undefined')
69
 
    {
70
 
        // We have a current_path - give a suggestion to the server
71
 
        var path;
72
 
        if (current_file.isdir)
73
 
        {
74
 
            // Browser
75
 
            path = path_join("/home", current_path);
76
 
        }
77
 
        else
78
 
        {
79
 
            // Editor - need to chop off filename
80
 
            var tmp_path = current_path.split('/');
81
 
            tmp_path.pop();
82
 
            path = path_join("/home", tmp_path.join('/'));
83
 
        }
84
 
        ajax_call(callback1, "consoleservice", "start", {"startdir": path}, "POST");
85
 
    }
86
 
    else
87
 
    {
88
 
        // No current_path - let the server decide
89
 
        ajax_call(callback1, "consoleservice", "start", {}, "POST");
90
 
    }
 
88
            try
 
89
            {
 
90
                server_key = JSON.parse(json_text).key;
 
91
                server_started = true;
 
92
                var args = {
 
93
                    "ivle.op": "chat", "kind": "splash", "key": server_key
 
94
                };
 
95
                var callback2 = function(xhr)
 
96
                {
 
97
                    console_response(null, null, xhr.responseText);
 
98
                    if (callback != null)
 
99
                        callback();
 
100
                };
 
101
                ajax_call(callback2, "console", "service", args, "POST");
 
102
            }
 
103
            catch (e)
 
104
            {
 
105
                alert("An error occured when starting the IVLE console. " +
 
106
                    "Please refresh the page and try again.\n" +
 
107
                    "Details have been logged for further examination.")
 
108
            }
 
109
        }
 
110
 
 
111
    $("#console_output").append(
 
112
        '<span class="console_message">IVLE console starting up...</span>\n');
 
113
    console_maximize(true);
 
114
    ajax_call(
 
115
        callback1, "console", "service",
 
116
        {"ivle.op": "start", "cwd": get_console_start_directory()}, 
 
117
        "POST");
 
118
}
 
119
 
 
120
/** Start up the console backend before the user has entered text.
 
121
 * This will disable the text input, start a backend, and enable the input
 
122
 * again.
 
123
 */
 
124
function start_server_early()
 
125
{
 
126
    var inputbox = document.getElementById("console_inputText");
 
127
    inputbox.setAttribute("disabled", "disabled");
 
128
    var callback = function(xhr)
 
129
    {
 
130
        inputbox.removeAttribute("disabled")
 
131
    }
 
132
    start_server(callback);
91
133
}
92
134
 
93
135
/** Initialises the console. All apps which import console are required to
117
159
function console_minimize()
118
160
{
119
161
    if (!windowpane_mode) return;
120
 
    console_body.setAttribute("class", "windowpane minimal");
 
162
    console_body.setAttribute("class", "console_body windowpane minimal");
121
163
    console_filler.setAttribute("class", "windowpane minimal");
122
164
}
123
165
 
124
166
/** Show the main console panel, so it enlarges out to its full size.
125
167
 */
126
 
function console_maximize()
 
168
function console_maximize(do_not_start)
127
169
{
128
170
    if (!windowpane_mode) return;
129
 
    console_body.setAttribute("class", "windowpane maximal");
 
171
    if (!do_not_start && !server_started) start_server_early();
 
172
    console_body.setAttribute("class", "console_body windowpane maximal");
130
173
    console_filler.setAttribute("class", "windowpane maximal");
131
174
}
132
175
 
252
295
{
253
296
    interrupted = false;
254
297
 
 
298
    // Open up the console so we can see the output
 
299
    console_maximize();
 
300
 
255
301
    if (typeof(inputbox) == "string")
256
302
    {
257
 
        var inputline = inputbox;
 
303
        var inputline = inputbox + "\n";
258
304
        inputbox = null;
259
 
        var graytimer = null;
260
305
    }
261
306
    else
262
307
    {
263
 
        GLOBAL_inputbox = inputbox;     /* For timer */
264
 
        var inputline = inputbox.value;
265
 
        var graytimer = setTimeout("GLOBAL_inputbox.setAttribute(\"class\", "
266
 
            + "\"disabled\");", 100);
 
308
        /* Disable the text box */
 
309
        inputbox.setAttribute("disabled", "disabled");
 
310
 
 
311
        var inputline = inputbox.value + "\n";
267
312
    }
268
313
    var output = document.getElementById("console_output");
269
314
    {
270
315
        // Print ">>>" span
271
316
        var span = document.createElement("span");
272
317
        span.setAttribute("class", "inputPrompt");
273
 
        span.appendChild(document.createTextNode(">>> "));
 
318
        span.appendChild(document.createTextNode(
 
319
              document.getElementById("console_prompt").firstChild.nodeValue)
 
320
                        );
274
321
        output.appendChild(span);
275
322
        // Print input line itself in a span
276
323
        var span = document.createElement("span");
277
324
        span.setAttribute("class", "inputMsg");
278
 
        span.appendChild(document.createTextNode(inputline + "\n"));
 
325
        span.appendChild(document.createTextNode(inputline));
279
326
        output.appendChild(span);
 
327
        divScroll.activeScroll();
280
328
    }
281
 
    var args = {"key": server_key, "text":inputline};
 
329
    var args = {
 
330
        "ivle.op": "chat", "kind": which, "key": server_key,
 
331
        "text": inputline, "cwd": get_console_start_directory()
 
332
        };
282
333
    var callback = function(xhr)
283
334
        {
284
 
            console_response(inputbox, graytimer, inputline, xhr.responseText);
 
335
            console_response(inputbox, inputline, xhr.responseText);
285
336
        }
286
 
    /* Disable the text box */
287
 
    if (inputbox != null)
288
 
        inputbox.setAttribute("disabled", "disabled");
289
 
    ajax_call(callback, "consoleservice", which, args, "POST");
 
337
    ajax_call(callback, "console", "service", args, "POST");
290
338
}
291
339
 
292
 
function console_response(inputbox, graytimer, inputline, responseText)
 
340
function console_response(inputbox, inputline, responseText)
293
341
{
294
342
    try
295
343
    {
303
351
    var output = document.getElementById("console_output");
304
352
    if (res.hasOwnProperty('okay'))
305
353
    {
 
354
        // Success!
 
355
        if (res.okay)
 
356
        {
 
357
            output.appendChild(document.createTextNode(res.okay + "\n"));
 
358
            output.appendChild(span);
 
359
            divScroll.activeScroll();
 
360
        }
306
361
        // set the prompt to >>>
307
 
        var prompt = document.getElementById("console_prompt");
308
 
        prompt.replaceChild(document.createTextNode(">>> "), prompt.firstChild);
 
362
        set_prompt(">>>");
309
363
    }
310
364
    else if (res.hasOwnProperty('exc'))
311
365
    {
312
366
        // Failure!
313
367
        // print out the error message (res.exc)
314
 
        var span = document.createElement("span");
315
 
        span.setAttribute("class", "errorMsg");
316
 
        span.appendChild(document.createTextNode(res.exc + "\n"));
317
 
        output.appendChild(span);
 
368
        print_error(res.exc);
 
369
        
318
370
        // set the prompt to >>>
319
 
        var prompt = document.getElementById("console_prompt");
320
 
        prompt.replaceChild(document.createTextNode(">>> "), prompt.firstChild);
 
371
        set_prompt(">>>");
321
372
    }
322
373
    else if (res.hasOwnProperty('restart') && res.hasOwnProperty('key'))
323
374
    {
328
379
 
329
380
        // Print a reason to explain why we'd do such a horrible thing
330
381
        // (console timeout, server error etc.)
331
 
        var span = document.createElement("span");
332
 
        span.setAttribute("class", "errorMsg");
333
 
        span.appendChild(document.createTextNode("Console Restart: " + res.restart + "\n"));
334
 
        output.appendChild(span);
 
382
        print_error(
 
383
            "IVLE console restarted: " + res.restart, "console_message");
 
384
        
335
385
        // set the prompt to >>>
336
 
        var prompt = document.getElementById("console_prompt");
337
 
        prompt.replaceChild(document.createTextNode(">>> "), prompt.firstChild);
338
 
 
 
386
        set_prompt(">>>");
339
387
    }
340
388
    else if (res.hasOwnProperty('more'))
341
389
    {
342
390
        // Need more input, so set the prompt to ...
343
 
        var prompt = document.getElementById("console_prompt");
344
 
        prompt.replaceChild(document.createTextNode("... "), prompt.firstChild);
 
391
        set_prompt("...");
345
392
    }
346
393
    else if (res.hasOwnProperty('output'))
347
394
    {
348
395
        if (res.output.length > 0)
349
396
        {
350
397
            output.appendChild(document.createTextNode(res.output));
 
398
            divScroll.activeScroll();
351
399
        }
352
400
        var callback = function(xhr)
353
401
            {
354
 
                console_response(inputbox, graytimer,
355
 
                                 null, xhr.responseText);
 
402
                console_response(inputbox, null, xhr.responseText);
356
403
            }
357
404
        if (interrupted)
358
405
        {
362
409
        {
363
410
            var kind = "chat";
364
411
        }
365
 
        var args = {"key": server_key, "text":''};
366
 
        ajax_call(callback, "consoleservice", kind, args, "POST");
 
412
        var args = {
 
413
            "ivle.op": "chat", "kind": kind, "key": server_key,
 
414
            "text": '', "cwd": get_console_start_directory()
 
415
            };
 
416
        ajax_call(callback, "console", "service", args, "POST");
367
417
 
368
418
        // Open up the console so we can see the output
369
 
        // FIXME: do we need to maximize here?
370
419
        console_maximize();
371
420
 
372
 
        /* Auto-scrolling */
373
 
        divScroll.activeScroll();
374
 
 
375
421
        // Return early, so we don't re-enable the input box.
376
422
        return;
377
423
    }
378
 
    else {
379
 
        // assert res.hasOwnProperty('input')
380
 
        var prompt = document.getElementById("console_prompt");
381
 
        prompt.replaceChild(document.createTextNode("+++ "), prompt.firstChild);
 
424
    else if (res.hasOwnProperty('input'))
 
425
    {
 
426
        set_prompt("+++");
 
427
    }
 
428
    else
 
429
    {
 
430
        alert("An internal error occurred in the python console.");
 
431
        return;
382
432
    }
383
433
 
384
434
    if (inputbox != null)
385
435
    {
386
436
        /* Re-enable the text box */
387
 
        clearTimeout(graytimer);
388
437
        inputbox.removeAttribute("disabled");
389
 
        inputbox.removeAttribute("class");
390
438
        interrupted = false;
391
439
    }
392
440
 
393
 
    /* Open up the console so we can see the output */
394
 
    console_maximize();
395
441
    /* Auto-scrolling */
396
442
    divScroll.activeScroll();
397
443
 
398
444
    // Focus the input box by default
399
 
    document.getElementById("console_output").focus()
400
 
    document.getElementById("console_inputText").focus()
 
445
    document.getElementById("console_output").focus();
 
446
    document.getElementById("console_inputText").focus();
401
447
}
402
448
 
403
449
function catch_input(key)
442
488
            hist.submit(inp.value);
443
489
            inp.value = hist.curr();
444
490
        }
 
491
 
 
492
        /* Disable the text box. This will be redone by
 
493
         * console_enter_line, but we do it here too in case start_server
 
494
         * takes a while.
 
495
         */
 
496
        inp.setAttribute("disabled", "disabled");
445
497
        /* Start the server if it hasn't already been started */
446
498
        start_server(callback);
447
499
        break;
448
500
    case 38:                /* Up arrow */
449
501
        hist.up(inp.value);
450
502
        inp.value = hist.curr();
 
503
        /* Inhibit further responses to this event, or WebKit moves the
 
504
         * cursor to the start. */
 
505
        return false;
451
506
        break;
452
507
    case 40:                /* Down arrow */
453
508
        hist.down(inp.value);
454
509
        inp.value = hist.curr();
 
510
        return false;
455
511
        break;
456
512
    }
457
513
}
458
514
 
 
515
/** Resets the console by signalling the old console to expire and starting a 
 
516
 * new one.
 
517
 */
 
518
function console_reset()
 
519
{
 
520
    // FIXME: We show some feedback here - either disable input or at very 
 
521
    // least the reset button.
 
522
 
 
523
    // Restart the console
 
524
    if(!server_started)
 
525
    {
 
526
        start_server(null);
 
527
    }
 
528
    else
 
529
    {
 
530
        xhr = ajax_call(null, "console", "service", {"ivle.op": "chat", "kind": "terminate", "key": server_key, "cwd": get_console_start_directory()}, "POST");
 
531
        console_response(null, null, xhr.responseText);
 
532
    }
 
533
}
 
534
 
 
535
/** Prints an error line in the console **/
 
536
function print_error(error, cls)
 
537
{
 
538
    if (!cls)
 
539
        cls = "errorMsg";
 
540
 
 
541
    var output = document.getElementById("console_output");
 
542
  
 
543
    // Create text block
 
544
    var span = document.createElement("span");
 
545
    span.setAttribute("class", cls);
 
546
    span.appendChild(document.createTextNode(error + "\n"));
 
547
    output.appendChild(span);
 
548
 
 
549
    // Autoscroll
 
550
    divScroll.activeScroll();
 
551
}
 
552
 
 
553
/** Sets the prompt text **/
 
554
function set_prompt(prompt_text)
 
555
{
 
556
    var prompt = document.getElementById("console_prompt");
 
557
    prompt.replaceChild(document.createTextNode(prompt_text + " "), prompt.firstChild);
 
558
}
 
559
 
459
560
/**** Following Code modified from ******************************************/
460
561
/**** http://radio.javaranch.com/pascarello/2006/08/17/1155837038219.html ***/
461
562
/****************************************************************************/
473
574
        
474
575
    if (scrollDiv.scrollHeight > 0)
475
576
        currentHeight = scrollDiv.scrollHeight;
476
 
    else 
477
 
        if (objDiv.offsetHeight > 0)
478
 
            currentHeight = scrollDiv.offsetHeight;
 
577
    else if (scrollDiv.offsetHeight > 0)
 
578
        currentHeight = scrollDiv.offsetHeight;
479
579
 
480
580
    scrollDiv.scrollTop = currentHeight;
481
581