summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGravatar Adam Chlipala <adamc@hcoop.net>2009-01-17 09:47:30 -0500
committerGravatar Adam Chlipala <adamc@hcoop.net>2009-01-17 09:47:30 -0500
commit65bf7ccfc6245257f810a7c95c48b9a37fed34e7 (patch)
tree730db370cf9cd008c5cef8a958cc816c8926f231 /lib
parent45ac3423d55ad88509374176ca15eca34afd0f1e (diff)
Add dynamic content under proper parents
Diffstat (limited to 'lib')
-rw-r--r--lib/js/urweb.js47
1 files changed, 24 insertions, 23 deletions
diff --git a/lib/js/urweb.js b/lib/js/urweb.js
index 689792f7..7bb6849e 100644
--- a/lib/js/urweb.js
+++ b/lib/js/urweb.js
@@ -37,53 +37,54 @@ function sb(x,y) {
return s;
}
-function lastParent(pos) {
+function lastParent() {
+ var pos = document;
+
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");
-}
+var thisScript = null;
-function curParent() {
- return lastParent(parents ? parents.v : document);
+function addNode(node) {
+ if (thisScript) {
+ thisScript.parentNode.appendChild(node);
+ thisScript.parentNode.removeChild(thisScript);
+ } else
+ lastParent().appendChild(node);
}
-function populate(node, html) {
- node.innerHTML = html;
+function runScripts(node) {
+ var savedScript = thisScript;
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();
+ thisScript = scripts[i];
+ eval(thisScript.textContent);
}
+
+ thisScript = savedScript;
+}
+
+function populate(node, html) {
+ node.innerHTML = html;
+ runScripts(node);
}
function dyn(s) {
var x = document.createElement("span");
- x.innerHTML = s.v;
- curParent().appendChild(x);
+ populate(x, s.v);
+ addNode(x);
s.h = cons(function() { populate(x, s.v) }, s.h);
}
function inp(t, s) {
var x = document.createElement(t);
x.value = s.v;
- curParent().appendChild(x);
+ addNode(x);
s.h = cons(function() { x.value = s.v }, s.h);
x.onkeyup = function() { sv(s, x.value) };
}