From 41815a680d479012dc12fccc71e9c55b064da77d Mon Sep 17 00:00:00 2001 From: MattMo Date: Fri, 22 Apr 2022 00:43:36 -0700 Subject: [PATCH] Added keepQuery option to router navigate. --- ignite-html-router.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/ignite-html-router.js b/ignite-html-router.js index 97fbdae..344e744 100644 --- a/ignite-html-router.js +++ b/ignite-html-router.js @@ -86,14 +86,16 @@ IgniteTemplate.prototype.route = function(routes, showCallback = null, hideCallb * @param {String|IgniteProperty|Function} route The route to navigate to. * @param {Any|IgniteProperty|Function} data The data to pass along with this navigate, this only applies when not refreshing, default is null. * @param {Boolean|IgniteProperty|Function} refresh Whether or not to refresh the page, default is false. + * @param {Boolean|IgniteProperty|Function} keepQuery Whether or not to keep the current url query, default is false. * @returns {IgniteTemplate} This ignite template. */ -IgniteTemplate.prototype.navigate = function(route, data = null, refresh = false) { +IgniteTemplate.prototype.navigate = function(route, data = null, refresh = false, keepQuery = false) { return this.onClick(() => { Router.navigate( (route instanceof IgniteProperty ? route.value : (route instanceof Function ? route() : route)), (data instanceof IgniteProperty ? data.value : (data instanceof Function ? data() : data)), - (refresh instanceof IgniteProperty ? refresh.value : (refresh instanceof Function ? refresh() : refresh)) + (refresh instanceof IgniteProperty ? refresh.value : (refresh instanceof Function ? refresh() : refresh)), + (keepQuery instanceof IgniteProperty ? keepQuery.value : (keepQuery instanceof Function ? keepQuery() : keepQuery)) ); }); } @@ -274,8 +276,18 @@ class Router { * @param {String} route The route to navigate to. * @param {Any} data The data to pass along with this navigate, this only applies when not refreshing, default is null. * @param {Boolean} refresh Whether or not to refresh the page, default is false. + * @param {Boolean} keepQuery Whether or not to keep the current url query, default is false. */ - static navigate(route, data = null, refresh = false) { + static navigate(route, data = null, refresh = false, keepQuery = false) { + //If keepQuery is true then include the current query. + if (keepQuery) { + if (route.includes("?")) { + route = route.split("?")[0] + window.location.search; + } else { + route = route + window.location.search; + } + } + if (refresh) { if (Router.hashMode) { //In hash mode the route can't start with / or #, we have to handle it here.