# HG changeset patch # User Adam Chlipala # Date 1315764889 14400 # Node ID df6a7a22760aa26e82fcb676b9e588d0216b4428 # Parent 6fe6bda2b92858c766da56e1ef1d865c37857299 New Basis functions: preventDefault and stopPropagation (code contributed by Vladimir Shabanov) diff -r 6fe6bda2b928 -r df6a7a22760a doc/manual.tex --- a/doc/manual.tex Sun Sep 11 11:37:26 2011 -0400 +++ b/doc/manual.tex Sun Sep 11 14:14:49 2011 -0400 @@ -1983,6 +1983,13 @@ \mt{val} \; \mt{onMouseup} : \mt{transaction} \; \mt{unit} \to \mt{transaction} \; \mt{unit} \end{array}$$ +Versions of standard JavaScript functions are provided that event handlers may call to mask default handling or prevent bubbling of events up to parent DOM nodes, respectively. + +$$\begin{array}{l} + \mt{val} \; \mt{preventDefault} : \mt{transaction} \; \mt{unit} \\ + \mt{val} \; \mt{stopPropagation} : \mt{transaction} \; \mt{unit} +\end{array}$$ + \subsubsection{Node IDs} There is an abstract type of node IDs that may be assigned to \cd{id} attributes of most HTML tags. diff -r 6fe6bda2b928 -r df6a7a22760a lib/js/urweb.js --- a/lib/js/urweb.js Sun Sep 11 11:37:26 2011 -0400 +++ b/lib/js/urweb.js Sun Sep 11 14:14:49 2011 -0400 @@ -215,9 +215,9 @@ function uw_handler(name, f) { var old = document[name]; if (old == undefined) - document[name] = function() { execF(f); return false; }; + document[name] = function(event) { uw_event = event; execF(f); }; else - document[name] = function() { old(); execF(f); return false; }; + document[name] = function(event) { uw_event = event; old(); execF(f); }; } function uw_onClick(f) { @@ -239,9 +239,9 @@ 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; }; + document[name] = function(event) { uw_event = event; execF(execF(f, kc())); }; else - document[name] = function(event) { uw_event = event; old(); execF(execF(f, kc())); return false; }; + document[name] = function(event) { uw_event = event; old(); execF(execF(f, kc())); }; } function uw_onKeydown(f) { @@ -256,6 +256,19 @@ uw_keyHandler("onkeyup", f); } +// Cancelling of further event processing + +function uw_preventDefault() { + var e = window.event ? window.event : uw_event; + e.returnValue = false; + if (e.preventDefault) e.preventDefault(); +} + +function uw_stopPropagation() { + var e = window.event ? window.event : uw_event; + e.cancelBubble = true; + if (e.stopPropagation) e.stopPropagation(); +} // Embedding closures in XML strings diff -r 6fe6bda2b928 -r df6a7a22760a lib/ur/basis.urs --- a/lib/ur/basis.urs Sun Sep 11 11:37:26 2011 -0400 +++ b/lib/ur/basis.urs Sun Sep 11 14:14:49 2011 -0400 @@ -870,6 +870,11 @@ val onMousedown : transaction unit -> transaction unit val onMouseup : transaction unit -> transaction unit +(* Prevents default handling of current event *) +val preventDefault : transaction unit +(* Stops propagation of current event *) +val stopPropagation : transaction unit + val show_xml : ctx ::: {Unit} -> use ::: {Type} -> bind ::: {Type} -> show (xml ctx use bind) diff -r 6fe6bda2b928 -r df6a7a22760a src/settings.sml --- a/src/settings.sml Sun Sep 11 11:37:26 2011 -0400 +++ b/src/settings.sml Sun Sep 11 14:14:49 2011 -0400 @@ -156,6 +156,8 @@ "onKeyup", "onMousedown", "onMouseup", + "preventDefault", + "stopPropagation", "fresh"] val benign = ref benignBase @@ -182,7 +184,9 @@ "onKeypress", "onKeyup", "onMousedown", - "onMouseup"] + "onMouseup", + "preventDefault", + "stopPropagation"] val client = ref clientBase fun setClientOnly ls = client := S.addList (clientBase, ls) fun isClientOnly x = S.member (!client, x) @@ -280,6 +284,8 @@ ("onKeyup", "uw_onKeyup"), ("onMousedown", "uw_onMousedown"), ("onMouseup", "uw_onMouseup"), + ("preventDefault", "uw_preventDefault"), + ("stopPropagation", "uw_stopPropagation"), ("fresh", "fresh")] val jsFuncs = ref jsFuncsBase