summaryrefslogtreecommitdiff
path: root/lib/js
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
commit6ee23b4ea530ff85a780cc8401cdcd078bed4ada (patch)
tree730db370cf9cd008c5cef8a958cc816c8926f231 /lib/js
parented56c462bf4131b7e179c72bfafb4f6967bc27dc (diff)
Add dynamic content under proper parents
Diffstat (limited to 'lib/js')
-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) };
}