@intuitionrobotics/thunderstorm
Version:
89 lines • 3.54 kB
JavaScript
/*
* Thunderstorm is a full web app framework!
*
* Typescript & Express backend infrastructure that natively runs on firebase function
* Typescript & React frontend infrastructure
*
* Copyright (C) 2020 Intuition Robotics
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { addItemToArray, BadImplementationException, Module } from "@intuitionrobotics/ts-common";
import * as React from "react";
import { defaultLinkNode, defaultNavLinkNode, defaultRouteNode, RoutePath } from "./route.js";
import { Redirect, Switch } from "react-router-dom";
import { BrowserHistoryModule } from "../HistoryModule.js";
class RoutingModule_Class extends Module {
routes = {};
ordinalRoutes = [];
createNavLinkNode;
createRouteNode;
createLinkNode;
constructor() {
super("RoutingModule");
this.createNavLinkNode = defaultNavLinkNode;
this.createLinkNode = defaultLinkNode;
this.createRouteNode = defaultRouteNode;
}
init() {
}
clearRoutes() {
for (const item of this.ordinalRoutes) {
delete this.routes[item];
}
this.ordinalRoutes.splice(0);
}
addRoute(key, route, component) {
const previousRoute = this.routes[key];
if (previousRoute)
throw new BadImplementationException(`Route key '${key}' MUST be unique!!\n Found two routes with matching key: '${route}' && '${previousRoute.path}'`);
addItemToArray(this.ordinalRoutes, key);
return this.routes[key] = new RoutePath(key, route, component);
}
getRoute(key) {
const route = this.routes[key];
if (!route)
throw new BadImplementationException(`No Route for key '${key}'... Did you forget to add it??`);
return route;
}
isRouteRegistered(key) {
return !!this.routes[key];
}
getPath(key) {
return this.getRoute(key).path;
}
goToRoute(key, params) {
const pathname = this.getPath(key);
const search = RoutePath.composeStringQuery(params);
BrowserHistoryModule.push({ pathname, search });
}
redirect(key) {
return React.createElement(Redirect, { to: RoutingModule.getPath(key) });
}
getMyRouteKey = () => Object.keys(this.routes).find(key => this.routes[key].path === BrowserHistoryModule.getCurrent().pathname);
// need to figure out how to create parameterized urls from this call !!
getNavLinks(keys) {
return keys.map(key => this.getRoute(key)).filter(route => route.visible && route.visible()).map(route => this.createNavLinkNode(route));
}
getNavLink(key) {
return this.createNavLinkNode(this.getRoute(key));
}
getLink(key) {
return this.createLinkNode(this.getRoute(key));
}
getRoutesMap(keys) {
return React.createElement(Switch, null, (keys || this.ordinalRoutes).map(key => this.createRouteNode(this.getRoute(key))));
}
}
export const RoutingModule = new RoutingModule_Class();
//# sourceMappingURL=RoutingModule.js.map