@rxdi/router 0.7.219 → 0.7.221

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.
@@ -1,101 +1,6 @@
1
- export type NavigationTrigger = {
2
- activate: () => any;
3
- /**
4
- * Configures what triggers Router navigation events:
5
- * - `POPSTATE`: popstate events on the current `window`
6
- * - `CLICK`: click events on `<a>` links leading to the current page
7
- *
8
- * This method is invoked with the pre-configured values when creating a new Router instance.
9
- * By default, both `POPSTATE` and `CLICK` are enabled. This setup is expected to cover most of the use cases.
10
- *
11
- * See the `router-config.js` for the default navigation triggers config. Based on it, you can
12
- * create the own one and only import the triggers you need, instead of pulling in all the code,
13
- * e.g. if you want to handle `click` differently.
14
- *
15
- * See also **Navigation Triggers** section in [Live Examples](#/classes/Router/demos/demo/index.html).
16
- */
17
- inactivate: () => any;
18
- };
19
- /**
20
- */
21
- export class Resolver {
22
- /**
23
- * URL constructor polyfill hook. Creates and returns an URL instance.
24
- */
25
- static __createUrl(url: any, base: any): URL;
26
- constructor(routes: any, options?: {});
27
- baseUrl: any;
28
- errorHandler: any;
29
- resolveRoute: any;
30
- context: any;
31
- root: any;
32
- /**
33
- * Returns the current list of routes (as a shallow copy). Adding / removing
34
- * routes to / from the returned array does not affect the routing config,
35
- * but modifying the route objects does.
36
- *
37
- * @return {!Array<!Router.Route>}
38
- */
39
- getRoutes(): Array<Router.Route>;
40
- /**
41
- * Sets the routing config (replacing the existing one).
42
- *
43
- * @param {!Array<!Router.Route>|!Router.Route} routes a single route or an array of those
44
- * (the array is shallow copied)
45
- */
46
- setRoutes(routes: Array<Router.Route> | Router.Route): void;
47
- /**
48
- * Appends one or several routes to the routing config and returns the
49
- * effective routing config after the operation.
50
- *
51
- * @param {!Array<!Router.Route>|!Router.Route} routes a single route or an array of those
52
- * (the array is shallow copied)
53
- * @return {!Array<!Router.Route>}
54
- * @protected
55
- */
56
- protected addRoutes(routes: Array<Router.Route> | Router.Route): Array<Router.Route>;
57
- /**
58
- * Removes all existing routes from the routing config.
59
- */
60
- removeRoutes(): void;
61
- /**
62
- * Asynchronously resolves the given pathname, i.e. finds all routes matching
63
- * the pathname and tries resolving them one after another in the order they
64
- * are listed in the routes config until the first non-null result.
65
- *
66
- * Returns a promise that is fulfilled with the return value of an object that consists of the first
67
- * route handler result that returns something other than `null` or `undefined` and context used to get this result.
68
- *
69
- * If no route handlers return a non-null result, or if no route matches the
70
- * given pathname the returned promise is rejected with a 'page not found'
71
- * `Error`.
72
- *
73
- * @param {!string|!{pathname: !string}} pathnameOrContext the pathname to
74
- * resolve or a context object with a `pathname` property and other
75
- * properties to pass to the route resolver functions.
76
- * @return {!Promise<any>}
77
- */
78
- resolve(pathnameOrContext: string | {
79
- pathname: string;
80
- }): Promise<any>;
81
- /**
82
- * If the baseUrl property is set, transforms the baseUrl and returns the full
83
- * actual `base` string for using in the `new URL(path, base);` and for
84
- * prepernding the paths with. The returned base ends with a trailing slash.
85
- *
86
- * Otherwise, returns empty string.
87
- */
88
- get __effectiveBaseUrl(): any;
89
- /**
90
- * If the baseUrl is set, matches the pathname with the router’s baseUrl,
91
- * and returns the local pathname with the baseUrl stripped out.
92
- *
93
- * If the pathname does not match the baseUrl, returns undefined.
94
- *
95
- * If the `baseUrl` is not set, returns the unmodified pathname argument.
96
- */
97
- __normalizePathname(pathname: any): any;
98
- }
1
+ import { Resolver } from './resolver';
2
+ import { ChainItem, PreventResult, RedirectResult, // fix for shadowing
3
+ Route, RouteContext, RouteParams, RouteResult, RouterLocation, RouterOptions } from './types';
99
4
  /**
100
5
  * A simple client-side router for single-page applications. It uses
101
6
  * express-style middleware and has a first-class support for Web Components and
@@ -131,23 +36,18 @@ export class Resolver {
131
36
  * a given path. It can re-render when triggered or automatically on
132
37
  * 'popstate' and / or 'click' events.
133
38
  */
134
- export class Router extends Resolver {
135
- /**
136
- * Triggers navigation to a new path. Returns a boolean without waiting until
137
- * the navigation is complete. Returns `true` if at least one `Router`
138
- * has handled the navigation (was subscribed and had `baseUrl` matching
139
- * the `path` argument), otherwise returns `false`.
140
- *
141
- * @param {!string|!{pathname: !string, search: (string|undefined), hash: (string|undefined)}} path
142
- * a new in-app path string, or an URL-like object with `pathname`
143
- * string property, and optional `search` and `hash` string properties.
144
- * @return {boolean}
145
- */
146
- static go(path: string | {
147
- pathname: string;
148
- search: (string | undefined);
149
- hash: (string | undefined);
150
- }): boolean;
39
+ export declare class Router extends Resolver {
40
+ ready: Promise<RouterLocation | Node | undefined>;
41
+ location: RouterLocation;
42
+ __lastStartedRenderId: number;
43
+ __navigationEventHandler: (event?: CustomEvent) => void;
44
+ __outlet?: Node;
45
+ __previousContext?: RouteContext;
46
+ __urlForName?: (routeName: string, params?: RouteParams) => string;
47
+ __createdByRouter: WeakMap<HTMLElement, boolean>;
48
+ __addedByRouter: WeakMap<HTMLElement, boolean>;
49
+ __appearingContent?: HTMLElement[] | null;
50
+ __disappearingContent?: HTMLElement[] | null;
151
51
  /**
152
52
  * Creates a new Router instance with a given outlet, and
153
53
  * automatically subscribes it to navigation events on the `window`.
@@ -157,45 +57,16 @@ export class Router extends Resolver {
157
57
  * const router = new Router();
158
58
  * router.setOutlet(outlet);
159
59
  * ```
160
- * @param {?Node=} outlet
161
- * @param {?RouterOptions=} options
60
+ * @param outlet
61
+ * @param options
162
62
  */
163
- constructor(outlet?: (Node | null) | undefined, options?: (RouterOptions | null) | undefined);
164
- resolveRoute: (context: any) => Promise<any>;
63
+ constructor(outlet?: Node, options?: RouterOptions);
64
+ __resolveRoute(context: RouteContext): Promise<RouteResult | undefined>;
165
65
  /**
166
- * The base URL for all routes in the router instance. By default,
167
- * if the base element exists in the `<head>`, vaadin-router
168
- * takes the `<base href>` attribute value, resolves against current `document.URL`
169
- * and gets the `pathname` from the result.
170
- *
171
- * @public
172
- * @type {string}
66
+ * Takes current routes and set it
67
+ * @param routes: Route<C>[]
68
+ * @returns void
173
69
  */
174
- public baseUrl: string;
175
- /**
176
- * A promise that is settled after the current render cycle completes. If
177
- * there is no render cycle in progress the promise is immediately settled
178
- * with the last render cycle result.
179
- *
180
- * @public
181
- * @type {!Promise<!RouterLocation>}
182
- */
183
- public ready: Promise<RouterLocation>;
184
- /**
185
- * Contains read-only information about the current router location:
186
- * pathname, active routes, parameters. See the
187
- * [Location type declaration](#/classes/RouterLocation)
188
- * for more details.
189
- *
190
- * @public
191
- * @type {!RouterLocation}
192
- */
193
- public location: RouterLocation;
194
- __lastStartedRenderId: number;
195
- __navigationEventHandler: any;
196
- __createdByRouter: WeakMap<object, any>;
197
- __addedByRouter: WeakMap<object, any>;
198
- __resolveRoute(context: any): Promise<any>;
199
70
  /**
200
71
  * Sets the router outlet (the DOM node where the content for the current
201
72
  * route is inserted). Any content pre-existing in the router outlet is
@@ -203,17 +74,24 @@ export class Router extends Resolver {
203
74
  *
204
75
  * NOTE: this method is automatically invoked first time when creating a new Router instance.
205
76
  *
206
- * @param {?Node} outlet the DOM node where the content for the current route
77
+ * @param outlet the DOM node where the content for the current route
207
78
  * is inserted.
208
79
  */
209
- setOutlet(outlet: Node | null): void;
210
- __outlet: Node;
80
+ setOutlet(outlet?: Node): void;
81
+ /**
82
+ * Returns the current router outlet. The initial value is undefined.
83
+ */
211
84
  /**
212
- * Returns the current router outlet. The initial value is `undefined`.
85
+ * Returns the current router outlet. The initial value is undefined.
213
86
  *
214
- * @return {?Node} the current router outlet (or `undefined`)
87
+ * @return the current router outlet (or `undefined`)
88
+ */
89
+ getOutlet(): Node | undefined;
90
+ /**
91
+ * Takes current routes and set it
92
+ * @param routes: Route | Route[]
93
+ * @returns Route | Route[]
215
94
  */
216
- getOutlet(): Node | null;
217
95
  /**
218
96
  * Sets the routing config (replacing the existing one) and triggers a
219
97
  * navigation event so that the router outlet is refreshed according to the
@@ -296,15 +174,20 @@ export class Router extends Resolver {
296
174
  * with current context. Note: the component created by this function is reused if visiting the same path twice in row.
297
175
  *
298
176
  *
299
- * @param {!Array<!Route>|!Route} routes a single route or an array of those
300
- * @param {?boolean} skipRender configure the router but skip rendering the
177
+ * @param routes a single route or an array of those
178
+ * @param skipRender configure the router but skip rendering the
301
179
  * route corresponding to the current `window.location` values
302
180
  *
303
- * @return {!Promise<!Node>}
181
+ * @return
182
+ */
183
+ setRoutes(routes: Route | Route[], skipRender?: boolean): Promise<RouterLocation | Node | undefined>;
184
+ /**
185
+ * Asynchronously resolves the given pathname and renders the resolved route component into the router outlet. If no router outlet is set at the time of calling this method, or at the time when the route resolution is completed, a TypeError is thrown.
186
+ * Returns a promise that is fulfilled with the router outlet DOM Node after the route component is created and inserted into the router outlet, or rejected if no route matches the given path.
187
+ * If another render pass is started before the previous one is completed, the result of the previous render pass is ignored.
188
+ * @param pathnameOrContext — the pathname to render or a context object with a pathname property and other properties to pass to the resolver.
189
+ * @param shouldUpdateHistory
304
190
  */
305
- setRoutes(routes: Array<Route> | Route, skipRender?: boolean | null): Promise<Node>;
306
- __previousContext: any;
307
- __urlForName: (routeName: any, params: any) => any;
308
191
  /**
309
192
  * Asynchronously resolves the given pathname and renders the resolved route
310
193
  * component into the router outlet. If no router outlet is set at the time of
@@ -318,43 +201,42 @@ export class Router extends Resolver {
318
201
  * If another render pass is started before the previous one is completed, the
319
202
  * result of the previous render pass is ignored.
320
203
  *
321
- * @param {!string|!{pathname: !string, search: ?string, hash: ?string}} pathnameOrContext
204
+ * @param pathnameOrContext
322
205
  * the pathname to render or a context object with a `pathname` property,
323
206
  * optional `search` and `hash` properties, and other properties
324
207
  * to pass to the resolver.
325
- * @param {boolean=} shouldUpdateHistory
208
+ * @param shouldUpdateHistory
326
209
  * update browser history with the rendered location
327
- * @return {!Promise<!Node>}
210
+ * @return
328
211
  */
329
- render(pathnameOrContext: string | {
212
+ render(pathnameOrContext: string | Partial<RouteContext>, shouldUpdateHistory?: boolean): Promise<RouterLocation | Node | undefined>;
213
+ __fullyResolveChain(topOfTheChainContextBeforeRedirects: RouteContext, contextBeforeRedirects?: RouteContext): Promise<RouteContext>;
214
+ __findComponentContextAfterAllRedirects(context: RouteContext): Promise<RouteContext>;
215
+ __amendWithOnBeforeCallbacks(contextWithFullChain: RouteContext): Promise<RouteContext>;
216
+ __runOnBeforeCallbacks(newContext: RouteContext): Promise<RouteContext>;
217
+ __runOnBeforeLeaveCallbacks(callbacks: Promise<PreventResult | RedirectResult | void | undefined>, newContext: RouteContext, commands: {
218
+ prevent: () => PreventResult;
219
+ }, chainElement: ChainItem): Promise<PreventResult | RedirectResult | void | undefined>;
220
+ __runOnBeforeEnterCallbacks(callbacks: Promise<PreventResult | RedirectResult | void | undefined>, newContext: RouteContext, commands: {
221
+ prevent: () => PreventResult;
222
+ redirect: (pathname: string) => RedirectResult;
223
+ }, chainElement: ChainItem): Promise<PreventResult | RedirectResult | void | undefined>;
224
+ __isReusableElement(element?: HTMLElement, otherElement?: HTMLElement): boolean;
225
+ __isLatestRender(context: RouteContext): boolean;
226
+ __redirect(redirectData: {
330
227
  pathname: string;
331
- search: string | null;
332
- hash: string | null;
333
- }, shouldUpdateHistory?: boolean | undefined): Promise<Node>;
334
- __fullyResolveChain(topOfTheChainContextBeforeRedirects: any, contextBeforeRedirects?: any): any;
335
- __findComponentContextAfterAllRedirects(context: any): any;
336
- __amendWithOnBeforeCallbacks(contextWithFullChain: any): Promise<any>;
337
- __runOnBeforeCallbacks(newContext: any): Promise<any>;
338
- __runOnBeforeLeaveCallbacks(callbacks: any, newContext: any, commands: any, chainElement: any): any;
339
- __runOnBeforeEnterCallbacks(callbacks: any, newContext: any, commands: any, chainElement: any): any;
340
- __isReusableElement(element: any, otherElement: any): boolean;
341
- __isLatestRender(context: any): boolean;
342
- __redirect(redirectData: any, counter: any, renderId: any): Promise<any>;
228
+ from: string;
229
+ params: RouteParams;
230
+ }, counter?: number, renderId?: number): Promise<RouteContext>;
343
231
  __ensureOutlet(outlet?: Node): void;
344
- __updateBrowserHistory({ pathname, search, hash }: {
345
- pathname: any;
346
- search?: string;
347
- hash?: string;
348
- }, replace: any): void;
349
- __copyUnchangedElements(context: any, previousContext: any): Node;
350
- __addAppearingContent(context: any, previousContext: any): void;
351
- __appearingContent: any[];
352
- __disappearingContent: any[];
232
+ __updateBrowserHistory({ pathname, search, hash }: RouteContext, replace?: boolean): void;
233
+ __copyUnchangedElements(context: RouteContext, previousContext?: RouteContext): HTMLElement | Node;
234
+ __addAppearingContent(context: RouteContext, previousContext?: RouteContext): void;
353
235
  __removeDisappearingContent(): void;
354
236
  __removeAppearingContent(): void;
355
- __runOnAfterLeaveCallbacks(currentContext: any, targetContext: any): void;
356
- __runOnAfterEnterCallbacks(currentContext: any): void;
357
- __animateIfNeeded(context: any): Promise<any>;
237
+ __runOnAfterLeaveCallbacks(currentContext: RouteContext, targetContext?: RouteContext): void;
238
+ __runOnAfterEnterCallbacks(currentContext: RouteContext): void;
239
+ __animateIfNeeded(context: RouteContext): Promise<RouteContext>;
358
240
  /**
359
241
  * Subscribes this instance to navigation events on the `window`.
360
242
  *
@@ -367,7 +249,7 @@ export class Router extends Resolver {
367
249
  * method.
368
250
  */
369
251
  unsubscribe(): void;
370
- __onNavigationEvent(event: any): void;
252
+ __onNavigationEvent(event?: CustomEvent): void;
371
253
  /**
372
254
  * Generates a URL for the route with the given name, optionally performing
373
255
  * substitution of parameters.
@@ -379,25 +261,40 @@ export class Router extends Resolver {
379
261
  * It is not possible to generate URLs using a name for routes set with
380
262
  * a children function.
381
263
  *
382
- * @function urlForName
383
- * @param {!string} name the route name or the route’s `component` name.
384
- * @param {Params=} params Optional object with route path parameters.
264
+ * @param name the route name or the route’s `component` name.
265
+ * @param params Optional object with route path parameters.
385
266
  * Named parameters are passed by name (`params[name] = value`), unnamed
386
267
  * parameters are passed by index (`params[index] = value`).
387
268
  *
388
- * @return {string}
269
+ * @return
389
270
  */
390
- urlForName(name: string, params?: Params | undefined): string;
271
+ urlForName(name: string, params?: RouteParams): string;
391
272
  /**
392
273
  * Generates a URL for the given route path, optionally performing
393
274
  * substitution of parameters.
394
275
  *
395
- * @param {!string} path string route path declared in [express.js syntax](https://expressjs.com/en/guide/routing.html#route-paths").
396
- * @param {Params=} params Optional object with route path parameters.
276
+ * @param path string route path declared in [express.js syntax](https://expressjs.com/en/guide/routing.html#route-paths").
277
+ * @param params Optional object with route path parameters.
397
278
  * Named parameters are passed by name (`params[name] = value`), unnamed
398
279
  * parameters are passed by index (`params[index] = value`).
399
280
  *
400
- * @return {string}
281
+ * @return
401
282
  */
402
- urlForPath(path: string, params?: Params | undefined): string;
283
+ urlForPath(path: string, params?: RouteParams): string;
284
+ /**
285
+ * Triggers navigation to a new path. Returns a boolean without waiting until
286
+ * the navigation is complete. Returns `true` if at least one `Router`
287
+ * has handled the navigation (was subscribed and had `baseUrl` matching
288
+ * the `path` argument), otherwise returns `false`.
289
+ *
290
+ * @param path
291
+ * a new in-app path string, or an URL-like object with `pathname`
292
+ * string property, and optional `search` and `hash` string properties.
293
+ * @return
294
+ */
295
+ static go(path: string | {
296
+ pathname: string;
297
+ search?: string;
298
+ hash?: string;
299
+ }): boolean;
403
300
  }