on master help sign in
Merge branch 'master' of https://github.com/evancz/Elm
5349b40evancz
Examples/elm-happstack/Main.hs+12−8
@@ -5,8 +5,8 @@ import Happstack.Server
5import Language.Elm
5import Language.Elm
6import Language.Elm.Quasi
6import Language.Elm.Quasi
7
7
8−elmLoc :: String
8+elmRuntime="elm-runtime.js"
9−elmLoc = "https://raw.github.com/evancz/Elm/master/elm/elm-runtime-0.7.1.1.js"
9+elmRTPath='/':elmRuntime
10
10
11-- elmResponse is a "nice to have" helper function for compiling
11-- elmResponse is a "nice to have" helper function for compiling
12-- Elm code when using Elm with Happstack. At some point this might
12-- Elm code when using Elm with Happstack. At some point this might
@@ -15,7 +15,7 @@ elmResponse :: ElmSource a
15 => String -- ^ Page title
15 => String -- ^ Page title
16 -> a -- ^ elm source
16 -> a -- ^ elm source
17 -> Response
17 -> Response
18−elmResponse title = toResponse . toHtml elmLoc title
18+elmResponse title = toResponse . toHtml elmRTPath title
19
19
20-- embedding variables (in this case URLs)
20-- embedding variables (in this case URLs)
21rootHandler :: ServerPart Response
21rootHandler :: ServerPart Response
@@ -52,11 +52,13 @@ shapesHandler :: ServerPart Response
52shapesHandler = ok $ elmResponse "Simple shapes" $ shapesPage
52shapesHandler = ok $ elmResponse "Simple shapes" $ shapesPage
53
53
54-- routing
54-- routing
55−elmExample :: ServerPart Response
55+elmExample :: String -> ServerPart Response
56−elmExample =
56+elmExample elmLoc = do
57− msum [ nullDir >> rootHandler
57+ msum [ nullDir >> rootHandler
58+ , dir elmRuntime $ nullDir >>
59+ serveFile (guessContentTypeM mimeTypes) elmLoc
58 , dir "mouse" $ nullDir >>
60 , dir "mouse" $ nullDir >>
59− mouseHandler
61+ mouseHandler
60 , dir "clock" $ nullDir >>
62 , dir "clock" $ nullDir >>
61 clockHandler
63 clockHandler
62 , dir "shapes" $ nullDir >>
64 , dir "shapes" $ nullDir >>
@@ -64,4 +66,6 @@ elmExample =
64 ]
66 ]
65
67
66main :: IO ()
68main :: IO ()
69+main = do
70+ elmLoc <- Language.Elm.runtimeLocation
67−main = simpleHTTP nullConf {port = 3000} elmExample
71+ simpleHTTP nullConf {port = 3000} $ elmExample elmLoc
Examples/elm-happstack/elm_source/clock.elm+2−2
@@ -1,5 +1,5 @@
1
1
2−import Signal.Time (every)
2+import Time (every)
3
3
4hand clr len time =
4hand clr len time =
5 let t = pi * time / 30 - pi / 2 in
5 let t = pi * time / 30 - pi / 2 in
@@ -10,4 +10,4 @@ clock t = collage 400 400 [ filled (rgb 96 176 224) $ ngon 12 110 (200, 200)
10 , hand black 100 (t/60)
10 , hand black 100 (t/60)
11 , hand black 60 (t/720) ]
11 , hand black 60 (t/720) ]
12
12
13−main = lift clock (every 1)
13+main = lift clock (every 1)
Examples/elm-happstack/elm_source/index.elm+20−11
@@ -1,11 +1,11 @@
1−title w = size w 60 . box 4 . text . header . toText $ "Elm-Yesod"
1+title w = size w 60 . text . header . toText $ "Elm-Happstack"
2
2
3lightGrey = rgb 240 241 244
3lightGrey = rgb 240 241 244
4mediumGrey = rgb 216 221 225
4mediumGrey = rgb 216 221 225
5heading outer inner =
5heading outer inner =
6− color mediumGrey . size outer 61 . box 1 .
6+ color mediumGrey . size outer 61 .
7− color lightGrey . size outer 60 . box 5 .
7+ color lightGrey . size outer 60 .
8− size inner 60 . box 5 $ title inner
8+ size inner 60 $ title inner
9
9
10skeleton body outer =
10skeleton body outer =
11 let inner = if outer < 820 then outer - 20 else 800 in
11 let inner = if outer < 820 then outer - 20 else 800 in
@@ -17,19 +17,28 @@ skeleton body outer =
17
17
18section = text . bold . Text.height (5/4) . toText
18section = text . bold . Text.height (5/4) . toText
19
19
20+-- splicing of values works but literal substitution of strings
21+-- for this reason, we need to have quotes.
22+mouseLink = "#{mouse}"
23+clockLink = "#{clock}"
24+shapesLink = "#{shapes}"
25+
26+
20info w = List.map (\f -> f ()) . List.intersperse (\x -> plainText "&nbsp;") . List.map ((\e x -> e) . width w) $
27info w = List.map (\f -> f ()) . List.intersperse (\x -> plainText "&nbsp;") . List.map ((\e x -> e) . width w) $
21− [ section "Written in Elm, served with Yesod"
28+ [ section "Written in Elm, served with Happstack"
22 , text $ toText "This page is written in " ++ Text.link "http://elm-lang.org/" (toText "Elm") ++
29 , text $ toText "This page is written in " ++ Text.link "http://elm-lang.org/" (toText "Elm") ++
23 toText " and served using the " ++
30 toText " and served using the " ++
24− Text.link "http://yesodweb.com/" (toText "Yesod Web Framework") ++
31+ Text.link "http://happstack.com/" (toText "Happstack Web Framework") ++
25 toText ". Since you are looking at this page it is safe to assume that you already have the example code. "
32 toText ". Since you are looking at this page it is safe to assume that you already have the example code. "
26− , text $ toText "Type-safe URLs are rendered using simple QuasiQuoter variable interpolation."
33+ , text $ toText "URLs are rendered using simple QuasiQuoter variable interpolation."
27 , section "More examples:"
34 , section "More examples:"
28− , text $ toText "- " ++ Text.link mouse (toText "A simple mouse input example")
35+ , text $ toText "- " ++ Text.link mouseLink (toText "A simple mouse input example")
29− , text $ toText "- " ++ Text.link clock (toText "An animated analog clock")
36+ , text $ toText "- " ++ Text.link clockLink (toText "An animated analog clock")
30− , text $ toText "- " ++ Text.link shapes (toText "Some simple rendered shapes")
37+ , text $ toText "- " ++ Text.link shapesLink (toText "Some simple rendered shapes")
31 ]
38 ]
32
39
40+-- The following line does not parse, replaced it something slightly different
33−body outer inner = width outer . box 2 . flow down . (:) (plainText "&nbsp;") $ info inner
41+-- body outer inner = width outer . flow down $ (:) (plainText "&nbsp;") $ info inner
42+body outer inner = width outer . flow down $ info inner
34
43
35main = lift (skeleton body) Window.width
44main = lift (skeleton body) Window.width
Examples/elm-happstack/elm_source/mouse.elm+4−4
@@ -1,15 +1,15 @@
1
1
2-- Move your mouse around above the canvas!
2-- Move your mouse around above the canvas!
3
3
4−import Signal.Mouse (position)
4+import Mouse (position)
5−import Signal.Window (dimensions)
5+import Window (dimensions)
6
6
7myBlue = rgb 0 85 170
7myBlue = rgb 0 85 170
8myGreen = rgba 28 267 85 (1/2)
8myGreen = rgba 28 267 85 (1/2)
9
9
10scene (x,y) (w,h) =
10scene (x,y) (w,h) =
11− collage w h [ filled myBlue . rotate ((x+y)/1000) $ ngon 4 100 (200,200)
11+ collage w h [ rotate (toFloat (x+y) / 1000) $ filled myBlue $ ngon 4 100 (200,200)
12 , filled myGreen $ ngon 5 30 (x,y)
12 , filled myGreen $ ngon 5 30 (x,y)
13 ]
13 ]
14
14
15−main = lift2 scene position dimensions
15+main = lift2 scene Mouse.position Window.dimensions