RoutingSystem.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _react = _interopRequireDefault(require("react"));
  7. var _navigo = _interopRequireDefault(require("navigo"));
  8. var _callbackjs = _interopRequireDefault(require("../helpers/callbackjs"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
  11. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  12. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  13. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  14. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  15. var RoutingSystem =
  16. /*#__PURE__*/
  17. function () {
  18. function RoutingSystem() {
  19. var _this = this;
  20. var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  21. _classCallCheck(this, RoutingSystem);
  22. this._root = props.root || window.location.protocol + "//" + window.location.host;
  23. this._useHash = !!props.useHash; // Defaults to: false
  24. this._hash = props.hash || '#'; // Defaults to: '#'
  25. this.router = new _navigo.default(this._root, this._useHash, this._hash);
  26. this.router.notFound(function () {
  27. return _this._notFound();
  28. });
  29. this.router.on(function () {
  30. return _this._home();
  31. });
  32. this.routes = {};
  33. this.routeNames = {};
  34. this.routesByName = {};
  35. this.mods = {};
  36. this.handlers = {};
  37. this.currentView = 'root';
  38. this.currentState = {};
  39. this.callbacks = (0, _callbackjs.default)('RoutingSystem');
  40. this.home = "/home";
  41. }
  42. _createClass(RoutingSystem, [{
  43. key: "setHome",
  44. value: function setHome(p) {
  45. this.home = p;
  46. }
  47. }, {
  48. key: "_notFound",
  49. value: function _notFound(params, query) {
  50. console.log("URL not found!", params, query);
  51. this.currentView = 'root';
  52. this._routeChanged();
  53. }
  54. }, {
  55. key: "_home",
  56. value: function _home() {
  57. // this.currentView = 'root';
  58. this.router.navigate(this.home);
  59. }
  60. }, {
  61. key: "_handler",
  62. value: function _handler(params, query, modid) {
  63. console.log("Handler", params, query);
  64. this.currentView = modid;
  65. this._routeChanged();
  66. return true;
  67. }
  68. }, {
  69. key: "onRouteChange",
  70. value: function onRouteChange(key, fn) {
  71. var regex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '\\w+';
  72. this.callbacks.add(key, fn, regex);
  73. }
  74. }, {
  75. key: "_routeChanged",
  76. value: function _routeChanged() {
  77. var route = this.getRoute();
  78. this.callbacks.match(route.url);
  79. }
  80. }, {
  81. key: "addRoute",
  82. value: function addRoute(route, modid, routeName) {
  83. var _this2 = this;
  84. if (this.mods[modid]) {} //solo route! // disabled
  85. //delete this.routes[this.mods[modid]];
  86. //this.router.off(this.handlers[route]);
  87. // if(this.routes[route])return;
  88. this.routeNames[route] = routeName;
  89. this.routesByName[routeName] = route;
  90. this.routes[route] = modid;
  91. this.mods[modid] = route;
  92. this.handlers[route] = function (p, q) {
  93. return _this2._handler(p, q, modid);
  94. };
  95. this.router.on(route, this.handlers[route]); // console.log("Route ", route, " added for " , modid);
  96. }
  97. }, {
  98. key: "removeRoute",
  99. value: function removeRoute(modid, router) {
  100. var route = router || this.mods[modid];
  101. console.log("Removed route", route, " from mod with id:", modid, " and route: ", route);
  102. this.router.off(this.handlers[route]);
  103. delete this.handlers[route];
  104. delete this.routes[route];
  105. delete this.routesByName[this.routeNames[route]];
  106. delete this.routeNames[route];
  107. delete this.mods[modid];
  108. }
  109. }, {
  110. key: "resolve",
  111. value: function resolve() {
  112. this.router.resolve();
  113. }
  114. }, {
  115. key: "go",
  116. value: function go(url) {
  117. var state = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  118. var replace = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  119. this.currentState = _objectSpread({}, state);
  120. if (replace) this.router.pause();
  121. this.router.navigate(url);
  122. if (replace) {
  123. this.router.resume();
  124. }
  125. }
  126. }, {
  127. key: "goName",
  128. value: function goName(name, state) {
  129. if (this.routesByName[name]) {
  130. this.router.navigate(this.routesByName[name]);
  131. return true;
  132. }
  133. return false;
  134. }
  135. }, {
  136. key: "getURL",
  137. value: function getURL(url) {
  138. var name = this.getNameURL(url);
  139. return name || url;
  140. }
  141. }, {
  142. key: "getNameURL",
  143. value: function getNameURL(name) {
  144. return this.routesByName[name];
  145. }
  146. }, {
  147. key: "getCurrentView",
  148. value: function getCurrentView() {
  149. return this.currentView;
  150. }
  151. }, {
  152. key: "getView",
  153. value: function getView(viewname) {
  154. return this.routes[viewname];
  155. }
  156. }, {
  157. key: "getRouteName",
  158. value: function getRouteName(id) {
  159. return this.routeNames[id];
  160. }
  161. }, {
  162. key: "getRoute",
  163. value: function getRoute() {
  164. return this.router.lastRouteResolved();
  165. }
  166. }, {
  167. key: "getRoutes",
  168. value: function getRoutes() {
  169. var _this3 = this;
  170. return Object.keys(this.routes).map(function (key) {
  171. return {
  172. route: key,
  173. id: _this3.routes[key],
  174. routeName: _this3.routeNames[key]
  175. };
  176. });
  177. }
  178. }, {
  179. key: "getRoutesRegs",
  180. value: function getRoutesRegs() {
  181. var res = [];
  182. for (var i in this.routes) {
  183. var url = i;
  184. var search = /:[a-z]*/;
  185. var parts = url.split(search);
  186. var reg = parts.join('[a-z0-9]+');
  187. var exp = new RegExp(reg);
  188. res.push({
  189. url: url,
  190. reg: exp,
  191. id: this.routes[i]
  192. });
  193. }
  194. return res;
  195. }
  196. }, {
  197. key: "clean",
  198. value: function clean() {
  199. var _this4 = this;
  200. this.routes = {};
  201. this.router.destroy();
  202. this.router = new _navigo.default(this._root, this._useHash, this._hash);
  203. this.router.notFound(function () {
  204. return _this4._notFound();
  205. });
  206. this.router.on(function () {
  207. return _this4._home();
  208. });
  209. this.routes = {};
  210. this.routeNames = {};
  211. this.routesByName = {};
  212. this.mods = {};
  213. this.handlers = {};
  214. }
  215. }, {
  216. key: "import",
  217. value: function _import(data) {
  218. for (var i in data.routes) {
  219. this.addRoute(i, data.routes[i], data.routeNames[i] || data.routes[i]);
  220. }
  221. }
  222. }, {
  223. key: "export",
  224. value: function _export() {
  225. return {
  226. routes: this.routes,
  227. routeNames: this.routeNames
  228. };
  229. }
  230. }]);
  231. return RoutingSystem;
  232. }();
  233. var _default = RoutingSystem;
  234. exports.default = _default;