@@ -43,32 +43,38 @@ defaultPaginateConfiguration = PaginateConfiguration
4343-- - @$index@: 1-based index of the current page.
4444-- - @$length@: Total number of pages.
4545--
46+-- When @$previous@ or @$next@ are not available, they will be just a label
47+-- without a link. The same goes for when we are on the first or last page for
48+-- @$first@ and @$last@.
4649paginate :: (Renderable a)
4750 => PaginateConfiguration
4851 -> [a]
4952 -> [CombinedRenderable a CustomPage]
5053paginate configuration renderables = paginate' Nothing renderables (1 :: Int)
5154 where
55+ -- Create a link with a given label, taken from the configuration.
5256 linkWithLabel f r = link (f configuration) `fmap` getUrl r
5357
54− first = linkWithLabel firstLabel (head renderables)
55− last' = linkWithLabel lastLabel (last renderables)
56− length' = length renderables
57−
58+ -- The main function that creates combined renderables by recursing over
59+ -- the list of renderables.
5860 paginate' _ [] _ = []
5961 paginate' maybePrev (x:xs) index =
60− let previous = case maybePrev of
61− (Just r) -> linkWithLabel previousLabel r
62− Nothing -> return $ previousLabel configuration
63− next = case xs of
64− (n:_) -> linkWithLabel nextLabel n
65− [] -> return $ nextLabel configuration
62+ let (previous, first) = case maybePrev of
63+ (Just r) -> ( linkWithLabel previousLabel r
64+ , linkWithLabel firstLabel (head renderables) )
65+ Nothing -> ( return $ previousLabel configuration
66+ , return $ firstLabel configuration )
67+ (next, last') = case xs of
68+ (n:_) -> ( linkWithLabel nextLabel n
69+ , linkWithLabel lastLabel (last renderables) )
70+ [] -> ( return $ nextLabel configuration
71+ , return $ lastLabel configuration )
6672 customPage = createCustomPage "" []
6773 [ ("previous", Right previous)
6874 , ("next", Right next)
6975 , ("first", Right first)
7076 , ("last", Right last')
7177 , ("index", Left $ show index)
72− , ("length", Left $ show length')
78+ , ("length", Left $ show $ length $ renderables)
7379 ]
7480 in (x `combine` customPage) : paginate' (Just x) xs (index + 1)