-
Notifications
You must be signed in to change notification settings - Fork 0
/
RenderDemo.elm
77 lines (61 loc) · 1.46 KB
/
RenderDemo.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
port module RenderDemo exposing (..)
import Task
import Update.Extra as Update
import Html exposing (..)
import Html.App as App
import Html.Events exposing (..)
import Html.Attributes exposing (..)
type alias CellID = Int
type Msg
= Push
| Pop
| OnCellAdded CellID
type alias Model =
{ cells : List CellID
}
init : (Model, Cmd Msg)
init =
({ cells = []
}, Cmd.none)
update : Msg -> Model -> (Model, Cmd Msg)
update message ({cells} as model) =
case message of
Push ->
let
uid = List.length cells
in
({ model
| cells = [uid] ++ cells
}, Cmd.none)
|> Update.andThen update (OnCellAdded uid)
Pop ->
case cells of
[] -> (model, Cmd.none)
_ :: cells -> ({ model | cells = cells }, Cmd.none)
OnCellAdded counter ->
(model, onCellAdded counter)
port onCellAdded : CellID -> Cmd msg
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch []
view : Model -> Html Msg
view model =
let
remove =
button [ onClick Pop ] [ text "Pop" ]
insert =
button [ onClick Push ] [ text "Push" ]
cells =
List.map viewCell (List.reverse model.cells)
in
div [] ([remove, insert] ++ cells)
viewCell : CellID -> Html Msg
viewCell cellID =
div [id ("cell:" ++ toString cellID)] [ text ("Here I am: " ++ toString cellID) ]
main =
App.program
{ init = init
, update = update
, view = view
, subscriptions = subscriptions
}