comparison lib/js/urweb.js @ 899:25a038a9194b

dtable example and demos both working
author Adam Chlipala <adamc@hcoop.net>
date Thu, 06 Aug 2009 11:54:28 -0400
parents d1d0b18afd3d
children ed381288ce9a
comparison
equal deleted inserted replaced
898:d1d0b18afd3d 899:25a038a9194b
226 function parent() { 226 function parent() {
227 return thisScript ? thisScript.parentNode : lastParent(); 227 return thisScript ? thisScript.parentNode : lastParent();
228 } 228 }
229 229
230 function addNode(node) { 230 function addNode(node) {
231 if (thisScript) { 231 if (thisScript)
232 thisScript.parentNode.appendChild(node); 232 thisScript.parentNode.replaceChild(node, thisScript);
233 thisScript.parentNode.removeChild(thisScript); 233 else
234 } else {
235 lastParent().appendChild(node); 234 lastParent().appendChild(node);
236 } 235 }
237 }
238
239 function setHTML(html) {
240 var x = document.createElement("span");
241 x.dead = false;
242 x.signal = null;
243 x.sources = null;
244 x.closures = null;
245 x.innerHTML = html;
246 addNode(x);
247 runScripts(x);
248 }
249 236
250 var thisScript = null; 237 var thisScript = null;
251 238
252 function runScripts(node) { 239 function runScripts(node) {
253 var savedScript = thisScript; 240 if (node.getElementsByTagName) {
254 241 var savedScript = thisScript;
255 var scripts = node.getElementsByTagName("script"), scriptsCopy = []; 242
256 var len = scripts.length; 243 var scripts = node.getElementsByTagName("script"), scriptsCopy = [];
257 for (var i = 0; i < len; ++i) 244 var len = scripts.length;
258 scriptsCopy[i] = scripts[i]; 245 for (var i = 0; i < len; ++i)
259 for (var i = 0; i < len; ++i) { 246 scriptsCopy[i] = scripts[i];
260 thisScript = scriptsCopy[i]; 247 for (var i = 0; i < len; ++i) {
261 try { 248 thisScript = scriptsCopy[i];
262 eval(thisScript.textContent); 249 try {
263 } catch (v) { 250 eval(thisScript.textContent);
264 doExn(v); 251 } catch (v) {
252 doExn(v);
253 }
254 if (thisScript.parentNode)
255 thisScripts.parentNode.removeChild(thisScript);
265 } 256 }
266 } 257
267 258 thisScript = savedScript;
268 thisScript = savedScript; 259 }
269 } 260 }
270 261
271 262
272 // Dynamic tree entry points 263 // Dynamic tree entry points
273 264
274 function dyn(s) { 265 function dyn(s) {
275 var x = parent(); 266 var x = document.createElement("div");
267 x.style.display = "inline";
276 x.dead = false; 268 x.dead = false;
277 x.signal = s; 269 x.signal = s;
278 x.sources = null; 270 x.sources = null;
279 x.closures = null; 271 x.closures = null;
272
273 var first = null;
274
280 x.recreate = function(v) { 275 x.recreate = function(v) {
281 for (var ls = x.closures; ls; ls = ls.next) 276 for (var ls = x.closures; ls; ls = ls.next)
282 freeClosure(ls.data); 277 freeClosure(ls.data);
283 278
284 var doKind = function(kind) { 279 for (var node = first; node && node != x; ) {
285 var arr = x.getElementsByTagName(kind); 280 if (node.getElementsByTagName) {
286 for (var i = 0; i < arr.length; ++i) { 281 var arr = node.getElementsByTagName("div");
287 var span = arr[i]; 282 for (var i = 0; i < arr.length; ++i) {
288 span.dead = true; 283 var span = arr[i];
289 for (var ls = span.sources; ls; ls = ls.next) 284 span.dead = true;
290 ls.data.dyns = remove(span, ls.data.dyns); 285 for (var ls = span.sources; ls; ls = ls.next)
291 for (var ls = span.closures; ls; ls = ls.next) 286 ls.data.dyns = remove(span, ls.data.dyns);
292 freeClosure(ls.data); 287 for (var ls = span.closures; ls; ls = ls.next)
288 freeClosure(ls.data);
289 }
293 } 290 }
294 }; 291
295 292 var old = node;
296 doKind("span"); 293 node = node.nextSibling;
297 doKind("tbody"); 294 old.parentNode.removeChild(old);
295 }
298 296
299 var cls = {v : null}; 297 var cls = {v : null};
300 x.innerHTML = flatten(cls, v); 298 var dummy = document.createElement("table");
299 dummy.innerHTML = flatten(cls, v);
301 x.closures = cls.v; 300 x.closures = cls.v;
302 runScripts(x); 301 runScripts(dummy);
302
303 for (first = dummy.firstChild; first && first.tagName == "TBODY"; first = first.firstChild);
304 for (var node = first; node; ) {
305 var old = node;
306 node = node.nextSibling;
307 x.parentNode.insertBefore(old, x);
308 }
303 }; 309 };
310
311 addNode(x);
304 populate(x); 312 populate(x);
305 } 313 }
306 314
307 function input(t, s, recreate) { 315 function input(t, s, recreate) {
308 var x = document.createElement(t); 316 var x = document.createElement(t);
309 x.dead = false; 317 x.dead = false;
310 x.signal = ss(s); 318 x.signal = ss(s);
311 x.sources = null; 319 x.sources = null;
312 x.recreate = recreate(x); 320 x.recreate = recreate(x);
321 addNode(x);
313 populate(x); 322 populate(x);
314 addNode(x);
315 323
316 return x; 324 return x;
317 } 325 }
318 326
319 function inp(s) { 327 function inp(s) {
329 x.innerHTML = content; 337 x.innerHTML = content;
330 x.value = s.data; 338 x.value = s.data;
331 if (x.value != s.data) 339 if (x.value != s.data)
332 sv(s, x.value); 340 sv(s, x.value);
333 x.onchange = function() { sv(s, x.value) }; 341 x.onchange = function() { sv(s, x.value) };
334
335 342
336 return x; 343 return x;
337 } 344 }
338 345
339 function chk(s) { 346 function chk(s) {