From 0620d925b1045b17cad613a3cc5a1fbb3748c83c Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 27 Mar 2016 21:35:52 +0200 Subject: Moving some files --- src/client/elm/LoggedIn/View/Paging.elm | 100 ++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 src/client/elm/LoggedIn/View/Paging.elm (limited to 'src/client/elm/LoggedIn/View/Paging.elm') diff --git a/src/client/elm/LoggedIn/View/Paging.elm b/src/client/elm/LoggedIn/View/Paging.elm new file mode 100644 index 0000000..0a149e9 --- /dev/null +++ b/src/client/elm/LoggedIn/View/Paging.elm @@ -0,0 +1,100 @@ +module LoggedIn.View.Paging + ( paymentsPaging + ) where + +import Signal exposing (Address) + +import Html exposing (..) +import Html.Attributes exposing (..) +import Html.Events exposing (..) + +import LoggedIn.Action as LoggedInAction +import LoggedIn.Model as LoggedInModel + +import Action exposing (..) +import Model.Payment exposing (perPage) + +import View.Icon exposing (renderIcon) + +showedPages : Int +showedPages = 5 + +paymentsPaging : Address Action -> LoggedInModel.Model -> Html +paymentsPaging address loggedInModel = + let maxPage = ceiling (toFloat loggedInModel.paymentsCount / toFloat perPage) + pages = truncatePages loggedInModel.currentPage [1..maxPage] + in if maxPage == 1 + then + text "" + else + div + [ class "pages" ] + ( ( if loggedInModel.currentPage > 1 + then [ firstPage address, previousPage address loggedInModel ] + else [] + ) + ++ ( List.map (paymentsPage address loggedInModel) pages) + ++ ( if loggedInModel.currentPage < maxPage + then [ nextPage address loggedInModel, lastPage address maxPage ] + else [] + ) + ) + +truncatePages : Int -> List Int -> List Int +truncatePages currentPage pages = + let totalPages = List.length pages + showedLeftPages = ceiling ((toFloat showedPages - 1) / 2) + showedRightPages = floor ((toFloat showedPages - 1) / 2) + truncatedPages = + if currentPage < showedLeftPages then + [1..showedPages] + else if currentPage > totalPages - showedRightPages then + [(totalPages - showedPages)..totalPages] + else + [(currentPage - showedLeftPages)..(currentPage + showedRightPages)] + in List.filter (flip List.member pages) truncatedPages + +firstPage : Address Action -> Html +firstPage address = + button + [ class "page" + , onClick address (UpdateLoggedIn (LoggedInAction.UpdatePage 1)) + ] + [ renderIcon "fast-backward" ] + +previousPage : Address Action -> LoggedInModel.Model -> Html +previousPage address loggedInModel = + button + [ class "page" + , onClick address (UpdateLoggedIn (LoggedInAction.UpdatePage (loggedInModel.currentPage - 1))) + ] + [ renderIcon "backward" ] + +nextPage : Address Action -> LoggedInModel.Model -> Html +nextPage address loggedInModel = + button + [ class "page" + , onClick address (UpdateLoggedIn (LoggedInAction.UpdatePage (loggedInModel.currentPage + 1))) + ] + [ renderIcon "forward" ] + +lastPage : Address Action -> Int -> Html +lastPage address maxPage = + button + [ class "page" + , onClick address (UpdateLoggedIn (LoggedInAction.UpdatePage maxPage)) + ] + [ renderIcon "fast-forward" ] + +paymentsPage : Address Action -> LoggedInModel.Model -> Int -> Html +paymentsPage address loggedInModel page = + let onCurrentPage = page == loggedInModel.currentPage + in button + [ classList + [ ("page", True) + , ("current", onCurrentPage) + ] + , onClick address <| + if onCurrentPage then Action.NoOp else UpdateLoggedIn (LoggedInAction.UpdatePage page) + ] + [ text (toString page) ] -- cgit v1.2.3