Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. import Html exposing (beginnerProgram, div, button, text,input)
  2. import Html.Events exposing (onClick, onInput)
  3. import Html.Attributes exposing (..)
  4.  
  5. main : Program Never Model Msg
  6. main =
  7. beginnerProgram { model = model, update = update, view = view}
  8.  
  9. type alias Model =
  10. { input : String
  11. , tasks : List String
  12. , done : Int
  13. , dayGoal : Int --default is 3
  14. , taskHistory : List String
  15. }
  16.  
  17. model : Model
  18. model =
  19. Model "" [] 0 3 []
  20.  
  21. type Msg =
  22. Add
  23. | Del
  24. | DelCheckbox String
  25. | Input String
  26. | SetGoal
  27.  
  28.  
  29. filterFirst : String -> List String -> List String
  30. filterFirst str list = if str == ""
  31. then list
  32. else if (List.length list) == 0
  33. then []
  34. else if (List.take 1 list) == [str]
  35. then (List.drop 1 list)
  36. else List.append (List.take 1 list) (filterFirst str (List.drop 1 list))
  37.  
  38. toAdd : List String -> String -> Int
  39. toAdd list str = if List.member str list
  40. then 1
  41. else 0
  42.  
  43. getFirst : String -> List String -> String
  44. getFirst str list = if List.member str list
  45. then str
  46. else ""
  47.  
  48. appendHistory : String -> List String -> List String
  49. appendHistory str list = if str == ""
  50. then list
  51. else
  52. (List.append list [str])
  53.  
  54. --List.filter (\x-> (x/=model.input || model.input == "")
  55. update : Msg -> Model -> Model
  56. update msg model=
  57. case msg of
  58. Del ->
  59. (Model model.input (filterFirst model.input model.tasks)
  60. (model.done+(toAdd model.tasks model.input)) (model.dayGoal) (appendHistory model.input model.taskHistory)) --because otherwise 1 delete with no args deletes every task
  61. Add ->
  62. (Model model.input (List.append model.tasks [model.input]) model.done (model.dayGoal) model.taskHistory)
  63. Input str ->
  64. (Model str model.tasks model.done (model.dayGoal) model.taskHistory)
  65. DelCheckbox str ->
  66. (Model model.input (filterFirst str model.tasks)
  67. (model.done+(toAdd model.tasks model.input)) (model.dayGoal) (appendHistory str model.taskHistory))
  68. SetGoal ->
  69. (Model "" model.tasks model.done (Result.withDefault 0 (String.toInt model.input)) model.taskHistory)
  70.  
  71. checkIfdayGoal : Model -> Bool
  72. checkIfdayGoal model = if model.done >= model.dayGoal
  73. then True
  74. else
  75. False
  76.  
  77. getCongratulations : Model -> String
  78. getCongratulations model =
  79. if model.dayGoal == 0
  80. then "Man, be ambitious!"
  81. else if (checkIfdayGoal model)
  82. then "Congratulations, goal achieved!"
  83. else
  84. "Keep it up, all you need is " ++ (toString (model.dayGoal - model.done))
  85.  
  86. view : Model -> Html.Html Msg
  87. view model =
  88. div []
  89. [
  90. div [doneStyle] [text ("Done today: " ++ toString model.done)]
  91. , div [doneStyle] [text (getCongratulations model)]
  92. , div [alignCenter] [
  93. input [type_ "text", placeholder "Add new task",onInput Input] []
  94. , button [onClick Add] [text "Add"]
  95. , button [onClick Del] [text "Delete"]]
  96. , div [alignCenter] [input [type_ "text", placeholder "Set your day goal!",onInput Input] [] , button [onClick SetGoal] [text "Set Goal"]]
  97. , div [checkboxStyle] (List.map viewCheckbox model.tasks)
  98. , div [alignCenter] (List.map viewCheckbox model.taskHistory)
  99. ]
  100.  
  101. doneStyle : Html.Attribute msg
  102. doneStyle =
  103. style
  104. [
  105. ("padding","10px 0")
  106. ,("text-align","center")
  107. ]
  108.  
  109. checkboxStyle : Html.Attribute msg
  110. checkboxStyle =
  111. style
  112. [
  113. ("padding","20px 0")
  114. ,("text-align","center")
  115. ]
  116.  
  117. alignCenter : Html.Attribute msg
  118. alignCenter = style [("text-align","center")]
  119.  
  120.  
  121. viewMessage : String -> Html.Html msg
  122. viewMessage msg =
  123. div [] [ text msg ]
  124.  
  125. viewCheckbox : String -> Html.Html Msg
  126. viewCheckbox msg =
  127. div [] [ input [ Html.Attributes.type_ "checkbox", onClick (DelCheckbox msg)] []
  128. , text msg
  129. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement