Add router level support for rendering react componenent pages

This commit is contained in:
Bill Thornton 2021-05-27 15:34:27 -04:00
parent 4aa203c0c6
commit eb605615d1
6 changed files with 34 additions and 3 deletions

View File

@ -11,6 +11,7 @@ import viewManager from './viewManager/viewManager';
import Dashboard from '../scripts/clientUtils'; import Dashboard from '../scripts/clientUtils';
import ServerConnections from './ServerConnections'; import ServerConnections from './ServerConnections';
import alert from './alert'; import alert from './alert';
import reactControllerFactory from './reactControllerFactory';
class AppRouter { class AppRouter {
allRoutes = []; allRoutes = [];
@ -341,7 +342,9 @@ class AppRouter {
this.sendRouteToViewManager(ctx, next, route, controllerFactory); this.sendRouteToViewManager(ctx, next, route, controllerFactory);
}; };
if (route.controller) { if (route.pageComponent) {
onInitComplete(reactControllerFactory);
} else if (route.controller) {
import('../controllers/' + route.controller).then(onInitComplete); import('../controllers/' + route.controller).then(onInitComplete);
} else { } else {
onInitComplete(); onInitComplete();
@ -373,6 +376,7 @@ class AppRouter {
fullscreen: route.fullscreen, fullscreen: route.fullscreen,
controllerFactory: controllerFactory, controllerFactory: controllerFactory,
options: { options: {
pageComponent: route.pageComponent,
supportsThemeMedia: route.supportsThemeMedia || false, supportsThemeMedia: route.supportsThemeMedia || false,
enableMediaControl: route.enableMediaControl !== false enableMediaControl: route.enableMediaControl !== false
}, },

View File

@ -0,0 +1,7 @@
import React from 'react';
const TestPage = () => (
<h1>Hello from React!</h1>
);
export default TestPage;

View File

@ -0,0 +1,13 @@
import React from 'react';
import ReactDOM from 'react-dom';
// TODO: Probably need to rehydrate on view restores
export default (view, params, { detail }) => {
if (detail.options?.pageComponent) {
import(/* webpackChunkName: "[request]" */ `./pages/${detail.options.pageComponent}`)
.then(({ default: component }) => {
ReactDOM.render(React.createElement(component), view);
});
}
};

View File

@ -21,9 +21,9 @@ viewContainer.setOnBeforeChange(function (newView, isRestored, options) {
newView.initComplete = true; newView.initComplete = true;
if (typeof options.controllerFactory === 'function') { if (typeof options.controllerFactory === 'function') {
new options.controllerFactory(newView, eventDetail.detail.params); new options.controllerFactory(newView, eventDetail.detail.params, eventDetail);
} else if (options.controllerFactory && typeof options.controllerFactory.default === 'function') { } else if (options.controllerFactory && typeof options.controllerFactory.default === 'function') {
new options.controllerFactory.default(newView, eventDetail.detail.params); new options.controllerFactory.default(newView, eventDetail.detail.params, eventDetail);
} }
if (!options.controllerFactory || dispatchPageEvents) { if (!options.controllerFactory || dispatchPageEvents) {

View File

@ -0,0 +1 @@
<div id="react-test" data-role="page" class="page standalonePage"></div>

View File

@ -24,6 +24,12 @@ import { appRouter } from '../components/appRouter';
appRouter.addRoute(path, newRoute); appRouter.addRoute(path, newRoute);
} }
defineRoute({
alias: '/reactTest.html',
path: 'reactTest.html',
pageComponent: 'TestPage'
});
defineRoute({ defineRoute({
alias: '/addserver.html', alias: '/addserver.html',
path: 'session/addServer/index.html', path: 'session/addServer/index.html',