annotate lib/js/urweb.js @ 2306:a7cc6c29d836

New release
author Adam Chlipala <adam@chlipala.net>
date Sun, 20 Dec 2015 14:25:46 -0500
parents fc1c89627178
children
rev   line source
adamc@1036 1 // Detect browser quirks that we should be aware of.
adamc@1036 2
adamc@1036 3 function needsDynPrefix() {
adamc@1260 4 var span = document.createElement("span");
adamc@1260 5 span.innerHTML = "<script>alert('test');</script>";
adamc@1260 6 var scripts = span.getElementsByTagName("script");
adamc@1260 7 return scripts.length == 0;
adamc@1036 8 }
adamc@1036 9
adamc@1036 10 var dynPrefix = needsDynPrefix() ? "<span style=\"display:none\">A</span>" : "";
adamc@1036 11
adamc@970 12 // Function versions of operators
adamc@970 13
adamc@970 14 function not(x) { return !x; }
adamc@970 15 function neg(x) { return -x; }
adamc@970 16
adamc@970 17 function eq(x, y) { return x == y; }
adamc@970 18 function plus(x, y) { return x + y; }
adamc@970 19 function minus(x, y) { return x - y; }
adamc@970 20 function times(x, y) { return x * y; }
adamc@970 21 function div(x, y) { return x / y; }
adam@1619 22 function divInt(x, y) { if (y == 0) er("Division by zero"); var n = x / y; return n < 0 ? Math.ceil(n) : Math.floor(n); }
adamc@970 23 function mod(x, y) { return x % y; }
adam@1619 24 function modInt(x, y) { if (y == 0) er("Division by zero"); var n = x % y; return n < 0 ? Math.ceil(n) : Math.floor(n); }
adamc@970 25 function lt(x, y) { return x < y; }
adamc@970 26 function le(x, y) { return x <= y; }
adamc@970 27
adamc@1061 28 // Characters
adamc@1061 29
adamc@1061 30 function isLower(c) { return c >= 'a' && c <= 'z'; }
adamc@1061 31 function isUpper(c) { return c >= 'A' && c <= 'Z'; }
adamc@1061 32 function isAlpha(c) { return isLower(c) || isUpper(c); }
adamc@1061 33 function isDigit(c) { return c >= '0' && c <= '9'; }
adamc@1061 34 function isAlnum(c) { return isAlpha(c) || isDigit(c); }
adamc@1061 35 function isBlank(c) { return c == ' ' || c == '\t'; }
adamc@1061 36 function isSpace(c) { return isBlank(c) || c == '\r' || c == '\n'; }
adamc@1061 37 function isXdigit(c) { return isDigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); }
adam@1927 38 function ord(c) { return c.charCodeAt(0); }
adam@1927 39 function isPrint(c) { return ord(c) > 31 && ord(c) < 127; }
adamc@1177 40 function toLower(c) { return c.toLowerCase(); }
adamc@1177 41 function toUpper(c) { return c.toUpperCase(); }
adamc@1061 42
adamc@692 43 // Lists
adamc@692 44
adamc@580 45 function cons(v, ls) {
adamc@1260 46 return { next : ls, data : v };
adamc@690 47 }
adamc@969 48 function rev(ls) {
adamc@1260 49 var acc = null;
adamc@1260 50 for (; ls; ls = ls.next)
adam@1643 51 acc = cons(ls.data, acc);
adamc@1260 52 return acc;
adamc@969 53 }
adamc@690 54 function concat(ls1, ls2) {
adamc@1260 55 var acc = ls2;
adamc@1260 56 ls1 = rev(ls1);
adamc@1260 57 for (; ls1; ls1 = ls1.next)
adamc@1260 58 acc = cons(ls1.data, acc);
adamc@1260 59 return acc;
adamc@690 60 }
adamc@690 61 function member(x, ls) {
adamc@1260 62 for (; ls; ls = ls.next)
adamc@1260 63 if (ls.data == x)
adamc@1260 64 return true;
adamc@1260 65 return false;
adamc@690 66 }
adamc@690 67 function remove(x, ls) {
adamc@1260 68 var acc = null;
adamc@969 69
adamc@1260 70 for (; ls; ls = ls.next)
adamc@1260 71 if (ls.data == x)
adamc@1260 72 return concat(acc, ls.next);
adamc@1260 73 else
adamc@1260 74 acc = cons(ls.data, acc);
adamc@969 75
adamc@1260 76 return ls;
adamc@690 77 }
adamc@690 78 function union(ls1, ls2) {
adamc@1260 79 var acc = ls2;
adamc@969 80
adamc@1260 81 for (; ls1; ls1 = ls1.next)
adamc@1260 82 if (!member(ls1.data, ls2))
adamc@1260 83 acc = cons(ls1.data, acc);
adamc@969 84
adamc@1260 85 return acc;
adamc@580 86 }
adamc@703 87 function length(ls) {
adamc@1260 88 var acc = 0;
adamc@969 89
adamc@1260 90 for (; ls; ls = ls.next)
adamc@1260 91 ++acc;
adamc@969 92
adamc@1260 93 return acc;
adamc@703 94 }
adamc@670 95
adamc@690 96
adam@1571 97 // Floats
adam@1571 98
adam@1571 99 function float(n) {
adam@1571 100 return n;
adam@1571 101 }
adam@1571 102
adam@1571 103 function trunc(n) {
adam@1571 104 return ~~n;
adam@1571 105 }
adam@1571 106
adam@1571 107 function ceil(n) {
adam@1571 108 return Math.ceil(n);
adam@1571 109 }
adam@1571 110
adam@1571 111 function round(n) {
adam@1571 112 return Math.round(n);
adam@1571 113 }
adam@1571 114
adam@2110 115 function pow(n, m) {
adam@2110 116 return Math.pow(n, m);
adam@2110 117 }
adam@2110 118
adam@1571 119
adam@1599 120 // Time, represented as counts of microseconds since the epoch
adam@1487 121
adam@1629 122 var time_format = "%c";
adam@1629 123
adam@1487 124 function showTime(tm) {
adam@1629 125 return strftime(time_format, tm);
adam@1629 126 }
adam@1629 127
adam@1629 128 function showTimeHtml(tm) {
adam@1629 129 return eh(showTime(tm));
adam@1487 130 }
adam@1487 131
adam@1487 132 function now() {
adam@1487 133 return (new Date()).getTime() * 1000;
adam@1487 134 }
adam@1487 135
adam@1514 136 function diffInSeconds(tm1, tm2) {
adam@1514 137 return Math.round((tm2 - tm1) / 1000000);
adam@1514 138 }
adam@1514 139
adam@1685 140 function diffInMilliseconds(tm1, tm2) {
adam@1685 141 return Math.round((tm2 - tm1) / 1000);
adam@1685 142 }
adam@1685 143
adam@1514 144 function toSeconds(tm) {
adam@1514 145 return Math.round(tm / 1000000);
adam@1514 146 }
adam@1514 147
adam@1685 148 function toMilliseconds(tm) {
adam@1685 149 return Math.round(tm / 1000);
adam@1685 150 }
adam@1685 151
adam@2187 152 function fromMilliseconds(tm) {
adam@2187 153 return tm * 1000;
adam@2187 154 }
adam@2187 155
adam@1518 156 function addSeconds(tm, n) {
adam@1518 157 return tm + n * 1000000;
adam@1518 158 }
adam@1518 159
adam@1599 160 function stringToTime_error(string) {
adam@1599 161 var t = Date.parse(string);
adam@1599 162 if (isNaN(t))
adam@1632 163 er("Invalid date string: " + string);
adam@1599 164 else
adam@1599 165 return t * 1000;
adam@1599 166 }
adam@1599 167
adam@1599 168 function stringToTime(string) {
adam@1599 169 try {
adam@1599 170 var t = Date.parse(string);
adam@1599 171 if (isNaN(t))
adam@1599 172 return null;
adam@1599 173 else
adam@1599 174 return t * 1000;
adam@1599 175 } catch (e) {
adam@1599 176 return null;
adam@1599 177 }
rmbruijn@1597 178 }
rmbruijn@1597 179
adam@1609 180 /*
adam@1615 181 strftime() implementation from:
adam@1615 182 YUI 3.4.1 (build 4118)
adam@1615 183 Copyright 2011 Yahoo! Inc. All rights reserved.
adam@1615 184 Licensed under the BSD License.
adam@1615 185 http://yuilibrary.com/license/
adam@1615 186 */
adam@1609 187
adam@1615 188 var xPad=function (x, pad, r)
adam@1615 189 {
adam@1615 190 if(typeof r === "undefined")
adam@1615 191 {
adam@1615 192 r=10;
adam@1615 193 }
adam@1615 194 pad = pad.toString();
adam@1615 195 for( ; parseInt(x, 10)<r && r>1; r/=10) {
adam@1615 196 x = pad + x;
adam@1615 197 }
adam@1615 198 return x.toString();
adam@1615 199 };
adam@1609 200
adam@1615 201 var YDateEn = {
adam@1615 202 a: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
adam@1615 203 A: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
adam@1615 204 b: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
adam@1615 205 B: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
adam@1615 206 c: "%a %d %b %Y %T %Z",
adam@1615 207 p: ["AM", "PM"],
adam@1615 208 P: ["am", "pm"],
adam@1615 209 r: "%I:%M:%S %p",
adam@1615 210 x: "%d/%m/%y",
adam@1615 211 X: "%T"
adam@1615 212 };
adam@1609 213
adam@1615 214 var Dt = {
adam@1615 215 formats: {
adam@1615 216 a: function (d, l) { return l.a[d.getDay()]; },
adam@1615 217 A: function (d, l) { return l.A[d.getDay()]; },
adam@1615 218 b: function (d, l) { return l.b[d.getMonth()]; },
adam@1615 219 B: function (d, l) { return l.B[d.getMonth()]; },
adam@1615 220 C: function (d) { return xPad(parseInt(d.getFullYear()/100, 10), 0); },
adam@1615 221 d: ["getDate", "0"],
adam@1615 222 e: ["getDate", " "],
adam@1615 223 g: function (d) { return xPad(parseInt(Dt.formats.G(d)%100, 10), 0); },
adam@1615 224 G: function (d) {
adam@1615 225 var y = d.getFullYear();
adam@1615 226 var V = parseInt(Dt.formats.V(d), 10);
adam@1615 227 var W = parseInt(Dt.formats.W(d), 10);
phurst@1986 228
adam@1615 229 if(W > V) {
adam@1615 230 y++;
adam@1615 231 } else if(W===0 && V>=52) {
adam@1615 232 y--;
adam@1615 233 }
phurst@1986 234
adam@1615 235 return y;
adam@1615 236 },
adam@1615 237 H: ["getHours", "0"],
adam@1615 238 I: function (d) { var I=d.getHours()%12; return xPad(I===0?12:I, 0); },
adam@1615 239 j: function (d) {
adam@1615 240 var gmd_1 = new Date("" + d.getFullYear() + "/1/1 GMT");
adam@1615 241 var gmdate = new Date("" + d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate() + " GMT");
adam@1615 242 var ms = gmdate - gmd_1;
adam@1615 243 var doy = parseInt(ms/60000/60/24, 10)+1;
adam@1615 244 return xPad(doy, 0, 100);
adam@1615 245 },
adam@1615 246 k: ["getHours", " "],
adam@1615 247 l: function (d) { var I=d.getHours()%12; return xPad(I===0?12:I, " "); },
adam@1615 248 m: function (d) { return xPad(d.getMonth()+1, 0); },
adam@1615 249 M: ["getMinutes", "0"],
adam@1615 250 p: function (d, l) { return l.p[d.getHours() >= 12 ? 1 : 0 ]; },
adam@1615 251 P: function (d, l) { return l.P[d.getHours() >= 12 ? 1 : 0 ]; },
adam@1615 252 s: function (d, l) { return parseInt(d.getTime()/1000, 10); },
adam@1615 253 S: ["getSeconds", "0"],
adam@1615 254 u: function (d) { var dow = d.getDay(); return dow===0?7:dow; },
adam@1615 255 U: function (d) {
adam@1615 256 var doy = parseInt(Dt.formats.j(d), 10);
adam@1615 257 var rdow = 6-d.getDay();
adam@1615 258 var woy = parseInt((doy+rdow)/7, 10);
adam@1615 259 return xPad(woy, 0);
adam@1615 260 },
adam@1615 261 V: function (d) {
adam@1615 262 var woy = parseInt(Dt.formats.W(d), 10);
adam@1615 263 var dow1_1 = (new Date("" + d.getFullYear() + "/1/1")).getDay();
adam@1615 264 var idow = woy + (dow1_1 > 4 || dow1_1 <= 1 ? 0 : 1);
adam@1615 265 if(idow === 53 && (new Date("" + d.getFullYear() + "/12/31")).getDay() < 4)
adam@1615 266 {
adam@1615 267 idow = 1;
adam@1615 268 }
adam@1615 269 else if(idow === 0)
adam@1615 270 {
adam@1615 271 idow = Dt.formats.V(new Date("" + (d.getFullYear()-1) + "/12/31"));
adam@1615 272 }
phurst@1986 273
adam@1615 274 return xPad(idow, 0);
adam@1615 275 },
adam@1615 276 w: "getDay",
adam@1615 277 W: function (d) {
adam@1615 278 var doy = parseInt(Dt.formats.j(d), 10);
adam@1615 279 var rdow = 7-Dt.formats.u(d);
adam@1615 280 var woy = parseInt((doy+rdow)/7, 10);
adam@1615 281 return xPad(woy, 0, 10);
adam@1615 282 },
adam@1615 283 y: function (d) { return xPad(d.getFullYear()%100, 0); },
adam@1615 284 Y: "getFullYear",
adam@1615 285 z: function (d) {
adam@1615 286 var o = d.getTimezoneOffset();
adam@1615 287 var H = xPad(parseInt(Math.abs(o/60), 10), 0);
adam@1615 288 var M = xPad(Math.abs(o%60), 0);
adam@1615 289 return (o>0?"-":"+") + H + M;
adam@1615 290 },
adam@1615 291 Z: function (d) {
adam@1615 292 var tz = d.toString().replace(/^.*:\d\d( GMT[+-]\d+)? \(?([A-Za-z ]+)\)?\d*$/, "$2").replace(/[a-z ]/g, "");
adam@1615 293 if(tz.length > 4) {
adam@1615 294 tz = Dt.formats.z(d);
adam@1615 295 }
adam@1615 296 return tz;
adam@1615 297 },
adam@1615 298 "%": function (d) { return "%"; }
adam@1615 299 },
adam@1615 300
adam@1615 301 aggregates: {
adam@1615 302 c: "locale",
adam@1615 303 D: "%m/%d/%y",
adam@1615 304 F: "%Y-%m-%d",
adam@1615 305 h: "%b",
adam@1615 306 n: "\n",
adam@1615 307 r: "%I:%M:%S %p",
adam@1615 308 R: "%H:%M",
adam@1615 309 t: "\t",
adam@1615 310 T: "%H:%M:%S",
adam@1615 311 x: "locale",
adam@1615 312 X: "locale"
adam@1615 313 },
adam@1615 314
adam@1615 315 format : function (oDate, format) {
adam@1615 316 var replace_aggs = function (m0, m1) {
adam@1615 317 var f = Dt.aggregates[m1];
adam@1615 318 return (f === "locale" ? YDateEn[m1] : f);
adam@1615 319 };
adam@1615 320
adam@1615 321 var replace_formats = function (m0, m1) {
adam@1615 322 var f = Dt.formats[m1];
adam@1615 323 switch(typeof f) {
adam@1615 324 case "string":
adam@1615 325 return oDate[f]();
adam@1615 326 case "function":
adam@1615 327 return f.call(oDate, oDate, YDateEn);
adam@1615 328 case "array":
adam@1615 329 case "object":
adam@1615 330 if(typeof(f[0]) === "string")
adam@1615 331 return xPad(oDate[f[0]](), f[1]);
adam@1615 332 default:
adam@1615 333 return m1;
adam@1615 334 }
adam@1615 335 };
adam@1615 336
adam@1615 337 while(format.match(/%[cDFhnrRtTxX]/)) {
adam@1615 338 format = format.replace(/%([cDFhnrRtTxX])/g, replace_aggs);
adam@1609 339 }
adam@1615 340
adam@1615 341 var str = format.replace(/%([aAbBCdegGHIjklmMpPsSuUVwWyYzZ%])/g, replace_formats);
adam@1615 342
adam@1615 343 replace_aggs = replace_formats = undefined;
adam@1615 344
adam@1615 345 return str;
adam@1615 346 }
adam@1609 347 };
adam@1609 348
adam@1615 349 // End of YUI code
adam@1609 350
adam@1609 351 function strftime(fmt, thisTime)
adam@1609 352 {
adam@1609 353 var thisDate = new Date();
adam@1622 354 thisDate.setTime(Math.floor(thisTime / 1000));
adam@1615 355 return Dt.format(thisDate, fmt);
phurst@1986 356 };
phurst@1986 357
phurst@1986 358 function fromDatetime(year, month, date, hour, minute, second) {
phurst@1986 359 return (new Date(year, month, date, hour, minute, second)).getTime() * 1000;
phurst@1986 360 };
phurst@1986 361
phurst@1986 362 function datetimeYear(t) {
phurst@1986 363 return (new Date(t / 1000)).getYear() + 1900;
phurst@1986 364 };
phurst@1986 365
phurst@1986 366 function datetimeMonth(t) {
phurst@1986 367 return (new Date(t / 1000)).getMonth();
phurst@1986 368 };
phurst@1986 369
phurst@1986 370 function datetimeDay(t) {
phurst@1986 371 return (new Date(t / 1000)).getDate();
phurst@1986 372 };
phurst@1986 373
phurst@1986 374 function datetimeHour(t) {
phurst@1986 375 return (new Date(t / 1000)).getHours();
phurst@1986 376 };
phurst@1986 377
phurst@1986 378 function datetimeMinute(t) {
phurst@1986 379 return (new Date(t / 1000)).getMinutes();
phurst@1986 380 };
phurst@1986 381
phurst@1986 382 function datetimeSecond(t) {
phurst@1986 383 return (new Date(t / 1000)).getSeconds();
phurst@1986 384 };
phurst@1986 385
phurst@1986 386 function datetimeDayOfWeek(t) {
phurst@1986 387 return (new Date(t / 1000)).getDay();
phurst@1986 388 };
adam@1609 389
adam@1487 390
adamc@729 391 // Error handling
adamc@729 392
adam@1625 393 function uw_debug(msg) {
adam@1625 394 try {
adam@1625 395 console.debug(msg);
adam@1625 396 } catch (e) {
adam@1625 397 alert("DEBUG: " + msg);
adam@1625 398 }
adam@1625 399
adam@1625 400 return 0;
adam@1625 401 }
adam@1625 402
adamc@729 403 function whine(msg) {
adamc@1260 404 alert(msg);
adamc@1260 405 throw msg;
adamc@729 406 }
adamc@729 407
adamc@810 408 function pf(loc) {
adamc@1260 409 throw ("Pattern match failure (" + loc + ")");
adamc@729 410 }
adamc@729 411
adam@1553 412 var lameDuck = false;
adam@1553 413
adamc@798 414 function runHandlers(kind, ls, arg) {
adam@1553 415 if (!lameDuck) {
adam@1553 416 if (ls == null)
adam@1553 417 alert(kind + ": " + arg);
adam@1553 418 for (; ls; ls = ls.next)
adam@1553 419 try {
adam@1890 420 exec({c:"a", f:{c:"a", f:ls.data, x:{c:"c", v:arg}}, x:{c:"c", v:null}});
adam@1553 421 } catch (v) { }
adam@1553 422 }
adamc@729 423 }
adamc@729 424
adamc@729 425 var errorHandlers = null;
adamc@729 426
adam@1890 427 function flift0(v) {
adam@1890 428 return {c:"c", v:v};
adam@1890 429 }
adam@1890 430
adamc@729 431 function onError(f) {
adam@1890 432 errorHandlers = cons(flift0(f), errorHandlers);
adamc@729 433 }
adamc@729 434
adamc@729 435 function er(s) {
adamc@1260 436 runHandlers("Error", errorHandlers, s);
adamc@1260 437 throw {uw_error: s};
adamc@729 438 }
adamc@729 439
adamc@729 440 var failHandlers = null;
adamc@729 441
adamc@729 442 function onFail(f) {
adam@1890 443 failHandlers = cons(flift0(f), failHandlers);
adamc@729 444 }
adamc@729 445
adamc@729 446 function doExn(v) {
adamc@1260 447 if (v == null || v.uw_error == null) {
adamc@1260 448 var s = (v == null ? "null" : v.message ? v.message : v.toString());
adamc@1260 449 if (v != null && v.fileName && v.lineNumber)
adamc@1260 450 s += " (" + v.fileName + ":" + v.lineNumber + ")";
adamc@1260 451 runHandlers("Fail", failHandlers, s);
adamc@1260 452 }
adamc@729 453 }
adamc@729 454
adamc@729 455 var disconnectHandlers = null;
adamc@729 456
adamc@976 457 function flift(f) {
kkallio@1484 458 return {c: "c", v:{env:cons(f,null), body:{c:"v", n:1}}};
adamc@976 459 }
adamc@976 460
adamc@729 461 function onDisconnect(f) {
adamc@1260 462 disconnectHandlers = cons(flift(f), disconnectHandlers);
adamc@729 463 }
adamc@729 464
adamc@729 465 function discon() {
adamc@1260 466 runHandlers("Disconnect", disconnectHandlers, null);
adamc@729 467 }
adamc@729 468
adamc@729 469 var connectHandlers = null;
adamc@729 470
adamc@729 471 function onConnectFail(f) {
adamc@1260 472 connectHandlers = cons(flift(f), connectHandlers);
adamc@729 473 }
adamc@729 474
adam@2188 475 function conn(msg) {
adam@2188 476 var rx = /(.*)<body>((.|\n|\r)*)<\/body>(.*)/g;
adam@2188 477 var arr = rx.exec(msg);
adam@2188 478 msg = (arr && arr.length >= 3) ? arr[2] : msg;
adam@2188 479 runHandlers("RPC failure", connectHandlers, msg);
adamc@729 480 }
adamc@729 481
adamc@729 482 var serverHandlers = null;
adamc@729 483
adamc@729 484 function onServerError(f) {
adam@1890 485 serverHandlers = cons(flift0(f), serverHandlers);
adamc@729 486 }
adamc@729 487
adamc@729 488 function servErr(s) {
adamc@1260 489 window.setTimeout(function () { runHandlers("Server", serverHandlers, s); }, 0);
adamc@729 490 }
adamc@729 491
adam@1783 492 // Key and mouse events
adam@1555 493
adam@1555 494 var uw_event = null;
adam@1555 495
adam@1783 496 function uw_getEvent() {
adam@1783 497 return window.event ? window.event : uw_event;
adam@1555 498 }
adam@1555 499
adam@1783 500 function firstGood(x, y) {
adam@1783 501 if (x == undefined || x == 0)
adam@1783 502 return y;
adam@1783 503 else
adam@1783 504 return x;
adam@1783 505 }
adam@1783 506
adam@1783 507 function uw_mouseEvent() {
adam@1783 508 var ev = uw_getEvent();
adam@1783 509
adam@1783 510 return {_ScreenX : firstGood(ev.screenX, 0),
adam@1783 511 _ScreenY : firstGood(ev.screenY, 0),
adam@1783 512 _ClientX : firstGood(ev.clientX, 0),
adam@1783 513 _ClientY : firstGood(ev.clientY, 0),
adam@1783 514 _CtrlKey : firstGood(ev.ctrlKey, false),
adam@1783 515 _ShiftKey : firstGood(ev.shiftKey, false),
adam@1783 516 _AltKey : firstGood(ev.altKey, false),
adam@1783 517 _MetaKey : firstGood(ev.metaKey, false),
adam@1783 518 _Button : ev.button == 2 ? "Right" : ev.button == 1 ? "Middle" : "Left"};
adam@1783 519 }
adam@1783 520
adam@1783 521 function uw_keyEvent() {
adam@1783 522 var ev = uw_getEvent();
adam@1783 523
adam@1783 524 return {_KeyCode : firstGood(ev.keyCode, ev.which),
adam@1783 525 _CtrlKey : firstGood(ev.ctrlKey, false),
adam@1783 526 _ShiftKey : firstGood(ev.shiftKey, false),
adam@1783 527 _AltKey : firstGood(ev.altKey, false),
adam@1783 528 _MetaKey : firstGood(ev.metaKey, false)};
adam@1783 529 }
adam@1783 530
adam@1783 531
adam@1783 532
adam@1555 533 // Document events
adam@1555 534
adam@1555 535 function uw_handler(name, f) {
adam@1555 536 var old = document[name];
adam@1555 537 if (old == undefined)
adam@1783 538 document[name] = function(event) { uw_event = event; execF(execF(f, uw_mouseEvent())); };
adam@1555 539 else
adam@1783 540 document[name] = function(event) { uw_event = event; old(); execF(execF(f, uw_mouseEvent())); };
adam@1555 541 }
adam@1555 542
adam@1555 543 function uw_onClick(f) {
adam@1555 544 uw_handler("onclick", f);
adam@1555 545 }
adam@1555 546
ziv@2130 547 function uw_onContextmenu(f) {
ziv@2130 548 uw_handler("oncontextmenu", f);
ziv@2130 549 }
ziv@2130 550
adam@1555 551 function uw_onDblclick(f) {
adam@1555 552 uw_handler("ondblclick", f);
adam@1555 553 }
adam@1555 554
adam@1555 555 function uw_onMousedown(f) {
adam@1555 556 uw_handler("onmousedown", f);
adam@1555 557 }
adam@1555 558
ziv@2130 559 function uw_onMouseenter(f) {
ziv@2130 560 uw_handler("onmouseenter", f);
ziv@2130 561 }
ziv@2130 562
ziv@2130 563 function uw_onMouseleave(f) {
ziv@2130 564 uw_handler("onmouseleave", f);
ziv@2130 565 }
ziv@2130 566
adam@1791 567 function uw_onMousemove(f) {
adam@1791 568 uw_handler("onmousemove", f);
adam@1791 569 }
adam@1791 570
adam@1791 571 function uw_onMouseout(f) {
adam@1791 572 uw_handler("onmouseout", f);
adam@1791 573 }
adam@1791 574
adam@1791 575 function uw_onMouseover(f) {
adam@1791 576 uw_handler("onmouseover", f);
adam@1791 577 }
adam@1791 578
adam@1555 579 function uw_onMouseup(f) {
adam@1555 580 uw_handler("onmouseup", f);
adam@1555 581 }
adam@1555 582
adam@1555 583 function uw_keyHandler(name, f) {
adam@1555 584 var old = document[name];
adam@1555 585 if (old == undefined)
adam@1783 586 document[name] = function(event) { uw_event = event; execF(execF(f, uw_keyEvent())); };
adam@1555 587 else
adam@1783 588 document[name] = function(event) { uw_event = event; old(); execF(execF(f, uw_keyEvent())); };
adam@1555 589 }
adam@1555 590
adam@1555 591 function uw_onKeydown(f) {
adam@1555 592 uw_keyHandler("onkeydown", f);
adam@1555 593 }
adam@1555 594
adam@1555 595 function uw_onKeypress(f) {
adam@1555 596 uw_keyHandler("onkeypress", f);
adam@1555 597 }
adam@1555 598
adam@1555 599 function uw_onKeyup(f) {
adam@1555 600 uw_keyHandler("onkeyup", f);
adam@1555 601 }
adam@1555 602
adam@1559 603 // Cancelling of further event processing
adam@1559 604
adam@1559 605 function uw_preventDefault() {
adam@1559 606 var e = window.event ? window.event : uw_event;
adam@1559 607 e.returnValue = false;
adam@1559 608 if (e.preventDefault) e.preventDefault();
adam@1559 609 }
adam@1559 610
adam@1559 611 function uw_stopPropagation() {
adam@1559 612 var e = window.event ? window.event : uw_event;
adam@1559 613 e.cancelBubble = true;
adam@1559 614 if (e.stopPropagation) e.stopPropagation();
adam@1559 615 }
adamc@729 616
adamc@692 617 // Embedding closures in XML strings
adamc@692 618
adamc@693 619 function cs(f) {
adamc@1260 620 return {closure: f};
adamc@693 621 }
adamc@693 622
adamc@693 623 function isWeird(v) {
adamc@1260 624 return v.closure != null || v.cat1 != null;
adamc@693 625 }
adamc@693 626
adamc@692 627 function cat(s1, s2) {
adamc@1260 628 if (isWeird(s1) || isWeird(s2))
adamc@1260 629 return {cat1: s1, cat2: s2};
adamc@1260 630 else
adamc@1260 631 return s1 + s2;
adamc@692 632 }
adamc@692 633
adamc@692 634 var closures = [];
adamc@703 635 var freeClosures = null;
adamc@692 636
adamc@692 637 function newClosure(f) {
adamc@1260 638 var n;
adamc@1260 639 if (freeClosures == null) {
adamc@1260 640 n = closures.length;
adamc@1260 641 } else {
adamc@1260 642 n = freeClosures.data;
adamc@1260 643 freeClosures = freeClosures.next;
adamc@1260 644 }
adamc@1260 645 closures[n] = f;
adamc@1260 646 return n;
adamc@692 647 }
adamc@692 648
adamc@703 649 function freeClosure(n) {
adamc@1260 650 closures[n] = null;
adamc@1260 651 freeClosures = cons(n, freeClosures);
adamc@703 652 }
adamc@703 653
adamc@692 654 function cr(n) {
adamc@1260 655 return closures[n];
adamc@692 656 }
adamc@692 657
adam@2129 658 function flattenAcc(a, cls, trs) {
adam@2129 659 while (trs) {
adam@2129 660 var tr = trs.data;
adam@2129 661 trs = trs.next;
adam@2129 662
adam@2129 663 if (tr.cat1 != null) {
adam@2129 664 trs = cons(tr.cat1, cons(tr.cat2, trs));
adam@2129 665 } else if (tr.closure != null) {
adam@2129 666 var cl = newClosure(tr.closure);
adam@2129 667 cls.v = cons(cl, cls.v);
adam@2129 668 a.push("cr(", cl.toString(), ")");
adam@2129 669 } else
adam@2129 670 a.push(tr);
adam@2129 671 }
vshabanoff@1813 672 }
vshabanoff@1813 673
vshabanoff@1813 674 function flatten(cls, tr) {
vshabanoff@1813 675 var a = [];
adam@2129 676 flattenAcc(a, cls, cons(tr, null));
vshabanoff@1813 677 return a.join("");
adamc@692 678 }
adamc@692 679
adamc@728 680 function flattenLocal(s) {
adamc@1260 681 var cls = {v : null};
adamc@1260 682 var r = flatten(cls, s);
adamc@1260 683 for (cl = cls.v; cl != null; cl = cl.next)
adamc@1260 684 freeClosure(cl.data);
adamc@1260 685 return r;
adamc@728 686 }
adamc@728 687
adamc@692 688
adamc@692 689 // Dynamic tree management
adamc@692 690
adamc@690 691 function populate(node) {
adamc@1260 692 var s = node.signal;
adamc@1260 693 var oldSources = node.sources;
adamc@1260 694 try {
adamc@1260 695 var sr = execF(s, null);
adamc@1260 696 var newSources = sr._sources;
adamc@690 697
adamc@1260 698 for (var sp = oldSources; sp; sp = sp.next)
adamc@1260 699 if (!member(sp.data, newSources))
adamc@1260 700 sp.data.dyns = remove(node, sp.data.dyns);
adamc@690 701
adamc@1260 702 for (var sp = newSources; sp; sp = sp.next)
adamc@1260 703 if (!member(sp.data, oldSources))
adamc@1260 704 sp.data.dyns = cons(node, sp.data.dyns);
adamc@690 705
adamc@1260 706 node.sources = newSources;
adamc@1260 707 node.recreate(sr._data);
adamc@1260 708 } catch (v) {
adamc@1260 709 doExn(v);
adamc@1260 710 }
adamc@579 711 }
adamc@574 712
adamc@580 713 function sc(v) {
adamc@1260 714 return {data : v, dyns : null};
adamc@580 715 }
adamc@580 716 function sv(s, v) {
adam@1491 717 if (s.data != v) {
adam@1491 718 s.data = v;
adamc@967 719
adam@1491 720 for (var ls = s.dyns; ls; ls = ls.next)
adam@1491 721 if (!ls.dead)
adam@1491 722 populate(ls.data);
adam@1491 723 }
adamc@580 724 }
adamc@601 725 function sg(s) {
adamc@1260 726 return s.data;
adamc@601 727 }
adamc@579 728
adamc@580 729 function ss(s) {
adamc@1260 730 return {env:cons(s, null), body:{c:"r", l:
adamc@1260 731 cons({n:"sources", v:{c:"c", v:cons(s, null)}},
adamc@1260 732 cons({n:"data", v:{c:"f", f:sg, a:cons({c:"v", n:1}, null)}}, null))}};
adamc@580 733 }
adamc@580 734 function sr(v) {
adamc@1260 735 return {env:null, body:{c:"c", v:{_sources : null, _data : v}}};
adamc@580 736 }
adamc@580 737 function sb(x,y) {
adamc@1260 738 return {env:cons(y,cons(x,null)),
adamc@1260 739 body:{c:"=",
adamc@1260 740 e1:{c:"a", f:{c:"v", n:2}, x:{c:"c", v:null}},
adamc@1260 741 e2:{c:"=",
adamc@1260 742 e1:{c:"a",
adamc@1260 743 f:{c:"a", f:{c:"v", n:2}, x:{c:".", r:{c:"v", n:0}, f:"data"}},
adamc@1260 744 x:{c:"c", v:null}},
adamc@1260 745 e2:{c:"r", l:cons(
adamc@1260 746 {n:"sources", v:{c:"f", f:union, a:cons({c:".", r:{c:"v", n:1}, f:"sources"},
adamc@1260 747 cons({c:".", r:{c:"v", n:0}, f:"sources"}, null))}},
adamc@1260 748 cons({n:"data", v:{c:".", r:{c:"v", n:0}, f:"data"}}, null))}}}};
adamc@580 749 }
adamc@841 750 function scur(s) {
adamc@1260 751 return execF(s, null)._data;
adamc@841 752 }
adamc@571 753
adamc@604 754 function lastParent() {
adamc@1260 755 var pos = document.body;
adamc@604 756
adamc@1260 757 while (pos.lastChild && pos.lastChild.nodeType == 1)
adamc@1260 758 pos = pos.lastChild;
adamc@600 759
adamc@1260 760 pos = pos.parentNode;
adamc@1039 761
adamc@1260 762 return pos;
adamc@600 763 }
adamc@600 764
adam@1803 765 var thisScript = null;
adam@1803 766
adamc@604 767 function addNode(node) {
adam@1678 768 if (thisScript) {
adam@1678 769 if (thisScript.parentNode)
adam@1678 770 thisScript.parentNode.replaceChild(node, thisScript);
adam@1678 771 } else
adamc@1260 772 lastParent().appendChild(node);
adamc@603 773 }
adamc@603 774
adam@1803 775 function runScripts(node) {
adam@1803 776 if (node.tagName == "SCRIPT") {
adam@1803 777 var savedScript = thisScript;
adam@1803 778 thisScript = node;
phurst@1986 779
adam@1803 780 try {
adam@1803 781 eval(thisScript.text);
adam@1803 782 } catch (v) {
adam@1803 783 doExn(v);
adam@1803 784 }
adam@1803 785 if (thisScript.parentNode)
adam@1803 786 thisScript.parentNode.removeChild(thisScript);
adamc@690 787
adam@1803 788 thisScript = savedScript;
adam@1803 789 } else if (node.getElementsByTagName) {
adamc@1260 790 var savedScript = thisScript;
adamc@603 791
adamc@1260 792 var scripts = node.getElementsByTagName("script"), scriptsCopy = [];
adamc@1260 793 var len = scripts.length;
adamc@1260 794 for (var i = 0; i < len; ++i)
adamc@1260 795 scriptsCopy[i] = scripts[i];
adamc@1260 796 for (var i = 0; i < len; ++i) {
adamc@1260 797 thisScript = scriptsCopy[i];
adamc@1039 798
adamc@1260 799 try {
adamc@1260 800 eval(thisScript.text);
adamc@1260 801 } catch (v) {
adamc@1260 802 doExn(v);
adamc@1260 803 }
adamc@1260 804 if (thisScript.parentNode)
adamc@1260 805 thisScript.parentNode.removeChild(thisScript);
adamc@1260 806 }
adamc@1260 807
adamc@1260 808 thisScript = savedScript;
adamc@729 809 }
adamc@603 810 }
adamc@603 811
adamc@603 812
adamc@692 813 // Dynamic tree entry points
adamc@692 814
adamc@967 815 function killScript(scr) {
adamc@1260 816 scr.dead = true;
adamc@1260 817 for (var ls = scr.sources; ls; ls = ls.next)
adamc@1260 818 ls.data.dyns = remove(scr, ls.data.dyns);
adamc@1260 819 for (var ls = scr.closures; ls; ls = ls.next)
adamc@1260 820 freeClosure(ls.data);
adamc@967 821 }
adamc@967 822
adam@1538 823 // Sometimes we wind up with tables that contain <script>s outside the single <tbody>.
adam@1538 824 // To avoid dealing with that case, we normalize by moving <script>s into <tbody>.
adam@1538 825 function normalizeTable(table) {
adam@1538 826 var orig = table;
adam@1538 827
adam@1538 828 var script, next;
adam@1538 829
adam@1803 830 while (table && table.tagName != "TABLE")
adam@1538 831 table = table.parentNode;
adam@1538 832
adam@1538 833 for (var tbody = table.firstChild; tbody; tbody = tbody.nextSibling) {
adam@1538 834 if (tbody.tagName == "TBODY") {
adam@1681 835 var firstChild = tbody.firstChild;
adam@1681 836
adam@1538 837 for (script = table.firstChild; script && script != tbody; script = next) {
adam@1538 838 next = script.nextSibling;
adam@1538 839
adam@1681 840 if (firstChild)
adam@1681 841 tbody.insertBefore(script, firstChild);
adam@1681 842 else
adam@1681 843 tbody.appendChild(script);
adam@1538 844 }
adam@1538 845
adam@1538 846 return;
adam@1538 847 }
adam@1538 848 }
adam@1538 849
adam@1538 850 var tbody = document.createElement("tbody");
adam@1538 851 for (script = table.firstChild; script; script = next) {
adam@1538 852 next = script.nextSibling;
rmbruijn@1597 853
adam@1681 854 tbody.appendChild(script);
adam@1538 855 }
adam@1538 856 table.appendChild(tbody);
adam@1538 857 }
adam@1538 858
adam@1803 859 var suspendScripts = false;
adam@1803 860
adamc@901 861 function dyn(pnode, s) {
adam@1803 862 if (suspendScripts)
adam@1803 863 return;
adam@1803 864
adamc@1260 865 var x = document.createElement("script");
adamc@1260 866 x.dead = false;
adamc@1260 867 x.signal = s;
adamc@1260 868 x.sources = null;
adamc@1260 869 x.closures = null;
adamc@899 870
adamc@1260 871 var firstChild = null;
adamc@899 872
adamc@1260 873 x.recreate = function(v) {
adamc@1260 874 for (var ls = x.closures; ls; ls = ls.next)
adamc@1260 875 freeClosure(ls.data);
adamc@945 876
adamc@946 877 var next;
adamc@1260 878 for (var child = firstChild; child && child != x; child = next) {
adamc@1260 879 next = child.nextSibling;
adamc@1260 880
adamc@1260 881 killScript(child);
adamc@1260 882 if (child.getElementsByTagName) {
adamc@1260 883 var arr = child.getElementsByTagName("script");
adamc@1260 884 for (var i = 0; i < arr.length; ++i)
adamc@1260 885 killScript(arr[i]);
adamc@1260 886 }
adamc@1260 887
adamc@1260 888 if (child.parentNode)
adamc@1260 889 child.parentNode.removeChild(child);
adamc@946 890 }
adamc@903 891
adamc@1260 892 var cls = {v : null};
adamc@1260 893 var html = flatten(cls, v);
adamc@1260 894 if (pnode != "table" && pnode != "tr")
adamc@1260 895 html = dynPrefix + html;
adamc@1260 896 x.closures = cls.v;
adamc@946 897
adamc@1260 898 if (pnode == "table") {
adam@1538 899 normalizeTable(x.parentNode);
adam@1538 900
adamc@1260 901 var dummy = document.createElement("body");
adam@1803 902 suspendScripts = true;
adam@1803 903 try {
adam@1803 904 dummy.innerHTML = "<table>" + html + "</table>";
adam@1803 905 } catch (e) {
adam@1803 906 suspendScripts = false;
adam@1803 907 throw e;
adam@1803 908 }
adam@1803 909
adamc@1260 910 var table = x.parentNode;
adamc@1260 911
adamc@1260 912 if (table) {
adamc@1260 913 firstChild = null;
adamc@1260 914 var tbody;
adamc@1260 915
adamc@1260 916 var arr = dummy.getElementsByTagName("tbody");
adamc@1260 917
adamc@1260 918 var tbody;
adamc@1260 919 if (arr.length > 0 && arr[0].parentNode == dummy.firstChild) {
adamc@1260 920 tbody = arr[0];
adamc@1260 921 var next;
adamc@1260 922 for (var node = dummy.firstChild.firstChild; node; node = next) {
adamc@1260 923 next = node.nextSibling;
adamc@1260 924
adamc@1260 925 if (node.tagName != "TBODY")
adamc@1260 926 tbody.appendChild(node);
adamc@1260 927 }
adamc@1260 928 } else
adamc@1260 929 tbody = dummy.firstChild;
adamc@1260 930
adamc@1260 931 var next;
adamc@1260 932 firstChild = document.createElement("script");
adamc@1260 933 table.insertBefore(firstChild, x);
adamc@1260 934 for (var node = tbody.firstChild; node; node = next) {
adamc@1260 935 next = node.nextSibling;
adamc@1260 936 table.insertBefore(node, x);
adam@1803 937 suspendScripts = false;
adam@1803 938 runScripts(node);
adam@1803 939 suspendScripts = true;
adamc@1260 940 }
adamc@1260 941 }
adam@1803 942
adam@1803 943 suspendScripts = false;
adamc@1260 944 } else if (pnode == "tr") {
adamc@1260 945 var dummy = document.createElement("body");
adam@1803 946 suspendScripts = true;
adam@1803 947 try {
adam@1803 948 dummy.innerHTML = "<table><tr>" + html + "</tr></table>";
adam@1803 949 } catch (e) {
adam@1803 950 suspendScripts = false;
adam@1803 951 throw e;
adam@1803 952 }
adam@1803 953
adamc@1260 954 var table = x.parentNode;
adamc@1260 955
adamc@1260 956 if (table) {
adamc@1260 957 var arr = dummy.getElementsByTagName("tr");
adamc@1260 958 firstChild = null;
adamc@1260 959 var tr;
adamc@1260 960 if (arr.length > 0 && table != null)
adamc@1260 961 tr = arr[0];
adamc@1260 962 else
adamc@1260 963 tr = dummy.firstChild;
adamc@1260 964
adamc@1260 965 var next;
adamc@1260 966 firstChild = document.createElement("script");
adamc@1260 967 table.insertBefore(firstChild, x);
adamc@1260 968 for (var node = tr.firstChild; node; node = next) {
adamc@1260 969 next = node.nextSibling;
adamc@1260 970 table.insertBefore(node, x);
adam@1803 971 suspendScripts = false;
adam@1803 972 runScripts(node);
adam@1803 973 suspendScripts = true;
adamc@1260 974 }
adam@1803 975 };
adam@1803 976
adam@1803 977 suspendScripts = false;
adamc@1260 978 } else {
adamc@1260 979 firstChild = document.createElement("span");
adam@1803 980
adam@1803 981 suspendScripts = true;
adam@1803 982 try {
adam@1803 983 firstChild.innerHTML = html;
adam@1803 984 if (x.parentNode)
adam@1803 985 x.parentNode.insertBefore(firstChild, x);
adam@1803 986 } catch (e) {
adam@1803 987 suspendScripts = false;
adam@1803 988 throw e;
adam@1803 989 }
adam@1803 990 suspendScripts = false;
adam@1802 991 runScripts(firstChild);
adamc@903 992 }
adamc@1260 993 };
adamc@899 994
adamc@1260 995 addNode(x);
adamc@1260 996 populate(x);
adamc@571 997 }
adamc@582 998
adam@1702 999 function setInnerHTML(node, html) {
adam@1702 1000 var x;
adam@1702 1001
adam@1702 1002 if (node.previousSibling && node.previousSibling.closures != undefined) {
adam@1702 1003 x = node.previousSibling;
adam@1702 1004
adam@1702 1005 for (var ls = x.closures; ls; ls = ls.next)
adam@1702 1006 freeClosure(ls.data);
adam@1702 1007
adam@1702 1008 if (node.getElementsByTagName) {
adam@1702 1009 var arr = node.getElementsByTagName("script");
adam@1702 1010 for (var i = 0; i < arr.length; ++i)
adam@1702 1011 killScript(arr[i]);
adam@1702 1012 }
adam@1702 1013 } else {
adam@1702 1014 x = document.createElement("script");
adam@1702 1015 x.dead = false;
adam@1702 1016 x.sources = null;
adam@1702 1017
adam@1702 1018 if (node.parentNode)
adam@1702 1019 node.parentNode.insertBefore(x, node);
adam@1702 1020 else
adam@1702 1021 whine("setInnerHTML: node is not already in the DOM tree");
adam@1702 1022 }
adam@1702 1023
adam@1702 1024 var cls = {v : null};
adam@1702 1025 var html = flatten(cls, html);
adam@1702 1026 x.closures = cls.v;
adam@1803 1027 suspendScripts = true;
adam@1702 1028 node.innerHTML = html;
adam@1803 1029 suspendScripts = false;
adam@1702 1030 runScripts(node);
adam@1702 1031 }
adam@1702 1032
adam@1792 1033 var maySuspend = true;
adam@1792 1034
adam@1786 1035 function active(s) {
adam@1803 1036 if (suspendScripts)
adam@1803 1037 return;
adam@1803 1038
adam@1792 1039 var ms = maySuspend;
adam@1792 1040 maySuspend = false;
adam@1792 1041 try {
adam@1792 1042 var html = execF(s);
adam@1792 1043 } catch (e) {
adam@1792 1044 maySuspend = ms;
adam@1792 1045 throw e;
adam@1792 1046 }
adam@1792 1047 maySuspend = ms;
adam@2002 1048 if (html != "") {
adam@2002 1049 var span = document.createElement("span");
adam@2002 1050 addNode(span);
adam@2002 1051 setInnerHTML(span, html);
adam@2002 1052 }
adam@1786 1053 }
adam@1786 1054
adam@1463 1055 function input(x, s, recreate, type, name) {
adam@1463 1056 if (name) x.name = name;
adamc@1260 1057 if (type) x.type = type;
adamc@1260 1058 x.dead = false;
adamc@1260 1059 x.signal = ss(s);
adamc@1260 1060 x.sources = null;
adamc@1260 1061 x.recreate = recreate(x);
adamc@1260 1062 addNode(x);
adamc@1260 1063 populate(x);
adamc@816 1064
adamc@1260 1065 return x;
adamc@816 1066 }
adamc@816 1067
adam@2079 1068 function inpt(type, s, name) {
adam@1803 1069 if (suspendScripts)
adam@1803 1070 return;
adam@1803 1071
adamc@1260 1072 var x = input(document.createElement("input"), s,
adam@2079 1073 function(x) { return function(v) { if (x.value != v) x.value = v; }; }, type, name);
adamc@1260 1074 x.value = s.data;
adam@1491 1075 x.onkeyup = x.oninput = x.onchange = x.onpropertychange = function() { sv(s, x.value) };
adamc@816 1076
adamc@1260 1077 return x;
adamc@816 1078 }
adamc@816 1079
adam@2079 1080 function inp(s, name) {
adam@2079 1081 return inpt("text", s, name);
adam@2079 1082 }
adam@2079 1083
ziv@2055 1084 function password(s, name) {
adam@2079 1085 return inpt("password", s, name);
adam@2079 1086 }
ziv@2055 1087
adam@2079 1088 function email(s, name) {
adam@2079 1089 return inpt("email", s, name);
adam@2079 1090 }
ziv@2055 1091
adam@2079 1092 function search(s, name) {
adam@2079 1093 return inpt("search", s, name);
adam@2079 1094 }
adam@2079 1095
adam@2079 1096 function url(s, name) {
adam@2079 1097 return inpt("url", s, name);
adam@2079 1098 }
adam@2079 1099
adam@2079 1100 function tel(s, name) {
adam@2079 1101 return inpt("tel", s, name);
adam@2079 1102 }
adam@2079 1103
adam@2079 1104 function color(s, name) {
adam@2079 1105 return inpt("color", s, name);
ziv@2055 1106 }
ziv@2055 1107
adam@2080 1108 function number(s, name) {
adam@2080 1109 return inpt("number", s, name);
adam@2080 1110 }
adam@2080 1111
adam@2080 1112 function range(s, name) {
adam@2080 1113 return inpt("range", s, name);
adam@2080 1114 }
adam@2080 1115
adam@2080 1116 function date(s, name) {
adam@2080 1117 return inpt("date", s, name);
adam@2080 1118 }
adam@2080 1119
adam@2080 1120 function datetime(s, name) {
adam@2080 1121 return inpt("datetime", s, name);
adam@2080 1122 }
adam@2080 1123
adam@2080 1124 function datetime_local(s, name) {
adam@2080 1125 return inpt("datetime-local", s, name);
adam@2080 1126 }
adam@2080 1127
adam@2080 1128 function month(s, name) {
adam@2080 1129 return inpt("month", s, name);
adam@2080 1130 }
adam@2080 1131
adam@2080 1132 function week(s, name) {
adam@2080 1133 return inpt("week", s, name);
adam@2080 1134 }
adam@2080 1135
adam@2080 1136 function time(s, name) {
adam@2080 1137 return inpt("time", s, name);
adam@2080 1138 }
adam@2080 1139
adam@2080 1140
adam@1479 1141 function selectValue(x) {
adam@1479 1142 if (x.options.length == 0)
adam@1479 1143 return "";
adam@1479 1144 else
adam@1479 1145 return x.options[x.selectedIndex].value;
adam@1479 1146 }
adam@1479 1147
adam@1479 1148 function setSelectValue(x, v) {
adam@1479 1149 for (var i = 0; i < x.options.length; ++i) {
adam@1479 1150 if(x.options[i].value == v) {
adam@1479 1151 x.selectedIndex = i;
adam@1479 1152 return;
adam@1479 1153 }
adam@1479 1154 }
adam@1479 1155 }
adam@1479 1156
adamc@816 1157 function sel(s, content) {
adam@1803 1158 if (suspendScripts)
adam@1803 1159 return;
adam@1803 1160
adamc@1260 1161 var dummy = document.createElement("span");
adamc@1260 1162 dummy.innerHTML = "<select>" + content + "</select>";
adam@1479 1163 var x = input(dummy.firstChild, s, function(x) { return function(v) { if (selectValue(x) != v) setSelectValue(x, v); }; });
adam@1479 1164
adam@1479 1165 for (var i = 0; i < x.options.length; ++i) {
adam@1479 1166 if (x.options[i].value == "")
adam@1479 1167 x.options[i].value = x.options[i].text;
adam@1479 1168 else
adam@1479 1169 x.options[i].value = x.options[i].value.substring(1);
adam@1479 1170 }
adam@1479 1171
adam@1479 1172 setSelectValue(x, s.data);
adam@1479 1173 if (selectValue(x) != s.data)
adam@1479 1174 sv(s, selectValue(x));
adam@1479 1175 x.onchange = function() { sv(s, selectValue(x)) };
adamc@797 1176
adamc@1260 1177 return x;
adamc@598 1178 }
adamc@598 1179
adamc@817 1180 function chk(s) {
adam@1803 1181 if (suspendScripts)
adam@1803 1182 return;
adam@1803 1183
adamc@1260 1184 var x = input(document.createElement("input"), s,
adamc@1260 1185 function(x) { return function(v) { if (x.checked != v) x.checked = v; }; }, "checkbox");
adamc@1260 1186 x.defaultChecked = x.checked = s.data;
adam@1610 1187 x.onclick = x.onkeyup = x.oninput = x.onchange = x.onpropertychange = function() { sv(s, x.checked) };
adamc@817 1188
adamc@1260 1189 return x;
adamc@817 1190 }
adamc@817 1191
adamc@1099 1192 function tbx(s) {
adam@1803 1193 if (suspendScripts)
adam@1803 1194 return;
adam@1803 1195
adamc@1260 1196 var x = input(document.createElement("textarea"), s,
adam@1781 1197 function(x) { return function(v) { if (x.value != v) x.value = v; }; });
adamc@1260 1198 x.innerHTML = s.data;
adam@1491 1199 x.onkeyup = x.oninput = x.onchange = x.onpropertychange = function() { sv(s, x.value) };
adamc@1099 1200
adamc@1260 1201 return x;
adamc@1099 1202 }
adamc@1099 1203
adam@2012 1204 function dynClass(pnode, html, s_class, s_style) {
adam@1803 1205 if (suspendScripts)
adam@1803 1206 return;
adam@1803 1207
adam@1643 1208 var htmlCls = {v : null};
adam@1643 1209 html = flatten(htmlCls, html);
adam@1643 1210 htmlCls = htmlCls.v;
adam@1643 1211
adam@2012 1212 var dummy = document.createElement(pnode);
adam@1803 1213 suspendScripts = true;
adam@1643 1214 dummy.innerHTML = html;
adam@1803 1215 suspendScripts = false;
adam@1643 1216 var html = dummy.firstChild;
adam@1643 1217 dummy.removeChild(html);
adam@2200 1218 if (pnode == "table" && html.tagName == "TBODY") {
adam@2200 1219 html = html.firstChild;
adam@2200 1220 }
adam@1643 1221 addNode(html);
adam@1792 1222 runScripts(html);
adam@1643 1223
adam@1751 1224 if (s_class) {
adam@1751 1225 var x = document.createElement("script");
adam@1751 1226 x.dead = false;
adam@1751 1227 x.signal = s_class;
adam@1751 1228 x.sources = null;
adam@2128 1229 x.closures = htmlCls;
phurst@1986 1230
adam@1751 1231 x.recreate = function(v) {
adam@1751 1232 for (var ls = x.closures; ls != htmlCls; ls = ls.next)
adam@1751 1233 freeClosure(ls.data);
adam@1643 1234
adam@1751 1235 var cls = {v : null};
adam@1751 1236 html.className = flatten(cls, v);
adam@1751 1237 x.closures = concat(cls.v, htmlCls);
adam@1751 1238 }
adam@1643 1239
vshabanoff@1811 1240 html.appendChild(x);
adam@1751 1241 populate(x);
adam@1751 1242 }
adam@1751 1243
adam@1751 1244 if (s_style) {
vshabanoff@1811 1245 var htmlCls2 = s_class ? null : htmlCls;
adam@2012 1246 var y = document.createElement("script");
adam@2012 1247 y.dead = false;
adam@2012 1248 y.signal = s_style;
adam@2012 1249 y.sources = null;
adam@2012 1250 y.closures = htmlCls2;
phurst@1986 1251
adam@2012 1252 y.recreate = function(v) {
adam@2012 1253 for (var ls = y.closures; ls != htmlCls2; ls = ls.next)
adam@1751 1254 freeClosure(ls.data);
adam@1751 1255
adam@1751 1256 var cls = {v : null};
adam@1751 1257 html.style.cssText = flatten(cls, v);
adam@2012 1258 y.closures = concat(cls.v, htmlCls2);
adam@1751 1259 }
adam@1751 1260
adam@2012 1261 html.appendChild(y);
adam@2012 1262 populate(y);
adam@1751 1263 }
adam@1643 1264 }
adam@1643 1265
adam@2121 1266 function bodyDynClass(s_class, s_style) {
adam@2121 1267 if (suspendScripts)
adam@2121 1268 return;
adam@2121 1269
adam@2121 1270 var htmlCls = null;
adam@2121 1271
adam@2121 1272 if (s_class) {
adam@2121 1273 var x = document.createElement("script");
adam@2121 1274 x.dead = false;
adam@2121 1275 x.signal = s_class;
adam@2121 1276 x.sources = null;
adam@2121 1277 x.closures = htmlCls;
adam@2121 1278
adam@2121 1279 x.recreate = function(v) {
adam@2121 1280 for (var ls = x.closures; ls != htmlCls; ls = ls.next)
adam@2121 1281 freeClosure(ls.data);
adam@2121 1282
adam@2121 1283 var cls = {v : null};
adam@2121 1284 document.body.className = flatten(cls, v);
adam@2121 1285 console.log("className to + " + document.body.className);
adam@2121 1286 x.closures = concat(cls.v, htmlCls);
adam@2121 1287 }
adam@2121 1288
adam@2121 1289 document.body.appendChild(x);
adam@2121 1290 populate(x);
adam@2121 1291 }
adam@2121 1292
adam@2121 1293 if (s_style) {
adam@2121 1294 var htmlCls2 = s_class ? null : htmlCls;
adam@2121 1295 var y = document.createElement("script");
adam@2121 1296 y.dead = false;
adam@2121 1297 y.signal = s_style;
adam@2121 1298 y.sources = null;
adam@2121 1299 y.closures = htmlCls2;
adam@2121 1300
adam@2121 1301 y.recreate = function(v) {
adam@2121 1302 for (var ls = y.closures; ls != htmlCls2; ls = ls.next)
adam@2121 1303 freeClosure(ls.data);
adam@2121 1304
adam@2121 1305 var cls = {v : null};
adam@2121 1306 document.body.style.cssText = flatten(cls, v);
adam@2121 1307 console.log("style to + " + document.body.style.cssText);
adam@2121 1308 y.closures = concat(cls.v, htmlCls2);
adam@2121 1309 }
adam@2121 1310
adam@2121 1311 document.body.appendChild(y);
adam@2121 1312 populate(y);
adam@2121 1313 }
adam@2121 1314 }
adam@2121 1315
adamc@800 1316 function addOnChange(x, f) {
adamc@1260 1317 var old = x.onchange;
adam@1290 1318 if (old == null)
adam@1290 1319 x.onchange = f;
adam@1290 1320 else
adam@1290 1321 x.onchange = function() { old(); f(); };
adam@1290 1322 }
adam@1290 1323
adam@1290 1324 function addOnKeyUp(x, f) {
adam@1290 1325 var old = x.onkeyup;
adam@1290 1326 if (old == null)
adam@1290 1327 x.onkeyup = f;
adam@1290 1328 else
adam@1290 1329 x.onkeyup = function(x) { old(x); f(x); };
adamc@800 1330 }
adamc@800 1331
adamc@692 1332
adamc@692 1333 // Basic string operations
adamc@692 1334
adamc@597 1335 function eh(x) {
adamc@1260 1336 if (x == null)
adamc@1260 1337 return "NULL";
adamc@1260 1338 else
adamc@1260 1339 return x.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
adamc@597 1340 }
adamc@597 1341
adamc@582 1342 function ts(x) { return x.toString() }
adamc@586 1343 function bs(b) { return (b ? "True" : "False") }
adam@2097 1344 function s2b(s) { return s == "True" ? true : s == "False" ? false : null; }
adam@2097 1345 function s2be(s) { return s == "True" ? true : s == "False" ? false : er("Illegal Boolean " ^ s); }
adamc@586 1346
adamc@1057 1347 function id(x) { return x; }
adamc@1038 1348 function sub(s, i) { return s.charAt(i); }
adamc@828 1349 function suf(s, i) { return s.substring(i); }
adamc@828 1350 function slen(s) { return s.length; }
adamc@829 1351 function sidx(s, ch) {
adamc@1260 1352 var r = s.indexOf(ch);
adamc@1260 1353 if (r == -1)
adamc@1260 1354 return null;
adamc@1260 1355 else
adamc@1260 1356 return r;
adamc@829 1357 }
dukhovni@2045 1358 function ssidx(h, n) {
dukhovni@2045 1359 var r = h.indexOf(n);
dukhovni@2045 1360 if (r == -1)
dukhovni@2045 1361 return null;
dukhovni@2045 1362 else
dukhovni@2045 1363 return r;
dukhovni@2045 1364 }
adamc@831 1365 function sspn(s, chs) {
adamc@1260 1366 for (var i = 0; i < s.length; ++i)
adamc@1260 1367 if (chs.indexOf(s.charAt(i)) != -1)
adamc@1260 1368 return i;
adamc@831 1369
adam@1793 1370 return s.length;
adamc@831 1371 }
adamc@829 1372 function schr(s, ch) {
adamc@1260 1373 var r = s.indexOf(ch);
adamc@1260 1374 if (r == -1)
adamc@1260 1375 return null;
adamc@1260 1376 else
adamc@1260 1377 return s.substring(r);
adamc@829 1378 }
adamc@829 1379 function ssub(s, start, len) {
adamc@1260 1380 return s.substring(start, start+len);
adamc@829 1381 }
adam@1624 1382 function strlenGe(s, len) {
adam@1624 1383 return s.length >= len;
adam@1624 1384 }
adamc@821 1385
adam@1621 1386 function trimZeroes(s) {
adam@1621 1387 for (var i = 0; i < s.length; ++i)
adam@1632 1388 if (s.charAt(i) != '0') {
adam@1621 1389 if (i > 0)
adam@1621 1390 return s.substring(i);
adam@1621 1391 else
adam@1621 1392 return s;
adam@1621 1393 }
adam@1621 1394
adam@1631 1395 if (s.length == 0)
adam@1621 1396 return s;
adam@1621 1397 else
adam@1631 1398 return "0";
adam@1621 1399 }
adam@1621 1400
adamc@649 1401 function pi(s) {
adam@1631 1402 var st = trimZeroes(s);
adam@1631 1403 var r = parseInt(st);
adam@1631 1404 if (r.toString() == st)
adamc@1260 1405 return r;
adamc@1260 1406 else
adamc@1260 1407 er("Can't parse int: " + s);
adamc@649 1408 }
adamc@649 1409
adamc@649 1410 function pfl(s) {
adamc@1260 1411 var r = parseFloat(s);
adamc@1260 1412 if (r.toString() == s)
adamc@1260 1413 return r;
adamc@1260 1414 else
adamc@1260 1415 er("Can't parse float: " + s);
adamc@649 1416 }
adamc@649 1417
adamc@838 1418 function pio(s) {
adam@1631 1419 var st = trimZeroes(s);
adam@1631 1420 var r = parseInt(st);
adam@1631 1421 if (r.toString() == st)
adamc@1260 1422 return r;
adamc@1260 1423 else
adamc@1260 1424 return null;
adamc@838 1425 }
adamc@838 1426
adamc@838 1427 function pflo(s) {
adamc@1260 1428 var r = parseFloat(s);
adamc@1260 1429 if (r.toString() == s)
adamc@1260 1430 return r;
adamc@1260 1431 else
adamc@1260 1432 return null;
adamc@838 1433 }
adamc@838 1434
adam@1620 1435 function parseSource(s1, s2) {
adam@1620 1436 return eval("s" + s1 + "_" + s2);
adam@1620 1437 }
adam@1620 1438
adamc@692 1439 function uf(s) {
adamc@1260 1440 if (s.length == 0)
adamc@1260 1441 return "_";
vshabanoff@1813 1442 s = s.replace(/\./g, ".2E");
vshabanoff@1813 1443 return (s.charAt(0) == '_' ? "_" : "") + encodeURIComponent(s).replace(/%/g, ".");
adamc@691 1444 }
adamc@691 1445
adamc@692 1446 function uu(s) {
adamc@1260 1447 if (s.length > 0 && s.charAt(0) == '_') {
adamc@1260 1448 s = s.substring(1);
adamc@1260 1449 } else if (s.length >= 3 && (s.charAt(0) == '%' || s.charAt(0) == '.')
adamc@1260 1450 && s.charAt(1) == '5' && (s.charAt(2) == 'f' || s.charAt(2) == 'F'))
adamc@1260 1451 s = s.substring(3);
vshabanoff@1813 1452 s = s.replace(/\+/g, " ");
vshabanoff@1813 1453 s = s.replace(/\./g, "%");
adamc@1260 1454 return decodeURIComponent(s);
adamc@905 1455 }
adamc@905 1456
adamc@1108 1457 function atr(s) {
vshabanoff@1813 1458 return s.replace(/\"/g, "&quot;").replace(/&/g, "&amp;")
vshabanoff@1813 1459 }
adamc@1108 1460
adamc@912 1461 function ub(b) {
adamc@1260 1462 return b ? "1" : "0";
adamc@912 1463 }
adamc@912 1464
adamc@905 1465 function uul(getToken, getData) {
adamc@1260 1466 var tok = getToken();
adamc@1260 1467 if (tok == "Nil") {
adamc@1260 1468 return null;
adamc@1260 1469 } else if (tok == "Cons") {
adamc@1260 1470 var d = getData();
adamc@1260 1471 var l = uul(getToken, getData);
adamc@1260 1472 return {_1:d, _2:l};
adamc@1260 1473 } else
adamc@1260 1474 whine("Can't unmarshal list (" + tok + ")");
adamc@692 1475 }
adamc@692 1476
adamc@961 1477 function strcmp(str1, str2) {
adamc@1260 1478 return ((str1 == str2) ? 0 : ((str1 > str2) ? 1 : -1));
adamc@961 1479 }
adamc@692 1480
adam@2189 1481 function chr(n) {
adam@2189 1482 return String.fromCharCode(n);
adam@2189 1483 }
adam@2189 1484
adam@2189 1485 function htmlifySpecialChar(ch) {
adam@2189 1486 return "&#" + ch.charCodeAt(0) + ";";
adam@2189 1487 }
adam@2189 1488
adamc@603 1489
adamc@692 1490 // Remote calls
adamc@609 1491
adamc@703 1492 var client_id = null;
adamc@668 1493 var client_pass = 0;
adamc@668 1494 var url_prefix = "/";
adamc@673 1495 var timeout = 60;
adam@1552 1496 var isPost = false;
adamc@668 1497
adamc@668 1498 function getXHR(uri)
adamc@609 1499 {
adamc@609 1500 try {
adamc@1260 1501 return new XMLHttpRequest();
adamc@609 1502 } catch (e) {
adamc@1260 1503 try {
adamc@1260 1504 return new ActiveXObject("Msxml2.XMLHTTP");
adamc@1260 1505 } catch (e) {
adamc@1260 1506 try {
adamc@1260 1507 return new ActiveXObject("Microsoft.XMLHTTP");
adamc@1260 1508 } catch (e) {
adamc@1260 1509 whine("Your browser doesn't seem to support AJAX.");
adamc@1260 1510 }
adamc@1260 1511 }
adamc@609 1512 }
adamc@609 1513 }
adamc@609 1514
adamc@736 1515 var sig = null;
adamc@736 1516
adamc@1042 1517 var unloading = false, inFlight = null;
adamc@1042 1518
adamc@1042 1519 function unload() {
adamc@1260 1520 for (; inFlight; inFlight = inFlight.next) {
adamc@1260 1521 inFlight.data.abort();
adamc@1260 1522 }
adamc@1042 1523 }
adamc@1042 1524
adam@1691 1525 function requestUri(xhr, uri, needsSig, isRpc) {
adam@1691 1526 var extraData = null;
adam@1691 1527
adam@1691 1528 if (isRpc && uri.length > 2000) {
adam@1691 1529 extraData = uri.substring(2000);
adam@1691 1530 uri = uri.substring(0, 2000);
adam@1691 1531 }
adam@1691 1532
vshabanoff@1763 1533 xhr.open("POST", uri, !unloading);
adamc@1260 1534 xhr.setRequestHeader("Content-type", "text/plain");
adamc@668 1535
adamc@1260 1536 if (client_id != null) {
adamc@1260 1537 xhr.setRequestHeader("UrWeb-Client", client_id.toString());
adamc@1260 1538 xhr.setRequestHeader("UrWeb-Pass", client_pass.toString());
adamc@1260 1539 }
adamc@668 1540
adamc@1260 1541 if (needsSig) {
adamc@1260 1542 if (sig == null)
adamc@1260 1543 whine("Missing cookie signature!");
adamc@736 1544
adamc@1260 1545 xhr.setRequestHeader("UrWeb-Sig", sig);
adamc@1260 1546 }
adamc@736 1547
adamc@1260 1548 inFlight = cons(xhr, inFlight);
adam@1691 1549 xhr.send(extraData);
adamc@668 1550 }
adamc@668 1551
adamc@1042 1552 function xhrFinished(xhr) {
adamc@1260 1553 xhr.abort();
adamc@1260 1554 inFlight = remove(xhr, inFlight);
adamc@1042 1555 }
adamc@1042 1556
adamc@1111 1557 function unurlify(parse, s) {
adamc@1260 1558 return parse(s);
adamc@1111 1559 }
adamc@1111 1560
adam@1385 1561 function redirect(s) {
adam@1385 1562 window.location = s;
adam@1385 1563 }
adam@1385 1564
adam@1848 1565 function makeSome(isN, v) {
adam@1848 1566 if (isN)
adam@1848 1567 return {v: v};
adam@1848 1568 else
adam@1848 1569 return v;
adam@1848 1570 }
adam@1848 1571
adam@1848 1572 function rc(prefix, uri, parse, k, needsSig, isN) {
adam@1792 1573 if (!maySuspend)
adam@1794 1574 er("May not 'rpc' in main thread of 'code' for <active>");
adam@1792 1575
adamc@1260 1576 uri = cat(prefix, uri);
adamc@1260 1577 uri = flattenLocal(uri);
adamc@1260 1578 var xhr = getXHR();
adamc@609 1579
adamc@1260 1580 xhr.onreadystatechange = function() {
adamc@1260 1581 if (xhr.readyState == 4) {
adamc@1260 1582 var isok = false;
adamc@612 1583
adamc@1260 1584 try {
adamc@1260 1585 if (xhr.status == 200)
adamc@1260 1586 isok = true;
adamc@1260 1587 } catch (e) { }
adamc@612 1588
adamc@1260 1589 if (isok) {
adam@1446 1590 var lines = xhr.responseText.split("\n");
adam@1848 1591 if (lines.length != 2) {
adam@1848 1592 if (isN == null)
adam@1848 1593 whine("Bad RPC response lines");
adam@1848 1594 else
adam@1848 1595 k(null);
adam@1848 1596 } else {
adam@1848 1597 eval(lines[0]);
adam@1446 1598
adam@1848 1599 try {
adam@1848 1600 var v = parse(lines[1]);
adam@1848 1601 try {
adam@1848 1602 k(makeSome(isN, v));
adam@1848 1603 } catch (v) {
adam@1848 1604 doExn(v);
adam@1848 1605 }
adam@1848 1606 } catch (v) {
adam@1848 1607 k(null);
adam@1848 1608 }
adamc@1260 1609 }
adamc@1260 1610 } else {
adam@1848 1611 if (isN == null)
adam@2188 1612 conn(xhr.responseText);
adam@1848 1613 else
adam@1848 1614 k(null);
adamc@1260 1615 }
adamc@1260 1616
adamc@1260 1617 xhrFinished(xhr);
adamc@729 1618 }
adamc@1260 1619 };
adamc@1042 1620
adam@1691 1621 requestUri(xhr, uri, needsSig, true);
adamc@609 1622 }
adamc@667 1623
adamc@667 1624 function path_join(s1, s2) {
adamc@1260 1625 if (s1.length > 0 && s1.charAt(s1.length-1) == '/')
adamc@1260 1626 return s1 + s2;
adamc@1260 1627 else
adamc@1260 1628 return s1 + "/" + s2;
adamc@667 1629 }
adamc@667 1630
adamc@670 1631 var channels = [];
adamc@670 1632
adamc@670 1633 function newQueue() {
adamc@1260 1634 return { front : null, back : null };
adamc@670 1635 }
adamc@670 1636 function enqueue(q, v) {
adamc@1260 1637 if (q.front == null) {
adamc@1260 1638 q.front = cons(v, null);
adamc@1260 1639 q.back = q.front;
adamc@1260 1640 } else {
adamc@1260 1641 var node = cons(v, null);
adamc@1260 1642 q.back.next = node;
adamc@1260 1643 q.back = node;
adamc@1260 1644 }
adamc@670 1645 }
adamc@670 1646 function dequeue(q) {
adamc@670 1647 if (q.front == null)
adamc@1260 1648 return null;
adamc@1260 1649 else {
adamc@1260 1650 var r = q.front.data;
adamc@1260 1651 q.front = q.front.next;
adamc@1260 1652 if (q.front == null)
adamc@1260 1653 q.back = null;
adamc@1260 1654 return r;
adamc@1260 1655 }
adamc@670 1656 }
adamc@670 1657
adamc@670 1658 function newChannel() {
adamc@1260 1659 return { msgs : newQueue(), listeners : newQueue() };
adamc@670 1660 }
adamc@670 1661
adamc@667 1662 function listener() {
adamc@1260 1663 var uri = path_join(url_prefix, ".msgs");
adamc@1260 1664 var xhr = getXHR();
adamc@1260 1665 var tid, orsc, onTimeout;
adamc@673 1666
adamc@1260 1667 var connect = function () {
adamc@1260 1668 xhr.onreadystatechange = orsc;
adamc@1260 1669 tid = window.setTimeout(onTimeout, timeout * 500);
adam@1691 1670 requestUri(xhr, uri, false, false);
adamc@1260 1671 }
adamc@673 1672
adamc@1260 1673 orsc = function() {
adamc@1260 1674 if (xhr.readyState == 4) {
adamc@1260 1675 window.clearTimeout(tid);
adamc@673 1676
adamc@1260 1677 var isok = false;
adamc@667 1678
adamc@1260 1679 try {
adamc@1260 1680 if (xhr.status == 200)
adamc@1260 1681 isok = true;
adamc@1260 1682 } catch (e) { }
adamc@667 1683
adamc@1260 1684 if (isok) {
adam@1447 1685 var text = xhr.responseText;
adamc@1260 1686 if (text == "")
adamc@1260 1687 return;
adamc@1260 1688 var lines = text.split("\n");
adamc@889 1689
adam@1552 1690 if (lines.length == 1 && lines[0] == "R") {
adam@1553 1691 lameDuck = true;
adam@1553 1692
adam@1552 1693 if (isPost)
adam@1552 1694 history.back();
adam@1552 1695 else
adam@1552 1696 location.reload();
adam@1552 1697
adam@1552 1698 return;
adam@1552 1699 }
adam@1552 1700
adamc@1260 1701 if (lines.length < 2) {
adamc@1260 1702 discon();
adamc@1260 1703 return;
adamc@1260 1704 }
adamc@1260 1705
adam@1447 1706 var messageReader = function(i) {
adam@1447 1707 if (i+1 >= lines.length) {
adam@1447 1708 xhrFinished(xhr);
adam@1447 1709 connect();
adam@1447 1710 }
adam@1447 1711 else {
adam@1447 1712 var chn = lines[i];
adam@1447 1713 var msg = lines[i+1];
adamc@1260 1714
adam@1447 1715 if (chn == "E") {
adam@1447 1716 eval(msg);
adam@1447 1717 window.setTimeout(function() { messageReader(i+2); }, 0);
adam@1447 1718 } else {
adam@1447 1719 if (chn < 0)
adam@1447 1720 whine("Out-of-bounds channel in message from remote server");
adamc@1260 1721
adam@1447 1722 var ch;
adamc@1260 1723
adam@1447 1724 if (chn >= channels.length || channels[chn] == null) {
adam@1447 1725 ch = newChannel();
adam@1447 1726 channels[chn] = ch;
adam@1447 1727 } else
adam@1447 1728 ch = channels[chn];
adamc@1260 1729
adam@1447 1730 var listener = dequeue(ch.listeners);
adam@1447 1731 if (listener == null) {
adam@1447 1732 enqueue(ch.msgs, msg);
adam@1447 1733 } else {
adam@1447 1734 try {
adam@1447 1735 listener(msg);
adam@1447 1736 } catch (v) {
adam@1447 1737 doExn(v);
adam@1447 1738 }
adam@1447 1739 }
adam@1447 1740
adam@1447 1741 messageReader(i+2);
adamc@1260 1742 }
adamc@1260 1743 }
adamc@1260 1744 }
adamc@1260 1745
adam@1447 1746 messageReader(0);
adamc@1260 1747 }
adamc@1260 1748 else {
adamc@1260 1749 try {
adamc@1260 1750 if (xhr.status != 0)
adamc@1260 1751 servErr("Error querying remote server for messages: " + xhr.status);
adamc@1260 1752 } catch (e) { }
adamc@1260 1753 }
adamc@729 1754 }
adamc@1260 1755 };
adamc@669 1756
adamc@1260 1757 onTimeout = function() {
adamc@1260 1758 xhrFinished(xhr);
adamc@1260 1759 connect();
adamc@1260 1760 };
adamc@670 1761
adamc@673 1762 connect();
adamc@667 1763 }
adamc@670 1764
adamc@670 1765 function rv(chn, parse, k) {
adam@1792 1766 if (!maySuspend)
adam@1794 1767 er("May not 'recv' in main thread of 'code' for <active>");
adam@1792 1768
adamc@1260 1769 if (chn == null)
adam@1942 1770 er("Client-side code tried to recv() from a channel belonging to a different page view.");
adamc@682 1771
adamc@1260 1772 if (chn < 0)
adamc@1260 1773 whine("Out-of-bounds channel receive");
adamc@670 1774
adamc@1260 1775 var ch;
adamc@670 1776
adamc@1260 1777 if (chn >= channels.length || channels[chn] == null) {
adamc@1260 1778 ch = newChannel();
adamc@1260 1779 channels[chn] = ch;
adamc@1260 1780 } else
adamc@1260 1781 ch = channels[chn];
adamc@670 1782
adamc@1260 1783 var msg = dequeue(ch.msgs);
adamc@1260 1784 if (msg == null) {
adamc@1260 1785 enqueue(ch.listeners, function(msg) { k(parse(msg)); });
adamc@1260 1786 } else {
adamc@1260 1787 try {
adamc@1260 1788 k(parse(msg));
adamc@1260 1789 } catch (v) {
adamc@1260 1790 doExn(v);
adamc@1260 1791 }
adamc@728 1792 }
adamc@670 1793 }
adamc@693 1794
adamc@978 1795 function sl(ms, k) {
adam@1792 1796 if (!maySuspend)
adam@1794 1797 er("May not 'sleep' in main thread of 'code' for <active>");
adam@1792 1798
adamc@1260 1799 window.setTimeout(function() { k(null); }, ms);
adamc@1021 1800 }
adamc@1021 1801
adamc@1021 1802 function sp(e) {
adam@1794 1803 window.setTimeout(function() { execF(e); }, 0);
adamc@978 1804 }
adamc@978 1805
adamc@693 1806
adamc@970 1807 // The Ur interpreter
adamc@970 1808
adamc@970 1809 var urfuncs = [];
adamc@970 1810
adamc@970 1811 function lookup(env, n) {
adamc@1260 1812 while (env != null) {
adamc@1260 1813 if (n == 0)
adamc@1260 1814 return env.data;
adamc@1260 1815 else {
adamc@1260 1816 --n;
adamc@1260 1817 env = env.next;
adamc@1260 1818 }
adamc@970 1819 }
adamc@970 1820
adamc@1260 1821 whine("Out-of-bounds Ur variable reference");
adamc@970 1822 }
adamc@970 1823
adamc@974 1824 function execP(env, p, v) {
adamc@1260 1825 switch (p.c) {
adamc@1260 1826 case "w":
adamc@1260 1827 return env;
adamc@1260 1828 case "v":
adamc@1260 1829 return cons(v, env);
adamc@1260 1830 case "c":
adamc@1260 1831 if (v == p.v)
adamc@1260 1832 return env;
adamc@1260 1833 else
adamc@1260 1834 return false;
adamc@1260 1835 case "s":
adamc@1260 1836 if (v == null)
adamc@1260 1837 return false;
adamc@1260 1838 else
adamc@1260 1839 return execP(env, p.p, p.n ? v.v : v);
adamc@1260 1840 case "1":
adamc@1260 1841 if (v.n != p.n)
adamc@1260 1842 return false;
adamc@1260 1843 else
adamc@1260 1844 return execP(env, p.p, v.v);
adamc@1260 1845 case "r":
adamc@1260 1846 for (var fs = p.l; fs != null; fs = fs.next) {
adamc@1260 1847 env = execP(env, fs.data.p, v["_" + fs.data.n]);
adamc@1260 1848 if (env == false)
adamc@1260 1849 return false;
adamc@1260 1850 }
adamc@1260 1851 return env;
adamc@1260 1852 default:
adamc@1260 1853 whine("Unknown Ur pattern kind" + p.c);
adamc@974 1854 }
adamc@974 1855 }
adamc@974 1856
adamc@970 1857 function exec0(env, e) {
adamc@1260 1858 return exec1(env, null, e);
adamc@1020 1859 }
adamc@1020 1860
adamc@1020 1861 function exec1(env, stack, e) {
adamc@1260 1862 var stack, usedK = false;
adamc@970 1863
adamc@1260 1864 var saveEnv = function() {
adamc@1260 1865 if (stack.next != null && stack.next.data.c != "<")
adamc@1260 1866 stack = cons({c: "<", env: env}, stack.next);
adamc@1260 1867 else
adamc@1260 1868 stack = stack.next;
adamc@1260 1869 };
adamc@983 1870
adamc@1260 1871 while (true) {
adamc@1260 1872 switch (e.c) {
adamc@1260 1873 case "c":
adamc@1260 1874 var v = e.v;
adamc@1260 1875 if (stack == null)
adamc@1260 1876 return v;
adamc@1260 1877 var fr = stack.data;
adamc@970 1878
adamc@1260 1879 switch (fr.c) {
adamc@1260 1880 case "s":
adamc@1260 1881 e = {c: "c", v: {v: v}};
adamc@1260 1882 stack = stack.next;
adamc@1260 1883 break;
adamc@1260 1884 case "1":
adamc@1260 1885 e = {c: "c", v: {n: fr.n, v: v}};
adamc@1260 1886 stack = stack.next;
adamc@1260 1887 break;
adamc@1260 1888 case "f":
adamc@1260 1889 fr.args[fr.pos++] = v;
adamc@1260 1890 if (fr.a == null) {
adamc@1260 1891 var res;
adamc@1260 1892 stack = stack.next;
adamc@1035 1893
adamc@1260 1894 if (fr.f.apply)
adamc@1260 1895 res = fr.f.apply(null, fr.args);
adamc@1260 1896 else if (fr.args.length == 0)
adamc@1260 1897 res = fr.f();
adamc@1260 1898 else if (fr.args.length == 1)
adamc@1260 1899 res = fr.f(fr.args[0]);
adamc@1260 1900 else if (fr.args.length == 2)
adamc@1260 1901 res = fr.f(fr.args[0], fr.args[1]);
adamc@1260 1902 else if (fr.args.length == 3)
adamc@1260 1903 res = fr.f(fr.args[0], fr.args[1], fr.args[2]);
adamc@1260 1904 else if (fr.args.length == 4)
adamc@1260 1905 res = fr.f(fr.args[0], fr.args[1], fr.args[2], fr.args[3]);
adamc@1260 1906 else if (fr.args.length == 5)
adamc@1260 1907 res = fr.f(fr.args[0], fr.args[1], fr.args[2], fr.args[3], fr.args[4]);
adamc@1260 1908 else
adamc@1260 1909 whine("Native function has " + fr.args.length + " args, but there is no special case for that count.");
adamc@1035 1910
adamc@1260 1911 e = {c: "c", v: res};
adamc@1260 1912 if (usedK) return null;
adamc@1260 1913 } else {
adamc@1260 1914 e = fr.a.data;
adamc@1260 1915 fr.a = fr.a.next;
adamc@1260 1916 }
adamc@1260 1917 break;
adamc@1260 1918 case "a1":
adamc@1260 1919 e = fr.x;
adamc@1260 1920 stack = cons({c: "a2", f: v}, stack.next);
adamc@1260 1921 break;
adamc@1260 1922 case "a2":
adamc@1260 1923 if (fr.f == null)
adamc@1260 1924 whine("Ur: applying null function");
adamc@1260 1925 else if (fr.f.body) {
adamc@1260 1926 saveEnv();
adamc@1260 1927 env = cons(v, fr.f.env);
adamc@1260 1928 e = fr.f.body;
adamc@1260 1929 } else {
adamc@1260 1930 e = {c: "c", v: fr.f(v)};
adamc@1260 1931 stack = stack.next;
adamc@1260 1932 }
adamc@1260 1933 break;
adamc@1260 1934 case "<":
adamc@1260 1935 env = fr.env;
adamc@1260 1936 stack = stack.next;
adamc@1260 1937 break;
adamc@1260 1938 case "r":
adamc@1260 1939 fr.fs["_" + fr.n] = v;
adamc@1260 1940 if (fr.l == null) {
adamc@1260 1941 e = {c: "c", v: fr.fs};
adamc@1260 1942 stack = stack.next;
adamc@1260 1943 } else {
adamc@1260 1944 fr.n = fr.l.data.n;
adamc@1260 1945 e = fr.l.data.v;
adamc@1260 1946 fr.l = fr.l.next;
adamc@1260 1947 }
adamc@1260 1948 break;
adamc@1260 1949 case ".":
adamc@1260 1950 e = {c: "c", v: v["_" + fr.f]};
adamc@1260 1951 stack = stack.next;
adamc@1260 1952 break;
adamc@1260 1953 case ";":
adamc@1260 1954 e = fr.e2;
adamc@1260 1955 stack = stack.next;
adamc@1260 1956 break;
adamc@1260 1957 case "=":
adamc@1260 1958 saveEnv();
adamc@1260 1959 env = cons(v, env);
adamc@1260 1960 e = fr.e2;
adamc@1260 1961 break;
adamc@1260 1962 case "m":
adamc@1260 1963 var ps;
adamc@1260 1964 for (ps = fr.p; ps != null; ps = ps.next) {
adamc@1260 1965 var r = execP(env, ps.data.p, v);
adamc@1260 1966 if (r != false) {
adamc@1260 1967 saveEnv();
adamc@1260 1968 env = r;
adamc@1260 1969 e = ps.data.b;
adamc@1260 1970 break;
adamc@1260 1971 }
adamc@1260 1972 }
adamc@1260 1973 if (ps == null)
adamc@1260 1974 whine("Match failure in Ur interpretation");
adamc@1260 1975 break;
adamc@1260 1976 default:
adamc@1260 1977 whine("Unknown Ur continuation kind " + fr.c);
adamc@1260 1978 }
adamc@1260 1979
adamc@1260 1980 break;
adamc@1260 1981 case "v":
adamc@1260 1982 e = {c: "c", v: lookup(env, e.n)};
adamc@1260 1983 break;
adamc@1260 1984 case "n":
adamc@1260 1985 var idx = e.n;
adamc@1260 1986 e = urfuncs[idx];
adamc@1260 1987 if (e.c == "t")
adamc@1262 1988 e = urfuncs[idx] = eval("(" + e.f + ")");
adamc@1260 1989 break;
adamc@1260 1990 case "s":
adamc@1260 1991 stack = cons({c: "s"}, stack);
adamc@1260 1992 e = e.v;
adamc@1260 1993 break;
adamc@1260 1994 case "1":
adamc@1260 1995 stack = cons({c: "1", n: e.n}, stack);
adamc@1260 1996 e = e.v;
adamc@1260 1997 break;
adamc@1260 1998 case "f":
adamc@1260 1999 if (e.a == null)
adam@1798 2000 e = {c: "c", v: e.f()};
adamc@1260 2001 else {
adamc@1260 2002 var args = [];
adam@1798 2003 stack = cons({c: "f", f: e.f, args: args, pos: 0, a: e.a.next}, stack);
adamc@1260 2004 if (!e.a.data.c) alert("[2] fr.f = " + e.f + "; 0 = " + e.a.data);
adamc@1260 2005 e = e.a.data;
adamc@1260 2006 }
adamc@1260 2007 break;
adamc@1260 2008 case "l":
adamc@1260 2009 e = {c: "c", v: {env: env, body: e.b}};
adamc@1260 2010 break;
adamc@1260 2011 case "a":
adamc@1260 2012 stack = cons({c: "a1", x: e.x}, stack);
adamc@1260 2013 e = e.f;
adamc@1260 2014 break;
adamc@1260 2015 case "r":
adamc@1260 2016 if (e.l == null)
adamc@1260 2017 whine("Empty Ur record in interpretation");
adamc@1260 2018 var fs = {};
adamc@1260 2019 stack = cons({c: "r", n: e.l.data.n, fs: fs, l: e.l.next}, stack);
adamc@1260 2020 e = e.l.data.v;
adamc@1260 2021 break;
adamc@1260 2022 case ".":
adamc@1260 2023 stack = cons({c: ".", f: e.f}, stack);
adamc@1260 2024 e = e.r;
adamc@1260 2025 break;
adamc@1260 2026 case ";":
adamc@1260 2027 stack = cons({c: ";", e2: e.e2}, stack);
adamc@1260 2028 e = e.e1;
adamc@1260 2029 break;
adamc@1260 2030 case "=":
adamc@1260 2031 stack = cons({c: "=", e2: e.e2}, stack);
adamc@1260 2032 e = e.e1;
adamc@1260 2033 break;
adamc@1260 2034 case "m":
adamc@1260 2035 stack = cons({c: "m", p: e.p}, stack);
adamc@1260 2036 e = e.e;
adamc@1260 2037 break;
adamc@1260 2038 case "e":
adamc@1260 2039 e = {c: "c", v: cs({c: "wc", env: env, body: e.e})};
adamc@1260 2040 break;
adamc@1260 2041 case "wc":
adamc@1260 2042 env = e.env;
adamc@1260 2043 e = e.body;
adamc@1260 2044 break;
adamc@1260 2045 case "K":
adamc@1260 2046 { var savedStack = stack.next, savedEnv = env;
adamc@1260 2047 e = {c: "c", v: function(v) { return exec1(savedEnv, savedStack, {c: "c", v: v}); } };}
adamc@1260 2048 usedK = true;
rmbruijn@1597 2049 break;
adamc@1260 2050 default:
adamc@1260 2051 whine("Unknown Ur expression kind " + e.c);
adamc@970 2052 }
adamc@970 2053 }
adamc@970 2054 }
adamc@970 2055
adamc@976 2056 function execD(e) {
adamc@1260 2057 return exec0(null, e);
adamc@976 2058 }
adamc@976 2059
adamc@970 2060 function exec(e) {
adamc@1260 2061 var r = exec0(null, e);
adamc@970 2062
adamc@1260 2063 if (r != null && r.body != null)
adamc@1260 2064 return function(v) { return exec0(cons(v, r.env), r.body); };
adamc@1260 2065 else
adamc@1260 2066 return r;
adamc@895 2067 }
adamc@895 2068
adamc@977 2069 function execF(f, x) {
adamc@1260 2070 return exec0(cons(x, f.env), f.body);
adamc@977 2071 }
adamc@977 2072
adamc@895 2073
adam@1290 2074 // Wrappers
adam@1290 2075
adam@1290 2076 function confrm(s) {
adam@1290 2077 return confirm(s) ? true : false;
adam@1290 2078 }
adam@1290 2079
adam@1290 2080
adamc@1323 2081 // URL blessing
adamc@1323 2082
adamc@1323 2083 var urlRules = null;
adamc@1323 2084
adamc@1323 2085 function checkUrl(s) {
adamc@1323 2086 for (var r = urlRules; r; r = r.next) {
adamc@1323 2087 var ru = r.data;
adamc@1323 2088 if (ru.prefix ? s.indexOf(ru.pattern) == 0 : s == ru.pattern)
adamc@1323 2089 return ru.allow ? s : null;
adamc@1323 2090 }
adamc@1323 2091
adamc@1323 2092 return null;
adamc@1323 2093 }
adamc@1323 2094
adamc@1323 2095 function bless(s) {
adamc@1323 2096 u = checkUrl(s);
adamc@1323 2097 if (u == null)
adamc@1323 2098 er("Disallowed URL: " + s);
adamc@1323 2099 return u;
adamc@1323 2100 }
adamc@1323 2101
adam@1556 2102
adam@2008 2103 // Attribute name blessing
adam@2008 2104
adam@2008 2105 function blessData(s) {
adam@2008 2106 for (var i = 0; i < s.length; ++i) {
adam@2008 2107 var c = s[i];
adam@2008 2108 if (!isAlnum(c) && c != '-' && c != '_')
adam@2008 2109 er("Disallowed character in data-* attribute name");
adam@2008 2110 }
adam@2008 2111
adam@2008 2112 return s;
adam@2008 2113 }
adam@2008 2114
adam@2008 2115
adam@1755 2116 // CSS validation
adam@1755 2117
adam@1755 2118 function atom(s) {
adam@1755 2119 for (var i = 0; i < s.length; ++i) {
adam@1755 2120 var c = s[i];
adam@1755 2121 if (!isAlnum(c) && c != '+' && c != '-' && c != '.' && c != '%' && c != '#')
adam@1755 2122 er("Disallowed character in CSS atom");
adam@1755 2123 }
adam@1755 2124
adam@1755 2125 return s;
adam@1755 2126 }
adam@1755 2127
adam@1755 2128 function css_url(s) {
adam@1755 2129 for (var i = 0; i < s.length; ++i) {
adam@1755 2130 var c = s[i];
adam@1755 2131 if (!isAlnum(c) && c != ':' && c != '/' && c != '.' && c != '_' && c != '+'
adam@1755 2132 && c != '-' && c != '%' && c != '?' && c != '&' && c != '=' && c != '#')
adam@1755 2133 er("Disallowed character in CSS URL");
adam@1755 2134 }
adam@1755 2135
adam@1755 2136 return s;
adam@1755 2137 }
adam@1755 2138
adam@1755 2139 function property(s) {
adam@1755 2140 if (s.length <= 0)
adam@1755 2141 er("Empty CSS property");
adam@1755 2142
adam@1755 2143 if (!isLower(s[0]) && s[0] != '_')
adam@1755 2144 er("Bad initial character in CSS property");
adam@1755 2145
adam@1755 2146 for (var i = 0; i < s.length; ++i) {
adam@1755 2147 var c = s[i];
adam@1755 2148 if (!isLower(c) && !isDigit(c) && c != '_' && c != '-')
adam@1755 2149 er("Disallowed character in CSS property");
adam@1755 2150 }
adam@1755 2151
adam@1755 2152 return s;
adam@1755 2153 }
adam@1755 2154
adam@1755 2155
adam@1556 2156 // ID generation
adam@1556 2157
adam@1556 2158 var nextId = 0;
adam@1556 2159
adam@1556 2160 function fresh() {
adam@1703 2161 return "uw" + (--nextId);
adam@1556 2162 }
adam@1556 2163
adam@1785 2164 function giveFocus(id) {
adam@1803 2165 var node = document.getElementById(id);
adam@1785 2166
adam@1803 2167 if (node)
adam@1803 2168 node.focus();
adam@1803 2169 else
adam@1803 2170 er("Tried to give focus to ID not used in document: " + id);
adam@1785 2171 }
adam@1785 2172
rmbruijn@1597 2173
adamc@693 2174 // App-specific code