-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterface.elm
More file actions
355 lines (320 loc) · 10.4 KB
/
Interface.elm
File metadata and controls
355 lines (320 loc) · 10.4 KB
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
344
345
346
347
348
349
350
351
352
353
354
355
module Interface where
import Html exposing (Html, Attribute)
import Html.Attributes as Attr
import Html.Events as Events
import Signal exposing (Mailbox, mailbox)
import Time
import Result as R
import Json.Decode as Decode
import Graphics.Element as GE
import Graphics.Collage
import Expression exposing (..)
import ExpParser as Parser
import Eval as E
import Examples exposing (examples)
import Graphic as G
-- CSS classes here ---
basicStyle =
[ ("font-size","12pt")
, ("font-family", "monospace")
, ("white-space","pre")
]
containerStyle : Attribute
containerStyle =
Attr.style <|
basicStyle ++
[ ("position","relative")
, ("top", "50pt")
, ("left","100pt")
, ("width","800pt")
--, ("height", "100%")
]
inputStyle : Attribute
inputStyle =
Attr.style <|
basicStyle ++
[ ("position","relative")
, ("height","50pt")
, ("width", "300pt")
--, ("left", "0pt")
, ("border", "3pt")
, ("border-style", "solid")
, ("border-color","blue")
, ("background-color","white")
]
outputStyle : Attribute
outputStyle =
Attr.style <|
basicStyle ++
[ ("position","relative")
, ("height", "140pt")
, ("width","370pt")
--, ("top","50pt")
--, ("left", "-50pt")
, ("border", "3pt")
, ("border-style", "solid")
, ("border-color", "green")
, ("background-color", "white")
]
graphicOutputStyle : Attribute
graphicOutputStyle =
Attr.style <|
basicStyle ++
[ ("position", "absolute")
, ("height", "270pt")
, ("width", "300pt")
, ("top","60pt")
, ("right", "0pt")
, ("border","3pt")
, ("border-style", "solid")
, ("border-color", "green")
, ("background-color", "white")
]
buttonBasic =
[ ("position","absolute")
, ("left", "330pt")
, ("border","none")
, ("border-radius","8pt")
, ("color", "white")
, ("text-align","center")
, ("text-decoration", "none")
, ("display", "inline-block")
, ("font-size","16pt")
, ("cursor", "pointer")
]
captionStyle : Attribute
captionStyle =
Attr.style
[ ("font-size", "36pt")
, ("font-family","Times New Roman")
, ("text-align","center")
, ("color", "red")
]
captionStyle1 : Attribute
captionStyle1 =
Attr.style
[ ("font-size", "20pt")
, ("font-family", "Palatino Linotype")
, ("color", "black")
]
captionStyle2 : Attribute
captionStyle2 =
Attr.style
[ ("position","absolute")
, ("right", "180pt")
, ("top","35pt")
, ("font-size", "20pt")
, ("font-family", "Palatino Linotype")
, ("color", "black")
]
captionStyle3 : Attribute
captionStyle3 =
Attr.style
[ ("position","absolute")
--, ("left", "0pt")
--, ("bottom","20pt")
, ("right", "245pt")
, ("top","340pt")
, ("font-size", "10pt")
, ("font-family", "Arial")
, ("color", "black")
]
bodyStyle : Attribute
bodyStyle =
Attr.style
[ ("background-color", "grey") ]
errorStyle : Attribute
errorStyle =
Attr.style
[ ("font-size", "20pt")
, ("font-family", "Arial Black")
, ("color", "red")
]
dropDownStyle : Attribute
dropDownStyle =
Attr.style
[ ("position","absolute")
, ("top", "-40pt")
, ("right", "50pt")
, ("width", "200pt")
, ("height", "40pt")
, ("font-size", "20pt")
, ("font-family", "Book Antiqua")
]
--------------------------------------------------
compute : String -> Result String String
compute input =
case Parser.parse input of
Ok exp -> R.map Parser.unparse <| E.eval exp
Err s -> Err <| "parse error: please check your input"
btnMailbox : Mailbox String
btnMailbox = mailbox ""
evtMailbox : Mailbox Event
evtMailbox = mailbox (UpModel identity)
-- add more fields to model if necessary
type alias Model =
{ input : String
, output : Result String String
, graphic : Maybe GE.Element
}
type Event = UpModel (Model -> Model)
upstate : Event -> Model -> Model
upstate event model =
case event of
UpModel f -> f model
events : Signal Event
events = Signal.merge evtMailbox.signal eventsFromJS
updateGraphic : String -> (Maybe String, Maybe GE.Element)
updateGraphic input =
case Parser.parse input of
Ok e -> if Parser.isFunc e || Parser.isRealConst e then (Nothing, Just <| G.plot (-1,1) 150 e)
else (Just "plot error: input not plottable", Nothing)
Err s -> (Just "parse error: Please check your input", Nothing)
fromOk : Result String String -> String
fromOk mx =
case mx of
Ok s -> s
Err s -> s
-- http://stackoverflow.com/questions/32426042/how-to-print-index-of-selected-option-in-elm
targetSelectedIndex : Decode.Decoder Int
targetSelectedIndex = Decode.at ["target", "selectedIndex"] Decode.int
index : Int -> List a -> a
index n xs =
if n < 0 then Debug.crash "index: negative index"
else case xs of
[] -> Debug.crash "index: empty list"
x::xs' -> if n == 0 then x else index (n-1) xs'
view : Model -> Html
view model =
let body =
let options =
let foo (name,code) =
Html.option [ Attr.value name] [ Html.text name ]
in
List.map foo examples
in
let dropDown =
Html.select
[ Attr.contenteditable False
--, dropDownStyle
, Events.on "change" targetSelectedIndex <| \i ->
let (_,code) = index i examples in
Signal.message evtMailbox.address <| UpModel <| \model ->
{ model | input = code }
]
options
in
let br = Html.br [] [] in
let example = Html.span
[ dropDownStyle ]
[ Html.text "Examples", br, dropDown ]
in
let input =
Html.div
[ inputStyle, Attr.contenteditable True, Attr.id "input" ]
[ Html.text model.input ]
in
let output =
case model.output of
Ok s -> Html.div
[ outputStyle, Attr.contenteditable False, Attr.id "output" ]
[ Html.text s ]
Err s -> Html.div
[ outputStyle, Attr.contenteditable False, Attr.id "output" ]
[ Html.text s]
in
let graphicOutput =
case model.graphic of
Just graph -> Html.div
[ graphicOutputStyle, Attr.id "graphicOutput" ]
[ Html.fromElement graph ]
_ -> Html.div [ graphicOutputStyle, Attr.id "graphicOutput" ] []
in
let computeBtn =
Html.button
[ Attr.contenteditable False
, Attr.style <|
buttonBasic ++
[ ("top", "0pt")
, ("background-color", "green")
, ("padding", "10pt")
]
, Events.onMouseDown btnMailbox.address "update"
, Events.onMouseUp btnMailbox.address (fromOk model.output)
]
[ Html.text "Compute" ]
in
let clearBtn =
Html.button
[ Attr.contenteditable False
, Attr.style <|
buttonBasic ++
[ ("top", "45pt")
, ("background-color", "#269F40")
, ("padding", "10pt 24pt")
]
, Events.onClick evtMailbox.address <| UpModel <| \model ->
{ model | input = "", output = Ok "", graphic = Nothing}
, Events.onMouseUp btnMailbox.address "clear"
]
[ Html.text "Clear" ]
in
let plotBtn =
Html.button
[ Attr.contenteditable False
, Attr.style <|
buttonBasic ++
[ ("top", "90pt")
, ("background-color","#73B76D")
, ("padding", "10pt 29pt")
]
, Events.onClick btnMailbox.address "graphicUpdate"
]
[ Html.text "Plot" ]
in
let inputCaption =
Html.span [ captionStyle1 ] [ Html.text "Input"]
in
let outputCaption =
Html.span [ captionStyle1 ] [ Html.text "Output"]
in
let graphicCaption =
Html.span [ captionStyle2 ] [ Html.text "Graphic Output" ]
in
let graphicInfo =
Html.span [ captionStyle3 ] [ Html.text "domain: [-1,1]" ]
in
Html.div
[ containerStyle ]
[ inputCaption, example, br, input, computeBtn, clearBtn, plotBtn, graphicCaption, graphicInfo, graphicOutput, br, br, br, outputCaption, output ]
in
let header =
let caption =
Html.h1 [ captionStyle ] [ Html.text "WolframAlpha in Elm" ]
in
Html.header [] [ caption ]
in
Html.body [ bodyStyle ] [ header, body ]
initModel : Model
initModel = { input = "", output = Ok "", graphic = Nothing}
--- interaction with javascript ---
eventsFromJS : Signal Event
eventsFromJS =
let foo (info, s) =
case info of
"update" -> UpModel <| \model ->
case compute s of
Ok s' -> { model | input = s, output = Ok <| "$$" ++ s' ++ "$$" }
Err s' -> { model | input = s, output = Err s'}
"graphicUpdate" -> UpModel <| \model ->
case updateGraphic s of
(Nothing, g) -> { model | input = s, graphic = g }
(Just s', _) -> { model | input = s, output = Err s', graphic = Nothing }
_ -> Debug.crash "signalFromJS"
in
Signal.map foo signalFromJS
port signalFromJS : Signal (String, String)
port signalToJS : Signal String
port signalToJS = btnMailbox.signal
main : Signal Html
main = Signal.map view (Signal.foldp upstate initModel events)