Skip to content

Commit

Permalink
Change visibility to a Union Type.
Browse files Browse the repository at this point in the history
This makes it more difficult to send the model through a port for
serialization, so I ripped that feature out. It seems like this won't
be easy until there is a solution to

elm/compiler#490
  • Loading branch information
jwmerrill committed Jan 31, 2016
1 parent 8be8914 commit 3e2699a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
47 changes: 26 additions & 21 deletions Todo.elm
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ type alias Model =
{ tasks : List Task
, field : String
, uid : Int
, visibility : String
, visibility : Visibility
}

type Visibility
= All
| Active
| Completed

type alias Task =
{ description : String
Expand All @@ -57,7 +61,7 @@ newTask desc id =
emptyModel : Model
emptyModel =
{ tasks = []
, visibility = "All"
, visibility = All
, field = ""
, uid = 0
}
Expand All @@ -78,7 +82,7 @@ type Action
| DeleteComplete
| Check Int Bool
| CheckAll Bool
| ChangeVisibility String
| ChangeVisibility Visibility


-- How we update our Model on a given Action?
Expand Down Expand Up @@ -178,13 +182,13 @@ taskEntry address task =
]


taskList : Address Action -> String -> List Task -> Html
taskList : Address Action -> Visibility -> List Task -> Html
taskList address visibility tasks =
let isVisible todo =
case visibility of
"Completed" -> todo.completed
"Active" -> not todo.completed
_ -> True
Completed -> todo.completed
Active -> not todo.completed
All -> True

allCompleted = List.all .completed tasks

Expand Down Expand Up @@ -246,7 +250,7 @@ todoItem address todo =
]


controls : Address Action -> String -> List Task -> Html
controls : Address Action -> Visibility -> List Task -> Html
controls address visibility tasks =
let tasksCompleted = List.length (List.filter .completed tasks)
tasksLeft = List.length tasks - tasksCompleted
Expand All @@ -263,11 +267,11 @@ controls address visibility tasks =
]
, ul
[ id "filters" ]
[ visibilitySwap address "#/" "All" visibility
[ visibilitySwap address "#/" All visibility
, text " "
, visibilitySwap address "#/active" "Active" visibility
, visibilitySwap address "#/active" Active visibility
, text " "
, visibilitySwap address "#/completed" "Completed" visibility
, visibilitySwap address "#/completed" Completed visibility
]
, button
[ class "clear-completed"
Expand All @@ -279,11 +283,11 @@ controls address visibility tasks =
]


visibilitySwap : Address Action -> String -> String -> String -> Html
visibilitySwap : Address Action -> String -> Visibility -> Visibility -> Html
visibilitySwap address uri visibility actualVisibility =
li
[ onClick address (ChangeVisibility visibility) ]
[ a [ href uri, classList [("selected", visibility == actualVisibility)] ] [ text visibility ] ]
[ a [ href uri, classList [("selected", visibility == actualVisibility)] ] [ text (representVisibility visibility) ] ]


infoFooter : Html
Expand All @@ -301,6 +305,14 @@ infoFooter =
]


representVisibility : Visibility -> String
representVisibility visibility =
case visibility of
All -> "All"
Active -> "Active"
Completed -> "Completed"


---- INPUTS ----

-- wire the entire application together
Expand All @@ -317,7 +329,7 @@ model =

initialModel : Model
initialModel =
Maybe.withDefault emptyModel getStorage
emptyModel


-- actions from user input
Expand All @@ -341,10 +353,3 @@ port focus =
actions.signal
|> Signal.filter needsFocus (EditingTask 0 True)
|> Signal.map toSelector


-- interactions with localStorage to save the model
port getStorage : Maybe Model

port setStorage : Signal Model
port setStorage = model
5 changes: 1 addition & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<script type="text/javascript">
var storedState = localStorage.getItem('elm-todo-state');
var startingState = storedState ? JSON.parse(storedState) : null;
var todomvc = Elm.fullscreen(Elm.Todo, { getStorage: startingState });
var todomvc = Elm.fullscreen(Elm.Todo);
todomvc.ports.focus.subscribe(function(selector) {
setTimeout(function() {
var nodes = document.querySelectorAll(selector);
Expand All @@ -23,9 +23,6 @@
}
}, 50);
});
todomvc.ports.setStorage.subscribe(function(state) {
localStorage.setItem('elm-todo-state', JSON.stringify(state));
});
</script>

</html>

0 comments on commit 3e2699a

Please sign in to comment.