3
0

RoutingSystem.js 4.2 KB

1
  1. var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.default=exports.RouteNode=void 0;var _toConsumableArray2=_interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));var _classCallCheck2=_interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));var _createClass2=_interopRequireDefault(require("@babel/runtime/helpers/createClass"));var _possibleConstructorReturn2=_interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));var _getPrototypeOf2=_interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));var _inherits2=_interopRequireDefault(require("@babel/runtime/helpers/inherits"));var _react=_interopRequireDefault(require("react"));var _graph=require("../helpers/graph");var _tree=require("../helpers/tree");var _ViewSystem=_interopRequireDefault(require("./ViewSystem"));function log(){var _console;(_console=console).log.apply(_console,arguments);}var RouteNode=function(_Node){(0,_inherits2.default)(RouteNode,_Node);function RouteNode(routeName,uri){var _this;var view=arguments.length>2&&arguments[2]!==undefined?arguments[2]:null;var defaultLink=arguments.length>3&&arguments[3]!==undefined?arguments[3]:null;(0,_classCallCheck2.default)(this,RouteNode);_this=(0,_possibleConstructorReturn2.default)(this,(0,_getPrototypeOf2.default)(RouteNode).call(this,uri));_this.routeName=routeName;_this.uri=uri;_this.view=view;_this.defaultLink=defaultLink;return _this;}(0,_createClass2.default)(RouteNode,[{key:"hasView",value:function hasView(){return!!this.view;}},{key:"getDefaultLink",value:function getDefaultLink(){return this.defaultLink;}}]);return RouteNode;}(_graph.Node);exports.RouteNode=RouteNode;var RoutingSystem=function(){function RoutingSystem(){(0,_classCallCheck2.default)(this,RoutingSystem);this.topology=new _graph.Graph();this.currentNode=null;this.homeNode=null;}(0,_createClass2.default)(RoutingSystem,[{key:"setHome",value:function setHome(home){this.homeNode=home;this.currentNode=this.homeNode;return this;}},{key:"addRouteNode",value:function addRouteNode(node){var parent=arguments.length>1&&arguments[1]!==undefined?arguments[1]:this.rootNode;this.topology.addNode(node);this.topology.linkNodes(node,parent);return this;}},{key:"addRoute",value:function addRoute(route,routeName){var acc="";var nodeNames=route.split('.').map(function(item,index){acc=[].concat((0,_toConsumableArray2.default)(acc),[item]);return acc.join('.');});var i=0;while(i<nodeNames.length){if(!this.topology.has(nodeNames[i])){var temp=new RouteNode(routeName,nodeNames[i]);this.topology.addNode(temp);if(i>0){var parent=this.topology.getNode(nodeNames[i-1]);var link=this.topology.linkNodes(parent,temp);parent.defaultLink=link;}}else{log(" HAS ALREADY ",nodeNames[i]);}i++;}return this;}},{key:"goTo",value:function goTo(route){return this.currentNode=route;}},{key:"setView",value:function setView(route,view){var node=this.topology.getNode(route);node.view=view;return this;}},{key:"setDefaultLink",value:function setDefaultLink(route,dest){var node=route;var dst=dest;var links=this.topology.getLinks(route).out;var link=new _graph.Link(node,dst);if(!links.has(link)){throw new Error("There is no link from "+route+" to "+dest);}node.defaultLink=link;return this;}},{key:"getView",value:function getView(route){var node=this.topology.getNode(route);return node.view;}},{key:"getCurrentView",value:function getCurrentView(){return this.topology.getNode(this.currentNode).view;}},{key:"removeRoute",value:function removeRoute(route){var temp=this.topology.getNode(route);this.topology.removeNode(temp);return temp;}},{key:"print",value:function print(){log("Printing Routes: ");for(var node in this.topology.nodes){log("Node: ",node);var outSet=this.topology.getLinks(node).out;outSet.forEach(function(link){return log("Link to : ",link.to.id);});}}},{key:"export",value:function _export(){var graph=this.topology.export();return{topology:graph,home:this.homeNode};}},{key:"import",value:function _import(data){var topology=data.topology,home=data.home;try{this.topology.import(topology,RouteNode);this.setHome(home);}catch(e){console.log(e);throw new Error("RS Import Failed: ");}return this;}}]);return RoutingSystem;}();exports.default=RoutingSystem;