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

« back to all changes in this revision

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

  • Committer: drtomc
  • Date: 2008-01-30 21:05:02 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:337
Make trampoline use the path canonicalization code.
Add a makefile. This is only half a solution to the build issue, but it's
a start.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 * Date: 30/1/2008
21
21
 */
22
22
 
23
 
var server_key;
 
23
digest_constant = "hello";
24
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 = "    ";
 
25
var server_host;
 
26
var server_port;
 
27
var server_magic;
29
28
 
30
29
/* Console DOM objects */
31
30
console_body = null;
32
31
console_filler = null;
33
32
 
34
 
windowpane_mode = false;
35
 
server_started = false;
36
 
 
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.
 
33
/* Starts the console server.
 
34
 * Returns an object with fields "host", "port", "magic" describing the
 
35
 * server.
43
36
 */
44
37
function start_server()
45
38
{
46
 
    if (server_started) return;
47
39
    var xhr = ajax_call("consoleservice", "start", {}, "POST");
48
40
    var json_text = xhr.responseText;
49
 
    server_key = JSON.parse(json_text);
50
 
    server_started = true;
 
41
    return JSON.parse(json_text);
51
42
}
52
43
 
53
44
/** Initialises the console. All apps which import console are required to
61
52
{
62
53
    /* Set up the console as a floating pane */
63
54
    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
55
    console_filler = document.getElementById("console_filler");
68
56
    if (windowpane)
69
 
    {
70
 
        windowpane_mode = true;
71
57
        console_minimize();
72
 
    }
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
76
 
     * first line. */
77
 
    start_server();
 
58
    /* Start the server */
 
59
    var server_info = start_server();
 
60
    server_host = server_info.host;
 
61
    server_port = server_info.port;
 
62
    server_magic = server_info.magic;
78
63
}
79
64
 
80
65
/** Hide the main console panel, so the console minimizes to just an input box
81
66
 *  at the page bottom. */
82
67
function console_minimize()
83
68
{
84
 
    if (!windowpane_mode) return;
85
69
    console_body.setAttribute("class", "windowpane minimal");
86
70
    console_filler.setAttribute("class", "windowpane minimal");
87
71
}
90
74
 */
91
75
function console_maximize()
92
76
{
93
 
    if (!windowpane_mode) return;
94
77
    console_body.setAttribute("class", "windowpane maximal");
95
78
    console_filler.setAttribute("class", "windowpane maximal");
96
 
    /* Focus the input box by default */
97
 
    document.getElementById("console_inputText").focus()
98
79
}
99
80
 
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.
 
81
/* Below here imported from trunk/console/console.js
 
82
 * (Tom Conway)
102
83
 */
103
 
function historyUp(current_text)
 
84
 
 
85
var magic = 'xyzzy';
 
86
 
 
87
function historyUp()
104
88
{
105
 
    /* Remember the changes made to this item */
106
 
    this.edited[this.cursor] = current_text;
107
 
    if (this.cursor > 0)
 
89
    if (this.cursor >= 0)
108
90
    {
109
91
        this.cursor--;
110
92
    }
111
 
    this.earliestCursor = this.cursor;
112
93
}
113
94
 
114
 
function historyDown(current_text)
 
95
function historyDown()
115
96
{
116
 
    /* Remember the changes made to this item */
117
 
    this.edited[this.cursor] = current_text;
118
 
    if (this.cursor < this.items.length - 1)
 
97
    if (this.cursor < this.items.length)
119
98
    {
120
99
        this.cursor++;
121
100
    }
123
102
 
124
103
function historyCurr()
125
104
{
126
 
    return this.edited[this.cursor];
 
105
    if (this.cursor < 0 || this.cursor >= this.items.length)
 
106
    {
 
107
        return "";
 
108
    }
 
109
    return this.items[this.cursor];
127
110
}
128
111
 
129
 
function historySubmit(text)
 
112
function historyAdd(text)
130
113
{
131
 
    /* Copy the selected item's "edited" version over the permanent version of
132
 
     * the last item. */
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
138
 
     * items set.
139
 
     * Not the whole history - just start from the earliest edited one.
140
 
     * (This avoids slowdown over extended usage time).
141
 
     */
142
 
    for (var i=this.earliestCursor; i<=this.cursor; i++)
143
 
        this.edited[i] = this.items[i];
144
 
    this.earliestCursor = this.cursor;
 
114
    this.items[this.items.length] = text;
 
115
    this.cursor = this.items.length;
145
116
}
146
117
 
147
118
function historyShow()
148
119
{
149
120
    var res = "";
 
121
    if (this.cursor == -1)
 
122
    {
 
123
        res += "[]";
 
124
    }
150
125
    for (var i = 0; i < this.items.length; i++)
151
126
    {
152
127
        if (i == this.cursor)
167
142
    return res;
168
143
}
169
144
 
170
 
/* How history works
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.
177
 
 *
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.
182
 
 *
183
 
 * Cursor never points to an invalid location.
184
 
 */
185
145
function History()
186
146
{
187
 
    this.items = new Array("");
188
 
    this.edited = new Array("");
189
 
    this.cursor = 0;
190
 
    this.earliestCursor = 0;
 
147
    this.items = new Array();
 
148
    this.cursor = -1;
191
149
    this.up = historyUp;
192
150
    this.down = historyDown;
193
151
    this.curr = historyCurr;
194
 
    this.submit = historySubmit;
 
152
    this.add = historyAdd;
195
153
    this.show = historyShow;
196
154
}
197
155
 
201
159
 * to its response by writing to the output box.
202
160
 * Also maximize the console window if not already.
203
161
 */
204
 
function console_enter_line(inputline, which)
 
162
function console_enter_line(inputline)
205
163
{
206
 
    /* Start the server if it hasn't already been started */
207
 
    start_server();
208
 
    var args = {"key": server_key, "text":inputline};
209
 
    var xmlhttp = ajax_call("consoleservice", which, args, "POST");
 
164
    var digest = hex_md5(inputline + magic);
 
165
    var args = {"host": server_host, "port": server_port,
 
166
                    "digest":digest, "text":inputline};
 
167
    var xmlhttp = ajax_call("consoleservice", "chat", args, "POST");
210
168
 
211
169
    var res = JSON.parse(xmlhttp.responseText);
212
170
    var output = document.getElementById("console_output");
239
197
    else if (res.hasOwnProperty('exc'))
240
198
    {
241
199
        // Failure!
242
 
        // print out any output that came before the error
243
 
        if (res.exc[0].length > 0)
244
 
        {
245
 
            var pre = document.createElement("pre");
246
 
            pre.setAttribute("class", "outputMsg");
247
 
            pre.appendChild(document.createTextNode(res.exc[0]));
248
 
            output.appendChild(pre);
249
 
        }
250
 
 
251
200
        // print out the error message (res.exc)
252
201
        var pre = document.createElement("pre");
253
202
        pre.setAttribute("class", "errorMsg");
254
 
        pre.appendChild(document.createTextNode(res.exc[1]));
 
203
        pre.appendChild(document.createTextNode(res.exc));
255
204
        output.appendChild(pre);
256
205
    }
257
206
    else if (res.hasOwnProperty('more'))
272
221
function catch_input(key)
273
222
{
274
223
    var inp = document.getElementById('console_inputText');
275
 
    switch (key)
276
 
    {
277
 
    case 9:                 /* Tab key */
278
 
        var selstart = inp.selectionStart;
279
 
        var selend = inp.selectionEnd;
280
 
        if (selstart == selend)
281
 
        {
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);
285
 
        }
286
 
        else
287
 
        {
288
 
            /* Text is selected. Just indent the whole line
289
 
             * by inserting a tab at the start */
290
 
            inp.value = TAB_STRING + inp.value;
291
 
        }
292
 
        /* Update the selection so the same characters as before are selected
293
 
         */
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
297
 
         * box */
298
 
        return false;
299
 
        /* Note: If it happens that some browsers don't support event
300
 
         * cancelling properly, this hack might work instead:
301
 
        setTimeout(
302
 
            "document.getElementById('console_inputText').focus()",
303
 
            0);
304
 
         */
305
 
        break;
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();
311
 
        break;
312
 
    case 38:                /* Up arrow */
313
 
        hist.up(inp.value);
314
 
        inp.value = hist.curr();
315
 
        break;
316
 
    case 40:                /* Down arrow */
317
 
        hist.down(inp.value);
318
 
        inp.value = hist.curr();
319
 
        break;
 
224
    if (key == 13)
 
225
    {
 
226
        console_enter_line(inp.value);
 
227
        hist.add(inp.value);
 
228
        inp.value = hist.curr();
 
229
    }
 
230
    if (key == 38)
 
231
    {
 
232
        hist.up();
 
233
        inp.value = hist.curr();
 
234
    }
 
235
    if (key == 40)
 
236
    {
 
237
        hist.down();
 
238
        inp.value = hist.curr();
320
239
    }
321
240
}