This repository has been archived by the owner on Sep 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
navigation.ts
612 lines (532 loc) · 18.9 KB
/
navigation.ts
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
import { DomEventDispatcher, OnDomEvent } from '@frontmeans/dom-events';
import { CxEntry, cxScoped, cxSingle } from '@proc7ts/context-values';
import {
AfterEvent,
AfterEvent__symbol,
EventKeeper,
EventSender,
mapAfter,
onAny,
OnEvent,
OnEvent__symbol,
onSupplied,
trackValue,
} from '@proc7ts/fun-events';
import { mergeFunctions, noop } from '@proc7ts/primitives';
import { BootstrapContext, BootstrapWindow } from '@wesib/wesib';
import { NavHistory, PageEntry } from './nav-history.impl';
import { NavigationAgent } from './navigation-agent';
import {
EnterPageEvent,
LeavePageEvent,
NavigationEvent,
NavigationEventType,
StayOnPageEvent,
} from './navigation.event';
import { Page } from './page';
import { PageParam } from './page-param';
const Navigation$perContext: CxEntry.Definer<Navigation> = /*#__PURE__*/ cxScoped(
BootstrapContext,
/*#__PURE__*/ cxSingle({
byDefault: Navigation$byDefault,
}),
);
/**
* Browser navigation service.
*
* Expected to be used as a [History] and [Location] APIs replacement.
*
* Fires additional navigation events the browser does not support natively.
*
* Implements an `EventSender` interface by sending {@link NavigationEvent navigation events} to registered receivers.
*
* Implements an `EventKeeper` interface by sending current {@link Page page} to registered receivers.
*
* Available as bootstrap context value.
*
* [History]: https://developer.mozilla.org/en-US/docs/Web/API/History
* [Location]: https://developer.mozilla.org/en-US/docs/Web/API/Location
*/
export abstract class Navigation implements EventSender<[NavigationEvent]>, EventKeeper<[Page]> {
static perContext(target: CxEntry.Target<Navigation>): CxEntry.Definition<Navigation> {
return Navigation$perContext(target);
}
static toString(): string {
return '[Navigation]';
}
/**
* Current page.
*/
abstract readonly page: Page;
/**
* The number of element in navigation history.
*/
abstract readonly length: number;
/**
* An `OnDomEvent` sender of {@link EnterPageEvent enter page events}.
*/
abstract readonly onEnter: OnDomEvent<EnterPageEvent>;
/**
* An `OnDomEvent` sender of {@link LeavePageEvent leave page events}.
*
* The registered listener may cancel navigation by calling `preventDefault()` method of received event.
*/
abstract readonly onLeave: OnDomEvent<LeavePageEvent>;
/**
* An `OnDomEvent` {@link StayOnPageEvent stay on page events}.
*
* The registered listener is informed when navigation has been cancelled by one of leave page event receivers,
* navigation failed due to e.g. invalid URL, or when another navigation request initiated before the page left.
*
* @returns `OnDomEvent` sender of {@link StayOnPageEvent stay on page events}.
*/
abstract readonly onStay: OnDomEvent<StayOnPageEvent>;
/**
* An `OnEvent` sender of {@link NavigationEvent navigation events}.
*
* The `[OnEvent__symbol]` property is an alias of this one.
*/
abstract readonly on: OnEvent<[NavigationEvent]>;
[OnEvent__symbol](): OnEvent<[NavigationEvent]> {
return this.on;
}
/**
* An `AfterEvent` keeper of {@link page current page}.
*
* The `[AfterEvent__symbol]` property is an alias of this one.
*/
abstract readonly read: AfterEvent<[Page]>;
[AfterEvent__symbol](): AfterEvent<[Page]> {
return this.read;
}
/**
* Goes to the previous page in navigation history.
*
* Calling this method is the same as calling `go(-1)`.
*/
back(): void {
this.go(-1);
}
/**
* Goes to the next page in navigation history.
*
* Calling this method is the same as calling `go(1)`.
*/
forward(): void {
this.go(1);
}
/**
* Loads a page from navigation history, identified by its relative location to the current page.
*
* For example `-1` navigates to previous page, while `1` navigates to the next one. If you specify an out-of-bounds
* value (for instance, specifying -1 when there are no previously-visited pages in navigation history), this method
* silently has no effect.
*
* @param delta - Relative location in navigation history to navigate to. The absent value or value of `0` reloads
* the current page.
*/
abstract go(delta?: number): void;
/**
* Reloads current page.
*
* Calling this method is the same as calling `go()`.
*/
reload(): void {
this.go();
}
/**
* Opens a page by navigating to the given `target`.
*
* Appends an entry to navigation history.
*
* @param target - Either navigation target or URL to navigate to.
* @fires PreNavigateEvent#wesib:preNavigate On window object prior to actually navigate.
* Then navigates to the `target`, unless the event cancelled.
* @fires NavigateEvent@wesib:navigate On window object when navigation succeed.
*
* @returns A promise resolved to navigated page, or to `null` otherwise.
*/
abstract open(target: Navigation.Target | string | URL): Promise<Page | null>;
/**
* Replaces current navigation history entry with the given `target`.
*
* @param target - Either navigation target or URL to replace current history entry with.
* @fires PreNavigateEvent#wesib:preNavigate On window object prior to actually update the history.
* Then navigates to the `target`, unless the event cancelled.
* @fires NavigateEvent@wesib:navigate On window object when history updated.
*
* @returns A promise resolved to navigated page, or to `null` otherwise.
*/
abstract replace(target: Navigation.Target | string | URL): Promise<Page | null>;
/**
* Replaces current page URL with the given one.
*
* Does not alter current page state, and does not trigger any events.
*
* @param url - An URL to replace the the current one with.
*
* @returns Current page with updated URL.
*/
abstract update(url: string | URL): Page;
/**
* Creates parameterized navigation instance and assigns a page parameter to apply to target page.
*
* @typeParam T - Parameter value type.
* @typeParam TInput - Parameter input type.
* @param ref - A reference to page navigation parameter to apply.
* @param input - Parameter input to use when constructing its value.
*
* @returns New parameterized navigation instance.
*/
abstract with<T, TInput>(ref: PageParam.Ref<T, TInput>, input: TInput): Navigation.Parameterized;
}
export namespace Navigation {
/**
* Parameterized navigation.
*
* Allows to assign target page parameters prior to navigating to it.
*/
export interface Parameterized {
/**
* Applies parameter to navigation target page.
*
* @typeParam T - Parameter value type.
* @typeParam TInput - Parameter input type.
* @param ref - A reference to page navigation parameter to apply.
* @param input - Parameter input to use when constructing its value.
*
* @returns New parameterized navigation instance.
*/
with<T, TInput>(ref: PageParam.Ref<T, TInput>, input: TInput): Parameterized;
/**
* Opens a page by navigating to the given `target` with provided page parameters.
*
* Appends an entry to navigation history.
*
* @param target - Either navigation target or URL to navigate to. Navigates to current page URL when omitted.
* @fires PreNavigateEvent#wesib:preNavigate On window object prior to actually navigate.
* Then navigates to the `target`, unless the event cancelled.
* @fires NavigateEvent@wesib:navigate On window object when navigation succeed.
*
* @returns A promise resolved to navigated page, or to `null` otherwise.
*/
open(target?: Navigation.Target | string | URL): Promise<Page | null>;
/**
* Replaces the most recent entry in navigation history with the given `target` and provided page parameters.
*
* @param target - Either navigation target or URL to replace the latest history entry with. Navigates to current
* page URL when omitted.
* @fires PreNavigateEvent#wesib:preNavigate On window object prior to actually update the history.
* Then navigates to the `target`, unless the event cancelled.
* @fires NavigateEvent@wesib:navigate On window object when history updated.
*
* @returns A promise resolved to navigated page, or to `null` otherwise.
*/
replace(target?: Navigation.Target | string | URL): Promise<Page | null>;
/**
* Pretends navigation.
*
* Prepares navigation and parameters, but does not actually navigate. Instead it calls the provided callback
* function.
*
* This is useful e.g. to build target URL or evaluate target page parameter.
*
* @param target - Either navigation target or URL to pretend navigation to.
* @param callback - A callback function receiving two pages as parameters: the page to leave, and the page to open.
* The latter one is valid only inside callback, as its parameters will be cleaned up right after callback returns.
* The value returned from callback is then returned from this method call. It may be used to collect some data
* from target page.
*
* @returns Either the value returned by callback, or `undefined` when navigation failed.
*/
pretend<T>(
target: Navigation.Target | string | URL,
callback: (this: void, from: Page, to: Page) => T,
): T | undefined;
/**
* Pretends navigation to the same page.
*
* Prepares navigation and parameters, but does not actually navigate. Instead it calls the provided callback
* function.
*
* This is useful e.g. to build target URL or evaluate target page parameter.
*
* @param callback - A callback function receiving two pages as parameters: the page to leave, and the page to open.
* The latter one is valid only inside callback, as its parameters will be cleaned up right after callback returns.
* The value returned from callback is then returned from this method call. It may be used to collect some data
* from target page.
*
* @returns Either the value returned by callback, or `undefined` when navigation failed.
*/
pretend<T>(callback: (this: void, from: Page, to: Page) => T): T | undefined;
/**
* Pretends navigation and builds navigation target.
*
* Prepares navigation and parameters, but does not actually navigate. Instead it calls the provided callback
* function.
*
* This is useful e.g. to build target URL or evaluate target page parameter.
*
* @param target - Either navigation target or URL to pretend navigation to. Prepends navigation to current page
* when omitted.
*
* @returns Either Navigation target with URL value, or `undefined` when navigation failed.
*/
pretend(target?: Navigation.Target | string | URL): URLTarget | undefined;
}
/**
* Navigation target.
*
* This is passed to {@link Navigation.open} and {@link Navigation.replace} methods.
*/
export interface Target {
/**
* A URL to update the browser location string to.
*/
readonly url?: string | URL | undefined;
/**
* Opaque data to apply to session history. I.e. either push or replace.
*/
readonly data?: unknown | undefined;
/**
* New window title.
*/
readonly title?: string | undefined;
}
/**
* Navigation target with URL value.
*/
export interface URLTarget extends Target {
readonly url: URL;
}
}
function Navigation$byDefault(target: CxEntry.Target<Navigation>): Navigation {
const window = target.get(BootstrapWindow);
const { document, history } = window;
const dispatcher = new DomEventDispatcher(window);
const navHistory = target.get(NavHistory);
const agent = target.get(NavigationAgent);
const nav = trackValue<PageEntry>(navHistory.init());
nav.read(nextEntry => nextEntry.apply()); // The very first page entry receiver applies scheduled updates to page
let next: Promise<any> = Promise.resolve();
dispatcher.on<PopStateEvent>('popstate')(popState => {
const entry = navHistory.popState(popState, nav);
if (entry) {
dispatcher.dispatch(
new EnterPageEvent(NavigationEventType.EnterPage, {
when: popState.state != null ? 'return' : 'enter',
to: entry.page,
}),
);
}
});
dispatcher.on('hashchange')(() => {
const entry = navHistory.hashChange(nav);
if (entry) {
dispatcher.dispatch(
new EnterPageEvent(NavigationEventType.EnterPage, {
when: 'enter',
to: entry.page,
}),
);
}
});
type NavTarget = { -readonly [K in keyof Navigation.URLTarget]: Navigation.URLTarget[K] };
class Navigation$ extends Navigation {
readonly onEnter: OnDomEvent<EnterPageEvent>;
readonly onLeave: OnDomEvent<LeavePageEvent>;
readonly onStay: OnDomEvent<StayOnPageEvent>;
readonly on: OnEvent<[NavigationEvent]>;
readonly read: AfterEvent<[Page]>;
constructor() {
super();
this.onEnter = dispatcher.on<EnterPageEvent>(NavigationEventType.EnterPage);
this.onLeave = dispatcher.on<LeavePageEvent>(NavigationEventType.LeavePage);
this.onStay = dispatcher.on<StayOnPageEvent>(NavigationEventType.StayOnPage);
this.on = onAny<[NavigationEvent]>(
onSupplied(this.onEnter),
onSupplied(this.onLeave),
onSupplied(this.onStay),
);
this.read = nav.read.do(mapAfter(({ page }) => page));
}
get page(): Page {
return nav.it.page;
}
get length(): number {
return history.length;
}
go(delta?: number): void {
history.go(delta);
}
open(target: Navigation.Target | string | URL): Promise<Page | null> {
return navigate('pre-open', 'open', target);
}
replace(target: Navigation.Target | string | URL): Promise<Page | null> {
return navigate('pre-replace', 'replace', target);
}
update(url: string | URL): Page {
return navHistory.update(nav, toURL(url)).page;
}
with<T, TInput>(ref: PageParam.Ref<T, TInput>, input: TInput): Navigation.Parameterized {
return withParam(page => page.put(ref, input));
}
}
return new Navigation$();
function withParam(applyParams: (page: Page) => void): Navigation.Parameterized {
return {
with<T, TInput>(ref: PageParam.Ref<T, TInput>, input: TInput): Navigation.Parameterized {
return withParam(mergeFunctions(applyParams, page => page.put(ref, input)));
},
open(target?: Navigation.Target | string | URL) {
return navigate('pre-open', 'open', target, applyParams);
},
replace(target?: Navigation.Target | string | URL) {
return navigate('pre-replace', 'replace', target, applyParams);
},
pretend<T>(
targetOrCallback?:
| Navigation.Target
| string
| URL
| ((this: void, from: Page, to: Page) => T),
callback: (this: void, from: Page, to: Page) => T = (_from, to) => to as unknown as T,
): T | undefined {
let target: Navigation.Target | string | URL | undefined;
if (typeof targetOrCallback === 'function') {
callback = targetOrCallback;
target = undefined;
} else {
target = targetOrCallback;
}
const navTarget = navTargetOf(target);
const fromEntry = nav.it;
const toEntry = newEntry('pretend', fromEntry, navTarget, applyParams);
try {
return applyAgent('pretend', fromEntry, navTarget, toEntry)
? callback(fromEntry.page, toEntry.page)
: undefined;
} finally {
toEntry.stay(nav.it.page);
}
},
};
}
function toURL(url: string | URL | undefined): URL {
if (typeof url === 'string') {
return new URL(url, document.baseURI);
}
return url || nav.it.page.url;
}
function navTargetOf(target?: Navigation.Target | string | URL): NavTarget {
if (target == null || typeof target === 'string' || target instanceof URL) {
return { url: toURL(target) };
}
if (target.url instanceof URL) {
return target as Navigation.URLTarget;
}
return { ...target, url: toURL(target.url) };
}
function navigate(
whenLeave: 'pre-open' | 'pre-replace',
when: 'open' | 'replace',
target?: Navigation.Target | string | URL,
applyParams: (page: Page) => void = noop,
): Promise<Page | null> {
const navTarget = navTargetOf(target);
const promise = (next = next.then(doNavigate, doNavigate));
return promise;
function doNavigate(): Page | null {
let toEntry: PageEntry | undefined = undefined;
try {
const prepared = prepare();
if (!prepared) {
return prepared; // Navigation cancelled
}
toEntry = prepared;
navHistory[when](toEntry, nav);
dispatcher.dispatch(
new EnterPageEvent(NavigationEventType.EnterPage, {
when,
to: toEntry.page,
}),
);
return toEntry.page;
} catch (e) {
stay(toEntry, e);
throw e;
}
}
function prepare(): PageEntry | null {
if (next !== promise) {
return stay();
}
const fromEntry = nav.it;
const toEntry = newEntry(whenLeave, fromEntry, navTarget, applyParams);
const leavePage = new LeavePageEvent(NavigationEventType.LeavePage, {
when: whenLeave,
from: fromEntry.page,
to: toEntry.page,
});
if (
!dispatcher.dispatch(leavePage)
|| next !== promise
|| !applyAgent(whenLeave, fromEntry, navTarget, toEntry)
) {
return stay(toEntry);
}
return toEntry;
}
function stay(toEntry?: PageEntry, reason?: unknown): null {
if (toEntry) {
toEntry.stay(nav.it.page);
}
dispatcher.dispatch(
new StayOnPageEvent(NavigationEventType.StayOnPage, {
from: nav.it.page,
to: navTarget,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
reason,
}),
);
return null;
}
}
function newEntry(
whenLeave: 'pretend' | 'pre-open' | 'pre-replace',
fromEntry: PageEntry,
navTarget: NavTarget,
applyParams: (page: Page) => void,
): PageEntry {
const toEntry = navHistory.newEntry(navTarget);
try {
fromEntry.transfer(toEntry, whenLeave);
applyParams(toEntry.page);
} catch (e) {
toEntry.stay(nav.it.page);
throw e;
}
return toEntry;
}
function applyAgent(
whenLeave: 'pretend' | 'pre-open' | 'pre-replace',
fromEntry: PageEntry,
navTarget: NavTarget,
toEntry: PageEntry,
): boolean {
let navigated = false;
agent(
({ url, data, title }) => {
navigated = true;
navTarget.url = url;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
navTarget.data = data;
navTarget.title = title;
},
whenLeave,
fromEntry.page,
toEntry.page,
);
return navigated;
}
}