diff lib/js/urweb.js @ 603:b1064de2b1f9

dlist example working
author Adam Chlipala <adamc@hcoop.net>
date Fri, 16 Jan 2009 15:49:10 -0500
parents 1d34d916c206
children 20c083327364
line wrap: on
line diff
--- a/lib/js/urweb.js	Tue Jan 13 15:23:48 2009 -0500
+++ b/lib/js/urweb.js	Fri Jan 16 15:49:10 2009 -0500
@@ -37,26 +37,53 @@
   return s;
 }
 
-function myParent() {
-  var pos = document;
-
+function lastParent(pos) {
   while (pos.lastChild && pos.lastChild.nodeType == 1)
     pos = pos.lastChild;
 
   return pos.parentNode;
 }
 
+var parents = null;
+
+function pushParent(node) {
+  parents = cons(node, parents);
+}
+
+function popParent() {
+  if (parents)
+    parents = parents.n;
+  else
+    alert("popParent: stack underflow");
+}
+
+function curParent() {
+  return lastParent(parents ? parents.v : document);
+}
+
+function populate(node, html) {
+  node.innerHTML = html;
+
+  var scripts = node.getElementsByTagName("script");
+  var len = scripts.length;
+  for (var i = 0; i < len; ++i) {
+    pushParent(scripts[i].parentNode);
+    eval(scripts[i].textContent);
+    popParent();
+  }
+}
+
 function dyn(s) {
   var x = document.createElement("span");
   x.innerHTML = s.v;
-  myParent().appendChild(x);
-  s.h = cons(function() { x.innerHTML = s.v }, s.h);
+  curParent().appendChild(x);
+  s.h = cons(function() { populate(x, s.v) }, s.h);
 }
 
 function inp(t, s) {
   var x = document.createElement(t);
   x.value = s.v;
-  myParent().appendChild(x);
+  curParent().appendChild(x);
   s.h = cons(function() { x.value = s.v }, s.h);
   x.onkeyup = function() { sv(s, x.value) };
 }
@@ -70,3 +97,15 @@
 
 function pf() { alert("Pattern match failure") }
 
+var closures = [];
+
+function ca(f) {
+  var n = closures.length;
+  closures[n] = f;
+  return n;
+}
+
+function cr(n) {
+  return closures[n]();
+}
+