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

217 by mattgiuca
Console: Python code generates a minimal document with a DIV and links to
1
digest_constant = "hello";
2
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
3
var server_host;
4
var server_port;
5
var server_magic;
6
217 by mattgiuca
Console: Python code generates a minimal document with a DIV and links to
7
/* Starts the console server.
8
 * Returns an object with fields "host", "port", "magic" describing the
9
 * server.
10
 */
11
function start_server()
12
{
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
13
    var xhr = ajax_call("consoleservice", "start", {}, "POST");
217 by mattgiuca
Console: Python code generates a minimal document with a DIV and links to
14
    var json_text = xhr.responseText;
15
    return JSON.parse(json_text);
16
}
17
18
function onload()
19
{
20
    /* Start the server */
21
    var server_info = start_server();
276 by mattgiuca
Console now runs inside IVLE (without requiring an IFRAME). The separate
22
    server_host = server_info.host;
23
    server_port = server_info.port;
24
    server_magic = server_info.magic;
25
}
26
27
/* Below here imported from trunk/console/console.js
28
 * (Tom Conway)
29
 */
30
31
var magic = 'xyzzy';
32
33
function historyUp()
34
{
35
    if (this.cursor >= 0)
36
    {
37
        this.cursor--;
38
    }
39
}
40
41
function historyDown()
42
{
43
    if (this.cursor < this.items.length)
44
    {
45
        this.cursor++;
46
    }
47
}
48
49
function historyCurr()
50
{
51
    if (this.cursor < 0 || this.cursor >= this.items.length)
52
    {
53
        return "";
54
    }
55
    return this.items[this.cursor];
56
}
57
58
function historyAdd(text)
59
{
60
    this.items[this.items.length] = text;
61
    this.cursor = this.items.length;
62
}
63
64
function historyShow()
65
{
66
    var res = "";
67
    if (this.cursor == -1)
68
    {
69
        res += "[]";
70
    }
71
    for (var i = 0; i < this.items.length; i++)
72
    {
73
        if (i == this.cursor)
74
        {
75
            res += "["
76
        }
77
        res += this.items[i].toString();
78
        if (i == this.cursor)
79
        {
80
            res += "]"
81
        }
82
        res += " "
83
    }
84
    if (this.cursor == this.items.length)
85
    {
86
        res += "[]";
87
    }
88
    return res;
89
}
90
91
function History()
92
{
93
    this.items = new Array();
94
    this.cursor = -1;
95
    this.up = historyUp;
96
    this.down = historyDown;
97
    this.curr = historyCurr;
98
    this.add = historyAdd;
99
    this.show = historyShow;
100
}
101
102
var hist = new History();
103
104
function enter_line()
105
{
106
    var inp = document.getElementById('inputText');
107
    var digest = hex_md5(inp.value + magic);
108
    var args = {"host": server_host, "port": server_port,
109
                    "digest":digest, "text":inp.value};
110
    var xmlhttp = ajax_call("consoleservice", "chat", args, "POST");
111
112
    var res = JSON.parse(xmlhttp.responseText);
113
    var output = document.getElementById("output");
114
    {
115
        var pre = document.createElement("pre");
116
        pre.setAttribute("class", "inputMsg");
117
        pre.appendChild(document.createTextNode(inp.value + "\n"));
118
        output.appendChild(pre);
119
    }
120
    if (res.hasOwnProperty('okay'))
121
    {
122
        // Success!
123
        // print out the output (res.okay[0])
124
        var pre = document.createElement("pre");
125
        pre.setAttribute("class", "outputMsg");
126
        pre.appendChild(document.createTextNode(res.okay[0]));
127
        output.appendChild(pre);
128
        // print out the return value (res.okay[1])
129
        if (res.okay[1])
130
        {
131
            var pre = document.createElement("pre");
132
            pre.setAttribute("class", "outputMsg");
133
            pre.appendChild(document.createTextNode(res.okay[1] + "\n"));
134
            output.appendChild(pre);
135
        }
136
        // set the prompt to >>>
137
        var prompt = document.getElementById("prompt");
138
        prompt.replaceChild(document.createTextNode(">>> "), prompt.firstChild);
139
    }
140
    else if (res.hasOwnProperty('exc'))
141
    {
142
        // Failure!
143
        // print out the error message (res.exc)
144
        var pre = document.createElement("pre");
145
        pre.setAttribute("class", "errorMsg");
146
        pre.appendChild(document.createTextNode(res.exc));
147
        output.appendChild(pre);
148
    }
149
    else if (res.hasOwnProperty('more'))
150
    {
151
        // Need more input, so set the prompt to ...
152
        var prompt = document.getElementById("prompt");
153
        prompt.replaceChild(document.createTextNode("... "), prompt.firstChild);
154
    }
155
    else {
156
        // assert res.hasOwnProperty('input')
157
        var prompt = document.getElementById("prompt");
158
        prompt.replaceChild(document.createTextNode("+++ "), prompt.firstChild);
159
    }
160
}
161
162
function catch_input(key)
163
{
164
    var inp = document.getElementById('inputText');
165
    if (key == 13)
166
    {
167
        enter_line();
168
        hist.add(inp.value);
169
        inp.value = hist.curr();
170
    }
171
    if (key == 38)
172
    {
173
        hist.up();
174
        inp.value = hist.curr();
175
    }
176
    if (key == 40)
177
    {
178
        hist.down();
179
        inp.value = hist.curr();
180
    }
217 by mattgiuca
Console: Python code generates a minimal document with a DIV and links to
181
}