annotate lib/js/urweb.js @ 2156:d857da05f042

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