"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireDefault(require("react")); var _navigo = _interopRequireDefault(require("navigo")); var _callbackjs = _interopRequireDefault(require("../helpers/callbackjs")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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; } 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; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } var RoutingSystem = /*#__PURE__*/ function () { function RoutingSystem() { var _this = this; var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; _classCallCheck(this, RoutingSystem); this._root = props.root || window.location.protocol + "//" + window.location.host; this._useHash = !!props.useHash; // Defaults to: false this._hash = props.hash || '#'; // Defaults to: '#' this.router = new _navigo.default(this._root, this._useHash, this._hash); this.router.notFound(function () { return _this._notFound(); }); this.router.on(function () { return _this._home(); }); this.routes = {}; this.routeNames = {}; this.routesByName = {}; this.mods = {}; this.handlers = {}; this.currentView = 'root'; this.currentState = {}; this.callbacks = (0, _callbackjs.default)('RoutingSystem'); this.home = "/home"; } _createClass(RoutingSystem, [{ key: "setHome", value: function setHome(p) { this.home = p; } }, { key: "_notFound", value: function _notFound(params, query) { console.log("URL not found!", params, query); this.currentView = 'root'; this._routeChanged(); } }, { key: "_home", value: function _home() { // this.currentView = 'root'; this.router.navigate(this.home); } }, { key: "_handler", value: function _handler(params, query, modid) { console.log("Handler", params, query); this.currentView = modid; this._routeChanged(); return true; } }, { key: "onRouteChange", value: function onRouteChange(key, fn) { var regex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '\\w+'; this.callbacks.add(key, fn, regex); } }, { key: "_routeChanged", value: function _routeChanged() { var route = this.getRoute(); this.callbacks.match(route.url); } }, { key: "addRoute", value: function addRoute(route, modid, routeName) { var _this2 = this; if (this.mods[modid]) {} //solo route! // disabled //delete this.routes[this.mods[modid]]; //this.router.off(this.handlers[route]); // if(this.routes[route])return; this.routeNames[route] = routeName; this.routesByName[routeName] = route; this.routes[route] = modid; this.mods[modid] = route; this.handlers[route] = function (p, q) { return _this2._handler(p, q, modid); }; this.router.on(route, this.handlers[route]); // console.log("Route ", route, " added for " , modid); } }, { key: "removeRoute", value: function removeRoute(modid, router) { var route = router || this.mods[modid]; console.log("Removed route", route, " from mod with id:", modid, " and route: ", route); this.router.off(this.handlers[route]); delete this.handlers[route]; delete this.routes[route]; delete this.routesByName[this.routeNames[route]]; delete this.routeNames[route]; delete this.mods[modid]; } }, { key: "resolve", value: function resolve() { this.router.resolve(); } }, { key: "go", value: function go(url) { var state = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var replace = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; this.currentState = _objectSpread({}, state); if (replace) this.router.pause(); this.router.navigate(url); if (replace) { this.router.resume(); } } }, { key: "goName", value: function goName(name, state) { if (this.routesByName[name]) { this.router.navigate(this.routesByName[name]); return true; } return false; } }, { key: "getURL", value: function getURL(url) { var name = this.getNameURL(url); return name || url; } }, { key: "getNameURL", value: function getNameURL(name) { return this.routesByName[name]; } }, { key: "getCurrentView", value: function getCurrentView() { return this.currentView; } }, { key: "getView", value: function getView(viewname) { return this.routes[viewname]; } }, { key: "getRouteName", value: function getRouteName(id) { return this.routeNames[id]; } }, { key: "getRoute", value: function getRoute() { return this.router.lastRouteResolved(); } }, { key: "getRoutes", value: function getRoutes() { var _this3 = this; return Object.keys(this.routes).map(function (key) { return { route: key, id: _this3.routes[key], routeName: _this3.routeNames[key] }; }); } }, { key: "getRoutesRegs", value: function getRoutesRegs() { var res = []; for (var i in this.routes) { var url = i; var search = /:[a-z]*/; var parts = url.split(search); var reg = parts.join('[a-z0-9]+'); var exp = new RegExp(reg); res.push({ url: url, reg: exp, id: this.routes[i] }); } return res; } }, { key: "clean", value: function clean() { var _this4 = this; this.routes = {}; this.router.destroy(); this.router = new _navigo.default(this._root, this._useHash, this._hash); this.router.notFound(function () { return _this4._notFound(); }); this.router.on(function () { return _this4._home(); }); this.routes = {}; this.routeNames = {}; this.routesByName = {}; this.mods = {}; this.handlers = {}; } }, { key: "import", value: function _import(data) { for (var i in data.routes) { this.addRoute(i, data.routes[i], data.routeNames[i] || data.routes[i]); } } }, { key: "export", value: function _export() { return { routes: this.routes, routeNames: this.routeNames }; } }]); return RoutingSystem; }(); var _default = RoutingSystem; exports.default = _default;