Skip to content

Commit

Permalink
Renamed splices
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsd committed Aug 22, 2012
1 parent 2289f45 commit 01916b5
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 83 deletions.
10 changes: 5 additions & 5 deletions README.md
Expand Up @@ -23,11 +23,11 @@ First, some imports
```haskell ```haskell
import Control.Applicative import Control.Applicative
import Data.Monoid import Data.Monoid
import Data.Text (Text) import Data.Text (Text)
import qualified Data.Text as T import qualified Data.Text as T
import Language.Javascript.JMacro import Language.Javascript.JMacro
import Snap.Snaplet.LiftAjax.Splice import qualified Snap.Snaplet.LiftAjax.Js as Js
import qualified Snap.Snaplet.LiftAjax.Js as Js import qualified Snap.Snaplet.LiftAjax.Splice as Ajax
import Text.Digestive import Text.Digestive
import Text.Digestive.Heist import Text.Digestive.Heist
import Text.Templating.Heist import Text.Templating.Heist
Expand All @@ -46,7 +46,7 @@ To this form, associate a function `process` which takes the result
(or an error) and returns javascript to be executed client-side. (or an error) and returns javascript to be executed client-side.
```haskell ```haskell
addIntsSplice :: Splice AppHandler addIntsSplice :: Splice AppHandler
addIntsSplice = ajaxFormWithSplices digestiveSplices addInts process addIntsSplice = Ajax.formWithSplices digestiveSplices addInts process
where where
process :: Either (View Text) Int -> AppHandler JStat process :: Either (View Text) Int -> AppHandler JStat
process (Right z) = return $ Js.alert $ "the sum is " <> T.pack (show z) process (Right z) = return $ Js.alert $ "the sum is " <> T.pack (show z)
Expand Down
155 changes: 77 additions & 78 deletions src/Snap/Snaplet/LiftAjax/Splice.hs
Expand Up @@ -2,17 +2,17 @@
{-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE QuasiQuotes #-}


module Snap.Snaplet.LiftAjax.Splice module Snap.Snaplet.LiftAjax.Splice
( ajaxFormWithHandler ( formWithHandler
, ajaxForm , form
, ajaxFormWithSplices , formWithSplices
, ajaxElemWithHandler , elemWithHandler
, ajaxElemWithParser , elemWithParser
, ajaxButton , button
, ajaxButton_ , readButton
, ajaxJsonButton , jsonButton
, ajaxAnchor , anchor
, ajaxAnchor_ , readAnchor
, ajaxJsonAnchor , jsonAnchor
) where ) where


------------------------------------------------------------------------------ ------------------------------------------------------------------------------
Expand Down Expand Up @@ -72,8 +72,8 @@ addAjaxCallback = liftAjax . addCallback . (>>= Js.write)
-- Forms -- Forms
------------------------------------------------------------------------------ ------------------------------------------------------------------------------


ajaxFormWithHandler :: HasAjax b => Handler b b JStat -> Splice (Handler b b) formWithHandler :: HasAjax b => Handler b b JStat -> Splice (Handler b b)
ajaxFormWithHandler h = do formWithHandler h = do
X.Element _ attrs cs <- getParamNode X.Element _ attrs cs <- getParamNode
formId <- liftAjax newRandomId formId <- liftAjax newRandomId
handlerId <- addAjaxCallback h handlerId <- addAjaxCallback h
Expand All @@ -82,74 +82,73 @@ ajaxFormWithHandler h = do
, ("name", hidAsText handlerId) , ("name", hidAsText handlerId)
, ("id", hidAsText handlerId) , ("id", hidAsText handlerId)
] [] ] []
form = X.Element "form" (addAttrs [ ("action", "javascript://") f = X.Element "form" (addAttrs [ ("action", "javascript://")
, ("onsubmit", Js.showAsText sendForm) , ("onsubmit", Js.showAsText sendForm)
, ("id", formId) , ("id", formId)
] attrs) (children ++ [hidden]) ] attrs) (children ++ [hidden])
sendForm = ajaxCall [jmacroE|$(`("#"<>formId)`).serialize()|] sendForm = ajaxCall [jmacroE|$(`("#"<>formId)`).serialize()|]
return [form] return [f]


ajaxForm :: HasAjax b => form :: HasAjax b =>
Text Text
-> Form v (Handler b b) a -> Form v (Handler b b) a
-> FormHandler b v a -> FormHandler b v a
-> Splice (Handler b b) -> Splice (Handler b b)
ajaxForm name form process = form name f process = formWithHandler $ do
ajaxFormWithHandler $ do (view, result) <- runForm name f
(view, result) <- runForm name form process (maybe (Left view) Right result)
process (maybe (Left view) Right result)

formWithSplices :: HasAjax b =>
ajaxFormWithSplices :: HasAjax b => (View v -> [(Text, Splice (Handler b b))])
(View v -> [(Text, Splice (Handler b b))]) -> Form v (Handler b b) a
-> Form v (Handler b b) a -> FormHandler b v a
-> FormHandler b v a -> Splice (Handler b b)
-> Splice (Handler b b) formWithSplices splices f process = do
ajaxFormWithSplices splices form process = do
name <- liftAjax newRandomId name <- liftAjax newRandomId
view <- lift $ getForm name form view <- lift $ getForm name f
localTS (bindSplices $ splices view) $ ajaxForm name form process localTS (bindSplices $ splices view) $ form name f process


------------------------------------------------------------------------------ ------------------------------------------------------------------------------
-- Elements -- Elements
------------------------------------------------------------------------------ ------------------------------------------------------------------------------


ajaxElemWithHandler :: HasAjax b => ajaxElem :: HasAjax b =>
[(Text, JExpr)] [(Text, JExpr)]
-> Handler b b JStat -> Handler b b JStat
-> HeistT (Handler b b) (Attrs, Children) -> HeistT (Handler b b) (Attrs, Children)
ajaxElemWithHandler jsParams h = do ajaxElem jsParams h = do
X.Element _ as cs <- getParamNode X.Element _ as cs <- getParamNode
handlerId <- addAjaxCallback h handlerId <- addAjaxCallback h
children <- runNodeList cs children <- runNodeList cs
let call = ajaxCallWithParams $ (hidAsText handlerId, Js.null) : jsParams let call = ajaxCallWithParams $ (hidAsText handlerId, Js.null) : jsParams
return (addAttrs [("onclick", Js.showAsText call)] as, children) return (addAttrs [("onclick", Js.showAsText call)] as, children)


ajaxElemWithParser :: HasAjax b => elemWithHandler :: HasAjax b => Handler b b JStat -> HeistT (Handler b b) (Attrs, Children)
(B.ByteString -> Maybe a) elemWithHandler = ajaxElem []
-> JExpr
-> ButtonHandler b a elemWithParser :: HasAjax b =>
-> HeistT (Handler b b) (Attrs, Children) (B.ByteString -> Maybe a)
ajaxElemWithParser parse jsExpr process = do -> JExpr
-> ButtonHandler b a
-> HeistT (Handler b b) (Attrs, Children)
elemWithParser parse jsExpr process = do
exprName <- liftAjax newRandomId exprName <- liftAjax newRandomId
ajaxElemWithHandler [(exprName, jsExpr)] $ do ajaxElem [(exprName, jsExpr)] $ do
expr <- getTextRqParam exprName expr <- getTextRqParam exprName
process $ expr >>= parse process $ expr >>= parse


ajaxElem :: (HasAjax b, Read a) => readElem :: (HasAjax b, Read a) =>
JExpr JExpr
-> ButtonHandler b a -> ButtonHandler b a
-> HeistT (Handler b b) (Attrs, Children) -> HeistT (Handler b b) (Attrs, Children)
ajaxElem = ajaxElemWithParser $ readMay . B.unpack readElem = elemWithParser $ readMay . B.unpack

ajaxJsonElem :: (HasAjax b, FromJSON a) =>
JExpr
-> ButtonHandler b a
-> HeistT (Handler b b) (Attrs, Children)
ajaxJsonElem jsonExpr = ajaxElemWithParser (decode . lazyFromStrict)
[jmacroE|JSON.stringify(`(jsonExpr)`)|]


ajaxElem_ :: HasAjax b => Handler b b JStat -> HeistT (Handler b b) (Attrs, Children) jsonElem :: (HasAjax b, FromJSON a) =>
ajaxElem_ = ajaxElemWithHandler [] JExpr
-> ButtonHandler b a
-> HeistT (Handler b b) (Attrs, Children)
jsonElem jsonExpr = elemWithParser (decode . lazyFromStrict)
[jmacroE|JSON.stringify(`(jsonExpr)`)|]


------------------------------------------------------------------------------ ------------------------------------------------------------------------------
-- Buttons -- Buttons
Expand All @@ -158,16 +157,16 @@ ajaxElem_ = ajaxElemWithHandler []
toButton :: HeistT (Handler b b) (Attrs, Children) -> Splice (Handler b b) toButton :: HeistT (Handler b b) (Attrs, Children) -> Splice (Handler b b)
toButton = fmap $ pure . uncurry (X.Element "button") toButton = fmap $ pure . uncurry (X.Element "button")


ajaxButton :: (HasAjax b, Read a) => button :: HasAjax b => Handler b b JStat -> Splice (Handler b b)
JExpr -> ButtonHandler b a -> Splice (Handler b b) button h = toButton $ elemWithHandler h
ajaxButton j h = toButton $ ajaxElem j h


ajaxButton_ :: HasAjax b => Handler b b JStat -> Splice (Handler b b) readButton :: (HasAjax b, Read a) =>
ajaxButton_ h = toButton $ ajaxElem_ h JExpr -> ButtonHandler b a -> Splice (Handler b b)
readButton j h = toButton $ readElem j h


ajaxJsonButton :: (HasAjax b, FromJSON a) => jsonButton :: (HasAjax b, FromJSON a) =>
JExpr -> ButtonHandler b a -> Splice (Handler b b) JExpr -> ButtonHandler b a -> Splice (Handler b b)
ajaxJsonButton j h = toButton $ ajaxJsonElem j h jsonButton j h = toButton $ jsonElem j h


------------------------------------------------------------------------------ ------------------------------------------------------------------------------
-- Anchors -- Anchors
Expand All @@ -176,13 +175,13 @@ ajaxJsonButton j h = toButton $ ajaxJsonElem j h
toAnchor :: HeistT (Handler b b) (Attrs, Children) -> Splice (Handler b b) toAnchor :: HeistT (Handler b b) (Attrs, Children) -> Splice (Handler b b)
toAnchor = fmap $ \(as,cs) -> [X.Element "a" (addAttrs [("href", "javascript://")] as) cs] toAnchor = fmap $ \(as,cs) -> [X.Element "a" (addAttrs [("href", "javascript://")] as) cs]


ajaxAnchor :: (HasAjax b, Read a) => anchor :: HasAjax b => Handler b b JStat -> Splice (Handler b b)
JExpr -> ButtonHandler b a -> Splice (Handler b b) anchor h = toAnchor $ elemWithHandler h
ajaxAnchor j h = toAnchor $ ajaxElem j h


ajaxAnchor_ :: HasAjax b => Handler b b JStat -> Splice (Handler b b) readAnchor :: (HasAjax b, Read a) =>
ajaxAnchor_ h = toAnchor $ ajaxElem_ h JExpr -> ButtonHandler b a -> Splice (Handler b b)
readAnchor j h = toAnchor $ readElem j h


ajaxJsonAnchor :: (HasAjax b, FromJSON a) => jsonAnchor :: (HasAjax b, FromJSON a) =>
JExpr -> ButtonHandler b a -> Splice (Handler b b) JExpr -> ButtonHandler b a -> Splice (Handler b b)
ajaxJsonAnchor j h = toAnchor $ ajaxJsonElem j h jsonAnchor j h = toAnchor $ jsonElem j h

0 comments on commit 01916b5

Please sign in to comment.