{- This file is part of Vervis. - - Written in 2016 by fr33domlover . - - ♡ Copying is an act of love. Please copy, reuse and share. - - The author(s) have dedicated all copyright and related and neighboring - rights to this software to the public domain worldwide. This software is - distributed without any warranty. - - You should have received a copy of the CC0 Public Domain Dedication along - with this software. If not, see - . -} module Yesod.Paginate.Local ( -- * Settings NavWidgetSettings () , nwsFirst , nwsLast , nwsPrev , nwsNext , nwsCurrent -- * Widget , pageNavWidget ) where import Prelude import Data.Default.Class import Data.Text (Text) import Text.Blaze (ToMarkup) import Yesod.Core (RenderRoute (..)) import Yesod.Core.Widget (WidgetT, whamlet) import qualified Formatting as F import Data.Paginate.Local -- | Settings for building a UI page navigation widget. data NavWidgetSettings = NavWidgetSettings { -- | Label for the first page link. Examples: 1, First, ≪, ⋘. nwsFirst :: Text -- | Label for the last page link. The parameter is the number of the -- last page. Examples: The page number, Last, ≫, ⋙. , nwsLast :: Int -> Text -- | Label for the previous page link. The parameter is the page number. -- Examples: The page number, Previous, <, ≪. , nwsPrev :: Int -> Text -- | Label for the next page link. The parameter is the page number. -- Examples: The page number, Next, >, ≫. , nwsNext :: Int -> Text -- | Label for the current page. The parameters are the current page -- number, and the total number of pages. Example: /Page 3 of 8/. , nwsCurrent :: Int -> Int -> Text } instance Default NavWidgetSettings where def = NavWidgetSettings { nwsFirst = "≪" , nwsLast = \ _ -> "≫" , nwsPrev = \ _ -> "<" , nwsNext = \ _ -> ">" , nwsCurrent = F.sformat (F.int F.% " / " F.% F.int) } pageNavWidget :: ToMarkup t => NavModel -> NavWidgetSettings -> (Int -> (Route site, t)) -> WidgetT site IO () pageNavWidget nm nws mklink = let link n label = let (route, suffix) = mklink n in [whamlet| #{label} |] in [whamlet|
    $if nmFirst nm
  • ^{link 1 $ nwsFirst nws}
  • #{nwsCurrent nws (nmCurrent nm) (nmTotal nm)} $if nmLast nm
  • ^{link (nmTotal nm) (nwsLast nws $ nmTotal nm)} |]