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

« back to all changes in this revision

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

  • Committer: William Grant
  • Date: 2010-02-11 05:09:56 UTC
  • Revision ID: grantw@unimelb.edu.au-20100211050956-t5i2z6b8iulxteza
Unbreak existing tests.

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).key;
62
 
            server_started = true;
63
 
            if (callback != null)
64
 
                callback();
 
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
            }
65
101
        }
66
102
 
67
 
    //var current_path;
68
 
    if((typeof(current_path) != 'undefined') && current_file)
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, "console", "service", {"ivle.op": "start", "cwd": path}, "POST");
85
 
    }
86
 
    else
87
 
    {
88
 
        // No current_path - let the server decide
89
 
        ajax_call(callback1, "console", "service", {"ivle.op": "start"}, "POST");
90
 
    }
 
103
    ajax_call(
 
104
        callback1, "console", "service",
 
105
        {"ivle.op": "start", "cwd": get_console_start_directory()}, 
 
106
        "POST");
91
107
}
92
108
 
93
109
/** Initialises the console. All apps which import console are required to
254
270
 
255
271
    if (typeof(inputbox) == "string")
256
272
    {
257
 
        var inputline = inputbox;
 
273
        var inputline = inputbox + "\n";
258
274
        inputbox = null;
259
 
        var graytimer = null;
260
275
    }
261
276
    else
262
277
    {
263
 
        GLOBAL_inputbox = inputbox;     /* For timer */
 
278
        /* Disable the text box */
 
279
        inputbox.setAttribute("disabled", "disabled");
 
280
 
264
281
        var inputline = inputbox.value + "\n";
265
 
        var graytimer = setTimeout("GLOBAL_inputbox.setAttribute(\"class\", "
266
 
            + "\"disabled\");", 100);
267
282
    }
268
283
    var output = document.getElementById("console_output");
269
284
    {
271
286
        var span = document.createElement("span");
272
287
        span.setAttribute("class", "inputPrompt");
273
288
        span.appendChild(document.createTextNode(
274
 
              document.getElementById("console_prompt").firstChild.textContent)
 
289
              document.getElementById("console_prompt").firstChild.nodeValue)
275
290
                        );
276
291
        output.appendChild(span);
277
292
        // Print input line itself in a span
280
295
        span.appendChild(document.createTextNode(inputline));
281
296
        output.appendChild(span);
282
297
    }
283
 
    var args = {"ivle.op": "chat", "kind": which, "key": server_key, "text":inputline};
 
298
    var args = {
 
299
        "ivle.op": "chat", "kind": which, "key": server_key,
 
300
        "text": inputline, "cwd": get_console_start_directory()
 
301
        };
284
302
    var callback = function(xhr)
285
303
        {
286
 
            console_response(inputbox, graytimer, inputline, xhr.responseText);
 
304
            console_response(inputbox, inputline, xhr.responseText);
287
305
        }
288
 
    /* Disable the text box */
289
 
    if (inputbox != null)
290
 
        inputbox.setAttribute("disabled", "disabled");
291
306
    ajax_call(callback, "console", "service", args, "POST");
292
307
}
293
308
 
294
 
function console_response(inputbox, graytimer, inputline, responseText)
 
309
function console_response(inputbox, inputline, responseText)
295
310
{
296
311
    try
297
312
    {
350
365
        }
351
366
        var callback = function(xhr)
352
367
            {
353
 
                console_response(inputbox, graytimer,
354
 
                                 null, xhr.responseText);
 
368
                console_response(inputbox, null, xhr.responseText);
355
369
            }
356
370
        if (interrupted)
357
371
        {
361
375
        {
362
376
            var kind = "chat";
363
377
        }
364
 
        var args = {"ivle.op": "chat", "kind": kind, "key": server_key, "text":''};
 
378
        var args = {
 
379
            "ivle.op": "chat", "kind": kind, "key": server_key,
 
380
            "text": '', "cwd": get_console_start_directory()
 
381
            };
365
382
        ajax_call(callback, "console", "service", args, "POST");
366
383
 
367
384
        // Open up the console so we can see the output
383
400
    if (inputbox != null)
384
401
    {
385
402
        /* Re-enable the text box */
386
 
        clearTimeout(graytimer);
387
403
        inputbox.removeAttribute("disabled");
388
 
        inputbox.removeAttribute("class");
389
404
        interrupted = false;
390
405
    }
391
406
 
441
456
            hist.submit(inp.value);
442
457
            inp.value = hist.curr();
443
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");
444
465
        /* Start the server if it hasn't already been started */
445
466
        start_server(callback);
446
467
        break;
447
468
    case 38:                /* Up arrow */
448
469
        hist.up(inp.value);
449
470
        inp.value = hist.curr();
 
471
        /* Inhibit further responses to this event, or WebKit moves the
 
472
         * cursor to the start. */
 
473
        return false;
450
474
        break;
451
475
    case 40:                /* Down arrow */
452
476
        hist.down(inp.value);
453
477
        inp.value = hist.curr();
 
478
        return false;
454
479
        break;
455
480
    }
456
481
}
470
495
    }
471
496
    else
472
497
    {
473
 
        xhr = ajax_call(null, "console", "service", {"ivle.op": "chat", "kind": "terminate", "key": server_key}, "POST");
474
 
        console_response(null, null, null, xhr.responseText);
 
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);
475
500
    }
476
501
}
477
502