-
Notifications
You must be signed in to change notification settings - Fork 0
/
elmTest - Figured out recursive list def & updating.elm
255 lines (213 loc) · 5.19 KB
/
elmTest - Figured out recursive list def & updating.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import Html exposing (..)
import Html.App as App
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Json.Decode as Json
import String
main =
App.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
-- test material
--type alias Todo =
-- {id:Int}
--{id : Int, children : List Todo }
--testModel : Todo
--testModel =
--{
-- id = 0
--}
type alias Todo =
{ id: Int
, description: String
, children: TodoChildren
}
type TodoChildren = TodoChildren (List Todo)
--Create a new Todo Entry
newEntry: String -> Int -> Todo
newEntry str newId =
{
id = newId
, description = str
, children = (TodoChildren [])
}
-- type TodoList = TodoList (List Todo)
--type List Todo = Empty | Node Todo (List Todo)
type alias Model =
{ entries : TodoChildren--List Todo
, field: String
, uid : Int
}
emptyModel : Model
emptyModel =
{
entries = (TodoChildren [])
, field = ""
, uid = 0
}
init : (Model, Cmd Msg)
init =
( emptyModel, Cmd.none)
-- UPDATE
type Msg
= NoOp
| Add
| UpdateTodo Int String
| UpdateField String
| AddChildTodo Todo
-- | NewFace Int
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
NoOp ->
model ! []
Add ->
(addTodo model) ! [] --Model "new" 1)
UpdateTodo id newDescription ->
((updateTodoModelDesc model id newDescription), Cmd.none )
-- {model | entries = updateTodochild } ! []
-- let
-- updateTodo t =
-- if t.id == id then {t | description = newDescription} else t
-- in
-- ({model | entries = List.map updateTodo model.entries }) ! []
UpdateField str ->
{ model | field = str } ! []
AddChildTodo todo ->
model ! []
-- let
-- addNewChildTodo str parentTodo =
-- model.uid++ parentTodo.children ++ [newEntry str model.uid]
-- in
-- addNewChildTodo model.field todo
-- {model.entries Todo.id | name = str}
-- ! []
-- (model, Random.generate NewFace (Random.int 1 6))
--NewFace newFace ->
-- (Model newFace, Cmd.none)
updateTodoModelDesc : Model -> Int -> String -> Model
updateTodoModelDesc model id desc =
{
model
| entries = (updateTodochildDesc model.entries id desc)
}
updateTodochildDesc : TodoChildren -> Int -> String -> TodoChildren
updateTodochildDesc (TodoChildren todoChild) todoId newDesc =
let
updateTodoDesc todo =
if todo.id == todoId then {todo | description = newDesc} else todo
in
TodoChildren (List.map updateTodoDesc todoChild)
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
-- Functions
-- getNextTodoId : Model -> Int
-- getNextTodoId model =
-- (model.uid = model.uid + 1)
addTodo : Model -> Model
addTodo model =
{ model
| uid = model.uid + 1
, field = ""
, entries =
if String.isEmpty model.field then
model.entries
else
--model.entries ++ [newEntry model.field model.uid]
addTodoChild model.entries model.field model.uid
}
addTodoChild : TodoChildren -> String -> Int -> TodoChildren
addTodoChild (TodoChildren todoChild) newTodoText newTodoVal =
(TodoChildren (todoChild ++ [{children = (TodoChildren []), description = newTodoText, id = newTodoVal}]))
--Implement onEnter keyboard function
onEnter : Msg -> Attribute Msg
onEnter msg =
let
tagger code =
if code == 13 then msg else NoOp
in
on "keydown" (Json.map tagger keyCode)
-- VIEW FUNCTIONS
--show the root view
showRootView : String -> Html Msg
showRootView str =
div[]
[
h1 [] [ text "Welcome to Todolist" ]
, h2 [] [ text "Below are the list of todos" ]
, div [] [text str]
, input
[
placeholder "Enter the name of a new Todo"
, value str
, autofocus True
, onInput UpdateField
, onEnter Add
]
[]
, button [onClick Add] [text "Add New Todo"]
]
--Display a list of todos
displayTodoList : TodoChildren -> Html Msg
displayTodoList (TodoChildren todoList) =
div[]
(List.map displaySingleTodo todoList)
--Style
myStyle : Attribute Msg
myStyle =
style[
("width", "100%")
]
--Display single todo
displaySingleTodo : Todo -> Html Msg
displaySingleTodo todo =
div[]
[
input[myStyle
-- , onTodoEnter (AddChildTodo todo)
, onInput (UpdateTodo todo.id)
, value todo.description
]
[
-- text todo.description
]
, div [ style [("margin-left", "15px;")] ]
[
displayTodoList todo.children
]
]
-- displayChildTodoList : TodoList -> Html Msg
-- displayChildTodoList todoList =
-- div[]
-- (List.map displaySingleTodo todoList)
--Display single todo
-- displaySingleTodo : Todo -> Html Msg
-- displaySingleTodo todo =
-- div[]
-- [
-- input[myStyle
-- -- , onTodoEnter (AddChildTodo todo)
-- , onInput (UpdateTodo todo.id)
-- , value todo.description
-- ]
-- [
-- -- text todo.description
-- ]
-- , div [ style ("margin-left", "15px;") ]
-- [
-- displayChildTodoList todo.children
-- ]
-- ]
-- VIEW
view : Model -> Html Msg
view model =
div []
[ showRootView model.field
, displayTodoList model.entries
]