This repository has been archived by the owner on Aug 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Navigation.elm
424 lines (297 loc) · 9.58 KB
/
Navigation.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
effect module Navigation where { command = MyCmd, subscription = MySub } exposing
( back, forward
, load, reload, reloadAndSkipCache
, newUrl, modifyUrl
, program, programWithFlags
, Location
)
{-| This is a library for managing browser navigation yourself.
The core functionality is the ability to “navigate” to new URLs,
changing the address bar of the browser *without* the browser kicking off a
request to your servers. Instead, you manage the changes yourself in Elm.
# Change the URL
@docs newUrl, modifyUrl
# Navigation
@docs back, forward
# Force Page Loads
@docs load, reload, reloadAndSkipCache
# Programs with Locations
@docs program, programWithFlags, Location
-}
import Dom.LowLevel exposing (onWindow)
import Html exposing (Html)
import Json.Decode as Json
import Native.Navigation
import Process
import Task exposing (Task)
-- PROGRAMS
{-| Same as [`Html.program`][doc], but your `update` function gets messages
whenever the URL changes.
[doc]: http://package.elm-lang.org/packages/elm-lang/html/latest/Html#program
The first difference is the `Location -> msg` argument. This converts a
[`Location`](#location) into a message whenever the URL changes. That message
is fed into your `update` function just like any other one.
The second difference is that the `init` function takes `Location` as an
argument. This lets you use the URL on the first frame.
**Note:** A location message is produced every time the URL changes. This
includes things exposed by this library, like `back` and `newUrl`, as well as
whenever the user clicks the back or forward buttons of the browsers. So if
the URL changes, you will hear about it in your `update` function.
-}
program
: (Location -> msg)
->
{ init : Location -> (model, Cmd msg)
, update : msg -> model -> (model, Cmd msg)
, view : model -> Html msg
, subscriptions : model -> Sub msg
}
-> Program Never model msg
program locationToMessage stuff =
let
subs model =
Sub.batch
[ subscription (Monitor locationToMessage)
, stuff.subscriptions model
]
init =
stuff.init (Native.Navigation.getLocation ())
in
Html.program
{ init = init
, view = stuff.view
, update = stuff.update
, subscriptions = subs
}
{-| Works the same as [`program`](#program), but it can also handle flags.
See [`Html.programWithFlags`][doc] for more information.
[doc]: http://package.elm-lang.org/packages/elm-lang/html/latest/Html#programWithFlags
-}
programWithFlags
: (Location -> msg)
->
{ init : flags -> Location -> (model, Cmd msg)
, update : msg -> model -> (model, Cmd msg)
, view : model -> Html msg
, subscriptions : model -> Sub msg
}
-> Program flags model msg
programWithFlags locationToMessage stuff =
let
subs model =
Sub.batch
[ subscription (Monitor locationToMessage)
, stuff.subscriptions model
]
init flags =
stuff.init flags (Native.Navigation.getLocation ())
in
Html.programWithFlags
{ init = init
, view = stuff.view
, update = stuff.update
, subscriptions = subs
}
-- TIME TRAVEL
{-| Go back some number of pages. So `back 1` goes back one page, and `back 2`
goes back two pages.
**Note:** You only manage the browser history that *you* created. Think of this
library as letting you have access to a small part of the overall history. So
if you go back farther than the history you own, you will just go back to some
other website!
-}
back : Int -> Cmd msg
back n =
command (Jump -n)
{-| Go forward some number of pages. So `forward 1` goes forward one page, and
`forward 2` goes forward two pages. If there are no more pages in the future,
this will do nothing.
**Note:** You only manage the browser history that *you* created. Think of this
library as letting you have access to a small part of the overall history. So
if you go forward farther than the history you own, the user will end up on
whatever website they visited next!
-}
forward : Int -> Cmd msg
forward n =
command (Jump n)
{-| Leave the current page and load the given URL. **This always results in a
page load**, even if the provided URL is the same as the current one.
load "http://elm-lang.org"
Use [`newUrl`](#newUrl) and [`modifyUrl`](#modifyUrl) if you want to change
the URL without a page load.
-}
load : String -> Cmd msg
load url =
command (Visit url)
{-| Reload the current page. **This always results in a page load!**
This may grab resources from the browser cache, so use
[`reloadAndSkipCache`](reloadAndSkipCache) if you want to be sure
that you are not loading any cached resources.
-}
reload : Cmd msg
reload =
command (Reload False)
{-| Reload the current page without using the browser cache. **This always
results in a page load!** It is more common to want [`reload`](reload).
-}
reloadAndSkipCache : Cmd msg
reloadAndSkipCache =
command (Reload True)
-- CHANGE HISTORY
{-| Step to a new URL. This will add a new entry to the browser history.
**Note:** If the user has gone `back` a few pages, there will be “future
pages” that the user can go `forward` to. Adding a new URL in that
scenario will clear out any future pages. It is like going back in time and
making a different choice.
-}
newUrl : String -> Cmd msg
newUrl url =
command (New url)
{-| Modify the current URL. This *will not* add a new entry to the browser
history. It just changes the one you are on right now.
-}
modifyUrl : String -> Cmd msg
modifyUrl url =
command (Modify url)
-- LOCATION
{-| A bunch of information about the address bar.
**Note 1:** Almost everyone will want to use a URL parsing library like
[`evancz/url-parser`][parse] to turn a `Location` into something more useful
in your `update` function.
[parse]: https://github.com/evancz/url-parser
**Note 2:** These fields correspond exactly with the fields of `document.location`
as described [here](https://developer.mozilla.org/en-US/docs/Web/API/Location).
-}
type alias Location =
{ href : String
, host : String
, hostname : String
, protocol : String
, origin : String
, port_ : String
, pathname : String
, search : String
, hash : String
, username : String
, password : String
}
-- EFFECT MANAGER
type MyCmd msg
= Jump Int
| New String
| Modify String
| Visit String
| Reload Bool
cmdMap : (a -> b) -> MyCmd a -> MyCmd b
cmdMap _ myCmd =
case myCmd of
Jump n ->
Jump n
New url ->
New url
Modify url ->
Modify url
Visit url ->
Visit url
Reload skipCache ->
Reload skipCache
type MySub msg =
Monitor (Location -> msg)
subMap : (a -> b) -> MySub a -> MySub b
subMap func (Monitor tagger) =
Monitor (tagger >> func)
-- STATE
type alias State msg =
{ subs : List (MySub msg)
, popWatcher : Maybe PopWatcher
}
type PopWatcher
= Normal Process.Id
| InternetExplorer Process.Id Process.Id
-- INIT
init : Task Never (State msg)
init =
Task.succeed (State [] Nothing)
-- SELF MESSAGES
onSelfMsg : Platform.Router msg Location -> Location -> State msg -> Task Never (State msg)
onSelfMsg router location state =
notify router state.subs location
&> Task.succeed state
(&>) task1 task2 =
Task.andThen (\_ -> task2) task1
-- APP MESSAGES
onEffects : Platform.Router msg Location -> List (MyCmd msg) -> List (MySub msg) -> State msg -> Task Never (State msg)
onEffects router cmds subs {popWatcher} =
let
stepState =
case (subs, popWatcher) of
([], Just watcher) ->
killPopWatcher watcher
&> Task.succeed (State subs Nothing)
(_ :: _, Nothing) ->
Task.map (State subs << Just) (spawnPopWatcher router)
(_, _) ->
Task.succeed (State subs popWatcher)
in
Task.sequence (List.map (cmdHelp router subs) cmds)
&> stepState
cmdHelp : Platform.Router msg Location -> List (MySub msg) -> MyCmd msg -> Task Never ()
cmdHelp router subs cmd =
case cmd of
Jump n ->
go n
New url ->
pushState url
|> Task.andThen (notify router subs)
Modify url ->
replaceState url
|> Task.andThen (notify router subs)
Visit url ->
setLocation url
Reload skipCache ->
reloadPage skipCache
notify : Platform.Router msg Location -> List (MySub msg) -> Location -> Task x ()
notify router subs location =
let
send (Monitor tagger) =
Platform.sendToApp router (tagger location)
in
Task.sequence (List.map send subs)
&> Task.succeed ()
setLocation : String -> Task x ()
setLocation =
Native.Navigation.setLocation
reloadPage : Bool -> Task x ()
reloadPage =
Native.Navigation.reloadPage
go : Int -> Task x ()
go =
Native.Navigation.go
pushState : String -> Task x Location
pushState =
Native.Navigation.pushState
replaceState : String -> Task x Location
replaceState =
Native.Navigation.replaceState
-- POP WATCHER STUFF
spawnPopWatcher : Platform.Router msg Location -> Task x PopWatcher
spawnPopWatcher router =
let
reportLocation _ =
Platform.sendToSelf router (Native.Navigation.getLocation ())
in
if Native.Navigation.isInternetExplorer11 () then
Task.map2 InternetExplorer
(Process.spawn (onWindow "popstate" Json.value reportLocation))
(Process.spawn (onWindow "hashchange" Json.value reportLocation))
else
Task.map Normal <|
Process.spawn (onWindow "popstate" Json.value reportLocation)
killPopWatcher : PopWatcher -> Task x ()
killPopWatcher popWatcher =
case popWatcher of
Normal pid ->
Process.kill pid
InternetExplorer pid1 pid2 ->
Process.kill pid1
&> Process.kill pid2