@@ -3,6 +3,9 @@ module Demo.JS ( readInputState
3
3
, writeInputState
4
4
, mkRandomInput
5
5
, sDrawButton
6
+ , printHighError
7
+ , printLowError
8
+ , cullErrors
6
9
, sRandomButton
7
10
, drawList
8
11
, placeValues
@@ -14,6 +17,7 @@ module Demo.JS ( readInputState
14
17
15
18
import Control.Monad
16
19
import Control.Applicative
20
+ import Text.Read (readMaybe )
17
21
import JavaScript.JQuery
18
22
import JavaScript.Canvas hiding (Left , Right )
19
23
import GHCJS.Types
@@ -48,10 +52,15 @@ sCellNum i = select (pack (template (cellMkName i)))
48
52
++ n
49
53
++ " \" type=\" text\" name=\" a\" /></div>"
50
54
51
- getGenInfo :: IO (String , String )
52
- getGenInfo = do start <- fmap unpack (sStartHead >>= getVal)
53
- size <- fmap unpack (sNumCells >>= getVal)
54
- return (start, size)
55
+ getGenInfo :: IO (Either String (Int , Int ))
56
+ getGenInfo =
57
+ do start <- fmap unpack (sStartHead >>= getVal)
58
+ size <- fmap unpack (sNumCells >>= getVal)
59
+ case (readMaybe start, readMaybe size) of
60
+ (Nothing ,_) -> return (Left " \" Starting Index\" requires Integer" )
61
+ (Just _, Nothing ) ->
62
+ return (Left " \" Number of Memory Cells\" requires Integer" )
63
+ (Just i, Just s) -> return (Right (i,s))
55
64
56
65
placeValues :: Int -> Int -> IO ()
57
66
placeValues start size =
@@ -166,9 +175,9 @@ mkCanvas = do
166
175
return ()
167
176
168
177
displayOutput :: Either String Layout -> IO ()
169
- displayOutput l = cullError >> case l of
170
- Left er -> printError er
171
- Right ls -> drawList ls
178
+ displayOutput l = cullErrors >> case l of
179
+ Left er -> printLowError er
180
+ Right ls -> drawList ls
172
181
173
182
withPadding :: (Double , Double ) -> (Double , Double )
174
183
withPadding (x,y) = (x - (2 * canvasXPadding), y - (2 * canvasYPadding))
@@ -283,8 +292,19 @@ drawElem c scale elem =
283
292
284
293
restore c
285
294
286
- cullError = return ()
287
- printError a = return ()
295
+ cullErrors = select " #lowError" >>= remove
296
+ >> select " #highError" >>= remove
297
+ >> return ()
298
+
299
+ printHighError = printError " highError" " #b"
300
+ printLowError = printError " lowError" " #c"
301
+
302
+ printError a b e =
303
+ do err <- select (pack
304
+ (" <p class=\" errors\" id=\" " ++ a ++ " \" >Error: (" ++ e ++ " )</p>" ))
305
+ par <- select (pack b)
306
+ appendJQuery err par
307
+ return ()
288
308
289
309
drawTextCenter :: Coord -- location at which to center the text
290
310
-> Double -- maximum width of the text
0 commit comments