1
0
Fork 0
mirror of https://code.naskya.net/repos/ndqEd synced 2025-01-27 17:17:51 +09:00
vervis/src/Yesod/Paginate/Local.hs
2016-05-11 14:42:41 +00:00

89 lines
2.7 KiB
Haskell

{- This file is part of Vervis.
-
- Written in 2016 by fr33domlover <fr33domlover@riseup.net>.
-
- ♡ 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
- <http://creativecommons.org/publicdomain/zero/1.0/>.
-}
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|
<a href=@{route}#{suffix}>#{label}
|]
in [whamlet|
<ul>
$if nmFirst nm
<li>
^{link 1 $ nwsFirst nws}
<li>#{nwsCurrent nws (nmCurrent nm) (nmTotal nm)}
$if nmLast nm
<li>
^{link (nmTotal nm) (nwsLast nws $ nmTotal nm)}
|]