Eventually-decentralized project hosting and management platform
Clone
HTTPS:
darcs clone https://vervis.peers.community/repos/WvWbo
SSH:
darcs clone USERNAME@vervis.peers.community:WvWbo
Tags
TODO
Local.hs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | {- This file is part of Vervis.
-
- Written in 2016, 2019 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 Data.Paginate.Local
( -- * Settings
-- ** Jump settings
JumpSettings ()
, jumpMin
, jumpFactor
, jumpRound
-- ** Navigation settings
, NavSettings ()
, navEdges
, navJump
, navNext
-- ** Pagination settings
, PaginateSettings ()
, psSelect
, psCurrent
, psPer
-- * Results
-- ** Navigation controls
, NavModel ()
, nmFirst
, nmPrevJumps
, nmPrev
, nmCurrent
, nmTotal
, nmNext
, nmNextJumps
, nmLast
-- ** Paginate
, paginateMaybe
, paginateCount
, paginateTop
)
where
import Data.Default.Class
import Data.Maybe
import Data.Ratio
data JumpSettings = JumpSettings
{ -- | Minimal jump size to display. Smaller jumps will be discarded.
jumpMin :: Int
-- | Ratio of size of consecutive jumps.
, jumpFactor :: Ratio Int
-- | Round jump page numbers to be multiples of this number. 1 means no
-- rounding. 10 means all jumps will be to page numbers that are
-- multiples of 10. And so on.
, jumpRound :: Int
}
instance Default JumpSettings where
def = JumpSettings
{ jumpMin = 10
, jumpFactor = 2 % 3
, jumpRound = 10
}
data NavSettings = NavSettings
{ -- | Whether to always show links to first and last pages
navEdges :: Bool
-- | Whether and how to show jump links
, navJump :: Maybe JumpSettings
-- | Number of next\/prev page links to show on each side of the current
-- page.
, navNext :: Int
}
instance Default NavSettings where
def = NavSettings
{ navEdges = True
, navJump = Just def
, navNext = 3
}
data PaginateSettings m f i = PaginateSettings
{ -- | Get the total number of items being paginated, and given an offset
-- and a limit, get the specified subset of the items. The offset tells
-- you how many items to skip from the beginning of the list, and then
-- the limit says how many items you should take after skipping.
psSelect :: Int -> Int -> m (Int, f i)
-- | Get the current page
, psCurrent :: m (Maybe Int)
-- | How many items to list in one page
, psPer :: Int
}
instance Default (PaginateSettings m f i) where
def = PaginateSettings
{ psSelect = error "You didn't implement psSelect"
, psCurrent = error "You didn't implement psCurrent"
, psPer = 30
}
data NavModel = NavModel
{
nmFirst :: Bool
, nmPrevJumps :: [Int]
, nmPrev :: Maybe ([Int], Int)
, nmCurrent :: Int
, nmTotal :: Int
, nmNext :: Maybe (Int, [Int])
, nmNextJumps :: [Int]
, nmLast :: Bool
}
-- | Given the number of items per page and the current page number, determine
-- the offset and limit.
subseq :: Int -> Int -> (Int, Int)
subseq per curr =
let offset = (curr - 1) * per
limit = per
in (offset, limit)
navModel :: NavSettings -> Int -> Int -> NavModel
navModel ns curr total = NavModel
{ nmFirst = navEdges ns
, nmPrevJumps = [] --TODO
, nmPrev =
if curr == 1 || navNext ns < 1
then Nothing
else Just ([max 1 (curr - navNext ns) .. curr - 2], curr - 1)
, nmCurrent = curr
, nmTotal = total
, nmNext =
if curr >= total || navNext ns < 1
then Nothing
else Just (curr + 1, [curr + 2 .. min total (curr + navNext ns)])
, nmNextJumps = [] --TODO
, nmLast = navEdges ns
}
paginate
:: Monad m
=> PaginateSettings m f i
-> NavSettings
-> Int
-> m (Int, Int, f i, NavModel)
paginate ps ns curr = do
let (offset, limit) = subseq (psPer ps) curr
(total, items) <- psSelect ps offset limit
let pages =
let (d, m) = total `divMod` psPer ps
in if m == 0 then d else d + 1
return (total, pages, items, navModel ns curr pages)
-- | Get a page's contents and its navigation controls.
paginateMaybe
:: Monad m
=> PaginateSettings m f i
-- ^ How to get the page contents and split them into pages
-> NavSettings
-- ^ How to build page navigation controls for the user interface
-> m (Maybe (Int, Int, f i, NavModel))
-- ^ The items in the current page, and the navigation controls
paginateMaybe ps ns = do
mcurr <- psCurrent ps
traverse (paginate ps ns) mcurr
paginateCount
:: Monad m
=> PaginateSettings m f i
-> NavSettings
-> m Int
-> m (Int, Int, Maybe (f i, NavModel))
paginateCount ps ns count = do
mresult <- paginateMaybe ps ns
case mresult of
Nothing -> do
total <- count
let pages =
let (d, m) = total `divMod` psPer ps
in if m == 0 then d else d + 1
return (total, pages, Nothing)
Just (total, pages, items, nav) ->
return (total, pages, Just (items, nav))
paginateTop
:: Monad m
=> PaginateSettings m f i
-> NavSettings
-> m (Int, Int, f i, NavModel)
paginateTop ps ns = do
curr <- fromMaybe 1 <$> psCurrent ps
paginate ps ns curr
|