-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtboard.elm
210 lines (157 loc) · 5.07 KB
/
tboard.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
module TBoard exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onClick, onInput)
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
type alias Model =
TBoard
type alias TBoard =
{ name : String, lists : List TList, increment : Int }
type alias TList =
{ id : Int, name : String, newCard : TCard, cards : List TCard, isEditing : Bool }
type alias TCard =
{ description : String }
init : ( Model, Cmd Msg )
init =
let
list1 =
TList 1 "Listardi" (TCard "") [ TCard "Cartita", TCard "Cartonga" ] False
list2 =
TList 2 "Listox" (TCard "") [ TCard "Cartucha" ] False
in
( TBoard "Boarding" [ list1, list2 ] 3, Cmd.none )
-- UPDATE
type Msg
= AddList
| DeleteList Int
| AddCard Int
| TypeCard Int String
| EditModeList Int
| TypeList Int String
update : Msg -> Model -> ( Model, Cmd Msg )
update msg board =
case msg of
EditModeList id ->
let
updateEntry list =
if list.id == id then
{ list | isEditing = not list.isEditing }
else
list
in
( { board | lists = List.map updateEntry board.lists }, Cmd.none )
TypeList id name ->
let
updateEntry list =
if list.id == id then
{ list | name = name }
else
list
in
( { board | lists = List.map updateEntry board.lists }, Cmd.none )
AddList ->
let
lists =
List.append board.lists [ TList board.increment "New List" (TCard "") [] False ]
in
( { board | lists = lists, increment = board.increment + 1 }, Cmd.none )
DeleteList id ->
( { board | lists = List.filter (\n -> n.id /= id) board.lists }, Cmd.none )
TypeCard id name ->
let
updateEntry list =
if list.id == id then
{ list | newCard = TCard name }
else
list
in
( { board | lists = List.map updateEntry board.lists }, Cmd.none )
AddCard id ->
let
updateEntry list =
if list.id == id then
{ list | cards = List.append list.cards [ list.newCard ], newCard = TCard "" }
else
list
in
( { board | lists = List.map updateEntry board.lists }, Cmd.none )
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
-- VIEW
view : TBoard -> Html Msg
view board =
div [ class "col-xs-12" ]
[ h3 [] [ text board.name ]
, hr [] []
, div [ class "row" ]
(List.map displayList board.lists
++ [ displayNewList ]
)
]
displayNewList : Html Msg
displayNewList =
displayBox
[ a [ onClick AddList ]
[ div [ class "panel-body" ]
[ h3 [ class "panel-title" ] [ text "Create new list..." ]
]
]
]
displayList : TList -> Html Msg
displayList list =
displayBox
[ displayTitle list
, displayCards list
, displayForm list
]
displayBox : List (Html Msg) -> Html Msg
displayBox list =
div [ class "col-xs-12 col-sm-3" ]
[ div [ class "panel panel-default" ]
list
]
displayTitle : TList -> Html Msg
displayTitle list =
div [ class "panel-body" ]
[ if list.isEditing then
div [ class "form-inline edit-title" ]
[ input [ onInput (TypeList list.id), value list.name, class "form-control" ] []
, button [ onClick (EditModeList list.id), class "btn btn-default btn-sm" ] [ text "OK" ]
]
else
h3 [ class "panel-title" ]
[ span [ onClick (EditModeList list.id) ] [ text list.name ]
, a [ class "delete-btn", onClick (DeleteList list.id) ]
[ span [ class "glyphicon glyphicon-remove" ] []
]
]
]
displayCard : TCard -> Html Msg
displayCard card =
li [ class "list-group-item" ] [ text card.description ]
displayCards : TList -> Html Msg
displayCards list =
ul [ class "list-group" ] (List.map displayCard list.cards)
displayForm : TList -> Html Msg
displayForm list =
div [ class "panel-footer" ]
[ div [ class "form-group" ]
[ textarea
[ onInput (TypeCard list.id)
, value list.newCard.description
, placeholder "card name"
, class "form-control"
]
[]
]
, button [ onClick (AddCard list.id), class "btn btn-default btn-sm" ] [ text "Create Card" ]
]