@24i/bigscreen-sdk 1.0.19-alpha.2340 → 1.0.19-alpha.2343
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/package.json +2 -2
- package/packages/router/src/Route.tsx +10 -27
- package/packages/router/src/Router.tsx +59 -13
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@24i/bigscreen-sdk",
|
|
3
|
-
"version": "1.0.19-alpha.
|
|
3
|
+
"version": "1.0.19-alpha.2343",
|
|
4
4
|
"description": "SmartApps BIGscreen SDK monorepo",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
"@24i/bigscreen-sdk": "file:.",
|
|
56
56
|
"@24i/eslint-config-smartapps-bigscreen-linters": "file:./libs/linters",
|
|
57
57
|
"@24i/smartapps-bigscreen-changelog-generator": "0.17.0",
|
|
58
|
-
"@babel/core": "^7.
|
|
58
|
+
"@babel/core": "^7.22.1",
|
|
59
59
|
"@babel/preset-env": "^7.22.4",
|
|
60
60
|
"@types/fs-extra": "^11.0.1",
|
|
61
61
|
"@types/inquirer": "^9.0.3",
|
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
Component,
|
|
4
4
|
createRef,
|
|
5
5
|
detachNode,
|
|
6
|
+
Reference,
|
|
6
7
|
replaceNode,
|
|
7
8
|
} from '@24i/bigscreen-sdk/jsx';
|
|
8
9
|
import { filter, forEach } from '@24i/bigscreen-sdk/perf-utils/array';
|
|
@@ -13,9 +14,9 @@ import { IRoute, RouteParams } from './types';
|
|
|
13
14
|
|
|
14
15
|
type Props = {
|
|
15
16
|
path: string,
|
|
17
|
+
domParent?: Reference<HTMLElement>;
|
|
16
18
|
regexpPath?: RegExp,
|
|
17
19
|
paramNames?: string[],
|
|
18
|
-
isDetachable?: boolean;
|
|
19
20
|
Component: Function,
|
|
20
21
|
};
|
|
21
22
|
|
|
@@ -26,18 +27,10 @@ export class Route extends Component<Props> {
|
|
|
26
27
|
|
|
27
28
|
isDetached = false;
|
|
28
29
|
|
|
29
|
-
domParent: HTMLElement | null = null;
|
|
30
|
-
|
|
31
30
|
domRef = createRef<HTMLDivElement>();
|
|
32
31
|
|
|
33
32
|
component = createRef<IRoute | null>();
|
|
34
33
|
|
|
35
|
-
componentDidMount() {
|
|
36
|
-
const { isDetachable } = this.props;
|
|
37
|
-
this.domParent = this.domRef.current?.parentElement!;
|
|
38
|
-
if (isDetachable) this.detach();
|
|
39
|
-
}
|
|
40
|
-
|
|
41
34
|
getParams(): RouteParams {
|
|
42
35
|
const { regexpPath, paramNames } = this.props;
|
|
43
36
|
if (!paramNames || !regexpPath) return {};
|
|
@@ -63,7 +56,6 @@ export class Route extends Component<Props> {
|
|
|
63
56
|
|
|
64
57
|
show() {
|
|
65
58
|
menuService.addEventListener('visibilitychange', this.onMenuVisibilityChange);
|
|
66
|
-
this.showRoute();
|
|
67
59
|
if (!this.wasCreated) {
|
|
68
60
|
this.mountRoute();
|
|
69
61
|
this.component.current?.activate(this.getParams());
|
|
@@ -74,10 +66,7 @@ export class Route extends Component<Props> {
|
|
|
74
66
|
|
|
75
67
|
hide() {
|
|
76
68
|
menuService.removeEventListener('visibilitychange', this.onMenuVisibilityChange);
|
|
77
|
-
|
|
78
|
-
this.component.current?.deactivate();
|
|
79
|
-
this.hideRoute();
|
|
80
|
-
}
|
|
69
|
+
this.deactivate();
|
|
81
70
|
}
|
|
82
71
|
|
|
83
72
|
replace(newRoute: Route) {
|
|
@@ -89,8 +78,9 @@ export class Route extends Component<Props> {
|
|
|
89
78
|
}
|
|
90
79
|
|
|
91
80
|
attach() {
|
|
81
|
+
const { domParent } = this.props;
|
|
92
82
|
if (this.isDetached) {
|
|
93
|
-
attachNode(this.domRef,
|
|
83
|
+
attachNode(this.domRef, domParent!);
|
|
94
84
|
this.isDetached = false;
|
|
95
85
|
}
|
|
96
86
|
}
|
|
@@ -114,14 +104,12 @@ export class Route extends Component<Props> {
|
|
|
114
104
|
}
|
|
115
105
|
}
|
|
116
106
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
if (!isDetachable) this.domRef.current!.style.display = 'block';
|
|
107
|
+
showRoute() {
|
|
108
|
+
this.domRef.current!.style.display = 'block';
|
|
120
109
|
}
|
|
121
110
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
if (!isDetachable) this.domRef.current!.style.display = 'none';
|
|
111
|
+
hideRoute() {
|
|
112
|
+
this.domRef.current!.style.display = 'none';
|
|
125
113
|
}
|
|
126
114
|
|
|
127
115
|
private mountRoute() {
|
|
@@ -134,13 +122,8 @@ export class Route extends Component<Props> {
|
|
|
134
122
|
}
|
|
135
123
|
|
|
136
124
|
render() {
|
|
137
|
-
const { isDetachable } = this.props;
|
|
138
125
|
return (
|
|
139
|
-
<div
|
|
140
|
-
className="route"
|
|
141
|
-
style={!isDetachable ? { display: 'none' } : {}}
|
|
142
|
-
ref={this.domRef}
|
|
143
|
-
/>
|
|
126
|
+
<div className="route" ref={this.domRef} />
|
|
144
127
|
);
|
|
145
128
|
}
|
|
146
129
|
}
|
|
@@ -28,6 +28,8 @@ const filterNulls = (allChildren: (JSX.Element | null)[]) => (
|
|
|
28
28
|
export class Router extends Component<Props> {
|
|
29
29
|
paths: { [key: string]: RoutePath } = {};
|
|
30
30
|
|
|
31
|
+
routerDomRef = createRef<HTMLDivElement>();
|
|
32
|
+
|
|
31
33
|
static defaultProps = {
|
|
32
34
|
defaultPath: '/',
|
|
33
35
|
detachInactiveRoutes: true,
|
|
@@ -38,12 +40,14 @@ export class Router extends Component<Props> {
|
|
|
38
40
|
constructor(props: Props) {
|
|
39
41
|
super(props);
|
|
40
42
|
routerRef.current = this;
|
|
41
|
-
this.
|
|
43
|
+
const { children } = this.props;
|
|
44
|
+
this.createRefs(children);
|
|
42
45
|
}
|
|
43
46
|
|
|
44
47
|
componentDidMount() {
|
|
45
48
|
window.document.addEventListener('keydown', this.onKeyDown, false);
|
|
46
49
|
window.addEventListener('hashchange', this.onHashChange, false);
|
|
50
|
+
this.hideAllRoutes();
|
|
47
51
|
this.goToDefaultPath();
|
|
48
52
|
}
|
|
49
53
|
|
|
@@ -91,6 +95,8 @@ export class Router extends Component<Props> {
|
|
|
91
95
|
return activeRoutePath?.ref.current || null;
|
|
92
96
|
}
|
|
93
97
|
|
|
98
|
+
// TODO: refactor to reduce complexity
|
|
99
|
+
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
94
100
|
switchRoutes(
|
|
95
101
|
routePathToHide: RoutePath | null,
|
|
96
102
|
routePathToShow: RoutePath | null,
|
|
@@ -104,18 +110,31 @@ export class Router extends Component<Props> {
|
|
|
104
110
|
} else if (routePathToShow) {
|
|
105
111
|
routeToHide!.hide();
|
|
106
112
|
routePathToHide.isVisible = false;
|
|
107
|
-
if (detachInactiveRoutes)
|
|
113
|
+
if (detachInactiveRoutes) {
|
|
114
|
+
routeToHide!.replace(routeToShow!);
|
|
115
|
+
} else {
|
|
116
|
+
routeToHide!.hideRoute();
|
|
117
|
+
routeToShow!.showRoute();
|
|
118
|
+
}
|
|
108
119
|
routeToShow!.show();
|
|
109
120
|
routePathToShow.isVisible = true;
|
|
110
121
|
return;
|
|
111
122
|
} else {
|
|
112
123
|
routeToHide!.hide();
|
|
113
124
|
routePathToHide.isVisible = false;
|
|
114
|
-
if (detachInactiveRoutes)
|
|
125
|
+
if (detachInactiveRoutes) {
|
|
126
|
+
routeToHide!.detach();
|
|
127
|
+
} else {
|
|
128
|
+
routeToHide!.hideRoute();
|
|
129
|
+
}
|
|
115
130
|
}
|
|
116
131
|
}
|
|
117
132
|
if (routePathToShow) {
|
|
118
|
-
if (detachInactiveRoutes)
|
|
133
|
+
if (detachInactiveRoutes) {
|
|
134
|
+
routeToShow!.attach();
|
|
135
|
+
} else {
|
|
136
|
+
routeToShow!.showRoute();
|
|
137
|
+
}
|
|
119
138
|
routeToShow!.show();
|
|
120
139
|
routePathToShow.isVisible = true;
|
|
121
140
|
}
|
|
@@ -125,11 +144,10 @@ export class Router extends Component<Props> {
|
|
|
125
144
|
this.getActiveRoute()?.focus();
|
|
126
145
|
}
|
|
127
146
|
|
|
128
|
-
createRefs() {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
const { path } = child.props;
|
|
147
|
+
createRefs(routes: JSX.Element[]) {
|
|
148
|
+
forEach(routes, (route: JSX.Element) => {
|
|
149
|
+
if (route.type === Route) {
|
|
150
|
+
const { path } = route.props;
|
|
133
151
|
const { regexpPath, paramNames } = pathToRegExp(path);
|
|
134
152
|
this.paths[path] = {
|
|
135
153
|
ref: createRef<Route>(),
|
|
@@ -151,9 +169,28 @@ export class Router extends Component<Props> {
|
|
|
151
169
|
}
|
|
152
170
|
}
|
|
153
171
|
|
|
154
|
-
|
|
155
|
-
const {
|
|
156
|
-
|
|
172
|
+
hideAllRoutes() {
|
|
173
|
+
const { detachInactiveRoutes } = this.props;
|
|
174
|
+
forEach(Object.keys(this.paths), (key: string) => {
|
|
175
|
+
const route = this.paths[key];
|
|
176
|
+
if (!route.isVisible) {
|
|
177
|
+
if (detachInactiveRoutes) {
|
|
178
|
+
route.ref.current!.detach();
|
|
179
|
+
} else {
|
|
180
|
+
route.ref.current!.hideRoute();
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
appendRoutes(routes: JSX.Element[]) {
|
|
187
|
+
this.createRefs(routes);
|
|
188
|
+
this.appendMount(this.renderRoutes(routes), this.routerDomRef);
|
|
189
|
+
this.hideAllRoutes();
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
renderRoutes(routes: JSX.Element[]) {
|
|
193
|
+
return filterNulls(map(routes, (child: JSX.Element) => {
|
|
157
194
|
if (child.type === Route) {
|
|
158
195
|
const { path, Component: RouteComponent } = child.props;
|
|
159
196
|
const route = this.paths[path];
|
|
@@ -161,14 +198,23 @@ export class Router extends Component<Props> {
|
|
|
161
198
|
<Route
|
|
162
199
|
ref={route.ref}
|
|
163
200
|
path={path}
|
|
201
|
+
domParent={this.routerDomRef}
|
|
164
202
|
regexpPath={route.regexpPath}
|
|
165
203
|
Component={RouteComponent}
|
|
166
204
|
paramNames={route.paramNames}
|
|
167
|
-
isDetachable={detachInactiveRoutes}
|
|
168
205
|
/>
|
|
169
206
|
);
|
|
170
207
|
}
|
|
171
208
|
return null;
|
|
172
209
|
}));
|
|
173
210
|
}
|
|
211
|
+
|
|
212
|
+
render() {
|
|
213
|
+
const { children } = this.props;
|
|
214
|
+
return (
|
|
215
|
+
<div ref={this.routerDomRef} className="router">
|
|
216
|
+
{this.renderRoutes(children)}
|
|
217
|
+
</div>
|
|
218
|
+
);
|
|
219
|
+
}
|
|
174
220
|
}
|