Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a6a7276

Browse files
committed
Add routing
1 parent d8f0932 commit a6a7276

File tree

5 files changed

+147
-33
lines changed

5 files changed

+147
-33
lines changed

‎elm.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,18 @@
1414
"elm/random": "1.0.0",
1515
"elm/regex": "1.0.0",
1616
"elm/svg": "1.0.1",
17+
"elm/url": "1.0.0",
1718
"json-tools/json-schema": "1.0.1",
1819
"json-tools/json-value": "1.0.1",
1920
"zwilias/elm-utf-tools": "2.0.1"
2021
},
2122
"indirect": {
2223
"elm/time": "1.0.0",
23-
"elm/url": "1.0.0",
2424
"elm/virtual-dom": "1.0.2"
2525
}
2626
},
2727
"test-dependencies": {
2828
"direct": {},
2929
"indirect": {}
30-
},
31-
"homepage": "https://json-tools.github.io/json-form"
32-
}
30+
}
31+
}

‎src/Demo.elm

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
module Demo exposing (init, update, view)
1+
module Demo exposing (Msg(..), init, update, view)
22

3-
import Browser exposing (Document)
3+
import Browser exposing (Document, UrlRequest(..))
4+
import Browser.Navigation exposing (Key)
45
import Html exposing (Html, div, span, text)
56
import Html.Attributes exposing (class, classList, style)
67
import Html.Events exposing (onClick)
@@ -9,7 +10,9 @@ import Json.Form
910
import Json.Form.Config exposing (TextFieldStyle(..))
1011
import Json.Schema.Definitions exposing (Schema)
1112
import Json.Value exposing (JsonValue(..))
12-
import Snippets exposing (Example, Snippet(..), getSnippet, getSnippetTitle)
13+
import Route exposing (Route(..))
14+
import Showcase exposing (Example, Showcase(..), getShowcase, getShowcaseTitle)
15+
import Url exposing (Url)
1316

1417

1518
type alias ExampleDemo =
@@ -19,32 +22,56 @@ type alias ExampleDemo =
1922

2023

2124
type alias Model =
22-
{ showcase : Snippet
25+
{ showcase : Showcase
2326
, examples : List ExampleDemo
27+
, key : Key
2428
}
2529

2630

27-
initialShowcase : Snippet
31+
initialShowcase : Showcase
2832
initialShowcase =
2933
Validation
3034

3135

32-
init : Value -> ( Model, Cmd Msg )
33-
init _ =
36+
init : Value -> Url->Key->( Model, Cmd Msg )
37+
init _ url key =
3438
{ showcase = initialShowcase
3539
, examples = []
40+
, key = key
3641
}
37-
|> update (SetShowcase initialShowcase)
42+
|> update (url |>Route.fromLocation |>SetRoute)
3843

3944

4045
type Msg
41-
= SetShowcase Snippet
46+
= SetShowcase Showcase
4247
| JsonFormMsg Int Json.Form.Msg
48+
| SetRoute (Maybe Route)
49+
| UrlRequested UrlRequest
4350

4451

4552
update : Msg -> Model -> ( Model, Cmd Msg )
4653
update message model =
4754
case message of
55+
UrlRequested urlRequest ->
56+
case urlRequest of
57+
Internal url ->
58+
( model
59+
, Browser.Navigation.pushUrl model.key (Url.toString url)
60+
)
61+
62+
External url ->
63+
( model
64+
, Browser.Navigation.load url
65+
)
66+
67+
SetRoute route ->
68+
case route of
69+
Just (ShowcasePage sc) ->
70+
model |> update (SetShowcase sc)
71+
72+
_ ->
73+
model |> update (SetShowcase initialShowcase)
74+
4875
JsonFormMsg index msg ->
4976
let
5077
( examples, cmds ) =
@@ -71,7 +98,7 @@ update message model =
7198
SetShowcase s ->
7299
let
73100
( examples, cmds ) =
74-
getSnippet s
101+
getShowcase s
75102
|> List.indexedMap
76103
(\index example ->
77104
let
@@ -107,23 +134,24 @@ view model =
107134

108135
topbar : Model -> Html Msg
109136
topbar model =
110-
Snippets.index
137+
Showcase.index
111138
|> List.map (snippetTab model.showcase)
112139
|> div [ class "app-topbar" ]
113140

114141

115-
snippetTab : Snippet -> Snippet -> Html Msg
116-
snippetTab activeSnippet snippet =
142+
snippetTab : Showcase -> Showcase -> Html Msg
143+
snippetTab activeShowcase showcase =
117144
div
118145
[ classList
119146
[ ( "tab", True )
120-
, ( "tab--active", snippet == activeSnippet )
147+
, ( "tab--active", showcase == activeShowcase )
121148
]
122-
, onClick <| SetShowcase snippet
123149
]
124-
[ snippet
125-
|> getSnippetTitle
126-
|> text
150+
[ Html.a [ Route.href <| ShowcasePage showcase ]
151+
[ showcase
152+
|> getShowcaseTitle
153+
|> text
154+
]
127155
]
128156

129157

‎src/Main.elm

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
module Main exposing (main)
22

33
import Browser
4-
import Demo exposing (init, update, view)
4+
import Demo exposing (Msg(..), init, update, view)
55
import Html
6+
import Route
67

78

89
main =
9-
Browser.document
10-
{ init = init
10+
Browser.application
11+
{ view = view
12+
, init = init
1113
, update = update
12-
, view = view
1314
, subscriptions = \_ -> Sub.none
15+
, onUrlRequest = UrlRequested
16+
, onUrlChange = Route.fromLocation >> SetRoute
1417
}

‎src/Route.elm

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
module Route exposing (Route(..), fromLocation, href)
2+
3+
import Html exposing (Attribute)
4+
import Html.Attributes as Attr
5+
import Showcase exposing (Showcase)
6+
import Url exposing (Url)
7+
import Url.Parser exposing ((</>), Parser, custom, oneOf, parse, s, string)
8+
9+
10+
11+
-- ROUTING --
12+
13+
14+
type Route
15+
= ShowcasePage Showcase
16+
17+
18+
route : Parser (Route -> a) a
19+
route =
20+
oneOf
21+
[ Url.Parser.map ShowcasePage (s "showcase" </> showcaseParser)
22+
]
23+
24+
25+
showcaseParser =
26+
custom "SHOWCASE" Showcase.getShowcaseById
27+
28+
29+
30+
-- INTERNAL --
31+
32+
33+
routeToString : Route -> String
34+
routeToString page =
35+
let
36+
pieces =
37+
case page of
38+
ShowcasePage sc ->
39+
[ "showcase", sc |> Showcase.getShowcaseId ]
40+
in
41+
"/" ++ String.join "/" pieces
42+
43+
44+
45+
-- PUBLIC HELPERS --
46+
47+
48+
href : Route -> Attribute msg
49+
href =
50+
routeToString >> Attr.href
51+
52+
53+
fromLocation : Url -> Maybe Route
54+
fromLocation url =
55+
parse route url

‎src/Snippets.elm renamed to ‎src/Showcase.elm

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
module Snippets exposing (Example, Snippet(..), getSnippet, getSnippetTitle, index)
1+
module Showcase exposing (Example, Showcase(..), getShowcase, getShowcaseById, getShowcaseId, getShowcaseTitle, index)
22

33
import Json.Encode as Encode exposing (int, list, object, string)
44
import Json.Schema.Builder exposing (..)
55
import Json.Schema.Definitions exposing (Schema(..), SingleType(..), Type(..), blankSchema, blankSubSchema)
66

77

8-
type Snippet
8+
type Showcase
99
= InputTypes
1010
| Rules
1111
| Validation
@@ -17,16 +17,45 @@ type alias Example =
1717
}
1818

1919

20-
index : List Snippet
20+
index : List Showcase
2121
index =
2222
[ InputTypes
2323
, Rules
2424
, Validation
2525
]
2626

2727

28-
getSnippetTitle : Snippet -> String
29-
getSnippetTitle ds =
28+
getShowcaseById : String -> Maybe Showcase
29+
getShowcaseById id =
30+
case id of
31+
"types" ->
32+
Just InputTypes
33+
34+
"rules" ->
35+
Just Rules
36+
37+
"validation" ->
38+
Just Validation
39+
40+
_ ->
41+
Nothing
42+
43+
44+
getShowcaseId : Showcase -> String
45+
getShowcaseId s =
46+
case s of
47+
InputTypes ->
48+
"types"
49+
50+
Rules ->
51+
"rules"
52+
53+
Validation ->
54+
"validation"
55+
56+
57+
getShowcaseTitle : Showcase -> String
58+
getShowcaseTitle ds =
3059
case ds of
3160
InputTypes ->
3261
"Types"
@@ -48,8 +77,8 @@ makeExample title sb =
4877
}
4978

5079

51-
getSnippet : Snippet -> List Example
52-
getSnippet ds =
80+
getShowcase : Showcase -> List Example
81+
getShowcase ds =
5382
case ds of
5483
InputTypes ->
5584
[ buildSchema

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /