{-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE OverloadedStrings #-} module Yesod.Paginator.Widget ( getCurrentPage , paginationWidget ) where import Yesod import Control.Monad (when) import Data.Maybe (fromMaybe) import Data.Text (Text) import qualified Data.Text as T -- | Individual links to pages need to follow strict (but sane) markup -- to be styled correctly by bootstrap. This type allows construction -- of such links in both enabled and disabled states. data PageLink = Enabled Int String String -- ^ page, content, class | Disabled String String -- ^ content, class -- | Correctly show one of the constructed links showLink :: [(Text, Text)] -> PageLink -> GWidget s m () showLink params (Enabled pg cnt cls) = do let param = ("p", T.pack . show $ pg) [whamlet|
  • #{cnt} |] where updateGetParam :: [(Text,Text)] -> (Text,Text) -> Text updateGetParam getParams (p, n) = (T.cons '?') . T.intercalate "&" . map (\(k,v) -> k `T.append` "=" `T.append` v) . (++ [(p, n)]) . filter ((/= p) . fst) $ getParams showLink _ (Disabled cnt cls) = [whamlet|
  • #{cnt} |] -- | A widget showing pagination links. Follows bootstrap principles. -- Utilizes a \"p\" GET param but leaves all other GET params intact. paginationWidget :: Int -- ^ current page -> Int -- ^ items per page -> Int -- ^ total number of items -> GWidget s m () paginationWidget page per tot = do -- total / per + 1 for any remainder let pages = (\(n, r) -> n + (min r 1)) $ tot `divMod` per when (pages > 1) $ do curParams <- lift $ fmap reqGetParams getRequest [whamlet|