-
-
Save ssciantarelli/0cba172048f3c9c28754ba4d0ac5a6b1 to your computer and use it in GitHub Desktop.
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
module Main exposing (..) | |
import Html exposing (..) | |
import Html.Attributes exposing (..) | |
import Html.Events exposing (..) | |
import Navigation | |
import Json.Decode as Json | |
import RouteUrl exposing (HistoryEntry(..), UrlChange) | |
import UrlParser exposing (..) | |
main = | |
RouteUrl.program | |
{ init = init | |
, view = view | |
, update = update | |
, subscriptions = (\_ -> Sub.none) | |
} | |
type alias Model = | |
{ currentRoute : Route } | |
type Route | |
= DogRoute | |
| CatRoute | |
type Msg | |
= SetActiveRoute Route | |
| NoOpString String | |
| NoOp | |
init : ( Model, Cmd Msg ) | |
init = | |
let | |
model = { currentRoute = DogRoute } | |
in | |
( model, Cmd.none ) | |
delta2url : Model -> Model -> Maybe UrlChange | |
delta2url previous current = | |
case current.currentRoute of | |
DogRoute -> | |
Just <| UrlChange NewEntry "/dogs" | |
CatRoute -> | |
Just <| UrlChange NewEntry "/cats" | |
url2messages : Navigation.Location -> List Msg | |
url2messages location = | |
case parser location of | |
Just route -> | |
[SetActiveRoute route] | |
Nothing -> | |
[NoOp] | |
matchers: Parser (Route -> a) a | |
matchers = | |
oneOf | |
[ UrlParser.map DogRoute (UrlParser.s "dogs") | |
, UrlParser.map CatRoute (UrlParser.s "cats") | |
] | |
parser : Navigation.Location -> Maybe Route | |
parser location = | |
parsePath matchers location | |
-- UPDATE | |
update : Msg -> Model -> ( Model, Cmd Msg ) | |
update msg model = | |
case msg of | |
SetActiveRoute activeRoute -> | |
( { model | currentRoute = activeRoute }, Cmd.none ) | |
NoOpString _ -> | |
(model, Cmd.none) | |
NoOp -> | |
(model, Cmd.none) | |
-- VIEW | |
view : Model -> Html Msg | |
view model = | |
div [] | |
[ h1 [] [ text "Pages" ] | |
, ul [] (List.map viewLink [ "cats", "dogs" ]) | |
] | |
viewLink : String -> Html Msg | |
viewLink name = | |
li [] | |
[ a | |
[ href ("/" ++ name) | |
, stopPropagation | |
] | |
[ text name ] | |
] | |
stopPropagation = | |
onWithOptions "click" | |
{ stopPropagation = False | |
, preventDefault = True | |
} | |
(Json.map NoOpString <| Json.succeed "") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment