RoutingSystem.js 2.9 KB

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