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

« back to all changes in this revision

Viewing changes to console/console.js

  • Committer: drtomc
  • Date: 2007-12-11 03:26:29 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:25
A bit more work on the userdb stuff.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
var magic = 'xyzzy';
2
 
 
3
 
function historyUp()
4
 
{
5
 
    if (this.cursor >= 0)
6
 
    {
7
 
        this.cursor--;
8
 
    }
9
 
}
10
 
 
11
 
function historyDown()
12
 
{
13
 
    if (this.cursor < this.items.length)
14
 
    {
15
 
        this.cursor++;
16
 
    }
17
 
}
18
 
 
19
 
function historyCurr()
20
 
{
21
 
    if (this.cursor < 0 || this.cursor >= this.items.length)
22
 
    {
23
 
        return "";
24
 
    }
25
 
    return this.items[this.cursor];
26
 
}
27
 
 
28
 
function historyAdd(text)
29
 
{
30
 
    this.items[this.items.length] = text;
31
 
    this.cursor = this.items.length;
32
 
}
33
 
 
34
 
function historyShow()
35
 
{
36
 
    var res = "";
37
 
    if (this.cursor == -1)
38
 
    {
39
 
        res += "[]";
40
 
    }
41
 
    for (var i = 0; i < this.items.length; i++)
42
 
    {
43
 
        if (i == this.cursor)
44
 
        {
45
 
            res += "["
46
 
        }
47
 
        res += this.items[i].toString();
48
 
        if (i == this.cursor)
49
 
        {
50
 
            res += "]"
51
 
        }
52
 
        res += " "
53
 
    }
54
 
    if (this.cursor == this.items.length)
55
 
    {
56
 
        res += "[]";
57
 
    }
58
 
    return res;
59
 
}
60
 
 
61
 
function History()
62
 
{
63
 
    this.items = new Array();
64
 
    this.cursor = -1;
65
 
    this.up = historyUp;
66
 
    this.down = historyDown;
67
 
    this.curr = historyCurr;
68
 
    this.add = historyAdd;
69
 
    this.show = historyShow;
70
 
}
71
 
 
72
 
var hist = new History();
73
 
 
74
 
function make_post_body(args)
75
 
{
76
 
    var qs = '';
77
 
    for (key in args)
78
 
    {
79
 
        vals = args[key];
80
 
        // vals can be an array, to make multiple args with the same name
81
 
        // To handle this, make non-array objects into an array, then loop
82
 
        if (!(vals instanceof Array))
83
 
            vals = [vals];
84
 
        var i;
85
 
        for (i=0; i<vals.length; i++)
86
 
        {
87
 
            if (i > 0)
88
 
            {
89
 
                qs += "&";
90
 
            }
91
 
            qs += encodeURIComponent(key) + "=" + encodeURIComponent(vals[i]);
92
 
        }
93
 
    }
94
 
    return qs;
95
 
}
96
 
 
97
 
function enter_line()
98
 
{
99
 
    var inp = document.getElementById('inputText');
100
 
    var digest = hex_md5(inp.value + magic);
101
 
    var xmlhttp = new XMLHttpRequest();
102
 
    xmlhttp.open("POST", "chat", false);
103
 
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
104
 
    xmlhttp.send(make_post_body({"digest":digest, "text":inp.value}))
105
 
    var res = JSON.parse(xmlhttp.responseText);
106
 
    var output = document.getElementById("output");
107
 
    {
108
 
        var pre = document.createElement("pre");
109
 
        pre.setAttribute("class", "inputMsg");
110
 
        pre.appendChild(document.createTextNode(inp.value + "\n"));
111
 
        output.appendChild(pre);
112
 
    }
113
 
    if (res.hasOwnProperty('okay'))
114
 
    {
115
 
        // Success!
116
 
        // print out the output (res.okay[0])
117
 
        var pre = document.createElement("pre");
118
 
        pre.setAttribute("class", "outputMsg");
119
 
        pre.appendChild(document.createTextNode(res.okay[0]));
120
 
        output.appendChild(pre);
121
 
        // print out the return value (res.okay[1])
122
 
        if (res.okay[1])
123
 
        {
124
 
            var pre = document.createElement("pre");
125
 
            pre.setAttribute("class", "outputMsg");
126
 
            pre.appendChild(document.createTextNode(res.okay[1] + "\n"));
127
 
            output.appendChild(pre);
128
 
        }
129
 
        // set the prompt to >>>
130
 
        var prompt = document.getElementById("prompt");
131
 
        prompt.replaceChild(document.createTextNode(">>> "), prompt.firstChild);
132
 
    }
133
 
    else if (res.hasOwnProperty('exc'))
134
 
    {
135
 
        // Failure!
136
 
        // print out the error message (res.exc)
137
 
        var pre = document.createElement("pre");
138
 
        pre.setAttribute("class", "errorMsg");
139
 
        pre.appendChild(document.createTextNode(res.exc));
140
 
        output.appendChild(pre);
141
 
    }
142
 
    else if (res.hasOwnProperty('more'))
143
 
    {
144
 
        // Need more input, so set the prompt to ...
145
 
        var prompt = document.getElementById("prompt");
146
 
        prompt.replaceChild(document.createTextNode("... "), prompt.firstChild);
147
 
    }
148
 
    else {
149
 
        // assert res.hasOwnProperty('input')
150
 
        var prompt = document.getElementById("prompt");
151
 
        prompt.replaceChild(document.createTextNode("+++ "), prompt.firstChild);
152
 
    }
153
 
}
154
 
 
155
 
function catch_input(key)
156
 
{
157
 
    var inp = document.getElementById('inputText');
158
 
    if (key == 13)
159
 
    {
160
 
        enter_line();
161
 
        hist.add(inp.value);
162
 
        inp.value = hist.curr();
163
 
    }
164
 
    if (key == 38)
165
 
    {
166
 
        hist.up();
167
 
        inp.value = hist.curr();
168
 
    }
169
 
    if (key == 40)
170
 
    {
171
 
        hist.down();
172
 
        inp.value = hist.curr();
173
 
    }
174
 
}