make api more useable

This commit is contained in:
Kyle McKean 2017-04-14 15:06:00 -04:00
parent cb9d9091b8
commit 31c423ad1a

View File

@ -12,11 +12,9 @@ module Reflex.Dom.Colonnade
-- * Table Encoders -- * Table Encoders
, basic , basic
, static , static
, eventful
, dynamic
, dynamicEventful
, capped , capped
, cappedEventful , dynamic
, dynamicCapped
-- * Cell Functions -- * Cell Functions
, cell , cell
, charCell , charCell
@ -32,10 +30,8 @@ import qualified Data.Text.Lazy as LT
import qualified Data.Text.Lazy.Builder as LT import qualified Data.Text.Lazy.Builder as LT
import qualified Data.Map.Strict as M import qualified Data.Map.Strict as M
import Data.Foldable (Foldable(..),for_) import Data.Foldable (Foldable(..),for_)
import Data.Traversable (for)
import Data.Semigroup (Semigroup(..)) import Data.Semigroup (Semigroup(..))
import Control.Applicative (liftA2) import Control.Applicative (liftA2)
import Control.Monad (void)
import Reflex.Dom import Reflex.Dom
import Colonnade (Colonnade,Headed,Fascia,Cornice) import Colonnade (Colonnade,Headed,Fascia,Cornice)
import qualified Colonnade.Encode as E import qualified Colonnade.Encode as E
@ -107,15 +103,15 @@ body bodyAttrs trAttrs colonnade collection =
E.rowMonoidal colonnade (WrappedApplicative . elFromCell "td") a E.rowMonoidal colonnade (WrappedApplicative . elFromCell "td") a
static :: static ::
(DomBuilder t m, PostBuild t m, Foldable f, Foldable h) (DomBuilder t m, PostBuild t m, Foldable f, Foldable h, Monoid e)
=> M.Map T.Text T.Text -- ^ @\<table\>@ tag attributes => M.Map T.Text T.Text -- ^ @\<table\>@ tag attributes
-> Maybe (M.Map T.Text T.Text, M.Map T.Text T.Text) -> Maybe (M.Map T.Text T.Text, M.Map T.Text T.Text)
-- ^ Attributes of @\<thead\>@ and its @\<tr\>@, pass 'Nothing' to omit @\<thead\>@ -- ^ Attributes of @\<thead\>@ and its @\<tr\>@, pass 'Nothing' to omit @\<thead\>@
-> M.Map T.Text T.Text -- ^ @\<tbody\>@ tag attributes -> M.Map T.Text T.Text -- ^ @\<tbody\>@ tag attributes
-> (a -> M.Map T.Text T.Text) -- ^ @\<tr\>@ tag attributes -> (a -> M.Map T.Text T.Text) -- ^ @\<tr\>@ tag attributes
-> Colonnade h a (Cell t m ()) -- ^ Data encoding strategy -> Colonnade h a (Cell t m e) -- ^ Data encoding strategy
-> f a -- ^ Collection of data -> f a -- ^ Collection of data
-> m () -> m e
static tableAttrs mheadAttrs bodyAttrs trAttrs colonnade collection = static tableAttrs mheadAttrs bodyAttrs trAttrs colonnade collection =
elAttr "table" tableAttrs $ do elAttr "table" tableAttrs $ do
for_ mheadAttrs $ \(headAttrs,headTrAttrs) -> for_ mheadAttrs $ \(headAttrs,headTrAttrs) ->
@ -123,114 +119,91 @@ static tableAttrs mheadAttrs bodyAttrs trAttrs colonnade collection =
E.headerMonadicGeneral_ colonnade (elFromCell "th") E.headerMonadicGeneral_ colonnade (elFromCell "th")
body bodyAttrs trAttrs colonnade collection body bodyAttrs trAttrs colonnade collection
eventful :: encodeCorniceHead ::
(DomBuilder t m, PostBuild t m, Foldable f, Foldable h, Semigroup e) (DomBuilder t m, PostBuild t m, Monoid e)
=> M.Map T.Text T.Text
-> Fascia p (M.Map T.Text T.Text)
-> E.AnnotatedCornice p a (Cell t m e)
-> m e
encodeCorniceHead headAttrs fascia annCornice =
elAttr "thead" headAttrs (unWrappedApplicative thead)
where thead = E.headersMonoidal (Just (fascia, addAttr)) [(th,id)] annCornice
th size (Cell attrs contents) = WrappedApplicative (elDynAttr "th" (fmap addColspan attrs) contents)
where addColspan = M.insert "colspan" (T.pack (show size))
addAttr attrs = WrappedApplicative . elAttr "tr" attrs . unWrappedApplicative
capped ::
(DomBuilder t m, PostBuild t m, MonadHold t m, Foldable f, Monoid e)
=> M.Map T.Text T.Text -- ^ @\<table\>@ tag attributes => M.Map T.Text T.Text -- ^ @\<table\>@ tag attributes
-> Maybe (M.Map T.Text T.Text, M.Map T.Text T.Text) -> M.Map T.Text T.Text -- ^ @\<thead\>@ tag attributes
-- ^ Attributes of @\<thead\>@ and its @\<tr\>@, pass 'Nothing' to omit @\<thead\>@
-> M.Map T.Text T.Text -- ^ @\<tbody\>@ tag attributes -> M.Map T.Text T.Text -- ^ @\<tbody\>@ tag attributes
-> (a -> M.Map T.Text T.Text) -- ^ @\<tr\>@ tag attributes -> (a -> M.Map T.Text T.Text) -- ^ @\<tr\>@ tag attributes
-> Colonnade h a (Cell t m (Event t e)) -- ^ Data encoding strategy -> Fascia p (M.Map T.Text T.Text) -- ^ Attributes for @\<tr\>@ elements in the @\<thead\>@
-> Cornice p a (Cell t m e) -- ^ Data encoding strategy
-> f a -- ^ Collection of data -> f a -- ^ Collection of data
-> m (Event t e) -> m e
eventful tableAttrs mheadAttrs bodyAttrs trAttrs colonnade collection = capped tableAttrs headAttrs bodyAttrs trAttrs fascia cornice collection =
elAttr "table" tableAttrs $ do elAttr "table" tableAttrs $ do
eHead <- for mheadAttrs $ \(headAttrs,headTrAttrs) -> h <- encodeCorniceHead headAttrs fascia (E.annotate cornice)
elAttr "thead" headAttrs . elAttr "tr" headTrAttrs $ b <- body bodyAttrs trAttrs (E.discard cornice) collection
E.headerMonadicGeneral colonnade (elFromCell "th") return (h `mappend` b)
eBody <- body bodyAttrs trAttrs colonnade collection
return (maybe never id eHead <> eBody)
dynamicBody :: (DomBuilder t m, PostBuild t m, Foldable f, Semigroup e, Monoid e) dynamicBody :: (DomBuilder t m, PostBuild t m, Foldable f, Semigroup e, Monoid e)
=> Dynamic t (M.Map T.Text T.Text) => Dynamic t (M.Map T.Text T.Text)
-> (a -> M.Map T.Text T.Text) -> (a -> M.Map T.Text T.Text)
-> Colonnade p a (Cell t m e) -> Colonnade p a (Cell t m e)
-> f (Dynamic t a) -> Dynamic t (f a)
-> m (Event t e) -> m (Event t e)
dynamicBody bodyAttrs trAttrs colonnade collection = dynamicBody bodyAttrs trAttrs colonnade dynCollection =
elDynAttr "tbody" bodyAttrs . unWrappedApplicative . flip foldMap collection $ \aDyn -> elDynAttr "tbody" bodyAttrs . dyn . ffor dynCollection $ \collection ->
WrappedApplicative . unWrappedApplicative .
elDynAttr "tr" (fmap trAttrs aDyn) $ flip foldMap collection $ \a ->
dyn (fmap (unWrappedApplicative . E.rowMonoidal colonnade (WrappedApplicative . elFromCell "td")) aDyn) WrappedApplicative .
elAttr "tr" (trAttrs a) .
unWrappedApplicative . E.rowMonoidal colonnade (WrappedApplicative . elFromCell "td") $ a
dynamic :: dynamic ::
(DomBuilder t m, PostBuild t m, Foldable f, Foldable h) (DomBuilder t m, PostBuild t m, Foldable f, Foldable h, Semigroup e, Monoid e)
=> Dynamic t (M.Map T.Text T.Text) -- ^ @\<table\>@ tag attributes => Dynamic t (M.Map T.Text T.Text) -- ^ @\<table\>@ tag attributes
-> Maybe (Dynamic t (M.Map T.Text T.Text), Dynamic t (M.Map T.Text T.Text)) -> Maybe (Dynamic t (M.Map T.Text T.Text), Dynamic t (M.Map T.Text T.Text))
-- ^ Attributes of @\<thead\>@ and its @\<tr\>@, pass 'Nothing' to omit @\<thead\>@ -- ^ Attributes of @\<thead\>@ and its @\<tr\>@, pass 'Nothing' to omit @\<thead\>@
-> Dynamic t (M.Map T.Text T.Text) -- ^ @\<tbody\>@ tag attributes -> Dynamic t (M.Map T.Text T.Text) -- ^ @\<tbody\>@ tag attributes
-> (a -> M.Map T.Text T.Text) -- ^ @\<tr\>@ tag attributes -> (a -> M.Map T.Text T.Text) -- ^ @\<tr\>@ tag attributes
-> Colonnade h a (Cell t m ()) -- ^ Data encoding strategy -> Colonnade h a (Cell t m e) -- ^ Data encoding strategy
-> f (Dynamic t a) -- ^ Collection of data -> Dynamic t (f a) -- ^ Collection of data
-> m () -> m (Event t e)
dynamic tableAttrs mheadAttrs bodyAttrs trAttrs colonnade collection = dynamic tableAttrs mheadAttrs bodyAttrs trAttrs colonnade collection =
elDynAttr "table" tableAttrs $ do elDynAttr "table" tableAttrs $ do
for_ mheadAttrs $ \(headAttrs,headTrAttrs) -> for_ mheadAttrs $ \(headAttrs,headTrAttrs) ->
elDynAttr "thead" headAttrs . elDynAttr "tr" headTrAttrs $ elDynAttr "thead" headAttrs . elDynAttr "tr" headTrAttrs $
E.headerMonadicGeneral_ colonnade (elFromCell "th") E.headerMonadicGeneral_ colonnade (elFromCell "th")
void (dynamicBody bodyAttrs trAttrs colonnade collection) dynamicBody bodyAttrs trAttrs colonnade collection
dynamicEventful :: encodeCorniceHeadDynamic ::
(DomBuilder t m, PostBuild t m, MonadHold t m, Foldable f, Foldable h, Semigroup e)
=> Dynamic t (M.Map T.Text T.Text) -- ^ @\<table\>@ tag attributes
-> Maybe (Dynamic t (M.Map T.Text T.Text), Dynamic t (M.Map T.Text T.Text))
-- ^ Attributes of @\<thead\>@ and its @\<tr\>@, pass 'Nothing' to omit @\<thead\>@
-> Dynamic t (M.Map T.Text T.Text) -- ^ @\<tbody\>@ tag attributes
-> (a -> M.Map T.Text T.Text) -- ^ @\<tr\>@ tag attributes
-> Colonnade h a (Cell t m (Event t e)) -- ^ Data encoding strategy
-> f (Dynamic t a) -- ^ Collection of data
-> m (Event t e)
dynamicEventful tableAttrs mheadAttrs bodyAttrs trAttrs colonnade collection =
elDynAttr "table" tableAttrs $ do
eHead <- for mheadAttrs $ \(headAttrs,headTrAttrs) ->
elDynAttr "thead" headAttrs . elDynAttr "tr" headTrAttrs $
E.headerMonadicGeneral colonnade (elFromCell "th")
eeBody <- dynamicBody bodyAttrs trAttrs colonnade collection
eBody <- hold never eeBody
return (maybe never id eHead <> switch eBody)
encodeCorniceHead ::
(DomBuilder t m, PostBuild t m, Monoid e) (DomBuilder t m, PostBuild t m, Monoid e)
=> Dynamic t (M.Map T.Text T.Text) => Dynamic t (M.Map T.Text T.Text)
-> Fascia p (Dynamic t (M.Map T.Text T.Text)) -> Fascia p (Dynamic t (M.Map T.Text T.Text))
-> E.AnnotatedCornice p a (Cell t m e) -> E.AnnotatedCornice p a (Cell t m e)
-> m e -> m e
encodeCorniceHead headAttrs fascia annCornice = encodeCorniceHeadDynamic headAttrs fascia annCornice =
elDynAttr "thead" headAttrs (unWrappedApplicative thead) elDynAttr "thead" headAttrs (unWrappedApplicative thead)
where thead = E.headersMonoidal (Just (fascia, addAttr)) [(th,id)] annCornice where thead = E.headersMonoidal (Just (fascia, addAttr)) [(th,id)] annCornice
th size (Cell attrs contents) = WrappedApplicative (elDynAttr "th" (fmap addColspan attrs) contents) th size (Cell attrs contents) = WrappedApplicative (elDynAttr "th" (fmap addColspan attrs) contents)
where addColspan = M.insert "colspan" (T.pack (show size)) where addColspan = M.insert "colspan" (T.pack (show size))
addAttr attrs = WrappedApplicative . elDynAttr "tr" attrs . unWrappedApplicative addAttr attrs = WrappedApplicative . elDynAttr "tr" attrs . unWrappedApplicative
capped :: dynamicCapped ::
(DomBuilder t m, PostBuild t m, MonadHold t m, Foldable f) (DomBuilder t m, PostBuild t m, MonadHold t m, Foldable f, Semigroup e, Monoid e)
=> Dynamic t (M.Map T.Text T.Text) -- ^ @\<table\>@ tag attributes => Dynamic t (M.Map T.Text T.Text) -- ^ @\<table\>@ tag attributes
-> Dynamic t (M.Map T.Text T.Text) -- ^ @\<thead\>@ tag attributes -> Dynamic t (M.Map T.Text T.Text) -- ^ @\<thead\>@ tag attributes
-> Dynamic t (M.Map T.Text T.Text) -- ^ @\<tbody\>@ tag attributes -> Dynamic t (M.Map T.Text T.Text) -- ^ @\<tbody\>@ tag attributes
-> (a -> M.Map T.Text T.Text) -- ^ @\<tr\>@ tag attributes -> (a -> M.Map T.Text T.Text) -- ^ @\<tr\>@ tag attributes
-> Fascia p (Dynamic t (M.Map T.Text T.Text)) -- ^ Attributes for @\<tr\>@ elements in the @\<thead\>@ -> Fascia p (Dynamic t (M.Map T.Text T.Text)) -- ^ Attributes for @\<tr\>@ elements in the @\<thead\>@
-> Cornice p a (Cell t m ()) -- ^ Data encoding strategy -> Cornice p a (Cell t m e) -- ^ Data encoding strategy
-> f (Dynamic t a) -- ^ Collection of data -> Dynamic t (f a) -- ^ Collection of data
-> m ()
capped tableAttrs headAttrs bodyAttrs trAttrs fascia cornice collection =
elDynAttr "table" tableAttrs $ do
encodeCorniceHead headAttrs fascia (E.annotate cornice)
void (dynamicBody bodyAttrs trAttrs (E.discard cornice) collection)
cappedEventful ::
forall t m f e p a.
(DomBuilder t m, PostBuild t m, MonadHold t m, Foldable f, Semigroup e)
=> Dynamic t (M.Map T.Text T.Text) -- ^ @\<table\>@ tag attributes
-> Dynamic t (M.Map T.Text T.Text) -- ^ @\<thead\>@ tag attributes
-> Dynamic t (M.Map T.Text T.Text) -- ^ @\<tbody\>@ tag attributes
-> (a -> M.Map T.Text T.Text) -- ^ @\<tr\>@ tag attributes
-> Fascia p (Dynamic t (M.Map T.Text T.Text)) -- ^ Attributes for @\<tr\>@ elements in the @\<thead\>@
-> Cornice p a (Cell t m (Event t e)) -- ^ Data encoding strategy
-> f (Dynamic t a) -- ^ Collection of data
-> m (Event t e) -> m (Event t e)
cappedEventful tableAttrs headAttrs bodyAttrs trAttrs fascia cornice collection = dynamicCapped tableAttrs headAttrs bodyAttrs trAttrs fascia cornice collection =
elDynAttr "table" tableAttrs $ do elDynAttr "table" tableAttrs $ do
eHead <- encodeCorniceHead headAttrs fascia (E.annotate cornice) -- TODO: Figure out what this ignored argument represents and dont ignore it
eeBody <- dynamicBody bodyAttrs trAttrs (E.discard cornice) collection _ <- encodeCorniceHeadDynamic headAttrs fascia (E.annotate cornice)
eBody <- hold never eeBody dynamicBody bodyAttrs trAttrs (E.discard cornice) collection
return (eHead <> switch eBody)