comparison lib/js/urweb.js @ 604:20c083327364

Add dynamic content under proper parents
author Adam Chlipala <adamc@hcoop.net>
date Sat, 17 Jan 2009 09:47:30 -0500
parents b1064de2b1f9
children 5145181b02fa
comparison
equal deleted inserted replaced
603:b1064de2b1f9 604:20c083327364
35 reZ(); 35 reZ();
36 36
37 return s; 37 return s;
38 } 38 }
39 39
40 function lastParent(pos) { 40 function lastParent() {
41 var pos = document;
42
41 while (pos.lastChild && pos.lastChild.nodeType == 1) 43 while (pos.lastChild && pos.lastChild.nodeType == 1)
42 pos = pos.lastChild; 44 pos = pos.lastChild;
43 45
44 return pos.parentNode; 46 return pos.parentNode;
45 } 47 }
46 48
47 var parents = null; 49 var thisScript = null;
48 50
49 function pushParent(node) { 51 function addNode(node) {
50 parents = cons(node, parents); 52 if (thisScript) {
53 thisScript.parentNode.appendChild(node);
54 thisScript.parentNode.removeChild(thisScript);
55 } else
56 lastParent().appendChild(node);
51 } 57 }
52 58
53 function popParent() { 59 function runScripts(node) {
54 if (parents) 60 var savedScript = thisScript;
55 parents = parents.n;
56 else
57 alert("popParent: stack underflow");
58 }
59 61
60 function curParent() { 62 var scripts = node.getElementsByTagName("script");
61 return lastParent(parents ? parents.v : document); 63 var len = scripts.length;
64 for (var i = 0; i < len; ++i) {
65 thisScript = scripts[i];
66 eval(thisScript.textContent);
67 }
68
69 thisScript = savedScript;
62 } 70 }
63 71
64 function populate(node, html) { 72 function populate(node, html) {
65 node.innerHTML = html; 73 node.innerHTML = html;
66 74 runScripts(node);
67 var scripts = node.getElementsByTagName("script");
68 var len = scripts.length;
69 for (var i = 0; i < len; ++i) {
70 pushParent(scripts[i].parentNode);
71 eval(scripts[i].textContent);
72 popParent();
73 }
74 } 75 }
75 76
76 function dyn(s) { 77 function dyn(s) {
77 var x = document.createElement("span"); 78 var x = document.createElement("span");
78 x.innerHTML = s.v; 79 populate(x, s.v);
79 curParent().appendChild(x); 80 addNode(x);
80 s.h = cons(function() { populate(x, s.v) }, s.h); 81 s.h = cons(function() { populate(x, s.v) }, s.h);
81 } 82 }
82 83
83 function inp(t, s) { 84 function inp(t, s) {
84 var x = document.createElement(t); 85 var x = document.createElement(t);
85 x.value = s.v; 86 x.value = s.v;
86 curParent().appendChild(x); 87 addNode(x);
87 s.h = cons(function() { x.value = s.v }, s.h); 88 s.h = cons(function() { x.value = s.v }, s.h);
88 x.onkeyup = function() { sv(s, x.value) }; 89 x.onkeyup = function() { sv(s, x.value) };
89 } 90 }
90 91
91 function eh(x) { 92 function eh(x) {