Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. import Html exposing (..)
  2. import Html.Attributes exposing(..)
  3. import Html.Events exposing(..)
  4. import Signal exposing(..)
  5.  
  6. --MODEL
  7. type alias Model = {
  8. blueClicks : Int,
  9. redClicks : Int,
  10. greenClicks : Int
  11. }
  12.  
  13. initialModel : Model
  14. initialModel = Model 0 0 0
  15.  
  16.  
  17. --UPDATE
  18. type Action = NoOp
  19. | BlueClick
  20. | RedClick
  21. | GreenClick
  22.  
  23.  
  24. update : Action -> Model -> Model
  25. update action model =
  26. case action of
  27. NoOp ->
  28. model
  29.  
  30. BlueClick ->
  31. { model | blueClicks <- model.blueClicks + 1 }
  32.  
  33. RedClick ->
  34. { model | redClicks <- model.redClicks + 1 }
  35.  
  36. GreenClick ->
  37. { model | greenClicks <- model.greenClicks + 1 }
  38.  
  39.  
  40. --VIEW
  41. buttonStyle : String -> Attribute
  42. buttonStyle colour =
  43. style [
  44. ("margin", "10px")
  45. , ("display", "block")
  46. , ("width", "150px")
  47. , ("height", "40px")
  48. , ("background-color", colour)
  49. , ("color", "#ffffff")]
  50.  
  51.  
  52. totalClicks : Model -> String
  53. totalClicks model =
  54. (toString (model.blueClicks + model.redClicks + model.greenClicks))
  55.  
  56.  
  57. view : Address Action -> Model -> Html
  58. view address model =
  59. div [] [
  60. button [
  61. (buttonStyle "blue"),
  62. onClick address BlueClick
  63. ] [ text ("Blue Clicked: " ++ (toString model.blueClicks)) ],
  64.  
  65. button [(buttonStyle "red"),
  66. onClick address RedClick ] [ text ("Red Clicked: " ++ (toString model.redClicks)) ],
  67.  
  68. button [(buttonStyle "green"),
  69. onClick address GreenClick ] [ text ("Green Clicked: " ++ (toString model.greenClicks)) ],
  70.  
  71. div [] [
  72. text ("Total Clicks: " ++ (totalClicks model))
  73. ]
  74. ]
  75.  
  76. --WIRING
  77. actions : Mailbox Action
  78. actions =
  79. Signal.mailbox NoOp
  80.  
  81. model : Signal Model
  82. model =
  83. Signal.foldp update initialModel actions.signal
  84.  
  85.  
  86. main =
  87. Signal.map (view actions.address) model
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement