annotate lib/js/urweb.js @ 964:fbc3a0eef45a

Paging mostly working; just need to get it working properly with filtering
author Adam Chlipala <adamc@hcoop.net>
date Sat, 19 Sep 2009 14:21:25 -0400
parents 8c37699de273
children 1b80b9df0d32
rev   line source
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@901 255 thisScript.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@901 265 function dyn(pnode, s) {
adamc@901 266 var x = document.createElement("script");
adamc@690 267 x.dead = false;
adamc@690 268 x.signal = s;
adamc@690 269 x.sources = null;
adamc@703 270 x.closures = null;
adamc@899 271
adamc@902 272 var firstChild = null;
adamc@899 273
adamc@690 274 x.recreate = function(v) {
adamc@703 275 for (var ls = x.closures; ls; ls = ls.next)
adamc@703 276 freeClosure(ls.data);
adamc@692 277
adamc@902 278 var next;
adamc@902 279 for (var child = firstChild; child && child != x; child = next) {
adamc@902 280 next = child.nextSibling;
adamc@901 281 if (child.getElementsByTagName) {
adamc@901 282 var arr = child.getElementsByTagName("script");
adamc@899 283 for (var i = 0; i < arr.length; ++i) {
adamc@899 284 var span = arr[i];
adamc@899 285 span.dead = true;
adamc@899 286 for (var ls = span.sources; ls; ls = ls.next)
adamc@899 287 ls.data.dyns = remove(span, ls.data.dyns);
adamc@899 288 for (var ls = span.closures; ls; ls = ls.next)
adamc@899 289 freeClosure(ls.data);
adamc@899 290 }
adamc@847 291 }
adamc@847 292
adamc@901 293 if (child.parentNode)
adamc@901 294 child.parentNode.removeChild(child);
adamc@899 295 }
adamc@690 296
adamc@703 297 var cls = {v : null};
adamc@901 298 var html = flatten(cls, v);
adamc@703 299 x.closures = cls.v;
adamc@899 300
adamc@901 301 if (pnode == "table") {
adamc@901 302 var dummy = document.createElement("body");
adamc@901 303 dummy.innerHTML = "<table>" + html + "</table>";
adamc@901 304 runScripts(dummy);
adamc@901 305 var table = x.parentNode;
adamc@901 306
adamc@946 307 if (table) {
adamc@946 308 var arr = dummy.getElementsByTagName("tbody");
adamc@946 309 firstChild = null;
adamc@946 310 var tbody;
adamc@946 311 if (arr.length > 0 && table != null)
adamc@946 312 tbody = arr[0];
adamc@946 313 else
adamc@946 314 tbody = dummy.firstChild;
adamc@945 315
adamc@946 316 var next;
adamc@946 317 firstChild = document.createElement("script");
adamc@946 318 table.insertBefore(firstChild, x);
adamc@946 319 for (var node = tbody.firstChild; node; node = next) {
adamc@946 320 next = node.nextSibling;
adamc@946 321 table.insertBefore(node, x);
adamc@946 322 }
adamc@901 323 }
adamc@903 324 } else if (pnode == "tr") {
adamc@903 325 var dummy = document.createElement("body");
adamc@903 326 dummy.innerHTML = "<table><tr>" + html + "</tr></table>";
adamc@903 327 runScripts(dummy);
adamc@903 328 var table = x.parentNode;
adamc@903 329
adamc@946 330 if (table) {
adamc@946 331 var arr = dummy.getElementsByTagName("tr");
adamc@946 332 firstChild = null;
adamc@946 333 var tbody;
adamc@946 334 if (arr.length > 0 && table != null)
adamc@946 335 tbody = arr[0];
adamc@946 336 else
adamc@946 337 tbody = dummy.firstChild;
adamc@946 338
adamc@946 339 var next;
adamc@903 340 firstChild = document.createElement("script");
adamc@903 341 table.insertBefore(firstChild, x);
adamc@903 342 for (var node = tbody.firstChild; node; node = next) {
adamc@903 343 next = node.nextSibling;
adamc@903 344 table.insertBefore(node, x);
adamc@903 345 }
adamc@903 346 }
adamc@901 347 } else {
adamc@902 348 firstChild = document.createElement("span");
adamc@902 349 firstChild.innerHTML = html;
adamc@902 350 runScripts(firstChild);
adamc@900 351 if (x.parentNode)
adamc@902 352 x.parentNode.insertBefore(firstChild, x);
adamc@899 353 }
adamc@690 354 };
adamc@899 355
adamc@899 356 addNode(x);
adamc@690 357 populate(x);
adamc@571 358 }
adamc@582 359
adamc@817 360 function input(t, s, recreate) {
adamc@598 361 var x = document.createElement(t);
adamc@690 362 x.dead = false;
adamc@690 363 x.signal = ss(s);
adamc@690 364 x.sources = null;
adamc@817 365 x.recreate = recreate(x);
adamc@899 366 addNode(x);
adamc@690 367 populate(x);
adamc@816 368
adamc@816 369 return x;
adamc@816 370 }
adamc@816 371
adamc@816 372 function inp(s) {
adamc@817 373 var x = input("input", s, function(x) { return function(v) { if (x.value != v) x.value = v; }; });
adamc@816 374 x.value = s.data;
adamc@816 375 x.onkeyup = function() { sv(s, x.value) };
adamc@816 376
adamc@816 377 return x;
adamc@816 378 }
adamc@816 379
adamc@816 380 function sel(s, content) {
adamc@817 381 var x = input("select", s, function(x) { return function(v) { if (x.value != v) x.value = v; }; });
adamc@816 382 x.innerHTML = content;
adamc@816 383 x.value = s.data;
adamc@835 384 if (x.value != s.data)
adamc@835 385 sv(s, x.value);
adamc@816 386 x.onchange = function() { sv(s, x.value) };
adamc@797 387
adamc@606 388 return x;
adamc@598 389 }
adamc@598 390
adamc@817 391 function chk(s) {
adamc@817 392 var x = input("input", s, function(x) { return function(v) { if (x.checked != v) x.checked = v; }; });
adamc@817 393 x.type = "checkbox";
adamc@817 394 x.checked = s.data;
adamc@817 395 x.onchange = function() { sv(s, x.checked) };
adamc@817 396
adamc@817 397 return x;
adamc@817 398 }
adamc@817 399
adamc@800 400 function addOnChange(x, f) {
adamc@800 401 var old = x.onchange;
adamc@800 402 x.onchange = function() { old(); f (); };
adamc@800 403 }
adamc@800 404
adamc@692 405
adamc@692 406 // Basic string operations
adamc@692 407
adamc@597 408 function eh(x) {
adamc@800 409 if (x == null)
adamc@800 410 return "NULL";
adamc@800 411 else
adamc@800 412 return x.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
adamc@597 413 }
adamc@597 414
adamc@582 415 function ts(x) { return x.toString() }
adamc@586 416 function bs(b) { return (b ? "True" : "False") }
adamc@586 417
adamc@828 418 function sub(s, i) { return s[i]; }
adamc@828 419 function suf(s, i) { return s.substring(i); }
adamc@828 420 function slen(s) { return s.length; }
adamc@829 421 function sidx(s, ch) {
adamc@829 422 var r = s.indexOf(ch);
adamc@829 423 if (r == -1)
adamc@829 424 return null;
adamc@829 425 else
adamc@829 426 return r;
adamc@829 427 }
adamc@831 428 function sspn(s, chs) {
adamc@831 429 for (var i = 0; i < s.length; ++i)
adamc@831 430 if (chs.indexOf(s[i]) != -1)
adamc@831 431 return i;
adamc@831 432
adamc@831 433 return null;
adamc@831 434 }
adamc@829 435 function schr(s, ch) {
adamc@829 436 var r = s.indexOf(ch);
adamc@829 437 if (r == -1)
adamc@829 438 return null;
adamc@829 439 else
adamc@829 440 return s.substring(r);
adamc@829 441 }
adamc@829 442 function ssub(s, start, len) {
adamc@829 443 return s.substring(start, start+len);
adamc@829 444 }
adamc@821 445
adamc@649 446 function pi(s) {
adamc@649 447 var r = parseInt(s);
adamc@649 448 if (r.toString() == s)
adamc@649 449 return r;
adamc@649 450 else
adamc@729 451 er("Can't parse int: " + s);
adamc@649 452 }
adamc@649 453
adamc@649 454 function pfl(s) {
adamc@649 455 var r = parseFloat(s);
adamc@649 456 if (r.toString() == s)
adamc@649 457 return r;
adamc@649 458 else
adamc@729 459 er("Can't parse float: " + s);
adamc@649 460 }
adamc@649 461
adamc@838 462 function pio(s) {
adamc@838 463 var r = parseInt(s);
adamc@838 464 if (r.toString() == s)
adamc@838 465 return r;
adamc@838 466 else
adamc@838 467 return null;
adamc@838 468 }
adamc@838 469
adamc@838 470 function pflo(s) {
adamc@838 471 var r = parseFloat(s);
adamc@838 472 if (r.toString() == s)
adamc@838 473 return r;
adamc@838 474 else
adamc@838 475 return null;
adamc@838 476 }
adamc@838 477
adamc@692 478 function uf(s) {
adamc@925 479 if (s.length == 0)
adamc@925 480 return "_";
adamc@925 481 return (s[0] == '_' ? "_" : "")
adamc@925 482 + escape(s).replace(new RegExp ("/", "g"), "%2F").replace(new RegExp ("\\+", "g"), "%2B");
adamc@691 483 }
adamc@691 484
adamc@692 485 function uu(s) {
adamc@925 486 if (s.length > 0 && s[0] == '_')
adamc@925 487 s = s.substring(1);
adamc@927 488 else if (s.length >= 3 && s[0] == '%' && s[1] == '5' && (s[2] == 'f' || s[2] == 'F'))
adamc@927 489 s = s.substring(3);
adamc@905 490 return unescape(s.replace(new RegExp ("\\+", "g"), " "));
adamc@905 491 }
adamc@905 492
adamc@912 493 function ub(b) {
adamc@912 494 return b ? "1" : "0";
adamc@912 495 }
adamc@912 496
adamc@905 497 function uul(getToken, getData) {
adamc@905 498 var tok = getToken();
adamc@905 499 if (tok == "Nil") {
adamc@905 500 return null;
adamc@905 501 } else if (tok == "Cons") {
adamc@905 502 var d = getData();
adamc@905 503 var l = uul(getToken, getData);
adamc@905 504 return {_1:d, _2:l};
adamc@905 505 } else
adamc@905 506 throw ("Can't unmarshal list (" + tok + ")");
adamc@692 507 }
adamc@692 508
adamc@961 509 function strcmp(str1, str2) {
adamc@961 510 return ((str1 == str2) ? 0 : ((str1 > str2) ? 1 : -1));
adamc@961 511 }
adamc@692 512
adamc@603 513
adamc@692 514 // Remote calls
adamc@609 515
adamc@703 516 var client_id = null;
adamc@668 517 var client_pass = 0;
adamc@668 518 var url_prefix = "/";
adamc@673 519 var timeout = 60;
adamc@668 520
adamc@668 521 function getXHR(uri)
adamc@609 522 {
adamc@609 523 try {
adamc@609 524 return new XMLHttpRequest();
adamc@609 525 } catch (e) {
adamc@609 526 try {
adamc@609 527 return new ActiveXObject("Msxml2.XMLHTTP");
adamc@609 528 } catch (e) {
adamc@609 529 try {
adamc@609 530 return new ActiveXObject("Microsoft.XMLHTTP");
adamc@609 531 } catch (e) {
adamc@609 532 throw "Your browser doesn't seem to support AJAX.";
adamc@609 533 }
adamc@609 534 }
adamc@609 535 }
adamc@609 536 }
adamc@609 537
adamc@736 538 var sig = null;
adamc@736 539
adamc@736 540 function requestUri(xhr, uri, needsSig) {
adamc@668 541 xhr.open("GET", uri, true);
adamc@668 542
adamc@703 543 if (client_id != null) {
adamc@668 544 xhr.setRequestHeader("UrWeb-Client", client_id.toString());
adamc@668 545 xhr.setRequestHeader("UrWeb-Pass", client_pass.toString());
adamc@668 546 }
adamc@668 547
adamc@736 548 if (needsSig) {
adamc@736 549 if (sig == null)
adamc@736 550 whine("Missing cookie signature!");
adamc@736 551
adamc@736 552 xhr.setRequestHeader("UrWeb-Sig", sig);
adamc@736 553 }
adamc@736 554
adamc@668 555 xhr.send(null);
adamc@668 556 }
adamc@668 557
adamc@736 558 function rc(uri, parse, k, needsSig) {
adamc@728 559 uri = flattenLocal(uri);
adamc@609 560 var xhr = getXHR();
adamc@609 561
adamc@609 562 xhr.onreadystatechange = function() {
adamc@612 563 if (xhr.readyState == 4) {
adamc@612 564 var isok = false;
adamc@612 565
adamc@612 566 try {
adamc@612 567 if (xhr.status == 200)
adamc@612 568 isok = true;
adamc@612 569 } catch (e) { }
adamc@612 570
adamc@729 571 if (isok) {
adamc@729 572 try {
adamc@729 573 k(parse(xhr.responseText));
adamc@729 574 } catch (v) {
adamc@729 575 doExn(v);
adamc@729 576 }
adamc@729 577 } else {
adamc@729 578 conn();
adamc@649 579 }
adamc@612 580 }
adamc@609 581 };
adamc@609 582
adamc@736 583 requestUri(xhr, uri, needsSig);
adamc@609 584 }
adamc@667 585
adamc@667 586 function path_join(s1, s2) {
adamc@667 587 if (s1.length > 0 && s1[s1.length-1] == '/')
adamc@667 588 return s1 + s2;
adamc@667 589 else
adamc@667 590 return s1 + "/" + s2;
adamc@667 591 }
adamc@667 592
adamc@670 593 var channels = [];
adamc@670 594
adamc@670 595 function newQueue() {
adamc@670 596 return { front : null, back : null };
adamc@670 597 }
adamc@670 598 function enqueue(q, v) {
adamc@670 599 if (q.front == null) {
adamc@670 600 q.front = cons(v, null);
adamc@670 601 q.back = q.front;
adamc@670 602 } else {
adamc@670 603 var node = cons(v, null);
adamc@690 604 q.back.next = node;
adamc@670 605 q.back = node;
adamc@670 606 }
adamc@670 607 }
adamc@670 608 function dequeue(q) {
adamc@670 609 if (q.front == null)
adamc@670 610 return null;
adamc@670 611 else {
adamc@690 612 var r = q.front.data;
adamc@690 613 q.front = q.front.next;
adamc@670 614 if (q.front == null)
adamc@670 615 q.back = null;
adamc@670 616 return r;
adamc@670 617 }
adamc@670 618 }
adamc@670 619
adamc@670 620 function newChannel() {
adamc@670 621 return { msgs : newQueue(), listeners : newQueue() };
adamc@670 622 }
adamc@670 623
adamc@667 624 function listener() {
adamc@668 625 var uri = path_join(url_prefix, ".msgs");
adamc@667 626 var xhr = getXHR();
adamc@673 627 var tid, orsc, onTimeout;
adamc@673 628
adamc@673 629 var connect = function () {
adamc@673 630 xhr.onreadystatechange = orsc;
adamc@673 631 tid = window.setTimeout(onTimeout, timeout * 500);
adamc@736 632 requestUri(xhr, uri, false);
adamc@673 633 }
adamc@673 634
adamc@673 635 orsc = function() {
adamc@667 636 if (xhr.readyState == 4) {
adamc@673 637 window.clearTimeout(tid);
adamc@673 638
adamc@667 639 var isok = false;
adamc@667 640
adamc@667 641 try {
adamc@667 642 if (xhr.status == 200)
adamc@667 643 isok = true;
adamc@667 644 } catch (e) { }
adamc@667 645
adamc@668 646 if (isok) {
adamc@889 647 var text = xhr.responseText
adamc@889 648 if (text == "")
adamc@889 649 return;
adamc@889 650 var lines = text.split("\n");
adamc@889 651
adamc@729 652 if (lines.length < 2) {
adamc@729 653 discon();
adamc@729 654 return;
adamc@729 655 }
adamc@669 656
adamc@669 657 for (var i = 0; i+1 < lines.length; i += 2) {
adamc@670 658 var chn = lines[i];
adamc@670 659 var msg = lines[i+1];
adamc@670 660
adamc@670 661 if (chn < 0)
adamc@670 662 whine("Out-of-bounds channel in message from remote server");
adamc@670 663
adamc@670 664 var ch;
adamc@670 665
adamc@670 666 if (chn >= channels.length || channels[chn] == null) {
adamc@670 667 ch = newChannel();
adamc@670 668 channels[chn] = ch;
adamc@670 669 } else
adamc@670 670 ch = channels[chn];
adamc@670 671
adamc@670 672 var listener = dequeue(ch.listeners);
adamc@670 673 if (listener == null) {
adamc@670 674 enqueue(ch.msgs, msg);
adamc@670 675 } else {
adamc@728 676 try {
adamc@728 677 listener(msg);
adamc@728 678 } catch (v) {
adamc@728 679 doExn(v);
adamc@728 680 }
adamc@670 681 }
adamc@669 682 }
adamc@669 683
adamc@673 684 connect();
adamc@668 685 }
adamc@667 686 else {
adamc@729 687 try {
adamc@824 688 if (xhr.status != 0)
adamc@824 689 servErr("Error querying remote server for messages: " + xhr.status);
adamc@889 690 } catch (e) { }
adamc@667 691 }
adamc@667 692 }
adamc@667 693 };
adamc@667 694
adamc@673 695 onTimeout = function() {
adamc@673 696 xhr.abort();
adamc@673 697 connect();
adamc@673 698 };
adamc@673 699
adamc@673 700 connect();
adamc@667 701 }
adamc@670 702
adamc@670 703 function rv(chn, parse, k) {
adamc@682 704 if (chn == null)
adamc@682 705 return;
adamc@682 706
adamc@670 707 if (chn < 0)
adamc@670 708 whine("Out-of-bounds channel receive");
adamc@670 709
adamc@670 710 var ch;
adamc@670 711
adamc@670 712 if (chn >= channels.length || channels[chn] == null) {
adamc@670 713 ch = newChannel();
adamc@670 714 channels[chn] = ch;
adamc@670 715 } else
adamc@670 716 ch = channels[chn];
adamc@670 717
adamc@670 718 var msg = dequeue(ch.msgs);
adamc@670 719 if (msg == null) {
adamc@670 720 enqueue(ch.listeners, function(msg) { k(parse(msg))(null); });
adamc@670 721 } else {
adamc@728 722 try {
adamc@728 723 k(parse(msg))(null);
adamc@728 724 } catch (v) {
adamc@728 725 doExn(v);
adamc@728 726 }
adamc@670 727 }
adamc@670 728 }
adamc@693 729
adamc@693 730
adamc@895 731 // Key events
adamc@895 732
adamc@895 733 function kc(e) {
adamc@895 734 return window.event ? e.keyCode : e.which;
adamc@895 735 }
adamc@895 736
adamc@895 737
adamc@693 738 // App-specific code
adamc@693 739