1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import { InjectionToken, NgModule, ErrorHandler, Inject } from '@angular/core';
|
7 | import { NavigationStart, RoutesRecognized, NavigationCancel, NavigationError, NavigationEnd, Router } from '@angular/router';
|
8 | import { select, Store, createSelector } from '@ngrx/store';
|
9 | import { withLatestFrom } from 'rxjs/operators';
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | const ROUTER_REQUEST = '@ngrx/router-store/request';
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | const ROUTER_NAVIGATION = '@ngrx/router-store/navigation';
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | const ROUTER_CANCEL = '@ngrx/router-store/cancel';
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | const ROUTER_ERROR = '@ngrx/router-store/error';
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | const ROUTER_NAVIGATED = '@ngrx/router-store/navigated';
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | function routerReducer(state, action) {
|
52 |
|
53 |
|
54 | const routerAction = ( (action));
|
55 | switch (routerAction.type) {
|
56 | case ROUTER_NAVIGATION:
|
57 | case ROUTER_ERROR:
|
58 | case ROUTER_CANCEL:
|
59 | return {
|
60 | state: routerAction.payload.routerState,
|
61 | navigationId: routerAction.payload.event.id,
|
62 | };
|
63 | default:
|
64 | return ( (state));
|
65 | }
|
66 | }
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | class RouterStateSerializer {
|
77 | }
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | class DefaultRouterStateSerializer {
|
84 | |
85 |
|
86 |
|
87 |
|
88 | serialize(routerState) {
|
89 | return {
|
90 | root: this.serializeRoute(routerState.root),
|
91 | url: routerState.url,
|
92 | };
|
93 | }
|
94 | |
95 |
|
96 |
|
97 |
|
98 |
|
99 | serializeRoute(route) {
|
100 |
|
101 | const children = route.children.map(( |
102 |
|
103 |
|
104 |
|
105 | c => this.serializeRoute(c)));
|
106 | return {
|
107 | params: route.params,
|
108 | paramMap: route.paramMap,
|
109 | data: route.data,
|
110 | url: route.url,
|
111 | outlet: route.outlet,
|
112 | routeConfig: route.routeConfig
|
113 | ? {
|
114 | component: route.routeConfig.component,
|
115 | path: route.routeConfig.path,
|
116 | pathMatch: route.routeConfig.pathMatch,
|
117 | redirectTo: route.routeConfig.redirectTo,
|
118 | outlet: route.routeConfig.outlet,
|
119 | }
|
120 | : null,
|
121 | queryParams: route.queryParams,
|
122 | queryParamMap: route.queryParamMap,
|
123 | fragment: route.fragment,
|
124 | component: ( ((route.routeConfig
|
125 | ? route.routeConfig.component
|
126 | : undefined))),
|
127 | root: ( (undefined)),
|
128 | parent: ( (undefined)),
|
129 | firstChild: children[0],
|
130 | pathFromRoot: ( (undefined)),
|
131 | children,
|
132 | };
|
133 | }
|
134 | }
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | class MinimalRouterStateSerializer {
|
141 | |
142 |
|
143 |
|
144 |
|
145 | serialize(routerState) {
|
146 | return {
|
147 | root: this.serializeRoute(routerState.root),
|
148 | url: routerState.url,
|
149 | };
|
150 | }
|
151 | |
152 |
|
153 |
|
154 |
|
155 |
|
156 | serializeRoute(route) {
|
157 |
|
158 | const children = route.children.map(( |
159 |
|
160 |
|
161 |
|
162 | c => this.serializeRoute(c)));
|
163 | return {
|
164 | params: route.params,
|
165 | data: route.data,
|
166 | url: route.url,
|
167 | outlet: route.outlet,
|
168 | routeConfig: route.routeConfig
|
169 | ? {
|
170 | path: route.routeConfig.path,
|
171 | pathMatch: route.routeConfig.pathMatch,
|
172 | redirectTo: route.routeConfig.redirectTo,
|
173 | outlet: route.routeConfig.outlet,
|
174 | }
|
175 | : null,
|
176 | queryParams: route.queryParams,
|
177 | fragment: route.fragment,
|
178 | firstChild: children[0],
|
179 | children,
|
180 | };
|
181 | }
|
182 | }
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | const NavigationActionTiming = {
|
190 | PreActivation: 1,
|
191 | PostActivation: 2,
|
192 | };
|
193 | NavigationActionTiming[NavigationActionTiming.PreActivation] = 'PreActivation';
|
194 | NavigationActionTiming[NavigationActionTiming.PostActivation] = 'PostActivation';
|
195 |
|
196 | const _ROUTER_CONFIG = new InjectionToken('@ngrx/router-store Internal Configuration');
|
197 |
|
198 | const ROUTER_CONFIG = new InjectionToken('@ngrx/router-store Configuration');
|
199 |
|
200 | const DEFAULT_ROUTER_FEATURENAME = 'router';
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | function _createRouterConfig(config) {
|
206 | return Object.assign({ stateKey: DEFAULT_ROUTER_FEATURENAME, serializer: DefaultRouterStateSerializer, navigationActionTiming: NavigationActionTiming.PreActivation }, config);
|
207 | }
|
208 |
|
209 | const RouterTrigger = {
|
210 | NONE: 1,
|
211 | ROUTER: 2,
|
212 | STORE: 3,
|
213 | };
|
214 | RouterTrigger[RouterTrigger.NONE] = 'NONE';
|
215 | RouterTrigger[RouterTrigger.ROUTER] = 'ROUTER';
|
216 | RouterTrigger[RouterTrigger.STORE] = 'STORE';
|
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 | class StoreRouterConnectingModule {
|
260 | |
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 | constructor(store, router, serializer, errorHandler, config) {
|
268 | this.store = store;
|
269 | this.router = router;
|
270 | this.serializer = serializer;
|
271 | this.errorHandler = errorHandler;
|
272 | this.config = config;
|
273 | this.lastEvent = null;
|
274 | this.trigger = RouterTrigger.NONE;
|
275 | this.stateKey = ( (this.config.stateKey));
|
276 | this.setUpStoreStateListener();
|
277 | this.setUpRouterEventsListener();
|
278 | }
|
279 | |
280 |
|
281 |
|
282 |
|
283 |
|
284 | static forRoot(config = {}) {
|
285 | return {
|
286 | ngModule: StoreRouterConnectingModule,
|
287 | providers: [
|
288 | { provide: _ROUTER_CONFIG, useValue: config },
|
289 | {
|
290 | provide: ROUTER_CONFIG,
|
291 | useFactory: _createRouterConfig,
|
292 | deps: [_ROUTER_CONFIG],
|
293 | },
|
294 | {
|
295 | provide: RouterStateSerializer,
|
296 | useClass: config.serializer
|
297 | ? config.serializer
|
298 | : config.routerState === 1
|
299 | ? MinimalRouterStateSerializer
|
300 | : DefaultRouterStateSerializer,
|
301 | },
|
302 | ],
|
303 | };
|
304 | }
|
305 | |
306 |
|
307 |
|
308 |
|
309 | setUpStoreStateListener() {
|
310 | this.store
|
311 | .pipe(select(( (this.stateKey))), withLatestFrom(this.store))
|
312 | .subscribe(( |
313 |
|
314 |
|
315 |
|
316 | ([routerStoreState, storeState]) => {
|
317 | this.navigateIfNeeded(routerStoreState, storeState);
|
318 | }));
|
319 | }
|
320 | |
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 | navigateIfNeeded(routerStoreState, storeState) {
|
327 | if (!routerStoreState || !routerStoreState.state) {
|
328 | return;
|
329 | }
|
330 | if (this.trigger === RouterTrigger.ROUTER) {
|
331 | return;
|
332 | }
|
333 | if (this.lastEvent instanceof NavigationStart) {
|
334 | return;
|
335 | }
|
336 |
|
337 | const url = routerStoreState.state.url;
|
338 | if (this.router.url !== url) {
|
339 | this.storeState = storeState;
|
340 | this.trigger = RouterTrigger.STORE;
|
341 | this.router.navigateByUrl(url).catch(( |
342 |
|
343 |
|
344 |
|
345 | error => {
|
346 | this.errorHandler.handleError(error);
|
347 | }));
|
348 | }
|
349 | }
|
350 | |
351 |
|
352 |
|
353 |
|
354 | setUpRouterEventsListener() {
|
355 |
|
356 | const dispatchNavLate = this.config.navigationActionTiming ===
|
357 | NavigationActionTiming.PostActivation;
|
358 |
|
359 | let routesRecognized;
|
360 | this.router.events
|
361 | .pipe(withLatestFrom(this.store))
|
362 | .subscribe(( |
363 |
|
364 |
|
365 |
|
366 | ([event, storeState]) => {
|
367 | this.lastEvent = event;
|
368 | if (event instanceof NavigationStart) {
|
369 | this.routerState = this.serializer.serialize(this.router.routerState.snapshot);
|
370 | if (this.trigger !== RouterTrigger.STORE) {
|
371 | this.storeState = storeState;
|
372 | this.dispatchRouterRequest(event);
|
373 | }
|
374 | }
|
375 | else if (event instanceof RoutesRecognized) {
|
376 | routesRecognized = event;
|
377 | if (!dispatchNavLate && this.trigger !== RouterTrigger.STORE) {
|
378 | this.dispatchRouterNavigation(event);
|
379 | }
|
380 | }
|
381 | else if (event instanceof NavigationCancel) {
|
382 | this.dispatchRouterCancel(event);
|
383 | this.reset();
|
384 | }
|
385 | else if (event instanceof NavigationError) {
|
386 | this.dispatchRouterError(event);
|
387 | this.reset();
|
388 | }
|
389 | else if (event instanceof NavigationEnd) {
|
390 | if (this.trigger !== RouterTrigger.STORE) {
|
391 | if (dispatchNavLate) {
|
392 | this.dispatchRouterNavigation(routesRecognized);
|
393 | }
|
394 | this.dispatchRouterNavigated(event);
|
395 | }
|
396 | this.reset();
|
397 | }
|
398 | }));
|
399 | }
|
400 | |
401 |
|
402 |
|
403 |
|
404 |
|
405 | dispatchRouterRequest(event) {
|
406 | this.dispatchRouterAction(ROUTER_REQUEST, { event });
|
407 | }
|
408 | |
409 |
|
410 |
|
411 |
|
412 |
|
413 | dispatchRouterNavigation(lastRoutesRecognized) {
|
414 |
|
415 | const nextRouterState = this.serializer.serialize(lastRoutesRecognized.state);
|
416 | this.dispatchRouterAction(ROUTER_NAVIGATION, {
|
417 | routerState: nextRouterState,
|
418 | event: new RoutesRecognized(lastRoutesRecognized.id, lastRoutesRecognized.url, lastRoutesRecognized.urlAfterRedirects, nextRouterState),
|
419 | });
|
420 | }
|
421 | |
422 |
|
423 |
|
424 |
|
425 |
|
426 | dispatchRouterCancel(event) {
|
427 | this.dispatchRouterAction(ROUTER_CANCEL, {
|
428 | storeState: this.storeState,
|
429 | event,
|
430 | });
|
431 | }
|
432 | |
433 |
|
434 |
|
435 |
|
436 |
|
437 | dispatchRouterError(event) {
|
438 | this.dispatchRouterAction(ROUTER_ERROR, {
|
439 | storeState: this.storeState,
|
440 | event: new NavigationError(event.id, event.url, `${event}`),
|
441 | });
|
442 | }
|
443 | |
444 |
|
445 |
|
446 |
|
447 |
|
448 | dispatchRouterNavigated(event) {
|
449 |
|
450 | const routerState = this.serializer.serialize(this.router.routerState.snapshot);
|
451 | this.dispatchRouterAction(ROUTER_NAVIGATED, { event, routerState });
|
452 | }
|
453 | |
454 |
|
455 |
|
456 |
|
457 |
|
458 |
|
459 | dispatchRouterAction(type, payload) {
|
460 | this.trigger = RouterTrigger.ROUTER;
|
461 | try {
|
462 | this.store.dispatch({
|
463 | type,
|
464 | payload: Object.assign({ routerState: this.routerState }, payload, { event: this.config.routerState === 1
|
465 | ? { id: payload.event.id, url: payload.event.url }
|
466 | : payload.event }),
|
467 | });
|
468 | }
|
469 | finally {
|
470 | this.trigger = RouterTrigger.NONE;
|
471 | }
|
472 | }
|
473 | |
474 |
|
475 |
|
476 |
|
477 | reset() {
|
478 | this.trigger = RouterTrigger.NONE;
|
479 | this.storeState = null;
|
480 | this.routerState = null;
|
481 | }
|
482 | }
|
483 | StoreRouterConnectingModule.decorators = [
|
484 | { type: NgModule, args: [{},] }
|
485 | ];
|
486 |
|
487 | StoreRouterConnectingModule.ctorParameters = () => [
|
488 | { type: Store },
|
489 | { type: Router },
|
490 | { type: RouterStateSerializer },
|
491 | { type: ErrorHandler },
|
492 | { type: undefined, decorators: [{ type: Inject, args: [ROUTER_CONFIG,] }] }
|
493 | ];
|
494 |
|
495 |
|
496 |
|
497 |
|
498 |
|
499 |
|
500 |
|
501 |
|
502 |
|
503 |
|
504 | function getSelectors(selectState) {
|
505 |
|
506 | const selectRouterState = createSelector(selectState, ( |
507 |
|
508 |
|
509 |
|
510 | router => router && router.state));
|
511 |
|
512 | const selectCurrentRoute = createSelector(selectRouterState, ( |
513 |
|
514 |
|
515 |
|
516 | routerState => {
|
517 | if (!routerState) {
|
518 | return undefined;
|
519 | }
|
520 |
|
521 | let route = routerState.root;
|
522 | while (route.firstChild) {
|
523 | route = route.firstChild;
|
524 | }
|
525 | return route;
|
526 | }));
|
527 |
|
528 | const selectQueryParams = createSelector(selectCurrentRoute, ( |
529 |
|
530 |
|
531 |
|
532 | route => route && route.queryParams));
|
533 |
|
534 | const selectQueryParam = ( |
535 |
|
536 |
|
537 |
|
538 | (param) => createSelector(selectQueryParams, ( |
539 |
|
540 |
|
541 |
|
542 | params => params && params[param])));
|
543 |
|
544 | const selectRouteParams = createSelector(selectCurrentRoute, ( |
545 |
|
546 |
|
547 |
|
548 | route => route && route.params));
|
549 |
|
550 | const selectRouteParam = ( |
551 |
|
552 |
|
553 |
|
554 | (param) => createSelector(selectRouteParams, ( |
555 |
|
556 |
|
557 |
|
558 | params => params && params[param])));
|
559 |
|
560 | const selectRouteData = createSelector(selectCurrentRoute, ( |
561 |
|
562 |
|
563 |
|
564 | route => route && route.data));
|
565 |
|
566 | const selectUrl = createSelector(selectRouterState, ( |
567 |
|
568 |
|
569 |
|
570 | routerState => routerState && routerState.url));
|
571 | return {
|
572 | selectCurrentRoute,
|
573 | selectQueryParams,
|
574 | selectQueryParam,
|
575 | selectRouteParams,
|
576 | selectRouteParam,
|
577 | selectRouteData,
|
578 | selectUrl,
|
579 | };
|
580 | }
|
581 |
|
582 |
|
583 |
|
584 |
|
585 |
|
586 |
|
587 |
|
588 |
|
589 |
|
590 |
|
591 |
|
592 |
|
593 |
|
594 |
|
595 |
|
596 |
|
597 |
|
598 |
|
599 |
|
600 |
|
601 | export { _ROUTER_CONFIG as ɵngrx_modules_router_store_router_store_a, _createRouterConfig as ɵngrx_modules_router_store_router_store_b, ROUTER_ERROR, ROUTER_CANCEL, ROUTER_NAVIGATION, ROUTER_NAVIGATED, ROUTER_REQUEST, routerReducer, StoreRouterConnectingModule, NavigationActionTiming, ROUTER_CONFIG, DEFAULT_ROUTER_FEATURENAME, RouterStateSerializer, DefaultRouterStateSerializer, MinimalRouterStateSerializer, getSelectors };
|
602 |
|