From 1ed9f5c505fc61802542e8dc05415fe4a1d99198 Mon Sep 17 00:00:00 2001 From: Bradley Taylor Date: Thu, 23 Dec 2021 19:49:51 +0000 Subject: [PATCH] Preserve selected tab when navigating between pages. --- templates/CRM/common/TabHeader.js | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/templates/CRM/common/TabHeader.js b/templates/CRM/common/TabHeader.js index 24547f8de7..6c049a034c 100644 --- a/templates/CRM/common/TabHeader.js +++ b/templates/CRM/common/TabHeader.js @@ -16,6 +16,13 @@ CRM.alert(ts('Your changes in the %1 tab have not been saved.', {1: ui.oldTab.text()}), ts('Unsaved Changes'), 'warning'); } }) + .on('tabsactivate', function(e, ui) { + var tabId = ui.newTab.attr('id'); + if (tabId && tabId.length) { + tabId = tabId.slice(4); // Remove leading 'tab_' + history.replaceState(null, '', updateUrlParameter('selectedChild', tabId)); + } + }) .on('tabsbeforeload', function(e, ui) { // Use civicrm ajax wrappers rather than the default $.load if (!ui.panel.data("civiCrmSnippet")) { @@ -148,4 +155,31 @@ $panel.crmSnippet('destroy'); } }; + + /** + * Updates the query parameter in the page URL, + * or adds the parameter if its not currently there. + * + * @param {string} param + * @param {string} value + * @return void + */ + function updateUrlParameter(param, value) { + var newUrl, + newSearch, + href = window.location.href, + search = window.location.search; + if (search.indexOf('?' + param) !== -1 || search.indexOf('&' + param) !== -1 ) { + var regExp = new RegExp(param + "(.+?)(&|$)", "g"); + newSearch = search.replace(regExp, param + "=" + value + "$2"); + newUrl = href.replace(search, newSearch); + } else if (search.length) { + newSearch = search + '&' + param + "=" + value; + newUrl = href.replace(search, newSearch); + } else { + newSearch = '?' + param + "=" + value; + newUrl = location.protocol + '//' + location.hostname + location.pathname + newSearch + location.hash; + } + window.history.replaceState("", "", newUrl); + } })(CRM.$, CRM._); -- 2.25.1