@ngrdt/router 0.0.99 → 0.1.0
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.
- package/fesm2022/ngrdt-router.mjs +1 -1465
- package/package.json +4 -4
- package/types/ngrdt-router.d.ts +3 -3
- package/README.md +0 -255
- package/fesm2022/ngrdt-router.mjs.map +0 -1
|
@@ -1,1465 +1 @@
|
|
|
1
|
-
import * as i0 from '@angular/core';
|
|
2
|
-
import { InjectionToken, inject, EnvironmentInjector, runInInjectionContext, makeEnvironmentProviders, Injectable, DestroyRef, Renderer2, ElementRef, Input, Directive, afterNextRender, input, linkedSignal, booleanAttribute, computed, effect } from '@angular/core';
|
|
3
|
-
import { Location, PlatformLocation } from '@angular/common';
|
|
4
|
-
import * as i1 from '@angular/router';
|
|
5
|
-
import { Router, NavigationEnd, RouterModule, RouterLink, GuardsCheckStart } from '@angular/router';
|
|
6
|
-
import { RdtStringUtils } from '@ngrdt/utils';
|
|
7
|
-
import { filter, take, fromEvent } from 'rxjs';
|
|
8
|
-
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
|
9
|
-
import { RDT_BUTTON_BASE_PROVIDER } from '@ngrdt/button';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Injection token that defines behavior when route cannot be entered (canBeEntered returns false).
|
|
13
|
-
* If value is undefined, route will be redirected to ''.
|
|
14
|
-
*/
|
|
15
|
-
const RDT_CANNOT_BE_ENTERED_PROVIDER = new InjectionToken('RDT_CANNOT_BE_ENTERED', { factory: () => undefined, providedIn: 'root' });
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Type-safe container for route parameters keyed by route instance.
|
|
19
|
-
* Stores params for multiple routes in a hierarchy (e.g. parent + child params from a single URL).
|
|
20
|
-
* Parameters are keyed internally by absolute path, so two different `RdtRoute` instances
|
|
21
|
-
* with the same path share the same slot.
|
|
22
|
-
*/
|
|
23
|
-
class RdtParameters {
|
|
24
|
-
params;
|
|
25
|
-
constructor(params = {}) {
|
|
26
|
-
this.params = params;
|
|
27
|
-
}
|
|
28
|
-
get(route) {
|
|
29
|
-
return this.params[route.absolutePath] ?? null;
|
|
30
|
-
}
|
|
31
|
-
set(route, params) {
|
|
32
|
-
this.params[route.absolutePath] = params;
|
|
33
|
-
return this;
|
|
34
|
-
}
|
|
35
|
-
append(route, params) {
|
|
36
|
-
const existing = this.get(route) ?? {};
|
|
37
|
-
this.params[route.absolutePath] = { ...existing, ...params };
|
|
38
|
-
return this;
|
|
39
|
-
}
|
|
40
|
-
setAll(params) {
|
|
41
|
-
this.params = { ...this.params, ...params.params };
|
|
42
|
-
return this;
|
|
43
|
-
}
|
|
44
|
-
*[Symbol.iterator]() {
|
|
45
|
-
const keys = Object.keys(this.params);
|
|
46
|
-
keys.sort((a, b) => a.length - b.length);
|
|
47
|
-
for (const key of keys) {
|
|
48
|
-
yield [key, this.params[key]];
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
function ALWAYS_TRUE() {
|
|
53
|
-
return true;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
var RdtNavigationSource;
|
|
57
|
-
(function (RdtNavigationSource) {
|
|
58
|
-
RdtNavigationSource["BREADCRUMB"] = "breadcrumb";
|
|
59
|
-
})(RdtNavigationSource || (RdtNavigationSource = {}));
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Bridges an `RdtRoute` to Angular's `Route` config.
|
|
63
|
-
* Obtained via `RdtRoute.toAngularRoute()`. Use the fluent API to attach
|
|
64
|
-
* components, guards, resolvers, and providers, then call `build()` to produce
|
|
65
|
-
* the Angular `Route` object.
|
|
66
|
-
*
|
|
67
|
-
* `build()` validates that children defined on the `RdtRoute` match the children
|
|
68
|
-
* passed via `withChildren()` — a mismatch throws at startup, catching misconfiguration early.
|
|
69
|
-
*
|
|
70
|
-
* When a route has both a component and children, `build()` automatically restructures
|
|
71
|
-
* the output into Angular's required wrapper format (parent with empty-path child for the component).
|
|
72
|
-
*/
|
|
73
|
-
class RdtAngularRoute {
|
|
74
|
-
route;
|
|
75
|
-
children = [];
|
|
76
|
-
// Must be set from component declaration files
|
|
77
|
-
loadComponent;
|
|
78
|
-
loadChildren;
|
|
79
|
-
component;
|
|
80
|
-
providers = [];
|
|
81
|
-
providersForChildren = [];
|
|
82
|
-
resolvers = {};
|
|
83
|
-
canActivate = [];
|
|
84
|
-
canActivateChild = [];
|
|
85
|
-
canDeactivate = [];
|
|
86
|
-
runGuardsAndResolvers;
|
|
87
|
-
data;
|
|
88
|
-
constructor(route) {
|
|
89
|
-
this.route = route;
|
|
90
|
-
}
|
|
91
|
-
provide(...provider) {
|
|
92
|
-
this.providers.push(...provider);
|
|
93
|
-
return this;
|
|
94
|
-
}
|
|
95
|
-
provideToChildren(...provider) {
|
|
96
|
-
this.providersForChildren.push(...provider);
|
|
97
|
-
this.providers.push(...provider);
|
|
98
|
-
return this;
|
|
99
|
-
}
|
|
100
|
-
withLazyComponent(callback) {
|
|
101
|
-
this.loadComponent = callback;
|
|
102
|
-
return this;
|
|
103
|
-
}
|
|
104
|
-
withModule(callback) {
|
|
105
|
-
this.loadChildren = callback;
|
|
106
|
-
return this;
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* @deprecated Use withModule() instead.
|
|
110
|
-
*/
|
|
111
|
-
setModule(callback) {
|
|
112
|
-
this.loadChildren = callback;
|
|
113
|
-
return this;
|
|
114
|
-
}
|
|
115
|
-
withComponent(comp) {
|
|
116
|
-
this.component = comp;
|
|
117
|
-
return this;
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* @deprecated Use withComponent() instead.
|
|
121
|
-
*/
|
|
122
|
-
setComponent(comp) {
|
|
123
|
-
return this.withComponent(comp);
|
|
124
|
-
}
|
|
125
|
-
withChildren(...routes) {
|
|
126
|
-
this.children = routes;
|
|
127
|
-
return this;
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
|
-
* @deprecated Use withChildren() instead.
|
|
131
|
-
*/
|
|
132
|
-
setChildren(...routes) {
|
|
133
|
-
return this.withChildren(...routes);
|
|
134
|
-
}
|
|
135
|
-
addResolve(paramName, resolver) {
|
|
136
|
-
this.resolvers[paramName] = resolver;
|
|
137
|
-
return this;
|
|
138
|
-
}
|
|
139
|
-
addCanActivate(...fns) {
|
|
140
|
-
this.canActivate.push(...fns);
|
|
141
|
-
return this;
|
|
142
|
-
}
|
|
143
|
-
addCanDeactivate(...fns) {
|
|
144
|
-
this.canDeactivate.push(...fns);
|
|
145
|
-
return this;
|
|
146
|
-
}
|
|
147
|
-
addCanActivateChild(...fns) {
|
|
148
|
-
this.canActivateChild.push(...fns);
|
|
149
|
-
return this;
|
|
150
|
-
}
|
|
151
|
-
withRunGuardsAndResolvers(value) {
|
|
152
|
-
this.runGuardsAndResolvers = value;
|
|
153
|
-
return this;
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* @deprecated Use withRunGuardsAndResolvers() instead.
|
|
157
|
-
*/
|
|
158
|
-
setRunGuardsAndResolvers(value) {
|
|
159
|
-
return this.withRunGuardsAndResolvers(value);
|
|
160
|
-
}
|
|
161
|
-
build() {
|
|
162
|
-
this.checkChildrenMatch();
|
|
163
|
-
let res = {
|
|
164
|
-
path: this.route.path,
|
|
165
|
-
providers: this.providers,
|
|
166
|
-
title: this.route.name,
|
|
167
|
-
canMatch: this.getCanMatch(),
|
|
168
|
-
};
|
|
169
|
-
if (this.component) {
|
|
170
|
-
res.component = this.component;
|
|
171
|
-
}
|
|
172
|
-
else if (this.loadChildren) {
|
|
173
|
-
res.loadChildren = this.loadChildren;
|
|
174
|
-
}
|
|
175
|
-
else if (this.loadComponent) {
|
|
176
|
-
res.loadComponent = this.loadComponent;
|
|
177
|
-
}
|
|
178
|
-
res.data = this.data ?? {
|
|
179
|
-
breadcrumb: {
|
|
180
|
-
label: this.route.name,
|
|
181
|
-
disable: this.route.entryDisabled,
|
|
182
|
-
staticStateParams: {
|
|
183
|
-
src: RdtNavigationSource.BREADCRUMB,
|
|
184
|
-
},
|
|
185
|
-
},
|
|
186
|
-
...this.route.data,
|
|
187
|
-
};
|
|
188
|
-
const guard = (route, state) => {
|
|
189
|
-
const injector = inject(EnvironmentInjector);
|
|
190
|
-
const rdtRouter = inject(RdtRouterService);
|
|
191
|
-
const parsed = rdtRouter.parseAbsoluteUrl(state.url);
|
|
192
|
-
const combinedParams = parsed?.params ?? {
|
|
193
|
-
params: {},
|
|
194
|
-
query: {},
|
|
195
|
-
state: {},
|
|
196
|
-
route: new RdtParameters(),
|
|
197
|
-
};
|
|
198
|
-
combinedParams.query = route.queryParams;
|
|
199
|
-
if (runInInjectionContext(injector, () => this.route.canBeEnteredFn(parsed?.route ?? this.route, combinedParams))) {
|
|
200
|
-
return true;
|
|
201
|
-
}
|
|
202
|
-
else {
|
|
203
|
-
const router = inject(Router);
|
|
204
|
-
const location = inject(Location);
|
|
205
|
-
let url = inject(RDT_CANNOT_BE_ENTERED_PROVIDER);
|
|
206
|
-
if (typeof url === 'function') {
|
|
207
|
-
url = runInInjectionContext(injector, url.bind(url, location.path(), state.url, parsed?.route ?? this.route));
|
|
208
|
-
}
|
|
209
|
-
if (typeof url === 'string') {
|
|
210
|
-
return router.parseUrl(url);
|
|
211
|
-
}
|
|
212
|
-
else {
|
|
213
|
-
return false;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
};
|
|
217
|
-
this.canActivate.push(guard);
|
|
218
|
-
res.canActivate = this.canActivate;
|
|
219
|
-
res.canDeactivate = this.canDeactivate;
|
|
220
|
-
if (this.providersForChildren.length > 0) {
|
|
221
|
-
this.children.forEach((ch) => this.provideToChildrenRec(ch));
|
|
222
|
-
}
|
|
223
|
-
// If children exist, change
|
|
224
|
-
// {
|
|
225
|
-
// path: '/path',
|
|
226
|
-
// component: FeatureComponent,
|
|
227
|
-
// children: [...]
|
|
228
|
-
// }
|
|
229
|
-
// into
|
|
230
|
-
// {
|
|
231
|
-
// path: '/path',
|
|
232
|
-
// children: [
|
|
233
|
-
// {
|
|
234
|
-
// path: '',
|
|
235
|
-
// component: FeatureComponent
|
|
236
|
-
// },
|
|
237
|
-
// ...
|
|
238
|
-
// ]
|
|
239
|
-
// }
|
|
240
|
-
if (this.children.length > 0) {
|
|
241
|
-
if (this.loadChildren || this.component || this.loadComponent) {
|
|
242
|
-
const path = res.path;
|
|
243
|
-
res.path = '';
|
|
244
|
-
res.pathMatch = 'full';
|
|
245
|
-
res.canMatch = undefined;
|
|
246
|
-
res = {
|
|
247
|
-
path: path,
|
|
248
|
-
pathMatch: 'prefix',
|
|
249
|
-
children: [res, ...this.children],
|
|
250
|
-
canActivateChild: this.canActivateChild,
|
|
251
|
-
canMatch: this.getCanMatch(),
|
|
252
|
-
data: res.data,
|
|
253
|
-
providers: res.providers,
|
|
254
|
-
canActivate: res.canActivate,
|
|
255
|
-
canDeactivate: res.canDeactivate,
|
|
256
|
-
};
|
|
257
|
-
}
|
|
258
|
-
else {
|
|
259
|
-
res.children = this.children;
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
if (Object.keys(this.resolvers).length > 0) {
|
|
263
|
-
res.resolve = this.resolvers;
|
|
264
|
-
}
|
|
265
|
-
if (this.runGuardsAndResolvers) {
|
|
266
|
-
res.runGuardsAndResolvers = this.runGuardsAndResolvers;
|
|
267
|
-
}
|
|
268
|
-
return res;
|
|
269
|
-
}
|
|
270
|
-
getCanMatch() {
|
|
271
|
-
const paramMap = this.route.paramMap;
|
|
272
|
-
const hasConstrainedParams = Object.keys(paramMap).some((p) => paramMap[p] === 'number' || Array.isArray(paramMap[p]));
|
|
273
|
-
if (!hasConstrainedParams) {
|
|
274
|
-
return undefined;
|
|
275
|
-
}
|
|
276
|
-
const routeSegmentLength = this.route.path.split('/').length;
|
|
277
|
-
const regex = this.route.regex
|
|
278
|
-
? new RegExp(`^${this.route.regex.source}$`)
|
|
279
|
-
: this.route.absoluteRegex;
|
|
280
|
-
return [
|
|
281
|
-
(route, segments) => {
|
|
282
|
-
const path = segments
|
|
283
|
-
.slice(0, routeSegmentLength)
|
|
284
|
-
.map((s) => s.path)
|
|
285
|
-
.join('/');
|
|
286
|
-
return regex.test(path);
|
|
287
|
-
},
|
|
288
|
-
];
|
|
289
|
-
}
|
|
290
|
-
provideToChildrenRec(child) {
|
|
291
|
-
if (!child.providers) {
|
|
292
|
-
child.providers = [];
|
|
293
|
-
}
|
|
294
|
-
this.providersForChildren.forEach((p) => {
|
|
295
|
-
if (typeof p === 'object' && 'provide' in p) {
|
|
296
|
-
const exists = child.providers.some((pr) => {
|
|
297
|
-
if (typeof pr === 'object' && 'provide' in pr) {
|
|
298
|
-
return pr.provide === p.provide;
|
|
299
|
-
}
|
|
300
|
-
return false;
|
|
301
|
-
});
|
|
302
|
-
if (!exists) {
|
|
303
|
-
child.providers.unshift(p);
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
else {
|
|
307
|
-
child.providers.unshift(p);
|
|
308
|
-
}
|
|
309
|
-
});
|
|
310
|
-
if (child.children) {
|
|
311
|
-
child.children.forEach((ch) => this.provideToChildrenRec(ch));
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
checkChildrenMatch() {
|
|
315
|
-
// Check that all routes are present if route is not
|
|
316
|
-
// lazy loaded module.
|
|
317
|
-
if (!this.loadChildren &&
|
|
318
|
-
(this.route.children.length > 0 || this.children.length > 0)) {
|
|
319
|
-
if (this.children.length < this.route.children.length) {
|
|
320
|
-
throw new Error(`RdtRoute ${this.route.name} has ${this.route.children.length} children, but RdtAngularRoute has ${this.children.length}. These numbers must match. Did you forget to call .setChildren() method in global routes definition? Routes with following names should be present: ${this.route.children
|
|
321
|
-
.map((ch) => ch.name)
|
|
322
|
-
.join(', ')}`);
|
|
323
|
-
}
|
|
324
|
-
else if (this.children.length > this.route.children.length) {
|
|
325
|
-
throw new Error(`RdtRoute ${this.route.name} has ${this.route.children.length} children, but RdtAngularRoute has ${this.children.length}. These numbers must match. Did you forget to call .setChildren() method in feature shell routes definition? Routes with following paths should be present: ${this.children
|
|
326
|
-
.map((ch) => ch.path)
|
|
327
|
-
.join(', ')}`);
|
|
328
|
-
}
|
|
329
|
-
const n1 = this.children.map((ch) => ch.path);
|
|
330
|
-
const n2 = this.route.children.map((ch) => ch.path);
|
|
331
|
-
n1.sort();
|
|
332
|
-
n2.sort();
|
|
333
|
-
for (let i = 0; i < n1.length; i++) {
|
|
334
|
-
if (n1[i] !== n2[i]) {
|
|
335
|
-
throw new Error(`Paths of children provided in RdtRoute and
|
|
336
|
-
RdtAngularRoute do not match. ${n1[i]} != ${n2[i]}`);
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
class RdtRouteBase {
|
|
344
|
-
_orderedParams = [];
|
|
345
|
-
_paramMap = {};
|
|
346
|
-
_regex = null;
|
|
347
|
-
_paramMappings = [];
|
|
348
|
-
_name;
|
|
349
|
-
_path;
|
|
350
|
-
_parent = null;
|
|
351
|
-
_children = [];
|
|
352
|
-
_entryDisabled = false;
|
|
353
|
-
_canBeEntered = ALWAYS_TRUE;
|
|
354
|
-
_data = {};
|
|
355
|
-
/**
|
|
356
|
-
* Data that is passed to Angular route definition.
|
|
357
|
-
*/
|
|
358
|
-
get data() {
|
|
359
|
-
return this._data;
|
|
360
|
-
}
|
|
361
|
-
/**
|
|
362
|
-
* Regular expression route path relative to parent route.
|
|
363
|
-
* Hostname excluded.
|
|
364
|
-
*/
|
|
365
|
-
get regex() {
|
|
366
|
-
return this._regex;
|
|
367
|
-
}
|
|
368
|
-
/**
|
|
369
|
-
* Denotes disabled route entry.
|
|
370
|
-
* If true, route should have no NgModule
|
|
371
|
-
* or component and only serves as virtual module
|
|
372
|
-
* for breadcrumb.
|
|
373
|
-
*/
|
|
374
|
-
get entryDisabled() {
|
|
375
|
-
return this._entryDisabled;
|
|
376
|
-
}
|
|
377
|
-
/**
|
|
378
|
-
* Map of parameters and their types.
|
|
379
|
-
*/
|
|
380
|
-
get paramMap() {
|
|
381
|
-
return this._paramMap;
|
|
382
|
-
}
|
|
383
|
-
/**
|
|
384
|
-
* Human readable name for display in breadcrumbs, title, etc.
|
|
385
|
-
*/
|
|
386
|
-
get name() {
|
|
387
|
-
return this._name;
|
|
388
|
-
}
|
|
389
|
-
/**
|
|
390
|
-
* Path of the route relative to parent route.
|
|
391
|
-
*/
|
|
392
|
-
get path() {
|
|
393
|
-
return this._path;
|
|
394
|
-
}
|
|
395
|
-
/**
|
|
396
|
-
* Parent route.
|
|
397
|
-
*/
|
|
398
|
-
get parent() {
|
|
399
|
-
return this._parent;
|
|
400
|
-
}
|
|
401
|
-
/**
|
|
402
|
-
* Child routes.
|
|
403
|
-
*/
|
|
404
|
-
get children() {
|
|
405
|
-
return this._children;
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
/**
|
|
410
|
-
* Immutable, type-safe route definition. Created via `RdtRouteBuilder.build()`.
|
|
411
|
-
*
|
|
412
|
-
* Each instance represents a single route in the hierarchy and holds its path pattern,
|
|
413
|
-
* parameter types, parent reference, and access guard. Use `withStaticParams()`,
|
|
414
|
-
* `withQueryParams()`, or `withStateParams()` to create derived instances with
|
|
415
|
-
* pre-filled values (the original is never mutated).
|
|
416
|
-
*
|
|
417
|
-
* To bridge to Angular's router, call `toAngularRoute()` to get an `RdtAngularRoute`
|
|
418
|
-
* builder that produces an Angular `Route` config object.
|
|
419
|
-
*
|
|
420
|
-
* @typeParam T - Shape of this route's own parameters.
|
|
421
|
-
*/
|
|
422
|
-
class RdtRoute extends RdtRouteBase {
|
|
423
|
-
_absoluteRegex;
|
|
424
|
-
_staticParams = {};
|
|
425
|
-
_queryParams = {};
|
|
426
|
-
_stateParams = {};
|
|
427
|
-
/**
|
|
428
|
-
* Absolute path of parent route or '/' if there is no parent.
|
|
429
|
-
* Hostname excluded.
|
|
430
|
-
*/
|
|
431
|
-
get basePath() {
|
|
432
|
-
return this._parent ? this._parent.absolutePath : '/';
|
|
433
|
-
}
|
|
434
|
-
/**
|
|
435
|
-
* Absolute path of the route. Hostname excluded.
|
|
436
|
-
*/
|
|
437
|
-
get absolutePath() {
|
|
438
|
-
return RdtStringUtils.joinPaths(this.basePath, this._path);
|
|
439
|
-
}
|
|
440
|
-
/**
|
|
441
|
-
* Regular expression to match only this route (not children
|
|
442
|
-
* or parent routes). Hostname excluded.
|
|
443
|
-
*/
|
|
444
|
-
get absoluteRegex() {
|
|
445
|
-
if (this._absoluteRegex) {
|
|
446
|
-
return this._absoluteRegex;
|
|
447
|
-
}
|
|
448
|
-
if (!this._regex) {
|
|
449
|
-
this._absoluteRegex = new RegExp('^/$');
|
|
450
|
-
if (this._parent) {
|
|
451
|
-
throw new Error('Nested route with empty path is not allowed.');
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
else if (this._parent) {
|
|
455
|
-
// Remove initial ^ and ending $
|
|
456
|
-
const parent = this._parent.absoluteRegex.source.slice(1, -1);
|
|
457
|
-
this._absoluteRegex = new RegExp('^' + RdtStringUtils.joinPaths(parent, this._regex.source) + '$');
|
|
458
|
-
}
|
|
459
|
-
else {
|
|
460
|
-
this._absoluteRegex = new RegExp('^' + RdtStringUtils.joinPaths('/', this._regex.source) + '$');
|
|
461
|
-
}
|
|
462
|
-
return this._absoluteRegex;
|
|
463
|
-
}
|
|
464
|
-
/**
|
|
465
|
-
* Statically set state params used when navigating to this instance.
|
|
466
|
-
*/
|
|
467
|
-
get stateParams() {
|
|
468
|
-
return this._stateParams;
|
|
469
|
-
}
|
|
470
|
-
/**
|
|
471
|
-
* Statically set query params used when navigating to this instance.
|
|
472
|
-
*/
|
|
473
|
-
get queryParams() {
|
|
474
|
-
return this._queryParams;
|
|
475
|
-
}
|
|
476
|
-
/**
|
|
477
|
-
* @param url Absolute path to test.
|
|
478
|
-
* @returns True if url matches this route.
|
|
479
|
-
*/
|
|
480
|
-
testUrl(url) {
|
|
481
|
-
return this.absoluteRegex.test(url);
|
|
482
|
-
}
|
|
483
|
-
/**
|
|
484
|
-
* Statically set parameters used when navigating to this instance.
|
|
485
|
-
*/
|
|
486
|
-
get staticParams() {
|
|
487
|
-
return { ...this._staticParams };
|
|
488
|
-
}
|
|
489
|
-
get hasCanBeEnteredGuard() {
|
|
490
|
-
return this._canBeEntered !== ALWAYS_TRUE;
|
|
491
|
-
}
|
|
492
|
-
/**
|
|
493
|
-
* Meta guard that is used by [rdtRouterLink] and RdtMenu
|
|
494
|
-
* to determine if route can be entered.
|
|
495
|
-
* @param injector Environment or custom injector.
|
|
496
|
-
* @param params Optional parameters to pass to guard function.
|
|
497
|
-
* @returns True if route can be entered.
|
|
498
|
-
*/
|
|
499
|
-
canBeEntered(injector, combinedParams = {}) {
|
|
500
|
-
const route = this.getStaticParams();
|
|
501
|
-
let query = this._queryParams;
|
|
502
|
-
let state = this._stateParams;
|
|
503
|
-
if (combinedParams.params) {
|
|
504
|
-
route.set(this, combinedParams.params);
|
|
505
|
-
}
|
|
506
|
-
if (combinedParams.route) {
|
|
507
|
-
route.setAll(combinedParams.route);
|
|
508
|
-
}
|
|
509
|
-
if (combinedParams.query) {
|
|
510
|
-
query = { ...query, ...combinedParams.query };
|
|
511
|
-
}
|
|
512
|
-
if (combinedParams.state) {
|
|
513
|
-
state = { ...state, ...combinedParams.state };
|
|
514
|
-
}
|
|
515
|
-
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
516
|
-
let current = this;
|
|
517
|
-
while (current) {
|
|
518
|
-
const combined = {
|
|
519
|
-
params: route.get(current) ?? {},
|
|
520
|
-
route: route,
|
|
521
|
-
query: query,
|
|
522
|
-
state: state,
|
|
523
|
-
};
|
|
524
|
-
if (injector) {
|
|
525
|
-
const c = current;
|
|
526
|
-
if (!runInInjectionContext(injector, () => c._canBeEntered(c, combined))) {
|
|
527
|
-
return false;
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
else if (!current._canBeEntered(current, combined)) {
|
|
531
|
-
return false;
|
|
532
|
-
}
|
|
533
|
-
current = current._parent;
|
|
534
|
-
}
|
|
535
|
-
return true;
|
|
536
|
-
}
|
|
537
|
-
get canBeEnteredFn() {
|
|
538
|
-
return this._canBeEntered;
|
|
539
|
-
}
|
|
540
|
-
/**
|
|
541
|
-
* Extracts url parameters from absolute path for this path and each parent.
|
|
542
|
-
*/
|
|
543
|
-
parseAbsoluteUrl(url) {
|
|
544
|
-
const path = RdtStringUtils.stripQueryParams(url);
|
|
545
|
-
const reg = this.absoluteRegex;
|
|
546
|
-
const matches = reg.exec(path);
|
|
547
|
-
if (matches) {
|
|
548
|
-
const query = RdtStringUtils.parseQueryParams(url);
|
|
549
|
-
const values = matches.slice(1).reverse();
|
|
550
|
-
const obj = this.fill(values);
|
|
551
|
-
const route = new RdtParameters(obj);
|
|
552
|
-
const params = route.get(this) ?? {};
|
|
553
|
-
return {
|
|
554
|
-
params: params,
|
|
555
|
-
route: route,
|
|
556
|
-
query: query,
|
|
557
|
-
state: {},
|
|
558
|
-
};
|
|
559
|
-
}
|
|
560
|
-
else {
|
|
561
|
-
return null;
|
|
562
|
-
}
|
|
563
|
-
}
|
|
564
|
-
fill(values, res = {}) {
|
|
565
|
-
const params = {};
|
|
566
|
-
const reversedParams = this._orderedParams.slice().reverse();
|
|
567
|
-
for (let i = 0; i < reversedParams.length && i < values.length; i++) {
|
|
568
|
-
const key = reversedParams[i];
|
|
569
|
-
const mappedKey = this._paramMappings.find((m) => m.urlName === key)?.tableName ?? key;
|
|
570
|
-
let val = decodeURIComponent(values[i]);
|
|
571
|
-
if (this._paramMap[key] === 'number') {
|
|
572
|
-
val = parseInt(val);
|
|
573
|
-
}
|
|
574
|
-
params[mappedKey] = val;
|
|
575
|
-
}
|
|
576
|
-
res[this.absolutePath] = params;
|
|
577
|
-
if (values.length > reversedParams.length && this._parent) {
|
|
578
|
-
this._parent.fill(values.slice(reversedParams.length), res);
|
|
579
|
-
}
|
|
580
|
-
return res;
|
|
581
|
-
}
|
|
582
|
-
/**
|
|
583
|
-
* Fills parameters in path with values from paramMap.
|
|
584
|
-
* Use withStaticParams() beforehand in case parent paths contain parameters.
|
|
585
|
-
* @param paramMap Must contain all parameters for this route.
|
|
586
|
-
* @returns Absolute path.
|
|
587
|
-
*/
|
|
588
|
-
createAbsoluteUrl(paramMap = this._staticParams) {
|
|
589
|
-
if (!this._parent) {
|
|
590
|
-
return this.createUrl(this.absolutePath, paramMap);
|
|
591
|
-
}
|
|
592
|
-
else {
|
|
593
|
-
return RdtStringUtils.joinPaths(this._parent.createAbsoluteUrl(), this.createRelativeUrl(paramMap));
|
|
594
|
-
}
|
|
595
|
-
}
|
|
596
|
-
createRelativeUrl(paramMap = this._staticParams) {
|
|
597
|
-
return this.createUrl(this.path, paramMap);
|
|
598
|
-
}
|
|
599
|
-
createUrl(path, paramMap) {
|
|
600
|
-
const paramNames = Object.keys(this._paramMap);
|
|
601
|
-
paramNames.sort((a, b) => b.length - a.length);
|
|
602
|
-
const extendedParamMap = { ...paramMap };
|
|
603
|
-
this._paramMappings.forEach((mapping) => {
|
|
604
|
-
if (mapping.tableName in paramMap) {
|
|
605
|
-
extendedParamMap[mapping.urlName] = paramMap[mapping.tableName];
|
|
606
|
-
}
|
|
607
|
-
});
|
|
608
|
-
paramNames.forEach((param) => {
|
|
609
|
-
if (!(param in extendedParamMap)) {
|
|
610
|
-
throw new Error(`Param ${param} is missing for route ${this.absolutePath}. Please pass object with required parameters.`);
|
|
611
|
-
}
|
|
612
|
-
const value = extendedParamMap[param];
|
|
613
|
-
const type = this._paramMap[param];
|
|
614
|
-
if (Array.isArray(type) && !type.includes(`${value}`)) {
|
|
615
|
-
throw new Error(`Value "${value}" is not allowed for enum param "${param}" in route ${this.absolutePath}. Allowed values: ${type.join(', ')}`);
|
|
616
|
-
}
|
|
617
|
-
path = path.replace(`:${param}`, encodeURIComponent(`${value}`));
|
|
618
|
-
});
|
|
619
|
-
return path;
|
|
620
|
-
}
|
|
621
|
-
toAngularRoute() {
|
|
622
|
-
return new RdtAngularRoute(this);
|
|
623
|
-
}
|
|
624
|
-
/**
|
|
625
|
-
* Provides static mapping for parameter names tableName -> urlName.
|
|
626
|
-
* Returns new instance of RdtRoute<T> that is clone of
|
|
627
|
-
* this route and its parents (NOT children).
|
|
628
|
-
* @returns Clone of this route with mapped parameter names.
|
|
629
|
-
*/
|
|
630
|
-
withParamMappings(mappings) {
|
|
631
|
-
const clone = this.clone();
|
|
632
|
-
clone._paramMappings = mappings;
|
|
633
|
-
return clone;
|
|
634
|
-
}
|
|
635
|
-
/**
|
|
636
|
-
* Sets parameters for itself or parents.
|
|
637
|
-
* Returns new instance of RdtRoute<T> that is clone of
|
|
638
|
-
* this route and its parents (NOT children).
|
|
639
|
-
* @returns Clone of this route with static parameters set.
|
|
640
|
-
*/
|
|
641
|
-
withStaticParams(param1, param2) {
|
|
642
|
-
const clone = this.clone();
|
|
643
|
-
if (param2) {
|
|
644
|
-
const routeAbsPath = param1.absolutePath;
|
|
645
|
-
let current = clone;
|
|
646
|
-
while (current) {
|
|
647
|
-
if (current.absolutePath === routeAbsPath) {
|
|
648
|
-
current._staticParams = param2;
|
|
649
|
-
}
|
|
650
|
-
current = current._parent;
|
|
651
|
-
}
|
|
652
|
-
}
|
|
653
|
-
else {
|
|
654
|
-
clone._staticParams = param1;
|
|
655
|
-
}
|
|
656
|
-
return clone;
|
|
657
|
-
}
|
|
658
|
-
withQueryParams(queryParams) {
|
|
659
|
-
const clone = this.clone();
|
|
660
|
-
clone._queryParams = { ...queryParams };
|
|
661
|
-
return clone;
|
|
662
|
-
}
|
|
663
|
-
withStateParams(stateParams) {
|
|
664
|
-
const clone = this.clone();
|
|
665
|
-
clone._stateParams = { ...stateParams };
|
|
666
|
-
return clone;
|
|
667
|
-
}
|
|
668
|
-
/**
|
|
669
|
-
* @returns Partial of object that only contains properties necessary to create this route.
|
|
670
|
-
*/
|
|
671
|
-
extractRouteParams(row) {
|
|
672
|
-
const res = {};
|
|
673
|
-
this._orderedParams.forEach((param) => {
|
|
674
|
-
const mapping = this._paramMappings.find((m) => m.urlName === param);
|
|
675
|
-
const mappedParam = (mapping?.tableName ?? param);
|
|
676
|
-
if (mappedParam in row) {
|
|
677
|
-
res[mappedParam] = row[mappedParam];
|
|
678
|
-
}
|
|
679
|
-
});
|
|
680
|
-
return res;
|
|
681
|
-
}
|
|
682
|
-
getStaticParams() {
|
|
683
|
-
const res = new RdtParameters();
|
|
684
|
-
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
685
|
-
let current = this;
|
|
686
|
-
while (current) {
|
|
687
|
-
res.set(current, current._staticParams);
|
|
688
|
-
current = current._parent;
|
|
689
|
-
}
|
|
690
|
-
return res;
|
|
691
|
-
}
|
|
692
|
-
/**
|
|
693
|
-
* @returns New instance of RdtRoute<T> that is clone of
|
|
694
|
-
* this route and its parents (NOT children).
|
|
695
|
-
*/
|
|
696
|
-
clone() {
|
|
697
|
-
const clone = new RdtRoute();
|
|
698
|
-
clone._name = this._name;
|
|
699
|
-
clone._parent = this._parent?.clone() ?? null;
|
|
700
|
-
clone._path = this.path;
|
|
701
|
-
clone._entryDisabled = this._entryDisabled;
|
|
702
|
-
clone._orderedParams = [...this._orderedParams];
|
|
703
|
-
clone._paramMap = { ...this._paramMap };
|
|
704
|
-
clone._regex = this._regex ? new RegExp(this._regex) : null;
|
|
705
|
-
clone._paramMappings = this._paramMappings;
|
|
706
|
-
clone._staticParams = { ...this._staticParams };
|
|
707
|
-
clone._stateParams = { ...this._stateParams };
|
|
708
|
-
clone._canBeEntered = this._canBeEntered;
|
|
709
|
-
clone._queryParams = { ...this._queryParams };
|
|
710
|
-
return clone;
|
|
711
|
-
}
|
|
712
|
-
static getGroup(type) {
|
|
713
|
-
if (Array.isArray(type)) {
|
|
714
|
-
return ('(' +
|
|
715
|
-
type.map((v) => v.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('|') +
|
|
716
|
-
')');
|
|
717
|
-
}
|
|
718
|
-
return RdtRoute.groups[type];
|
|
719
|
-
}
|
|
720
|
-
setRegex() {
|
|
721
|
-
const params = Object.keys(this._paramMap);
|
|
722
|
-
params.sort((a, b) => b.length - a.length);
|
|
723
|
-
let substituted = this.path;
|
|
724
|
-
params.forEach((p) => {
|
|
725
|
-
const type = this._paramMap[p];
|
|
726
|
-
if (!type) {
|
|
727
|
-
throw new Error('Params is not set i');
|
|
728
|
-
}
|
|
729
|
-
substituted = substituted.replace(`:${p}`, RdtRoute.getGroup(type));
|
|
730
|
-
});
|
|
731
|
-
if (substituted === '') {
|
|
732
|
-
this._regex = null;
|
|
733
|
-
}
|
|
734
|
-
else {
|
|
735
|
-
this._regex = new RegExp(substituted);
|
|
736
|
-
}
|
|
737
|
-
}
|
|
738
|
-
static fromBuilder(builder) {
|
|
739
|
-
const route = new RdtRoute();
|
|
740
|
-
route._name = builder.name;
|
|
741
|
-
route._path = builder.path;
|
|
742
|
-
route._entryDisabled = builder.entryDisabled;
|
|
743
|
-
route._orderedParams = builder.orderedParams;
|
|
744
|
-
route._canBeEntered = builder.canBeEntered;
|
|
745
|
-
route._paramMap = builder.paramMap;
|
|
746
|
-
route._regex = builder.regex ? new RegExp(builder.regex) : null;
|
|
747
|
-
route._paramMappings = builder.paramMappings;
|
|
748
|
-
route._children = builder.children;
|
|
749
|
-
route._data = builder.data;
|
|
750
|
-
route._children.forEach((child) => (child._parent = route));
|
|
751
|
-
route.setRegex();
|
|
752
|
-
return route;
|
|
753
|
-
}
|
|
754
|
-
static groups = {
|
|
755
|
-
string: '([^/]+)',
|
|
756
|
-
number: '(-?\\d+)',
|
|
757
|
-
};
|
|
758
|
-
}
|
|
759
|
-
|
|
760
|
-
/**
|
|
761
|
-
* Injection token holding all application routes as a flat array.
|
|
762
|
-
* Used internally by `RdtRouterService` to match URLs and enable type-safe navigation.
|
|
763
|
-
* Prefer using `provideRdtRoutes()` instead of providing this token directly.
|
|
764
|
-
*/
|
|
765
|
-
const RDT_ROUTES_PROVIDER = new InjectionToken('RDT_ROUTES_PROVIDER');
|
|
766
|
-
/**
|
|
767
|
-
* Provides all application routes to `RdtRouterService`.
|
|
768
|
-
* Accepts either a route module object (`import * as routes from './routes'`)
|
|
769
|
-
* or a plain array of `RdtRoute` instances.
|
|
770
|
-
*
|
|
771
|
-
* @example
|
|
772
|
-
* ```ts
|
|
773
|
-
* import * as ALL_ROUTES from './rdt-routes';
|
|
774
|
-
*
|
|
775
|
-
* export const appConfig: ApplicationConfig = {
|
|
776
|
-
* providers: [
|
|
777
|
-
* provideRdtRoutes(ALL_ROUTES),
|
|
778
|
-
* ],
|
|
779
|
-
* };
|
|
780
|
-
* ```
|
|
781
|
-
*/
|
|
782
|
-
function provideRdtRoutes(routes) {
|
|
783
|
-
const routeArray = Array.isArray(routes)
|
|
784
|
-
? routes
|
|
785
|
-
: Object.values(routes).filter((v) => v instanceof RdtRoute);
|
|
786
|
-
return makeEnvironmentProviders([
|
|
787
|
-
{ provide: RDT_ROUTES_PROVIDER, useValue: routeArray },
|
|
788
|
-
]);
|
|
789
|
-
}
|
|
790
|
-
|
|
791
|
-
const DEFAULT_TARGET = '_self';
|
|
792
|
-
const RDT_STATE_PARAMS_KEY = 'RdtStateParams';
|
|
793
|
-
/**
|
|
794
|
-
* Central navigation service for type-safe routing.
|
|
795
|
-
* Wraps Angular's `Router` with `RdtRoute`-based navigation, URL parsing, and history tracking.
|
|
796
|
-
* Requires `RDT_ROUTES_PROVIDER` to be configured with all application routes.
|
|
797
|
-
*/
|
|
798
|
-
class RdtRouterService {
|
|
799
|
-
allRoutes = inject(RDT_ROUTES_PROVIDER, { optional: true });
|
|
800
|
-
baseHref = inject(PlatformLocation).getBaseHrefFromDOM();
|
|
801
|
-
location = inject(Location);
|
|
802
|
-
router = inject(Router);
|
|
803
|
-
_previousUrl = null;
|
|
804
|
-
_currentUrl = null;
|
|
805
|
-
get previousUrl() {
|
|
806
|
-
return this._previousUrl;
|
|
807
|
-
}
|
|
808
|
-
get currentUrl() {
|
|
809
|
-
return this._currentUrl;
|
|
810
|
-
}
|
|
811
|
-
parsePreviousUrl() {
|
|
812
|
-
return this._previousUrl ? this.parseAbsoluteUrl(this._previousUrl) : null;
|
|
813
|
-
}
|
|
814
|
-
parseCurrentUrl() {
|
|
815
|
-
return this._currentUrl ? this.parseAbsoluteUrl(this._currentUrl) : null;
|
|
816
|
-
}
|
|
817
|
-
navigationEnd$ = this.router.events.pipe(filter((e) => e instanceof NavigationEnd));
|
|
818
|
-
nextNavigationEnd$ = this.navigationEnd$.pipe(take(1));
|
|
819
|
-
constructor() {
|
|
820
|
-
if (this.allRoutes === null) {
|
|
821
|
-
console.warn('All routes not provided. Make sure to provide RDT_ROUTES_PROVIDER with RdtRoute[].');
|
|
822
|
-
this.allRoutes = [];
|
|
823
|
-
}
|
|
824
|
-
this.navigationEnd$.subscribe((e) => {
|
|
825
|
-
this._previousUrl = this._currentUrl;
|
|
826
|
-
this._currentUrl = e.url;
|
|
827
|
-
});
|
|
828
|
-
}
|
|
829
|
-
/**
|
|
830
|
-
* @returns window.history.state extended by state parameters passed from parent window.
|
|
831
|
-
*/
|
|
832
|
-
getHistoryState() {
|
|
833
|
-
if ('RdtStateParams' in window &&
|
|
834
|
-
typeof window[RDT_STATE_PARAMS_KEY] === 'object') {
|
|
835
|
-
return { ...window[RDT_STATE_PARAMS_KEY], ...(history.state ?? {}) };
|
|
836
|
-
}
|
|
837
|
-
else {
|
|
838
|
-
return history.state ?? {};
|
|
839
|
-
}
|
|
840
|
-
}
|
|
841
|
-
/**
|
|
842
|
-
* Navigates to the specified route.
|
|
843
|
-
* @param link Route to navigate to.
|
|
844
|
-
* @param params Parameter of route passed as first argument or RdtParameters object.
|
|
845
|
-
* @param extras Allows you to specify additional parameters for navigation. These extras have higher priority than those defined in the route.
|
|
846
|
-
* @returns Promise that resolves when navigation is complete.
|
|
847
|
-
*/
|
|
848
|
-
navigate(link, params, extras) {
|
|
849
|
-
let url;
|
|
850
|
-
if (params instanceof RdtParameters) {
|
|
851
|
-
let linkCpy = link;
|
|
852
|
-
for (let r = linkCpy; r !== null; r = r.parent) {
|
|
853
|
-
const p = params.get(r);
|
|
854
|
-
if (p) {
|
|
855
|
-
linkCpy = linkCpy.withStaticParams(r, p);
|
|
856
|
-
}
|
|
857
|
-
}
|
|
858
|
-
url = linkCpy.createAbsoluteUrl();
|
|
859
|
-
}
|
|
860
|
-
else {
|
|
861
|
-
url = params ? link.createAbsoluteUrl(params) : link.createAbsoluteUrl();
|
|
862
|
-
}
|
|
863
|
-
const target = extras?.target ?? DEFAULT_TARGET;
|
|
864
|
-
const queryParams = extras?.query ?? link.queryParams;
|
|
865
|
-
const stateParams = extras?.state ?? link.stateParams;
|
|
866
|
-
if (target === '_self') {
|
|
867
|
-
return this.router.navigate([url], {
|
|
868
|
-
queryParams: queryParams,
|
|
869
|
-
state: stateParams,
|
|
870
|
-
replaceUrl: extras?.replaceUrl,
|
|
871
|
-
});
|
|
872
|
-
}
|
|
873
|
-
const absolutePath = RdtStringUtils.createAbsoluteUrl(url, this.baseHref);
|
|
874
|
-
const pathWithParams = RdtStringUtils.appendQueryParams(absolutePath, queryParams);
|
|
875
|
-
const win = window.open(pathWithParams, target);
|
|
876
|
-
if (win) {
|
|
877
|
-
win[RDT_STATE_PARAMS_KEY] = stateParams;
|
|
878
|
-
}
|
|
879
|
-
return Promise.resolve(true);
|
|
880
|
-
}
|
|
881
|
-
navigateHome(extras) {
|
|
882
|
-
return this.router.navigateByUrl(RdtStringUtils.appendQueryParams('/', extras?.query ?? {}), {
|
|
883
|
-
state: extras?.state,
|
|
884
|
-
replaceUrl: extras?.replaceUrl,
|
|
885
|
-
});
|
|
886
|
-
}
|
|
887
|
-
navigateBack(extras) {
|
|
888
|
-
const parsed = this.parseAbsoluteUrl();
|
|
889
|
-
if (parsed) {
|
|
890
|
-
let route = parsed.route.withStaticParams(parsed.params);
|
|
891
|
-
do {
|
|
892
|
-
route = route.parent;
|
|
893
|
-
} while (route && route.entryDisabled);
|
|
894
|
-
// In case route has no ancestor with allowed entry,
|
|
895
|
-
if (!route) {
|
|
896
|
-
return this.navigateHome(extras);
|
|
897
|
-
}
|
|
898
|
-
if (extras) {
|
|
899
|
-
if (extras.query) {
|
|
900
|
-
route = route.withQueryParams(extras.query);
|
|
901
|
-
}
|
|
902
|
-
if (extras.state) {
|
|
903
|
-
route = route.withStateParams(extras.state);
|
|
904
|
-
}
|
|
905
|
-
}
|
|
906
|
-
return this.navigate(route, extras);
|
|
907
|
-
}
|
|
908
|
-
else {
|
|
909
|
-
console.warn(`Cannot go back from ${this.location.path()} because no route matches current url`);
|
|
910
|
-
return null;
|
|
911
|
-
}
|
|
912
|
-
}
|
|
913
|
-
/**
|
|
914
|
-
* This method will try to find matching route for given absolute URL, extract its parameters and return them.
|
|
915
|
-
* If no route matches the URL, it returns null. Parameters are extracted only for the last matching route, not ancestors.
|
|
916
|
-
* @param url Absolute URL to parse (e.g. '/home/details/123?query=abc').
|
|
917
|
-
* @returns Object containing the matching route and its parameters, or null if no route is matching.
|
|
918
|
-
*/
|
|
919
|
-
parseAbsoluteUrl(url = this.location.path()) {
|
|
920
|
-
const stripped = RdtStringUtils.stripQueryParams(url);
|
|
921
|
-
if (this.allRoutes) {
|
|
922
|
-
for (const route of this.allRoutes) {
|
|
923
|
-
const params = route.parseAbsoluteUrl(stripped);
|
|
924
|
-
if (params) {
|
|
925
|
-
return { route, params };
|
|
926
|
-
}
|
|
927
|
-
}
|
|
928
|
-
}
|
|
929
|
-
return null;
|
|
930
|
-
}
|
|
931
|
-
extractAllParams(currentRoute) {
|
|
932
|
-
if (!currentRoute) {
|
|
933
|
-
const parsed = this.parseAbsoluteUrl();
|
|
934
|
-
if (!parsed) {
|
|
935
|
-
console.warn('No route matches current url.');
|
|
936
|
-
return null;
|
|
937
|
-
}
|
|
938
|
-
currentRoute = parsed.route;
|
|
939
|
-
}
|
|
940
|
-
const url = this.location.path();
|
|
941
|
-
return currentRoute.parseAbsoluteUrl(url);
|
|
942
|
-
}
|
|
943
|
-
isParentOfCurrentLocation(route) {
|
|
944
|
-
return true;
|
|
945
|
-
}
|
|
946
|
-
removeQueryParams(...paramNames) {
|
|
947
|
-
const regex = new RegExp(`[?&](${paramNames.join('|')})=[^&]*`, 'g');
|
|
948
|
-
return history.replaceState(history.state, '', location.pathname + location.search.replace(regex, '').replace(/^&/, '?'));
|
|
949
|
-
}
|
|
950
|
-
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.1.2", ngImport: i0, type: RdtRouterService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
951
|
-
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.1.2", ngImport: i0, type: RdtRouterService, providedIn: 'root' });
|
|
952
|
-
}
|
|
953
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.1.2", ngImport: i0, type: RdtRouterService, decorators: [{
|
|
954
|
-
type: Injectable,
|
|
955
|
-
args: [{
|
|
956
|
-
providedIn: 'root',
|
|
957
|
-
}]
|
|
958
|
-
}], ctorParameters: () => [] });
|
|
959
|
-
|
|
960
|
-
const DEFAULT_PARAM_TYPE = 'number';
|
|
961
|
-
/**
|
|
962
|
-
* Fluent builder for creating `RdtRoute` instances.
|
|
963
|
-
* Routes are defined once in a central file and referenced everywhere by object, eliminating string-based path typos.
|
|
964
|
-
*
|
|
965
|
-
* @example
|
|
966
|
-
* ```ts
|
|
967
|
-
* const USER_DETAIL = new RdtRouteBuilder<{ userId: number }>()
|
|
968
|
-
* .withName('User Detail')
|
|
969
|
-
* .withPath(':userId')
|
|
970
|
-
* .withParam('userId', 'number')
|
|
971
|
-
* .withCanBeEntered((route, params) => params.params.userId !== 0)
|
|
972
|
-
* .build();
|
|
973
|
-
* ```
|
|
974
|
-
*
|
|
975
|
-
* @typeParam T - Shape of the route's parameters. Enforced at compile time when navigating or creating URLs.
|
|
976
|
-
*/
|
|
977
|
-
class RdtRouteBuilder extends RdtRouteBase {
|
|
978
|
-
get canBeEntered() {
|
|
979
|
-
return this._canBeEntered;
|
|
980
|
-
}
|
|
981
|
-
get orderedParams() {
|
|
982
|
-
return this._orderedParams;
|
|
983
|
-
}
|
|
984
|
-
get paramMappings() {
|
|
985
|
-
return this._paramMappings;
|
|
986
|
-
}
|
|
987
|
-
/**
|
|
988
|
-
* CanBeEntered function is synchronous function which
|
|
989
|
-
* lets framework hide menu buttons and disable links.
|
|
990
|
-
* Function is run in environment injection context, so
|
|
991
|
-
* you can inject global services, tokens, etc.
|
|
992
|
-
* @param fn
|
|
993
|
-
*/
|
|
994
|
-
withCanBeEntered(fn) {
|
|
995
|
-
this._canBeEntered = fn;
|
|
996
|
-
return this;
|
|
997
|
-
}
|
|
998
|
-
/**
|
|
999
|
-
* @deprecated Use withCanBeEntered() instead.
|
|
1000
|
-
*/
|
|
1001
|
-
setCanBeEntered(fn) {
|
|
1002
|
-
return this.withCanBeEntered(fn);
|
|
1003
|
-
}
|
|
1004
|
-
/**
|
|
1005
|
-
* Entry disabled route will generate disabled breadcrumb.
|
|
1006
|
-
* It should not have any component nor module added to it.
|
|
1007
|
-
* @param value
|
|
1008
|
-
* @returns
|
|
1009
|
-
*/
|
|
1010
|
-
withEntryDisabled(value = true) {
|
|
1011
|
-
this._entryDisabled = value;
|
|
1012
|
-
return this;
|
|
1013
|
-
}
|
|
1014
|
-
/**
|
|
1015
|
-
* @deprecated Use withEntryDisabled() instead.
|
|
1016
|
-
*/
|
|
1017
|
-
setEntryDisabled(value = true) {
|
|
1018
|
-
return this.withEntryDisabled(value);
|
|
1019
|
-
}
|
|
1020
|
-
/**
|
|
1021
|
-
* Sets path relative to parent. It is defined the same way as Angular route path including parameters.
|
|
1022
|
-
* You need to call setParam() for each parameter you use in the path.
|
|
1023
|
-
*/
|
|
1024
|
-
withPath(path) {
|
|
1025
|
-
this._path = path;
|
|
1026
|
-
const s = path.split('/');
|
|
1027
|
-
const params = [];
|
|
1028
|
-
s.forEach((part) => {
|
|
1029
|
-
if (part.includes(':')) {
|
|
1030
|
-
const param = part.split(':')[1];
|
|
1031
|
-
if (param) {
|
|
1032
|
-
params.push(param);
|
|
1033
|
-
if (!this._paramMap[param]) {
|
|
1034
|
-
this._paramMap[param] = DEFAULT_PARAM_TYPE;
|
|
1035
|
-
}
|
|
1036
|
-
}
|
|
1037
|
-
}
|
|
1038
|
-
});
|
|
1039
|
-
this._orderedParams = params;
|
|
1040
|
-
return this;
|
|
1041
|
-
}
|
|
1042
|
-
/**
|
|
1043
|
-
* @deprecated Use withPath() instead.
|
|
1044
|
-
*/
|
|
1045
|
-
setPath(path) {
|
|
1046
|
-
return this.withPath(path);
|
|
1047
|
-
}
|
|
1048
|
-
/**
|
|
1049
|
-
* Sets Angular route data.
|
|
1050
|
-
* @param data Angular route data object. Breadcrumb key is merged into it.
|
|
1051
|
-
*/
|
|
1052
|
-
withData(data) {
|
|
1053
|
-
this._data = data;
|
|
1054
|
-
return this;
|
|
1055
|
-
}
|
|
1056
|
-
/**
|
|
1057
|
-
* @deprecated Use withData() instead.
|
|
1058
|
-
*/
|
|
1059
|
-
setData(data) {
|
|
1060
|
-
return this.withData(data);
|
|
1061
|
-
}
|
|
1062
|
-
/**
|
|
1063
|
-
* Defines parameter type and lets framework parse it.
|
|
1064
|
-
* @param paramName Name of the parameter in the path.
|
|
1065
|
-
* @param type 'string', 'number', or an array of allowed string values (enum).
|
|
1066
|
-
*/
|
|
1067
|
-
withParam(paramName, type) {
|
|
1068
|
-
this._paramMap[paramName] = type;
|
|
1069
|
-
return this;
|
|
1070
|
-
}
|
|
1071
|
-
/**
|
|
1072
|
-
* @deprecated Use withParam() instead.
|
|
1073
|
-
*/
|
|
1074
|
-
setParam(paramName, type) {
|
|
1075
|
-
return this.withParam(paramName, type);
|
|
1076
|
-
}
|
|
1077
|
-
/**
|
|
1078
|
-
* Sets name to display in breadcrumb, etc.
|
|
1079
|
-
*/
|
|
1080
|
-
withName(name) {
|
|
1081
|
-
this._name = name;
|
|
1082
|
-
return this;
|
|
1083
|
-
}
|
|
1084
|
-
/**
|
|
1085
|
-
* @deprecated Use withName() instead.
|
|
1086
|
-
*/
|
|
1087
|
-
setName(name) {
|
|
1088
|
-
return this.withName(name);
|
|
1089
|
-
}
|
|
1090
|
-
/**
|
|
1091
|
-
* Call .build() on children before you call this method!
|
|
1092
|
-
*/
|
|
1093
|
-
withChildren(...children) {
|
|
1094
|
-
this._children = children;
|
|
1095
|
-
return this;
|
|
1096
|
-
}
|
|
1097
|
-
/**
|
|
1098
|
-
* @deprecated Use withChildren() instead.
|
|
1099
|
-
*/
|
|
1100
|
-
setChildren(...children) {
|
|
1101
|
-
return this.withChildren(...children);
|
|
1102
|
-
}
|
|
1103
|
-
build() {
|
|
1104
|
-
if (typeof this._path !== 'string') {
|
|
1105
|
-
throw new Error('Please provide path for route. Empty string is acceptable.');
|
|
1106
|
-
}
|
|
1107
|
-
return RdtRoute.fromBuilder(this);
|
|
1108
|
-
}
|
|
1109
|
-
}
|
|
1110
|
-
|
|
1111
|
-
const EXACT_TRUE_OPTS = {
|
|
1112
|
-
paths: 'exact',
|
|
1113
|
-
queryParams: 'exact',
|
|
1114
|
-
fragment: 'ignored',
|
|
1115
|
-
matrixParams: 'ignored',
|
|
1116
|
-
};
|
|
1117
|
-
const EXACT_FALSE_OPTS = {
|
|
1118
|
-
paths: 'subset',
|
|
1119
|
-
queryParams: 'subset',
|
|
1120
|
-
fragment: 'ignored',
|
|
1121
|
-
matrixParams: 'ignored',
|
|
1122
|
-
};
|
|
1123
|
-
/**
|
|
1124
|
-
* Directive that appends class to host if any of the watched routes is active.
|
|
1125
|
-
*/
|
|
1126
|
-
class RdtAnyRouteActiveDirective {
|
|
1127
|
-
destroyRef = inject(DestroyRef);
|
|
1128
|
-
router = inject(Router);
|
|
1129
|
-
renderer = inject(Renderer2);
|
|
1130
|
-
elRef = inject(ElementRef);
|
|
1131
|
-
set anyRouteActive(data) {
|
|
1132
|
-
this.classes = this.parseClasses(data);
|
|
1133
|
-
}
|
|
1134
|
-
watchedRoutes = [];
|
|
1135
|
-
set anyRouteActiveOptions(value) {
|
|
1136
|
-
this._anyRouteActiveOptions = this.parseOptions(value);
|
|
1137
|
-
}
|
|
1138
|
-
_anyRouteActiveOptions = EXACT_FALSE_OPTS;
|
|
1139
|
-
ariaCurrentWhenActive;
|
|
1140
|
-
get isActive() {
|
|
1141
|
-
return this._isActive;
|
|
1142
|
-
}
|
|
1143
|
-
_isActive = false;
|
|
1144
|
-
classes = [];
|
|
1145
|
-
ngOnInit() {
|
|
1146
|
-
this.update();
|
|
1147
|
-
this.listenRouterEvents();
|
|
1148
|
-
}
|
|
1149
|
-
ngOnChanges(changes) {
|
|
1150
|
-
if (changes['anyRouteActive']?.previousValue) {
|
|
1151
|
-
const prev = this.parseClasses(changes['anyRouteActive'].previousValue);
|
|
1152
|
-
prev.forEach((c) => this.removeClass(c));
|
|
1153
|
-
}
|
|
1154
|
-
this.update();
|
|
1155
|
-
}
|
|
1156
|
-
update() {
|
|
1157
|
-
this._isActive = this.watchedRoutes.some((route) => {
|
|
1158
|
-
const param = typeof route === 'string' ? route : this.router.createUrlTree(route);
|
|
1159
|
-
return this.router.isActive(param, this._anyRouteActiveOptions);
|
|
1160
|
-
});
|
|
1161
|
-
this.setClasses();
|
|
1162
|
-
this.setAriaCurrent();
|
|
1163
|
-
}
|
|
1164
|
-
setAriaCurrent() {
|
|
1165
|
-
const el = this.elRef.nativeElement;
|
|
1166
|
-
if (this._isActive && this.ariaCurrentWhenActive !== undefined) {
|
|
1167
|
-
const value = this.ariaCurrentWhenActive.toString();
|
|
1168
|
-
this.renderer.setAttribute(el, 'aria-current', value);
|
|
1169
|
-
}
|
|
1170
|
-
else {
|
|
1171
|
-
this.renderer.removeAttribute(el, 'aria-current');
|
|
1172
|
-
}
|
|
1173
|
-
}
|
|
1174
|
-
setClasses() {
|
|
1175
|
-
if (this._isActive) {
|
|
1176
|
-
this.classes.forEach((c) => this.addClass(c));
|
|
1177
|
-
}
|
|
1178
|
-
else {
|
|
1179
|
-
this.classes.forEach((c) => this.removeClass(c));
|
|
1180
|
-
}
|
|
1181
|
-
}
|
|
1182
|
-
addClass(className) {
|
|
1183
|
-
this.renderer.addClass(this.elRef.nativeElement, className);
|
|
1184
|
-
}
|
|
1185
|
-
removeClass(className) {
|
|
1186
|
-
this.renderer.removeClass(this.elRef.nativeElement, className);
|
|
1187
|
-
}
|
|
1188
|
-
listenRouterEvents() {
|
|
1189
|
-
this.router.events
|
|
1190
|
-
.pipe(filter((ev) => ev instanceof NavigationEnd), takeUntilDestroyed(this.destroyRef))
|
|
1191
|
-
.subscribe(() => this.update());
|
|
1192
|
-
}
|
|
1193
|
-
parseClasses(data) {
|
|
1194
|
-
const classes = Array.isArray(data) ? data : data.split(' ');
|
|
1195
|
-
return classes.filter((c) => !!c);
|
|
1196
|
-
}
|
|
1197
|
-
parseOptions(input) {
|
|
1198
|
-
if ('exact' in input) {
|
|
1199
|
-
if (input.exact) {
|
|
1200
|
-
return EXACT_TRUE_OPTS;
|
|
1201
|
-
}
|
|
1202
|
-
else {
|
|
1203
|
-
return EXACT_FALSE_OPTS;
|
|
1204
|
-
}
|
|
1205
|
-
}
|
|
1206
|
-
else {
|
|
1207
|
-
return input;
|
|
1208
|
-
}
|
|
1209
|
-
}
|
|
1210
|
-
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.1.2", ngImport: i0, type: RdtAnyRouteActiveDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive });
|
|
1211
|
-
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "21.1.2", type: RdtAnyRouteActiveDirective, isStandalone: true, selector: "[rdtAnyRouteActive]", inputs: { anyRouteActive: ["rdtAnyRouteActive", "anyRouteActive"], watchedRoutes: "watchedRoutes", anyRouteActiveOptions: "anyRouteActiveOptions", ariaCurrentWhenActive: "ariaCurrentWhenActive" }, providers: [RouterModule], usesOnChanges: true, ngImport: i0 });
|
|
1212
|
-
}
|
|
1213
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.1.2", ngImport: i0, type: RdtAnyRouteActiveDirective, decorators: [{
|
|
1214
|
-
type: Directive,
|
|
1215
|
-
args: [{
|
|
1216
|
-
selector: '[rdtAnyRouteActive]',
|
|
1217
|
-
standalone: true,
|
|
1218
|
-
providers: [RouterModule],
|
|
1219
|
-
}]
|
|
1220
|
-
}], propDecorators: { anyRouteActive: [{
|
|
1221
|
-
type: Input,
|
|
1222
|
-
args: [{ required: true, alias: 'rdtAnyRouteActive' }]
|
|
1223
|
-
}], watchedRoutes: [{
|
|
1224
|
-
type: Input,
|
|
1225
|
-
args: [{ required: true }]
|
|
1226
|
-
}], anyRouteActiveOptions: [{
|
|
1227
|
-
type: Input
|
|
1228
|
-
}], ariaCurrentWhenActive: [{
|
|
1229
|
-
type: Input
|
|
1230
|
-
}] } });
|
|
1231
|
-
|
|
1232
|
-
class RdtBackLinkDirective {
|
|
1233
|
-
destroyRef = inject(DestroyRef);
|
|
1234
|
-
location = inject(Location);
|
|
1235
|
-
elRef = inject((ElementRef));
|
|
1236
|
-
buttonClass = inject(RDT_BUTTON_BASE_PROVIDER);
|
|
1237
|
-
buttonRef = inject(this.buttonClass, {
|
|
1238
|
-
optional: true,
|
|
1239
|
-
host: true,
|
|
1240
|
-
});
|
|
1241
|
-
constructor() {
|
|
1242
|
-
afterNextRender(() => {
|
|
1243
|
-
if (this.buttonRef) {
|
|
1244
|
-
this.buttonRef.click.subscribe(() => this.location.back());
|
|
1245
|
-
}
|
|
1246
|
-
else {
|
|
1247
|
-
fromEvent(this.elRef.nativeElement, 'click')
|
|
1248
|
-
.pipe(takeUntilDestroyed(this.destroyRef))
|
|
1249
|
-
.subscribe(() => this.location.back());
|
|
1250
|
-
}
|
|
1251
|
-
});
|
|
1252
|
-
}
|
|
1253
|
-
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.1.2", ngImport: i0, type: RdtBackLinkDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive });
|
|
1254
|
-
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "21.1.2", type: RdtBackLinkDirective, isStandalone: true, selector: "[rdtBackLink]", ngImport: i0 });
|
|
1255
|
-
}
|
|
1256
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.1.2", ngImport: i0, type: RdtBackLinkDirective, decorators: [{
|
|
1257
|
-
type: Directive,
|
|
1258
|
-
args: [{
|
|
1259
|
-
selector: '[rdtBackLink]',
|
|
1260
|
-
}]
|
|
1261
|
-
}], ctorParameters: () => [] });
|
|
1262
|
-
|
|
1263
|
-
const PERMISSION_DISABLED_KEY = Symbol();
|
|
1264
|
-
class RdtRouterLinkDirective {
|
|
1265
|
-
buttonClass = inject(RDT_BUTTON_BASE_PROVIDER);
|
|
1266
|
-
buttonRef = inject(this.buttonClass, {
|
|
1267
|
-
host: true,
|
|
1268
|
-
optional: true,
|
|
1269
|
-
});
|
|
1270
|
-
routerLinkRef = inject(RouterLink, { self: true });
|
|
1271
|
-
envInjector = inject(EnvironmentInjector);
|
|
1272
|
-
routeInput = input.required({ ...(ngDevMode ? { debugName: "routeInput" } : {}), alias: 'rdtRouterLink' });
|
|
1273
|
-
route = linkedSignal(() => this.routeInput(), ...(ngDevMode ? [{ debugName: "route" }] : []));
|
|
1274
|
-
target = input('_self', ...(ngDevMode ? [{ debugName: "target" }] : []));
|
|
1275
|
-
params = input(...(ngDevMode ? [undefined, { debugName: "params" }] : []));
|
|
1276
|
-
queryParams = input(...(ngDevMode ? [undefined, { debugName: "queryParams" }] : []));
|
|
1277
|
-
stateParams = input(...(ngDevMode ? [undefined, { debugName: "stateParams" }] : []));
|
|
1278
|
-
disabled = input(false, { ...(ngDevMode ? { debugName: "disabled" } : {}), alias: 'rdtDisabled',
|
|
1279
|
-
transform: booleanAttribute });
|
|
1280
|
-
canBeEntered = computed(() => {
|
|
1281
|
-
const route = this.route();
|
|
1282
|
-
const disabled = this.disabled();
|
|
1283
|
-
if (!route || disabled) {
|
|
1284
|
-
return false;
|
|
1285
|
-
}
|
|
1286
|
-
const combinedParams = {
|
|
1287
|
-
params: this.params(),
|
|
1288
|
-
query: this.queryParams(),
|
|
1289
|
-
state: this.stateParams(),
|
|
1290
|
-
};
|
|
1291
|
-
return route.canBeEntered(this.envInjector, combinedParams);
|
|
1292
|
-
}, ...(ngDevMode ? [{ debugName: "canBeEntered" }] : []));
|
|
1293
|
-
updateEffect = effect(() => {
|
|
1294
|
-
if (this.buttonRef) {
|
|
1295
|
-
this.updateButton();
|
|
1296
|
-
}
|
|
1297
|
-
else {
|
|
1298
|
-
this.updateRouterLink();
|
|
1299
|
-
}
|
|
1300
|
-
}, ...(ngDevMode ? [{ debugName: "updateEffect" }] : []));
|
|
1301
|
-
updateButton() {
|
|
1302
|
-
const route = this.route();
|
|
1303
|
-
if (!route) {
|
|
1304
|
-
this.clearButtonLink();
|
|
1305
|
-
return;
|
|
1306
|
-
}
|
|
1307
|
-
if (this.canBeEntered()) {
|
|
1308
|
-
this.setButtonLink(route);
|
|
1309
|
-
}
|
|
1310
|
-
else {
|
|
1311
|
-
this.disableButtonLink();
|
|
1312
|
-
}
|
|
1313
|
-
}
|
|
1314
|
-
setButtonLink(route) {
|
|
1315
|
-
const button = this.buttonRef;
|
|
1316
|
-
try {
|
|
1317
|
-
const ngHref = this.getNgHref(route);
|
|
1318
|
-
button.ngHref.set(ngHref);
|
|
1319
|
-
button.stateParams.set(this.stateParams() ?? route.stateParams);
|
|
1320
|
-
button.queryParams.set(this.queryParams() ?? route.queryParams);
|
|
1321
|
-
button.setDisabledState(false, PERMISSION_DISABLED_KEY);
|
|
1322
|
-
}
|
|
1323
|
-
catch (err) {
|
|
1324
|
-
console.error(err);
|
|
1325
|
-
this.disableButtonLink();
|
|
1326
|
-
}
|
|
1327
|
-
}
|
|
1328
|
-
disableButtonLink() {
|
|
1329
|
-
const button = this.buttonRef;
|
|
1330
|
-
button.ngHref.set(null);
|
|
1331
|
-
button.setDisabledState(true, PERMISSION_DISABLED_KEY);
|
|
1332
|
-
}
|
|
1333
|
-
clearButtonLink() {
|
|
1334
|
-
const button = this.buttonRef;
|
|
1335
|
-
button.ngHref.set(null);
|
|
1336
|
-
button.stateParams.set(undefined);
|
|
1337
|
-
button.queryParams.set(undefined);
|
|
1338
|
-
button.setDisabledState(false, PERMISSION_DISABLED_KEY);
|
|
1339
|
-
}
|
|
1340
|
-
updateRouterLink() {
|
|
1341
|
-
const route = this.route();
|
|
1342
|
-
if (!route) {
|
|
1343
|
-
this.clearRouterLink();
|
|
1344
|
-
}
|
|
1345
|
-
else if (this.canBeEntered()) {
|
|
1346
|
-
this.setRouterLink(route);
|
|
1347
|
-
}
|
|
1348
|
-
else {
|
|
1349
|
-
this.disableRouterLink();
|
|
1350
|
-
}
|
|
1351
|
-
}
|
|
1352
|
-
setRouterLink(route) {
|
|
1353
|
-
try {
|
|
1354
|
-
const ngHref = this.getNgHref(route);
|
|
1355
|
-
this.routerLinkRef.routerLink = ngHref;
|
|
1356
|
-
this.routerLinkRef.state = this.stateParams() ?? route.stateParams;
|
|
1357
|
-
this.routerLinkRef.queryParams = this.queryParams() ?? route.queryParams;
|
|
1358
|
-
this.routerLinkRef.ngOnChanges({});
|
|
1359
|
-
}
|
|
1360
|
-
catch (err) {
|
|
1361
|
-
console.error(err);
|
|
1362
|
-
this.disableRouterLink();
|
|
1363
|
-
}
|
|
1364
|
-
}
|
|
1365
|
-
disableRouterLink() {
|
|
1366
|
-
this.routerLinkRef.routerLink = null;
|
|
1367
|
-
this.routerLinkRef.ngOnChanges({});
|
|
1368
|
-
}
|
|
1369
|
-
clearRouterLink() {
|
|
1370
|
-
this.routerLinkRef.routerLink = null;
|
|
1371
|
-
this.routerLinkRef.state = undefined;
|
|
1372
|
-
this.routerLinkRef.queryParams = undefined;
|
|
1373
|
-
this.routerLinkRef.ngOnChanges({});
|
|
1374
|
-
}
|
|
1375
|
-
getNgHref(route) {
|
|
1376
|
-
return route.createAbsoluteUrl(this.params());
|
|
1377
|
-
}
|
|
1378
|
-
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.1.2", ngImport: i0, type: RdtRouterLinkDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive });
|
|
1379
|
-
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "21.1.2", type: RdtRouterLinkDirective, isStandalone: true, selector: "[rdtRouterLink]", inputs: { routeInput: { classPropertyName: "routeInput", publicName: "rdtRouterLink", isSignal: true, isRequired: true, transformFunction: null }, target: { classPropertyName: "target", publicName: "target", isSignal: true, isRequired: false, transformFunction: null }, params: { classPropertyName: "params", publicName: "params", isSignal: true, isRequired: false, transformFunction: null }, queryParams: { classPropertyName: "queryParams", publicName: "queryParams", isSignal: true, isRequired: false, transformFunction: null }, stateParams: { classPropertyName: "stateParams", publicName: "stateParams", isSignal: true, isRequired: false, transformFunction: null }, disabled: { classPropertyName: "disabled", publicName: "rdtDisabled", isSignal: true, isRequired: false, transformFunction: null } }, hostDirectives: [{ directive: i1.RouterLink, inputs: ["target", "target", "replaceUrl", "replaceUrl"] }], ngImport: i0 });
|
|
1380
|
-
}
|
|
1381
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.1.2", ngImport: i0, type: RdtRouterLinkDirective, decorators: [{
|
|
1382
|
-
type: Directive,
|
|
1383
|
-
args: [{
|
|
1384
|
-
selector: '[rdtRouterLink]',
|
|
1385
|
-
standalone: true,
|
|
1386
|
-
hostDirectives: [
|
|
1387
|
-
{
|
|
1388
|
-
directive: RouterLink,
|
|
1389
|
-
inputs: ['target', 'replaceUrl'],
|
|
1390
|
-
},
|
|
1391
|
-
],
|
|
1392
|
-
}]
|
|
1393
|
-
}], propDecorators: { routeInput: [{ type: i0.Input, args: [{ isSignal: true, alias: "rdtRouterLink", required: true }] }], target: [{ type: i0.Input, args: [{ isSignal: true, alias: "target", required: false }] }], params: [{ type: i0.Input, args: [{ isSignal: true, alias: "params", required: false }] }], queryParams: [{ type: i0.Input, args: [{ isSignal: true, alias: "queryParams", required: false }] }], stateParams: [{ type: i0.Input, args: [{ isSignal: true, alias: "stateParams", required: false }] }], disabled: [{ type: i0.Input, args: [{ isSignal: true, alias: "rdtDisabled", required: false }] }] } });
|
|
1394
|
-
|
|
1395
|
-
/**
|
|
1396
|
-
* `CanDeactivate` guard that blocks navigation when there are unsaved changes.
|
|
1397
|
-
* The routed component must implement `RdtGuardedContainer` from `@ngrdt/core`.
|
|
1398
|
-
* Add to routes via `RdtAngularRoute.addCanDeactivate(preventDataLossGuardFn)`.
|
|
1399
|
-
*/
|
|
1400
|
-
const preventDataLossGuardFn = (component) => {
|
|
1401
|
-
if (!component.guardRegistry.hasLeaveBlockers())
|
|
1402
|
-
return true;
|
|
1403
|
-
return component.guardRegistry.canLeaveAll();
|
|
1404
|
-
};
|
|
1405
|
-
|
|
1406
|
-
/**
|
|
1407
|
-
* Ensures that `preventDataLossGuardFn` is applied to every route at runtime,
|
|
1408
|
-
* even if it was not explicitly added in the route definition.
|
|
1409
|
-
* Call `ensureGlobalGuards()` once during app initialization.
|
|
1410
|
-
*
|
|
1411
|
-
* Also patches the browser back button behavior: replaces the URL back to the
|
|
1412
|
-
* previous value during guard checks so the address bar doesn't flash the new URL
|
|
1413
|
-
* before the guard potentially rejects it.
|
|
1414
|
-
*/
|
|
1415
|
-
class GlobalRouteGuardService {
|
|
1416
|
-
router;
|
|
1417
|
-
constructor(router) {
|
|
1418
|
-
this.router = router;
|
|
1419
|
-
}
|
|
1420
|
-
ensureGlobalGuards() {
|
|
1421
|
-
let lastUrl = '';
|
|
1422
|
-
this.router.events.subscribe((e) => {
|
|
1423
|
-
// Remember last url after successful navigation
|
|
1424
|
-
if (e instanceof NavigationEnd) {
|
|
1425
|
-
lastUrl = location.href;
|
|
1426
|
-
}
|
|
1427
|
-
if (e instanceof GuardsCheckStart) {
|
|
1428
|
-
let lastChild = e.state.root;
|
|
1429
|
-
while (lastChild?.firstChild) {
|
|
1430
|
-
lastChild = lastChild.firstChild;
|
|
1431
|
-
}
|
|
1432
|
-
// Native back button will immediately replace url on click
|
|
1433
|
-
// Replace it back to last url before guard check is finished.
|
|
1434
|
-
// Angular will set it to correct value afterwards.
|
|
1435
|
-
history.replaceState(window.history.state, '', lastUrl);
|
|
1436
|
-
if (lastChild?.routeConfig) {
|
|
1437
|
-
this.ensureCanDeactivateGuards(lastChild.routeConfig);
|
|
1438
|
-
}
|
|
1439
|
-
}
|
|
1440
|
-
});
|
|
1441
|
-
}
|
|
1442
|
-
ensureCanDeactivateGuards(routeConfig) {
|
|
1443
|
-
if (!routeConfig.canDeactivate) {
|
|
1444
|
-
routeConfig.canDeactivate = [];
|
|
1445
|
-
}
|
|
1446
|
-
const canDeactivateGuards = routeConfig.canDeactivate;
|
|
1447
|
-
// ensure that the "MySpecialCanDeactivateGuard" is in effect.
|
|
1448
|
-
if (!canDeactivateGuards.find((o) => o == preventDataLossGuardFn)) {
|
|
1449
|
-
canDeactivateGuards.push(preventDataLossGuardFn);
|
|
1450
|
-
}
|
|
1451
|
-
}
|
|
1452
|
-
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.1.2", ngImport: i0, type: GlobalRouteGuardService, deps: [{ token: i1.Router }], target: i0.ɵɵFactoryTarget.Injectable });
|
|
1453
|
-
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.1.2", ngImport: i0, type: GlobalRouteGuardService, providedIn: 'root' });
|
|
1454
|
-
}
|
|
1455
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.1.2", ngImport: i0, type: GlobalRouteGuardService, decorators: [{
|
|
1456
|
-
type: Injectable,
|
|
1457
|
-
args: [{ providedIn: 'root' }]
|
|
1458
|
-
}], ctorParameters: () => [{ type: i1.Router }] });
|
|
1459
|
-
|
|
1460
|
-
/**
|
|
1461
|
-
* Generated bundle index. Do not edit.
|
|
1462
|
-
*/
|
|
1463
|
-
|
|
1464
|
-
export { GlobalRouteGuardService, PERMISSION_DISABLED_KEY, RDT_CANNOT_BE_ENTERED_PROVIDER, RDT_ROUTES_PROVIDER, RdtAngularRoute, RdtAnyRouteActiveDirective, RdtBackLinkDirective, RdtNavigationSource, RdtParameters, RdtRoute, RdtRouteBuilder, RdtRouterLinkDirective, RdtRouterService, preventDataLossGuardFn, provideRdtRoutes };
|
|
1465
|
-
//# sourceMappingURL=ngrdt-router.mjs.map
|
|
1
|
+
import*as t from"@angular/core";import{InjectionToken as e,inject as r,EnvironmentInjector as a,runInInjectionContext as s,makeEnvironmentProviders as i,Injectable as n,DestroyRef as o,Renderer2 as h,ElementRef as l,Input as u,Directive as c,afterNextRender as p,input as d,linkedSignal as m,booleanAttribute as g,computed as v,effect as f}from"@angular/core";import{Location as y,PlatformLocation as b}from"@angular/common";import*as R from"@angular/router";import{Router as _,NavigationEnd as P,RouterModule as A,RouterLink as C,GuardsCheckStart as w}from"@angular/router";import{RdtViewStateService as D}from"@ngrdt/core";import{RdtStringUtils as E}from"@ngrdt/utils";import{filter as M,take as k,fromEvent as q}from"rxjs";import{takeUntilDestroyed as x}from"@angular/core/rxjs-interop";import{RDT_BUTTON_BASE_PROVIDER as U}from"@ngrdt/button";const B=new e("RDT_CANNOT_BE_ENTERED",{factory:()=>{},providedIn:"root"});class I{params;constructor(t={}){this.params=t}get(t){return this.params[t.absolutePath]??null}set(t,e){return this.params[t.absolutePath]=e,this}append(t,e){const r=this.get(t)??{};return this.params[t.absolutePath]={...r,...e},this}setAll(t){return this.params={...this.params,...t.params},this}*[Symbol.iterator](){const t=Object.keys(this.params);t.sort((t,e)=>t.length-e.length);for(const e of t)yield[e,this.params[e]]}}function L(){return!0}var N;!function(t){t.BREADCRUMB="breadcrumb"}(N||(N={}));class S{route;children=[];loadComponent;loadChildren;component;providers=[];providersForChildren=[];resolvers={};canActivate=[];canActivateChild=[];canDeactivate=[];runGuardsAndResolvers;data;constructor(t){this.route=t}provide(...t){return this.providers.push(...t),this}provideToChildren(...t){return this.providersForChildren.push(...t),this.providers.push(...t),this}withLazyComponent(t){return this.loadComponent=t,this}withModule(t){return this.loadChildren=t,this}setModule(t){return this.loadChildren=t,this}withComponent(t){return this.component=t,this}setComponent(t){return this.withComponent(t)}withChildren(...t){return this.children=t,this}setChildren(...t){return this.withChildren(...t)}addResolve(t,e){return this.resolvers[t]=e,this}addCanActivate(...t){return this.canActivate.push(...t),this}addCanDeactivate(...t){return this.canDeactivate.push(...t),this}addCanActivateChild(...t){return this.canActivateChild.push(...t),this}withRunGuardsAndResolvers(t){return this.runGuardsAndResolvers=t,this}setRunGuardsAndResolvers(t){return this.withRunGuardsAndResolvers(t)}build(){this.checkChildrenMatch();let t={path:this.route.path,providers:this.providers,title:this.route.name,canMatch:this.getCanMatch()};if(this.component?t.component=this.component:this.loadChildren?t.loadChildren=this.loadChildren:this.loadComponent&&(t.loadComponent=this.loadComponent),t.data=this.data??{breadcrumb:{label:this.route.name,disable:this.route.entryDisabled,staticStateParams:{src:N.BREADCRUMB}},...this.route.data},this.canActivate.push((t,e)=>{const i=r(a),n=r(T).parseAbsoluteUrl(e.url),o=n?.params??{params:{},query:{},state:{},route:new I};if(o.query=t.queryParams,s(i,()=>this.route.canBeEnteredFn(n?.route??this.route,o)))return!0;{const t=r(_),a=r(y);let o=r(B);return"function"==typeof o&&(o=s(i,o.bind(o,a.path(),e.url,n?.route??this.route))),"string"==typeof o&&t.parseUrl(o)}}),t.canActivate=this.canActivate,t.canDeactivate=this.canDeactivate,this.providersForChildren.length>0&&this.children.forEach(t=>this.provideToChildrenRec(t)),this.children.length>0)if(this.loadChildren||this.component||this.loadComponent){const e=t.path;t.path="",t.pathMatch="full",t.canMatch=void 0,t={path:e,pathMatch:"prefix",children:[t,...this.children],canActivateChild:this.canActivateChild,canMatch:this.getCanMatch(),data:t.data,providers:t.providers,canActivate:t.canActivate,canDeactivate:t.canDeactivate}}else t.children=this.children;return Object.keys(this.resolvers).length>0&&(t.resolve=this.resolvers),this.runGuardsAndResolvers&&(t.runGuardsAndResolvers=this.runGuardsAndResolvers),t}getCanMatch(){const t=this.route.paramMap;if(!Object.keys(t).some(e=>"number"===t[e]||Array.isArray(t[e])))return;const e=this.route.path.split("/").length,r=this.route.regex?new RegExp(`^${this.route.regex.source}$`):this.route.absoluteRegex;return[(t,a)=>{const s=a.slice(0,e).map(t=>t.path).join("/");return r.test(s)}]}provideToChildrenRec(t){t.providers||(t.providers=[]),this.providersForChildren.forEach(e=>{"object"==typeof e&&"provide"in e&&t.providers.some(t=>"object"==typeof t&&"provide"in t&&t.provide===e.provide)||t.providers.unshift(e)}),t.children&&t.children.forEach(t=>this.provideToChildrenRec(t))}checkChildrenMatch(){if(!this.loadChildren&&(this.route.children.length>0||this.children.length>0)){if(this.children.length<this.route.children.length)throw new Error(`RdtRoute ${this.route.name} has ${this.route.children.length} children, but RdtAngularRoute has ${this.children.length}. These numbers must match. Did you forget to call .setChildren() method in global routes definition? Routes with following names should be present: ${this.route.children.map(t=>t.name).join(", ")}`);if(this.children.length>this.route.children.length)throw new Error(`RdtRoute ${this.route.name} has ${this.route.children.length} children, but RdtAngularRoute has ${this.children.length}. These numbers must match. Did you forget to call .setChildren() method in feature shell routes definition? Routes with following paths should be present: ${this.children.map(t=>t.path).join(", ")}`);const t=this.children.map(t=>t.path),e=this.route.children.map(t=>t.path);t.sort(),e.sort();for(let r=0;r<t.length;r++)if(t[r]!==e[r])throw new Error(`Paths of children provided in RdtRoute and\n RdtAngularRoute do not match. ${t[r]} != ${e[r]}`)}}}class ${_orderedParams=[];_paramMap={};_regex=null;_paramMappings=[];_name;_path;_parent=null;_children=[];_entryDisabled=!1;_canBeEntered=L;_data={};get data(){return this._data}get regex(){return this._regex}get entryDisabled(){return this._entryDisabled}get paramMap(){return this._paramMap}get name(){return this._name}get path(){return this._path}get parent(){return this._parent}get children(){return this._children}}class j extends ${_absoluteRegex;_staticParams={};_queryParams={};_stateParams={};get basePath(){return this._parent?this._parent.absolutePath:"/"}get absolutePath(){return E.joinPaths(this.basePath,this._path)}get absoluteRegex(){if(this._absoluteRegex)return this._absoluteRegex;if(this._regex)if(this._parent){const t=this._parent.absoluteRegex.source.slice(1,-1);this._absoluteRegex=new RegExp("^"+E.joinPaths(t,this._regex.source)+"$")}else this._absoluteRegex=new RegExp("^"+E.joinPaths("/",this._regex.source)+"$");else if(this._absoluteRegex=new RegExp("^/$"),this._parent)throw new Error("Nested route with empty path is not allowed.");return this._absoluteRegex}get stateParams(){return this._stateParams}get queryParams(){return this._queryParams}testUrl(t){return this.absoluteRegex.test(t)}get staticParams(){return{...this._staticParams}}get hasCanBeEnteredGuard(){return this._canBeEntered!==L}canBeEntered(t,e={}){const r=this.getStaticParams();let a=this._queryParams,i=this._stateParams;e.params&&r.set(this,e.params),e.route&&r.setAll(e.route),e.query&&(a={...a,...e.query}),e.state&&(i={...i,...e.state});let n=this;for(;n;){const e={params:r.get(n)??{},route:r,query:a,state:i};if(t){const r=n;if(!s(t,()=>r._canBeEntered(r,e)))return!1}else if(!n._canBeEntered(n,e))return!1;n=n._parent}return!0}get canBeEnteredFn(){return this._canBeEntered}parseAbsoluteUrl(t){const e=E.stripQueryParams(t),r=this.absoluteRegex.exec(e);if(r){const e=E.parseQueryParams(t),a=r.slice(1).reverse(),s=this.fill(a),i=new I(s);return{params:i.get(this)??{},route:i,query:e,state:{}}}return null}fill(t,e={}){const r={},a=this._orderedParams.slice().reverse();for(let e=0;e<a.length&&e<t.length;e++){const s=a[e],i=this._paramMappings.find(t=>t.urlName===s)?.tableName??s;let n=decodeURIComponent(t[e]);"number"===this._paramMap[s]&&(n=parseInt(n)),r[i]=n}return e[this.absolutePath]=r,t.length>a.length&&this._parent&&this._parent.fill(t.slice(a.length),e),e}createAbsoluteUrl(t=this._staticParams){return this._parent?E.joinPaths(this._parent.createAbsoluteUrl(),this.createRelativeUrl(t)):this.createUrl(this.absolutePath,t)}createRelativeUrl(t=this._staticParams){return this.createUrl(this.path,t)}createUrl(t,e){const r=Object.keys(this._paramMap);r.sort((t,e)=>e.length-t.length);const a={...e};return this._paramMappings.forEach(t=>{t.tableName in e&&(a[t.urlName]=e[t.tableName])}),r.forEach(e=>{if(!(e in a))throw new Error(`Param ${e} is missing for route ${this.absolutePath}. Please pass object with required parameters.`);const r=a[e],s=this._paramMap[e];if(Array.isArray(s)&&!s.includes(`${r}`))throw new Error(`Value "${r}" is not allowed for enum param "${e}" in route ${this.absolutePath}. Allowed values: ${s.join(", ")}`);t=t.replace(`:${e}`,encodeURIComponent(`${r}`))}),t}toAngularRoute(){return new S(this)}withParamMappings(t){const e=this.clone();return e._paramMappings=t,e}withStaticParams(t,e){const r=this.clone();if(e){const a=t.absolutePath;let s=r;for(;s;)s.absolutePath===a&&(s._staticParams=e),s=s._parent}else r._staticParams=t;return r}withQueryParams(t){const e=this.clone();return e._queryParams={...t},e}withStateParams(t){const e=this.clone();return e._stateParams={...t},e}extractRouteParams(t){const e={};return this._orderedParams.forEach(r=>{const a=this._paramMappings.find(t=>t.urlName===r),s=a?.tableName??r;s in t&&(e[s]=t[s])}),e}getStaticParams(){const t=new I;let e=this;for(;e;)t.set(e,e._staticParams),e=e._parent;return t}clone(){const t=new j;return t._name=this._name,t._parent=this._parent?.clone()??null,t._path=this.path,t._entryDisabled=this._entryDisabled,t._orderedParams=[...this._orderedParams],t._paramMap={...this._paramMap},t._regex=this._regex?new RegExp(this._regex):null,t._paramMappings=this._paramMappings,t._staticParams={...this._staticParams},t._stateParams={...this._stateParams},t._canBeEntered=this._canBeEntered,t._queryParams={...this._queryParams},t}static getGroup(t){return Array.isArray(t)?"("+t.map(t=>t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")).join("|")+")":j.groups[t]}setRegex(){const t=Object.keys(this._paramMap);t.sort((t,e)=>e.length-t.length);let e=this.path;t.forEach(t=>{const r=this._paramMap[t];if(!r)throw new Error("Params is not set i");e=e.replace(`:${t}`,j.getGroup(r))}),this._regex=""===e?null:new RegExp(e)}static fromBuilder(t){const e=new j;return e._name=t.name,e._path=t.path,e._entryDisabled=t.entryDisabled,e._orderedParams=t.orderedParams,e._canBeEntered=t.canBeEntered,e._paramMap=t.paramMap,e._regex=t.regex?new RegExp(t.regex):null,e._paramMappings=t.paramMappings,e._children=t.children,e._data=t.data,e._children.forEach(t=>t._parent=e),e.setRegex(),e}static groups={string:"([^/]+)",number:"(-?\\d+)"}}const O=new e("RDT_ROUTES_PROVIDER");function F(t){const e=Array.isArray(t)?t:Object.values(t).filter(t=>t instanceof j);return i([{provide:O,useValue:e}])}const V="RdtStateParams";class T{allRoutes=r(O,{optional:!0});baseHref=r(b).getBaseHrefFromDOM();location=r(y);router=r(_);viewStateService=r(D,{optional:!0});_previousUrl=null;_currentUrl=null;get previousUrl(){return this._previousUrl}get currentUrl(){return this._currentUrl}parsePreviousUrl(){return this._previousUrl?this.parseAbsoluteUrl(this._previousUrl):null}parseCurrentUrl(){return this._currentUrl?this.parseAbsoluteUrl(this._currentUrl):null}navigationEnd$=this.router.events.pipe(M(t=>t instanceof P));nextNavigationEnd$=this.navigationEnd$.pipe(k(1));constructor(){null===this.allRoutes&&(console.warn("All routes not provided. Make sure to provide RDT_ROUTES_PROVIDER with RdtRoute[]."),this.allRoutes=[]),this.navigationEnd$.subscribe(t=>{this._previousUrl=this._currentUrl,this._currentUrl=t.url})}getHistoryState(){return"RdtStateParams"in window&&"object"==typeof window[V]?{...window[V],...history.state??{}}:history.state??{}}navigate(t,e,r){let a;if(e instanceof I){let r=t;for(let t=r;null!==t;t=t.parent){const a=e.get(t);a&&(r=r.withStaticParams(t,a))}a=r.createAbsoluteUrl()}else a=e?t.createAbsoluteUrl(e):t.createAbsoluteUrl();const s=r?.target??"_self",i=r?.query??t.queryParams,n=r?.state??t.stateParams;if("_self"===s)return this.router.navigate([a],{queryParams:i,state:n,replaceUrl:r?.replaceUrl});const o=E.createAbsoluteUrl(a,this.baseHref),h=E.appendQueryParams(o,i),l=window.open(h,s);return l&&(l[V]=n),Promise.resolve(!0)}navigateHome(t){return this.router.navigateByUrl(E.appendQueryParams("/",t?.query??{}),{state:t?.state,replaceUrl:t?.replaceUrl})}navigateBack(t){const e=this.parseAbsoluteUrl();if(e){let r=e.route.withStaticParams(e.params);do{r=r.parent}while(r&&r.entryDisabled);return r?(t&&(t.query&&(r=r.withQueryParams(t.query)),t.state&&(r=r.withStateParams(t.state))),this.viewStateService?.markNextAsRestore(),this.navigate(r,t)):this.navigateHome(t)}return console.warn(`Cannot go back from ${this.location.path()} because no route matches current url`),null}parseAbsoluteUrl(t=this.location.path()){const e=E.stripQueryParams(t);if(this.allRoutes)for(const t of this.allRoutes){const r=t.parseAbsoluteUrl(e);if(r)return{route:t,params:r}}return null}extractAllParams(t){if(!t){const e=this.parseAbsoluteUrl();if(!e)return console.warn("No route matches current url."),null;t=e.route}const e=this.location.path();return t.parseAbsoluteUrl(e)}isParentOfCurrentLocation(t){return!0}removeQueryParams(...t){const e=new RegExp(`[?&](${t.join("|")})=[^&]*`,"g");return history.replaceState(history.state,"",location.pathname+location.search.replace(e,"").replace(/^&/,"?"))}static"ɵfac"=t.ɵɵngDeclareFactory({minVersion:"12.0.0",version:"21.1.2",ngImport:t,type:T,deps:[],target:t.ɵɵFactoryTarget.Injectable});static"ɵprov"=t.ɵɵngDeclareInjectable({minVersion:"12.0.0",version:"21.1.2",ngImport:t,type:T,providedIn:"root"})}t.ɵɵngDeclareClassMetadata({minVersion:"12.0.0",version:"21.1.2",ngImport:t,type:T,decorators:[{type:n,args:[{providedIn:"root"}]}],ctorParameters:()=>[]});class G extends ${get canBeEntered(){return this._canBeEntered}get orderedParams(){return this._orderedParams}get paramMappings(){return this._paramMappings}withCanBeEntered(t){return this._canBeEntered=t,this}setCanBeEntered(t){return this.withCanBeEntered(t)}withEntryDisabled(t=!0){return this._entryDisabled=t,this}setEntryDisabled(t=!0){return this.withEntryDisabled(t)}withPath(t){this._path=t;const e=t.split("/"),r=[];return e.forEach(t=>{if(t.includes(":")){const e=t.split(":")[1];e&&(r.push(e),this._paramMap[e]||(this._paramMap[e]="number"))}}),this._orderedParams=r,this}setPath(t){return this.withPath(t)}withData(t){return this._data=t,this}setData(t){return this.withData(t)}withParam(t,e){return this._paramMap[t]=e,this}setParam(t,e){return this.withParam(t,e)}withName(t){return this._name=t,this}setName(t){return this.withName(t)}withChildren(...t){return this._children=t,this}setChildren(...t){return this.withChildren(...t)}build(){if("string"!=typeof this._path)throw new Error("Please provide path for route. Empty string is acceptable.");return j.fromBuilder(this)}}const H={paths:"exact",queryParams:"exact",fragment:"ignored",matrixParams:"ignored"},Q={paths:"subset",queryParams:"subset",fragment:"ignored",matrixParams:"ignored"};class W{destroyRef=r(o);router=r(_);renderer=r(h);elRef=r(l);set anyRouteActive(t){this.classes=this.parseClasses(t)}watchedRoutes=[];set anyRouteActiveOptions(t){this._anyRouteActiveOptions=this.parseOptions(t)}_anyRouteActiveOptions=Q;ariaCurrentWhenActive;get isActive(){return this._isActive}_isActive=!1;classes=[];ngOnInit(){this.update(),this.listenRouterEvents()}ngOnChanges(t){t.anyRouteActive?.previousValue&&this.parseClasses(t.anyRouteActive.previousValue).forEach(t=>this.removeClass(t)),this.update()}update(){this._isActive=this.watchedRoutes.some(t=>{const e="string"==typeof t?t:this.router.createUrlTree(t);return this.router.isActive(e,this._anyRouteActiveOptions)}),this.setClasses(),this.setAriaCurrent()}setAriaCurrent(){const t=this.elRef.nativeElement;if(this._isActive&&void 0!==this.ariaCurrentWhenActive){const e=this.ariaCurrentWhenActive.toString();this.renderer.setAttribute(t,"aria-current",e)}else this.renderer.removeAttribute(t,"aria-current")}setClasses(){this._isActive?this.classes.forEach(t=>this.addClass(t)):this.classes.forEach(t=>this.removeClass(t))}addClass(t){this.renderer.addClass(this.elRef.nativeElement,t)}removeClass(t){this.renderer.removeClass(this.elRef.nativeElement,t)}listenRouterEvents(){this.router.events.pipe(M(t=>t instanceof P),x(this.destroyRef)).subscribe(()=>this.update())}parseClasses(t){return(Array.isArray(t)?t:t.split(" ")).filter(t=>!!t)}parseOptions(t){return"exact"in t?t.exact?H:Q:t}static"ɵfac"=t.ɵɵngDeclareFactory({minVersion:"12.0.0",version:"21.1.2",ngImport:t,type:W,deps:[],target:t.ɵɵFactoryTarget.Directive});static"ɵdir"=t.ɵɵngDeclareDirective({minVersion:"14.0.0",version:"21.1.2",type:W,isStandalone:!0,selector:"[rdtAnyRouteActive]",inputs:{anyRouteActive:["rdtAnyRouteActive","anyRouteActive"],watchedRoutes:"watchedRoutes",anyRouteActiveOptions:"anyRouteActiveOptions",ariaCurrentWhenActive:"ariaCurrentWhenActive"},providers:[A],usesOnChanges:!0,ngImport:t})}t.ɵɵngDeclareClassMetadata({minVersion:"12.0.0",version:"21.1.2",ngImport:t,type:W,decorators:[{type:c,args:[{selector:"[rdtAnyRouteActive]",standalone:!0,providers:[A]}]}],propDecorators:{anyRouteActive:[{type:u,args:[{required:!0,alias:"rdtAnyRouteActive"}]}],watchedRoutes:[{type:u,args:[{required:!0}]}],anyRouteActiveOptions:[{type:u}],ariaCurrentWhenActive:[{type:u}]}});class z{destroyRef=r(o);location=r(y);elRef=r(l);buttonClass=r(U);buttonRef=r(this.buttonClass,{optional:!0,host:!0});constructor(){p(()=>{this.buttonRef?this.buttonRef.click.subscribe(()=>this.location.back()):q(this.elRef.nativeElement,"click").pipe(x(this.destroyRef)).subscribe(()=>this.location.back())})}static"ɵfac"=t.ɵɵngDeclareFactory({minVersion:"12.0.0",version:"21.1.2",ngImport:t,type:z,deps:[],target:t.ɵɵFactoryTarget.Directive});static"ɵdir"=t.ɵɵngDeclareDirective({minVersion:"14.0.0",version:"21.1.2",type:z,isStandalone:!0,selector:"[rdtBackLink]",ngImport:t})}t.ɵɵngDeclareClassMetadata({minVersion:"12.0.0",version:"21.1.2",ngImport:t,type:z,decorators:[{type:c,args:[{selector:"[rdtBackLink]"}]}],ctorParameters:()=>[]});const J=Symbol();class K{buttonClass=r(U);buttonRef=r(this.buttonClass,{host:!0,optional:!0});routerLinkRef=r(C,{self:!0});envInjector=r(a);routeInput=d.required({...ngDevMode?{debugName:"routeInput"}:{},alias:"rdtRouterLink"});route=m(()=>this.routeInput(),...ngDevMode?[{debugName:"route"}]:[]);target=d("_self",...ngDevMode?[{debugName:"target"}]:[]);params=d(...ngDevMode?[void 0,{debugName:"params"}]:[]);queryParams=d(...ngDevMode?[void 0,{debugName:"queryParams"}]:[]);stateParams=d(...ngDevMode?[void 0,{debugName:"stateParams"}]:[]);disabled=d(!1,{...ngDevMode?{debugName:"disabled"}:{},alias:"rdtDisabled",transform:g});canBeEntered=v(()=>{const t=this.route(),e=this.disabled();if(!t||e)return!1;const r={params:this.params(),query:this.queryParams(),state:this.stateParams()};return t.canBeEntered(this.envInjector,r)},...ngDevMode?[{debugName:"canBeEntered"}]:[]);updateEffect=f(()=>{this.buttonRef?this.updateButton():this.updateRouterLink()},...ngDevMode?[{debugName:"updateEffect"}]:[]);updateButton(){const t=this.route();t?this.canBeEntered()?this.setButtonLink(t):this.disableButtonLink():this.clearButtonLink()}setButtonLink(t){const e=this.buttonRef;try{const r=this.getNgHref(t);e.ngHref.set(r),e.stateParams.set(this.stateParams()??t.stateParams),e.queryParams.set(this.queryParams()??t.queryParams),e.setDisabledState(!1,J)}catch(t){console.error(t),this.disableButtonLink()}}disableButtonLink(){const t=this.buttonRef;t.ngHref.set(null),t.setDisabledState(!0,J)}clearButtonLink(){const t=this.buttonRef;t.ngHref.set(null),t.stateParams.set(void 0),t.queryParams.set(void 0),t.setDisabledState(!1,J)}updateRouterLink(){const t=this.route();t?this.canBeEntered()?this.setRouterLink(t):this.disableRouterLink():this.clearRouterLink()}setRouterLink(t){try{const e=this.getNgHref(t);this.routerLinkRef.routerLink=e,this.routerLinkRef.state=this.stateParams()??t.stateParams,this.routerLinkRef.queryParams=this.queryParams()??t.queryParams,this.routerLinkRef.ngOnChanges({})}catch(t){console.error(t),this.disableRouterLink()}}disableRouterLink(){this.routerLinkRef.routerLink=null,this.routerLinkRef.ngOnChanges({})}clearRouterLink(){this.routerLinkRef.routerLink=null,this.routerLinkRef.state=void 0,this.routerLinkRef.queryParams=void 0,this.routerLinkRef.ngOnChanges({})}getNgHref(t){return t.createAbsoluteUrl(this.params())}static"ɵfac"=t.ɵɵngDeclareFactory({minVersion:"12.0.0",version:"21.1.2",ngImport:t,type:K,deps:[],target:t.ɵɵFactoryTarget.Directive});static"ɵdir"=t.ɵɵngDeclareDirective({minVersion:"17.1.0",version:"21.1.2",type:K,isStandalone:!0,selector:"[rdtRouterLink]",inputs:{routeInput:{classPropertyName:"routeInput",publicName:"rdtRouterLink",isSignal:!0,isRequired:!0,transformFunction:null},target:{classPropertyName:"target",publicName:"target",isSignal:!0,isRequired:!1,transformFunction:null},params:{classPropertyName:"params",publicName:"params",isSignal:!0,isRequired:!1,transformFunction:null},queryParams:{classPropertyName:"queryParams",publicName:"queryParams",isSignal:!0,isRequired:!1,transformFunction:null},stateParams:{classPropertyName:"stateParams",publicName:"stateParams",isSignal:!0,isRequired:!1,transformFunction:null},disabled:{classPropertyName:"disabled",publicName:"rdtDisabled",isSignal:!0,isRequired:!1,transformFunction:null}},hostDirectives:[{directive:R.RouterLink,inputs:["target","target","replaceUrl","replaceUrl"]}],ngImport:t})}t.ɵɵngDeclareClassMetadata({minVersion:"12.0.0",version:"21.1.2",ngImport:t,type:K,decorators:[{type:c,args:[{selector:"[rdtRouterLink]",standalone:!0,hostDirectives:[{directive:C,inputs:["target","replaceUrl"]}]}]}],propDecorators:{routeInput:[{type:t.Input,args:[{isSignal:!0,alias:"rdtRouterLink",required:!0}]}],target:[{type:t.Input,args:[{isSignal:!0,alias:"target",required:!1}]}],params:[{type:t.Input,args:[{isSignal:!0,alias:"params",required:!1}]}],queryParams:[{type:t.Input,args:[{isSignal:!0,alias:"queryParams",required:!1}]}],stateParams:[{type:t.Input,args:[{isSignal:!0,alias:"stateParams",required:!1}]}],disabled:[{type:t.Input,args:[{isSignal:!0,alias:"rdtDisabled",required:!1}]}]}});const X=t=>!t.guardRegistry.hasLeaveBlockers()||t.guardRegistry.canLeaveAll();class Y{router=r(_);ensureGlobalGuards(){let t="";this.router.events.subscribe(e=>{if(e instanceof P&&(t=location.href),e instanceof w){let r=e.state.root;for(;r?.firstChild;)r=r.firstChild;history.replaceState(window.history.state,"",t),r?.routeConfig&&this.ensureCanDeactivateGuards(r.routeConfig)}})}ensureCanDeactivateGuards(t){t.canDeactivate||(t.canDeactivate=[]);const e=t.canDeactivate;e.find(t=>t==X)||e.push(X)}static"ɵfac"=t.ɵɵngDeclareFactory({minVersion:"12.0.0",version:"21.1.2",ngImport:t,type:Y,deps:[],target:t.ɵɵFactoryTarget.Injectable});static"ɵprov"=t.ɵɵngDeclareInjectable({minVersion:"12.0.0",version:"21.1.2",ngImport:t,type:Y,providedIn:"root"})}t.ɵɵngDeclareClassMetadata({minVersion:"12.0.0",version:"21.1.2",ngImport:t,type:Y,decorators:[{type:n,args:[{providedIn:"root"}]}]});export{Y as GlobalRouteGuardService,J as PERMISSION_DISABLED_KEY,B as RDT_CANNOT_BE_ENTERED_PROVIDER,O as RDT_ROUTES_PROVIDER,S as RdtAngularRoute,W as RdtAnyRouteActiveDirective,z as RdtBackLinkDirective,N as RdtNavigationSource,I as RdtParameters,j as RdtRoute,G as RdtRouteBuilder,K as RdtRouterLinkDirective,T as RdtRouterService,X as preventDataLossGuardFn,F as provideRdtRoutes};
|