use #define to avoid repeating #if, etc

This commit is contained in:
Greg Weber 2011-02-08 15:01:01 -08:00
parent 98aa67a68d
commit 22f6f175f0
2 changed files with 23 additions and 64 deletions

View File

@ -64,6 +64,11 @@ import Control.Arrow ((&&&))
import Data.List (group, sort) import Data.List (group, sort)
import Data.Monoid (mempty) import Data.Monoid (mempty)
#if __GLASGOW_HASKELL__ >= 700
#define HAMLET hamlet
#else
#define HAMLET $hamlet
#endif
-- | Display only the actual input widget code, without any decoration. -- | Display only the actual input widget code, without any decoration.
fieldsToPlain :: FormField sub y a -> Form sub y a fieldsToPlain :: FormField sub y a -> Form sub y a
fieldsToPlain = mapFormXml $ mapM_ fiInput fieldsToPlain = mapFormXml $ mapM_ fiInput
@ -73,12 +78,7 @@ fieldsToPlain = mapFormXml $ mapM_ fiInput
fieldsToTable :: FormField sub y a -> Form sub y a fieldsToTable :: FormField sub y a -> Form sub y a
fieldsToTable = mapFormXml $ mapM_ go fieldsToTable = mapFormXml $ mapM_ go
where where
go fi = go fi = [HAMLET|
#if __GLASGOW_HASKELL__ >= 700
[hamlet|
#else
[$hamlet|
#endif
<tr .#{clazz fi}> <tr .#{clazz fi}>
<td> <td>
<label for="#{fiIdent fi}">#{fiLabel fi} <label for="#{fiIdent fi}">#{fiLabel fi}
@ -94,12 +94,7 @@ fieldsToTable = mapFormXml $ mapM_ go
fieldsToDivs :: FormField sub y a -> Form sub y a fieldsToDivs :: FormField sub y a -> Form sub y a
fieldsToDivs = mapFormXml $ mapM_ go fieldsToDivs = mapFormXml $ mapM_ go
where where
go fi = go fi = [HAMLET|
#if __GLASGOW_HASKELL__ >= 700
[hamlet|
#else
[$hamlet|
#endif
<div .#{clazz fi}> <div .#{clazz fi}>
<label for="#{fiIdent fi}">#{fiLabel fi} <label for="#{fiIdent fi}">#{fiLabel fi}
<div .tooltip>#{fiTooltip fi} <div .tooltip>#{fiTooltip fi}
@ -134,12 +129,7 @@ runFormPost f = do
_ -> res _ -> res
return (res', xml, enctype, maybe mempty hidden nonce) return (res', xml, enctype, maybe mempty hidden nonce)
where where
hidden nonce = hidden nonce = [HAMLET|
#if __GLASGOW_HASKELL__ >= 700
[hamlet|
#else
[$hamlet|
#endif
<input type="hidden" name="#{nonceName}" value="#{nonce}"> <input type="hidden" name="#{nonceName}" value="#{nonce}">
|] |]
@ -172,12 +162,7 @@ runFormTable :: Route m -> String -> FormField s m a
-> GHandler s m (FormResult a, GWidget s m ()) -> GHandler s m (FormResult a, GWidget s m ())
runFormTable dest inputLabel form = do runFormTable dest inputLabel form = do
(res, widget, enctype, nonce) <- runFormPost $ fieldsToTable form (res, widget, enctype, nonce) <- runFormPost $ fieldsToTable form
let widget' = return (res, [HAMLET|
#if __GLASGOW_HASKELL__ >= 700
[hamlet|
#else
[$hamlet|
#endif
<form method="post" action="@{dest}" enctype="#{enctype}"> <form method="post" action="@{dest}" enctype="#{enctype}">
<table> <table>
\^{widget} \^{widget}
@ -185,27 +170,20 @@ runFormTable dest inputLabel form = do
<td colspan="2"> <td colspan="2">
\#{nonce} \#{nonce}
<input type="submit" value="#{inputLabel}"> <input type="submit" value="#{inputLabel}">
|] |])
return (res, widget')
-- | Same as 'runFormPostTable', but uses 'fieldsToDivs' for styling. -- | Same as 'runFormPostTable', but uses 'fieldsToDivs' for styling.
runFormDivs :: Route m -> String -> FormField s m a runFormDivs :: Route m -> String -> FormField s m a
-> GHandler s m (FormResult a, GWidget s m ()) -> GHandler s m (FormResult a, GWidget s m ())
runFormDivs dest inputLabel form = do runFormDivs dest inputLabel form = do
(res, widget, enctype, nonce) <- runFormPost $ fieldsToDivs form (res, widget, enctype, nonce) <- runFormPost $ fieldsToDivs form
let widget' = return (res, [HAMLET|
#if __GLASGOW_HASKELL__ >= 700
[hamlet|
#else
[$hamlet|
#endif
<form method="post" action="@{dest}" enctype="#{enctype}"> <form method="post" action="@{dest}" enctype="#{enctype}">
\^{widget} \^{widget}
<div> <div>
\#{nonce} \#{nonce}
<input type="submit" value="#{inputLabel}"> <input type="submit" value="#{inputLabel}">
|] |])
return (res, widget')
-- | Run a form against GET parameters, disregarding the resulting HTML and -- | Run a form against GET parameters, disregarding the resulting HTML and
-- returning an error response on invalid input. -- returning an error response on invalid input.
@ -223,12 +201,7 @@ generateForm :: GForm s m xml a -> GHandler s m (xml, Enctype, Html)
generateForm f = do generateForm f = do
(_, b, c) <- runFormGeneric [] [] f (_, b, c) <- runFormGeneric [] [] f
nonce <- fmap reqNonce getRequest nonce <- fmap reqNonce getRequest
return (b, c, return (b, c, [HAMLET|\
#if __GLASGOW_HASKELL__ >= 700
[hamlet|
#else
[$hamlet|
#endif
$maybe n <- nonce $maybe n <- nonce
<input type="hidden" name="#{nonceName}" value="#{n}"> <input type="hidden" name="#{nonceName}" value="#{n}">
|]) |])

View File

@ -63,6 +63,12 @@ import Data.Monoid
import Control.Monad (join) import Control.Monad (join)
import Data.Maybe (fromMaybe, isNothing) import Data.Maybe (fromMaybe, isNothing)
#if __GLASGOW_HASKELL__ >= 700
#define HAMLET hamlet
#else
#define HAMLET $hamlet
#endif
stringField :: (IsForm f, FormType f ~ String) stringField :: (IsForm f, FormType f ~ String)
=> FormFieldSettings -> Maybe String -> f => FormFieldSettings -> Maybe String -> f
stringField = requiredFieldHelper stringFieldProfile stringField = requiredFieldHelper stringFieldProfile
@ -141,12 +147,7 @@ boolField ffs orig = toForm $ do
{ fiLabel = string label { fiLabel = string label
, fiTooltip = tooltip , fiTooltip = tooltip
, fiIdent = theId , fiIdent = theId
, fiInput = , fiInput = [HAMLET|
#if __GLASGOW_HASKELL__ >= 700
[hamlet|
#else
[$hamlet|
#endif
<input id="#{theId}" type="checkbox" name="#{name}" :val:checked=""> <input id="#{theId}" type="checkbox" name="#{name}" :val:checked="">
|] |]
, fiErrors = case res of , fiErrors = case res of
@ -281,12 +282,7 @@ boolInput n = GForm $ do
Just "" -> FormSuccess False Just "" -> FormSuccess False
Just "false" -> FormSuccess False Just "false" -> FormSuccess False
Just _ -> FormSuccess True Just _ -> FormSuccess True
let xml = let xml = [HAMLET|
#if __GLASGOW_HASKELL__ >= 700
[hamlet|
#else
[$hamlet|
#endif
<input id="#{n}" type="checkbox" name="#{n}"> <input id="#{n}" type="checkbox" name="#{n}">
|] |]
return (res, [xml], UrlEncoded) return (res, [xml], UrlEncoded)
@ -401,12 +397,7 @@ maybeFileField ffs = toForm $ do
return (res, fi, Multipart) return (res, fi, Multipart)
fileWidget :: String -> String -> Bool -> GWidget s m () fileWidget :: String -> String -> Bool -> GWidget s m ()
fileWidget theId name isReq = fileWidget theId name isReq = [HAMLET|
#if __GLASGOW_HASKELL__ >= 700
[hamlet|
#else
[$hamlet|
#endif
<input id="#{theId}" type="file" name="#{name}" :isReq:required=""> <input id="#{theId}" type="file" name="#{name}" :isReq:required="">
|] |]
@ -436,12 +427,7 @@ radioField pairs ffs initial = toForm $ do
case res of case res of
FormSuccess y -> x == y FormSuccess y -> x == y
_ -> Just x == initial _ -> Just x == initial
let input = let input = [HAMLET|
#if __GLASGOW_HASKELL__ >= 700
[hamlet|
#else
[$hamlet|
#endif
<div id="#{theId}"> <div id="#{theId}">
$forall pair <- pairs' $forall pair <- pairs'
<div> <div>