@lwrjs/router 0.12.0-alpha.1 → 0.12.0-alpha.10
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/README.md +777 -0
- package/build/bundle/prod/lwr/navigation/navigation.js +1 -1
- package/build/bundle/prod/lwr/router/router.js +1 -1
- package/build/bundle/prod/lwr/routerContainer/routerContainer.js +1 -1
- package/build/cjs/modules/lwr/domRouter/domRouter.cjs +20 -10
- package/build/cjs/modules/lwr/historyRouter/historyRouter.cjs +3 -3
- package/build/cjs/modules/lwr/navigation/navigationApi.cjs +4 -4
- package/build/cjs/modules/lwr/navigation/navigationMixin.cjs +4 -4
- package/build/cjs/modules/lwr/router/router.cjs +14 -13
- package/build/cjs/modules/lwr/routerUtils/routeUtils.cjs +21 -8
- package/build/cjs/modules/lwr/routerUtils/routerUtils.cjs +1 -0
- package/build/cjs/modules/lwr/serverRouter/serverRouter.cjs +10 -10
- package/build/cjs/services/index.cjs +0 -1
- package/build/cjs/services/module-provider/index.cjs +13 -36
- package/build/cjs/services/module-provider/utils.cjs +18 -8
- package/build/es/modules/lwr/contextUtils/navigationApiStore.d.ts +3 -2
- package/build/es/modules/lwr/domRouter/domRouter.d.ts +14 -8
- package/build/es/modules/lwr/domRouter/domRouter.js +25 -10
- package/build/es/modules/lwr/historyRouter/historyRouter.d.ts +3 -2
- package/build/es/modules/lwr/historyRouter/historyRouter.js +4 -4
- package/build/es/modules/lwr/navigation/navigationApi.d.ts +9 -8
- package/build/es/modules/lwr/navigation/navigationApi.js +10 -10
- package/build/es/modules/lwr/navigation/navigationMixin.js +4 -4
- package/build/es/modules/lwr/router/router.d.ts +1 -1
- package/build/es/modules/lwr/router/router.js +15 -14
- package/build/es/modules/lwr/routerUtils/routeUtils.d.ts +7 -5
- package/build/es/modules/lwr/routerUtils/routeUtils.js +22 -8
- package/build/es/modules/lwr/routerUtils/routerUtils.d.ts +1 -1
- package/build/es/modules/lwr/routerUtils/routerUtils.js +1 -1
- package/build/es/modules/lwr/routerUtils/types.d.ts +17 -13
- package/build/es/modules/lwr/serverRouter/serverRouter.d.ts +4 -3
- package/build/es/modules/lwr/serverRouter/serverRouter.js +11 -11
- package/build/es/services/index.d.ts +3 -1
- package/build/es/services/index.js +0 -1
- package/build/es/services/module-provider/index.d.ts +6 -7
- package/build/es/services/module-provider/index.js +15 -47
- package/build/es/services/module-provider/utils.d.ts +2 -2
- package/build/es/services/module-provider/utils.js +24 -8
- package/build/modules/lwr/contextUtils/contextUtils.js +1 -0
- package/build/modules/lwr/domRouter/domRouter.js +27 -10
- package/build/modules/lwr/domRouterUtils/historyUtils.js +1 -0
- package/build/modules/lwr/historyRouter/historyRouter.js +4 -4
- package/build/modules/lwr/navigation/navigationApi.js +10 -10
- package/build/modules/lwr/navigation/navigationMixin.js +4 -4
- package/build/modules/lwr/router/router.js +15 -14
- package/build/modules/lwr/routerBridge/routerBridge.js +0 -1
- package/build/modules/lwr/routerContainer/utils.js +3 -1
- package/build/modules/lwr/routerUtils/routeDefUtils.js +0 -1
- package/build/modules/lwr/routerUtils/routeUtils.js +23 -8
- package/build/modules/lwr/routerUtils/routerUtils.js +1 -1
- package/build/modules/lwr/routerUtils/typeUtils.js +1 -0
- package/build/modules/lwr/serverRouter/serverRouter.js +11 -11
- package/package.json +9 -9
|
@@ -33,46 +33,31 @@ var DEFAULT_DIR = "$rootDir/src/routes";
|
|
|
33
33
|
var RouterModuleProvider = class {
|
|
34
34
|
constructor({routesDir = DEFAULT_DIR}, context) {
|
|
35
35
|
this.name = "router-module-provider";
|
|
36
|
-
this.
|
|
37
|
-
this.routerModuleCache = new Map();
|
|
36
|
+
this.watchedFileSet = new Set();
|
|
38
37
|
const {
|
|
39
|
-
|
|
40
|
-
config: {basePath, rootDir, contentDir, layoutsDir},
|
|
38
|
+
config: {rootDir, contentDir, i18n, layoutsDir},
|
|
41
39
|
runtimeEnvironment: {lwrVersion, watchFiles},
|
|
42
40
|
watcherFactory
|
|
43
41
|
} = context;
|
|
44
42
|
this.version = lwrVersion;
|
|
43
|
+
this.i18n = i18n;
|
|
45
44
|
this.routesDir = (0, import_shared_utils.normalizeResourcePath)(routesDir, {
|
|
46
45
|
rootDir,
|
|
47
46
|
assets: [],
|
|
48
47
|
contentDir,
|
|
49
48
|
layoutsDir
|
|
50
49
|
}).replace(/\/$/, "");
|
|
51
|
-
this.appBasePath = basePath ? basePath : void 0;
|
|
52
|
-
this.routerEmitter = appEmitter;
|
|
53
50
|
this.routerWatcher = watchFiles && watcherFactory ? (0, import_utils.setUpWatcher)(watcherFactory, this.onRouterModuleChange.bind(this)) : void 0;
|
|
54
51
|
}
|
|
55
|
-
async onRouterModuleChange(configPath
|
|
56
|
-
const moduleId = this.watchedFileMap.get(configPath);
|
|
57
|
-
if (!moduleId) {
|
|
58
|
-
throw new Error("We are observing an unprocessed Router Config file, this should not happen...");
|
|
59
|
-
}
|
|
52
|
+
async onRouterModuleChange(configPath) {
|
|
60
53
|
(0, import__.deleteRouterConfigJsonCacheEntry)(configPath);
|
|
61
|
-
this.routerModuleCache.delete(configPath);
|
|
62
|
-
if (!deleted) {
|
|
63
|
-
const recompiledModule = await this.getModule(moduleId);
|
|
64
|
-
if (recompiledModule) {
|
|
65
|
-
this.routerEmitter.notifyModuleSourceChanged(recompiledModule);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
54
|
}
|
|
69
|
-
watchConfigs(
|
|
55
|
+
watchConfigs(configPath) {
|
|
70
56
|
if (this.routerWatcher) {
|
|
71
|
-
|
|
72
|
-
if (!this.watchedFileMap.has(configPath)) {
|
|
57
|
+
if (!this.watchedFileSet.has(configPath)) {
|
|
73
58
|
this.routerWatcher.add(configPath);
|
|
59
|
+
this.watchedFileSet.add(configPath);
|
|
74
60
|
}
|
|
75
|
-
this.watchedFileMap.set(configPath, moduleId);
|
|
76
61
|
}
|
|
77
62
|
}
|
|
78
63
|
getRouterConfig(specifier) {
|
|
@@ -81,17 +66,15 @@ var RouterModuleProvider = class {
|
|
|
81
66
|
if (routerId) {
|
|
82
67
|
const configPath = (0, import_utils.getRouterConfigPath)(this.routesDir, specifier);
|
|
83
68
|
config = (0, import__.getClientRoutes)(configPath);
|
|
84
|
-
|
|
85
|
-
config.basePath = this.appBasePath;
|
|
86
|
-
}
|
|
69
|
+
this.watchConfigs(configPath);
|
|
87
70
|
}
|
|
88
71
|
return config;
|
|
89
72
|
}
|
|
90
|
-
async getModuleEntry({specifier}) {
|
|
73
|
+
async getModuleEntry({specifier}, runtimeParams) {
|
|
91
74
|
const config = this.getRouterConfig(specifier);
|
|
92
75
|
if (config) {
|
|
93
76
|
return {
|
|
94
|
-
id: `${specifier}|${this.version}`,
|
|
77
|
+
id: `${specifier}|${this.version}|${runtimeParams.basePath}|${runtimeParams.locale}`,
|
|
95
78
|
virtual: true,
|
|
96
79
|
entry: `<virtual>/${specifier}.js`,
|
|
97
80
|
specifier,
|
|
@@ -99,18 +82,14 @@ var RouterModuleProvider = class {
|
|
|
99
82
|
};
|
|
100
83
|
}
|
|
101
84
|
}
|
|
102
|
-
async getModule(moduleId) {
|
|
85
|
+
async getModule(moduleId, runtimeParams) {
|
|
103
86
|
const {specifier, namespace, name = specifier} = moduleId;
|
|
104
|
-
const moduleEntry = await this.getModuleEntry({specifier});
|
|
87
|
+
const moduleEntry = await this.getModuleEntry({specifier}, runtimeParams);
|
|
105
88
|
if (!moduleEntry) {
|
|
106
89
|
return;
|
|
107
90
|
}
|
|
108
|
-
const configPath = (0, import_utils.getRouterConfigPath)(this.routesDir, specifier);
|
|
109
|
-
if (this.routerModuleCache.has(configPath)) {
|
|
110
|
-
return this.routerModuleCache.get(configPath);
|
|
111
|
-
}
|
|
112
91
|
const config = this.getRouterConfig(specifier);
|
|
113
|
-
const compiledSource = (0, import_utils.generateModule)(config);
|
|
92
|
+
const compiledSource = (0, import_utils.generateModule)(config, this.i18n, runtimeParams);
|
|
114
93
|
const moduleCompiled = {
|
|
115
94
|
id: moduleEntry.id,
|
|
116
95
|
specifier,
|
|
@@ -122,8 +101,6 @@ var RouterModuleProvider = class {
|
|
|
122
101
|
ownHash: (0, import_shared_utils.hashContent)(compiledSource),
|
|
123
102
|
compiledSource
|
|
124
103
|
};
|
|
125
|
-
this.watchConfigs(specifier, moduleId);
|
|
126
|
-
this.routerModuleCache.set(configPath, moduleCompiled);
|
|
127
104
|
return moduleCompiled;
|
|
128
105
|
}
|
|
129
106
|
};
|
|
@@ -52,10 +52,19 @@ function getHandlerClassName(specifier) {
|
|
|
52
52
|
}
|
|
53
53
|
function generateHandlerClasses(routes) {
|
|
54
54
|
let handlerClasses = "";
|
|
55
|
-
|
|
55
|
+
const seenComponents = new Set();
|
|
56
|
+
const filteredAndDeDupedArray = routes.filter((item) => {
|
|
57
|
+
if (!item.component)
|
|
58
|
+
return false;
|
|
59
|
+
if (!seenComponents.has(item.component)) {
|
|
60
|
+
seenComponents.add(item.component);
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
return false;
|
|
64
|
+
});
|
|
65
|
+
filteredAndDeDupedArray.forEach((r) => {
|
|
56
66
|
const component = r.component;
|
|
57
|
-
|
|
58
|
-
handlerClasses += `class ${getHandlerClassName(component)} {
|
|
67
|
+
handlerClasses += `class ${getHandlerClassName(component)} {
|
|
59
68
|
callback;
|
|
60
69
|
constructor(callback) {
|
|
61
70
|
this.callback = callback;
|
|
@@ -73,7 +82,6 @@ function generateHandlerClasses(routes) {
|
|
|
73
82
|
}
|
|
74
83
|
}
|
|
75
84
|
`;
|
|
76
|
-
}
|
|
77
85
|
});
|
|
78
86
|
return handlerClasses;
|
|
79
87
|
}
|
|
@@ -92,14 +100,16 @@ function generateRouteDefinitions(routes) {
|
|
|
92
100
|
});
|
|
93
101
|
return `${routeDefs} ]`;
|
|
94
102
|
}
|
|
95
|
-
function generateModule(config = {routes: []}) {
|
|
96
|
-
const {
|
|
103
|
+
function generateModule(config = {routes: []}, {defaultLocale, uriPattern}, runtimeParams) {
|
|
104
|
+
const {caseSensitive, routes: jsonRoutes} = config;
|
|
97
105
|
const csString = caseSensitive ? "true" : "false";
|
|
98
106
|
const routes = generateRouteDefinitions(jsonRoutes);
|
|
99
107
|
const handlers = generateHandlerClasses(jsonRoutes);
|
|
108
|
+
const basePath = runtimeParams?.basePath || "";
|
|
109
|
+
const i18nRouterConfig = uriPattern === "path-prefix" ? `${JSON.stringify({locale: runtimeParams?.locale, defaultLocale})}` : void 0;
|
|
100
110
|
return `import { createRouter as createLwrRouter } from 'lwr/router';
|
|
101
111
|
${handlers}
|
|
102
|
-
export function createRouter({ basePath = '${basePath}', caseSensitive = ${csString} } = {}) {
|
|
103
|
-
return createLwrRouter({ basePath, caseSensitive, routes: ${routes} });
|
|
112
|
+
export function createRouter({ basePath = '${basePath}', caseSensitive = ${csString}, DEPRECATED_getRouteFromUrl, DEPRECATED_getUrlFromRoute } = {}) {
|
|
113
|
+
return createLwrRouter({ basePath, caseSensitive, i18n: ${i18nRouterConfig}, routes: ${routes}, DEPRECATED_getRouteFromUrl, DEPRECATED_getUrlFromRoute });
|
|
104
114
|
}`;
|
|
105
115
|
}
|
|
@@ -7,14 +7,15 @@
|
|
|
7
7
|
import type { RouteCallback, PageReference } from 'lwr/router';
|
|
8
8
|
import type { Unsubscriber } from 'lwr/observable';
|
|
9
9
|
import type { ContextId } from 'lwr/navigationContext';
|
|
10
|
+
import type { NavigateOptions } from '../routerUtils/types';
|
|
10
11
|
/**
|
|
11
12
|
* Provides the ability for a given context provider to find its NavigationHelm
|
|
12
13
|
* provider by just an ID. Having access to the NavigationHelm allows the consumer
|
|
13
14
|
* to direct navigation. Generally, the Helm is more powerful than the exposed APIs.
|
|
14
15
|
*/
|
|
15
16
|
export interface NavigationHelm {
|
|
16
|
-
navigate: (address: PageReference, replace?: boolean) => void;
|
|
17
|
-
generateUrl: (address: PageReference) => string | null;
|
|
17
|
+
navigate: (address: PageReference, replace?: boolean, options?: NavigateOptions) => void;
|
|
18
|
+
generateUrl: (address: PageReference, options?: NavigateOptions) => string | null;
|
|
18
19
|
subscribe: (callback: RouteCallback, replay?: boolean) => Unsubscriber;
|
|
19
20
|
}
|
|
20
21
|
interface NavigationContextData {
|
|
@@ -9,6 +9,7 @@ import type { ContextId } from 'lwr/navigationContext';
|
|
|
9
9
|
import type { Filter, MessageObject } from 'lwr/routerUtils';
|
|
10
10
|
import type { PageReference, Router, RouteCallback, RoutingMatch, RoutingResult } from 'lwr/router';
|
|
11
11
|
import type { Observable, Unsubscriber } from 'lwr/observable';
|
|
12
|
+
import type { NavigateOptions } from '../routerUtils/types';
|
|
12
13
|
export declare const NAV_EVENT: string;
|
|
13
14
|
export declare const PARENT_EVENT: string;
|
|
14
15
|
interface RouterParent {
|
|
@@ -23,9 +24,9 @@ interface DomRouterNode extends RouterParent {
|
|
|
23
24
|
parent?: DomRouter;
|
|
24
25
|
child?: DomRouter;
|
|
25
26
|
connected: boolean;
|
|
26
|
-
process(url: string, replace?: boolean): Promise<boolean>;
|
|
27
|
+
process(url: string, replace?: boolean, options?: NavigateOptions, updateHistory?: boolean): Promise<boolean>;
|
|
27
28
|
processError(messageObject: MessageObject): void;
|
|
28
|
-
preProcess(url: string): Promise<boolean>;
|
|
29
|
+
preProcess(url: string, options?: NavigateOptions): Promise<boolean>;
|
|
29
30
|
}
|
|
30
31
|
export interface DomRouter extends DomRouterNode {
|
|
31
32
|
router: Router<PageReference>;
|
|
@@ -37,8 +38,8 @@ export interface DomRouter extends DomRouterNode {
|
|
|
37
38
|
disconnect(): void;
|
|
38
39
|
addPreNavigate(filters: Filter<RouteChange> | Filter<RouteChange>[]): void;
|
|
39
40
|
addErrorNavigate(filters: Filter<MessageObject> | Filter<MessageObject>[]): void;
|
|
40
|
-
navigate(address: PageReference, replace?: boolean): void;
|
|
41
|
-
generateUrl(address: PageReference): string | null;
|
|
41
|
+
navigate(address: PageReference, replace?: boolean, options?: NavigateOptions): void;
|
|
42
|
+
generateUrl(address: PageReference, options?: NavigateOptions): string | null;
|
|
42
43
|
subscribe(callback: RouteCallback, replay?: boolean): Unsubscriber;
|
|
43
44
|
}
|
|
44
45
|
export declare class DomRouterImpl implements DomRouter {
|
|
@@ -109,9 +110,10 @@ export declare class DomRouterImpl implements DomRouter {
|
|
|
109
110
|
* After processing, delegate to a child router, if it exists.
|
|
110
111
|
*
|
|
111
112
|
* @param {string} url - Relative URL string to process
|
|
113
|
+
* @param {NavigateOptions} options - Additional navigation options (i.e. switch root locale)
|
|
112
114
|
* @returns {boolean} - True if the processing was NOT blocked by a preNavigate listener
|
|
113
115
|
*/
|
|
114
|
-
process(url: string,
|
|
116
|
+
process(url: string, _shouldReplace?: boolean, options?: NavigateOptions, _updateHistory?: boolean): Promise<boolean>;
|
|
115
117
|
/**
|
|
116
118
|
* Run the preNavigate filters for this router.
|
|
117
119
|
* After processing, delegate to a child router, if it exists.
|
|
@@ -121,7 +123,7 @@ export declare class DomRouterImpl implements DomRouter {
|
|
|
121
123
|
*
|
|
122
124
|
* @returns {Promise<boolean>} - Resolves to true if successful
|
|
123
125
|
*/
|
|
124
|
-
preProcess(url: string): Promise<boolean>;
|
|
126
|
+
preProcess(url: string, options?: NavigateOptions): Promise<boolean>;
|
|
125
127
|
/**
|
|
126
128
|
* Run the errorNavigate filters for this router.
|
|
127
129
|
* After processing, delegate to a child router, if it exists.
|
|
@@ -138,7 +140,7 @@ export declare class DomRouterImpl implements DomRouter {
|
|
|
138
140
|
* @param {*} options - Usually a boolean; when true the previous browser history
|
|
139
141
|
* entry should be replaced by this one
|
|
140
142
|
*/
|
|
141
|
-
navigate(address: PageReference, replace?: boolean): void;
|
|
143
|
+
navigate(address: PageReference, replace?: boolean, options?: NavigateOptions): void;
|
|
142
144
|
/**
|
|
143
145
|
* lightning/navigation
|
|
144
146
|
* Generate a URL based on the given route.
|
|
@@ -148,7 +150,7 @@ export declare class DomRouterImpl implements DomRouter {
|
|
|
148
150
|
*
|
|
149
151
|
* @returns {Promise<string>}
|
|
150
152
|
*/
|
|
151
|
-
generateUrl(address: PageReference): string | null;
|
|
153
|
+
generateUrl(address: PageReference, options?: NavigateOptions): string | null;
|
|
152
154
|
/**
|
|
153
155
|
* lightning/navigation
|
|
154
156
|
* Subscribe a callback to the Observable on the current route of this router.
|
|
@@ -177,6 +179,10 @@ export declare class DomRouterImpl implements DomRouter {
|
|
|
177
179
|
private _handleParentEvent;
|
|
178
180
|
private _sendEvent;
|
|
179
181
|
private _stripUrlForChild;
|
|
182
|
+
/**
|
|
183
|
+
* Filter the navigate options based on if this is a root router or not.
|
|
184
|
+
*/
|
|
185
|
+
private filterNavigateOptions;
|
|
180
186
|
}
|
|
181
187
|
/**
|
|
182
188
|
* Create a new root Router, attach to the Window.
|
|
@@ -148,7 +148,7 @@ export class DomRouterImpl {
|
|
|
148
148
|
});
|
|
149
149
|
const contextApi = {
|
|
150
150
|
navigate: (address, replace) => this.navigate(address, replace),
|
|
151
|
-
generateUrl: (address) => this.generateUrl(address),
|
|
151
|
+
generateUrl: (address, options) => this.generateUrl(address, options),
|
|
152
152
|
subscribe: (callback, replay) => this.subscribe(callback, replay),
|
|
153
153
|
};
|
|
154
154
|
registerNavigationHelm(this.contextId, contextApi);
|
|
@@ -269,10 +269,11 @@ export class DomRouterImpl {
|
|
|
269
269
|
* After processing, delegate to a child router, if it exists.
|
|
270
270
|
*
|
|
271
271
|
* @param {string} url - Relative URL string to process
|
|
272
|
+
* @param {NavigateOptions} options - Additional navigation options (i.e. switch root locale)
|
|
272
273
|
* @returns {boolean} - True if the processing was NOT blocked by a preNavigate listener
|
|
273
274
|
*/
|
|
274
275
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
275
|
-
async process(url,
|
|
276
|
+
async process(url, _shouldReplace, options, _updateHistory) {
|
|
276
277
|
// Mark the navigation event here instead of in navigate()
|
|
277
278
|
// This way, we catch ALL navigation events, since they all must go through process():
|
|
278
279
|
// 1. A component calls navigate()
|
|
@@ -284,7 +285,7 @@ export class DomRouterImpl {
|
|
|
284
285
|
// Run the root -> leaf chain of pre navigate filters, if this is the root.
|
|
285
286
|
try {
|
|
286
287
|
if (!this.parent) {
|
|
287
|
-
await this.preProcess(url);
|
|
288
|
+
await this.preProcess(url, options);
|
|
288
289
|
}
|
|
289
290
|
}
|
|
290
291
|
catch (e) {
|
|
@@ -298,7 +299,7 @@ export class DomRouterImpl {
|
|
|
298
299
|
// trigger the child to navigate afterwards
|
|
299
300
|
const address = this.router.parseUrl(url);
|
|
300
301
|
if (address) {
|
|
301
|
-
this.router.navigate(address);
|
|
302
|
+
this.router.navigate(address, options);
|
|
302
303
|
}
|
|
303
304
|
return true;
|
|
304
305
|
}
|
|
@@ -311,9 +312,9 @@ export class DomRouterImpl {
|
|
|
311
312
|
*
|
|
312
313
|
* @returns {Promise<boolean>} - Resolves to true if successful
|
|
313
314
|
*/
|
|
314
|
-
preProcess(url) {
|
|
315
|
+
preProcess(url, options) {
|
|
315
316
|
const address = this.router.parseUrl(url);
|
|
316
|
-
const routingMatch = address && this.router.matchRoute(address);
|
|
317
|
+
const routingMatch = address && this.router.matchRoute(address, options);
|
|
317
318
|
// Check that the URL has a matching route, otherwise it is an error.
|
|
318
319
|
if (!routingMatch) {
|
|
319
320
|
return Promise.reject(generateMessageObject(messages.MISSING_ROUTE, [url]));
|
|
@@ -360,9 +361,10 @@ export class DomRouterImpl {
|
|
|
360
361
|
* @param {*} options - Usually a boolean; when true the previous browser history
|
|
361
362
|
* entry should be replaced by this one
|
|
362
363
|
*/
|
|
363
|
-
navigate(address, replace) {
|
|
364
|
+
navigate(address, replace, options) {
|
|
365
|
+
const routerOptions = this.filterNavigateOptions(options);
|
|
364
366
|
// Ensure there is a string URL to pass to the navigation event.
|
|
365
|
-
let url = this.router.generateUrl(address);
|
|
367
|
+
let url = this.router.generateUrl(address, routerOptions);
|
|
366
368
|
if (url) {
|
|
367
369
|
// If this router is a child, we need to prepend the parent's matching portion
|
|
368
370
|
// of the url before sending the navigate event up
|
|
@@ -381,8 +383,9 @@ export class DomRouterImpl {
|
|
|
381
383
|
*
|
|
382
384
|
* @returns {Promise<string>}
|
|
383
385
|
*/
|
|
384
|
-
generateUrl(address) {
|
|
385
|
-
const
|
|
386
|
+
generateUrl(address, options) {
|
|
387
|
+
const routerOptions = this.filterNavigateOptions(options);
|
|
388
|
+
const url = this.router.generateUrl(address, routerOptions);
|
|
386
389
|
// Invalid addresses need to return null to indicate they are invalid
|
|
387
390
|
if (!url) {
|
|
388
391
|
return null;
|
|
@@ -425,6 +428,18 @@ export class DomRouterImpl {
|
|
|
425
428
|
}
|
|
426
429
|
return url;
|
|
427
430
|
}
|
|
431
|
+
/**
|
|
432
|
+
* Filter the navigate options based on if this is a root router or not.
|
|
433
|
+
*/
|
|
434
|
+
filterNavigateOptions(options) {
|
|
435
|
+
const isRoot = !this.parent;
|
|
436
|
+
const routerOptions = {
|
|
437
|
+
...options,
|
|
438
|
+
// Only allow switch locales if this is a root router
|
|
439
|
+
locale: isRoot ? options?.locale : undefined,
|
|
440
|
+
};
|
|
441
|
+
return routerOptions;
|
|
442
|
+
}
|
|
428
443
|
}
|
|
429
444
|
/**
|
|
430
445
|
* Create a new root Router, attach to the Window.
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
import { DomRouterImpl } from 'lwr/domRouter';
|
|
8
8
|
import type { Router, PageReference } from 'lwr/router';
|
|
9
9
|
import type { DomRouterConfig } from 'lwr/domRouterUtils';
|
|
10
|
+
import type { NavigateOptions } from '../routerUtils/types';
|
|
10
11
|
export declare class HistoryRouter extends DomRouterImpl {
|
|
11
12
|
historyDisabled: boolean;
|
|
12
13
|
/**
|
|
@@ -26,7 +27,7 @@ export declare class HistoryRouter extends DomRouterImpl {
|
|
|
26
27
|
*
|
|
27
28
|
* @returns {boolean} - True if the processing was NOT blocked by a preNavigate listener
|
|
28
29
|
*/
|
|
29
|
-
process(url: string, shouldReplace: boolean, updateHistory?: boolean): Promise<boolean>;
|
|
30
|
+
process(url: string, shouldReplace: boolean, options?: NavigateOptions, updateHistory?: boolean): Promise<boolean>;
|
|
30
31
|
/**
|
|
31
32
|
* Update the root route, and trickle down the router tree.
|
|
32
33
|
* Redirect to use the base path, if it is missing.
|
|
@@ -37,7 +38,7 @@ export declare class HistoryRouter extends DomRouterImpl {
|
|
|
37
38
|
}
|
|
38
39
|
/**
|
|
39
40
|
* Create a new root Router, attach to the Window.
|
|
40
|
-
* This is the public,
|
|
41
|
+
* This is the public, programmatic API for root router creation.
|
|
41
42
|
* An application can only have ONE root router.
|
|
42
43
|
*
|
|
43
44
|
* @param {object} config - The router config object
|
|
@@ -48,9 +48,9 @@ export class HistoryRouter extends DomRouterImpl {
|
|
|
48
48
|
*
|
|
49
49
|
* @returns {boolean} - True if the processing was NOT blocked by a preNavigate listener
|
|
50
50
|
*/
|
|
51
|
-
async process(url, shouldReplace, updateHistory = true) {
|
|
51
|
+
async process(url, shouldReplace, options, updateHistory = true) {
|
|
52
52
|
// Run the preNavigate hooks to check if this event should be processed.
|
|
53
|
-
const canContinue = await super.process(url);
|
|
53
|
+
const canContinue = await super.process(url, shouldReplace, options, updateHistory);
|
|
54
54
|
// Update the window location if this router is connected and is the root router
|
|
55
55
|
if (canContinue && !this.historyDisabled && updateHistory && this.connected && !this.parent) {
|
|
56
56
|
// Update the window history.
|
|
@@ -70,12 +70,12 @@ export class HistoryRouter extends DomRouterImpl {
|
|
|
70
70
|
* @param {string} url - The URL to go to
|
|
71
71
|
*/
|
|
72
72
|
catchBrowserUpdate(url) {
|
|
73
|
-
this.process(url, false, false);
|
|
73
|
+
this.process(url, false, {}, false);
|
|
74
74
|
}
|
|
75
75
|
}
|
|
76
76
|
/**
|
|
77
77
|
* Create a new root Router, attach to the Window.
|
|
78
|
-
* This is the public,
|
|
78
|
+
* This is the public, programmatic API for root router creation.
|
|
79
79
|
* An application can only have ONE root router.
|
|
80
80
|
*
|
|
81
81
|
* @param {object} config - The router config object
|
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
import type { ContextId } from 'lwr/navigationContext';
|
|
2
2
|
import type { PageReference } from 'lwr/router';
|
|
3
|
+
import type { NavigateOptions } from '../routerUtils/types';
|
|
3
4
|
/**
|
|
4
|
-
* Navigate programmatically.
|
|
5
|
+
* Navigate programmatically to a page reference.
|
|
5
6
|
* The Promise used within is deliberately not returned.
|
|
6
7
|
*
|
|
7
8
|
* @param {HTMLElement} context - The navigation context
|
|
8
|
-
* @param {
|
|
9
|
-
* @param {
|
|
10
|
-
*
|
|
9
|
+
* @param {PageReference} pageReference - A page reference location
|
|
10
|
+
* @param {boolean} replace - When true the previous browser history entry should be replaced by this one
|
|
11
|
+
* @param {NavigateOptions} options - Additional navigation options (i.e. switch locale)
|
|
11
12
|
*/
|
|
12
|
-
export declare function navigate(context: ContextId, pageReference: PageReference, replace?: boolean): void;
|
|
13
|
+
export declare function navigate(context: ContextId, pageReference: PageReference, replace?: boolean, options?: NavigateOptions): void;
|
|
13
14
|
/**
|
|
14
|
-
* Generate a URL for the given
|
|
15
|
+
* Generate a URL for the given page reference.
|
|
15
16
|
*
|
|
16
17
|
* @param {HTMLElement} context - The navigation context
|
|
17
|
-
* @param {
|
|
18
|
+
* @param {PageReference} pageReference - A page reference location
|
|
18
19
|
*
|
|
19
20
|
* @returns {Promise<string>}
|
|
20
21
|
*/
|
|
21
|
-
export declare function generateUrl(context: ContextId, pageReference: PageReference): string | null;
|
|
22
|
+
export declare function generateUrl(context: ContextId, pageReference: PageReference, options?: NavigateOptions): string | null;
|
|
22
23
|
//# sourceMappingURL=navigationApi.d.ts.map
|
|
@@ -1,27 +1,27 @@
|
|
|
1
1
|
import { getNavigationHelm } from 'lwr/contextUtils';
|
|
2
2
|
/**
|
|
3
|
-
* Navigate programmatically.
|
|
3
|
+
* Navigate programmatically to a page reference.
|
|
4
4
|
* The Promise used within is deliberately not returned.
|
|
5
5
|
*
|
|
6
6
|
* @param {HTMLElement} context - The navigation context
|
|
7
|
-
* @param {
|
|
8
|
-
* @param {
|
|
9
|
-
*
|
|
7
|
+
* @param {PageReference} pageReference - A page reference location
|
|
8
|
+
* @param {boolean} replace - When true the previous browser history entry should be replaced by this one
|
|
9
|
+
* @param {NavigateOptions} options - Additional navigation options (i.e. switch locale)
|
|
10
10
|
*/
|
|
11
|
-
export function navigate(context, pageReference, replace) {
|
|
11
|
+
export function navigate(context, pageReference, replace, options) {
|
|
12
12
|
const api = getNavigationHelm(context);
|
|
13
|
-
api.navigate(pageReference, replace);
|
|
13
|
+
api.navigate(pageReference, replace, options);
|
|
14
14
|
}
|
|
15
15
|
/**
|
|
16
|
-
* Generate a URL for the given
|
|
16
|
+
* Generate a URL for the given page reference.
|
|
17
17
|
*
|
|
18
18
|
* @param {HTMLElement} context - The navigation context
|
|
19
|
-
* @param {
|
|
19
|
+
* @param {PageReference} pageReference - A page reference location
|
|
20
20
|
*
|
|
21
21
|
* @returns {Promise<string>}
|
|
22
22
|
*/
|
|
23
|
-
export function generateUrl(context, pageReference) {
|
|
23
|
+
export function generateUrl(context, pageReference, options) {
|
|
24
24
|
const api = getNavigationHelm(context);
|
|
25
|
-
return api.generateUrl(pageReference);
|
|
25
|
+
return api.generateUrl(pageReference, options);
|
|
26
26
|
}
|
|
27
27
|
//# sourceMappingURL=navigationApi.js.map
|
|
@@ -60,16 +60,16 @@ function NavigationMixin(Base) {
|
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
|
-
[Navigate](pageRef, replace) {
|
|
63
|
+
[Navigate](pageRef, replace, options) {
|
|
64
64
|
if (!isSSR) {
|
|
65
65
|
this[GetContext]();
|
|
66
|
-
navigate(this[NavContext], pageRef, replace);
|
|
66
|
+
navigate(this[NavContext], pageRef, replace, options);
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
|
-
async [GenerateUrl](pageRef) {
|
|
69
|
+
async [GenerateUrl](pageRef, options) {
|
|
70
70
|
if (!isSSR) {
|
|
71
71
|
this[GetContext]();
|
|
72
|
-
return generateUrl(this[NavContext], pageRef);
|
|
72
|
+
return generateUrl(this[NavContext], pageRef, options);
|
|
73
73
|
}
|
|
74
74
|
else {
|
|
75
75
|
return null;
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
|
6
6
|
*/
|
|
7
7
|
import type { PageReference, RouteCallback, RouteDefinition, RouteHandler, RouteHandlerClass, Router, RouterConfig, RouteDestination, RoutingMatch, RoutingResult, ViewSet, ViewInfo } from 'lwr/routerUtils';
|
|
8
|
-
export declare function createRouter(config?: RouterConfig): Router<PageReference>;
|
|
8
|
+
export declare function createRouter(config?: RouterConfig<PageReference>): Router<PageReference>;
|
|
9
9
|
export type { PageReference, Router, RouterConfig, RouteCallback, RouteDefinition, RouteDestination, RouteHandler, RouteHandlerClass, RoutingMatch, RoutingResult, ViewSet, ViewInfo, };
|
|
10
10
|
export type { Module, RouteHandlerCallback, RouteHandlerModule, RouteInstance, StringAttributes, } from 'lwr/routerUtils';
|
|
11
11
|
//# sourceMappingURL=router.d.ts.map
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* SPDX-License-Identifier: MIT
|
|
5
5
|
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
|
6
6
|
*/
|
|
7
|
-
import { freeze, getUrlFromPageReference, getPageReferenceFromUrl, matchRouteByUrl, getUrlFromPageReferenceAndRouteDef, parseRoutes, } from 'lwr/routerUtils';
|
|
7
|
+
import { freeze, getUrlFromPageReference, getPageReferenceFromUrl, matchRouteByUrl, getUrlFromPageReferenceAndRouteDef, parseRoutes, DEFAULT_I18N_ROUTER_CONFIG, } from 'lwr/routerUtils';
|
|
8
8
|
import { generateMessage, messages } from 'lwr/routerErrors';
|
|
9
9
|
import { createObservable } from 'lwr/observable';
|
|
10
10
|
class RouterImpl {
|
|
@@ -20,10 +20,11 @@ class RouterImpl {
|
|
|
20
20
|
this.routeObservable = createObservable();
|
|
21
21
|
this.config = {
|
|
22
22
|
basePath: config.basePath || '',
|
|
23
|
+
i18n: config.i18n || DEFAULT_I18N_ROUTER_CONFIG,
|
|
23
24
|
caseSensitive: Boolean(config.caseSensitive),
|
|
24
25
|
routes: config.routes || [],
|
|
25
|
-
generateUrl: (address) => getUrlFromPageReference(address, this.compiledRoutes, this.config.basePath),
|
|
26
|
-
parseUrl: (url) => getPageReferenceFromUrl(url, this.compiledRoutes, this.config.basePath),
|
|
26
|
+
generateUrl: (address, options) => getUrlFromPageReference(address, this.compiledRoutes, this.config.basePath, this.config.i18n, options),
|
|
27
|
+
parseUrl: (url) => getPageReferenceFromUrl(url, this.compiledRoutes, this.config.basePath, this.config.i18n),
|
|
27
28
|
};
|
|
28
29
|
const { DEPRECATED_getRouteFromUrl, DEPRECATED_getUrlFromRoute } = config;
|
|
29
30
|
if (DEPRECATED_getRouteFromUrl) {
|
|
@@ -39,13 +40,13 @@ class RouterImpl {
|
|
|
39
40
|
*
|
|
40
41
|
* @param address
|
|
41
42
|
*/
|
|
42
|
-
generateUrl(address) {
|
|
43
|
+
generateUrl(address, options) {
|
|
43
44
|
const { DEPRECATED_getUrlFromRoute: getUrlFromRoute } = this.deprecatedConfig;
|
|
44
45
|
if (getUrlFromRoute) {
|
|
45
|
-
return getUrlFromRoute(address, this.config.generateUrl);
|
|
46
|
+
return getUrlFromRoute(address, this.config.generateUrl, options);
|
|
46
47
|
}
|
|
47
48
|
else {
|
|
48
|
-
return this.config.generateUrl(address);
|
|
49
|
+
return this.config.generateUrl(address, options);
|
|
49
50
|
}
|
|
50
51
|
}
|
|
51
52
|
/**
|
|
@@ -68,14 +69,14 @@ class RouterImpl {
|
|
|
68
69
|
*
|
|
69
70
|
* @param address - The address to match
|
|
70
71
|
*/
|
|
71
|
-
matchRoute(address) {
|
|
72
|
-
const url = typeof address === 'string' ? address : this.generateUrl(address);
|
|
72
|
+
matchRoute(address, options) {
|
|
73
|
+
const url = typeof address === 'string' ? address : this.generateUrl(address, options);
|
|
73
74
|
if (url === null) {
|
|
74
75
|
return null;
|
|
75
76
|
}
|
|
76
|
-
const match = matchRouteByUrl(url, this.compiledRoutes, this.config.basePath);
|
|
77
|
+
const match = matchRouteByUrl(url, this.compiledRoutes, this.config.basePath, this.config.i18n, options);
|
|
77
78
|
const pathMatch = match &&
|
|
78
|
-
getUrlFromPageReferenceAndRouteDef(match.route.pageReference, match.routeDefinition, this.config.basePath);
|
|
79
|
+
getUrlFromPageReferenceAndRouteDef(match.route.pageReference, match.routeDefinition, this.config.basePath, this.config.i18n, options);
|
|
79
80
|
if (!match || !pathMatch) {
|
|
80
81
|
return null;
|
|
81
82
|
}
|
|
@@ -90,10 +91,10 @@ class RouterImpl {
|
|
|
90
91
|
*
|
|
91
92
|
* @param address - The address to match to a viewset
|
|
92
93
|
*/
|
|
93
|
-
async resolveView(address) {
|
|
94
|
+
async resolveView(address, options) {
|
|
94
95
|
return new Promise((resolve, reject) => {
|
|
95
96
|
// get the RoutingMatch: { pathMatch, route, routeDefinition }
|
|
96
|
-
const routingMatch = this.matchRoute(address);
|
|
97
|
+
const routingMatch = this.matchRoute(address, options);
|
|
97
98
|
if (!routingMatch) {
|
|
98
99
|
return reject(generateMessage(messages.NO_ROUTE_MATCH, [JSON.stringify(address)]));
|
|
99
100
|
}
|
|
@@ -121,8 +122,8 @@ class RouterImpl {
|
|
|
121
122
|
*
|
|
122
123
|
* @param {object} address
|
|
123
124
|
*/
|
|
124
|
-
navigate(address) {
|
|
125
|
-
const routingMatch = this.matchRoute(address);
|
|
125
|
+
navigate(address, options) {
|
|
126
|
+
const routingMatch = this.matchRoute(address, options);
|
|
126
127
|
if (!routingMatch) {
|
|
127
128
|
throw new Error(generateMessage(messages.MISSING_ROUTE, [JSON.stringify(address)]));
|
|
128
129
|
}
|
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
* SPDX-License-Identifier: MIT
|
|
5
5
|
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
|
|
6
6
|
*/
|
|
7
|
-
import type { CompiledRouteDefinition, CompiledRoutingMatch, PageReference } from './types';
|
|
7
|
+
import type { CompiledRouteDefinition, CompiledRoutingMatch, I18nRouterConfig, NavigateOptions, PageReference } from './types';
|
|
8
|
+
export declare const DEFAULT_I18N_ROUTER_CONFIG: I18nRouterConfig;
|
|
8
9
|
/**
|
|
9
10
|
* f(URL, routes[]?) -> route
|
|
10
11
|
* Match a URL's path to a Route Definition, use these to build a route.
|
|
@@ -12,10 +13,11 @@ import type { CompiledRouteDefinition, CompiledRoutingMatch, PageReference } fro
|
|
|
12
13
|
* @param {string} url - URL string to turn into a route
|
|
13
14
|
* @param {array[object]} routeDefs - List of Route Definitions to match to the url
|
|
14
15
|
* @param {string} basePath - Optional: if provided, remove the base path before conversion.
|
|
16
|
+
* @param {object} i18n - Optional: if provided, remove the non-default locale before conversion.
|
|
15
17
|
*
|
|
16
18
|
* @returns {object}
|
|
17
19
|
*/
|
|
18
|
-
export declare function matchRouteByUrl(url: string, routeDefs: CompiledRouteDefinition[], basePath?: string): CompiledRoutingMatch | null;
|
|
20
|
+
export declare function matchRouteByUrl(url: string, routeDefs: CompiledRouteDefinition[], basePath?: string, i18n?: I18nRouterConfig, options?: NavigateOptions): CompiledRoutingMatch | null;
|
|
19
21
|
/**
|
|
20
22
|
* Serializes the given pageReference into a url based on the first route definition
|
|
21
23
|
* with a binding the pageReference matches against.
|
|
@@ -24,16 +26,16 @@ export declare function matchRouteByUrl(url: string, routeDefs: CompiledRouteDef
|
|
|
24
26
|
* @param basePath Base path for the url
|
|
25
27
|
* @returns the url or null if no match
|
|
26
28
|
*/
|
|
27
|
-
export declare function getUrlFromPageReference(pageReference: PageReference, routeDefs: CompiledRouteDefinition[], basePath?: string): string | null;
|
|
29
|
+
export declare function getUrlFromPageReference(pageReference: PageReference, routeDefs: CompiledRouteDefinition[], basePath?: string, i18n?: I18nRouterConfig, options?: NavigateOptions): string | null;
|
|
28
30
|
/**
|
|
29
31
|
* Generates a url for the given pageReference using the given routeDef
|
|
30
32
|
* @param pageReference pageReference to serialize
|
|
31
33
|
* @param routeDef routeDef to that defines how serialize the given pageReference
|
|
32
34
|
* @returns url for the given pageReference
|
|
33
35
|
*/
|
|
34
|
-
export declare function getUrlFromPageReferenceAndRouteDef(pageReference: PageReference, routeDef: CompiledRouteDefinition, basePath?: string): string;
|
|
36
|
+
export declare function getUrlFromPageReferenceAndRouteDef(pageReference: PageReference, routeDef: CompiledRouteDefinition, basePath?: string, i18n?: I18nRouterConfig, options?: NavigateOptions): string;
|
|
35
37
|
/**
|
|
36
38
|
* Obtains the pageReference for the given URL
|
|
37
39
|
*/
|
|
38
|
-
export declare function getPageReferenceFromUrl(url: string, routeDefs: CompiledRouteDefinition[], basePath?: string): PageReference | null;
|
|
40
|
+
export declare function getPageReferenceFromUrl(url: string, routeDefs: CompiledRouteDefinition[], basePath?: string, i18n?: I18nRouterConfig): PageReference | null;
|
|
39
41
|
//# sourceMappingURL=routeUtils.d.ts.map
|