@ngxs/router-plugin 3.7.3 → 3.7.4-dev.master-327646e

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,709 +4,735 @@ import { __decorate, __metadata } from 'tslib';
4
4
  import { NavigationStart, RoutesRecognized, ResolveEnd, NavigationCancel, NavigationError, NavigationEnd, Router, UrlSerializer } from '@angular/router';
5
5
  import { LocationStrategy, Location } from '@angular/common';
6
6
  import { isAngularInTestMode } from '@ngxs/store/internals';
7
+ import { Subscription } from 'rxjs';
7
8
  import { first } from 'rxjs/operators';
8
9
 
9
- /**
10
- * @fileoverview added by tsickle
11
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
12
- */
13
- /**
14
- * Public event api of the router
15
- */
16
- class Navigate {
17
- /**
18
- * @param {?} path
19
- * @param {?=} queryParams
20
- * @param {?=} extras
21
- */
22
- constructor(path, queryParams, extras) {
23
- this.path = path;
24
- this.queryParams = queryParams;
25
- this.extras = extras;
26
- }
27
- /**
28
- * @return {?}
29
- */
30
- static get type() {
31
- // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
32
- return '[Router] Navigate';
33
- }
34
- }
35
- if (false) {
36
- /** @type {?} */
37
- Navigate.prototype.path;
38
- /** @type {?} */
39
- Navigate.prototype.queryParams;
40
- /** @type {?} */
41
- Navigate.prototype.extras;
42
- }
43
- /**
44
- *
45
- * Angular Routers internal state events
46
- *
47
- */
48
- /**
49
- * An action dispatched when the router navigates.
50
- * @template T
51
- */
52
- class RouterNavigation {
53
- /**
54
- * @param {?} routerState
55
- * @param {?} event
56
- * @param {?=} trigger
57
- */
58
- constructor(routerState, event, trigger = 'none') {
59
- this.routerState = routerState;
60
- this.event = event;
61
- this.trigger = trigger;
62
- }
63
- /**
64
- * @return {?}
65
- */
66
- static get type() {
67
- // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
68
- return '[Router] RouterNavigation';
69
- }
70
- }
71
- if (false) {
72
- /** @type {?} */
73
- RouterNavigation.prototype.routerState;
74
- /** @type {?} */
75
- RouterNavigation.prototype.event;
76
- /** @type {?} */
77
- RouterNavigation.prototype.trigger;
78
- }
79
- /**
80
- * An action dispatched when the router cancel navigation.
81
- * @template T, V
82
- */
83
- class RouterCancel {
84
- /**
85
- * @param {?} routerState
86
- * @param {?} storeState
87
- * @param {?} event
88
- * @param {?=} trigger
89
- */
90
- constructor(routerState, storeState, event, trigger = 'none') {
91
- this.routerState = routerState;
92
- this.storeState = storeState;
93
- this.event = event;
94
- this.trigger = trigger;
95
- }
96
- /**
97
- * @return {?}
98
- */
99
- static get type() {
100
- // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
101
- return '[Router] RouterCancel';
102
- }
103
- }
104
- if (false) {
105
- /** @type {?} */
106
- RouterCancel.prototype.routerState;
107
- /** @type {?} */
108
- RouterCancel.prototype.storeState;
109
- /** @type {?} */
110
- RouterCancel.prototype.event;
111
- /** @type {?} */
112
- RouterCancel.prototype.trigger;
113
- }
114
- /**
115
- * An action dispatched when the router errors.
116
- * @template T, V
117
- */
118
- class RouterError {
119
- /**
120
- * @param {?} routerState
121
- * @param {?} storeState
122
- * @param {?} event
123
- * @param {?=} trigger
124
- */
125
- constructor(routerState, storeState, event, trigger = 'none') {
126
- this.routerState = routerState;
127
- this.storeState = storeState;
128
- this.event = event;
129
- this.trigger = trigger;
130
- }
131
- /**
132
- * @return {?}
133
- */
134
- static get type() {
135
- // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
136
- return '[Router] RouterError';
137
- }
138
- }
139
- if (false) {
140
- /** @type {?} */
141
- RouterError.prototype.routerState;
142
- /** @type {?} */
143
- RouterError.prototype.storeState;
144
- /** @type {?} */
145
- RouterError.prototype.event;
146
- /** @type {?} */
147
- RouterError.prototype.trigger;
148
- }
149
- /**
150
- * An action dispatched when the `ResolveEnd` event is triggered.
151
- * @template T
152
- */
153
- class RouterDataResolved {
154
- /**
155
- * @param {?} routerState
156
- * @param {?} event
157
- * @param {?=} trigger
158
- */
159
- constructor(routerState, event, trigger = 'none') {
160
- this.routerState = routerState;
161
- this.event = event;
162
- this.trigger = trigger;
163
- }
164
- /**
165
- * @return {?}
166
- */
167
- static get type() {
168
- // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
169
- return '[Router] RouterDataResolved';
170
- }
171
- }
172
- if (false) {
173
- /** @type {?} */
174
- RouterDataResolved.prototype.routerState;
175
- /** @type {?} */
176
- RouterDataResolved.prototype.event;
177
- /** @type {?} */
178
- RouterDataResolved.prototype.trigger;
10
+ /**
11
+ * @fileoverview added by tsickle
12
+ * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
13
+ */
14
+ /**
15
+ * Public event api of the router
16
+ */
17
+ class Navigate {
18
+ /**
19
+ * @param {?} path
20
+ * @param {?=} queryParams
21
+ * @param {?=} extras
22
+ */
23
+ constructor(path, queryParams, extras) {
24
+ this.path = path;
25
+ this.queryParams = queryParams;
26
+ this.extras = extras;
27
+ }
28
+ /**
29
+ * @return {?}
30
+ */
31
+ static get type() {
32
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
33
+ return '[Router] Navigate';
34
+ }
35
+ }
36
+ if (false) {
37
+ /** @type {?} */
38
+ Navigate.prototype.path;
39
+ /** @type {?} */
40
+ Navigate.prototype.queryParams;
41
+ /** @type {?} */
42
+ Navigate.prototype.extras;
43
+ }
44
+ /**
45
+ *
46
+ * Angular Routers internal state events
47
+ *
48
+ */
49
+ /**
50
+ * An action dispatched when the router navigates.
51
+ * @template T
52
+ */
53
+ class RouterNavigation {
54
+ /**
55
+ * @param {?} routerState
56
+ * @param {?} event
57
+ * @param {?=} trigger
58
+ */
59
+ constructor(routerState, event, trigger = 'none') {
60
+ this.routerState = routerState;
61
+ this.event = event;
62
+ this.trigger = trigger;
63
+ }
64
+ /**
65
+ * @return {?}
66
+ */
67
+ static get type() {
68
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
69
+ return '[Router] RouterNavigation';
70
+ }
71
+ }
72
+ if (false) {
73
+ /** @type {?} */
74
+ RouterNavigation.prototype.routerState;
75
+ /** @type {?} */
76
+ RouterNavigation.prototype.event;
77
+ /** @type {?} */
78
+ RouterNavigation.prototype.trigger;
79
+ }
80
+ /**
81
+ * An action dispatched when the router cancel navigation.
82
+ * @template T, V
83
+ */
84
+ class RouterCancel {
85
+ /**
86
+ * @param {?} routerState
87
+ * @param {?} storeState
88
+ * @param {?} event
89
+ * @param {?=} trigger
90
+ */
91
+ constructor(routerState, storeState, event, trigger = 'none') {
92
+ this.routerState = routerState;
93
+ this.storeState = storeState;
94
+ this.event = event;
95
+ this.trigger = trigger;
96
+ }
97
+ /**
98
+ * @return {?}
99
+ */
100
+ static get type() {
101
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
102
+ return '[Router] RouterCancel';
103
+ }
104
+ }
105
+ if (false) {
106
+ /** @type {?} */
107
+ RouterCancel.prototype.routerState;
108
+ /** @type {?} */
109
+ RouterCancel.prototype.storeState;
110
+ /** @type {?} */
111
+ RouterCancel.prototype.event;
112
+ /** @type {?} */
113
+ RouterCancel.prototype.trigger;
114
+ }
115
+ /**
116
+ * An action dispatched when the router errors.
117
+ * @template T, V
118
+ */
119
+ class RouterError {
120
+ /**
121
+ * @param {?} routerState
122
+ * @param {?} storeState
123
+ * @param {?} event
124
+ * @param {?=} trigger
125
+ */
126
+ constructor(routerState, storeState, event, trigger = 'none') {
127
+ this.routerState = routerState;
128
+ this.storeState = storeState;
129
+ this.event = event;
130
+ this.trigger = trigger;
131
+ }
132
+ /**
133
+ * @return {?}
134
+ */
135
+ static get type() {
136
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
137
+ return '[Router] RouterError';
138
+ }
139
+ }
140
+ if (false) {
141
+ /** @type {?} */
142
+ RouterError.prototype.routerState;
143
+ /** @type {?} */
144
+ RouterError.prototype.storeState;
145
+ /** @type {?} */
146
+ RouterError.prototype.event;
147
+ /** @type {?} */
148
+ RouterError.prototype.trigger;
149
+ }
150
+ /**
151
+ * An action dispatched when the `ResolveEnd` event is triggered.
152
+ * @template T
153
+ */
154
+ class RouterDataResolved {
155
+ /**
156
+ * @param {?} routerState
157
+ * @param {?} event
158
+ * @param {?=} trigger
159
+ */
160
+ constructor(routerState, event, trigger = 'none') {
161
+ this.routerState = routerState;
162
+ this.event = event;
163
+ this.trigger = trigger;
164
+ }
165
+ /**
166
+ * @return {?}
167
+ */
168
+ static get type() {
169
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
170
+ return '[Router] RouterDataResolved';
171
+ }
172
+ }
173
+ if (false) {
174
+ /** @type {?} */
175
+ RouterDataResolved.prototype.routerState;
176
+ /** @type {?} */
177
+ RouterDataResolved.prototype.event;
178
+ /** @type {?} */
179
+ RouterDataResolved.prototype.trigger;
179
180
  }
180
181
 
181
- /**
182
- * @fileoverview added by tsickle
183
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
184
- */
185
- /**
186
- * @abstract
187
- * @template T
188
- */
189
- class RouterStateSerializer {
190
- }
191
- if (false) {
192
- /**
193
- * @abstract
194
- * @param {?} routerState
195
- * @return {?}
196
- */
197
- RouterStateSerializer.prototype.serialize = function (routerState) { };
198
- }
199
- /**
200
- * @record
201
- */
202
- function SerializedRouterStateSnapshot() { }
203
- if (false) {
204
- /** @type {?} */
205
- SerializedRouterStateSnapshot.prototype.root;
206
- /** @type {?} */
207
- SerializedRouterStateSnapshot.prototype.url;
208
- }
209
- class DefaultRouterStateSerializer {
210
- /**
211
- * @param {?} routerState
212
- * @return {?}
213
- */
214
- serialize(routerState) {
215
- return {
216
- root: this.serializeRoute(routerState.root),
217
- url: routerState.url
218
- };
219
- }
220
- /**
221
- * @private
222
- * @param {?} route
223
- * @return {?}
224
- */
225
- serializeRoute(route) {
226
- /** @type {?} */
227
- const children = route.children.map((/**
228
- * @param {?} c
229
- * @return {?}
230
- */
231
- c => this.serializeRoute(c)));
232
- return {
233
- url: route.url,
234
- params: route.params,
235
- queryParams: route.queryParams,
236
- fragment: route.fragment,
237
- data: route.data,
238
- outlet: route.outlet,
239
- component: null,
240
- routeConfig: null,
241
- root: (/** @type {?} */ (null)),
242
- parent: null,
243
- firstChild: children[0],
244
- children: children,
245
- pathFromRoot: (/** @type {?} */ (null)),
246
- paramMap: route.paramMap,
247
- queryParamMap: route.queryParamMap,
248
- toString: route.toString
249
- };
250
- }
182
+ /**
183
+ * @fileoverview added by tsickle
184
+ * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
185
+ */
186
+ /**
187
+ * @abstract
188
+ * @template T
189
+ */
190
+ class RouterStateSerializer {
191
+ }
192
+ if (false) {
193
+ /**
194
+ * @abstract
195
+ * @param {?} routerState
196
+ * @return {?}
197
+ */
198
+ RouterStateSerializer.prototype.serialize = function (routerState) { };
199
+ }
200
+ /**
201
+ * @record
202
+ */
203
+ function SerializedRouterStateSnapshot() { }
204
+ if (false) {
205
+ /** @type {?} */
206
+ SerializedRouterStateSnapshot.prototype.root;
207
+ /** @type {?} */
208
+ SerializedRouterStateSnapshot.prototype.url;
209
+ }
210
+ class DefaultRouterStateSerializer {
211
+ /**
212
+ * @param {?} routerState
213
+ * @return {?}
214
+ */
215
+ serialize(routerState) {
216
+ return {
217
+ root: this.serializeRoute(routerState.root),
218
+ url: routerState.url
219
+ };
220
+ }
221
+ /**
222
+ * @private
223
+ * @param {?} route
224
+ * @return {?}
225
+ */
226
+ serializeRoute(route) {
227
+ /** @type {?} */
228
+ const children = route.children.map((/**
229
+ * @param {?} c
230
+ * @return {?}
231
+ */
232
+ c => this.serializeRoute(c)));
233
+ return {
234
+ url: route.url,
235
+ params: route.params,
236
+ queryParams: route.queryParams,
237
+ fragment: route.fragment,
238
+ data: route.data,
239
+ outlet: route.outlet,
240
+ component: null,
241
+ routeConfig: null,
242
+ root: (/** @type {?} */ (null)),
243
+ parent: null,
244
+ firstChild: children[0],
245
+ children: children,
246
+ pathFromRoot: (/** @type {?} */ (null)),
247
+ paramMap: route.paramMap,
248
+ queryParamMap: route.queryParamMap,
249
+ toString: route.toString
250
+ };
251
+ }
251
252
  }
252
253
 
253
- var RouterState_1;
254
- /**
255
- * @record
256
- * @template T
257
- */
258
- function RouterStateModel() { }
259
- if (false) {
260
- /** @type {?|undefined} */
261
- RouterStateModel.prototype.state;
262
- /** @type {?|undefined} */
263
- RouterStateModel.prototype.navigationId;
264
- /** @type {?} */
265
- RouterStateModel.prototype.trigger;
266
- }
267
- let RouterState = RouterState_1 = class RouterState {
268
- /**
269
- * @param {?} _store
270
- * @param {?} _router
271
- * @param {?} _serializer
272
- * @param {?} _ngZone
273
- * @param {?} _urlSerializer
274
- * @param {?} _locationStrategy
275
- * @param {?} _location
276
- */
277
- constructor(_store, _router, _serializer, _ngZone, _urlSerializer, _locationStrategy, _location) {
278
- this._store = _store;
279
- this._router = _router;
280
- this._serializer = _serializer;
281
- this._ngZone = _ngZone;
282
- this._urlSerializer = _urlSerializer;
283
- this._locationStrategy = _locationStrategy;
284
- this._location = _location;
285
- /**
286
- * Determines how navigation was performed by the `RouterState` itself
287
- * or outside via `new Navigate(...)`
288
- */
289
- this._trigger = 'none';
290
- /**
291
- * That's the serialized state from the `Router` class
292
- */
293
- this._routerState = null;
294
- /**
295
- * That's the value of the `RouterState` state
296
- */
297
- this._storeState = null;
298
- this._lastRoutesRecognized = (/** @type {?} */ (null));
299
- this.setUpStoreListener();
300
- this.setUpRouterEventsListener();
301
- this.checkInitialNavigationOnce();
302
- }
303
- /**
304
- * @template T
305
- * @param {?} state
306
- * @return {?}
307
- */
308
- static state(state) {
309
- return state && state.state;
310
- }
311
- /**
312
- * @param {?} state
313
- * @return {?}
314
- */
315
- static url(state) {
316
- return state && state.state && state.state.url;
317
- }
318
- /**
319
- * @param {?} _
320
- * @param {?} action
321
- * @return {?}
322
- */
323
- navigate(_, action) {
324
- return this._ngZone.run((/**
325
- * @return {?}
326
- */
327
- () => this._router.navigate(action.path, Object.assign({ queryParams: action.queryParams }, action.extras))));
328
- }
329
- /**
330
- * @param {?} ctx
331
- * @param {?} action
332
- * @return {?}
333
- */
334
- angularRouterAction(ctx, action) {
335
- ctx.setState(Object.assign({}, ctx.getState(), { trigger: action.trigger, state: action.routerState, navigationId: action.event.id }));
336
- }
337
- /**
338
- * @private
339
- * @return {?}
340
- */
341
- setUpStoreListener() {
342
- this._store.select(RouterState_1).subscribe((/**
343
- * @param {?} state
344
- * @return {?}
345
- */
346
- (state) => {
347
- this.navigateIfNeeded(state);
348
- }));
349
- }
350
- /**
351
- * @private
352
- * @return {?}
353
- */
354
- setUpRouterEventsListener() {
355
- this._router.events.subscribe((/**
356
- * @param {?} event
357
- * @return {?}
358
- */
359
- event => {
360
- if (event instanceof NavigationStart) {
361
- this.navigationStart();
362
- }
363
- else if (event instanceof RoutesRecognized) {
364
- this._lastRoutesRecognized = event;
365
- }
366
- else if (event instanceof ResolveEnd) {
367
- this.dispatchRouterDataResolved(event);
368
- }
369
- else if (event instanceof NavigationCancel) {
370
- this.dispatchRouterCancel(event);
371
- this.reset();
372
- }
373
- else if (event instanceof NavigationError) {
374
- this.dispatchRouterError(event);
375
- this.reset();
376
- }
377
- else if (event instanceof NavigationEnd) {
378
- this.navigationEnd();
379
- this.reset();
380
- }
381
- }));
382
- }
383
- /**
384
- * @private
385
- * @return {?}
386
- */
387
- navigationStart() {
388
- this._routerState = this._serializer.serialize(this._router.routerState.snapshot);
389
- if (this._trigger !== 'none') {
390
- this._storeState = this._store.selectSnapshot(RouterState_1);
391
- }
392
- }
393
- /**
394
- * @private
395
- * @return {?}
396
- */
397
- navigationEnd() {
398
- if (this.shouldDispatchRouterNavigation()) {
399
- this.dispatchRouterNavigation();
400
- }
401
- }
402
- /**
403
- * @private
404
- * @return {?}
405
- */
406
- shouldDispatchRouterNavigation() {
407
- if (!this._storeState)
408
- return true;
409
- return this._trigger !== 'store';
410
- }
411
- /**
412
- * @private
413
- * @param {?} state
414
- * @return {?}
415
- */
416
- navigateIfNeeded(state) {
417
- /** @type {?} */
418
- const canSkipNavigation = !this._storeState ||
419
- !this._storeState.state ||
420
- !state ||
421
- state.trigger === 'router' ||
422
- this._router.url === this._storeState.state.url;
423
- if (canSkipNavigation) {
424
- return;
425
- }
426
- this._trigger = 'store';
427
- this._ngZone.run((/**
428
- * @return {?}
429
- */
430
- () => {
431
- this._router.navigateByUrl((/** @type {?} */ ((/** @type {?} */ (this._storeState)).state)).url);
432
- }));
433
- }
434
- /**
435
- * @private
436
- * @return {?}
437
- */
438
- dispatchRouterNavigation() {
439
- /** @type {?} */
440
- const nextRouterState = this._serializer.serialize(this._lastRoutesRecognized.state);
441
- this.dispatchRouterAction(new RouterNavigation(nextRouterState, new RoutesRecognized(this._lastRoutesRecognized.id, this._lastRoutesRecognized.url, this._lastRoutesRecognized.urlAfterRedirects, nextRouterState), this._trigger));
442
- }
443
- /**
444
- * @private
445
- * @param {?} event
446
- * @return {?}
447
- */
448
- dispatchRouterCancel(event) {
449
- this.dispatchRouterAction(new RouterCancel((/** @type {?} */ (this._routerState)), this._storeState, event, this._trigger));
450
- }
451
- /**
452
- * @private
453
- * @param {?} event
454
- * @return {?}
455
- */
456
- dispatchRouterError(event) {
457
- this.dispatchRouterAction(new RouterError((/** @type {?} */ (this._routerState)), this._storeState, new NavigationError(event.id, event.url, `${event}`), this._trigger));
458
- }
459
- /**
460
- * @private
461
- * @template T
462
- * @param {?} action
463
- * @return {?}
464
- */
465
- dispatchRouterAction(action) {
466
- this._trigger = 'router';
467
- try {
468
- this._store.dispatch(action);
469
- }
470
- finally {
471
- this._trigger = 'none';
472
- }
473
- }
474
- /**
475
- * @private
476
- * @param {?} event
477
- * @return {?}
478
- */
479
- dispatchRouterDataResolved(event) {
480
- /** @type {?} */
481
- const routerState = this._serializer.serialize(event.state);
482
- this.dispatchRouterAction(new RouterDataResolved(routerState, event, this._trigger));
483
- }
484
- /**
485
- * @private
486
- * @return {?}
487
- */
488
- reset() {
489
- this._trigger = 'none';
490
- this._storeState = null;
491
- this._routerState = null;
492
- }
493
- /**
494
- * No sense to mess up the `setUpRouterEventsListener` method as we have
495
- * to perform this check only once and unsubscribe after the first event
496
- * is triggered
497
- * @private
498
- * @return {?}
499
- */
500
- checkInitialNavigationOnce() {
501
- if (isAngularInTestMode()) {
502
- return;
503
- }
504
- this._router.events
505
- .pipe(first((/**
506
- * @param {?} event
507
- * @return {?}
508
- */
509
- (event) => event instanceof RoutesRecognized)))
510
- .subscribe((/**
511
- * @param {?} __0
512
- * @return {?}
513
- */
514
- ({ url }) => {
515
- // `location.pathname` always equals manually entered URL in the address bar
516
- // e.g. `location.pathname === '/foo'`, but the `router` state has been initialized
517
- // with another URL (e.g. used in combination with `NgxsStoragePlugin`), thus the
518
- // `RouterNavigation` action will be dispatched and the user will be redirected to the
519
- // previously saved URL. We want to prevent such behavior, so we perform this check
520
- // `location.pathname` always equals manually entered URL in the address bar
521
- // e.g. `location.pathname === '/foo'`, but the `router` state has been initialized
522
- // with another URL (e.g. used in combination with `NgxsStoragePlugin`), thus the
523
- // `RouterNavigation` action will be dispatched and the user will be redirected to the
524
- // previously saved URL. We want to prevent such behavior, so we perform this check
525
- // `url` is a recognized URL by the Angular's router, while `currentUrl` is an actual URL
526
- // entered in the browser's address bar
527
- // `PathLocationStrategy.prototype.path()` returns a concatenation of
528
- // `PlatformLocation.pathname` and normalized `PlatformLocation.search`.
529
- // `Location.prototype.normalize` strips base href from the URL,
530
- // if `baseHref` (declared in angular.json) for example is `/en`
531
- // and the URL is `/test#anchor` - then `_locationStrategy.path(true)` will return `/en/test#anchor`,
532
- // but `/en/test#anchor` is not known to the Angular's router, so we have to strip `/en`
533
- // from the URL
534
- /** @type {?} */
535
- const currentUrl = this._location.normalize(this._locationStrategy.path(true));
536
- /** @type {?} */
537
- const currentUrlTree = this._urlSerializer.parse(currentUrl);
538
- // We need to serialize the URL because in that example `/test/?redirect=https://google.com/`
539
- // Angular will recognize it as `/test?redirect=https:%2F%2Fwww.google.com%2F`
540
- // so we have to run the `currentUrl` via the `UrlSerializer` that will encode characters
541
- /** @type {?} */
542
- const currentSerializedUrl = this._urlSerializer.serialize(currentUrlTree);
543
- // If URLs differ from each other - we've got to perform a redirect to the manually entered URL
544
- // in the address bar, as it must have a priority
545
- if (currentSerializedUrl !== url) {
546
- this._router.navigateByUrl(currentUrl);
547
- }
548
- }));
549
- }
550
- };
551
- RouterState.ctorParameters = () => [
552
- { type: Store },
553
- { type: Router },
554
- { type: RouterStateSerializer },
555
- { type: NgZone },
556
- { type: UrlSerializer },
557
- { type: LocationStrategy },
558
- { type: Location }
559
- ];
560
- RouterState.decorators = [
561
- { type: Injectable }
562
- ];
563
- /** @nocollapse */
564
- RouterState.ctorParameters = () => [
565
- { type: Store },
566
- { type: Router },
567
- { type: RouterStateSerializer },
568
- { type: NgZone },
569
- { type: UrlSerializer },
570
- { type: LocationStrategy },
571
- { type: Location }
572
- ];
573
- __decorate([
574
- Action(Navigate),
575
- __metadata("design:type", Function),
576
- __metadata("design:paramtypes", [Object, Navigate]),
577
- __metadata("design:returntype", void 0)
578
- ], RouterState.prototype, "navigate", null);
579
- __decorate([
580
- Action([RouterNavigation, RouterError, RouterCancel, RouterDataResolved]),
581
- __metadata("design:type", Function),
582
- __metadata("design:paramtypes", [Object, Object]),
583
- __metadata("design:returntype", void 0)
584
- ], RouterState.prototype, "angularRouterAction", null);
585
- __decorate([
586
- Selector(),
587
- __metadata("design:type", Function),
588
- __metadata("design:paramtypes", [Object]),
589
- __metadata("design:returntype", void 0)
590
- ], RouterState, "state", null);
591
- __decorate([
592
- Selector(),
593
- __metadata("design:type", Function),
594
- __metadata("design:paramtypes", [Object]),
595
- __metadata("design:returntype", Object)
596
- ], RouterState, "url", null);
597
- RouterState = RouterState_1 = __decorate([
598
- State({
599
- name: 'router',
600
- defaults: {
601
- state: undefined,
602
- navigationId: undefined,
603
- trigger: 'none'
604
- }
605
- }),
606
- __metadata("design:paramtypes", [Store,
607
- Router,
608
- RouterStateSerializer,
609
- NgZone,
610
- UrlSerializer,
611
- LocationStrategy,
612
- Location])
613
- ], RouterState);
614
- if (false) {
615
- /**
616
- * Determines how navigation was performed by the `RouterState` itself
617
- * or outside via `new Navigate(...)`
618
- * @type {?}
619
- * @private
620
- */
621
- RouterState.prototype._trigger;
622
- /**
623
- * That's the serialized state from the `Router` class
624
- * @type {?}
625
- * @private
626
- */
627
- RouterState.prototype._routerState;
628
- /**
629
- * That's the value of the `RouterState` state
630
- * @type {?}
631
- * @private
632
- */
633
- RouterState.prototype._storeState;
634
- /**
635
- * @type {?}
636
- * @private
637
- */
638
- RouterState.prototype._lastRoutesRecognized;
639
- /**
640
- * @type {?}
641
- * @private
642
- */
643
- RouterState.prototype._store;
644
- /**
645
- * @type {?}
646
- * @private
647
- */
648
- RouterState.prototype._router;
649
- /**
650
- * @type {?}
651
- * @private
652
- */
653
- RouterState.prototype._serializer;
654
- /**
655
- * @type {?}
656
- * @private
657
- */
658
- RouterState.prototype._ngZone;
659
- /**
660
- * @type {?}
661
- * @private
662
- */
663
- RouterState.prototype._urlSerializer;
664
- /**
665
- * @type {?}
666
- * @private
667
- */
668
- RouterState.prototype._locationStrategy;
669
- /**
670
- * @type {?}
671
- * @private
672
- */
673
- RouterState.prototype._location;
254
+ var RouterState_1;
255
+ /**
256
+ * @record
257
+ * @template T
258
+ */
259
+ function RouterStateModel() { }
260
+ if (false) {
261
+ /** @type {?|undefined} */
262
+ RouterStateModel.prototype.state;
263
+ /** @type {?|undefined} */
264
+ RouterStateModel.prototype.navigationId;
265
+ /** @type {?} */
266
+ RouterStateModel.prototype.trigger;
267
+ }
268
+ let RouterState = RouterState_1 = class RouterState {
269
+ /**
270
+ * @param {?} _store
271
+ * @param {?} _router
272
+ * @param {?} _serializer
273
+ * @param {?} _ngZone
274
+ * @param {?} _urlSerializer
275
+ * @param {?} _locationStrategy
276
+ * @param {?} _location
277
+ */
278
+ constructor(_store, _router, _serializer, _ngZone, _urlSerializer, _locationStrategy, _location) {
279
+ this._store = _store;
280
+ this._router = _router;
281
+ this._serializer = _serializer;
282
+ this._ngZone = _ngZone;
283
+ this._urlSerializer = _urlSerializer;
284
+ this._locationStrategy = _locationStrategy;
285
+ this._location = _location;
286
+ /**
287
+ * Determines how navigation was performed by the `RouterState` itself
288
+ * or outside via `new Navigate(...)`
289
+ */
290
+ this._trigger = 'none';
291
+ /**
292
+ * That's the serialized state from the `Router` class
293
+ */
294
+ this._routerState = null;
295
+ /**
296
+ * That's the value of the `RouterState` state
297
+ */
298
+ this._storeState = null;
299
+ this._lastRoutesRecognized = (/** @type {?} */ (null));
300
+ this._subscription = new Subscription();
301
+ this.setUpStoreListener();
302
+ this.setUpRouterEventsListener();
303
+ this.checkInitialNavigationOnce();
304
+ }
305
+ /**
306
+ * @template T
307
+ * @param {?} state
308
+ * @return {?}
309
+ */
310
+ static state(state) {
311
+ return state && state.state;
312
+ }
313
+ /**
314
+ * @param {?} state
315
+ * @return {?}
316
+ */
317
+ static url(state) {
318
+ return state && state.state && state.state.url;
319
+ }
320
+ /**
321
+ * @return {?}
322
+ */
323
+ ngOnDestroy() {
324
+ this._subscription.unsubscribe();
325
+ }
326
+ /**
327
+ * @param {?} _
328
+ * @param {?} action
329
+ * @return {?}
330
+ */
331
+ navigate(_, action) {
332
+ return this._ngZone.run((/**
333
+ * @return {?}
334
+ */
335
+ () => this._router.navigate(action.path, Object.assign({ queryParams: action.queryParams }, action.extras))));
336
+ }
337
+ /**
338
+ * @param {?} ctx
339
+ * @param {?} action
340
+ * @return {?}
341
+ */
342
+ angularRouterAction(ctx, action) {
343
+ ctx.setState(Object.assign({}, ctx.getState(), { trigger: action.trigger, state: action.routerState, navigationId: action.event.id }));
344
+ }
345
+ /**
346
+ * @private
347
+ * @return {?}
348
+ */
349
+ setUpStoreListener() {
350
+ /** @type {?} */
351
+ const subscription = this._store
352
+ .select(RouterState_1)
353
+ .subscribe((/**
354
+ * @param {?} state
355
+ * @return {?}
356
+ */
357
+ (state) => {
358
+ this.navigateIfNeeded(state);
359
+ }));
360
+ this._subscription.add(subscription);
361
+ }
362
+ /**
363
+ * @private
364
+ * @return {?}
365
+ */
366
+ setUpRouterEventsListener() {
367
+ /** @type {?} */
368
+ const subscription = this._router.events.subscribe((/**
369
+ * @param {?} event
370
+ * @return {?}
371
+ */
372
+ event => {
373
+ if (event instanceof NavigationStart) {
374
+ this.navigationStart();
375
+ }
376
+ else if (event instanceof RoutesRecognized) {
377
+ this._lastRoutesRecognized = event;
378
+ }
379
+ else if (event instanceof ResolveEnd) {
380
+ this.dispatchRouterDataResolved(event);
381
+ }
382
+ else if (event instanceof NavigationCancel) {
383
+ this.dispatchRouterCancel(event);
384
+ this.reset();
385
+ }
386
+ else if (event instanceof NavigationError) {
387
+ this.dispatchRouterError(event);
388
+ this.reset();
389
+ }
390
+ else if (event instanceof NavigationEnd) {
391
+ this.navigationEnd();
392
+ this.reset();
393
+ }
394
+ }));
395
+ this._subscription.add(subscription);
396
+ }
397
+ /**
398
+ * @private
399
+ * @return {?}
400
+ */
401
+ navigationStart() {
402
+ this._routerState = this._serializer.serialize(this._router.routerState.snapshot);
403
+ if (this._trigger !== 'none') {
404
+ this._storeState = this._store.selectSnapshot(RouterState_1);
405
+ }
406
+ }
407
+ /**
408
+ * @private
409
+ * @return {?}
410
+ */
411
+ navigationEnd() {
412
+ if (this.shouldDispatchRouterNavigation()) {
413
+ this.dispatchRouterNavigation();
414
+ }
415
+ }
416
+ /**
417
+ * @private
418
+ * @return {?}
419
+ */
420
+ shouldDispatchRouterNavigation() {
421
+ if (!this._storeState)
422
+ return true;
423
+ return this._trigger !== 'store';
424
+ }
425
+ /**
426
+ * @private
427
+ * @param {?} state
428
+ * @return {?}
429
+ */
430
+ navigateIfNeeded(state) {
431
+ /** @type {?} */
432
+ const canSkipNavigation = !this._storeState ||
433
+ !this._storeState.state ||
434
+ !state ||
435
+ state.trigger === 'router' ||
436
+ this._router.url === this._storeState.state.url;
437
+ if (canSkipNavigation) {
438
+ return;
439
+ }
440
+ this._trigger = 'store';
441
+ this._ngZone.run((/**
442
+ * @return {?}
443
+ */
444
+ () => {
445
+ this._router.navigateByUrl((/** @type {?} */ ((/** @type {?} */ (this._storeState)).state)).url);
446
+ }));
447
+ }
448
+ /**
449
+ * @private
450
+ * @return {?}
451
+ */
452
+ dispatchRouterNavigation() {
453
+ /** @type {?} */
454
+ const nextRouterState = this._serializer.serialize(this._lastRoutesRecognized.state);
455
+ this.dispatchRouterAction(new RouterNavigation(nextRouterState, new RoutesRecognized(this._lastRoutesRecognized.id, this._lastRoutesRecognized.url, this._lastRoutesRecognized.urlAfterRedirects, nextRouterState), this._trigger));
456
+ }
457
+ /**
458
+ * @private
459
+ * @param {?} event
460
+ * @return {?}
461
+ */
462
+ dispatchRouterCancel(event) {
463
+ this.dispatchRouterAction(new RouterCancel((/** @type {?} */ (this._routerState)), this._storeState, event, this._trigger));
464
+ }
465
+ /**
466
+ * @private
467
+ * @param {?} event
468
+ * @return {?}
469
+ */
470
+ dispatchRouterError(event) {
471
+ this.dispatchRouterAction(new RouterError((/** @type {?} */ (this._routerState)), this._storeState, new NavigationError(event.id, event.url, `${event}`), this._trigger));
472
+ }
473
+ /**
474
+ * @private
475
+ * @template T
476
+ * @param {?} action
477
+ * @return {?}
478
+ */
479
+ dispatchRouterAction(action) {
480
+ this._trigger = 'router';
481
+ try {
482
+ this._store.dispatch(action);
483
+ }
484
+ finally {
485
+ this._trigger = 'none';
486
+ }
487
+ }
488
+ /**
489
+ * @private
490
+ * @param {?} event
491
+ * @return {?}
492
+ */
493
+ dispatchRouterDataResolved(event) {
494
+ /** @type {?} */
495
+ const routerState = this._serializer.serialize(event.state);
496
+ this.dispatchRouterAction(new RouterDataResolved(routerState, event, this._trigger));
497
+ }
498
+ /**
499
+ * @private
500
+ * @return {?}
501
+ */
502
+ reset() {
503
+ this._trigger = 'none';
504
+ this._storeState = null;
505
+ this._routerState = null;
506
+ }
507
+ /**
508
+ * No sense to mess up the `setUpRouterEventsListener` method as we have
509
+ * to perform this check only once and unsubscribe after the first event
510
+ * is triggered
511
+ * @private
512
+ * @return {?}
513
+ */
514
+ checkInitialNavigationOnce() {
515
+ // Caretaker note: we have still left the `typeof` condition in order to avoid
516
+ // creating a breaking change for projects that still use the View Engine.
517
+ if ((typeof ngDevMode === 'undefined' || ngDevMode) &&
518
+ // Angular is running tests in development mode thus we can be sure that this method will be
519
+ // skipped in tests.
520
+ isAngularInTestMode()) {
521
+ return;
522
+ }
523
+ /** @type {?} */
524
+ const subscription = this._router.events
525
+ .pipe(first((/**
526
+ * @param {?} event
527
+ * @return {?}
528
+ */
529
+ (event) => event instanceof RoutesRecognized)))
530
+ .subscribe((/**
531
+ * @param {?} __0
532
+ * @return {?}
533
+ */
534
+ ({ url }) => {
535
+ // `location.pathname` always equals manually entered URL in the address bar
536
+ // e.g. `location.pathname === '/foo'`, but the `router` state has been initialized
537
+ // with another URL (e.g. used in combination with `NgxsStoragePlugin`), thus the
538
+ // `RouterNavigation` action will be dispatched and the user will be redirected to the
539
+ // previously saved URL. We want to prevent such behavior, so we perform this check
540
+ // `location.pathname` always equals manually entered URL in the address bar
541
+ // e.g. `location.pathname === '/foo'`, but the `router` state has been initialized
542
+ // with another URL (e.g. used in combination with `NgxsStoragePlugin`), thus the
543
+ // `RouterNavigation` action will be dispatched and the user will be redirected to the
544
+ // previously saved URL. We want to prevent such behavior, so we perform this check
545
+ // `url` is a recognized URL by the Angular's router, while `currentUrl` is an actual URL
546
+ // entered in the browser's address bar
547
+ // `PathLocationStrategy.prototype.path()` returns a concatenation of
548
+ // `PlatformLocation.pathname` and normalized `PlatformLocation.search`.
549
+ // `Location.prototype.normalize` strips base href from the URL,
550
+ // if `baseHref` (declared in angular.json) for example is `/en`
551
+ // and the URL is `/test#anchor` - then `_locationStrategy.path(true)` will return `/en/test#anchor`,
552
+ // but `/en/test#anchor` is not known to the Angular's router, so we have to strip `/en`
553
+ // from the URL
554
+ /** @type {?} */
555
+ const currentUrl = this._location.normalize(this._locationStrategy.path(true));
556
+ /** @type {?} */
557
+ const currentUrlTree = this._urlSerializer.parse(currentUrl);
558
+ // We need to serialize the URL because in that example `/test/?redirect=https://google.com/`
559
+ // Angular will recognize it as `/test?redirect=https:%2F%2Fwww.google.com%2F`
560
+ // so we have to run the `currentUrl` via the `UrlSerializer` that will encode characters
561
+ /** @type {?} */
562
+ const currentSerializedUrl = this._urlSerializer.serialize(currentUrlTree);
563
+ // If URLs differ from each other - we've got to perform a redirect to the manually entered URL
564
+ // in the address bar, as it must have a priority
565
+ if (currentSerializedUrl !== url) {
566
+ this._router.navigateByUrl(currentUrl);
567
+ }
568
+ }));
569
+ this._subscription.add(subscription);
570
+ }
571
+ };
572
+ RouterState.ctorParameters = () => [
573
+ { type: Store },
574
+ { type: Router },
575
+ { type: RouterStateSerializer },
576
+ { type: NgZone },
577
+ { type: UrlSerializer },
578
+ { type: LocationStrategy },
579
+ { type: Location }
580
+ ];
581
+ RouterState.decorators = [
582
+ { type: Injectable }
583
+ ];
584
+ /** @nocollapse */
585
+ RouterState.ctorParameters = () => [
586
+ { type: Store },
587
+ { type: Router },
588
+ { type: RouterStateSerializer },
589
+ { type: NgZone },
590
+ { type: UrlSerializer },
591
+ { type: LocationStrategy },
592
+ { type: Location }
593
+ ];
594
+ __decorate([
595
+ Action(Navigate),
596
+ __metadata("design:type", Function),
597
+ __metadata("design:paramtypes", [Object, Navigate]),
598
+ __metadata("design:returntype", void 0)
599
+ ], RouterState.prototype, "navigate", null);
600
+ __decorate([
601
+ Action([RouterNavigation, RouterError, RouterCancel, RouterDataResolved]),
602
+ __metadata("design:type", Function),
603
+ __metadata("design:paramtypes", [Object, Object]),
604
+ __metadata("design:returntype", void 0)
605
+ ], RouterState.prototype, "angularRouterAction", null);
606
+ __decorate([
607
+ Selector(),
608
+ __metadata("design:type", Function),
609
+ __metadata("design:paramtypes", [Object]),
610
+ __metadata("design:returntype", void 0)
611
+ ], RouterState, "state", null);
612
+ __decorate([
613
+ Selector(),
614
+ __metadata("design:type", Function),
615
+ __metadata("design:paramtypes", [Object]),
616
+ __metadata("design:returntype", Object)
617
+ ], RouterState, "url", null);
618
+ RouterState = RouterState_1 = __decorate([
619
+ State({
620
+ name: 'router',
621
+ defaults: {
622
+ state: undefined,
623
+ navigationId: undefined,
624
+ trigger: 'none'
625
+ }
626
+ }),
627
+ __metadata("design:paramtypes", [Store,
628
+ Router,
629
+ RouterStateSerializer,
630
+ NgZone,
631
+ UrlSerializer,
632
+ LocationStrategy,
633
+ Location])
634
+ ], RouterState);
635
+ if (false) {
636
+ /**
637
+ * Determines how navigation was performed by the `RouterState` itself
638
+ * or outside via `new Navigate(...)`
639
+ * @type {?}
640
+ * @private
641
+ */
642
+ RouterState.prototype._trigger;
643
+ /**
644
+ * That's the serialized state from the `Router` class
645
+ * @type {?}
646
+ * @private
647
+ */
648
+ RouterState.prototype._routerState;
649
+ /**
650
+ * That's the value of the `RouterState` state
651
+ * @type {?}
652
+ * @private
653
+ */
654
+ RouterState.prototype._storeState;
655
+ /**
656
+ * @type {?}
657
+ * @private
658
+ */
659
+ RouterState.prototype._lastRoutesRecognized;
660
+ /**
661
+ * @type {?}
662
+ * @private
663
+ */
664
+ RouterState.prototype._subscription;
665
+ /**
666
+ * @type {?}
667
+ * @private
668
+ */
669
+ RouterState.prototype._store;
670
+ /**
671
+ * @type {?}
672
+ * @private
673
+ */
674
+ RouterState.prototype._router;
675
+ /**
676
+ * @type {?}
677
+ * @private
678
+ */
679
+ RouterState.prototype._serializer;
680
+ /**
681
+ * @type {?}
682
+ * @private
683
+ */
684
+ RouterState.prototype._ngZone;
685
+ /**
686
+ * @type {?}
687
+ * @private
688
+ */
689
+ RouterState.prototype._urlSerializer;
690
+ /**
691
+ * @type {?}
692
+ * @private
693
+ */
694
+ RouterState.prototype._locationStrategy;
695
+ /**
696
+ * @type {?}
697
+ * @private
698
+ */
699
+ RouterState.prototype._location;
674
700
  }
675
701
 
676
- /**
677
- * @fileoverview added by tsickle
678
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
679
- */
680
- class NgxsRouterPluginModule {
681
- /**
682
- * @return {?}
683
- */
684
- static forRoot() {
685
- return {
686
- ngModule: NgxsRouterPluginModule,
687
- providers: [{ provide: RouterStateSerializer, useClass: DefaultRouterStateSerializer }]
688
- };
689
- }
690
- }
691
- NgxsRouterPluginModule.decorators = [
692
- { type: NgModule, args: [{
693
- imports: [NgxsModule.forFeature([RouterState])]
694
- },] }
702
+ /**
703
+ * @fileoverview added by tsickle
704
+ * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
705
+ */
706
+ class NgxsRouterPluginModule {
707
+ /**
708
+ * @return {?}
709
+ */
710
+ static forRoot() {
711
+ return {
712
+ ngModule: NgxsRouterPluginModule,
713
+ providers: [{ provide: RouterStateSerializer, useClass: DefaultRouterStateSerializer }]
714
+ };
715
+ }
716
+ }
717
+ NgxsRouterPluginModule.decorators = [
718
+ { type: NgModule, args: [{
719
+ imports: [NgxsModule.forFeature([RouterState])]
720
+ },] }
695
721
  ];
696
722
 
697
- /**
698
- * @fileoverview added by tsickle
699
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
723
+ /**
724
+ * @fileoverview added by tsickle
725
+ * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
700
726
  */
701
727
 
702
- /**
703
- * @fileoverview added by tsickle
704
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
728
+ /**
729
+ * @fileoverview added by tsickle
730
+ * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
705
731
  */
706
732
 
707
- /**
708
- * @fileoverview added by tsickle
709
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
733
+ /**
734
+ * @fileoverview added by tsickle
735
+ * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
710
736
  */
711
737
 
712
738
  export { DefaultRouterStateSerializer, Navigate, NgxsRouterPluginModule, RouterCancel, RouterDataResolved, RouterError, RouterNavigation, RouterState, RouterStateSerializer };