-
-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathMain.hs
328 lines (293 loc) · 8.52 KB
/
Main.hs
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE ExtendedDefaultRules #-}
{-# LANGUAGE CPP #-}
module Main where
import Data.Aeson hiding (Object)
import Data.Bool
import qualified Data.Map as M
import Data.Monoid
import GHC.Generics
import Miso
import Miso.String (MisoString)
import qualified Miso.String as S
import Control.Monad.IO.Class
#ifdef IOS
import Language.Javascript.JSaddle.WKWebView as JSaddle
runApp :: JSM () -> IO ()
runApp = JSaddle.run
#else
import Language.Javascript.JSaddle.Warp as JSaddle
runApp :: JSM () -> IO ()
runApp = JSaddle.run 8080
#endif
default (MisoString)
data Model = Model
{ entries :: [Entry]
, field :: MisoString
, uid :: Int
, visibility :: MisoString
, step :: Bool
} deriving (Show, Generic, Eq)
data Entry = Entry
{ description :: MisoString
, completed :: Bool
, editing :: Bool
, eid :: Int
, focussed :: Bool
} deriving (Show, Generic, Eq)
instance ToJSON Entry
instance ToJSON Model
instance FromJSON Entry
instance FromJSON Model
emptyModel :: Model
emptyModel = Model
{ entries = []
, visibility = "All"
, field = mempty
, uid = 0
, step = False
}
newEntry :: MisoString -> Int -> Entry
newEntry desc eid = Entry
{ description = desc
, completed = False
, editing = False
, eid = eid
, focussed = False
}
data Msg
= NoOp
| CurrentTime Int
| UpdateField MisoString
| EditingEntry Int Bool
| UpdateEntry Int MisoString
| Add
| Delete Int
| DeleteComplete
| Check Int Bool
| CheckAll Bool
| ChangeVisibility MisoString
deriving Show
main :: IO ()
main = runApp $ startApp App { initialAction = NoOp, ..}
where
model = emptyModel
update = updateModel
view = viewModel
events = defaultEvents
mountPoint = Nothing
subs = []
logLevel = Off
updateModel :: Msg -> Model -> Effect Msg Model
updateModel NoOp m = noEff m
updateModel (CurrentTime n) m =
m <# do liftIO (print n) >> pure NoOp
updateModel Add model@Model{..} =
noEff model {
uid = uid + 1
, field = mempty
, entries = entries <> [ newEntry field uid | not $ S.null field ]
}
updateModel (UpdateField str) model = noEff model { field = str }
updateModel (EditingEntry id' isEditing) model@Model{..} =
model { entries = newEntries } <# do
focus $ S.pack $ "todo-" ++ show id'
pure NoOp
where
newEntries = filterMap entries (\t -> eid t == id') $
\t -> t { editing = isEditing, focussed = isEditing }
updateModel (UpdateEntry id' task) model@Model{..} =
noEff model { entries = newEntries }
where
newEntries =
filterMap entries ((==id') . eid) $ \t ->
t { description = task }
updateModel (Delete id') model@Model{..} =
noEff model { entries = filter (\t -> eid t /= id') entries }
updateModel DeleteComplete model@Model{..} =
noEff model { entries = filter (not . completed) entries }
updateModel (Check id' isCompleted) model@Model{..} =
model { entries = newEntries } <# eff
where
eff =
liftIO (putStrLn "clicked check") >>
pure NoOp
newEntries =
filterMap entries (\t -> eid t == id') $ \t ->
t { completed = isCompleted }
updateModel (CheckAll isCompleted) model@Model{..} =
noEff model { entries = newEntries }
where
newEntries =
filterMap entries (const True) $
\t -> t { completed = isCompleted }
updateModel (ChangeVisibility v) model =
noEff model { visibility = v }
filterMap :: [a] -> (a -> Bool) -> (a -> a) -> [a]
filterMap xs predicate f = go' xs
where
go' [] = []
go' (y:ys)
| predicate y = f y : go' ys
| otherwise = y : go' ys
viewModel :: Model -> View Msg
viewModel m@Model{..} =
div_
[ class_ "todomvc-wrapper"
]
[ section_
[ class_ "todoapp" ]
[ viewInput m field
, viewEntries visibility entries
, viewControls m visibility entries
]
, infoFooter
]
viewEntries :: MisoString -> [ Entry ] -> View Msg
viewEntries visibility entries =
section_
[ class_ "main"
, style_ $ M.singleton "visibility" cssVisibility
]
[ input_
[ class_ "toggle-all"
, type_ "checkbox"
, name_ "toggle"
, id_ "toggle-all"
, checked_ allCompleted
, onClick $ CheckAll (not allCompleted)
]
, label_
[ for_ "toggle-all" ]
[ text $ S.pack "Mark all as complete" ]
, ul_ [ class_ "todo-list" ] $
flip map (filter isVisible entries) $ \t ->
viewKeyedEntry t
]
where
cssVisibility = bool "visible" "hidden" (null entries)
allCompleted = all (==True) $ completed <$> entries
isVisible Entry {..} =
case visibility of
"Completed" -> completed
"Active" -> not completed
_ -> True
viewKeyedEntry :: Entry -> View Msg
viewKeyedEntry = viewEntry
viewEntry :: Entry -> View Msg
viewEntry Entry {..} = liKeyed_ (toKey eid)
[ class_ $ S.intercalate " " $
[ "completed" | completed ] <> [ "editing" | editing ]
]
[ div_
[ class_ "view" ]
[ input_
[ class_ "toggle"
, type_ "checkbox"
, checked_ completed
, onClick $ Check eid (not completed)
]
, label_
[ onDoubleClick $ EditingEntry eid True ]
[ text description ]
, button_
[ class_ "destroy"
, onClick $ Delete eid
] []
]
, input_
[ class_ "edit"
, value_ description
, name_ "title"
, id_ $ "todo-" <> S.ms eid
, onInput $ UpdateEntry eid
, onBlur $ EditingEntry eid False
, onEnter $ EditingEntry eid False
]
]
viewControls :: Model -> MisoString -> [ Entry ] -> View Msg
viewControls model visibility entries =
footer_ [ class_ "footer"
, hidden_ (null entries)
]
[ viewControlsCount entriesLeft
, viewControlsFilters visibility
, viewControlsClear model entriesCompleted
]
where
entriesCompleted = length . filter completed $ entries
entriesLeft = length entries - entriesCompleted
viewControlsCount :: Int -> View Msg
viewControlsCount entriesLeft =
span_ [ class_ "todo-count" ]
[ strong_ [] [ text $ S.ms entriesLeft ]
, text (item_ <> " left")
]
where
item_ = S.pack $ bool " items" " item" (entriesLeft == 1)
viewControlsFilters :: MisoString -> View Msg
viewControlsFilters visibility =
ul_
[ class_ "filters" ]
[ visibilitySwap "#/" "All" visibility
, text " "
, visibilitySwap "#/active" "Active" visibility
, text " "
, visibilitySwap "#/completed" "Completed" visibility
]
visibilitySwap :: MisoString -> MisoString -> MisoString -> View Msg
visibilitySwap uri visibility actualVisibility =
li_ [ ]
[ a_ [ href_ uri
, class_ $ S.concat [ "selected" | visibility == actualVisibility ]
, onClick (ChangeVisibility visibility)
] [ text visibility ]
]
viewControlsClear :: Model -> Int -> View Msg
viewControlsClear _ entriesCompleted =
button_
[ class_ "clear-completed"
, prop "hidden" (entriesCompleted == 0)
, onClick DeleteComplete
]
[ text $ "Clear completed (" <> S.ms entriesCompleted <> ")" ]
viewInput :: Model -> MisoString -> View Msg
viewInput _ task =
header_ [ class_ "header" ]
[ h1_ [] [ text "todos" ]
, input_
[ class_ "new-todo"
, placeholder_ "What needs to be done?"
, autofocus_ True
, value_ task
, name_ "newTodo"
, onInput UpdateField
, onEnter Add
]
]
onEnter :: Msg -> Attribute Msg
onEnter action =
onKeyDown $ bool NoOp action . (== KeyCode 13)
infoFooter :: View Msg
infoFooter =
footer_ [ class_ "info" ]
[ p_ [] [ text "Double-click to edit a todo" ]
, p_ []
[ text "Written by "
, a_ [ href_ "https://github.com/dmjio" ] [ text "David Johnson" ]
]
, p_ []
[ text "Part of "
, a_ [ href_ "http://todomvc.com" ] [ text "TodoMVC" ]
]
]