module LoggedIn.Home.View.Paging ( paymentsPaging ) where import Html exposing (..) import Html.Attributes exposing (..) import Html.Events exposing (..) import LoggedIn.Action as LoggedInAction import LoggedIn.Home.Action as HomeAction import LoggedIn.Home.Model as HomeModel import Action exposing (Action) import Mailbox import LoggedData exposing (LoggedData) import Model.Payment as Payment exposing (Payments, perPage) import View.Icon exposing (renderIcon) showedPages : Int showedPages = 5 paymentsPaging : Payments -> HomeModel.Model -> Html paymentsPaging payments homeModel = let maxPage = ceiling (toFloat (List.length (Payment.punctual payments)) / toFloat perPage) pages = truncatePages homeModel.currentPage [1..maxPage] in if maxPage == 1 then text "" else div [ class "pages" ] ( [ firstPage homeModel, previousPage homeModel ] ++ ( List.map (paymentsPage homeModel) pages) ++ [ nextPage homeModel maxPage, lastPage homeModel maxPage ] ) 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 : HomeModel.Model -> Html firstPage homeModel = button [ classList [ ("page", True) , ("disable", homeModel.currentPage <= 1) ] , onClick Mailbox.address (Action.UpdateLoggedIn << LoggedInAction.HomeAction << HomeAction.UpdatePage <| 1) ] [ renderIcon "fast-backward" ] previousPage : HomeModel.Model -> Html previousPage homeModel = button [ class "page" , onClick Mailbox.address <| if homeModel.currentPage > 1 then (Action.UpdateLoggedIn << LoggedInAction.HomeAction << HomeAction.UpdatePage <| homeModel.currentPage - 1) else Action.NoOp ] [ renderIcon "backward" ] nextPage : HomeModel.Model -> Int -> Html nextPage homeModel maxPage = button [ class "page" , onClick Mailbox.address <| if homeModel.currentPage < maxPage then (Action.UpdateLoggedIn << LoggedInAction.HomeAction << HomeAction.UpdatePage <| homeModel.currentPage + 1) else Action.NoOp ] [ renderIcon "forward" ] lastPage : HomeModel.Model -> Int -> Html lastPage homeModel maxPage = button [ class "page" , onClick Mailbox.address (Action.UpdateLoggedIn << LoggedInAction.HomeAction << HomeAction.UpdatePage <| maxPage) ] [ renderIcon "fast-forward" ] paymentsPage : HomeModel.Model -> Int -> Html paymentsPage homeModel page = let onCurrentPage = page == homeModel.currentPage in button [ classList [ ("page", True) , ("current", onCurrentPage) ] , onClick Mailbox.address <| if onCurrentPage then Action.NoOp else Action.UpdateLoggedIn << LoggedInAction.HomeAction << HomeAction.UpdatePage <| page ] [ text (toString page) ]