adamc@692
|
1 // Lists
|
adamc@692
|
2
|
adamc@580
|
3 function cons(v, ls) {
|
adamc@690
|
4 return { next : ls, data : v };
|
adamc@690
|
5 }
|
adamc@690
|
6 function concat(ls1, ls2) {
|
adamc@690
|
7 return (ls1 ? cons(ls1.data, concat(ls1.next, ls2)) : ls2);
|
adamc@690
|
8 }
|
adamc@690
|
9 function member(x, ls) {
|
adamc@690
|
10 for (; ls; ls = ls.next)
|
adamc@690
|
11 if (ls.data == x)
|
adamc@690
|
12 return true;
|
adamc@690
|
13 return false;
|
adamc@690
|
14 }
|
adamc@690
|
15 function remove(x, ls) {
|
adamc@690
|
16 return (ls ? (ls.data == x ? ls.next : cons(ls.data, remove(x, ls.next))) : null);
|
adamc@690
|
17 }
|
adamc@690
|
18 function union(ls1, ls2) {
|
adamc@690
|
19 return (ls1 ? (member(ls1.data, ls2) ? union(ls1.next, ls2) : cons(ls1.data, union(ls1.next, ls2))) : ls2);
|
adamc@580
|
20 }
|
adamc@703
|
21 function length(ls) {
|
adamc@703
|
22 return (ls ? 1 + length(ls.next) : 0);
|
adamc@703
|
23 }
|
adamc@670
|
24
|
adamc@690
|
25
|
adamc@729
|
26 // Error handling
|
adamc@729
|
27
|
adamc@729
|
28 function whine(msg) {
|
adamc@729
|
29 alert(msg);
|
adamc@729
|
30 throw msg;
|
adamc@729
|
31 }
|
adamc@729
|
32
|
adamc@810
|
33 function pf(loc) {
|
adamc@835
|
34 throw ("Pattern match failure (" + loc + ")");
|
adamc@729
|
35 }
|
adamc@729
|
36
|
adamc@798
|
37 function runHandlers(kind, ls, arg) {
|
adamc@798
|
38 if (ls == null)
|
adamc@798
|
39 alert(kind + ": " + arg);
|
adamc@729
|
40 for (; ls; ls = ls.next)
|
adamc@729
|
41 try {
|
adamc@729
|
42 ls.data(arg)(null);
|
adamc@729
|
43 } catch (v) { }
|
adamc@729
|
44 }
|
adamc@729
|
45
|
adamc@729
|
46 var errorHandlers = null;
|
adamc@729
|
47
|
adamc@729
|
48 function onError(f) {
|
adamc@729
|
49 errorHandlers = cons(f, errorHandlers);
|
adamc@729
|
50 }
|
adamc@729
|
51
|
adamc@729
|
52 function er(s) {
|
adamc@798
|
53 runHandlers("Error", errorHandlers, s);
|
adamc@729
|
54 throw {uw_error: s};
|
adamc@729
|
55 }
|
adamc@729
|
56
|
adamc@729
|
57 var failHandlers = null;
|
adamc@729
|
58
|
adamc@729
|
59 function onFail(f) {
|
adamc@729
|
60 failHandlers = cons(f, failHandlers);
|
adamc@729
|
61 }
|
adamc@729
|
62
|
adamc@729
|
63 function doExn(v) {
|
adamc@729
|
64 if (v == null || v.uw_error == null) {
|
adamc@729
|
65 var s = (v == null ? "null" : v.toString());
|
adamc@798
|
66 runHandlers("Fail", failHandlers, s);
|
adamc@729
|
67 }
|
adamc@729
|
68 }
|
adamc@729
|
69
|
adamc@729
|
70 var disconnectHandlers = null;
|
adamc@729
|
71
|
adamc@729
|
72 function onDisconnect(f) {
|
adamc@729
|
73 disconnectHandlers = cons(function (_){return f}, disconnectHandlers);
|
adamc@729
|
74 }
|
adamc@729
|
75
|
adamc@729
|
76 function discon() {
|
adamc@798
|
77 runHandlers("Disconnect", disconnectHandlers, null);
|
adamc@729
|
78 }
|
adamc@729
|
79
|
adamc@729
|
80 var connectHandlers = null;
|
adamc@729
|
81
|
adamc@729
|
82 function onConnectFail(f) {
|
adamc@729
|
83 connectHandlers = cons(function (_){return f}, connectHandlers);
|
adamc@729
|
84 }
|
adamc@729
|
85
|
adamc@729
|
86 function conn() {
|
adamc@798
|
87 runHandlers("Connect", connectHandlers, null);
|
adamc@729
|
88 }
|
adamc@729
|
89
|
adamc@729
|
90 var serverHandlers = null;
|
adamc@729
|
91
|
adamc@729
|
92 function onServerError(f) {
|
adamc@729
|
93 serverHandlers = cons(f, serverHandlers);
|
adamc@729
|
94 }
|
adamc@729
|
95
|
adamc@729
|
96 function servErr(s) {
|
adamc@824
|
97 window.setTimeout(function () { runHandlers("Server", serverHandlers, s); }, 0);
|
adamc@729
|
98 }
|
adamc@729
|
99
|
adamc@729
|
100
|
adamc@692
|
101 // Embedding closures in XML strings
|
adamc@692
|
102
|
adamc@693
|
103 function cs(f) {
|
adamc@693
|
104 return {closure: f};
|
adamc@693
|
105 }
|
adamc@693
|
106
|
adamc@693
|
107 function isWeird(v) {
|
adamc@693
|
108 return v.closure != null || v.cat1 != null;
|
adamc@693
|
109 }
|
adamc@693
|
110
|
adamc@692
|
111 function cat(s1, s2) {
|
adamc@693
|
112 if (isWeird(s1) || isWeird(s2))
|
adamc@693
|
113 return {cat1: s1, cat2: s2};
|
adamc@693
|
114 else
|
adamc@692
|
115 return s1 + s2;
|
adamc@692
|
116 }
|
adamc@692
|
117
|
adamc@692
|
118 var closures = [];
|
adamc@703
|
119 var freeClosures = null;
|
adamc@692
|
120
|
adamc@692
|
121 function newClosure(f) {
|
adamc@703
|
122 var n;
|
adamc@703
|
123 if (freeClosures == null) {
|
adamc@703
|
124 n = closures.length;
|
adamc@703
|
125 } else {
|
adamc@703
|
126 n = freeClosures.data;
|
adamc@703
|
127 freeClosures = freeClosures.next;
|
adamc@703
|
128 }
|
adamc@692
|
129 closures[n] = f;
|
adamc@692
|
130 return n;
|
adamc@692
|
131 }
|
adamc@692
|
132
|
adamc@703
|
133 function freeClosure(n) {
|
adamc@703
|
134 closures[n] = null;
|
adamc@703
|
135 freeClosures = cons(n, freeClosures);
|
adamc@703
|
136 }
|
adamc@703
|
137
|
adamc@692
|
138 function cr(n) {
|
adamc@692
|
139 return closures[n]();
|
adamc@692
|
140 }
|
adamc@692
|
141
|
adamc@703
|
142 function flatten(cls, tr) {
|
adamc@693
|
143 if (tr.cat1 != null)
|
adamc@703
|
144 return flatten(cls, tr.cat1) + flatten(cls, tr.cat2);
|
adamc@703
|
145 else if (tr.closure != null) {
|
adamc@703
|
146 var cl = newClosure(tr.closure);
|
adamc@703
|
147 cls.v = cons(cl, cls.v);
|
adamc@703
|
148 return "cr(" + cl + ")";
|
adamc@703
|
149 } else
|
adamc@692
|
150 return tr;
|
adamc@692
|
151 }
|
adamc@692
|
152
|
adamc@728
|
153 function flattenLocal(s) {
|
adamc@728
|
154 var cls = {v : null};
|
adamc@728
|
155 var r = flatten(cls, s);
|
adamc@728
|
156 for (cl = cls.v; cl != null; cl = cl.next)
|
adamc@728
|
157 freeClosure(cl.data);
|
adamc@728
|
158 return r;
|
adamc@728
|
159 }
|
adamc@728
|
160
|
adamc@692
|
161
|
adamc@692
|
162
|
adamc@692
|
163 // Dynamic tree management
|
adamc@692
|
164
|
adamc@690
|
165 function populate(node) {
|
adamc@690
|
166 var s = node.signal;
|
adamc@690
|
167 var oldSources = node.sources;
|
adamc@729
|
168 try {
|
adamc@729
|
169 var sr = s();
|
adamc@729
|
170 var newSources = sr.sources;
|
adamc@690
|
171
|
adamc@729
|
172 for (var sp = oldSources; sp; sp = sp.next)
|
adamc@729
|
173 if (!member(sp.data, newSources))
|
adamc@729
|
174 sp.data.dyns = remove(node, sp.data.dyns);
|
adamc@690
|
175
|
adamc@729
|
176 for (var sp = newSources; sp; sp = sp.next)
|
adamc@729
|
177 if (!member(sp.data, oldSources))
|
adamc@729
|
178 sp.data.dyns = cons(node, sp.data.dyns);
|
adamc@690
|
179
|
adamc@729
|
180 node.sources = newSources;
|
adamc@729
|
181 node.recreate(sr.data);
|
adamc@729
|
182 } catch (v) {
|
adamc@729
|
183 doExn(v);
|
adamc@729
|
184 }
|
adamc@579
|
185 }
|
adamc@574
|
186
|
adamc@580
|
187 function sc(v) {
|
adamc@690
|
188 return {data : v, dyns : null};
|
adamc@580
|
189 }
|
adamc@580
|
190 function sv(s, v) {
|
adamc@690
|
191 s.data = v;
|
adamc@690
|
192 for (var ls = s.dyns; ls; ls = ls.next)
|
adamc@690
|
193 if (!ls.dead)
|
adamc@690
|
194 populate(ls.data);
|
adamc@580
|
195 }
|
adamc@601
|
196 function sg(s) {
|
adamc@690
|
197 return s.data;
|
adamc@601
|
198 }
|
adamc@579
|
199
|
adamc@580
|
200 function ss(s) {
|
adamc@690
|
201 return function() { return {sources : cons(s, null), data : s.data } };
|
adamc@580
|
202 }
|
adamc@580
|
203 function sr(v) {
|
adamc@690
|
204 return function() { return {sources : null, data : v } };
|
adamc@580
|
205 }
|
adamc@580
|
206 function sb(x,y) {
|
adamc@690
|
207 return function() {
|
adamc@690
|
208 var xr = x();
|
adamc@690
|
209 var yr = y(xr.data)();
|
adamc@690
|
210 return {sources : union(xr.sources, yr.sources), data : yr.data};
|
adamc@690
|
211 };
|
adamc@580
|
212 }
|
adamc@841
|
213 function scur(s) {
|
adamc@841
|
214 return s().data;
|
adamc@841
|
215 }
|
adamc@571
|
216
|
adamc@604
|
217 function lastParent() {
|
adamc@604
|
218 var pos = document;
|
adamc@604
|
219
|
adamc@600
|
220 while (pos.lastChild && pos.lastChild.nodeType == 1)
|
adamc@600
|
221 pos = pos.lastChild;
|
adamc@600
|
222
|
adamc@600
|
223 return pos.parentNode;
|
adamc@600
|
224 }
|
adamc@600
|
225
|
adamc@898
|
226 function parent() {
|
adamc@898
|
227 return thisScript ? thisScript.parentNode : lastParent();
|
adamc@898
|
228 }
|
adamc@898
|
229
|
adamc@604
|
230 function addNode(node) {
|
adamc@899
|
231 if (thisScript)
|
adamc@899
|
232 thisScript.parentNode.replaceChild(node, thisScript);
|
adamc@899
|
233 else
|
adamc@604
|
234 lastParent().appendChild(node);
|
adamc@603
|
235 }
|
adamc@603
|
236
|
adamc@690
|
237 var thisScript = null;
|
adamc@690
|
238
|
adamc@604
|
239 function runScripts(node) {
|
adamc@899
|
240 if (node.getElementsByTagName) {
|
adamc@899
|
241 var savedScript = thisScript;
|
adamc@603
|
242
|
adamc@899
|
243 var scripts = node.getElementsByTagName("script"), scriptsCopy = [];
|
adamc@899
|
244 var len = scripts.length;
|
adamc@899
|
245 for (var i = 0; i < len; ++i)
|
adamc@899
|
246 scriptsCopy[i] = scripts[i];
|
adamc@899
|
247 for (var i = 0; i < len; ++i) {
|
adamc@899
|
248 thisScript = scriptsCopy[i];
|
adamc@899
|
249 try {
|
adamc@899
|
250 eval(thisScript.textContent);
|
adamc@899
|
251 } catch (v) {
|
adamc@899
|
252 doExn(v);
|
adamc@899
|
253 }
|
adamc@899
|
254 if (thisScript.parentNode)
|
adamc@899
|
255 thisScripts.parentNode.removeChild(thisScript);
|
adamc@729
|
256 }
|
adamc@899
|
257
|
adamc@899
|
258 thisScript = savedScript;
|
adamc@604
|
259 }
|
adamc@603
|
260 }
|
adamc@603
|
261
|
adamc@603
|
262
|
adamc@692
|
263 // Dynamic tree entry points
|
adamc@692
|
264
|
adamc@571
|
265 function dyn(s) {
|
adamc@899
|
266 var x = document.createElement("div");
|
adamc@899
|
267 x.style.display = "inline";
|
adamc@690
|
268 x.dead = false;
|
adamc@690
|
269 x.signal = s;
|
adamc@690
|
270 x.sources = null;
|
adamc@703
|
271 x.closures = null;
|
adamc@899
|
272
|
adamc@899
|
273 var first = null;
|
adamc@899
|
274
|
adamc@690
|
275 x.recreate = function(v) {
|
adamc@703
|
276 for (var ls = x.closures; ls; ls = ls.next)
|
adamc@703
|
277 freeClosure(ls.data);
|
adamc@692
|
278
|
adamc@899
|
279 for (var node = first; node && node != x; ) {
|
adamc@899
|
280 if (node.getElementsByTagName) {
|
adamc@899
|
281 var arr = node.getElementsByTagName("div");
|
adamc@899
|
282 for (var i = 0; i < arr.length; ++i) {
|
adamc@899
|
283 var span = arr[i];
|
adamc@899
|
284 span.dead = true;
|
adamc@899
|
285 for (var ls = span.sources; ls; ls = ls.next)
|
adamc@899
|
286 ls.data.dyns = remove(span, ls.data.dyns);
|
adamc@899
|
287 for (var ls = span.closures; ls; ls = ls.next)
|
adamc@899
|
288 freeClosure(ls.data);
|
adamc@899
|
289 }
|
adamc@847
|
290 }
|
adamc@847
|
291
|
adamc@899
|
292 var old = node;
|
adamc@899
|
293 node = node.nextSibling;
|
adamc@899
|
294 old.parentNode.removeChild(old);
|
adamc@899
|
295 }
|
adamc@690
|
296
|
adamc@703
|
297 var cls = {v : null};
|
adamc@899
|
298 var dummy = document.createElement("table");
|
adamc@899
|
299 dummy.innerHTML = flatten(cls, v);
|
adamc@703
|
300 x.closures = cls.v;
|
adamc@899
|
301 runScripts(dummy);
|
adamc@899
|
302
|
adamc@899
|
303 for (first = dummy.firstChild; first && first.tagName == "TBODY"; first = first.firstChild);
|
adamc@899
|
304 for (var node = first; node; ) {
|
adamc@899
|
305 var old = node;
|
adamc@899
|
306 node = node.nextSibling;
|
adamc@899
|
307 x.parentNode.insertBefore(old, x);
|
adamc@899
|
308 }
|
adamc@690
|
309 };
|
adamc@899
|
310
|
adamc@899
|
311 addNode(x);
|
adamc@690
|
312 populate(x);
|
adamc@571
|
313 }
|
adamc@582
|
314
|
adamc@817
|
315 function input(t, s, recreate) {
|
adamc@598
|
316 var x = document.createElement(t);
|
adamc@690
|
317 x.dead = false;
|
adamc@690
|
318 x.signal = ss(s);
|
adamc@690
|
319 x.sources = null;
|
adamc@817
|
320 x.recreate = recreate(x);
|
adamc@899
|
321 addNode(x);
|
adamc@690
|
322 populate(x);
|
adamc@816
|
323
|
adamc@816
|
324 return x;
|
adamc@816
|
325 }
|
adamc@816
|
326
|
adamc@816
|
327 function inp(s) {
|
adamc@817
|
328 var x = input("input", s, function(x) { return function(v) { if (x.value != v) x.value = v; }; });
|
adamc@816
|
329 x.value = s.data;
|
adamc@816
|
330 x.onkeyup = function() { sv(s, x.value) };
|
adamc@816
|
331
|
adamc@816
|
332 return x;
|
adamc@816
|
333 }
|
adamc@816
|
334
|
adamc@816
|
335 function sel(s, content) {
|
adamc@817
|
336 var x = input("select", s, function(x) { return function(v) { if (x.value != v) x.value = v; }; });
|
adamc@816
|
337 x.innerHTML = content;
|
adamc@816
|
338 x.value = s.data;
|
adamc@835
|
339 if (x.value != s.data)
|
adamc@835
|
340 sv(s, x.value);
|
adamc@816
|
341 x.onchange = function() { sv(s, x.value) };
|
adamc@797
|
342
|
adamc@606
|
343 return x;
|
adamc@598
|
344 }
|
adamc@598
|
345
|
adamc@817
|
346 function chk(s) {
|
adamc@817
|
347 var x = input("input", s, function(x) { return function(v) { if (x.checked != v) x.checked = v; }; });
|
adamc@817
|
348 x.type = "checkbox";
|
adamc@817
|
349 x.checked = s.data;
|
adamc@817
|
350 x.onchange = function() { sv(s, x.checked) };
|
adamc@817
|
351
|
adamc@817
|
352 return x;
|
adamc@817
|
353 }
|
adamc@817
|
354
|
adamc@800
|
355 function addOnChange(x, f) {
|
adamc@800
|
356 var old = x.onchange;
|
adamc@800
|
357 x.onchange = function() { old(); f (); };
|
adamc@800
|
358 }
|
adamc@800
|
359
|
adamc@692
|
360
|
adamc@692
|
361 // Basic string operations
|
adamc@692
|
362
|
adamc@597
|
363 function eh(x) {
|
adamc@800
|
364 if (x == null)
|
adamc@800
|
365 return "NULL";
|
adamc@800
|
366 else
|
adamc@800
|
367 return x.split("&").join("&").split("<").join("<").split(">").join(">");
|
adamc@597
|
368 }
|
adamc@597
|
369
|
adamc@582
|
370 function ts(x) { return x.toString() }
|
adamc@586
|
371 function bs(b) { return (b ? "True" : "False") }
|
adamc@586
|
372
|
adamc@828
|
373 function sub(s, i) { return s[i]; }
|
adamc@828
|
374 function suf(s, i) { return s.substring(i); }
|
adamc@828
|
375 function slen(s) { return s.length; }
|
adamc@829
|
376 function sidx(s, ch) {
|
adamc@829
|
377 var r = s.indexOf(ch);
|
adamc@829
|
378 if (r == -1)
|
adamc@829
|
379 return null;
|
adamc@829
|
380 else
|
adamc@829
|
381 return r;
|
adamc@829
|
382 }
|
adamc@831
|
383 function sspn(s, chs) {
|
adamc@831
|
384 for (var i = 0; i < s.length; ++i)
|
adamc@831
|
385 if (chs.indexOf(s[i]) != -1)
|
adamc@831
|
386 return i;
|
adamc@831
|
387
|
adamc@831
|
388 return null;
|
adamc@831
|
389 }
|
adamc@829
|
390 function schr(s, ch) {
|
adamc@829
|
391 var r = s.indexOf(ch);
|
adamc@829
|
392 if (r == -1)
|
adamc@829
|
393 return null;
|
adamc@829
|
394 else
|
adamc@829
|
395 return s.substring(r);
|
adamc@829
|
396 }
|
adamc@829
|
397 function ssub(s, start, len) {
|
adamc@829
|
398 return s.substring(start, start+len);
|
adamc@829
|
399 }
|
adamc@821
|
400
|
adamc@649
|
401 function pi(s) {
|
adamc@649
|
402 var r = parseInt(s);
|
adamc@649
|
403 if (r.toString() == s)
|
adamc@649
|
404 return r;
|
adamc@649
|
405 else
|
adamc@729
|
406 er("Can't parse int: " + s);
|
adamc@649
|
407 }
|
adamc@649
|
408
|
adamc@649
|
409 function pfl(s) {
|
adamc@649
|
410 var r = parseFloat(s);
|
adamc@649
|
411 if (r.toString() == s)
|
adamc@649
|
412 return r;
|
adamc@649
|
413 else
|
adamc@729
|
414 er("Can't parse float: " + s);
|
adamc@649
|
415 }
|
adamc@649
|
416
|
adamc@838
|
417 function pio(s) {
|
adamc@838
|
418 var r = parseInt(s);
|
adamc@838
|
419 if (r.toString() == s)
|
adamc@838
|
420 return r;
|
adamc@838
|
421 else
|
adamc@838
|
422 return null;
|
adamc@838
|
423 }
|
adamc@838
|
424
|
adamc@838
|
425 function pflo(s) {
|
adamc@838
|
426 var r = parseFloat(s);
|
adamc@838
|
427 if (r.toString() == s)
|
adamc@838
|
428 return r;
|
adamc@838
|
429 else
|
adamc@838
|
430 return null;
|
adamc@838
|
431 }
|
adamc@838
|
432
|
adamc@692
|
433 function uf(s) {
|
adamc@889
|
434 return escape(s).replace(new RegExp ("/", "g"), "%2F").replace(new RegExp ("\\+", "g"), "%2B");
|
adamc@691
|
435 }
|
adamc@691
|
436
|
adamc@692
|
437 function uu(s) {
|
adamc@889
|
438 return unescape(s);
|
adamc@692
|
439 }
|
adamc@692
|
440
|
adamc@692
|
441
|
adamc@603
|
442
|
adamc@692
|
443 // Remote calls
|
adamc@609
|
444
|
adamc@703
|
445 var client_id = null;
|
adamc@668
|
446 var client_pass = 0;
|
adamc@668
|
447 var url_prefix = "/";
|
adamc@673
|
448 var timeout = 60;
|
adamc@668
|
449
|
adamc@668
|
450 function getXHR(uri)
|
adamc@609
|
451 {
|
adamc@609
|
452 try {
|
adamc@609
|
453 return new XMLHttpRequest();
|
adamc@609
|
454 } catch (e) {
|
adamc@609
|
455 try {
|
adamc@609
|
456 return new ActiveXObject("Msxml2.XMLHTTP");
|
adamc@609
|
457 } catch (e) {
|
adamc@609
|
458 try {
|
adamc@609
|
459 return new ActiveXObject("Microsoft.XMLHTTP");
|
adamc@609
|
460 } catch (e) {
|
adamc@609
|
461 throw "Your browser doesn't seem to support AJAX.";
|
adamc@609
|
462 }
|
adamc@609
|
463 }
|
adamc@609
|
464 }
|
adamc@609
|
465 }
|
adamc@609
|
466
|
adamc@736
|
467 var sig = null;
|
adamc@736
|
468
|
adamc@736
|
469 function requestUri(xhr, uri, needsSig) {
|
adamc@668
|
470 xhr.open("GET", uri, true);
|
adamc@668
|
471
|
adamc@703
|
472 if (client_id != null) {
|
adamc@668
|
473 xhr.setRequestHeader("UrWeb-Client", client_id.toString());
|
adamc@668
|
474 xhr.setRequestHeader("UrWeb-Pass", client_pass.toString());
|
adamc@668
|
475 }
|
adamc@668
|
476
|
adamc@736
|
477 if (needsSig) {
|
adamc@736
|
478 if (sig == null)
|
adamc@736
|
479 whine("Missing cookie signature!");
|
adamc@736
|
480
|
adamc@736
|
481 xhr.setRequestHeader("UrWeb-Sig", sig);
|
adamc@736
|
482 }
|
adamc@736
|
483
|
adamc@668
|
484 xhr.send(null);
|
adamc@668
|
485 }
|
adamc@668
|
486
|
adamc@736
|
487 function rc(uri, parse, k, needsSig) {
|
adamc@728
|
488 uri = flattenLocal(uri);
|
adamc@609
|
489 var xhr = getXHR();
|
adamc@609
|
490
|
adamc@609
|
491 xhr.onreadystatechange = function() {
|
adamc@612
|
492 if (xhr.readyState == 4) {
|
adamc@612
|
493 var isok = false;
|
adamc@612
|
494
|
adamc@612
|
495 try {
|
adamc@612
|
496 if (xhr.status == 200)
|
adamc@612
|
497 isok = true;
|
adamc@612
|
498 } catch (e) { }
|
adamc@612
|
499
|
adamc@729
|
500 if (isok) {
|
adamc@729
|
501 try {
|
adamc@729
|
502 k(parse(xhr.responseText));
|
adamc@729
|
503 } catch (v) {
|
adamc@729
|
504 doExn(v);
|
adamc@729
|
505 }
|
adamc@729
|
506 } else {
|
adamc@729
|
507 conn();
|
adamc@649
|
508 }
|
adamc@612
|
509 }
|
adamc@609
|
510 };
|
adamc@609
|
511
|
adamc@736
|
512 requestUri(xhr, uri, needsSig);
|
adamc@609
|
513 }
|
adamc@667
|
514
|
adamc@667
|
515 function path_join(s1, s2) {
|
adamc@667
|
516 if (s1.length > 0 && s1[s1.length-1] == '/')
|
adamc@667
|
517 return s1 + s2;
|
adamc@667
|
518 else
|
adamc@667
|
519 return s1 + "/" + s2;
|
adamc@667
|
520 }
|
adamc@667
|
521
|
adamc@670
|
522 var channels = [];
|
adamc@670
|
523
|
adamc@670
|
524 function newQueue() {
|
adamc@670
|
525 return { front : null, back : null };
|
adamc@670
|
526 }
|
adamc@670
|
527 function enqueue(q, v) {
|
adamc@670
|
528 if (q.front == null) {
|
adamc@670
|
529 q.front = cons(v, null);
|
adamc@670
|
530 q.back = q.front;
|
adamc@670
|
531 } else {
|
adamc@670
|
532 var node = cons(v, null);
|
adamc@690
|
533 q.back.next = node;
|
adamc@670
|
534 q.back = node;
|
adamc@670
|
535 }
|
adamc@670
|
536 }
|
adamc@670
|
537 function dequeue(q) {
|
adamc@670
|
538 if (q.front == null)
|
adamc@670
|
539 return null;
|
adamc@670
|
540 else {
|
adamc@690
|
541 var r = q.front.data;
|
adamc@690
|
542 q.front = q.front.next;
|
adamc@670
|
543 if (q.front == null)
|
adamc@670
|
544 q.back = null;
|
adamc@670
|
545 return r;
|
adamc@670
|
546 }
|
adamc@670
|
547 }
|
adamc@670
|
548
|
adamc@670
|
549 function newChannel() {
|
adamc@670
|
550 return { msgs : newQueue(), listeners : newQueue() };
|
adamc@670
|
551 }
|
adamc@670
|
552
|
adamc@667
|
553 function listener() {
|
adamc@668
|
554 var uri = path_join(url_prefix, ".msgs");
|
adamc@667
|
555 var xhr = getXHR();
|
adamc@673
|
556 var tid, orsc, onTimeout;
|
adamc@673
|
557
|
adamc@673
|
558 var connect = function () {
|
adamc@673
|
559 xhr.onreadystatechange = orsc;
|
adamc@673
|
560 tid = window.setTimeout(onTimeout, timeout * 500);
|
adamc@736
|
561 requestUri(xhr, uri, false);
|
adamc@673
|
562 }
|
adamc@673
|
563
|
adamc@673
|
564 orsc = function() {
|
adamc@667
|
565 if (xhr.readyState == 4) {
|
adamc@673
|
566 window.clearTimeout(tid);
|
adamc@673
|
567
|
adamc@667
|
568 var isok = false;
|
adamc@667
|
569
|
adamc@667
|
570 try {
|
adamc@667
|
571 if (xhr.status == 200)
|
adamc@667
|
572 isok = true;
|
adamc@667
|
573 } catch (e) { }
|
adamc@667
|
574
|
adamc@668
|
575 if (isok) {
|
adamc@889
|
576 var text = xhr.responseText
|
adamc@889
|
577 if (text == "")
|
adamc@889
|
578 return;
|
adamc@889
|
579 var lines = text.split("\n");
|
adamc@889
|
580
|
adamc@729
|
581 if (lines.length < 2) {
|
adamc@729
|
582 discon();
|
adamc@729
|
583 return;
|
adamc@729
|
584 }
|
adamc@669
|
585
|
adamc@669
|
586 for (var i = 0; i+1 < lines.length; i += 2) {
|
adamc@670
|
587 var chn = lines[i];
|
adamc@670
|
588 var msg = lines[i+1];
|
adamc@670
|
589
|
adamc@670
|
590 if (chn < 0)
|
adamc@670
|
591 whine("Out-of-bounds channel in message from remote server");
|
adamc@670
|
592
|
adamc@670
|
593 var ch;
|
adamc@670
|
594
|
adamc@670
|
595 if (chn >= channels.length || channels[chn] == null) {
|
adamc@670
|
596 ch = newChannel();
|
adamc@670
|
597 channels[chn] = ch;
|
adamc@670
|
598 } else
|
adamc@670
|
599 ch = channels[chn];
|
adamc@670
|
600
|
adamc@670
|
601 var listener = dequeue(ch.listeners);
|
adamc@670
|
602 if (listener == null) {
|
adamc@670
|
603 enqueue(ch.msgs, msg);
|
adamc@670
|
604 } else {
|
adamc@728
|
605 try {
|
adamc@728
|
606 listener(msg);
|
adamc@728
|
607 } catch (v) {
|
adamc@728
|
608 doExn(v);
|
adamc@728
|
609 }
|
adamc@670
|
610 }
|
adamc@669
|
611 }
|
adamc@669
|
612
|
adamc@673
|
613 connect();
|
adamc@668
|
614 }
|
adamc@667
|
615 else {
|
adamc@729
|
616 try {
|
adamc@824
|
617 if (xhr.status != 0)
|
adamc@824
|
618 servErr("Error querying remote server for messages: " + xhr.status);
|
adamc@889
|
619 } catch (e) { }
|
adamc@667
|
620 }
|
adamc@667
|
621 }
|
adamc@667
|
622 };
|
adamc@667
|
623
|
adamc@673
|
624 onTimeout = function() {
|
adamc@673
|
625 xhr.abort();
|
adamc@673
|
626 connect();
|
adamc@673
|
627 };
|
adamc@673
|
628
|
adamc@673
|
629 connect();
|
adamc@667
|
630 }
|
adamc@670
|
631
|
adamc@670
|
632 function rv(chn, parse, k) {
|
adamc@682
|
633 if (chn == null)
|
adamc@682
|
634 return;
|
adamc@682
|
635
|
adamc@670
|
636 if (chn < 0)
|
adamc@670
|
637 whine("Out-of-bounds channel receive");
|
adamc@670
|
638
|
adamc@670
|
639 var ch;
|
adamc@670
|
640
|
adamc@670
|
641 if (chn >= channels.length || channels[chn] == null) {
|
adamc@670
|
642 ch = newChannel();
|
adamc@670
|
643 channels[chn] = ch;
|
adamc@670
|
644 } else
|
adamc@670
|
645 ch = channels[chn];
|
adamc@670
|
646
|
adamc@670
|
647 var msg = dequeue(ch.msgs);
|
adamc@670
|
648 if (msg == null) {
|
adamc@670
|
649 enqueue(ch.listeners, function(msg) { k(parse(msg))(null); });
|
adamc@670
|
650 } else {
|
adamc@728
|
651 try {
|
adamc@728
|
652 k(parse(msg))(null);
|
adamc@728
|
653 } catch (v) {
|
adamc@728
|
654 doExn(v);
|
adamc@728
|
655 }
|
adamc@670
|
656 }
|
adamc@670
|
657 }
|
adamc@693
|
658
|
adamc@693
|
659
|
adamc@895
|
660 // Key events
|
adamc@895
|
661
|
adamc@895
|
662 function kc(e) {
|
adamc@895
|
663 return window.event ? e.keyCode : e.which;
|
adamc@895
|
664 }
|
adamc@895
|
665
|
adamc@895
|
666
|
adamc@693
|
667 // App-specific code
|
adamc@693
|
668
|