-
Notifications
You must be signed in to change notification settings - Fork 1
/
ManualTimerCluster.elm
81 lines (65 loc) · 2.51 KB
/
ManualTimerCluster.elm
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
module Component.ManualTimerCluster exposing (Msg (..), Model, init, update, view)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html as Html
import Html.Events exposing ( onClick )
import Dict exposing (Dict)
import Updater exposing (converter, Updater, Converter, Interface, toCmd, noReaction)
import Component.TaskTimer as TaskTimer
type alias Model = { timers : Dict Int TaskTimer.Model
, newID : Int }
type Msg = AddTimer (TaskTimer.Model, Cmd TaskTimer.Msg)
| DeleteTimer Int
| UpdaterMsg (Updater Model Msg)
timerC : Int -> Converter Msg TaskTimer.Msg
timerC n = converter
UpdaterMsg
{ get = (\ model -> Dict.get n model.timers)
, set = (\ timer model -> { model | timers = Dict.insert n timer model.timers } )
, update = TaskTimer.update
, react = noReaction }
--
init : (Model, Cmd Msg)
init = { timers = Dict.empty
, newID = 0 }
! [ ]
-- UPDATE
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
AddTimer (timerModel, timerCmd) ->
{ model | timers = Dict.insert model.newID timerModel model.timers
, newID = model.newID + 1 }
! [ Cmd.map (timerC model.newID) <| timerCmd ]
DeleteTimer id -> { model | timers = Dict.remove id model.timers } ! []
UpdaterMsg u -> u model
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch <| List.map (\ (id, timerModel) ->
Sub.map (timerC id) <| TaskTimer.subscriptions timerModel )
(Dict.toList model.timers)
-- VIEW
view : Model -> Html Msg
view model =
div [ ]
[ div [ style [("height", "420px")]] <|
List.map (\ (id, timerModel) ->
deletableTimer id <|
Html.map (timerC id) <| TaskTimer.view timerModel)
(Dict.toList model.timers)
, div [] [ button [ onClick <| AddTimer TaskTimer.init ] [ text "Add Timer" ] ]
]
deletableTimer : Int -> Html Msg -> Html Msg
deletableTimer id html = div [ style [ ("width", "215px")
, ("float", "left")
, ("height", "320px") ] ]
[ html
, button [ onClick <| DeleteTimer id ] [ text "Delete" ]
]
-- APP
main : Program Never Model Msg
main =
Html.program { init = init
, update = update
, subscriptions = subscriptions
, view = view }