RoutingSystem.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import React from 'react';
  2. import {Node, Link, Graph} from '../helpers/graph'
  3. import {TreeNode, Tree} from '../helpers/tree'
  4. function log(...m) {
  5. console.log(...m);
  6. }
  7. export class RouteNode extends Node {
  8. constructor(routeName, uri) {
  9. super(uri);
  10. this.routeName = routeName;
  11. this.uri = uri;
  12. this.view = null;
  13. this.defaultLink = null; // Goes to if no view is specified
  14. }
  15. hasView() {
  16. return !!this.view;
  17. }
  18. getDefaultLink() {
  19. return this.defaultLink;
  20. }
  21. }
  22. export default class RoutingSystem {
  23. constructor() {
  24. this.topology = new Graph();
  25. this.currentNode = null;
  26. }
  27. setHome(home) {
  28. this.currentNode = this.topology.getNode(home);
  29. return this;
  30. }
  31. addRouteNode(node, parent = this.rootNode) {
  32. this.topology.addNode(node);
  33. this.topology.linkNodes(node, parent);
  34. return this;
  35. }
  36. addRoute(route, routeName) {
  37. let acc = "";
  38. let nodeNames = route.split('.')
  39. .map((item, index) => {
  40. acc = [...acc, item];
  41. return acc.join('.');
  42. });
  43. log("nodeNames");
  44. log(nodeNames);
  45. let i = 0;
  46. while (i < nodeNames.length) {
  47. log("Checking ", nodeNames[i]);
  48. if (!this.topology.has(nodeNames[i])) {
  49. log("Creating ", nodeNames[i]);
  50. let temp = new RouteNode(routeName, nodeNames[i]);
  51. this.topology.addNode(temp);
  52. if(i>0) {
  53. let parent = this.topology.getNode(nodeNames[i-1]);
  54. let link = this.topology.linkNodes(parent, temp);
  55. parent.defaultLink = link;
  56. }
  57. } else {
  58. log(" HAS ALREADY ", nodeNames[i]);
  59. }
  60. i++;
  61. }
  62. return this;
  63. }
  64. goTo(route) {
  65. return this.currentNode = this.topology.getNode(route);
  66. }
  67. setView(route, view) {
  68. let node = this.topology.getNode(route);
  69. node.view = view;
  70. return this;
  71. }
  72. setDefaultLink(route, dest) {
  73. let node = this.topology.getNode(route);
  74. let dst = this.topology.getNode(dest);
  75. let links= this.topology.getLinks(route).out;
  76. let link = new Link(node, dst);
  77. if(!links.has(link)) {
  78. throw new Error(`There is no link from ${route} to ${dest}`);
  79. }
  80. node.defaultLink = link;
  81. return this;
  82. }
  83. getView(route) {
  84. let node = this.topology.getNode(route);
  85. return node.view;
  86. }
  87. getCurrentView() {
  88. return this.currentNode.view;
  89. }
  90. removeRoute(route) {
  91. let temp = this.topology.getNode(route);
  92. this.topology.removeNode(temp);
  93. return temp;
  94. }
  95. print() {
  96. log("Printing Routes: ");
  97. for(let node in this.topology.nodes) {
  98. log("Node: ", node)
  99. let outSet = this.topology.getLinks(node).out;
  100. outSet.forEach( link => log("Link to : ", link.to.id) );
  101. }
  102. }
  103. }