comparison 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
comparison
equal deleted inserted replaced
602:1d34d916c206 603:b1064de2b1f9
35 reZ(); 35 reZ();
36 36
37 return s; 37 return s;
38 } 38 }
39 39
40 function myParent() { 40 function lastParent(pos) {
41 var pos = document;
42
43 while (pos.lastChild && pos.lastChild.nodeType == 1) 41 while (pos.lastChild && pos.lastChild.nodeType == 1)
44 pos = pos.lastChild; 42 pos = pos.lastChild;
45 43
46 return pos.parentNode; 44 return pos.parentNode;
47 } 45 }
48 46
47 var parents = null;
48
49 function pushParent(node) {
50 parents = cons(node, parents);
51 }
52
53 function popParent() {
54 if (parents)
55 parents = parents.n;
56 else
57 alert("popParent: stack underflow");
58 }
59
60 function curParent() {
61 return lastParent(parents ? parents.v : document);
62 }
63
64 function populate(node, html) {
65 node.innerHTML = html;
66
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
49 function dyn(s) { 76 function dyn(s) {
50 var x = document.createElement("span"); 77 var x = document.createElement("span");
51 x.innerHTML = s.v; 78 x.innerHTML = s.v;
52 myParent().appendChild(x); 79 curParent().appendChild(x);
53 s.h = cons(function() { x.innerHTML = s.v }, s.h); 80 s.h = cons(function() { populate(x, s.v) }, s.h);
54 } 81 }
55 82
56 function inp(t, s) { 83 function inp(t, s) {
57 var x = document.createElement(t); 84 var x = document.createElement(t);
58 x.value = s.v; 85 x.value = s.v;
59 myParent().appendChild(x); 86 curParent().appendChild(x);
60 s.h = cons(function() { x.value = s.v }, s.h); 87 s.h = cons(function() { x.value = s.v }, s.h);
61 x.onkeyup = function() { sv(s, x.value) }; 88 x.onkeyup = function() { sv(s, x.value) };
62 } 89 }
63 90
64 function eh(x) { 91 function eh(x) {
68 function ts(x) { return x.toString() } 95 function ts(x) { return x.toString() }
69 function bs(b) { return (b ? "True" : "False") } 96 function bs(b) { return (b ? "True" : "False") }
70 97
71 function pf() { alert("Pattern match failure") } 98 function pf() { alert("Pattern match failure") }
72 99
100 var closures = [];
101
102 function ca(f) {
103 var n = closures.length;
104 closures[n] = f;
105 return n;
106 }
107
108 function cr(n) {
109 return closures[n]();
110 }
111