@ngrdt/router 0.0.95 → 0.0.96
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 +87 -22
- package/fesm2022/ngrdt-router.mjs.map +1 -1
- package/index.d.ts +25 -11
- package/package.json +4 -4
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as i0 from '@angular/core';
|
|
2
|
-
import { InjectionToken, inject, Injectable, EnvironmentInjector, runInInjectionContext, DestroyRef, Renderer2, ElementRef, Input, Directive, afterNextRender, input, linkedSignal, booleanAttribute, effect } from '@angular/core';
|
|
2
|
+
import { InjectionToken, inject, Injectable, EnvironmentInjector, runInInjectionContext, DestroyRef, Renderer2, ElementRef, Input, Directive, afterNextRender, input, linkedSignal, booleanAttribute, computed, effect } from '@angular/core';
|
|
3
3
|
import { defer, of, filter, take, fromEvent } from 'rxjs';
|
|
4
4
|
import { PlatformLocation, Location } from '@angular/common';
|
|
5
5
|
import * as i1 from '@angular/router';
|
|
@@ -36,6 +36,15 @@ class RdtParameters {
|
|
|
36
36
|
this.params[route.absolutePath] = params;
|
|
37
37
|
return this;
|
|
38
38
|
}
|
|
39
|
+
append(route, params) {
|
|
40
|
+
const existing = this.get(route) ?? {};
|
|
41
|
+
this.params[route.absolutePath] = { ...existing, ...params };
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
setAll(params) {
|
|
45
|
+
this.params = { ...this.params, ...params.params };
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
39
48
|
*[Symbol.iterator]() {
|
|
40
49
|
const keys = Object.keys(this.params);
|
|
41
50
|
keys.sort((a, b) => a.length - b.length);
|
|
@@ -190,13 +199,12 @@ class RdtRouterService {
|
|
|
190
199
|
const parsed = this.parseAbsoluteUrl();
|
|
191
200
|
if (!parsed) {
|
|
192
201
|
console.warn('No route matches current url.');
|
|
193
|
-
return
|
|
202
|
+
return null;
|
|
194
203
|
}
|
|
195
204
|
currentRoute = parsed.route;
|
|
196
205
|
}
|
|
197
206
|
const url = this.location.path();
|
|
198
|
-
|
|
199
|
-
return currentRoute.parseAbsoluteUrl(stripped);
|
|
207
|
+
return currentRoute.parseAbsoluteUrl(url);
|
|
200
208
|
}
|
|
201
209
|
isParentOfCurrentLocation(route) {
|
|
202
210
|
return true;
|
|
@@ -337,7 +345,16 @@ class RdtAngularRoute {
|
|
|
337
345
|
};
|
|
338
346
|
const guard = (route, state) => {
|
|
339
347
|
const injector = inject(EnvironmentInjector);
|
|
340
|
-
|
|
348
|
+
const rdtRouter = inject(RdtRouterService);
|
|
349
|
+
const parsed = rdtRouter.parseAbsoluteUrl(state.url);
|
|
350
|
+
const combinedParams = parsed?.params ?? {
|
|
351
|
+
params: {},
|
|
352
|
+
query: {},
|
|
353
|
+
state: {},
|
|
354
|
+
route: new RdtParameters(),
|
|
355
|
+
};
|
|
356
|
+
combinedParams.query = route.queryParams;
|
|
357
|
+
if (runInInjectionContext(injector, () => this.route.canBeEnteredFn(parsed?.route ?? this.route, combinedParams))) {
|
|
341
358
|
return true;
|
|
342
359
|
}
|
|
343
360
|
else {
|
|
@@ -345,8 +362,7 @@ class RdtAngularRoute {
|
|
|
345
362
|
const location = inject(Location);
|
|
346
363
|
let url = inject(RDT_CANNOT_BE_ENTERED_PROVIDER);
|
|
347
364
|
if (typeof url === 'function') {
|
|
348
|
-
|
|
349
|
-
url = runInInjectionContext(injector, url.bind(url, location.path(), state.url, this.route));
|
|
365
|
+
url = runInInjectionContext(injector, url.bind(url, location.path(), state.url, parsed?.route ?? this.route));
|
|
350
366
|
}
|
|
351
367
|
if (typeof url === 'string') {
|
|
352
368
|
return router.parseUrl(url);
|
|
@@ -622,31 +638,57 @@ class RdtRoute extends RdtRouteBase {
|
|
|
622
638
|
* Meta guard that is used by [rdtRouterLink] and RdtMenu
|
|
623
639
|
* to determine if route can be entered.
|
|
624
640
|
* @param injector Environment or custom injector.
|
|
641
|
+
* @param params Optional parameters to pass to guard function.
|
|
625
642
|
* @returns True if route can be entered.
|
|
626
643
|
*/
|
|
627
|
-
canBeEntered(injector) {
|
|
644
|
+
canBeEntered(injector, combinedParams = {}) {
|
|
645
|
+
const route = this.getStaticParams();
|
|
646
|
+
let query = this._queryParams;
|
|
647
|
+
let state = this._stateParams;
|
|
648
|
+
if (combinedParams.params) {
|
|
649
|
+
route.set(this, combinedParams.params);
|
|
650
|
+
}
|
|
651
|
+
if (combinedParams.route) {
|
|
652
|
+
route.setAll(combinedParams.route);
|
|
653
|
+
}
|
|
654
|
+
if (combinedParams.query) {
|
|
655
|
+
query = { ...query, ...combinedParams.query };
|
|
656
|
+
}
|
|
657
|
+
if (combinedParams.state) {
|
|
658
|
+
state = { ...state, ...combinedParams.state };
|
|
659
|
+
}
|
|
660
|
+
const combined = {
|
|
661
|
+
params: route.get(this) ?? {},
|
|
662
|
+
route: route,
|
|
663
|
+
query: query,
|
|
664
|
+
state: state,
|
|
665
|
+
};
|
|
628
666
|
return injector
|
|
629
|
-
? runInInjectionContext(injector, this._canBeEntered)
|
|
630
|
-
: this._canBeEntered();
|
|
667
|
+
? runInInjectionContext(injector, () => this._canBeEntered(this, combined))
|
|
668
|
+
: this._canBeEntered(this, combined);
|
|
669
|
+
}
|
|
670
|
+
get canBeEnteredFn() {
|
|
671
|
+
return this._canBeEntered;
|
|
631
672
|
}
|
|
632
673
|
/**
|
|
633
674
|
* Extracts url parameters from absolute path for this path and each parent.
|
|
634
675
|
*/
|
|
635
676
|
parseAbsoluteUrl(url) {
|
|
677
|
+
const path = RdtStringUtils.stripQueryParams(url);
|
|
636
678
|
const reg = this.absoluteRegex;
|
|
637
|
-
|
|
638
|
-
return null;
|
|
639
|
-
}
|
|
640
|
-
const matches = reg.exec(url);
|
|
679
|
+
const matches = reg.exec(path);
|
|
641
680
|
if (matches) {
|
|
681
|
+
const query = RdtStringUtils.parseQueryParams(url);
|
|
642
682
|
const values = matches.slice(1).reverse();
|
|
643
683
|
const obj = this.fill(values);
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
684
|
+
const route = new RdtParameters(obj);
|
|
685
|
+
const params = route.get(this) ?? {};
|
|
686
|
+
return {
|
|
687
|
+
params: params,
|
|
688
|
+
route: route,
|
|
689
|
+
query: query,
|
|
690
|
+
state: {},
|
|
691
|
+
};
|
|
650
692
|
}
|
|
651
693
|
else {
|
|
652
694
|
return null;
|
|
@@ -766,6 +808,16 @@ class RdtRoute extends RdtRouteBase {
|
|
|
766
808
|
});
|
|
767
809
|
return res;
|
|
768
810
|
}
|
|
811
|
+
getStaticParams() {
|
|
812
|
+
const res = new RdtParameters();
|
|
813
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
814
|
+
let current = this;
|
|
815
|
+
while (current) {
|
|
816
|
+
res.set(current, current._staticParams);
|
|
817
|
+
current = current._parent;
|
|
818
|
+
}
|
|
819
|
+
return res;
|
|
820
|
+
}
|
|
769
821
|
/**
|
|
770
822
|
* @returns New instance of RdtRoute<T> that is clone of
|
|
771
823
|
* this route and its parents (NOT children).
|
|
@@ -1130,6 +1182,19 @@ class RdtRouterLinkDirective {
|
|
|
1130
1182
|
alias: 'rdtDisabled',
|
|
1131
1183
|
transform: booleanAttribute,
|
|
1132
1184
|
});
|
|
1185
|
+
canBeEntered = computed(() => {
|
|
1186
|
+
const route = this.route();
|
|
1187
|
+
const disabled = this.disabled();
|
|
1188
|
+
if (!route || disabled) {
|
|
1189
|
+
return false;
|
|
1190
|
+
}
|
|
1191
|
+
const combinedParams = {
|
|
1192
|
+
params: this.params(),
|
|
1193
|
+
query: this.queryParams(),
|
|
1194
|
+
state: this.stateParams(),
|
|
1195
|
+
};
|
|
1196
|
+
return route.canBeEntered(this.envInjector, combinedParams);
|
|
1197
|
+
});
|
|
1133
1198
|
updateEffect = effect(() => {
|
|
1134
1199
|
if (this.buttonRef) {
|
|
1135
1200
|
this.updateButton();
|
|
@@ -1144,7 +1209,7 @@ class RdtRouterLinkDirective {
|
|
|
1144
1209
|
this.clearButtonLink();
|
|
1145
1210
|
return;
|
|
1146
1211
|
}
|
|
1147
|
-
if (
|
|
1212
|
+
if (this.canBeEntered()) {
|
|
1148
1213
|
this.setButtonLink(route);
|
|
1149
1214
|
}
|
|
1150
1215
|
else {
|
|
@@ -1182,7 +1247,7 @@ class RdtRouterLinkDirective {
|
|
|
1182
1247
|
if (!route) {
|
|
1183
1248
|
this.clearRouterLink();
|
|
1184
1249
|
}
|
|
1185
|
-
else if (
|
|
1250
|
+
else if (this.canBeEntered()) {
|
|
1186
1251
|
this.setRouterLink(route);
|
|
1187
1252
|
}
|
|
1188
1253
|
else {
|