From 4906733e1c12fd167a4236c63201a8f4e6daad63 Mon Sep 17 00:00:00 2001 From: Adam Chlipala Date: Tue, 3 Mar 2015 15:55:00 -0500 Subject: Support 'dynClass' and 'dynStyle' for --- lib/js/urweb.js | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- src/monoize.sml | 44 ++++++++++++++++++++++++++++++++++++-------- tests/dynClassB.ur | 17 +++++++++++++++++ tests/dynClassB.urp | 5 +++++ tests/style.css | 7 +++++++ 5 files changed, 116 insertions(+), 9 deletions(-) create mode 100644 tests/dynClassB.ur create mode 100644 tests/dynClassB.urp create mode 100644 tests/style.css diff --git a/lib/js/urweb.js b/lib/js/urweb.js index 3bf21dd2..bd5109c3 100644 --- a/lib/js/urweb.js +++ b/lib/js/urweb.js @@ -1200,7 +1200,7 @@ function dynClass(pnode, html, s_class, s_style) { x.dead = false; x.signal = s_class; x.sources = null; - x.closures = htmlCls; + x.closures = null; x.recreate = function(v) { for (var ls = x.closures; ls != htmlCls; ls = ls.next) @@ -1237,6 +1237,56 @@ function dynClass(pnode, html, s_class, s_style) { } } +function bodyDynClass(s_class, s_style) { + if (suspendScripts) + return; + + var htmlCls = null; + + if (s_class) { + var x = document.createElement("script"); + x.dead = false; + x.signal = s_class; + x.sources = null; + x.closures = htmlCls; + + x.recreate = function(v) { + for (var ls = x.closures; ls != htmlCls; ls = ls.next) + freeClosure(ls.data); + + var cls = {v : null}; + document.body.className = flatten(cls, v); + console.log("className to + " + document.body.className); + x.closures = concat(cls.v, htmlCls); + } + + document.body.appendChild(x); + populate(x); + } + + if (s_style) { + var htmlCls2 = s_class ? null : htmlCls; + var y = document.createElement("script"); + y.dead = false; + y.signal = s_style; + y.sources = null; + y.closures = htmlCls2; + + y.recreate = function(v) { + for (var ls = y.closures; ls != htmlCls2; ls = ls.next) + freeClosure(ls.data); + + var cls = {v : null}; + document.body.style.cssText = flatten(cls, v); + console.log("style to + " + document.body.style.cssText); + y.closures = concat(cls.v, htmlCls2); + } + + document.body.appendChild(y); + populate(y); + } +} + function addOnChange(x, f) { var old = x.onchange; if (old == null) diff --git a/src/monoize.sml b/src/monoize.sml index 5727d997..59c5d2ea 100644 --- a/src/monoize.sml +++ b/src/monoize.sml @@ -3267,6 +3267,11 @@ fun monoExp (env, st, fm) (all as (e, loc)) = else (NONE, NONE, attrs) + val (class, fm) = monoExp (env, st, fm) class + val (dynClass, fm) = monoExp (env, st, fm) dynClass + val (style, fm) = monoExp (env, st, fm) style + val (dynStyle, fm) = monoExp (env, st, fm) dynStyle + (* Special case for