forked from evancz/elm-architecture-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 77eb76d
Showing
13 changed files
with
560 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
elm-stuff | ||
elm.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
module Counter where | ||
|
||
import Html (..) | ||
import Html.Attributes (..) | ||
import Html.Events (..) | ||
import Signal | ||
|
||
|
||
-- MODEL | ||
|
||
type alias Model = Int | ||
|
||
|
||
-- UPDATE | ||
|
||
type Action = Increment | Decrement | ||
|
||
update : Action -> Model -> Model | ||
update action model = | ||
case action of | ||
Increment -> model + 1 | ||
Decrement -> model - 1 | ||
|
||
|
||
-- VIEW | ||
|
||
view : Model -> Html | ||
view model = | ||
div [] | ||
[ button [ onClick (Signal.send actionChannel Decrement) ] [ text "-" ] | ||
, div [ countStyle ] [ text (toString model) ] | ||
, button [ onClick (Signal.send actionChannel Increment) ] [ text "+" ] | ||
] | ||
|
||
|
||
countStyle : Attribute | ||
countStyle = | ||
style | ||
[ ("font-size", "20px") | ||
, ("font-family", "monospace") | ||
, ("display", "inline-block") | ||
, ("width", "50px") | ||
, ("text-align", "center") | ||
] | ||
|
||
|
||
-- SIGNALS | ||
|
||
main : Signal Html | ||
main = | ||
Signal.map view model | ||
|
||
model : Signal Model | ||
model = | ||
Signal.foldp update 0 (Signal.subscribe actionChannel) | ||
|
||
actionChannel : Signal.Channel Action | ||
actionChannel = | ||
Signal.channel Increment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"version": "1.0.0", | ||
"summary": "helpful summary of your project, less than 80 characters", | ||
"repository": "https://github.com/USER/PROJECT.git", | ||
"license": "BSD3", | ||
"source-directories": [ | ||
"." | ||
], | ||
"exposed-modules": [], | ||
"dependencies": { | ||
"elm-lang/core": "1.1.0 <= v < 2.0.0", | ||
"evancz/elm-html": "1.1.0 <= v < 2.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module Counter (Model, init, Action, update, view) where | ||
|
||
import Html (..) | ||
import Html.Attributes (..) | ||
import Html.Events (..) | ||
import LocalChannel (..) | ||
|
||
|
||
-- MODEL | ||
|
||
type alias Model = Int | ||
|
||
|
||
init : Int -> Model | ||
init count = count | ||
|
||
|
||
-- UPDATE | ||
|
||
type Action = Increment | Decrement | ||
|
||
|
||
update : Action -> Model -> Model | ||
update action model = | ||
case action of | ||
Increment -> model + 1 | ||
Decrement -> model - 1 | ||
|
||
|
||
-- VIEW | ||
|
||
view : LocalChannel Action -> Model -> Html | ||
view channel model = | ||
div [] | ||
[ button [ onClick (send channel Decrement) ] [ text "-" ] | ||
, div [ countStyle ] [ text (toString model) ] | ||
, button [ onClick (send channel Increment) ] [ text "+" ] | ||
] | ||
|
||
|
||
countStyle : Attribute | ||
countStyle = | ||
style | ||
[ ("font-size", "20px") | ||
, ("font-family", "monospace") | ||
, ("display", "inline-block") | ||
, ("width", "50px") | ||
, ("text-align", "center") | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
module CounterPair where | ||
|
||
import Counter | ||
import Html (..) | ||
import Html.Attributes (..) | ||
import Html.Events (..) | ||
import LocalChannel as LC | ||
import Signal | ||
|
||
|
||
-- MODEL | ||
|
||
type alias Model = | ||
{ topCounter : Counter.Model | ||
, bottomCounter : Counter.Model | ||
} | ||
|
||
|
||
init : Int -> Int -> Model | ||
init top bottom = | ||
{ topCounter = Counter.init top | ||
, bottomCounter = Counter.init bottom | ||
} | ||
|
||
|
||
-- UPDATE | ||
|
||
type Action | ||
= Reset | ||
| Top Counter.Action | ||
| Bottom Counter.Action | ||
|
||
|
||
update : Action -> Model -> Model | ||
update action model = | ||
case action of | ||
Reset -> init 0 0 | ||
|
||
Top act -> | ||
{ model | | ||
topCounter <- Counter.update act model.topCounter | ||
} | ||
|
||
Bottom act -> | ||
{ model | | ||
bottomCounter <- Counter.update act model.bottomCounter | ||
} | ||
|
||
|
||
-- VIEW | ||
|
||
view : Model -> Html | ||
view model = | ||
div [] | ||
[ Counter.view (LC.create Top actionChannel) model.topCounter | ||
, Counter.view (LC.create Bottom actionChannel) model.bottomCounter | ||
, button [ onClick (Signal.send actionChannel Reset) ] [ text "RESET" ] | ||
] | ||
|
||
|
||
-- SIGNALS | ||
|
||
main : Signal Html | ||
main = | ||
Signal.map view model | ||
|
||
|
||
model : Signal Model | ||
model = | ||
Signal.foldp update (init 0 0) (Signal.subscribe actionChannel) | ||
|
||
|
||
actionChannel : Signal.Channel Action | ||
actionChannel = | ||
Signal.channel Reset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"version": "1.0.0", | ||
"summary": "helpful summary of your project, less than 80 characters", | ||
"repository": "https://github.com/USER/PROJECT.git", | ||
"license": "BSD3", | ||
"source-directories": [ | ||
"." | ||
], | ||
"exposed-modules": [], | ||
"dependencies": { | ||
"elm-lang/core": "1.1.0 <= v < 2.0.0", | ||
"evancz/elm-html": "1.1.0 <= v < 2.0.0", | ||
"evancz/local-channel": "1.0.0 <= v < 2.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module Counter (Model, init, Action, update, view) where | ||
|
||
import Html (..) | ||
import Html.Attributes (..) | ||
import Html.Events (..) | ||
import LocalChannel (..) | ||
|
||
|
||
-- MODEL | ||
|
||
type alias Model = Int | ||
|
||
|
||
init : Int -> Model | ||
init count = count | ||
|
||
|
||
-- UPDATE | ||
|
||
type Action = Increment | Decrement | ||
|
||
|
||
update : Action -> Model -> Model | ||
update action model = | ||
case action of | ||
Increment -> model + 1 | ||
Decrement -> model - 1 | ||
|
||
|
||
-- VIEW | ||
|
||
view : LocalChannel Action -> Model -> Html | ||
view channel model = | ||
div [] | ||
[ button [ onClick (send channel Decrement) ] [ text "-" ] | ||
, div [ countStyle ] [ text (toString model) ] | ||
, button [ onClick (send channel Increment) ] [ text "+" ] | ||
] | ||
|
||
|
||
countStyle : Attribute | ||
countStyle = | ||
style | ||
[ ("font-size", "20px") | ||
, ("font-family", "monospace") | ||
, ("display", "inline-block") | ||
, ("width", "50px") | ||
, ("text-align", "center") | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
module CounterList where | ||
|
||
import Counter | ||
import Html (..) | ||
import Html.Attributes (..) | ||
import Html.Events (..) | ||
import List | ||
import LocalChannel as LC | ||
import Signal | ||
|
||
|
||
-- MODEL | ||
|
||
type alias Model = | ||
{ counters : List ( ID, Counter.Model ) | ||
, nextID : ID | ||
} | ||
|
||
type alias ID = Int | ||
|
||
|
||
init : Model | ||
init = | ||
{ counters = [] | ||
, nextID = 0 | ||
} | ||
|
||
|
||
-- UPDATE | ||
|
||
type Action | ||
= Insert | ||
| Remove | ||
| Modify ID Counter.Action | ||
|
||
|
||
update : Action -> Model -> Model | ||
update action model = | ||
case action of | ||
Insert -> | ||
let newCounter = ( model.nextID, Counter.init 0 ) | ||
newCounters = model.counters ++ [ newCounter ] | ||
in | ||
{ model | | ||
counters <- newCounters, | ||
nextID <- model.nextID + 1 | ||
} | ||
|
||
Remove -> | ||
{ model | counters <- List.drop 1 model.counters } | ||
|
||
Modify id counterAction -> | ||
let updateCounter (counterID, counterModel) = | ||
if counterID == id | ||
then (counterID, Counter.update counterAction counterModel) | ||
else (counterID, counterModel) | ||
in | ||
{ model | counters <- List.map updateCounter model.counters } | ||
|
||
|
||
-- VIEW | ||
|
||
view : Model -> Html | ||
view model = | ||
let counters = List.map viewCounter model.counters | ||
remove = button [ onClick (Signal.send actionChannel Remove) ] [ text "Remove" ] | ||
insert = button [ onClick (Signal.send actionChannel Insert) ] [ text "Add" ] | ||
in | ||
div [] ([remove, insert] ++ counters) | ||
|
||
|
||
viewCounter : (ID, Counter.Model) -> Html | ||
viewCounter (id, model) = | ||
Counter.view (LC.create (Modify id) actionChannel) model | ||
|
||
|
||
-- SIGNALS | ||
|
||
main : Signal Html | ||
main = | ||
Signal.map view model | ||
|
||
|
||
model : Signal Model | ||
model = | ||
Signal.foldp update init (Signal.subscribe actionChannel) | ||
|
||
|
||
actionChannel : Signal.Channel Action | ||
actionChannel = | ||
Signal.channel Insert |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"version": "1.0.0", | ||
"summary": "helpful summary of your project, less than 80 characters", | ||
"repository": "https://github.com/USER/PROJECT.git", | ||
"license": "BSD3", | ||
"source-directories": [ | ||
"." | ||
], | ||
"exposed-modules": [], | ||
"dependencies": { | ||
"elm-lang/core": "1.1.0 <= v < 2.0.0", | ||
"evancz/elm-html": "1.1.0 <= v < 2.0.0", | ||
"evancz/local-channel": "1.0.0 <= v < 2.0.0" | ||
} | ||
} |
Oops, something went wrong.