yesod-colonnade 0.1 → 0.2
raw patch · 2 files changed
+67/−18 lines, 2 filesdep ~colonnadePVP ok
version bump matches the API change (PVP)
Dependency ranges changed: colonnade
API changes (from Hackage documentation)
+ Yesod.Colonnade: anchorCell :: (a -> Route site) -> (a -> WidgetT site IO ()) -> a -> Cell site
+ Yesod.Colonnade: definitionTable :: [(Text, Text)] -> Colonnade Headed (Cell site) a -> a -> WidgetT site IO ()
+ Yesod.Colonnade: instance GHC.Base.Monoid (Yesod.Colonnade.Cell site)
+ Yesod.Colonnade: tableHeadless :: Foldable f => [(Text, Text)] -> Colonnade Headless (Cell site) a -> f a -> WidgetT site IO ()
- Yesod.Colonnade: listItems :: Foldable f => (WidgetT site IO () -> WidgetT site IO ()) -> (WidgetT site IO () -> WidgetT site IO () -> WidgetT site IO ()) -> Encoding Headed (Cell site) a -> f a -> WidgetT site IO ()
+ Yesod.Colonnade: listItems :: (WidgetT site IO () -> WidgetT site IO ()) -> (WidgetT site IO () -> WidgetT site IO () -> WidgetT site IO ()) -> Colonnade Headed (Cell site) a -> a -> WidgetT site IO ()
- Yesod.Colonnade: table :: Foldable f => [(Text, Text)] -> Encoding Headed (Cell site) a -> f a -> WidgetT site IO ()
+ Yesod.Colonnade: table :: Foldable f => [(Text, Text)] -> Colonnade Headed (Cell site) a -> f a -> WidgetT site IO ()
Files
- src/Yesod/Colonnade.hs +65/−16
- yesod-colonnade.cabal +2/−2
src/Yesod/Colonnade.hs view
@@ -2,24 +2,33 @@ {-# LANGUAGE QuasiQuotes #-} module Yesod.Colonnade- ( table- , listItems- , Cell(..)+ ( -- * Build Encoding+ Cell(..) , cell , stringCell , textCell , builderCell+ , anchorCell+ -- * Apply Encoding+ , table+ , tableHeadless+ , definitionTable+ , listItems ) where import Yesod.Core-import Colonnade.Types+import Colonnade.Types (Colonnade,Headed,Headless) import Data.Text (Text) import Control.Monad+import Data.Monoid import Data.String (IsString(..)) import qualified Colonnade.Encoding as Encoding+import qualified Data.Text as Text import qualified Data.Text.Lazy as LText import qualified Data.Text.Lazy.Builder as TBuilder +-- | The attributes that will be applied to a @<td>@ and+-- the HTML content that will go inside it. data Cell site = Cell { cellAttrs :: ![(Text,Text)] , cellContents :: !(WidgetT site IO ())@@ -28,44 +37,81 @@ instance IsString (Cell site) where fromString = stringCell +instance Monoid (Cell site) where+ mempty = Cell [] mempty+ mappend (Cell a1 c1) (Cell a2 c2) = Cell (mappend a1 a2) (mappend c1 c2)++-- | Create a 'Cell' from a 'Widget' cell :: WidgetT site IO () -> Cell site cell = Cell [] +-- | Create a 'Cell' from a 'String' stringCell :: String -> Cell site stringCell = cell . fromString +-- | Create a 'Cell' from a 'Text' textCell :: Text -> Cell site textCell = cell . toWidget . toHtml +-- | Create a 'Cell' from a text builder builderCell :: TBuilder.Builder -> Cell site builderCell = cell . toWidget . toHtml . LText.toStrict . TBuilder.toLazyText +-- | Creata a 'Cell' whose content is hyperlinked by wrapping+-- it in an @<a>@.+anchorCell :: + (a -> Route site) -- ^ Route that will go in @href@+ -> (a -> WidgetT site IO ()) -- ^ Content wrapped by @<a>@+ -> a -- ^ Value+ -> Cell site+anchorCell getRoute getContent a = cell $ do+ urlRender <- getUrlRender+ aTag [(Text.pack "href",urlRender (getRoute a))] (getContent a)+ -- | This determines the attributes that are added -- to the individual @li@s by concatenating the header\'s--- attributes with the data\'s attributes.-listItems :: Foldable f- => (WidgetT site IO () -> WidgetT site IO ())+-- attributes with the data\'s attributes. +listItems :: + (WidgetT site IO () -> WidgetT site IO ()) -- ^ Wrapper for items, often @ul@ -> (WidgetT site IO () -> WidgetT site IO () -> WidgetT site IO ()) -- ^ Combines header with data- -> Encoding Headed (Cell site) a+ -> Colonnade Headed (Cell site) a -- ^ How to encode data as a row- -> f a- -- ^ Rows of data+ -> a+ -- ^ The value to display -> WidgetT site IO ()-listItems ulWrap combine enc xs =- forM_ xs $ ulWrap . Encoding.runBothMonadic_ enc+listItems ulWrap combine enc =+ ulWrap . Encoding.runBothMonadic_ enc (\(Cell ha hc) (Cell ba bc) -> li (ha ++ ba) (combine hc bc) ) +-- | A two-column table with the header content displayed in the+-- first column and the data displayed in the second column. Note+-- that the generated HTML table does not have a @thead@.+definitionTable ::+ [(Text,Text)]+ -- ^ Attributes of @table@ element.+ -> Colonnade Headed (Cell site) a+ -- ^ How to encode data as a row+ -> a+ -- ^ The value to display+ -> WidgetT site IO ()+definitionTable attrs enc a = tableEl attrs $ tbody [] $ + Encoding.runBothMonadic_ enc+ (\theKey theValue -> tr [] $ do+ widgetFromCell td theKey+ widgetFromCell td theValue+ ) a+ -- | If you are using the bootstrap css framework, then you may want -- to call this with the first argument as: -- -- > table [("class","table table-striped")] ... table :: Foldable f => [(Text,Text)] -- ^ Attributes of @table@ element- -> Encoding Headed (Cell site) a -- ^ How to encode data as a row+ -> Colonnade Headed (Cell site) a -- ^ How to encode data as a row -> f a -- ^ Rows of data -> WidgetT site IO () table attrs enc xs = tableEl attrs $ do@@ -74,13 +120,13 @@ tableHeadless :: Foldable f => [(Text,Text)] -- ^ Attributes of @table@ element- -> Encoding Headless (Cell site) a -- ^ How to encode data as a row+ -> Colonnade Headless (Cell site) a -- ^ How to encode data as a row -> f a -- ^ Rows of data -> WidgetT site IO () tableHeadless attrs enc xs = tableEl attrs $ tableBody enc xs tableBody :: Foldable f- => Encoding h (Cell site) a -- ^ How to encode data as a row+ => Colonnade h (Cell site) a -- ^ How to encode data as a row -> f a -- ^ Rows of data -> WidgetT site IO () tableBody enc xs = tbody [] $ do@@ -94,7 +140,7 @@ widgetFromCell f (Cell attrs contents) = f attrs contents -tr,tbody,thead,tableEl,td,th,ul,li ::+tr,tbody,thead,tableEl,td,th,ul,li,aTag :: [(Text,Text)] -> WidgetT site IO () -> WidgetT site IO () tableEl str b = [whamlet| <table *{str}>^{b}@@ -119,5 +165,8 @@ |] li str b = [whamlet| <li *{str}>^{b}+|]+aTag str b = [whamlet|+ <a *{str}>^{b} |]
yesod-colonnade.cabal view
@@ -1,5 +1,5 @@ name: yesod-colonnade-version: 0.1+version: 0.2 synopsis: Helper functions for using yesod with colonnade description: Yesod and colonnade homepage: https://github.com/andrewthad/colonnade#readme@@ -18,7 +18,7 @@ Yesod.Colonnade build-depends: base >= 4.7 && < 5- , colonnade >= 0.4.6 && < 0.5+ , colonnade >= 0.5 && < 0.6 , yesod-core >= 1.4.0 && < 1.5 , text >= 1.0 && < 1.3 default-language: Haskell2010