@intuitionrobotics/thunderstorm
Version:
89 lines • 3.68 kB
JavaScript
"use strict";
/*
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.RoutingModule = void 0;
const ts_common_1 = require("@intuitionrobotics/ts-common");
const React = require("react");
const route_1 = require("./route");
const react_router_dom_1 = require("react-router-dom");
const HistoryModule_1 = require("../HistoryModule");
class RoutingModule_Class extends ts_common_1.Module {
constructor() {
super("RoutingModule");
this.routes = {};
this.ordinalRoutes = [];
this.getMyRouteKey = () => Object.keys(this.routes).find(key => this.routes[key].path === HistoryModule_1.BrowserHistoryModule.getCurrent().pathname);
this.createNavLinkNode = route_1.defaultNavLinkNode;
this.createLinkNode = route_1.defaultLinkNode;
this.createRouteNode = route_1.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 ts_common_1.BadImplementationException(`Route key '${key}' MUST be unique!!\n Found two routes with matching key: '${route}' && '${previousRoute.path}'`);
(0, ts_common_1.addItemToArray)(this.ordinalRoutes, key);
return this.routes[key] = new route_1.RoutePath(key, route, component);
}
getRoute(key) {
const route = this.routes[key];
if (!route)
throw new ts_common_1.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 = route_1.RoutePath.composeStringQuery(params);
HistoryModule_1.BrowserHistoryModule.push({ pathname, search });
}
redirect(key) {
return React.createElement(react_router_dom_1.Redirect, { to: exports.RoutingModule.getPath(key) });
}
// 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(react_router_dom_1.Switch, null, (keys || this.ordinalRoutes).map(key => this.createRouteNode(this.getRoute(key))));
}
}
exports.RoutingModule = new RoutingModule_Class();
//# sourceMappingURL=RoutingModule.js.map