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

« back to all changes in this revision

Viewing changes to www/media/console/console.js

  • Committer: mattgiuca
  • Date: 2008-02-25 00:02:33 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:559
Major JavaScript refactor: util.ajax_call is now asynchronous, not
    synchronous.
    This required a change to the interface - now takes a callback and calls
    that, instead of waiting to return.
console.js, browser.js, tutorial.js, tos.js (all users of ajax_call):
    Reworked how they call - all calls now create a callback
    continuationy-thing, and pass that to ajax_call.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
 
37
37
/* Starts the console server, if it isn't already.
38
38
 * This can be called any number of times - it only starts the one server.
 
39
 * Note that this is asynchronous. It will return after signalling to start
 
40
 * the server, but there is no guarantee that it has been started yet.
39
41
 * This is a separate step from console_init, as the server is only to be
40
42
 * started once the first command is entered.
41
43
 * Does not return a value. Writes to global variables
42
44
 * server_host, and server_port.
 
45
 *
 
46
 * \param callback Function which will be called after the server has been
 
47
 * started. No parameters are passed. May be null.
43
48
 */
44
 
function start_server()
 
49
function start_server(callback)
45
50
{
46
 
    if (server_started) return;
47
 
    var xhr = ajax_call("consoleservice", "start", {}, "POST");
48
 
    var json_text = xhr.responseText;
49
 
    server_key = JSON.parse(json_text);
50
 
    server_started = true;
 
51
    if (server_started)
 
52
    {
 
53
        callback();
 
54
        return;
 
55
    }
 
56
    var callback1 = function(xhr)
 
57
        {
 
58
            var json_text = xhr.responseText;
 
59
            server_key = JSON.parse(json_text);
 
60
            server_started = true;
 
61
            if (callback != null)
 
62
                callback();
 
63
        }
 
64
    ajax_call(callback1, "consoleservice", "start", {}, "POST");
51
65
}
52
66
 
53
67
/** Initialises the console. All apps which import console are required to
203
217
 */
204
218
function console_enter_line(inputline, which)
205
219
{
206
 
    /* Start the server if it hasn't already been started */
207
 
    start_server();
208
220
    var args = {"key": server_key, "text":inputline};
209
 
    var xmlhttp = ajax_call("consoleservice", which, args, "POST");
 
221
    var callback = function(xhr)
 
222
        {
 
223
            console_response(inputline, xhr.responseText);
 
224
        }
 
225
    ajax_call(callback, "consoleservice", which, args, "POST");
 
226
}
210
227
 
211
 
    var res = JSON.parse(xmlhttp.responseText);
 
228
function console_response(inputline, responseText)
 
229
{
 
230
    var res = JSON.parse(responseText);
212
231
    var output = document.getElementById("console_output");
213
232
    {
214
233
        var pre = document.createElement("pre");
304
323
         */
305
324
        break;
306
325
    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();
 
326
        var callback = function()
 
327
        {
 
328
            /* Send the line of text to the server */
 
329
            console_enter_line(inp.value, "chat");
 
330
            hist.submit(inp.value);
 
331
            inp.value = hist.curr();
 
332
        }
 
333
        /* Start the server if it hasn't already been started */
 
334
        start_server(callback);
311
335
        break;
312
336
    case 38:                /* Up arrow */
313
337
        hist.up(inp.value);