@@ -1,4 +1,4 @@
1−{-# LANGUAGE QuasiQuotes #-}
1+{-# LANGUAGE QuasiQuotes, TypeSynonymInstances #-}
2{- | This module provides a function for compiling Elm source code into a Yesod widget.
2{- | This module provides a function for compiling Elm source code into a Yesod widget.
3 In order to use this with your Yesod app, you need to define a defaultLayout like
3 In order to use this with your Yesod app, you need to define a defaultLayout like
4 function that embeds the elm-min.js file /in the <head> tag/.
4 function that embeds the elm-min.js file /in the <head> tag/.
@@ -15,32 +15,46 @@
16 A full example implementation is provided in the examples folder of the Elm github repository.
16 A full example implementation is provided in the examples folder of the Elm github repository.
18−module Language.Elm.Yesod (elmWidget) where
18+module Language.Elm.Yesod (
22+import Control.Monad (liftM)
20import Text.Blaze (preEscapedToMarkup)
23import Text.Blaze (preEscapedToMarkup)
21import Text.Julius
24import Text.Julius
22−import Yesod.Widget (toWidgetHead, toWidgetBody, GWidget)
25+import Yesod.Core
23import Language.Elm
28import Language.Elm
24import Language.Elm.Quasi
29import Language.Elm.Quasi
26import qualified Data.Text as TS
31import qualified Data.Text as TS
27import qualified Data.Text.Lazy as TL
32import qualified Data.Text.Lazy as TL
29−-- |toWidget takes some Elm code in String format and produces a widget. Usage example:
34+instance ElmSource (ElmUrl url) where
30−--
35+ toHtml = undefined
31−-- > toWidget [elmFile|elm-source/somePage.elm|]
36+ toParts = undefined
32−elmWidget :: ElmSource a
37+
33− => a -- ^ The Elm source code
39+elmWidget :: ElmUrl (Route master) -- ^ Elm source code
34− -> GWidget sub master ()
40+ -> GHandler sub master (GWidget sub master())
35−elmWidget source =
41+elmWidget source = do
42+ urlF <- getUrlRenderParams
43+ return $ mkElmWidget source urlF
46+mkElmWidget :: ElmUrl (Route master) -- ^ Elm source code
47+ -> RenderFn (Route master) -- ^ URL rendering function
48+ -> GWidget sub master ()
49+mkElmWidget source urlFn =
36− let (html, css, js) = toParts source in
50+ let (html, css, js) = toParts (urlFn, source) in
37 do toWidgetHead css
51 do toWidgetHead css
38 toWidgetHead [julius| #{js} |]
52 toWidgetHead [julius| #{js} |]
39 toWidgetBody html
53 toWidgetBody html
41-- | Return type of template-reading functions.
56-- | Return type of template-reading functions.
42type ElmUrl url = (url -> [(TS.Text, TS.Text)] -> TS.Text) -> Elm
57type ElmUrl url = (url -> [(TS.Text, TS.Text)] -> TS.Text) -> Elm
44−-- | render with route interpolation
59+-- | Readability synonym for render function
45−renderElmUrl :: (url -> [(TS.Text, TS.Text)] -> TS.Text) -> ElmUrl url -> TL.Text
60+type RenderFn url = (url -> [(TS.Text, TS.Text)] -> TS.Text)
46−renderElmUrl r s = renderElm $ s r