diff lib/js/urweb.js @ 1555:d5c961c709f9

New client-side, document-level event handlers
author Adam Chlipala <adam@chlipala.net>
date Sat, 03 Sep 2011 11:48:12 -0400
parents 4105f779de7b
children e1f5d9c4cc20
line wrap: on
line diff
--- a/lib/js/urweb.js	Sat Sep 03 08:57:13 2011 -0400
+++ b/lib/js/urweb.js	Sat Sep 03 11:48:12 2011 -0400
@@ -202,6 +202,60 @@
     window.setTimeout(function () { runHandlers("Server", serverHandlers, s); }, 0);
 }
 
+// Key events
+
+var uw_event = null;
+
+function kc() {
+    return window.event ? event.keyCode : (uw_event ? uw_event.which : 0);
+}
+
+// Document events
+
+function uw_handler(name, f) {
+    var old = document[name];
+    if (old == undefined)
+        document[name] = function() { execF(f); return false; };
+    else
+        document[name] = function() { old(); execF(f); return false; };
+}
+
+function uw_onClick(f) {
+    uw_handler("onclick", f);
+}
+
+function uw_onDblclick(f) {
+    uw_handler("ondblclick", f);
+}
+
+function uw_onMousedown(f) {
+    uw_handler("onmousedown", f);
+}
+
+function uw_onMouseup(f) {
+    uw_handler("onmouseup", f);
+}
+
+function uw_keyHandler(name, f) {
+    var old = document[name];
+    if (old == undefined)
+        document[name] = function(event) { uw_event = event; execF(execF(f, kc())); return false; };
+    else
+        document[name] = function(event) { uw_event = event; old(); execF(execF(f, kc())); return false; };
+}
+
+function uw_onKeydown(f) {
+    uw_keyHandler("onkeydown", f);
+}
+
+function uw_onKeypress(f) {
+    uw_keyHandler("onkeypress", f);
+}
+
+function uw_onKeyup(f) {
+    uw_keyHandler("onkeyup", f);
+}
+
 
 // Embedding closures in XML strings
 
@@ -1025,15 +1079,6 @@
 }
 
 
-// Key events
-
-var uw_event = null;
-
-function kc() {
-    return window.event ? event.keyCode : (uw_event ? uw_event.keyCode : 0);
-}
-
-
 // The Ur interpreter
 
 var urfuncs = [];