@breautek/router 1.0.2 → 2.0.1
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/CHANGELOG.md +28 -0
- package/dist/router.js +7761 -3865
- package/dist/src/HashStrategy.d.ts +9 -8
- package/dist/src/Route.d.ts +8 -5
- package/dist/src/RouteMatcher.d.ts +2 -2
- package/dist/src/Router.d.ts +9 -9
- package/dist/src/RouterStrategy.d.ts +3 -4
- package/dist/src/TransitionSlide.d.ts +5 -5
- package/dist/src/URLParser.d.ts +4 -4
- package/dist/src/URLStrategy.d.ts +7 -8
- package/dist/src/View.d.ts +2 -3
- package/package.json +39 -34
- package/src/HashStrategy.ts +44 -32
- package/src/Route.tsx +25 -9
- package/src/RouteMatcher.ts +5 -5
- package/src/Router.tsx +41 -41
- package/src/RouterStrategy.ts +5 -6
- package/src/TransitionSlide.ts +12 -12
- package/src/URLParser.ts +11 -11
- package/src/URLStrategy.ts +26 -27
- package/src/View.tsx +5 -6
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { RouterStrategy } from './RouterStrategy';
|
|
2
|
-
import { IDictionary } from '@totalpave/interfaces';
|
|
3
2
|
import { Router } from './Router';
|
|
4
3
|
/**
|
|
5
4
|
A {@link RouterStrategy} that manages a history stack using inline pound symbols `#`.
|
|
@@ -9,11 +8,12 @@ import { Router } from './Router';
|
|
|
9
8
|
`/#/mylink` will be produced.
|
|
10
9
|
*/
|
|
11
10
|
export declare class HashStrategy extends RouterStrategy {
|
|
12
|
-
private
|
|
13
|
-
private
|
|
14
|
-
private
|
|
11
|
+
private $base;
|
|
12
|
+
private $stack;
|
|
13
|
+
private $position;
|
|
14
|
+
private $lastFiredLocation;
|
|
15
15
|
constructor(router: Router);
|
|
16
|
-
private
|
|
16
|
+
private $init;
|
|
17
17
|
getLocation(): string;
|
|
18
18
|
getLocationAt(position: number): string;
|
|
19
19
|
getHistoryLength(): number;
|
|
@@ -21,8 +21,9 @@ export declare class HashStrategy extends RouterStrategy {
|
|
|
21
21
|
canGo(to: number): boolean;
|
|
22
22
|
peek(to: number): string;
|
|
23
23
|
go(to: number): void;
|
|
24
|
-
pushState(url: string, state?:
|
|
25
|
-
replaceState(url: string, state?:
|
|
24
|
+
pushState(url: string, state?: Record<any, any>): void;
|
|
25
|
+
replaceState(url: string, state?: Record<any, any>): void;
|
|
26
26
|
clear(): void;
|
|
27
|
-
private
|
|
27
|
+
private $navigate;
|
|
28
|
+
protected _fireURLChange(url: string): void;
|
|
28
29
|
}
|
package/dist/src/Route.d.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
+
import { View } from './View';
|
|
2
3
|
import { RouteMatcher } from './RouteMatcher';
|
|
3
4
|
import { RouterStrategy } from './RouterStrategy';
|
|
5
|
+
import { TransitionStrategy } from 'TransitionStrategy';
|
|
4
6
|
export interface IRouteProps<T> {
|
|
5
7
|
url: string;
|
|
6
8
|
component: React.ComponentClass<any>;
|
|
7
9
|
index?: boolean;
|
|
8
|
-
entryTransition?:
|
|
9
|
-
exitTransition?:
|
|
10
|
+
entryTransition?: TransitionStrategy;
|
|
11
|
+
exitTransition?: TransitionStrategy;
|
|
10
12
|
base?: string;
|
|
11
13
|
componentProps?: T;
|
|
12
14
|
matcher?: RouteMatcher;
|
|
@@ -22,9 +24,10 @@ export interface IRouteState {
|
|
|
22
24
|
* This class represents a route that renders a {@link View} component
|
|
23
25
|
*/
|
|
24
26
|
export declare class Route<TComponentProps extends IComponentProps = IComponentProps, TRouteProps extends IRouteProps<TComponentProps> = IRouteProps<TComponentProps>, TRouteState extends IRouteState = IRouteState> extends React.Component<TRouteProps, TRouteState> {
|
|
25
|
-
private
|
|
27
|
+
private $node;
|
|
26
28
|
constructor(props: TRouteProps);
|
|
27
29
|
render(): React.ReactNode;
|
|
28
|
-
|
|
29
|
-
private
|
|
30
|
+
getView(): View;
|
|
31
|
+
private $getComponentsToRender;
|
|
32
|
+
private $getChildren;
|
|
30
33
|
}
|
|
@@ -16,9 +16,9 @@ export interface IOnNoRouteFunction {
|
|
|
16
16
|
* based on the URL and the route url patterns.
|
|
17
17
|
*/
|
|
18
18
|
export declare class RouteMatcher {
|
|
19
|
-
private
|
|
19
|
+
private $strategy;
|
|
20
20
|
constructor(routerStrategy: RouterStrategy);
|
|
21
|
-
private
|
|
21
|
+
private $defaultNoRouteFunction;
|
|
22
22
|
/**
|
|
23
23
|
* Matches the url to the appropriate renderable route
|
|
24
24
|
*
|
package/dist/src/Router.d.ts
CHANGED
|
@@ -16,12 +16,12 @@ export interface IRouterState {
|
|
|
16
16
|
}
|
|
17
17
|
export declare class Router<TRouterProps extends IRouterProps = IRouterProps> extends React.Component<TRouterProps, IRouterState> {
|
|
18
18
|
state: IRouterState;
|
|
19
|
-
private
|
|
20
|
-
private
|
|
21
|
-
private
|
|
22
|
-
private
|
|
23
|
-
private
|
|
24
|
-
private static
|
|
19
|
+
private $lastRenderedRoute;
|
|
20
|
+
private $matcher;
|
|
21
|
+
private $awaitingTransition;
|
|
22
|
+
private $incomingRoute;
|
|
23
|
+
private $exitingRoute;
|
|
24
|
+
private static $instance;
|
|
25
25
|
constructor(props: TRouterProps);
|
|
26
26
|
static getInstance(): RouterStrategy;
|
|
27
27
|
/**
|
|
@@ -31,7 +31,7 @@ export declare class Router<TRouterProps extends IRouterProps = IRouterProps> ex
|
|
|
31
31
|
/**
|
|
32
32
|
* @ignore
|
|
33
33
|
*/
|
|
34
|
-
private
|
|
34
|
+
private $onURLChange;
|
|
35
35
|
componentDidMount(): void;
|
|
36
36
|
/**
|
|
37
37
|
* @ignore
|
|
@@ -77,10 +77,10 @@ export declare class Router<TRouterProps extends IRouterProps = IRouterProps> ex
|
|
|
77
77
|
/**
|
|
78
78
|
* Gets the potential routes
|
|
79
79
|
*/
|
|
80
|
-
private
|
|
80
|
+
private $getChildren;
|
|
81
81
|
/**
|
|
82
82
|
* Finds the index route. Returns null if there are no indexed routes.
|
|
83
83
|
*/
|
|
84
|
-
private
|
|
84
|
+
private $getIndexRoute;
|
|
85
85
|
}
|
|
86
86
|
export declare let getRouter: () => RouterStrategy;
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { EventEmitter } from 'events';
|
|
3
|
-
import { IDictionary } from '@totalpave/interfaces';
|
|
4
3
|
import { Router } from './Router';
|
|
5
4
|
export declare const EVENT_URL_CHANGE: string;
|
|
6
5
|
export declare type URLChangeCallback = (url: string) => void;
|
|
7
6
|
export declare abstract class RouterStrategy extends EventEmitter {
|
|
8
|
-
private
|
|
7
|
+
private $router;
|
|
9
8
|
constructor(router: Router);
|
|
10
9
|
/**
|
|
11
10
|
* Gets the router
|
|
@@ -98,14 +97,14 @@ export declare abstract class RouterStrategy extends EventEmitter {
|
|
|
98
97
|
* @param url
|
|
99
98
|
* @param state
|
|
100
99
|
*/
|
|
101
|
-
abstract pushState(url: string, state?:
|
|
100
|
+
abstract pushState(url: string, state?: Record<any, any>): void;
|
|
102
101
|
/**
|
|
103
102
|
* Replaces the current entry in the history stack with the new location.
|
|
104
103
|
* This will navigate the screen to the new location.
|
|
105
104
|
* @param url
|
|
106
105
|
* @param state
|
|
107
106
|
*/
|
|
108
|
-
abstract replaceState(url: string, state?:
|
|
107
|
+
abstract replaceState(url: string, state?: Record<any, any>): void;
|
|
109
108
|
/**
|
|
110
109
|
* Clears the history stack.
|
|
111
110
|
*/
|
|
@@ -7,11 +7,11 @@ export declare enum TransitionSlideDirection {
|
|
|
7
7
|
DOWN = 4
|
|
8
8
|
}
|
|
9
9
|
export declare class TransitionSlide extends TransitionStrategy {
|
|
10
|
-
private
|
|
11
|
-
private
|
|
12
|
-
private
|
|
10
|
+
private $slideDirection;
|
|
11
|
+
private $slideSpeed;
|
|
12
|
+
private $transitionTimeout;
|
|
13
13
|
constructor(slideDirection: TransitionSlideDirection, slideSpeed: number);
|
|
14
14
|
protected _execute(incoming: View, exiting: View): Promise<void>;
|
|
15
|
-
private
|
|
16
|
-
private
|
|
15
|
+
private $getTransitionString;
|
|
16
|
+
private $getSlideStyle;
|
|
17
17
|
}
|
package/dist/src/URLParser.d.ts
CHANGED
|
@@ -5,8 +5,8 @@ export interface IURLParams {
|
|
|
5
5
|
* Parses the URL for router paths and url-based variables.
|
|
6
6
|
*/
|
|
7
7
|
export declare class URLParser {
|
|
8
|
-
private
|
|
9
|
-
private
|
|
8
|
+
private $allowPartialMatch;
|
|
9
|
+
private $pattern;
|
|
10
10
|
/**
|
|
11
11
|
*
|
|
12
12
|
* @param {string} pattern The URL pattern
|
|
@@ -28,12 +28,12 @@ export declare class URLParser {
|
|
|
28
28
|
* @private
|
|
29
29
|
* @param {string} url URL to strip
|
|
30
30
|
*/
|
|
31
|
-
private
|
|
31
|
+
private $stripURL;
|
|
32
32
|
/**
|
|
33
33
|
* Splits the url into an array of URL parts, separated by the forward slash
|
|
34
34
|
*
|
|
35
35
|
* @private
|
|
36
36
|
* @param {string} url URL to split
|
|
37
37
|
*/
|
|
38
|
-
private
|
|
38
|
+
private $getParts;
|
|
39
39
|
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { RouterStrategy } from './RouterStrategy';
|
|
2
2
|
import { Router } from './Router';
|
|
3
|
-
import { IDictionary } from '@totalpave/interfaces';
|
|
4
3
|
/**
|
|
5
4
|
* @notice Using the URLStrategy requires some backend configuration
|
|
6
5
|
* to route URLs back to application.
|
|
@@ -10,15 +9,15 @@ import { IDictionary } from '@totalpave/interfaces';
|
|
|
10
9
|
* to the application vs other resources such as images.
|
|
11
10
|
*/
|
|
12
11
|
export declare class URLStrategy extends RouterStrategy {
|
|
13
|
-
private
|
|
14
|
-
private
|
|
15
|
-
private
|
|
12
|
+
private $base;
|
|
13
|
+
private $stack;
|
|
14
|
+
private $position;
|
|
16
15
|
/**
|
|
17
16
|
*
|
|
18
17
|
* @param {Router} router
|
|
19
18
|
*/
|
|
20
19
|
constructor(router: Router);
|
|
21
|
-
private
|
|
20
|
+
private $init;
|
|
22
21
|
getLocation(): string;
|
|
23
22
|
getLocationAt(position: number): string;
|
|
24
23
|
getHistoryLength(): number;
|
|
@@ -26,8 +25,8 @@ export declare class URLStrategy extends RouterStrategy {
|
|
|
26
25
|
peek(to: number): string;
|
|
27
26
|
canGo(to: number): boolean;
|
|
28
27
|
go(to: number): void;
|
|
29
|
-
pushState(url: string, state?:
|
|
30
|
-
replaceState(url: string, state?:
|
|
28
|
+
pushState(url: string, state?: Record<any, any>): void;
|
|
29
|
+
replaceState(url: string, state?: Record<any, any>): void;
|
|
31
30
|
clear(): void;
|
|
32
|
-
private
|
|
31
|
+
private $navigate;
|
|
33
32
|
}
|
package/dist/src/View.d.ts
CHANGED
|
@@ -2,7 +2,6 @@ import "core-js/stable";
|
|
|
2
2
|
import "regenerator-runtime/runtime";
|
|
3
3
|
import * as React from 'react';
|
|
4
4
|
import { RouterStrategy } from './RouterStrategy';
|
|
5
|
-
import { IDictionary } from '@totalpave/interfaces';
|
|
6
5
|
import { TransitionStrategy } from './TransitionStrategy';
|
|
7
6
|
import { IViewStylesheet } from './IViewStylesheet';
|
|
8
7
|
import "./View.scss";
|
|
@@ -12,7 +11,7 @@ export interface IViewProps {
|
|
|
12
11
|
exitTransition?: TransitionStrategy;
|
|
13
12
|
}
|
|
14
13
|
export declare abstract class View<TPageProps extends IViewProps = IViewProps> extends React.Component<TPageProps> {
|
|
15
|
-
private
|
|
14
|
+
private $node;
|
|
16
15
|
/**
|
|
17
16
|
* @param props See [IViewProps]
|
|
18
17
|
*/
|
|
@@ -45,7 +44,7 @@ export declare abstract class View<TPageProps extends IViewProps = IViewProps> e
|
|
|
45
44
|
* Get the inline styles for this view.
|
|
46
45
|
* Use React style notation.
|
|
47
46
|
*/
|
|
48
|
-
getViewStyles():
|
|
47
|
+
getViewStyles(): Record<any, string>;
|
|
49
48
|
protected abstract _renderView(): React.ReactNode;
|
|
50
49
|
render(): React.ReactNode;
|
|
51
50
|
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@breautek/router",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "An alternate react router.",
|
|
5
5
|
"main": "dist/router.js",
|
|
6
6
|
"types": "dist/src/api.d.ts",
|
|
7
7
|
"type": "commonjs",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"registry": "https://registry.npmjs.org"
|
|
10
|
+
},
|
|
8
11
|
"scripts": {
|
|
9
12
|
"test": "jest",
|
|
10
13
|
"lint": "eslint --ext .ts,.tsx '?(src|spec)/**/*.?(ts|tsx)' --cache",
|
|
@@ -35,49 +38,51 @@
|
|
|
35
38
|
"react-dom": ">=16.9.0 <17.0.0"
|
|
36
39
|
},
|
|
37
40
|
"devDependencies": {
|
|
38
|
-
"@babel/core": "7.
|
|
41
|
+
"@babel/core": "7.16.7",
|
|
39
42
|
"@babel/plugin-syntax-dynamic-import": "7.8.3",
|
|
40
|
-
"@babel/preset-env": "7.
|
|
41
|
-
"@babel/preset-react": "7.
|
|
42
|
-
"@babel/preset-typescript": "7.
|
|
43
|
-
"@babel/register": "7.
|
|
44
|
-
"@
|
|
45
|
-
"@
|
|
43
|
+
"@babel/preset-env": "7.16.7",
|
|
44
|
+
"@babel/preset-react": "7.16.7",
|
|
45
|
+
"@babel/preset-typescript": "7.16.7",
|
|
46
|
+
"@babel/register": "7.16.7",
|
|
47
|
+
"@rollup/plugin-babel": "5.3.0",
|
|
48
|
+
"@rollup/plugin-commonjs": "21.0.1",
|
|
49
|
+
"@rollup/plugin-json": "4.1.0",
|
|
50
|
+
"@rollup/plugin-node-resolve": "13.1.3",
|
|
51
|
+
"@totalpave/eslint-plugin": "5.1.0",
|
|
52
|
+
"@types/enzyme": "3.10.11",
|
|
46
53
|
"@types/enzyme-adapter-react-16": "1.0.6",
|
|
47
|
-
"@types/jest": "
|
|
48
|
-
"@
|
|
49
|
-
"@
|
|
50
|
-
"
|
|
54
|
+
"@types/jest": "27.4.0",
|
|
55
|
+
"@types/react": "16.14.4",
|
|
56
|
+
"@types/react-dom": "16.9.11",
|
|
57
|
+
"@types/react-test-renderer": "17.0.1",
|
|
58
|
+
"@typescript-eslint/eslint-plugin": "4.30.0",
|
|
59
|
+
"@typescript-eslint/parser": "4.30.0",
|
|
60
|
+
"ajv": "8.8.2",
|
|
51
61
|
"auto-changelog": "2.3.0",
|
|
52
|
-
"babel-jest": "
|
|
53
|
-
"core-js": "3.
|
|
62
|
+
"babel-jest": "27.4.6",
|
|
63
|
+
"core-js": "3.20.2",
|
|
54
64
|
"enzyme": "3.11.0",
|
|
55
65
|
"enzyme-adapter-react-16": "1.15.6",
|
|
56
|
-
"eslint": "7.
|
|
57
|
-
"eslint-plugin-react": "7.
|
|
58
|
-
"glob": "7.
|
|
66
|
+
"eslint": "7.32.0",
|
|
67
|
+
"eslint-plugin-react": "7.25.1",
|
|
68
|
+
"glob": "7.2.0",
|
|
59
69
|
"ignore-styles": "5.0.1",
|
|
60
|
-
"jest": "
|
|
61
|
-
"node-sass": "
|
|
70
|
+
"jest": "27.4.7",
|
|
71
|
+
"node-sass": "7.0.1",
|
|
62
72
|
"react": "16.14.0",
|
|
63
73
|
"react-dom": "16.14.0",
|
|
64
|
-
"regenerator-runtime": "0.13.
|
|
65
|
-
"rollup": "2.
|
|
66
|
-
"rollup-plugin-commonjs": "10.1.0",
|
|
67
|
-
"rollup-plugin-json": "4.0.0",
|
|
68
|
-
"rollup-plugin-node-resolve": "5.2.0",
|
|
74
|
+
"regenerator-runtime": "0.13.9",
|
|
75
|
+
"rollup": "2.63.0",
|
|
69
76
|
"rollup-plugin-progress": "1.1.2",
|
|
70
|
-
"rollup-plugin-sass": "1.2.
|
|
71
|
-
"rollup-plugin-typescript2": "0.
|
|
72
|
-
"ts-
|
|
73
|
-
"
|
|
74
|
-
"typedoc
|
|
75
|
-
"
|
|
77
|
+
"rollup-plugin-sass": "1.2.10",
|
|
78
|
+
"rollup-plugin-typescript2": "0.31.1",
|
|
79
|
+
"ts-jest": "27.1.2",
|
|
80
|
+
"ts-node": "10.4.0",
|
|
81
|
+
"typedoc": "0.21.9",
|
|
82
|
+
"typedoc-plugin-markdown": "3.10.4",
|
|
83
|
+
"typescript": "4.3.5"
|
|
76
84
|
},
|
|
77
85
|
"dependencies": {
|
|
78
|
-
"@
|
|
79
|
-
"@totalpave/interfaces": "1.0.0",
|
|
80
|
-
"@types/react": "16.14.4",
|
|
81
|
-
"@types/react-dom": "16.9.11"
|
|
86
|
+
"@totalpave/interfaces": "3.0.0"
|
|
82
87
|
}
|
|
83
88
|
}
|
package/src/HashStrategy.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
|
|
2
2
|
import {RouterStrategy} from './RouterStrategy';
|
|
3
|
-
import { IDictionary } from '@totalpave/interfaces';
|
|
4
3
|
import { Router } from './Router';
|
|
5
4
|
|
|
6
5
|
/**
|
|
@@ -11,28 +10,36 @@ import { Router } from './Router';
|
|
|
11
10
|
`/#/mylink` will be produced.
|
|
12
11
|
*/
|
|
13
12
|
export class HashStrategy extends RouterStrategy {
|
|
14
|
-
private
|
|
15
|
-
private
|
|
16
|
-
private
|
|
13
|
+
private $base: string;
|
|
14
|
+
private $stack: Array<string>;
|
|
15
|
+
private $position: number;
|
|
16
|
+
private $lastFiredLocation: string;
|
|
17
17
|
|
|
18
18
|
public constructor(router: Router) {
|
|
19
19
|
super(router);
|
|
20
|
-
this
|
|
21
|
-
this
|
|
22
|
-
this
|
|
20
|
+
this.$base = '#';
|
|
21
|
+
this.$stack = [];
|
|
22
|
+
this.$position = -1;
|
|
23
|
+
this.$lastFiredLocation = null; //this.getLocation();
|
|
23
24
|
|
|
24
25
|
window.addEventListener('popstate', (ev: PopStateEvent) => {
|
|
25
|
-
this.
|
|
26
|
+
let location = this.getLocation();
|
|
27
|
+
if (this.$lastFiredLocation !== location) {
|
|
28
|
+
this._fireURLChange(location);
|
|
29
|
+
}
|
|
26
30
|
});
|
|
27
31
|
|
|
28
32
|
window.addEventListener('hashchange', (e: HashChangeEvent) => {
|
|
29
|
-
this.
|
|
33
|
+
let location = this.getLocation();
|
|
34
|
+
if (this.$lastFiredLocation !== location) {
|
|
35
|
+
this._fireURLChange(location);
|
|
36
|
+
}
|
|
30
37
|
});
|
|
31
38
|
|
|
32
|
-
this
|
|
39
|
+
this.$init();
|
|
33
40
|
}
|
|
34
41
|
|
|
35
|
-
private
|
|
42
|
+
private $init(): void {
|
|
36
43
|
this.pushState(this.getLocation());
|
|
37
44
|
}
|
|
38
45
|
|
|
@@ -41,11 +48,11 @@ export class HashStrategy extends RouterStrategy {
|
|
|
41
48
|
}
|
|
42
49
|
|
|
43
50
|
public getLocationAt(position: number): string {
|
|
44
|
-
return this
|
|
51
|
+
return this.$stack[this.$position + position];
|
|
45
52
|
}
|
|
46
53
|
|
|
47
54
|
public getHistoryLength(): number {
|
|
48
|
-
return this.
|
|
55
|
+
return this.$stack.length;
|
|
49
56
|
}
|
|
50
57
|
|
|
51
58
|
public getScrollRestoration(): ScrollRestoration {
|
|
@@ -53,11 +60,11 @@ export class HashStrategy extends RouterStrategy {
|
|
|
53
60
|
}
|
|
54
61
|
|
|
55
62
|
public canGo(to: number): boolean {
|
|
56
|
-
return this
|
|
63
|
+
return this.$stack[this.$position + to] !== undefined;
|
|
57
64
|
}
|
|
58
65
|
|
|
59
66
|
public peek(to: number): string {
|
|
60
|
-
return this
|
|
67
|
+
return this.$stack[this.$position + to];
|
|
61
68
|
}
|
|
62
69
|
|
|
63
70
|
public go(to: number): void {
|
|
@@ -65,52 +72,57 @@ export class HashStrategy extends RouterStrategy {
|
|
|
65
72
|
return;
|
|
66
73
|
}
|
|
67
74
|
|
|
68
|
-
this
|
|
69
|
-
let url: string = this
|
|
75
|
+
this.$position += to;
|
|
76
|
+
let url: string = this.$stack[this.$position];
|
|
70
77
|
|
|
71
|
-
this
|
|
78
|
+
this.$navigate(url);
|
|
72
79
|
}
|
|
73
80
|
|
|
74
|
-
public pushState(url: string, state?:
|
|
81
|
+
public pushState(url: string, state?: Record<any, any>): void {
|
|
75
82
|
if (url === this.getLocation()) {
|
|
76
83
|
//We are already here, so do nothing.
|
|
77
84
|
return;
|
|
78
85
|
}
|
|
79
86
|
|
|
80
|
-
if (this.
|
|
81
|
-
this
|
|
87
|
+
if (this.$stack.length === 0) {
|
|
88
|
+
this.$stack[++this.$position] = this.getLocation();
|
|
82
89
|
}
|
|
83
90
|
|
|
84
|
-
this
|
|
91
|
+
this.$stack[++this.$position] = url;
|
|
85
92
|
|
|
86
93
|
//clear everything after position.
|
|
87
|
-
this
|
|
94
|
+
this.$stack = this.$stack.slice(0, this.$position + 1);
|
|
88
95
|
|
|
89
|
-
this
|
|
96
|
+
this.$navigate(url);
|
|
90
97
|
}
|
|
91
98
|
|
|
92
|
-
public replaceState(url: string, state?:
|
|
99
|
+
public replaceState(url: string, state?: Record<any, any>): void {
|
|
93
100
|
if (url === this.getLocation()) {
|
|
94
101
|
//We are already here, so do nothing.
|
|
95
102
|
return;
|
|
96
103
|
}
|
|
97
104
|
|
|
98
|
-
if (this
|
|
105
|
+
if (this.$position === -1) {
|
|
99
106
|
this.pushState(url, state);
|
|
100
107
|
}
|
|
101
108
|
else {
|
|
102
|
-
this
|
|
103
|
-
this
|
|
109
|
+
this.$stack[this.$position] = url;
|
|
110
|
+
this.$navigate(url);
|
|
104
111
|
}
|
|
105
112
|
}
|
|
106
113
|
|
|
107
114
|
public clear(): void {
|
|
108
|
-
this
|
|
109
|
-
this
|
|
115
|
+
this.$stack = [];
|
|
116
|
+
this.$position = -1;
|
|
110
117
|
}
|
|
111
118
|
|
|
112
|
-
private
|
|
113
|
-
window.location.hash = this
|
|
119
|
+
private $navigate(url: string): void {
|
|
120
|
+
window.location.hash = this.$base + url;
|
|
114
121
|
this._fireURLChange(this.getLocation());
|
|
115
122
|
}
|
|
123
|
+
|
|
124
|
+
protected _fireURLChange(url: string): void {
|
|
125
|
+
this.$lastFiredLocation = url;
|
|
126
|
+
super._fireURLChange(url);
|
|
127
|
+
}
|
|
116
128
|
}
|
package/src/Route.tsx
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
|
|
2
2
|
import * as React from 'react';
|
|
3
|
+
import { View } from './View';
|
|
3
4
|
import { RouteMatcher } from './RouteMatcher';
|
|
4
5
|
import { RouterStrategy } from './RouterStrategy';
|
|
6
|
+
import { TransitionStrategy } from 'TransitionStrategy';
|
|
5
7
|
|
|
6
8
|
export interface IRouteProps<T> {
|
|
7
9
|
url: string;
|
|
8
10
|
component: React.ComponentClass<any>;
|
|
9
11
|
index?: boolean;
|
|
10
|
-
entryTransition?:
|
|
11
|
-
exitTransition?:
|
|
12
|
+
entryTransition?: TransitionStrategy;
|
|
13
|
+
exitTransition?: TransitionStrategy;
|
|
12
14
|
|
|
13
15
|
base?: string;
|
|
14
16
|
componentProps?: T;
|
|
@@ -34,7 +36,7 @@ export interface IRouteState {
|
|
|
34
36
|
* This class represents a route that renders a {@link View} component
|
|
35
37
|
*/
|
|
36
38
|
export class Route<TComponentProps extends IComponentProps = IComponentProps, TRouteProps extends IRouteProps<TComponentProps> = IRouteProps<TComponentProps>, TRouteState extends IRouteState = IRouteState> extends React.Component<TRouteProps, TRouteState> {
|
|
37
|
-
private
|
|
39
|
+
private $node: View;
|
|
38
40
|
|
|
39
41
|
constructor(props: TRouteProps) {
|
|
40
42
|
super(props);
|
|
@@ -42,10 +44,14 @@ export class Route<TComponentProps extends IComponentProps = IComponentProps, TR
|
|
|
42
44
|
}
|
|
43
45
|
|
|
44
46
|
public render(): React.ReactNode {
|
|
45
|
-
return this
|
|
47
|
+
return this.$getComponentsToRender(this);
|
|
46
48
|
}
|
|
47
49
|
|
|
48
|
-
|
|
50
|
+
public getView(): View {
|
|
51
|
+
return this.$node;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private $getComponentsToRender(component: React.ReactElement | React.Component): React.ReactNode {
|
|
49
55
|
let url: string = component.props.url;
|
|
50
56
|
let base: string = component.props.base || '';
|
|
51
57
|
|
|
@@ -53,16 +59,26 @@ export class Route<TComponentProps extends IComponentProps = IComponentProps, TR
|
|
|
53
59
|
let ViewComponent: React.ElementType = component.props.component;
|
|
54
60
|
let child: React.ReactNode;
|
|
55
61
|
|
|
56
|
-
let routeComponent = component.props.matcher.match(url, this
|
|
62
|
+
let routeComponent = component.props.matcher.match(url, this.$getChildren(component), base);
|
|
57
63
|
if (routeComponent) {
|
|
58
|
-
child = this
|
|
64
|
+
child = this.$getComponentsToRender(routeComponent);
|
|
59
65
|
}
|
|
60
66
|
|
|
61
67
|
return (
|
|
62
68
|
<ViewComponent
|
|
63
69
|
{...component.props.componentProps}
|
|
64
70
|
ref={(node: React.Component) => {
|
|
65
|
-
|
|
71
|
+
if (node) {
|
|
72
|
+
if (node instanceof View) {
|
|
73
|
+
this.$node = node;
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
throw new Error('Routed components should be a View, but got ' + Object.getPrototypeOf(node).constructor.name + ' instead.');
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
this.$node = null;
|
|
81
|
+
}
|
|
66
82
|
}}
|
|
67
83
|
>
|
|
68
84
|
{child}
|
|
@@ -70,7 +86,7 @@ export class Route<TComponentProps extends IComponentProps = IComponentProps, TR
|
|
|
70
86
|
);
|
|
71
87
|
}
|
|
72
88
|
|
|
73
|
-
private
|
|
89
|
+
private $getChildren(component: React.Component | React.ReactElement): React.ReactElement[] {
|
|
74
90
|
let children: React.ReactElement[] = null;
|
|
75
91
|
|
|
76
92
|
if (!component) {
|
package/src/RouteMatcher.ts
CHANGED
|
@@ -21,13 +21,13 @@ export interface IOnNoRouteFunction {
|
|
|
21
21
|
* based on the URL and the route url patterns.
|
|
22
22
|
*/
|
|
23
23
|
export class RouteMatcher {
|
|
24
|
-
private
|
|
24
|
+
private $strategy: RouterStrategy;
|
|
25
25
|
|
|
26
26
|
public constructor(routerStrategy: RouterStrategy) {
|
|
27
|
-
this
|
|
27
|
+
this.$strategy = routerStrategy;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
private
|
|
30
|
+
private $defaultNoRouteFunction(indexRoute: React.ReactElement, routes: Array<React.ReactElement>): React.ReactElement {
|
|
31
31
|
return indexRoute;
|
|
32
32
|
}
|
|
33
33
|
|
|
@@ -55,7 +55,7 @@ export class RouteMatcher {
|
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
if (!componentToRender) {
|
|
58
|
-
componentToRender = (onNoRoute ? onNoRoute : this
|
|
58
|
+
componentToRender = (onNoRoute ? onNoRoute : this.$defaultNoRouteFunction)(indexRoute, children);
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
if (!componentToRender) {
|
|
@@ -74,7 +74,7 @@ export class RouteMatcher {
|
|
|
74
74
|
exitTransition: componentToRender.props.exitTransition,
|
|
75
75
|
componentProps: {
|
|
76
76
|
url : url,
|
|
77
|
-
router: this
|
|
77
|
+
router: this.$strategy
|
|
78
78
|
}
|
|
79
79
|
};
|
|
80
80
|
|