-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.elm
231 lines (175 loc) · 5.98 KB
/
Main.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
module Main exposing (main)
import Browser
import Browser.Navigation
import Html exposing (Html, datalist, div, input, label, option, p, text)
import Html.Attributes exposing (checked, href, id, list, name, placeholder, style, type_, value)
import Html.Events exposing (onClick, onInput)
import Url
import Url.Builder
import Url.Parser exposing ((</>), (<?>), int, map, s, string)
import Url.Parser.Query
--
type DecodeMode
= NoPercentEncodeDecode
| PercentEncodeDecodePath
buildAbsolute : DecodeMode -> List String -> List Url.Builder.QueryParameter -> String
buildAbsolute mode paths params =
case mode of
NoPercentEncodeDecode ->
Url.Builder.absolute
paths
params
PercentEncodeDecodePath ->
-- https://github.com/elm/url/issues/25
Url.Builder.absolute
(List.map Url.percentEncode paths)
params
parserString : DecodeMode -> Url.Parser.Parser (String -> a) a
parserString mode =
case mode of
NoPercentEncodeDecode ->
Url.Parser.string
PercentEncodeDecodePath ->
-- https://github.com/elm/url/issues/16
Url.Parser.custom "STRING" Url.percentDecode
--
type Page
= Page String String
router : DecodeMode -> Url.Parser.Parser (Page -> b) b
router mode =
Url.Parser.oneOf
[ Url.Parser.map Page
(s "path"
</> parserString mode
<?> (Url.Parser.Query.string "query" |> Url.Parser.Query.map (Maybe.withDefault ""))
)
]
linkTo : DecodeMode -> String -> String -> Html msg
linkTo mode path query =
let
params =
if query == "" then
[]
else
[ Url.Builder.string "query" query ]
url =
buildAbsolute mode [ "path", path ] params
in
Html.a [ href url ]
[ text
("absolute "
++ Debug.toString [ "path", path ]
++ (" [ string \"query\" "
++ Debug.toString query
++ "]"
)
++ " = "
++ Debug.toString url
)
]
--
main =
Browser.application
{ init = init
, view = view
, update = update
, subscriptions = always Sub.none
, onUrlRequest = OnUrlRequest
, onUrlChange = OnUrlChange
}
type alias Model =
{ navKey : Browser.Navigation.Key
, url : Url.Url
, mode : DecodeMode
, page : Maybe Page
, path : String
, query : String
}
updatePage : Url.Url -> Model -> Model
updatePage url model =
{ model | url = url, page = Url.Parser.parse (router model.mode) url }
type Msg
= OnUrlRequest Browser.UrlRequest
| OnUrlChange Url.Url
| OnDecodeMode DecodeMode
| OnPathInput String
| OnQueryInput String
init : {} -> Url.Url -> Browser.Navigation.Key -> ( Model, Cmd Msg )
init flags url navKey =
( updatePage url
{ navKey = navKey
, url = url
, mode = NoPercentEncodeDecode
, page = Nothing
, path = ""
, query = ""
}
, Cmd.none
)
view : Model -> Browser.Document Msg
view model =
Browser.Document "App"
[ Html.div [ style "margin" "10em", style "font-family" "monospace" ]
[ Html.p []
[ p []
[ text "Step 1. Set Path "
, input [ onInput OnPathInput, value model.path, placeholder "path", list "path-input" ] []
, autocompleteFor "path-input"
]
, p []
[ text "Step 2. Set Query"
, input [ onInput OnQueryInput, value model.query, placeholder "query", list "query-input" ] []
, autocompleteFor "query-input"
]
, p []
[ text "Step 3. Set Mode "
, modeRadioButton NoPercentEncodeDecode model
, modeRadioButton PercentEncodeDecodePath model
]
, p [] [ text "Step 4. Click ", linkTo model.mode model.path model.query ]
]
, if model.page == Nothing then
text ""
else
div []
[ div [] [ text ("Expect : " ++ Debug.toString (Just (Page model.path model.query))) ]
, div [] [ text ("Actual : " ++ Debug.toString model.page) ]
]
]
]
modeRadioButton : DecodeMode -> Model -> Html Msg
modeRadioButton decodeMode model =
label [] [ input [ onClick (OnDecodeMode decodeMode), type_ "radio", checked (model.mode == decodeMode) ] [], text (decodeModeString decodeMode) ]
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
OnUrlRequest (Browser.Internal url) ->
( model, Browser.Navigation.pushUrl model.navKey (Url.toString url) )
OnUrlRequest (Browser.External urlString) ->
( model, Browser.Navigation.load urlString )
OnUrlChange url ->
( updatePage url model, Cmd.none )
OnDecodeMode mode ->
( { model | mode = mode, page = Nothing }, Cmd.none )
OnPathInput string ->
( { model | path = string, page = Nothing }, Cmd.none )
OnQueryInput string ->
( { model | query = string, page = Nothing }, Cmd.none )
--
decodeModeString : DecodeMode -> String
decodeModeString mode =
case mode of
NoPercentEncodeDecode ->
"NoPercentEncodeDecode"
PercentEncodeDecodePath ->
"PercentEncodeDecodePath"
autocompleteFor : String -> Html msg
autocompleteFor string =
datalist [ id string ]
[ option [ value "hello" ] []
, option [ value "👍" ] []
, option [ value "!@#$%^&*()" ] []
, option [ value "{}[]<>;/" ] []
, option [ value "日本" ] []
, option [ value "خحجث" ] []
]