Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. module Main exposing (..)
  2.  
  3. import Html exposing (text, div)
  4. import UrlParser exposing (..)
  5. -- UrlParser > elm package install evancz/url-parser -y
  6.  
  7. import Navigation exposing (..)
  8.  
  9.  
  10. location : String -> Location
  11. location pathname =
  12. { pathname = pathname
  13. , search = ""
  14. , host = ""
  15. , hostname = ""
  16. , protocol = ""
  17. , origin = ""
  18. , port_ = ""
  19. , hash = ""
  20. , username = ""
  21. , password = ""
  22. , href = ""
  23. }
  24.  
  25.  
  26. type Page
  27. = PageOne
  28. | PageTwo Int
  29. | PageThree String
  30. | PageNotFound
  31.  
  32.  
  33. route : Parser (Page -> a) a
  34. route =
  35. oneOf
  36. [ UrlParser.map PageOne (s "pageone")
  37. , UrlParser.map PageTwo (s "pagetwo" </> int)
  38. , UrlParser.map PageThree (s "pagethree" </> string)
  39. ]
  40.  
  41.  
  42. locationToPage : Location -> ( String, Page )
  43. locationToPage location =
  44. parsePath route location
  45. |> Maybe.map (\p -> ( location.pathname, p ))
  46. |> Maybe.withDefault ( location.pathname, PageNotFound )
  47.  
  48.  
  49. locations : List String
  50. locations =
  51. [ "/junk"
  52. , "/pageone"
  53. , "/pagetwo/1234"
  54. , "/pagethree/something"
  55. ]
  56.  
  57.  
  58. pages : List ( String, Page )
  59. pages =
  60. List.map (location >> locationToPage) locations
  61.  
  62.  
  63. main : Html.Html msg
  64. main =
  65. pages
  66. |> List.map
  67. (\( path, page ) ->
  68. div []
  69. [ text <| path ++ " -> " ++ toString (page) ]
  70. )
  71. |> div []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement