annotate lib/js/urweb.js @ 669:f68eee90dbcf

Press a button to send a message
author Adam Chlipala <adamc@hcoop.net>
date Sun, 22 Mar 2009 15:16:34 -0400
parents b0c1a46b1f15
children f73913d97a40
rev   line source
adamc@580 1 function cons(v, ls) {
adamc@580 2 return { n : ls, v : v };
adamc@580 3 }
adamc@579 4 function callAll(ls) {
adamc@585 5 for (; ls; ls = ls.n)
adamc@579 6 ls.v();
adamc@579 7 }
adamc@574 8
adamc@580 9 function sc(v) {
adamc@580 10 return {v : v, h : null};
adamc@580 11 }
adamc@580 12 function sv(s, v) {
adamc@580 13 s.v = v;
adamc@580 14 callAll(s.h);
adamc@580 15 }
adamc@601 16 function sg(s) {
adamc@601 17 return s.v;
adamc@601 18 }
adamc@579 19
adamc@580 20 function ss(s) {
adamc@580 21 return s;
adamc@580 22 }
adamc@580 23 function sr(v) {
adamc@580 24 return {v : v, h : null};
adamc@580 25 }
adamc@580 26 function sb(x,y) {
adamc@580 27 var z = y(x.v);
adamc@580 28 var s = {v : z.v, h : null};
adamc@580 29
adamc@580 30 function reZ() {
adamc@580 31 z.h = cons(function() { s.v = z.v; callAll(s.h); }, z.h);
adamc@580 32 }
adamc@580 33
adamc@580 34 x.h = cons(function() { z = y(x.v); reZ(); s.v = z.v; callAll(s.h); }, x.h);
adamc@580 35 reZ();
adamc@580 36
adamc@580 37 return s;
adamc@580 38 }
adamc@571 39
adamc@604 40 function lastParent() {
adamc@604 41 var pos = document;
adamc@604 42
adamc@600 43 while (pos.lastChild && pos.lastChild.nodeType == 1)
adamc@600 44 pos = pos.lastChild;
adamc@600 45
adamc@600 46 return pos.parentNode;
adamc@600 47 }
adamc@600 48
adamc@604 49 var thisScript = null;
adamc@603 50
adamc@604 51 function addNode(node) {
adamc@604 52 if (thisScript) {
adamc@604 53 thisScript.parentNode.appendChild(node);
adamc@604 54 thisScript.parentNode.removeChild(thisScript);
adamc@604 55 } else
adamc@604 56 lastParent().appendChild(node);
adamc@603 57 }
adamc@603 58
adamc@604 59 function runScripts(node) {
adamc@604 60 var savedScript = thisScript;
adamc@603 61
adamc@646 62 var scripts = node.getElementsByTagName("script"), scriptsCopy = {};
adamc@604 63 var len = scripts.length;
adamc@646 64 for (var i = 0; i < len; ++i)
adamc@646 65 scriptsCopy[i] = scripts[i];
adamc@604 66 for (var i = 0; i < len; ++i) {
adamc@646 67 thisScript = scriptsCopy[i];
adamc@604 68 eval(thisScript.textContent);
adamc@604 69 }
adamc@604 70
adamc@604 71 thisScript = savedScript;
adamc@603 72 }
adamc@603 73
adamc@603 74 function populate(node, html) {
adamc@603 75 node.innerHTML = html;
adamc@604 76 runScripts(node);
adamc@603 77 }
adamc@603 78
adamc@571 79 function dyn(s) {
adamc@571 80 var x = document.createElement("span");
adamc@604 81 populate(x, s.v);
adamc@604 82 addNode(x);
adamc@603 83 s.h = cons(function() { populate(x, s.v) }, s.h);
adamc@571 84 }
adamc@582 85
adamc@598 86 function inp(t, s) {
adamc@598 87 var x = document.createElement(t);
adamc@598 88 x.value = s.v;
adamc@604 89 addNode(x);
adamc@598 90 s.h = cons(function() { x.value = s.v }, s.h);
adamc@598 91 x.onkeyup = function() { sv(s, x.value) };
adamc@606 92 return x;
adamc@598 93 }
adamc@598 94
adamc@597 95 function eh(x) {
adamc@597 96 return x.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
adamc@597 97 }
adamc@597 98
adamc@582 99 function ts(x) { return x.toString() }
adamc@586 100 function bs(b) { return (b ? "True" : "False") }
adamc@586 101
adamc@649 102 function pi(s) {
adamc@649 103 var r = parseInt(s);
adamc@649 104 if (r.toString() == s)
adamc@649 105 return r;
adamc@649 106 else
adamc@649 107 throw "Can't parse int: " + s;
adamc@649 108 }
adamc@649 109
adamc@649 110 function pfl(s) {
adamc@649 111 var r = parseFloat(s);
adamc@649 112 if (r.toString() == s)
adamc@649 113 return r;
adamc@649 114 else
adamc@649 115 throw "Can't parse float: " + s;
adamc@649 116 }
adamc@649 117
adamc@669 118 function whine(msg) {
adamc@669 119 alert(msg);
adamc@669 120 throw msg;
adamc@669 121 }
adamc@669 122
adamc@649 123 function pf() {
adamc@669 124 whine("Pattern match failure");
adamc@649 125 }
adamc@589 126
adamc@603 127 var closures = [];
adamc@603 128
adamc@603 129 function ca(f) {
adamc@603 130 var n = closures.length;
adamc@603 131 closures[n] = f;
adamc@603 132 return n;
adamc@603 133 }
adamc@603 134
adamc@603 135 function cr(n) {
adamc@603 136 return closures[n]();
adamc@603 137 }
adamc@603 138
adamc@609 139
adamc@668 140 var client_id = 0;
adamc@668 141 var client_pass = 0;
adamc@668 142 var url_prefix = "/";
adamc@668 143
adamc@668 144 function getXHR(uri)
adamc@609 145 {
adamc@609 146 try {
adamc@609 147 return new XMLHttpRequest();
adamc@609 148 } catch (e) {
adamc@609 149 try {
adamc@609 150 return new ActiveXObject("Msxml2.XMLHTTP");
adamc@609 151 } catch (e) {
adamc@609 152 try {
adamc@609 153 return new ActiveXObject("Microsoft.XMLHTTP");
adamc@609 154 } catch (e) {
adamc@609 155 throw "Your browser doesn't seem to support AJAX.";
adamc@609 156 }
adamc@609 157 }
adamc@609 158 }
adamc@609 159 }
adamc@609 160
adamc@668 161 function requestUri(xhr, uri) {
adamc@668 162 xhr.open("GET", uri, true);
adamc@668 163
adamc@668 164 if (client_id != 0) {
adamc@668 165 xhr.setRequestHeader("UrWeb-Client", client_id.toString());
adamc@668 166 xhr.setRequestHeader("UrWeb-Pass", client_pass.toString());
adamc@668 167 }
adamc@668 168
adamc@668 169 xhr.send(null);
adamc@668 170 }
adamc@668 171
adamc@613 172 function rc(uri, parse, k) {
adamc@609 173 var xhr = getXHR();
adamc@609 174
adamc@609 175 xhr.onreadystatechange = function() {
adamc@612 176 if (xhr.readyState == 4) {
adamc@612 177 var isok = false;
adamc@612 178
adamc@612 179 try {
adamc@612 180 if (xhr.status == 200)
adamc@612 181 isok = true;
adamc@612 182 } catch (e) { }
adamc@612 183
adamc@612 184 if (isok)
adamc@613 185 k(parse(xhr.responseText));
adamc@649 186 else {
adamc@669 187 whine("Error querying remote server!");
adamc@649 188 }
adamc@612 189 }
adamc@609 190 };
adamc@609 191
adamc@668 192 requestUri(xhr, uri);
adamc@609 193 }
adamc@667 194
adamc@667 195
adamc@667 196 function path_join(s1, s2) {
adamc@667 197 if (s1.length > 0 && s1[s1.length-1] == '/')
adamc@667 198 return s1 + s2;
adamc@667 199 else
adamc@667 200 return s1 + "/" + s2;
adamc@667 201 }
adamc@667 202
adamc@667 203 function listener() {
adamc@668 204 var uri = path_join(url_prefix, ".msgs");
adamc@667 205 var xhr = getXHR();
adamc@669 206 var orsc = function() {
adamc@667 207 if (xhr.readyState == 4) {
adamc@667 208 var isok = false;
adamc@667 209
adamc@667 210 try {
adamc@667 211 if (xhr.status == 200)
adamc@667 212 isok = true;
adamc@667 213 } catch (e) { }
adamc@667 214
adamc@668 215 if (isok) {
adamc@669 216 var lines = xhr.responseText.split("\n");
adamc@669 217 if (lines.length < 2)
adamc@669 218 whine("Empty message from remote server");
adamc@669 219
adamc@669 220 for (var i = 0; i+1 < lines.length; i += 2) {
adamc@669 221 alert("Message(" + lines[i] + "): " + lines[i+1]);
adamc@669 222 }
adamc@669 223
adamc@669 224 xhr.onreadystatechange = orsc;
adamc@668 225 requestUri(xhr, uri);
adamc@668 226 }
adamc@667 227 else {
adamc@669 228 whine("Error querying remote server for messages!");
adamc@667 229 }
adamc@667 230 }
adamc@667 231 };
adamc@667 232
adamc@669 233 xhr.onreadystatechange = orsc;
adamc@668 234 requestUri(xhr, uri);
adamc@667 235 }