~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-01-12 12:02:45 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:193
Apps: Added stubs for the 3 new apps, Editor, Console and Tutorial.
All of these apps currently just print stub messages, and so do their help
pages.
Note: Editor will eventually be integrated with file browser.
conf.apps: Added the 3 new apps to the tabs and apps list. Removed "dummy"
    from the apps lists (but did not delete its source).

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
 
digest_constant = "hello";
24
 
 
25
 
var server_host;
26
 
var server_port;
27
 
var server_magic;
28
 
 
29
 
/* Begin religious debate (tabs vs spaces) here: */
30
 
/* (This string will be inserted in the console when the user presses the Tab
31
 
 * key) */
32
 
TAB_STRING = "    ";
33
 
 
34
 
/* Console DOM objects */
35
 
console_body = null;
36
 
console_filler = null;
37
 
 
38
 
windowpane_mode = false;
39
 
 
40
 
/* Starts the console server.
41
 
 * Returns an object with fields "host", "port", "magic" describing the
42
 
 * server.
43
 
 */
44
 
function start_server()
45
 
{
46
 
    var xhr = ajax_call("consoleservice", "start", {}, "POST");
47
 
    var json_text = xhr.responseText;
48
 
    return JSON.parse(json_text);
49
 
}
50
 
 
51
 
/** Initialises the console. All apps which import console are required to
52
 
 * call this function.
53
 
 * Optional "windowpane" (bool), if true, will cause the console to go into
54
 
 * "window pane" mode which will allow it to be opened and closed, and float
55
 
 * over the page.
56
 
 * (Defaults to closed).
57
 
 */
58
 
function console_init(windowpane)
59
 
{
60
 
    /* Set up the console as a floating pane */
61
 
    console_body = document.getElementById("console_body");
62
 
    console_filler = document.getElementById("console_filler");
63
 
    if (windowpane)
64
 
    {
65
 
        windowpane_mode = true;
66
 
        console_minimize();
67
 
    }
68
 
    /* Start the server */
69
 
    var server_info = start_server();
70
 
    server_host = server_info.host;
71
 
    server_port = server_info.port;
72
 
    server_magic = server_info.magic;
73
 
}
74
 
 
75
 
/** Hide the main console panel, so the console minimizes to just an input box
76
 
 *  at the page bottom. */
77
 
function console_minimize()
78
 
{
79
 
    if (!windowpane_mode) return;
80
 
    console_body.setAttribute("class", "windowpane minimal");
81
 
    console_filler.setAttribute("class", "windowpane minimal");
82
 
}
83
 
 
84
 
/** Show the main console panel, so it enlarges out to its full size.
85
 
 */
86
 
function console_maximize()
87
 
{
88
 
    if (!windowpane_mode) return;
89
 
    console_body.setAttribute("class", "windowpane maximal");
90
 
    console_filler.setAttribute("class", "windowpane maximal");
91
 
}
92
 
 
93
 
/* Below here imported from trunk/console/console.js
94
 
 * (Tom Conway)
95
 
 */
96
 
 
97
 
var magic = 'xyzzy';
98
 
 
99
 
function historyUp()
100
 
{
101
 
    if (this.cursor >= 0)
102
 
    {
103
 
        this.cursor--;
104
 
    }
105
 
}
106
 
 
107
 
function historyDown()
108
 
{
109
 
    if (this.cursor < this.items.length)
110
 
    {
111
 
        this.cursor++;
112
 
    }
113
 
}
114
 
 
115
 
function historyCurr()
116
 
{
117
 
    if (this.cursor < 0 || this.cursor >= this.items.length)
118
 
    {
119
 
        return "";
120
 
    }
121
 
    return this.items[this.cursor];
122
 
}
123
 
 
124
 
function historyAdd(text)
125
 
{
126
 
    this.items[this.items.length] = text;
127
 
    this.cursor = this.items.length;
128
 
}
129
 
 
130
 
function historyShow()
131
 
{
132
 
    var res = "";
133
 
    if (this.cursor == -1)
134
 
    {
135
 
        res += "[]";
136
 
    }
137
 
    for (var i = 0; i < this.items.length; i++)
138
 
    {
139
 
        if (i == this.cursor)
140
 
        {
141
 
            res += "["
142
 
        }
143
 
        res += this.items[i].toString();
144
 
        if (i == this.cursor)
145
 
        {
146
 
            res += "]"
147
 
        }
148
 
        res += " "
149
 
    }
150
 
    if (this.cursor == this.items.length)
151
 
    {
152
 
        res += "[]";
153
 
    }
154
 
    return res;
155
 
}
156
 
 
157
 
function History()
158
 
{
159
 
    this.items = new Array();
160
 
    this.cursor = -1;
161
 
    this.up = historyUp;
162
 
    this.down = historyDown;
163
 
    this.curr = historyCurr;
164
 
    this.add = historyAdd;
165
 
    this.show = historyShow;
166
 
}
167
 
 
168
 
var hist = new History();
169
 
 
170
 
/** Send a line of text to the Python server, wait for its return, and react
171
 
 * to its response by writing to the output box.
172
 
 * Also maximize the console window if not already.
173
 
 */
174
 
function console_enter_line(inputline)
175
 
{
176
 
    var digest = hex_md5(inputline + magic);
177
 
    var args = {"host": server_host, "port": server_port,
178
 
                    "digest":digest, "text":inputline};
179
 
    var xmlhttp = ajax_call("consoleservice", "chat", args, "POST");
180
 
 
181
 
    var res = JSON.parse(xmlhttp.responseText);
182
 
    var output = document.getElementById("console_output");
183
 
    {
184
 
        var pre = document.createElement("pre");
185
 
        pre.setAttribute("class", "inputMsg");
186
 
        pre.appendChild(document.createTextNode(inputline + "\n"));
187
 
        output.appendChild(pre);
188
 
    }
189
 
    if (res.hasOwnProperty('okay'))
190
 
    {
191
 
        // Success!
192
 
        // print out the output (res.okay[0])
193
 
        var pre = document.createElement("pre");
194
 
        pre.setAttribute("class", "outputMsg");
195
 
        pre.appendChild(document.createTextNode(res.okay[0]));
196
 
        output.appendChild(pre);
197
 
        // print out the return value (res.okay[1])
198
 
        if (res.okay[1])
199
 
        {
200
 
            var pre = document.createElement("pre");
201
 
            pre.setAttribute("class", "outputMsg");
202
 
            pre.appendChild(document.createTextNode(res.okay[1] + "\n"));
203
 
            output.appendChild(pre);
204
 
        }
205
 
        // set the prompt to >>>
206
 
        var prompt = document.getElementById("console_prompt");
207
 
        prompt.replaceChild(document.createTextNode(">>> "), prompt.firstChild);
208
 
    }
209
 
    else if (res.hasOwnProperty('exc'))
210
 
    {
211
 
        // Failure!
212
 
        // print out the error message (res.exc)
213
 
        var pre = document.createElement("pre");
214
 
        pre.setAttribute("class", "errorMsg");
215
 
        pre.appendChild(document.createTextNode(res.exc));
216
 
        output.appendChild(pre);
217
 
    }
218
 
    else if (res.hasOwnProperty('more'))
219
 
    {
220
 
        // Need more input, so set the prompt to ...
221
 
        var prompt = document.getElementById("console_prompt");
222
 
        prompt.replaceChild(document.createTextNode("... "), prompt.firstChild);
223
 
    }
224
 
    else {
225
 
        // assert res.hasOwnProperty('input')
226
 
        var prompt = document.getElementById("console_prompt");
227
 
        prompt.replaceChild(document.createTextNode("+++ "), prompt.firstChild);
228
 
    }
229
 
    /* Open up the console so we can see the output */
230
 
    console_maximize();
231
 
}
232
 
 
233
 
function catch_input(key)
234
 
{
235
 
    var inp = document.getElementById('console_inputText');
236
 
    switch (key)
237
 
    {
238
 
    case 9:                 /* Tab key */
239
 
        var selstart = inp.selectionStart;
240
 
        var selend = inp.selectionEnd;
241
 
        if (selstart == selend)
242
 
        {
243
 
            /* No selection, just a carat. Insert a tab here. */
244
 
            inp.value = inp.value.substr(0, selstart)
245
 
                + TAB_STRING + inp.value.substr(selstart);
246
 
        }
247
 
        else
248
 
        {
249
 
            /* Text is selected. Just indent the whole line
250
 
             * by inserting a tab at the start */
251
 
            inp.value = TAB_STRING + inp.value;
252
 
        }
253
 
        /* Update the selection so the same characters as before are selected
254
 
         */
255
 
        inp.selectionStart = selstart + TAB_STRING.length;
256
 
        inp.selectionEnd = inp.selectionStart + (selend - selstart);
257
 
        /* Cancel the event, so the TAB key doesn't move focus away from this
258
 
         * box */
259
 
        return false;
260
 
        /* Note: If it happens that some browsers don't support event
261
 
         * cancelling properly, this hack might work instead:
262
 
        setTimeout(
263
 
            "document.getElementById('console_inputText').focus()",
264
 
            0);
265
 
         */
266
 
        break;
267
 
    case 13:                /* Enter key */
268
 
        /* Send the line of text to the server */
269
 
        console_enter_line(inp.value);
270
 
        hist.add(inp.value);
271
 
        inp.value = hist.curr();
272
 
        break;
273
 
    case 38:                /* Up arrow */
274
 
        hist.up();
275
 
        inp.value = hist.curr();
276
 
        break;
277
 
    case 40:                /* Down arrow */
278
 
        hist.down();
279
 
        inp.value = hist.curr();
280
 
        break;
281
 
    }
282
 
}