{-# LANGUAGE QuasiQuotes, TemplateHaskell, CPP, TypeFamilies, FlexibleInstances, MultiParamTypeClasses, FlexibleContexts #-}-- | Provides a pagination subsite for Yesod.---- Route layout:---- > / PaginateHomeR GET-- > /#Int PaginateStartR GET-- > /#Int/#Int PaginateR GET---- If no numbers are given, display the items starting from the beginning with the default count. If one number is given,-- display the items starting from that index with the default count. If two numbers are given, the first one is the count-- and the second is the starting index. (This is so the user can modify the end of the URL to move forward in the pagination.)moduleYesod.Paginate(Paginate(..),PaginateRoute(..),defaultPaginate,prevLink,nextLink)whereimportYesodimportControl.ApplicativeimportLanguage.Haskell.TH.SyntaxdataPaginatemasterrepa=Paginate{pgnDefaultCount::Int-- ^ How many items to show per page by default,pgnGetItems::Int->Int->GHandler(Paginatemasterrepa)master[a]-- ^ Get a certain count of items at a certain offset,pgnItemCount::GHandler(Paginatemasterrepa)masterInt-- ^ How many items there are in all,pgnDisplayItems::Int->Int->[a]->GHandler(Paginatemasterrepa)masterrep-- ^ Render the items on a page given the count and offset}mkYesodSub"Paginate master rep a"[ClassP''Yesod[VarT$mkName"master"],ClassP''HasReps[VarT$mkName"rep"]]#if GHC7[parseRoutes|#else[$parseRoutes|#endif/PaginateHomeRGET/#IntPaginateStartRGET/#Int/#IntPaginateRGET|]getPaginateHomeR::GHandler(Paginatemasterrepa)masterrepgetPaginateHomeR=dopgn<-getYesodSubtoMaster<-getRouteToMasterredirectRedirectSeeOther(toMaster(PaginateR(pgnDefaultCountpgn)0))getPaginateStartR::Int->GHandler(Paginatemasterrepa)masterrepgetPaginateStartRstart=dopgn<-getYesodSubtoMaster<-getRouteToMasterredirectRedirectSeeOther(toMaster(PaginateR(pgnDefaultCountpgn)start))getPaginateR::Int->Int->GHandler(Paginatemasterrepa)masterrepgetPaginateRhowmanystart=dopgn<-getYesodSubxs<-pgnGetItemspgnhowmanystartpgnDisplayItemspgnhowmanystartxsdefaultPaginate::(YesodPersistmaster,PersistBackend(YesodDBmaster(GHandler(Paginatemasterrepa)master)),PersistEntitya)=>Int-- ^ Default number of items to show->[Filtera]-- ^ Filters to apply->[Ordera]-- ^ Ordering to apply->(Int->Int->[a]->GHandler(Paginatemasterrepa)masterrep)-- ^ Display function->PaginatemasterrepadefaultPaginatexfsosd=Paginate{pgnDefaultCount=x,pgnGetItems=\yz->mapsnd<$>runDB(selectListfsosyz),pgnItemCount=runDB(countfs),pgnDisplayItems=d}-- | Link to the previous page.prevLink::Paginatemasterrepa->Int->Int->Maybe(Route(Paginatemasterrepa))prevLinkphowmanystart|start>0=Just(PaginateRhowmany(max0(start-howmany)))|otherwise=Nothing-- | Link to the next page.nextLink::Paginatemasterrepa->Int->Int->GHandler(Paginatemasterrepa)master(Maybe(Route(Paginatemasterrepa)))nextLinkphowmanystart=go<$>pgnItemCountpwheregol|start<l-howmany-1=Just(PaginateRhowmany(start+howmany))|otherwise=Nothing