module View.LoggedIn.Paging ( paymentsPaging ) where import Html exposing (..) import Html.Attributes exposing (..) import Html.Events exposing (..) import Model.View.LoggedInView exposing (..) import Model.Payment exposing (perPage) import ServerCommunication as SC exposing (serverCommunications) import Update exposing (..) import Update.LoggedIn exposing (..) import View.Icon exposing (renderIcon) showedPages : Int showedPages = 5 paymentsPaging : LoggedInView -> Html paymentsPaging 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, previousPage loggedInView ] else [] ) ++ ( List.map (paymentsPage loggedInView) pages) ++ ( if loggedInView.currentPage < maxPage then [ nextPage loggedInView, lastPage 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 -> [1..showedPages] | currentPage > totalPages - showedRightPages -> [(totalPages - showedPages)..totalPages] | otherwise -> [(currentPage - showedLeftPages)..(currentPage + showedRightPages)] in List.filter (flip List.member pages) truncatedPages firstPage : Html firstPage = button [ class "page" , onClick serverCommunications.address (SC.UpdatePage 1) ] [ renderIcon "fast-backward" ] previousPage : LoggedInView -> Html previousPage loggedInView = button [ class "page" , onClick serverCommunications.address (SC.UpdatePage (loggedInView.currentPage - 1)) ] [ renderIcon "backward" ] nextPage : LoggedInView -> Html nextPage loggedInView = button [ class "page" , onClick serverCommunications.address (SC.UpdatePage (loggedInView.currentPage + 1)) ] [ renderIcon "forward" ] lastPage : Int -> Html lastPage maxPage = button [ class "page" , onClick serverCommunications.address (SC.UpdatePage maxPage) ] [ renderIcon "fast-forward" ] paymentsPage : LoggedInView -> Int -> Html paymentsPage loggedInView page = let onCurrentPage = page == loggedInView.currentPage in button [ classList [ ("page", True) , ("current", onCurrentPage) ] , onClick serverCommunications.address <| if onCurrentPage then SC.NoCommunication else SC.UpdatePage page ] [ text (toString page) ]