@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/src/Router.tsx CHANGED
@@ -8,8 +8,8 @@ import {DefaultStrategy} from './DefaultStrategy';
8
8
  import {RouteMatcher} from './RouteMatcher';
9
9
  import { RouterStrategy } from './RouterStrategy';
10
10
  import { IRouterStrategyClass } from './IRouterStrategyClass';
11
- import {View} from './View';
12
11
  import {IOnNoRoute} from './IOnNoRoute';
12
+ import { Route } from "./Route";
13
13
 
14
14
  export interface IRouterProps {
15
15
  strategy?: IRouterStrategyClass;
@@ -26,13 +26,13 @@ export interface IRouterState {
26
26
  export class Router<TRouterProps extends IRouterProps = IRouterProps> extends React.Component<TRouterProps, IRouterState> {
27
27
  public state: IRouterState;
28
28
 
29
- private _lastRenderedRoute: any;
30
- private _matcher: RouteMatcher;
31
- private _awaitingTransition: boolean;
32
- private _incomingNode: View;
33
- private _exitingNode: View;
29
+ private $lastRenderedRoute: any;
30
+ private $matcher: RouteMatcher;
31
+ private $awaitingTransition: boolean;
32
+ private $incomingRoute: Route;
33
+ private $exitingRoute: Route;
34
34
 
35
- private static _instance: Router;
35
+ private static $instance: Router;
36
36
 
37
37
  public constructor(props: TRouterProps) {
38
38
  super(props);
@@ -54,17 +54,17 @@ export class Router<TRouterProps extends IRouterProps = IRouterProps> extends Re
54
54
  shouldTransition: false
55
55
  };
56
56
 
57
- this._lastRenderedRoute = null;
58
- this._onURLChange = this._onURLChange.bind(this);
59
- this._matcher = new RouteMatcher(strategy);
57
+ this.$lastRenderedRoute = null;
58
+ this.$onURLChange = this.$onURLChange.bind(this);
59
+ this.$matcher = new RouteMatcher(strategy);
60
60
  }
61
61
 
62
62
  public static getInstance(): RouterStrategy {
63
- if (!Router._instance) {
63
+ if (!Router.$instance) {
64
64
  return null;
65
65
  }
66
66
 
67
- return Router._instance.getRouterStrategy();
67
+ return Router.$instance.getRouterStrategy();
68
68
  }
69
69
 
70
70
  /**
@@ -77,7 +77,7 @@ export class Router<TRouterProps extends IRouterProps = IRouterProps> extends Re
77
77
  /**
78
78
  * @ignore
79
79
  */
80
- private _onURLChange(url: string): void {
80
+ private $onURLChange(url: string): void {
81
81
  if (url !== this.state.url) {
82
82
  this.setState({
83
83
  url: url,
@@ -87,8 +87,8 @@ export class Router<TRouterProps extends IRouterProps = IRouterProps> extends Re
87
87
  }
88
88
 
89
89
  public componentDidMount(): void {
90
- Router._instance = this;
91
- this.state.strategy.addURLChangeCallback(this._onURLChange);
90
+ Router.$instance = this;
91
+ this.state.strategy.addURLChangeCallback(this.$onURLChange);
92
92
  }
93
93
 
94
94
  /**
@@ -96,9 +96,9 @@ export class Router<TRouterProps extends IRouterProps = IRouterProps> extends Re
96
96
  */
97
97
  public UNSAFE_componentWillReceiveProps(nextProps: TRouterProps): void {
98
98
  if (nextProps.strategy && (this.state.strategy instanceof nextProps.strategy)) {
99
- this.state.strategy.removeURLChangeCallback(this._onURLChange);
99
+ this.state.strategy.removeURLChangeCallback(this.$onURLChange);
100
100
  let strat: RouterStrategy = new nextProps.strategy(this);
101
- strat.addURLChangeCallback(this._onURLChange);
101
+ strat.addURLChangeCallback(this.$onURLChange);
102
102
  this.setState({
103
103
  strategy: strat
104
104
  });
@@ -109,15 +109,15 @@ export class Router<TRouterProps extends IRouterProps = IRouterProps> extends Re
109
109
  * @ignore
110
110
  */
111
111
  public componentWillUnmount(): void {
112
- Router._instance = null;
113
- this.state.strategy.removeURLChangeCallback(this._onURLChange);
112
+ Router.$instance = null;
113
+ this.state.strategy.removeURLChangeCallback(this.$onURLChange);
114
114
  }
115
115
 
116
116
  /**
117
117
  * @ignore
118
118
  */
119
119
  public render(): React.ReactNode {
120
- let currentRoute: React.ReactElement = this._matcher.match(this.state.url || '/', this._getChildren(), '', this._getIndexRoute(), this.props.onNoRoute);
120
+ let currentRoute: React.ReactElement = this.$matcher.match(this.state.url || '/', this.$getChildren(), '', this.$getIndexRoute(), this.props.onNoRoute);
121
121
  // eslint-disable-next-line @typescript-eslint/naming-convention
122
122
  let Root: React.ElementType = null;
123
123
  if (this.props.component) {
@@ -127,25 +127,25 @@ export class Router<TRouterProps extends IRouterProps = IRouterProps> extends Re
127
127
  if (
128
128
  this.state.shouldTransition && (
129
129
  currentRoute.props.entryTransition || (
130
- this._lastRenderedRoute &&
131
- this._lastRenderedRoute.props.exitTransition
130
+ this.$lastRenderedRoute &&
131
+ this.$lastRenderedRoute.props.exitTransition
132
132
  )
133
133
  )
134
134
  ) {
135
- this._awaitingTransition = true;
135
+ this.$awaitingTransition = true;
136
136
  let exiting: React.ReactElement = null;
137
- if (this._lastRenderedRoute) {
138
- exiting = React.cloneElement(this._lastRenderedRoute, {
139
- ref : (node: View) => {
140
- this._exitingNode = node;
137
+ if (this.$lastRenderedRoute) {
138
+ exiting = React.cloneElement(this.$lastRenderedRoute, {
139
+ ref : (route: Route) => {
140
+ this.$exitingRoute = route;
141
141
  }
142
142
  });
143
143
  }
144
144
 
145
145
  // Incoming will always be safe to render, hence no defensive checks
146
146
  let incoming: React.ReactElement = React.cloneElement(currentRoute, {
147
- ref : (node: View) => {
148
- this._incomingNode = node;
147
+ ref : (route: Route) => {
148
+ this.$incomingRoute = route;
149
149
  }
150
150
  });
151
151
 
@@ -157,7 +157,7 @@ export class Router<TRouterProps extends IRouterProps = IRouterProps> extends Re
157
157
  }
158
158
  }
159
159
  else {
160
- this._lastRenderedRoute = currentRoute;
160
+ this.$lastRenderedRoute = currentRoute;
161
161
 
162
162
  if (Root) {
163
163
  // currentRoute must be rendered as an array; because, exiting and incoming is rendered as an array.
@@ -175,11 +175,11 @@ export class Router<TRouterProps extends IRouterProps = IRouterProps> extends Re
175
175
  * @ignore
176
176
  */
177
177
  public componentDidUpdate(): void {
178
- if (this._awaitingTransition) {
179
- this._awaitingTransition = false;
178
+ if (this.$awaitingTransition) {
179
+ this.$awaitingTransition = false;
180
180
  let exitTransitionPromise: Promise<void> = null;
181
- if (this._exitingNode && this._exitingNode.props.exitTransition) {
182
- exitTransitionPromise = this._exitingNode.props.exitTransition.execute(this._incomingNode, this._exitingNode);
181
+ if (this.$exitingRoute && this.$exitingRoute.props.exitTransition) {
182
+ exitTransitionPromise = this.$exitingRoute.props.exitTransition.execute(this.$incomingRoute.getView(), this.$exitingRoute.getView());
183
183
  }
184
184
  else {
185
185
  exitTransitionPromise = Promise.resolve();
@@ -187,8 +187,8 @@ export class Router<TRouterProps extends IRouterProps = IRouterProps> extends Re
187
187
 
188
188
  exitTransitionPromise.then(() => {
189
189
  let entryTransitionPromise = null;
190
- if (this._incomingNode.props.entryTransition) {
191
- entryTransitionPromise = this._incomingNode.props.entryTransition.execute(this._incomingNode, this._exitingNode);
190
+ if (this.$incomingRoute.props.entryTransition) {
191
+ entryTransitionPromise = this.$incomingRoute.props.entryTransition.execute(this.$incomingRoute.getView(), this.$exitingRoute.getView());
192
192
  }
193
193
  else {
194
194
  entryTransitionPromise = Promise.resolve();
@@ -197,8 +197,8 @@ export class Router<TRouterProps extends IRouterProps = IRouterProps> extends Re
197
197
  }).catch((error: Error) => {
198
198
  console.error(error);
199
199
  }).then(() => {
200
- this._incomingNode = null;
201
- this._exitingNode = null;
200
+ this.$incomingRoute = null;
201
+ this.$exitingRoute = null;
202
202
  this.setState({shouldTransition: false});
203
203
  });
204
204
  }
@@ -247,7 +247,7 @@ export class Router<TRouterProps extends IRouterProps = IRouterProps> extends Re
247
247
  /**
248
248
  * Gets the potential routes
249
249
  */
250
- private _getChildren(): React.ReactElement[] {
250
+ private $getChildren(): React.ReactElement[] {
251
251
  let children: React.ReactElement[] = null;
252
252
 
253
253
  if (this.props.children instanceof Array) {
@@ -263,8 +263,8 @@ export class Router<TRouterProps extends IRouterProps = IRouterProps> extends Re
263
263
  /**
264
264
  * Finds the index route. Returns null if there are no indexed routes.
265
265
  */
266
- private _getIndexRoute(): React.ReactElement {
267
- let children: React.ReactElement[] = this._getChildren();
266
+ private $getIndexRoute(): React.ReactElement {
267
+ let children: React.ReactElement[] = this.$getChildren();
268
268
  for (let i: number = 0; i < children.length; i++) {
269
269
  let child: React.ReactElement = children[i];
270
270
  if (child.props.index) {
@@ -1,5 +1,4 @@
1
1
  import { EventEmitter } from 'events';
2
- import {IDictionary} from '@totalpave/interfaces';
3
2
  import { Router } from './Router';
4
3
 
5
4
  export const EVENT_URL_CHANGE: string = 'urlchange';
@@ -7,18 +6,18 @@ export const EVENT_URL_CHANGE: string = 'urlchange';
7
6
  export type URLChangeCallback = (url: string) => void;
8
7
 
9
8
  export abstract class RouterStrategy extends EventEmitter {
10
- private _router: Router;
9
+ private $router: Router;
11
10
 
12
11
  public constructor(router: Router) {
13
12
  super();
14
- this._router = router;
13
+ this.$router = router;
15
14
  }
16
15
 
17
16
  /**
18
17
  * Gets the router
19
18
  */
20
19
  public getRouter(): Router {
21
- return this._router;
20
+ return this.$router;
22
21
  }
23
22
 
24
23
  /**
@@ -153,7 +152,7 @@ export abstract class RouterStrategy extends EventEmitter {
153
152
  * @param url
154
153
  * @param state
155
154
  */
156
- public abstract pushState(url: string, state?: IDictionary): void;
155
+ public abstract pushState(url: string, state?: Record<any, any>): void;
157
156
 
158
157
  /**
159
158
  * Replaces the current entry in the history stack with the new location.
@@ -161,7 +160,7 @@ export abstract class RouterStrategy extends EventEmitter {
161
160
  * @param url
162
161
  * @param state
163
162
  */
164
- public abstract replaceState(url: string, state?: IDictionary): void;
163
+ public abstract replaceState(url: string, state?: Record<any, any>): void;
165
164
 
166
165
  /**
167
166
  * Clears the history stack.
@@ -10,15 +10,15 @@ export enum TransitionSlideDirection {
10
10
  }
11
11
 
12
12
  export class TransitionSlide extends TransitionStrategy {
13
- private _slideDirection: TransitionSlideDirection;
14
- private _slideSpeed: number;
15
- private _transitionTimeout: number;
13
+ private $slideDirection: TransitionSlideDirection;
14
+ private $slideSpeed: number;
15
+ private $transitionTimeout: number;
16
16
 
17
17
  public constructor(slideDirection: TransitionSlideDirection, slideSpeed: number) {
18
18
  super();
19
19
 
20
- this._slideDirection = slideDirection || TransitionSlideDirection.LEFT;
21
- this._slideSpeed = slideSpeed || 0.25;
20
+ this.$slideDirection = slideDirection || TransitionSlideDirection.LEFT;
21
+ this.$slideSpeed = slideSpeed || 0.25;
22
22
  }
23
23
 
24
24
  protected _execute(incoming: View, exiting: View): Promise<void> {
@@ -36,7 +36,7 @@ export class TransitionSlide extends TransitionStrategy {
36
36
  exitingNode.style.pointerEvents = 'none';
37
37
  exitingNode.style.zIndex = '2';
38
38
 
39
- switch (this._slideDirection) {
39
+ switch (this.$slideDirection) {
40
40
  case TransitionSlideDirection.LEFT:
41
41
  incomingNode.style.left = '100%';
42
42
  exitingNode.style.left = '0%';
@@ -56,7 +56,7 @@ export class TransitionSlide extends TransitionStrategy {
56
56
  }
57
57
 
58
58
  //Set the appropriate transition string
59
- let transitionString = this._getTransitionString(this._slideDirection);
59
+ let transitionString = this.$getTransitionString(this.$slideDirection);
60
60
  incomingNode.style.transition = transitionString;
61
61
  exitingNode.style.transition = transitionString;
62
62
 
@@ -88,7 +88,7 @@ export class TransitionSlide extends TransitionStrategy {
88
88
 
89
89
  //Apply transition logic
90
90
  setTimeout(() => {
91
- switch (this._slideDirection) {
91
+ switch (this.$slideDirection) {
92
92
  case TransitionSlideDirection.LEFT:
93
93
  incomingNode.style.left = '0%';
94
94
  exitingNode.style.left = '-100%';
@@ -110,9 +110,9 @@ export class TransitionSlide extends TransitionStrategy {
110
110
  });
111
111
  }
112
112
 
113
- private _getTransitionString(direction: TransitionSlideDirection): string {
113
+ private $getTransitionString(direction: TransitionSlideDirection): string {
114
114
  let dir: string = null;
115
- let speed: string = `${this._slideSpeed}s`;
115
+ let speed: string = `${this.$slideSpeed}s`;
116
116
  switch (direction) {
117
117
  case TransitionSlideDirection.LEFT:
118
118
  case TransitionSlideDirection.RIGHT:
@@ -124,10 +124,10 @@ export class TransitionSlide extends TransitionStrategy {
124
124
  break;
125
125
  }
126
126
 
127
- return `${dir} ${speed} ${this._getSlideStyle()}`
127
+ return `${dir} ${speed} ${this.$getSlideStyle()}`
128
128
  }
129
129
 
130
- private _getSlideStyle(): string {
130
+ private $getSlideStyle(): string {
131
131
  return 'ease-in-out';
132
132
  }
133
133
  }
package/src/URLParser.ts CHANGED
@@ -7,8 +7,8 @@ export interface IURLParams {
7
7
  * Parses the URL for router paths and url-based variables.
8
8
  */
9
9
  export class URLParser {
10
- private _allowPartialMatch: boolean;
11
- private _pattern: string;
10
+ private $allowPartialMatch: boolean;
11
+ private $pattern: string;
12
12
 
13
13
  /**
14
14
  *
@@ -18,8 +18,8 @@ export class URLParser {
18
18
  * Defaults to false.
19
19
  */
20
20
  public constructor(pattern: string, allowPartialMatch: boolean = false) {
21
- this._allowPartialMatch = allowPartialMatch;
22
- this._pattern = this._stripURL(pattern);
21
+ this.$allowPartialMatch = allowPartialMatch;
22
+ this.$pattern = this.$stripURL(pattern);
23
23
  }
24
24
 
25
25
  /**
@@ -29,11 +29,11 @@ export class URLParser {
29
29
  * @param {string} url The url to test
30
30
  */
31
31
  public parse(url: string): IURLParams {
32
- url = this._stripURL(url);
33
- let parts: Array<string> = this._getParts(url);
34
- let patternParts: Array<string> = this._getParts(this._pattern);
32
+ url = this.$stripURL(url);
33
+ let parts: Array<string> = this.$getParts(url);
34
+ let patternParts: Array<string> = this.$getParts(this.$pattern);
35
35
 
36
- if ((!this._allowPartialMatch && parts.length !== patternParts.length) || url === '') {
36
+ if ((!this.$allowPartialMatch && parts.length !== patternParts.length) || url === '') {
37
37
  return null;
38
38
  }
39
39
 
@@ -66,7 +66,7 @@ export class URLParser {
66
66
  * @private
67
67
  * @param {string} url URL to strip
68
68
  */
69
- private _stripURL(url: string): string {
69
+ private $stripURL(url: string): string {
70
70
  while (url.charAt(0) === '/') {
71
71
  url = url.slice(1);
72
72
  }
@@ -84,8 +84,8 @@ export class URLParser {
84
84
  * @private
85
85
  * @param {string} url URL to split
86
86
  */
87
- private _getParts(url: string): Array<string> {
88
- url = this._stripURL(url);
87
+ private $getParts(url: string): Array<string> {
88
+ url = this.$stripURL(url);
89
89
  return url.split('/');
90
90
  }
91
91
  }
@@ -1,7 +1,6 @@
1
1
 
2
2
  import {RouterStrategy} from './RouterStrategy';
3
3
  import { Router } from './Router';
4
- import { IDictionary } from '@totalpave/interfaces';
5
4
 
6
5
  /**
7
6
  * @notice Using the URLStrategy requires some backend configuration
@@ -12,9 +11,9 @@ import { IDictionary } from '@totalpave/interfaces';
12
11
  * to the application vs other resources such as images.
13
12
  */
14
13
  export class URLStrategy extends RouterStrategy {
15
- private _base: string;
16
- private _stack: Array<string>;
17
- private _position: number;
14
+ private $base: string;
15
+ private $stack: Array<string>;
16
+ private $position: number;
18
17
 
19
18
  /**
20
19
  *
@@ -23,18 +22,18 @@ export class URLStrategy extends RouterStrategy {
23
22
  public constructor(router: Router) {
24
23
  super(router);
25
24
 
26
- this._base = window.location.origin + '/r'
27
- this._stack = [];
28
- this._position = -1;
25
+ this.$base = window.location.origin + '/r'
26
+ this.$stack = [];
27
+ this.$position = -1;
29
28
 
30
29
  window.addEventListener('popstate', () => {
31
30
  this._fireURLChange(this.getLocation());
32
31
  });
33
32
 
34
- this._init();
33
+ this.$init();
35
34
  }
36
35
 
37
- private _init(): void {
36
+ private $init(): void {
38
37
  this.pushState(this.getLocation());
39
38
  }
40
39
 
@@ -43,7 +42,7 @@ export class URLStrategy extends RouterStrategy {
43
42
  }
44
43
 
45
44
  public getLocationAt(position: number): string {
46
- return this._stack[this._position + position];
45
+ return this.$stack[this.$position + position];
47
46
  }
48
47
 
49
48
  public getHistoryLength(): number {
@@ -55,11 +54,11 @@ export class URLStrategy extends RouterStrategy {
55
54
  }
56
55
 
57
56
  public peek(to: number): string {
58
- return this._stack[this._position + to];
57
+ return this.$stack[this.$position + to];
59
58
  }
60
59
 
61
60
  public canGo(to: number): boolean {
62
- return this._stack[this._position + to] !== undefined;
61
+ return this.$stack[this.$position + to] !== undefined;
63
62
  }
64
63
 
65
64
  public go(to: number): void {
@@ -67,11 +66,11 @@ export class URLStrategy extends RouterStrategy {
67
66
  return;
68
67
  }
69
68
 
70
- this._position += to;
71
- this._navigate(this._stack[this._position]);
69
+ this.$position += to;
70
+ this.$navigate(this.$stack[this.$position]);
72
71
  }
73
72
 
74
- public pushState(url: string, state?: IDictionary): void {
73
+ public pushState(url: string, state?: Record<any, any>): void {
75
74
  if (state) {
76
75
  console.warn('Warning: The state parameter is not implemented yet.');
77
76
  }
@@ -81,15 +80,15 @@ export class URLStrategy extends RouterStrategy {
81
80
  return;
82
81
  }
83
82
 
84
- this._stack[++this._position] = url;
83
+ this.$stack[++this.$position] = url;
85
84
 
86
85
  //clear everything after position.
87
- this._stack = this._stack.slice(0, this._position + 1);
86
+ this.$stack = this.$stack.slice(0, this.$position + 1);
88
87
 
89
- this._navigate(url);
88
+ this.$navigate(url);
90
89
  }
91
90
 
92
- public replaceState(url: string, state?: IDictionary): void {
91
+ public replaceState(url: string, state?: Record<any, any>): void {
93
92
  if (state) {
94
93
  console.warn('Warning: The state parameter is not implemented yet.');
95
94
  }
@@ -99,26 +98,26 @@ export class URLStrategy extends RouterStrategy {
99
98
  return;
100
99
  }
101
100
 
102
- if (this._position === -1) {
101
+ if (this.$position === -1) {
103
102
  this.pushState(url, state);
104
103
  }
105
104
  else {
106
- this._stack[this._position] = url;
107
- this._navigate(url);
105
+ this.$stack[this.$position] = url;
106
+ this.$navigate(url);
108
107
  }
109
108
  }
110
109
 
111
110
  public clear(): void {
112
- this._stack = [];
113
- this._position = -1;
111
+ this.$stack = [];
112
+ this.$position = -1;
114
113
  }
115
114
 
116
- private _navigate(url: string, replace: boolean = false): void {
115
+ private $navigate(url: string, replace: boolean = false): void {
117
116
  if (replace) {
118
- window.history.replaceState({}, null, this._base + url);
117
+ window.history.replaceState({}, null, this.$base + url);
119
118
  }
120
119
  else {
121
- window.history.pushState({}, null, this._base + url);
120
+ window.history.pushState({}, null, this.$base + url);
122
121
  }
123
122
  this._fireURLChange(this.getLocation());
124
123
  }
package/src/View.tsx CHANGED
@@ -5,7 +5,6 @@ import "regenerator-runtime/runtime";
5
5
  import * as React from 'react';
6
6
 
7
7
  import { RouterStrategy } from './RouterStrategy';
8
- import { IDictionary } from '@totalpave/interfaces';
9
8
  import { TransitionStrategy } from './TransitionStrategy';
10
9
  import { IViewStylesheet } from './IViewStylesheet';
11
10
 
@@ -18,14 +17,14 @@ export interface IViewProps {
18
17
  }
19
18
 
20
19
  export abstract class View<TPageProps extends IViewProps = IViewProps> extends React.Component<TPageProps> {
21
- private _node: HTMLDivElement;
20
+ private $node: HTMLDivElement;
22
21
 
23
22
  /**
24
23
  * @param props See [IViewProps]
25
24
  */
26
25
  public constructor(props: TPageProps) {
27
26
  super(props);
28
- this._node = null;
27
+ this.$node = null;
29
28
  }
30
29
 
31
30
  /**
@@ -70,7 +69,7 @@ export abstract class View<TPageProps extends IViewProps = IViewProps> extends R
70
69
  * Gets the underlying HTML node for this View
71
70
  */
72
71
  public getNode(): HTMLElement {
73
- return this._node;
72
+ return this.$node;
74
73
  }
75
74
 
76
75
  /**
@@ -84,7 +83,7 @@ export abstract class View<TPageProps extends IViewProps = IViewProps> extends R
84
83
  * Get the inline styles for this view.
85
84
  * Use React style notation.
86
85
  */
87
- public getViewStyles(): IDictionary<string> {
86
+ public getViewStyles(): Record<any, string> {
88
87
  return {};
89
88
  }
90
89
 
@@ -95,7 +94,7 @@ export abstract class View<TPageProps extends IViewProps = IViewProps> extends R
95
94
  return (
96
95
  <div
97
96
  className={`View${cssClass ? ' ' + cssClass : ''}`}
98
- ref={(n) => { this._node = n; }}
97
+ ref={(n) => { this.$node = n; }}
99
98
  style={this.getViewStyles()}
100
99
  >
101
100
  {this._renderView()}