@@ -43,32 +43,38 @@ defaultPaginateConfiguration = PaginateConfiguration
43-- - @$index@: 1-based index of the current page.
43-- - @$index@: 1-based index of the current page.
44-- - @$length@: Total number of pages.
44-- - @$length@: Total number of pages.
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@.
46paginate :: (Renderable a)
49paginate :: (Renderable a)
47 => PaginateConfiguration
50 => PaginateConfiguration
49 -> [CombinedRenderable a CustomPage]
52 -> [CombinedRenderable a CustomPage]
50paginate configuration renderables = paginate' Nothing renderables (1 :: Int)
53paginate configuration renderables = paginate' Nothing renderables (1 :: Int)
55+ -- Create a link with a given label, taken from the configuration.
52 linkWithLabel f r = link (f configuration) `fmap` getUrl r
56 linkWithLabel f r = link (f configuration) `fmap` getUrl r
54− first = linkWithLabel firstLabel (head renderables)
58+ -- The main function that creates combined renderables by recursing over
55− last' = linkWithLabel lastLabel (last renderables)
56− length' = length renderables
59+ -- the list of renderables.
58 paginate' _ [] _ = []
60 paginate' _ [] _ = []
59 paginate' maybePrev (x:xs) index =
61 paginate' maybePrev (x:xs) index =
60− let previous = case maybePrev of
62+ let (previous, first) = case maybePrev of
61− (Just r) -> linkWithLabel previousLabel r
63+ (Just r) -> ( linkWithLabel previousLabel r
64+ , linkWithLabel firstLabel (head renderables) )
62− Nothing -> return $ previousLabel configuration
65+ Nothing -> ( return $ previousLabel configuration
66+ , return $ firstLabel configuration )
63− next = case xs of
67+ (next, last') = case xs of
64− (n:_) -> linkWithLabel nextLabel n
68+ (n:_) -> ( linkWithLabel nextLabel n
69+ , linkWithLabel lastLabel (last renderables) )
65− [] -> return $ nextLabel configuration
70+ [] -> ( return $ nextLabel configuration
71+ , return $ lastLabel configuration )
66 customPage = createCustomPage "" []
72 customPage = createCustomPage "" []
67 [ ("previous", Right previous)
73 [ ("previous", Right previous)
68 , ("next", Right next)
74 , ("next", Right next)
69 , ("first", Right first)
75 , ("first", Right first)
70 , ("last", Right last')
76 , ("last", Right last')
71 , ("index", Left $ show index)
77 , ("index", Left $ show index)
72− , ("length", Left $ show length')
78+ , ("length", Left $ show $ length $ renderables)
74 in (x `combine` customPage) : paginate' (Just x) xs (index + 1)
80 in (x `combine` customPage) : paginate' (Just x) xs (index + 1)