{-| Table building library for yesod This library is intended to be brought in by a qualified import along with type import as follows: > import qualified Yesod.Table as Table > import Yesod.Table (Table) There are two types in this module: 'Table' and 'Column'. Roughly, a 'Table' is just a list of 'Column's. Except in the case of rendering a 'Table', you should not need to use the data constructors of either of these types. (In fact, you should not need to refer to the type 'Column' either). Instead, you should use the functions 'singleton', 'text', 'int', etc. to build singleton 'Table's (a 'Table' with only one 'Column') and use monoidal concatenation to combine these. It is important to note that, as defined in this library, 'Table' refers to a blueprint for an HTML table, not a complete table with content. If you want to define your own table rendering function (and it's likely that you will), then you will need the aforementioned data constructors. You can look at the source of 'buildBootstrap' for an example of how to do this. -} module Yesod.Table ( Table(..) , Column(..) , buildBootstrap , singleton , widget , const , text , string , bytestring , show , printf , int , linked , when , whenWith , maybe , maybeWith , columns , columns' , bool ) where import Prelude hiding (const, mapM_, maybe, show, when) import qualified Prelude as Prelude import Data.ByteString (ByteString) import Data.Foldable (forM_, mapM_) import Data.Functor.Contravariant import Data.Functor.Contravariant.Divisible import qualified Data.Maybe as M import Data.Monoid import Data.Semigroup (Semigroup) import Data.Sequence (Seq) import qualified Data.Sequence as Seq import Data.Text (Text) import qualified Data.Text as Text import Data.Text.Encoding (decodeUtf8') import qualified Text.Printf as Printf import Yesod.Core import Yesod.Core.Widget newtype Table site a = Table (Seq (Column site a)) deriving (Semigroup, Monoid) data Column site a = Column { header :: !(WidgetT site IO ()) , cell :: !(a -> WidgetT site IO ()) } instance Contravariant (Column site) where contramap g (Column h c) = Column h (c . g) instance Contravariant (Table site) where contramap g (Table cols) = Table (fmap (contramap g) cols) instance Divisible (Table site) where conquer = mempty divide f (Table aCols) (Table bCols) = Table $ (fmap (contramap (fst . f)) aCols) <> (fmap (contramap (snd . f)) bCols) -- | This is the most primitive and essential operation for building a 'Table'. -- All other table-building functions (such as 'widget', 'text', and 'linked') -- build on top of 'singleton'. One common trend in the convenience functions -- is that they accept 'Text' as the table header. This is done because I have -- found that it is uncommon to need the full power of HTML in the header. -- Just know that if you need it, this function is the only way to get it. -- The first argument is a widget that is the -- content to be displayed in the table header. The second argument is the -- a function that consumes a value to produce the content shown in a row of the -- table body. These widgets need to be wrapped in a th/td if you are -- using this function. singleton :: WidgetT site IO () -> (a -> WidgetT site IO ()) -> Table site a singleton c h = Table (Seq.singleton (Column c h)) -- | This is the same as 'singleton', with the convenience of accepting -- the table header as 'Text'. It also wraps the header widget in -- a th element and wraps the cell widget in a td element. widget :: Text -> (a -> WidgetT site IO ()) -> Table site a widget h c = singleton (asWidgetIO [whamlet|