diff --git a/templates/CRM/common/TabHeader.js b/templates/CRM/common/TabHeader.js index 24547f8de7e9..7d4cf01c0bfd 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,25 @@ $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 href = window.location.href; + if (href.indexOf('?' + param) !== -1 || href.indexOf('&' + param) !== -1 ) { + var regExp = new RegExp(param + "(.+?)(&|$)", "g"); + var newUrl = href.replace(regExp, param + "=" + value + "$2"); + } else if (href.indexOf('?' + param) !== -1) { + var newUrl = href + '&' + param + "=" + value; + } else { + var newUrl = href + '?' + param + "=" + value; + } + window.history.pushState("", "", newUrl); + } })(CRM.$, CRM._);