Skip to content

Commit

Permalink
Fix showing ellipsis on small paging results
Browse files Browse the repository at this point in the history
  • Loading branch information
LeviButcher committed Apr 17, 2022
1 parent 5d309ec commit 99b7e97
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
12 changes: 5 additions & 7 deletions src/Distribution/Server/Pages/Recent.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import Data.Time.Clock ( UTCTime )
import Data.Time.Format ( defaultTimeLocale, formatTime )
import Data.Maybe ( listToMaybe, fromMaybe)
import Distribution.Server.Util.Paging (PaginatedConfiguration(..), hasNext,
hasPrev, nextURL, pageIndexRange, paginate, prevURL, toURL, allPagedURLs)
hasPrev, nextURL, pageIndexRange, paginate, prevURL, toURL, allPagedURLs, pagingInfo)

-- | Takes a list of package info, in reverse order by timestamp.

Expand Down Expand Up @@ -67,11 +67,9 @@ pageSizeForm base =


paginator :: PaginatedConfiguration -> URL -> Html
paginator pc@PaginatedConfiguration{currPage,totalAmount} baseUrl =
paginator pc@PaginatedConfiguration{currPage} baseUrl =
let
(start, end) = pageIndexRange pc
infoText = "Showing " ++ show start ++ " to " ++ show end ++ " of " ++ show totalAmount ++ " entries"
info = XHtml.thediv << infoText
info = XHtml.thediv << pagingInfo pc

next = XHtml.anchor ! [XHtml.href (fromMaybe "" (nextURL baseUrl pc)) | hasNext pc] << "Next"
prev = XHtml.anchor ! [XHtml.href (fromMaybe "" (prevURL baseUrl pc)) | hasPrev pc] << "Previous"
Expand All @@ -93,9 +91,9 @@ noAttr = XHtml.theclass ""
-- | Generates a list of links of the current possible paging links, recreates the functionality of the paging links on the search page
reducePagedLinks :: PaginatedConfiguration -> [Html] -> Html
reducePagedLinks PaginatedConfiguration{currPage} xs
| currPage >= (length xs - 3) = mconcat . keepLastPages .fillFirst $ xs -- Beginning ellipses
| length xs > 5 && currPage < 5 = mconcat . keepFirstPages . fillLast $ xs -- Ending ellipses
| length xs <= 5 = mconcat xs -- Do Nothing
| currPage >= (length xs - 3) = mconcat . keepLastPages .fillFirst $ xs -- Beginning ellipses
| currPage < 5 = mconcat . keepFirstPages . fillLast $ xs -- Ending ellipses
| otherwise = mconcat . keepMiddlePages . fillLast . fillFirst $ xs -- Begin and End ellipses
where filler = XHtml.thespan << "..."
fillFirst x = insertAt 1 filler x
Expand Down
13 changes: 11 additions & 2 deletions src/Distribution/Server/Util/Paging.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ module Distribution.Server.Util.Paging
nextURL,
prevURL,
toURL,
pagingInfo,
PaginatedConfiguration(..),
)
where
import Text.XHtml (URL)
import Data.List (genericTake, genericDrop, genericLength)

-- This could be better designed, perhaps turning PaginatedConfiguration into a function that returns the paging info
-- and the paged data
-- This could be better designed, perhaps turning PaginatedConfiguration into a
-- function that returns the paging info and the paged data
data PaginatedConfiguration = PaginatedConfiguration
{ currPage :: Int,
pageSize :: Int,
Expand Down Expand Up @@ -78,3 +79,11 @@ prevURL base conf@PaginatedConfiguration {currPage}
| page < 1 = Nothing
| otherwise = Just $ toURL base conf{currPage=page}
where page = pred currPage


pagingInfo :: PaginatedConfiguration -> String
pagingInfo pc@PaginatedConfiguration{totalAmount} = "Showing " ++ show start ++ " to "
++ show end ++ " of " ++ show totalAmount ++ endingText
where (start, end) = pageIndexRange pc
endingText = if pageAmount > 0 then " entries" else " entry"
pageAmount = end - start -- Starts Indexing at 1

0 comments on commit 99b7e97

Please sign in to comment.