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