UNPKG

18.3 kBJavaScriptView Raw
1/**
2 * @license NgRx 8.5.2
3 * (c) 2015-2018 Brandon Roberts, Mike Ryan, Rob Wormald, Victor Savkin
4 * License: MIT
5 */
6(function (global, factory) {
7 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('tslib'), require('@angular/core'), require('@angular/router'), require('@ngrx/store'), require('rxjs/operators')) :
8 typeof define === 'function' && define.amd ? define('@ngrx/router-store', ['exports', 'tslib', '@angular/core', '@angular/router', '@ngrx/store', 'rxjs/operators'], factory) :
9 (global = global || self, factory((global.ngrx = global.ngrx || {}, global.ngrx.routerStore = {}), global.tslib, global.ng.core, global.ng.router, global['@ngrx/store'], global.rxjs.operators));
10}(this, function (exports, tslib_1, core, router, store, operators) { 'use strict';
11
12 /**
13 * An action dispatched when a router navigation request is fired.
14 */
15 var ROUTER_REQUEST = '@ngrx/router-store/request';
16 /**
17 * An action dispatched when the router navigates.
18 */
19 var ROUTER_NAVIGATION = '@ngrx/router-store/navigation';
20 /**
21 * An action dispatched when the router cancels navigation.
22 */
23 var ROUTER_CANCEL = '@ngrx/router-store/cancel';
24 /**
25 * An action dispatched when the router errors.
26 */
27 var ROUTER_ERROR = '@ngrx/router-store/error';
28 /**
29 * An action dispatched after navigation has ended and new route is active.
30 */
31 var ROUTER_NAVIGATED = '@ngrx/router-store/navigated';
32
33 function routerReducer(state, action) {
34 // Allow compilation with strictFunctionTypes - ref: #1344
35 var routerAction = action;
36 switch (routerAction.type) {
37 case ROUTER_NAVIGATION:
38 case ROUTER_ERROR:
39 case ROUTER_CANCEL:
40 return {
41 state: routerAction.payload.routerState,
42 navigationId: routerAction.payload.event.id,
43 };
44 default:
45 return state;
46 }
47 }
48
49 var RouterStateSerializer = /** @class */ (function () {
50 function RouterStateSerializer() {
51 }
52 return RouterStateSerializer;
53 }());
54
55 var DefaultRouterStateSerializer = /** @class */ (function () {
56 function DefaultRouterStateSerializer() {
57 }
58 DefaultRouterStateSerializer.prototype.serialize = function (routerState) {
59 return {
60 root: this.serializeRoute(routerState.root),
61 url: routerState.url,
62 };
63 };
64 DefaultRouterStateSerializer.prototype.serializeRoute = function (route) {
65 var _this = this;
66 var children = route.children.map(function (c) { return _this.serializeRoute(c); });
67 return {
68 params: route.params,
69 paramMap: route.paramMap,
70 data: route.data,
71 url: route.url,
72 outlet: route.outlet,
73 routeConfig: route.routeConfig
74 ? {
75 component: route.routeConfig.component,
76 path: route.routeConfig.path,
77 pathMatch: route.routeConfig.pathMatch,
78 redirectTo: route.routeConfig.redirectTo,
79 outlet: route.routeConfig.outlet,
80 }
81 : null,
82 queryParams: route.queryParams,
83 queryParamMap: route.queryParamMap,
84 fragment: route.fragment,
85 component: (route.routeConfig
86 ? route.routeConfig.component
87 : undefined),
88 root: undefined,
89 parent: undefined,
90 firstChild: children[0],
91 pathFromRoot: undefined,
92 children: children,
93 };
94 };
95 return DefaultRouterStateSerializer;
96 }());
97
98 var MinimalRouterStateSerializer = /** @class */ (function () {
99 function MinimalRouterStateSerializer() {
100 }
101 MinimalRouterStateSerializer.prototype.serialize = function (routerState) {
102 return {
103 root: this.serializeRoute(routerState.root),
104 url: routerState.url,
105 };
106 };
107 MinimalRouterStateSerializer.prototype.serializeRoute = function (route) {
108 var _this = this;
109 var children = route.children.map(function (c) { return _this.serializeRoute(c); });
110 return {
111 params: route.params,
112 data: route.data,
113 url: route.url,
114 outlet: route.outlet,
115 routeConfig: route.routeConfig
116 ? {
117 path: route.routeConfig.path,
118 pathMatch: route.routeConfig.pathMatch,
119 redirectTo: route.routeConfig.redirectTo,
120 outlet: route.routeConfig.outlet,
121 }
122 : null,
123 queryParams: route.queryParams,
124 fragment: route.fragment,
125 firstChild: children[0],
126 children: children,
127 };
128 };
129 return MinimalRouterStateSerializer;
130 }());
131
132 (function (NavigationActionTiming) {
133 NavigationActionTiming[NavigationActionTiming["PreActivation"] = 1] = "PreActivation";
134 NavigationActionTiming[NavigationActionTiming["PostActivation"] = 2] = "PostActivation";
135 })(exports.NavigationActionTiming || (exports.NavigationActionTiming = {}));
136 var _ROUTER_CONFIG = new core.InjectionToken('@ngrx/router-store Internal Configuration');
137 var ROUTER_CONFIG = new core.InjectionToken('@ngrx/router-store Configuration');
138 var DEFAULT_ROUTER_FEATURENAME = 'router';
139 function _createRouterConfig(config) {
140 return tslib_1.__assign({ stateKey: DEFAULT_ROUTER_FEATURENAME, serializer: DefaultRouterStateSerializer, navigationActionTiming: exports.NavigationActionTiming.PreActivation }, config);
141 }
142 var RouterTrigger;
143 (function (RouterTrigger) {
144 RouterTrigger[RouterTrigger["NONE"] = 1] = "NONE";
145 RouterTrigger[RouterTrigger["ROUTER"] = 2] = "ROUTER";
146 RouterTrigger[RouterTrigger["STORE"] = 3] = "STORE";
147 })(RouterTrigger || (RouterTrigger = {}));
148 /**
149 * Connects RouterModule with StoreModule.
150 *
151 * During the navigation, before any guards or resolvers run, the router will dispatch
152 * a ROUTER_NAVIGATION action, which has the following signature:
153 *
154 * ```
155 * export type RouterNavigationPayload = {
156 * routerState: SerializedRouterStateSnapshot,
157 * event: RoutesRecognized
158 * }
159 * ```
160 *
161 * Either a reducer or an effect can be invoked in response to this action.
162 * If the invoked reducer throws, the navigation will be canceled.
163 *
164 * If navigation gets canceled because of a guard, a ROUTER_CANCEL action will be
165 * dispatched. If navigation results in an error, a ROUTER_ERROR action will be dispatched.
166 *
167 * Both ROUTER_CANCEL and ROUTER_ERROR contain the store state before the navigation
168 * which can be used to restore the consistency of the store.
169 *
170 * Usage:
171 *
172 * ```typescript
173 * @NgModule({
174 * declarations: [AppCmp, SimpleCmp],
175 * imports: [
176 * BrowserModule,
177 * StoreModule.forRoot(mapOfReducers),
178 * RouterModule.forRoot([
179 * { path: '', component: SimpleCmp },
180 * { path: 'next', component: SimpleCmp }
181 * ]),
182 * StoreRouterConnectingModule.forRoot()
183 * ],
184 * bootstrap: [AppCmp]
185 * })
186 * export class AppModule {
187 * }
188 * ```
189 */
190 var StoreRouterConnectingModule = /** @class */ (function () {
191 function StoreRouterConnectingModule(store, router, serializer, errorHandler, config) {
192 this.store = store;
193 this.router = router;
194 this.serializer = serializer;
195 this.errorHandler = errorHandler;
196 this.config = config;
197 this.lastEvent = null;
198 this.trigger = RouterTrigger.NONE;
199 this.stateKey = this.config.stateKey;
200 this.setUpStoreStateListener();
201 this.setUpRouterEventsListener();
202 }
203 StoreRouterConnectingModule_1 = StoreRouterConnectingModule;
204 StoreRouterConnectingModule.forRoot = function (config) {
205 if (config === void 0) { config = {}; }
206 return {
207 ngModule: StoreRouterConnectingModule_1,
208 providers: [
209 { provide: _ROUTER_CONFIG, useValue: config },
210 {
211 provide: ROUTER_CONFIG,
212 useFactory: _createRouterConfig,
213 deps: [_ROUTER_CONFIG],
214 },
215 {
216 provide: RouterStateSerializer,
217 useClass: config.serializer
218 ? config.serializer
219 : config.routerState === 1 /* Minimal */
220 ? MinimalRouterStateSerializer
221 : DefaultRouterStateSerializer,
222 },
223 ],
224 };
225 };
226 StoreRouterConnectingModule.prototype.setUpStoreStateListener = function () {
227 var _this = this;
228 this.store
229 .pipe(store.select(this.stateKey), operators.withLatestFrom(this.store))
230 .subscribe(function (_a) {
231 var _b = tslib_1.__read(_a, 2), routerStoreState = _b[0], storeState = _b[1];
232 _this.navigateIfNeeded(routerStoreState, storeState);
233 });
234 };
235 StoreRouterConnectingModule.prototype.navigateIfNeeded = function (routerStoreState, storeState) {
236 var _this = this;
237 if (!routerStoreState || !routerStoreState.state) {
238 return;
239 }
240 if (this.trigger === RouterTrigger.ROUTER) {
241 return;
242 }
243 if (this.lastEvent instanceof router.NavigationStart) {
244 return;
245 }
246 var url = routerStoreState.state.url;
247 if (this.router.url !== url) {
248 this.storeState = storeState;
249 this.trigger = RouterTrigger.STORE;
250 this.router.navigateByUrl(url).catch(function (error) {
251 _this.errorHandler.handleError(error);
252 });
253 }
254 };
255 StoreRouterConnectingModule.prototype.setUpRouterEventsListener = function () {
256 var _this = this;
257 var dispatchNavLate = this.config.navigationActionTiming ===
258 exports.NavigationActionTiming.PostActivation;
259 var routesRecognized;
260 this.router.events
261 .pipe(operators.withLatestFrom(this.store))
262 .subscribe(function (_a) {
263 var _b = tslib_1.__read(_a, 2), event = _b[0], storeState = _b[1];
264 _this.lastEvent = event;
265 if (event instanceof router.NavigationStart) {
266 _this.routerState = _this.serializer.serialize(_this.router.routerState.snapshot);
267 if (_this.trigger !== RouterTrigger.STORE) {
268 _this.storeState = storeState;
269 _this.dispatchRouterRequest(event);
270 }
271 }
272 else if (event instanceof router.RoutesRecognized) {
273 routesRecognized = event;
274 if (!dispatchNavLate && _this.trigger !== RouterTrigger.STORE) {
275 _this.dispatchRouterNavigation(event);
276 }
277 }
278 else if (event instanceof router.NavigationCancel) {
279 _this.dispatchRouterCancel(event);
280 _this.reset();
281 }
282 else if (event instanceof router.NavigationError) {
283 _this.dispatchRouterError(event);
284 _this.reset();
285 }
286 else if (event instanceof router.NavigationEnd) {
287 if (_this.trigger !== RouterTrigger.STORE) {
288 if (dispatchNavLate) {
289 _this.dispatchRouterNavigation(routesRecognized);
290 }
291 _this.dispatchRouterNavigated(event);
292 }
293 _this.reset();
294 }
295 });
296 };
297 StoreRouterConnectingModule.prototype.dispatchRouterRequest = function (event) {
298 this.dispatchRouterAction(ROUTER_REQUEST, { event: event });
299 };
300 StoreRouterConnectingModule.prototype.dispatchRouterNavigation = function (lastRoutesRecognized) {
301 var nextRouterState = this.serializer.serialize(lastRoutesRecognized.state);
302 this.dispatchRouterAction(ROUTER_NAVIGATION, {
303 routerState: nextRouterState,
304 event: new router.RoutesRecognized(lastRoutesRecognized.id, lastRoutesRecognized.url, lastRoutesRecognized.urlAfterRedirects, nextRouterState),
305 });
306 };
307 StoreRouterConnectingModule.prototype.dispatchRouterCancel = function (event) {
308 this.dispatchRouterAction(ROUTER_CANCEL, {
309 storeState: this.storeState,
310 event: event,
311 });
312 };
313 StoreRouterConnectingModule.prototype.dispatchRouterError = function (event) {
314 this.dispatchRouterAction(ROUTER_ERROR, {
315 storeState: this.storeState,
316 event: new router.NavigationError(event.id, event.url, "" + event),
317 });
318 };
319 StoreRouterConnectingModule.prototype.dispatchRouterNavigated = function (event) {
320 var routerState = this.serializer.serialize(this.router.routerState.snapshot);
321 this.dispatchRouterAction(ROUTER_NAVIGATED, { event: event, routerState: routerState });
322 };
323 StoreRouterConnectingModule.prototype.dispatchRouterAction = function (type, payload) {
324 this.trigger = RouterTrigger.ROUTER;
325 try {
326 this.store.dispatch({
327 type: type,
328 payload: tslib_1.__assign({ routerState: this.routerState }, payload, { event: this.config.routerState === 1 /* Minimal */
329 ? { id: payload.event.id, url: payload.event.url }
330 : payload.event }),
331 });
332 }
333 finally {
334 this.trigger = RouterTrigger.NONE;
335 }
336 };
337 StoreRouterConnectingModule.prototype.reset = function () {
338 this.trigger = RouterTrigger.NONE;
339 this.storeState = null;
340 this.routerState = null;
341 };
342 var StoreRouterConnectingModule_1;
343 StoreRouterConnectingModule = StoreRouterConnectingModule_1 = tslib_1.__decorate([
344 core.NgModule({}),
345 tslib_1.__param(4, core.Inject(ROUTER_CONFIG)),
346 tslib_1.__metadata("design:paramtypes", [store.Store,
347 router.Router,
348 RouterStateSerializer,
349 core.ErrorHandler, Object])
350 ], StoreRouterConnectingModule);
351 return StoreRouterConnectingModule;
352 }());
353
354 function getSelectors(selectState) {
355 var selectRouterState = store.createSelector(selectState, function (router) { return router && router.state; });
356 var selectCurrentRoute = store.createSelector(selectRouterState, function (routerState) {
357 if (!routerState) {
358 return undefined;
359 }
360 var route = routerState.root;
361 while (route.firstChild) {
362 route = route.firstChild;
363 }
364 return route;
365 });
366 var selectQueryParams = store.createSelector(selectCurrentRoute, function (route) { return route && route.queryParams; });
367 var selectQueryParam = function (param) {
368 return store.createSelector(selectQueryParams, function (params) { return params && params[param]; });
369 };
370 var selectRouteParams = store.createSelector(selectCurrentRoute, function (route) { return route && route.params; });
371 var selectRouteParam = function (param) {
372 return store.createSelector(selectRouteParams, function (params) { return params && params[param]; });
373 };
374 var selectRouteData = store.createSelector(selectCurrentRoute, function (route) { return route && route.data; });
375 var selectUrl = store.createSelector(selectRouterState, function (routerState) { return routerState && routerState.url; });
376 return {
377 selectCurrentRoute: selectCurrentRoute,
378 selectQueryParams: selectQueryParams,
379 selectQueryParam: selectQueryParam,
380 selectRouteParams: selectRouteParams,
381 selectRouteParam: selectRouteParam,
382 selectRouteData: selectRouteData,
383 selectUrl: selectUrl,
384 };
385 }
386
387 /**
388 * DO NOT EDIT
389 *
390 * This file is automatically generated at build
391 */
392
393 /**
394 * Generated bundle index. Do not edit.
395 */
396
397 exports.ɵngrx_modules_router_store_router_store_a = _ROUTER_CONFIG;
398 exports.ɵngrx_modules_router_store_router_store_b = _createRouterConfig;
399 exports.ROUTER_ERROR = ROUTER_ERROR;
400 exports.ROUTER_CANCEL = ROUTER_CANCEL;
401 exports.ROUTER_NAVIGATION = ROUTER_NAVIGATION;
402 exports.ROUTER_NAVIGATED = ROUTER_NAVIGATED;
403 exports.ROUTER_REQUEST = ROUTER_REQUEST;
404 exports.routerReducer = routerReducer;
405 exports.StoreRouterConnectingModule = StoreRouterConnectingModule;
406 exports.ROUTER_CONFIG = ROUTER_CONFIG;
407 exports.DEFAULT_ROUTER_FEATURENAME = DEFAULT_ROUTER_FEATURENAME;
408 exports.RouterStateSerializer = RouterStateSerializer;
409 exports.DefaultRouterStateSerializer = DefaultRouterStateSerializer;
410 exports.MinimalRouterStateSerializer = MinimalRouterStateSerializer;
411 exports.getSelectors = getSelectors;
412
413 Object.defineProperty(exports, '__esModule', { value: true });
414
415}));
416//# sourceMappingURL=router-store.umd.js.map