diff 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
line wrap: on
line diff
--- a/lib/js/urweb.js	Sun Jul 19 17:45:02 2009 -0400
+++ b/lib/js/urweb.js	Thu Aug 06 11:54:28 2009 -0400
@@ -228,79 +228,87 @@
 }
 
 function addNode(node) {
-  if (thisScript) {
-    thisScript.parentNode.appendChild(node);
-    thisScript.parentNode.removeChild(thisScript);
-  } else {
+  if (thisScript)
+    thisScript.parentNode.replaceChild(node, thisScript);
+  else
     lastParent().appendChild(node);
-  }
 }
 
-function setHTML(html) {
-  var x = document.createElement("span");
-  x.dead = false;
-  x.signal = null;
-  x.sources = null;
-  x.closures = null;
-  x.innerHTML = html;
-  addNode(x);
-  runScripts(x);
-}  
-
 var thisScript = null;
 
 function runScripts(node) {
-  var savedScript = thisScript;
+  if (node.getElementsByTagName) {
+    var savedScript = thisScript;
 
-  var scripts = node.getElementsByTagName("script"), scriptsCopy = [];
-  var len = scripts.length;
-  for (var i = 0; i < len; ++i)
-    scriptsCopy[i] = scripts[i];
-  for (var i = 0; i < len; ++i) {
-    thisScript = scriptsCopy[i];
-    try {
-      eval(thisScript.textContent);
-    } catch (v) {
-      doExn(v);
+    var scripts = node.getElementsByTagName("script"), scriptsCopy = [];
+    var len = scripts.length;
+    for (var i = 0; i < len; ++i)
+      scriptsCopy[i] = scripts[i];
+    for (var i = 0; i < len; ++i) {
+      thisScript = scriptsCopy[i];
+      try {
+        eval(thisScript.textContent);
+      } catch (v) {
+        doExn(v);
+      }
+      if (thisScript.parentNode)
+        thisScripts.parentNode.removeChild(thisScript);
     }
+
+    thisScript = savedScript;
   }
-
-  thisScript = savedScript;
 }
 
 
 // Dynamic tree entry points
 
 function dyn(s) {
-  var x = parent();
+  var x = document.createElement("div");
+  x.style.display = "inline";
   x.dead = false;
   x.signal = s;
   x.sources = null;
   x.closures = null;
+
+  var first = null;
+
   x.recreate = function(v) {
     for (var ls = x.closures; ls; ls = ls.next)
       freeClosure(ls.data);
 
-    var doKind = function(kind) {
-      var arr = x.getElementsByTagName(kind);
-      for (var i = 0; i < arr.length; ++i) {
-        var span = arr[i];
-        span.dead = true;
-        for (var ls = span.sources; ls; ls = ls.next)
-          ls.data.dyns = remove(span, ls.data.dyns);
-        for (var ls = span.closures; ls; ls = ls.next)
-          freeClosure(ls.data);
+    for (var node = first; node && node != x; ) {
+      if (node.getElementsByTagName) {
+        var arr = node.getElementsByTagName("div");
+        for (var i = 0; i < arr.length; ++i) {
+          var span = arr[i];
+          span.dead = true;
+          for (var ls = span.sources; ls; ls = ls.next)
+            ls.data.dyns = remove(span, ls.data.dyns);
+          for (var ls = span.closures; ls; ls = ls.next)
+            freeClosure(ls.data);
+        }
       }
-    };
 
-    doKind("span");
-    doKind("tbody");
+      var old = node;
+      node = node.nextSibling;
+      old.parentNode.removeChild(old);
+    }
 
     var cls = {v : null};
-    x.innerHTML = flatten(cls, v);
+    var dummy = document.createElement("table");
+    dummy.innerHTML = flatten(cls, v);
     x.closures = cls.v;
-    runScripts(x);
+    runScripts(dummy);
+
+    for (first = dummy.firstChild; first && first.tagName == "TBODY"; first = first.firstChild);
+    for (var node = first; node; ) {
+      var old = node;
+      node = node.nextSibling;
+      x.parentNode.insertBefore(old, x);
+    }
   };
+
+  addNode(x);
   populate(x);
 }
 
@@ -310,8 +318,8 @@
   x.signal = ss(s);
   x.sources = null;
   x.recreate = recreate(x);
+  addNode(x);
   populate(x);
-  addNode(x);
 
   return x;
 }
@@ -332,7 +340,6 @@
     sv(s, x.value);
   x.onchange = function() { sv(s, x.value) };
 
-
   return x;
 }