UNPKG

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