-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.hs
343 lines (327 loc) · 8.72 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
{-# LANGUAGE OverloadedStrings #-}
import Asterius.Types
import Web.Framework.Plzwrk.Asterius
import Control.Monad
import Data.HashMap.Strict hiding ( null )
import Data.IORef
import qualified Data.Set as S
import qualified Data.Text as DT
import Nouns
import Prelude hiding ( div
, span
)
import Web.Framework.Plzwrk
import Web.Framework.Plzwrk.Tag
hiding ( main
, main_
, main'_
)
import qualified Web.Framework.Plzwrk.Tag as T
( main
, main_
, main'_
)
data MyState = MyState
{ _name :: String
, _abstractToConcrete :: [(String, String)]
, _myNoun :: String
}
deriving Show
-- here is where we'll show our "surprise" aphorism
surprise =
(\noun -> if (length noun == 0)
then div'_ []
else p'__ $ concat ["Life is like", indefiniteArticle noun, noun]
)
<$> _myNoun
-- here is where we will input a noun for our "surprise" aphorosim
writeSomethingConcrete browser = input
[ ("type" , pT "text")
, ("style", pT "box-sizing:content-box")
, ( "input"
, pF
(\e s -> do
v <- (eventTargetValue browser) e
return $ maybe s (\q -> s { _myNoun = q }) v
)
)
]
[]
aphorismList =
(\a2c -> ul'
[("class", pT "res")]
(fmap (\(a, c) -> (li__ (concat [a, " is like", indefiniteArticle c, c])))
a2c
)
)
<$> _abstractToConcrete
addAphorismButton browser =
(\a2c -> button'
[ ("id" , pT "incr")
, ("class", pT "dim")
, ( "click"
, pF
(\e s -> do
(eventTargetBlur browser) e
(consoleLogS browser) $ "Here is the current state " <> show s
concept <- randAbstract (mathRandom browser)
comparedTo <- randConcrete (mathRandom browser)
let newS = s { _abstractToConcrete = (concept, comparedTo) : a2c }
(consoleLogS browser) $ "Here is the new state " <> show newS
return $ newS
)
)
]
[txt "More aphorisms"]
)
<$> _abstractToConcrete
removeAphorismButton browser =
(\a2c -> button'
[ ("id" , pT "decr")
, ("class", pT "dim")
, ( "click"
, pF
(\e s -> do
(eventTargetBlur browser) e
pure $ s { _abstractToConcrete = if null a2c then [] else tail a2c }
)
)
]
[txt "Less aphorisms"]
)
<$> _abstractToConcrete
loginText =
(\name ->
p'_ [txt "Logged in as: ", span [("class", pT "username")] [txt name]]
)
<$> _name
main :: IO ()
main = do
browser <- asteriusBrowser
-- add some css!
_head <- (documentHead browser)
_style <- (documentCreateElement browser) "style"
_css <- (documentCreateTextNode browser) (unwords myCss)
(nodeAppendChild browser) _style _css
(nodeAppendChild browser) _head _style
-- and here is our main div
let mainDivF = T.main_
[ section
[("class", pT "content")]
[ h1__ "Aphorism Machine"
, aphorismList
, br
, surprise
, div [("style", pT "width:100%;display:inline-block")]
[addAphorismButton browser, removeAphorismButton browser]
, writeSomethingConcrete browser
, loginText
]
]
let state = MyState "Bob" [] ""
plzwrk' mainDivF state browser
randFromList :: [String] -> IO Double -> IO String
randFromList l f = do
z <- f
let i = round $ fromIntegral (length l) * z
return $ l !! i
indefiniteArticle :: String -> String
indefiniteArticle x =
let hd = take 1 x
in if hd == "a" || hd == "e" || hd == "i" || hd == "o" || hd == "u"
then " an "
else " a "
randAbstract :: IO Double -> IO String
randAbstract = randFromList abstract
randConcrete :: IO Double -> IO String
randConcrete = randFromList concrete
myCss =
[ "body {\n"
, " margin: 0;\n"
, " font-family: -apple-system, BlinkMacSystemFont, \"Segoe UI\", \"Roboto\", \"Oxygen\", \"Ubuntu\", \"Cantarell\", \"Fira Sans\", \"Droid Sans\", \"Helvetica Neue\", sans-serif;\n"
, " text-rendering: optimizeLegibility;\n"
, " -webkit-font-smoothing: antialiased;\n"
, "}\n"
, "\n"
, "html,\n"
, "body {\n"
, " height: 100%;\n"
, "}\n"
, "\n"
, "body>div:first-child,\n"
, "body>div:first-child>div:first-child,\n"
, "body>div:first-child>div:first-child>div {\n"
, " height: inherit;\n"
, "}\n"
, "\n"
, "input {\n"
, " box-sizing: border-box;\n"
, " padding: 9.5px 15px;\n"
, " border: 0;\n"
, " text-align: center;\n"
, " border-bottom: 1px solid #d8d8d8;\n"
, " font-size: 14px;\n"
, " transition: border-bottom-color 100ms ease-in, color 100ms ease-in;\n"
, " max-width: 250px;\n"
, " border-radius: 0;\n"
, "}\n"
, "\n"
, "input:focus {\n"
, " outline: none;\n"
, " border-color: #000;\n"
, "}\n"
, "\n"
, ".dim {\n"
, " opacity: 1;\n"
, " transition: opacity .15s ease-in;\n"
, " cursor: pointer;\n"
, "}\n"
, ".dim:hover,\n"
, ".dim:focus {\n"
, " opacity: .5;\n"
, " transition: opacity .15s ease-in;\n"
, "}\n"
, ".dim:active {\n"
, " opacity: .8;\n"
, " transition: opacity .15s ease-out;\n"
, "}\n"
, "\n"
, "@media (min-width: 768px) {\n"
, " input {\n"
, " min-width: 300px;\n"
, " max-width: 620px;\n"
, " }\n"
, "}\n"
, "\n"
, "ul {\n"
, " list-style: none;\n"
, " padding-left: 0;\n"
, "}\n"
, "\n"
, "hr {\n"
, " margin-top: 15px;\n"
, " margin-bottom: 15px;\n"
, " width: 70%;\n"
, "}\n"
, "\n"
, "main {\n"
, " width: 100%;\n"
, " height: 100%;\n"
, " display: flex;\n"
, " justify-content: center;\n"
, " align-items: center;\n"
, " padding: 20px;\n"
, " box-sizing: border-box;\n"
, " flex-direction: column;\n"
, "}\n"
, "\n"
, ".content {\n"
, " text-align: center;\n"
, " max-width: 100%;\n"
, " -webkit-animation: fadein 2s;\n"
, " -moz-animation: fadein 2s;\n"
, " -ms-animation: fadein 2s;\n"
, " -o-animation: fadein 2s;\n"
, " animation: fadein 2s;\n"
, "}\n"
, "\n"
, "h1 {\n"
, " font-family: 'Montserrat', sans-serif;\n"
, " font-weight: normal;\n"
, " font-size: 32px;\n"
, " text-align: center;\n"
, " margin-bottom: 25px;\n"
, "}\n"
, "\n"
, "aside {\n"
, " display: flex;\n"
, " justify-content: center;\n"
, " align-items: center;\n"
, " padding: 50px 0 40px 0;\n"
, " position: absolute;\n"
, " bottom: 0;\n"
, " left: 0;\n"
, " right: 0;\n"
, "}\n"
, "\n"
, "aside nav {\n"
, " height: 18px;\n"
, " display: flex;\n"
, " justify-content: center;\n"
, " align-items: center;\n"
, "}\n"
, "\n"
, "aside nav a {\n"
, " font-size: 13px;\n"
, " color: #b2b2b2;\n"
, " text-decoration: none;\n"
, " transition: color 100ms ease-in;\n"
, "}\n"
, "\n"
, "aside nav b {\n"
, " display: block;\n"
, " background: #b2b2b2;\n"
, " width: 1px;\n"
, " height: 100%;\n"
, " margin: 0 10px;\n"
, "}\n"
, "\n"
, ".username {\n"
, " font-weight: 500;\n"
, "}\n"
, "\n"
, "p {\n"
, " font-weight: 400;\n"
, " font-size: 14px;\n"
, " line-height: 24px;\n"
, " max-width: 390px;\n"
, " text-align: center;\n"
, " margin: 14px auto 30px auto;\n"
, "}\n"
, "\n"
, "button {\n"
, " background-color: rgba(0, 0, 0, 0.671);\n"
, " border: none;\n"
, " color: white;\n"
, " padding: 10px 12px;\n"
, " margin: 10px;\n"
, " text-align: center;\n"
, " border-radius: 12px;\n"
, " text-decoration: none;\n"
, " display: inline-block;\n"
, " font-size: 14px;\n"
, " }\n"
, "\n"
, "@keyframes fadein {\n"
, " from {\n"
, " opacity: 0;\n"
, " }\n"
, " to {\n"
, " opacity: 1;\n"
, " }\n"
, "}\n"
, "\n"
, "@-moz-keyframes fadein {\n"
, " from {\n"
, " opacity: 0;\n"
, " }\n"
, " to {\n"
, " opacity: 1;\n"
, " }\n"
, "}\n"
, "\n"
, "@-webkit-keyframes fadein {\n"
, " from {\n"
, " opacity: 0;\n"
, " }\n"
, " to {\n"
, " opacity: 1;\n"
, " }\n"
, "}\n"
, "\n"
, "@media (max-height: 400px) {\n"
, " aside {\n"
, " display: none;\n"
, " }\n"
, "}\n"
]