aboutsummaryrefslogtreecommitdiff
path: root/src/client/View/Payments/Paging.elm
blob: c3db819e97320a4d2793e74ec42df687404c2419 (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
module View.Payments.Paging
  ( paymentsPaging
  ) where

import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)

import Model.View.PaymentView exposing (..)
import Model.Payment exposing (perPage)

import ServerCommunication as SC exposing (serverCommunications)

import Update exposing (..)
import Update.Payment exposing (..)

import View.Icon exposing (renderIcon)

showedPages : Int
showedPages = 5

paymentsPaging : PaymentView -> Html
paymentsPaging paymentView =
  let maxPage = ceiling (toFloat paymentView.paymentsCount / toFloat perPage)
      pages = truncatePages paymentView.currentPage [1..maxPage]
  in  if maxPage == 1
        then
          text ""
        else
          ul
            [ class "pages" ]
            (  ( if paymentView.currentPage > 1
                   then [ firstPage, previousPage paymentView ]
                   else []
               )
            ++ ( List.map (paymentsPage paymentView) pages)
            ++ ( if paymentView.currentPage < maxPage
                   then [ nextPage paymentView, 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 =
  li
    [ class "page"
    , onClick serverCommunications.address (SC.UpdatePage 1)
    ]
    [ renderIcon "fast-backward" ]

previousPage : PaymentView -> Html
previousPage paymentView =
  li
    [ class "page"
    , onClick serverCommunications.address (SC.UpdatePage (paymentView.currentPage - 1))
    ]
    [ renderIcon "backward" ]

nextPage : PaymentView -> Html
nextPage paymentView =
  li
    [ class "page"
    , onClick serverCommunications.address (SC.UpdatePage (paymentView.currentPage + 1))
    ]
    [ renderIcon "forward" ]

lastPage : Int -> Html
lastPage maxPage =
  li
    [ class "page"
    , onClick serverCommunications.address (SC.UpdatePage maxPage)
    ]
    [ renderIcon "fast-forward" ]

paymentsPage : PaymentView -> Int -> Html
paymentsPage paymentView page =
  let onCurrentPage = page == paymentView.currentPage
  in  li
        [ class ("page" ++ (if onCurrentPage then " current" else ""))
        , onClick serverCommunications.address <|
            if onCurrentPage then SC.NoCommunication else SC.UpdatePage page
        ]
        [ text (toString page) ]