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