aboutsummaryrefslogtreecommitdiff
path: root/src/View/Timer.elm
blob: 100514aa70dd9259b4c5441b3af2c3f69db781dc (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
module View.Timer
  ( timerView
  ) where

import Html (..)
import Html.Attributes (..)
import Html.Events (..)
import Time (Time)
import Signal
import Maybe
import List
import String

import Model.Model (..)
import Model.Timer (..)
import Model.Edition.Edition (..)
import Model.Edition.NameEdition (..)
import Model.Edition.TimeEdition (..)
import Model.TimerState (..)
import Model.Id (..)

import Update.Update (..)
import Update.UpdateTimer (..)

import View.ActivatedClasses (..)

import Utils.Maybe (..)

timerView : Model -> (Id, Timer) -> Html
timerView model (id, timer) =
  div
    [ class "timer" ]
    [ renderMaybeEdition model id Name (nameBlockReadOnly id timer) (nameBlockEdition id timer)
    , renderMaybeEdition model id Time (timeBlockReadOnly id timer) (timeBlockEdition timer)
    , playPauseBlock (id, timer)
    , stopBlock (id, timer)
    , removeBlock (id, timer)
    ]

nameBlockReadOnly : Id -> Timer -> Html
nameBlockReadOnly id timer =
  button
    [ class "name block"
    , onClick (Signal.send updates (Edit id Name))
    ]
    [ text (timerName id timer) ]

nameBlockEdition : Id -> Timer -> Edition -> Html
nameBlockEdition id timer edition =
  button
    [ [ (True, "name block edition")
      , (List.isEmpty edition.chars, "empty")
      ]
        |> activatedClasses
    , onClick (Signal.send updates ValidEdition)
    ]
    [ if List.isEmpty edition.chars
        then
          text (timerName id timer)
        else
          edition.chars
            |> renderNameEdition
            |> text
    ]

timerName : Id -> Timer -> String
timerName id = Maybe.withDefault ("Timer " ++ toString id) << .name

timeBlockReadOnly : Id -> Timer -> Html
timeBlockReadOnly id timer =
  button
    [ [ (True, "time block")
      , (timer.state == Ringing, "isRinging")
      , (timer.state == Running, "isRunning")
      ]
        |> activatedClasses
    , onClick
        <| if timer.state == Ringing
             then Signal.send updates (UpdateTimer id Stop)
             else Signal.send updates (Edit id Time)
    ]
    [ timeWithProgressBar timer ]

timeBlockEdition : Timer -> Edition -> Html
timeBlockEdition timer edition =
  let isEmptyEdition = List.isEmpty edition.chars
  in  button
        [ [ (True, "time block edition")
          , (isEmptyEdition, "empty")
          ]
            |> activatedClasses
        , onClick (Signal.send updates ValidEdition)
        ]
        [ if isEmptyEdition
            then
              timeWithProgressBar timer
            else
              text (editionView edition.chars)
        ]

editionView : List Char -> String
editionView numbers =
  let (minutes, seconds) = toMinutesAndSeconds numbers
  in  minutes ++ " : " ++ seconds

timeView : Time -> String
timeView time =
  let totalSeconds = truncate (time / 1000)
      totalMinutes = totalSeconds // 60
      restSeconds = totalSeconds `rem` 60
  in  (String.padLeft 2 '0' (toString totalMinutes)) ++ " : " ++ (String.padLeft 2 '0' (toString restSeconds))

timeWithProgressBar : Timer -> Html
timeWithProgressBar timer =
  div
    []
    [ span
        [ class "progressBar"
        , let width =
                1 - timer.currentTime / (initTime timer.initialTime)
                  |> (*) 198
                  |> round
                  |> toString
                  |> flip String.append "px"
          in style [ ("width", width) ]
        ]
        []
    , span
        [ class "text" ]
        [ text (timeView timer.currentTime) ]
    ]

playPauseBlock : (Id, Timer) -> Html
playPauseBlock (id, timer) =
  button
    [ class <| "playPause block"
    , onClick (Signal.send updates (UpdateTimer id ToggleRunning))
    ]
    [ let icon = if timer.state == Running then "fa-pause" else "fa-play"
      in  i
            [ class <| "fa fa-fw " ++ icon ]
            []
    ]

stopBlock : (Id, Timer) -> Html
stopBlock (id, timer) =
  button
    [ class <| "stop block"
    , onClick (Signal.send updates (UpdateTimer id Stop))
    ]
    [ i [ class "fa fa-fw fa-stop" ] [] ]

removeBlock : (Id, Timer) -> Html
removeBlock (id, timer) =
  button
    [ class <| "remove block"
    , onClick (Signal.send updates (RemoveTimer id))
    ]
    [ i [ class "fa fa-fw fa-remove" ] [] ]

renderMaybeEdition : Model -> Id -> Kind -> Html -> (Edition -> Html) -> Html
renderMaybeEdition model id kind readOnlyView editionView =
  let maybeEdition = filterMaybe (\edition -> edition.id == id) model.edition
  in  case maybeEdition of
        Just edition ->
          if edition.kind == kind
            then
              editionView edition
            else
              readOnlyView
        Nothing ->
          readOnlyView