module HTML where
import Prelude
import JQuery
import FFI
fromStringLit :: String -> String
fromStringLit = id
data Attr a b = a := b
class AsChild a
instance AsChild [Char]
instance AsChild (Fay HTML)
asChild :: (AsChild a) => a -> Fay HTML
asChild =
ffi "(function () { if (%1 instanceof Fay$$Cons) { return { 'instance' : 'CDATA', slot1 : true, slot2 : Fay$$fayToJs_string(%1) }; } else { var monad = Fay$$_(%1, true); return monad.value; }})()"
class AsAttr a
instance AsAttr (Attr String String)
asAttr :: (Attr String String) -> Fay (String, String)
asAttr ((:=) a b) = return (a, b)
data HTML
= Element String [(String, String)] [HTML]
| CDATA Bool String
genElement :: (Maybe String, String)
-> [Fay (String, String)]
-> [Fay HTML]
-> Fay HTML
genElement (_, n) genAttrs genChildren =
do attrs <- sequence $ genAttrs
children <- sequence $ genChildren
return (Element n attrs children)
renderHTML :: HTML
-> Fay JQuery
renderHTML (Element n attrs children) =
do elem <- selectElement =<< createElement n
mapM_ (\(n, v) -> setAttr n v elem) attrs
mapM_ (\child ->
do cElem <- renderHTML child
append cElem elem) children
return elem
renderHTML (CDATA True str) =
selectElement =<< createTextNode str
renderHTML (CDATA False str) =
do alert "Unsure how to insert pre-escaped text into the generated HTML."
selectElement =<< createTextNode str
alert :: String -> Fay ()
alert = ffi "window.alert(%1)"
tr :: [Fay (String, String)]
-> [Fay HTML]
-> Fay HTML
tr ats chd = genElement (Nothing, "tr") ats chd
td :: [Fay (String, String)]
-> [Fay HTML]
-> Fay HTML
td = genElement (Nothing, "td")
span_ :: [Fay (String, String)]
-> [Fay HTML]
-> Fay HTML
span_ = genElement (Nothing, "span")
pcdata :: String -> Fay HTML
pcdata = return . CDATA True
createElement :: String
-> Fay Element
createElement = ffi "document.createElement(%1)"
createTextNode :: String
-> Fay Element
createTextNode = ffi "document.createTextNode(%1)"