diff --git a/lib/components/Link.js b/lib/components/Link.js index f866800..816cb62 100644 --- a/lib/components/Link.js +++ b/lib/components/Link.js @@ -125,14 +125,15 @@ function (_React$Component) { children = _this$props2.children, routerStore = _this$props2.routerStore, activeClassName = _this$props2.activeClassName, - to = _this$props2.to, + _this$props2$to = _this$props2.to, + to = _this$props2$to === void 0 ? '' : _this$props2$to, props = _objectWithoutProperties(_this$props2, ["params", "queryParams", "refresh", "children", "routerStore", "activeClassName", "to"]); if (!routerStore) { return null; } - var href = (0, _generateUrl.routerStateToUrl)(routerStore, this.toState); + var href = to.substring(0, 1) === '#' ? to : (0, _generateUrl.routerStateToUrl)(routerStore, this.toState); if (activeClassName) { var index = href.indexOf('?'); diff --git a/lib/generate-url.js b/lib/generate-url.js index 0c298fe..9f287dd 100644 --- a/lib/generate-url.js +++ b/lib/generate-url.js @@ -84,8 +84,8 @@ var routerStateToUrl = function routerStateToUrl(routerStore, toState) { return generateUrl(route.path.pattern, params, queryParams); } catch (e) { - console.error('Missing parameter on route ', '\'' + toState.routeName + '\'', "\n", 'Original Error: ', e.message); - return '/#(missing param on route ' + toState.routeName + ')'; + console.error('Missing parameter for route ', '\'' + toState.routeName + '\'', "\n", 'Original Error: ', e.message); + return '/#(missing param for route ' + toState.routeName + ')'; } }; diff --git a/lib/start-router.js b/lib/start-router.js index d1b11c1..53a3e1d 100644 --- a/lib/start-router.js +++ b/lib/start-router.js @@ -36,7 +36,9 @@ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) r var startRouter = function startRouter(views, rootStore) { var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}, resources = _ref.resources, - config = _objectWithoutProperties(_ref, ["resources"]); + _ref$runAllEvents = _ref.runAllEvents, + runAllEvents = _ref$runAllEvents === void 0 ? false : _ref$runAllEvents, + config = _objectWithoutProperties(_ref, ["resources", "runAllEvents"]); var store = new _routerStore.RouterStore(); typeof rootStore === 'function' ? rootStore = rootStore(store) : rootStore.routerStore = store; @@ -138,17 +140,20 @@ var startRouter = function startRouter(views, rootStore) { } } - newPath = newPath.concat((0, _utils.buildLookupPath)(match.route)); // add routes from previous path for onExit event trigger + newPath = newPath.concat((0, _utils.buildLookupPath)(match.route)); + newPath = _toConsumableArray(new Set(newPath)); // remove duplicates + // add routes from previous path for onExit event trigger var oldPath = (0, _utils.buildLookupPath)(store.currentRoute).reverse().filter(function (route) { return route.isActive && !newPath.includes(route); - }); // filter all inactive routes from newPath except last one - // TODO there should be check if route params changed + }); + + if (!runAllEvents) { + newPath = newPath.filter(function (route, i) { + return !route.isActive || i === newPath.length - 1 && route === store.currentRoute; + }); + } // build params - newPath = _toConsumableArray(new Set(newPath)); - newPath = newPath.filter(function (route, i) { - return !route.isActive || i === newPath.length - 1 && route === store.currentRoute; - }); // build params var pathParams = newPath.reduce(function (obj, route) { return _objectSpread({}, route.defaultParams, {}, obj); @@ -168,8 +173,8 @@ var startRouter = function startRouter(views, rootStore) { var _loop = function _loop(_i) { var route = newPath[_i]; - fns = fns.concat((0, _utils.buildFnsArray)(route.beforeEnter, function (params, rootStore) { - store.onMatch(params, rootStore, route); + fns = fns.concat((0, _utils.buildFnsArray)(route.beforeEnter, runAllEvents && route.isActive && newPath.length - 1 !== _i || function (params, rootStore) { + return void store.onMatch(params, rootStore, route); })); }; diff --git a/package-lock.json b/package-lock.json index f07046d..3a5f39b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "damidev-mobx-router", - "version": "1.1.1", + "version": "1.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -3295,9 +3295,9 @@ "dev": true }, "path-to-regexp": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-3.1.0.tgz", - "integrity": "sha512-PtHLisEvUOepjc+sStXxJ/pDV/s5UBTOKWJY2SOz3e6E/iN/jLknY9WL72kTwRrwXDUbZTEAtSnJbz2fF127DA==" + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-3.2.0.tgz", + "integrity": "sha512-jczvQbCUS7XmS7o+y1aEO9OBVFeZBQ1MDSEqmO7xSoPgOPoowY/SxLpZ6Vh97/8qHZOteiCKb7gkG9gA2ZUxJA==" }, "posix-character-classes": { "version": "0.1.1", @@ -3331,9 +3331,9 @@ } }, "query-string": { - "version": "6.8.3", - "resolved": "https://registry.npmjs.org/query-string/-/query-string-6.8.3.tgz", - "integrity": "sha512-llcxWccnyaWlODe7A9hRjkvdCKamEKTh+wH8ITdTc3OhchaqUZteiSCX/2ablWHVrkVIe04dntnaZJ7BdyW0lQ==", + "version": "6.9.0", + "resolved": "https://registry.npmjs.org/query-string/-/query-string-6.9.0.tgz", + "integrity": "sha512-KG4bhCFYapExLsUHrFt+kQVEegF2agm4cpF/VNc6pZVthIfCc/GK8t8VyNIE3nyXG9DK3Tf2EGkxjR6/uRdYsA==", "requires": { "decode-uri-component": "^0.2.0", "split-on-first": "^1.0.0", diff --git a/package.json b/package.json index ffcdf7e..7e672d2 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "damidev-mobx-router", "description": "MobX powered router for React apps", - "version": "1.1.1", + "version": "1.2.0", "author": "Jakub Hrášek ", "license": "MIT", "bugs": { @@ -28,8 +28,8 @@ "url": "https://github.com/damidevelopment/mobx-router.git" }, "dependencies": { - "path-to-regexp": "^3.1.0", - "query-string": "^6.8.3" + "path-to-regexp": "^3.2.0", + "query-string": "^6.9.0" }, "peerDependencies": { "history": "^4.0.0",