123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- import React from 'react';
- import {Node, Link, Graph} from '../helpers/graph'
- import {TreeNode, Tree} from '../helpers/tree'
- function log(...m) {
- console.log(...m);
- }
- export class RouteNode extends Node {
- constructor(routeName, uri) {
- super(uri);
- this.routeName = routeName;
- this.uri = uri;
- this.view = null;
- this.defaultLink = null; // Goes to if no view is specified
- }
- hasView() {
- return !!this.view;
- }
- getDefaultLink() {
- return this.defaultLink;
- }
- }
- export default class RoutingSystem {
- constructor() {
- this.topology = new Graph();
- this.currentNode = null;
- }
- setHome(home) {
- this.currentNode = this.topology.getNode(home);
- return this;
- }
- addRouteNode(node, parent = this.rootNode) {
- this.topology.addNode(node);
- this.topology.linkNodes(node, parent);
- return this;
- }
- addRoute(route, routeName) {
- let acc = "";
- let nodeNames = route.split('.')
- .map((item, index) => {
- acc = [...acc, item];
- return acc.join('.');
- });
- log("nodeNames");
- log(nodeNames);
- let i = 0;
- while (i < nodeNames.length) {
- log("Checking ", nodeNames[i]);
- if (!this.topology.has(nodeNames[i])) {
- log("Creating ", nodeNames[i]);
- let temp = new RouteNode(routeName, nodeNames[i]);
- this.topology.addNode(temp);
- if(i>0) {
- let parent = this.topology.getNode(nodeNames[i-1]);
- let link = this.topology.linkNodes(parent, temp);
- parent.defaultLink = link;
- }
- } else {
- log(" HAS ALREADY ", nodeNames[i]);
- }
- i++;
- }
- return this;
- }
- goTo(route) {
- return this.currentNode = this.topology.getNode(route);
- }
- setView(route, view) {
- let node = this.topology.getNode(route);
- node.view = view;
- return this;
- }
- setDefaultLink(route, dest) {
- let node = this.topology.getNode(route);
- let dst = this.topology.getNode(dest);
- let links= this.topology.getLinks(route).out;
- let link = new Link(node, dst);
- if(!links.has(link)) {
- throw new Error(`There is no link from ${route} to ${dest}`);
- }
- node.defaultLink = link;
- return this;
- }
- getView(route) {
- let node = this.topology.getNode(route);
- return node.view;
- }
- getCurrentView() {
- return this.currentNode.view;
- }
- removeRoute(route) {
- let temp = this.topology.getNode(route);
- this.topology.removeNode(temp);
- return temp;
- }
- print() {
- log("Printing Routes: ");
- for(let node in this.topology.nodes) {
- log("Node: ", node)
- let outSet = this.topology.getLinks(node).out;
- outSet.forEach( link => log("Link to : ", link.to.id) );
- }
- }
- }
|