aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/View/LoggedIn/Paging.elm
blob: b722ee7add804979d8e0a96f04333b5b8aa71d2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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) ]